OSDN Git Service

2008-08-22 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_attr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ A T T R                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Einfo;    use Einfo;
30 with Elists;   use Elists;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch2;  use Exp_Ch2;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Ch6;  use Exp_Ch6;
35 with Exp_Ch9;  use Exp_Ch9;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Imgv; use Exp_Imgv;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Strm; use Exp_Strm;
40 with Exp_Tss;  use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Exp_VFpt; use Exp_VFpt;
43 with Fname;    use Fname;
44 with Freeze;   use Freeze;
45 with Gnatvsn;  use Gnatvsn;
46 with Itypes;   use Itypes;
47 with Lib;      use Lib;
48 with Namet;    use Namet;
49 with Nmake;    use Nmake;
50 with Nlists;   use Nlists;
51 with Opt;      use Opt;
52 with Restrict; use Restrict;
53 with Rident;   use Rident;
54 with Rtsfind;  use Rtsfind;
55 with Sem;      use Sem;
56 with Sem_Ch6;  use Sem_Ch6;
57 with Sem_Ch7;  use Sem_Ch7;
58 with Sem_Ch8;  use Sem_Ch8;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res;  use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo;    use Sinfo;
63 with Snames;   use Snames;
64 with Stand;    use Stand;
65 with Stringt;  use Stringt;
66 with Targparm; use Targparm;
67 with Tbuild;   use Tbuild;
68 with Ttypes;   use Ttypes;
69 with Uintp;    use Uintp;
70 with Uname;    use Uname;
71 with Validsw;  use Validsw;
72
73 package body Exp_Attr is
74
75    -----------------------
76    -- Local Subprograms --
77    -----------------------
78
79    procedure Compile_Stream_Body_In_Scope
80      (N     : Node_Id;
81       Decl  : Node_Id;
82       Arr   : Entity_Id;
83       Check : Boolean);
84    --  The body for a stream subprogram may be generated outside of the scope
85    --  of the type. If the type is fully private, it may depend on the full
86    --  view of other types (e.g. indices) that are currently private as well.
87    --  We install the declarations of the package in which the type is declared
88    --  before compiling the body in what is its proper environment. The Check
89    --  parameter indicates if checks are to be suppressed for the stream body.
90    --  We suppress checks for array/record reads, since the rule is that these
91    --  are like assignments, out of range values due to uninitialized storage,
92    --  or other invalid values do NOT cause a Constraint_Error to be raised.
93
94    procedure Expand_Access_To_Protected_Op
95      (N    : Node_Id;
96       Pref : Node_Id;
97       Typ  : Entity_Id);
98
99    --  An attribute reference to a protected subprogram is transformed into
100    --  a pair of pointers: one to the object, and one to the operations.
101    --  This expansion is performed for 'Access and for 'Unrestricted_Access.
102
103    procedure Expand_Fpt_Attribute
104      (N    : Node_Id;
105       Pkg  : RE_Id;
106       Nam  : Name_Id;
107       Args : List_Id);
108    --  This procedure expands a call to a floating-point attribute function.
109    --  N is the attribute reference node, and Args is a list of arguments to
110    --  be passed to the function call. Pkg identifies the package containing
111    --  the appropriate instantiation of System.Fat_Gen. Float arguments in Args
112    --  have already been converted to the floating-point type for which Pkg was
113    --  instantiated. The Nam argument is the relevant attribute processing
114    --  routine to be called. This is the same as the attribute name, except in
115    --  the Unaligned_Valid case.
116
117    procedure Expand_Fpt_Attribute_R (N : Node_Id);
118    --  This procedure expands a call to a floating-point attribute function
119    --  that takes a single floating-point argument. The function to be called
120    --  is always the same as the attribute name.
121
122    procedure Expand_Fpt_Attribute_RI (N : Node_Id);
123    --  This procedure expands a call to a floating-point attribute function
124    --  that takes one floating-point argument and one integer argument. The
125    --  function to be called is always the same as the attribute name.
126
127    procedure Expand_Fpt_Attribute_RR (N : Node_Id);
128    --  This procedure expands a call to a floating-point attribute function
129    --  that takes two floating-point arguments. The function to be called
130    --  is always the same as the attribute name.
131
132    procedure Expand_Pred_Succ (N : Node_Id);
133    --  Handles expansion of Pred or Succ attributes for case of non-real
134    --  operand with overflow checking required.
135
136    function Get_Index_Subtype (N : Node_Id) return Entity_Id;
137    --  Used for Last, Last, and Length, when the prefix is an array type.
138    --  Obtains the corresponding index subtype.
139
140    procedure Find_Fat_Info
141      (T        : Entity_Id;
142       Fat_Type : out Entity_Id;
143       Fat_Pkg  : out RE_Id);
144    --  Given a floating-point type T, identifies the package containing the
145    --  attributes for this type (returned in Fat_Pkg), and the corresponding
146    --  type for which this package was instantiated from Fat_Gen. Error if T
147    --  is not a floating-point type.
148
149    function Find_Stream_Subprogram
150      (Typ : Entity_Id;
151       Nam : TSS_Name_Type) return Entity_Id;
152    --  Returns the stream-oriented subprogram attribute for Typ. For tagged
153    --  types, the corresponding primitive operation is looked up, else the
154    --  appropriate TSS from the type itself, or from its closest ancestor
155    --  defining it, is returned. In both cases, inheritance of representation
156    --  aspects is thus taken into account.
157
158    function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
159    --  Given a type, find a corresponding stream convert pragma that applies to
160    --  the implementation base type of this type (Typ). If found, return the
161    --  pragma node, otherwise return Empty if no pragma is found.
162
163    function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
164    --  Utility for array attributes, returns true on packed constrained
165    --  arrays, and on access to same.
166
167    function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
168    --  Returns true iff the given node refers to an attribute call that
169    --  can be expanded directly by the back end and does not need front end
170    --  expansion. Typically used for rounding and truncation attributes that
171    --  appear directly inside a conversion to integer.
172
173    ----------------------------------
174    -- Compile_Stream_Body_In_Scope --
175    ----------------------------------
176
177    procedure Compile_Stream_Body_In_Scope
178      (N     : Node_Id;
179       Decl  : Node_Id;
180       Arr   : Entity_Id;
181       Check : Boolean)
182    is
183       Installed : Boolean := False;
184       Scop      : constant Entity_Id := Scope (Arr);
185       Curr      : constant Entity_Id := Current_Scope;
186
187    begin
188       if Is_Hidden (Arr)
189         and then not In_Open_Scopes (Scop)
190         and then Ekind (Scop) = E_Package
191       then
192          Push_Scope (Scop);
193          Install_Visible_Declarations (Scop);
194          Install_Private_Declarations (Scop);
195          Installed := True;
196
197          --  The entities in the package are now visible, but the generated
198          --  stream entity must appear in the current scope (usually an
199          --  enclosing stream function) so that itypes all have their proper
200          --  scopes.
201
202          Push_Scope (Curr);
203       end if;
204
205       if Check then
206          Insert_Action (N, Decl);
207       else
208          Insert_Action (N, Decl, Suppress => All_Checks);
209       end if;
210
211       if Installed then
212
213          --  Remove extra copy of current scope, and package itself
214
215          Pop_Scope;
216          End_Package_Scope (Scop);
217       end if;
218    end Compile_Stream_Body_In_Scope;
219
220    -----------------------------------
221    -- Expand_Access_To_Protected_Op --
222    -----------------------------------
223
224    procedure Expand_Access_To_Protected_Op
225      (N    : Node_Id;
226       Pref : Node_Id;
227       Typ  : Entity_Id)
228    is
229       --  The value of the attribute_reference is a record containing two
230       --  fields: an access to the protected object, and an access to the
231       --  subprogram itself. The prefix is a selected component.
232
233       Loc     : constant Source_Ptr := Sloc (N);
234       Agg     : Node_Id;
235       Btyp    : constant Entity_Id := Base_Type (Typ);
236       Sub     : Entity_Id;
237       E_T     : constant Entity_Id := Equivalent_Type (Btyp);
238       Acc     : constant Entity_Id :=
239                   Etype (Next_Component (First_Component (E_T)));
240       Obj_Ref : Node_Id;
241       Curr    : Entity_Id;
242
243       function May_Be_External_Call return Boolean;
244       --  If the 'Access is to a local operation, but appears in a context
245       --  where it may lead to a call from outside the object, we must treat
246       --  this as an external call. Clearly we cannot tell without full
247       --  flow analysis, and a subsequent call that uses this 'Access may
248       --  lead to a bounded error (trying to seize locks twice, e.g.). For
249       --  now we treat 'Access as a potential external call if it is an actual
250       --  in a call to an outside subprogram.
251
252       --------------------------
253       -- May_Be_External_Call --
254       --------------------------
255
256       function May_Be_External_Call return Boolean is
257          Subp : Entity_Id;
258          Par  : Node_Id := Parent (N);
259
260       begin
261          --  Account for the case where the Access attribute is part of a
262          --  named parameter association.
263
264          if Nkind (Par) = N_Parameter_Association then
265             Par := Parent (Par);
266          end if;
267
268          if Nkind_In (Par, N_Procedure_Call_Statement, N_Function_Call)
269             and then Is_Entity_Name (Name (Par))
270          then
271             Subp := Entity (Name (Par));
272             return not In_Open_Scopes (Scope (Subp));
273          else
274             return False;
275          end if;
276       end May_Be_External_Call;
277
278    --  Start of processing for Expand_Access_To_Protected_Op
279
280    begin
281       --  Within the body of the protected type, the prefix
282       --  designates a local operation, and the object is the first
283       --  parameter of the corresponding protected body of the
284       --  current enclosing operation.
285
286       if Is_Entity_Name (Pref) then
287          if May_Be_External_Call then
288             Sub :=
289               New_Occurrence_Of
290                 (External_Subprogram (Entity (Pref)), Loc);
291          else
292             Sub :=
293               New_Occurrence_Of
294                 (Protected_Body_Subprogram (Entity (Pref)), Loc);
295          end if;
296
297          --  Don't traverse the scopes when the attribute occurs within an init
298          --  proc, because we directly use the _init formal of the init proc in
299          --  that case.
300
301          Curr := Current_Scope;
302          if not Is_Init_Proc (Curr) then
303             pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
304
305             while Scope (Curr) /= Scope (Entity (Pref)) loop
306                Curr := Scope (Curr);
307             end loop;
308          end if;
309
310          --  In case of protected entries the first formal of its Protected_
311          --  Body_Subprogram is the address of the object.
312
313          if Ekind (Curr) = E_Entry then
314             Obj_Ref :=
315                New_Occurrence_Of
316                  (First_Formal
317                    (Protected_Body_Subprogram (Curr)), Loc);
318
319          --  If the current scope is an init proc, then use the address of the
320          --  _init formal as the object reference.
321
322          elsif Is_Init_Proc (Curr) then
323             Obj_Ref :=
324               Make_Attribute_Reference (Loc,
325                 Prefix         => New_Occurrence_Of (First_Formal (Curr), Loc),
326                 Attribute_Name => Name_Address);
327
328          --  In case of protected subprograms the first formal of its
329          --  Protected_Body_Subprogram is the object and we get its address.
330
331          else
332             Obj_Ref :=
333               Make_Attribute_Reference (Loc,
334                 Prefix =>
335                    New_Occurrence_Of
336                      (First_Formal
337                         (Protected_Body_Subprogram (Curr)), Loc),
338                 Attribute_Name => Name_Address);
339          end if;
340
341       --  Case where the prefix is not an entity name. Find the
342       --  version of the protected operation to be called from
343       --  outside the protected object.
344
345       else
346          Sub :=
347            New_Occurrence_Of
348              (External_Subprogram
349                (Entity (Selector_Name (Pref))), Loc);
350
351          Obj_Ref :=
352            Make_Attribute_Reference (Loc,
353              Prefix => Relocate_Node (Prefix (Pref)),
354                Attribute_Name => Name_Address);
355       end if;
356
357       Agg :=
358         Make_Aggregate (Loc,
359           Expressions =>
360             New_List (
361               Obj_Ref,
362               Unchecked_Convert_To (Acc,
363                 Make_Attribute_Reference (Loc,
364                   Prefix => Sub,
365                   Attribute_Name => Name_Address))));
366
367       Rewrite (N, Agg);
368
369       Analyze_And_Resolve (N, E_T);
370
371       --  For subsequent analysis,  the node must retain its type.
372       --  The backend will replace it with the equivalent type where
373       --  needed.
374
375       Set_Etype (N, Typ);
376    end Expand_Access_To_Protected_Op;
377
378    --------------------------
379    -- Expand_Fpt_Attribute --
380    --------------------------
381
382    procedure Expand_Fpt_Attribute
383      (N    : Node_Id;
384       Pkg  : RE_Id;
385       Nam  : Name_Id;
386       Args : List_Id)
387    is
388       Loc : constant Source_Ptr := Sloc (N);
389       Typ : constant Entity_Id  := Etype (N);
390       Fnm : Node_Id;
391
392    begin
393       --  The function name is the selected component Attr_xxx.yyy where
394       --  Attr_xxx is the package name, and yyy is the argument Nam.
395
396       --  Note: it would be more usual to have separate RE entries for each
397       --  of the entities in the Fat packages, but first they have identical
398       --  names (so we would have to have lots of renaming declarations to
399       --  meet the normal RE rule of separate names for all runtime entities),
400       --  and second there would be an awful lot of them!
401
402       Fnm :=
403         Make_Selected_Component (Loc,
404           Prefix        => New_Reference_To (RTE (Pkg), Loc),
405           Selector_Name => Make_Identifier (Loc, Nam));
406
407       --  The generated call is given the provided set of parameters, and then
408       --  wrapped in a conversion which converts the result to the target type
409       --  We use the base type as the target because a range check may be
410       --  required.
411
412       Rewrite (N,
413         Unchecked_Convert_To (Base_Type (Etype (N)),
414           Make_Function_Call (Loc,
415             Name                   => Fnm,
416             Parameter_Associations => Args)));
417
418       Analyze_And_Resolve (N, Typ);
419    end Expand_Fpt_Attribute;
420
421    ----------------------------
422    -- Expand_Fpt_Attribute_R --
423    ----------------------------
424
425    --  The single argument is converted to its root type to call the
426    --  appropriate runtime function, with the actual call being built
427    --  by Expand_Fpt_Attribute
428
429    procedure Expand_Fpt_Attribute_R (N : Node_Id) is
430       E1  : constant Node_Id    := First (Expressions (N));
431       Ftp : Entity_Id;
432       Pkg : RE_Id;
433    begin
434       Find_Fat_Info (Etype (E1), Ftp, Pkg);
435       Expand_Fpt_Attribute
436         (N, Pkg, Attribute_Name (N),
437          New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
438    end Expand_Fpt_Attribute_R;
439
440    -----------------------------
441    -- Expand_Fpt_Attribute_RI --
442    -----------------------------
443
444    --  The first argument is converted to its root type and the second
445    --  argument is converted to standard long long integer to call the
446    --  appropriate runtime function, with the actual call being built
447    --  by Expand_Fpt_Attribute
448
449    procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
450       E1  : constant Node_Id   := First (Expressions (N));
451       Ftp : Entity_Id;
452       Pkg : RE_Id;
453       E2  : constant Node_Id   := Next (E1);
454    begin
455       Find_Fat_Info (Etype (E1), Ftp, Pkg);
456       Expand_Fpt_Attribute
457         (N, Pkg, Attribute_Name (N),
458          New_List (
459            Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
460            Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
461    end Expand_Fpt_Attribute_RI;
462
463    -----------------------------
464    -- Expand_Fpt_Attribute_RR --
465    -----------------------------
466
467    --  The two arguments are converted to their root types to call the
468    --  appropriate runtime function, with the actual call being built
469    --  by Expand_Fpt_Attribute
470
471    procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
472       E1  : constant Node_Id   := First (Expressions (N));
473       Ftp : Entity_Id;
474       Pkg : RE_Id;
475       E2  : constant Node_Id   := Next (E1);
476    begin
477       Find_Fat_Info (Etype (E1), Ftp, Pkg);
478       Expand_Fpt_Attribute
479         (N, Pkg, Attribute_Name (N),
480          New_List (
481            Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
482            Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
483    end Expand_Fpt_Attribute_RR;
484
485    ----------------------------------
486    -- Expand_N_Attribute_Reference --
487    ----------------------------------
488
489    procedure Expand_N_Attribute_Reference (N : Node_Id) is
490       Loc   : constant Source_Ptr   := Sloc (N);
491       Typ   : constant Entity_Id    := Etype (N);
492       Btyp  : constant Entity_Id    := Base_Type (Typ);
493       Pref  : constant Node_Id      := Prefix (N);
494       Ptyp  : constant Entity_Id    := Etype (Pref);
495       Exprs : constant List_Id      := Expressions (N);
496       Id    : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
497
498       procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
499       --  Rewrites a stream attribute for Read, Write or Output with the
500       --  procedure call. Pname is the entity for the procedure to call.
501
502       ------------------------------
503       -- Rewrite_Stream_Proc_Call --
504       ------------------------------
505
506       procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
507          Item       : constant Node_Id   := Next (First (Exprs));
508          Formal     : constant Entity_Id := Next_Formal (First_Formal (Pname));
509          Formal_Typ : constant Entity_Id := Etype (Formal);
510          Is_Written : constant Boolean   := (Ekind (Formal) /= E_In_Parameter);
511
512       begin
513          --  The expansion depends on Item, the second actual, which is
514          --  the object being streamed in or out.
515
516          --  If the item is a component of a packed array type, and
517          --  a conversion is needed on exit, we introduce a temporary to
518          --  hold the value, because otherwise the packed reference will
519          --  not be properly expanded.
520
521          if Nkind (Item) = N_Indexed_Component
522            and then Is_Packed (Base_Type (Etype (Prefix (Item))))
523            and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
524            and then Is_Written
525          then
526             declare
527                Temp : constant Entity_Id :=
528                         Make_Defining_Identifier
529                           (Loc, New_Internal_Name ('V'));
530                Decl : Node_Id;
531                Assn : Node_Id;
532
533             begin
534                Decl :=
535                  Make_Object_Declaration (Loc,
536                    Defining_Identifier => Temp,
537                    Object_Definition    =>
538                      New_Occurrence_Of (Formal_Typ, Loc));
539                Set_Etype (Temp, Formal_Typ);
540
541                Assn :=
542                  Make_Assignment_Statement (Loc,
543                    Name => New_Copy_Tree (Item),
544                    Expression =>
545                      Unchecked_Convert_To
546                        (Etype (Item), New_Occurrence_Of (Temp, Loc)));
547
548                Rewrite (Item, New_Occurrence_Of (Temp, Loc));
549                Insert_Actions (N,
550                  New_List (
551                    Decl,
552                    Make_Procedure_Call_Statement (Loc,
553                      Name => New_Occurrence_Of (Pname, Loc),
554                      Parameter_Associations => Exprs),
555                    Assn));
556
557                Rewrite (N, Make_Null_Statement (Loc));
558                return;
559             end;
560          end if;
561
562          --  For the class-wide dispatching cases, and for cases in which
563          --  the base type of the second argument matches the base type of
564          --  the corresponding formal parameter (that is to say the stream
565          --  operation is not inherited), we are all set, and can use the
566          --  argument unchanged.
567
568          --  For all other cases we do an unchecked conversion of the second
569          --  parameter to the type of the formal of the procedure we are
570          --  calling. This deals with the private type cases, and with going
571          --  to the root type as required in elementary type case.
572
573          if not Is_Class_Wide_Type (Entity (Pref))
574            and then not Is_Class_Wide_Type (Etype (Item))
575            and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
576          then
577             Rewrite (Item,
578               Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
579
580             --  For untagged derived types set Assignment_OK, to prevent
581             --  copies from being created when the unchecked conversion
582             --  is expanded (which would happen in Remove_Side_Effects
583             --  if Expand_N_Unchecked_Conversion were allowed to call
584             --  Force_Evaluation). The copy could violate Ada semantics
585             --  in cases such as an actual that is an out parameter.
586             --  Note that this approach is also used in exp_ch7 for calls
587             --  to controlled type operations to prevent problems with
588             --  actuals wrapped in unchecked conversions.
589
590             if Is_Untagged_Derivation (Etype (Expression (Item))) then
591                Set_Assignment_OK (Item);
592             end if;
593          end if;
594
595          --  And now rewrite the call
596
597          Rewrite (N,
598            Make_Procedure_Call_Statement (Loc,
599              Name => New_Occurrence_Of (Pname, Loc),
600              Parameter_Associations => Exprs));
601
602          Analyze (N);
603       end Rewrite_Stream_Proc_Call;
604
605    --  Start of processing for Expand_N_Attribute_Reference
606
607    begin
608       --  Do required validity checking, if enabled. Do not apply check to
609       --  output parameters of an Asm instruction, since the value of this
610       --  is not set till after the attribute has been elaborated, and do
611       --  not apply the check to the arguments of a 'Read or 'Input attribute
612       --  reference since the scalar argument is an OUT scalar.
613
614       if Validity_Checks_On and then Validity_Check_Operands
615         and then Id /= Attribute_Asm_Output
616         and then Id /= Attribute_Read
617         and then Id /= Attribute_Input
618       then
619          declare
620             Expr : Node_Id;
621          begin
622             Expr := First (Expressions (N));
623             while Present (Expr) loop
624                Ensure_Valid (Expr);
625                Next (Expr);
626             end loop;
627          end;
628       end if;
629
630       --  Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
631       --  place function, then a temporary return object needs to be created
632       --  and access to it must be passed to the function. Currently we limit
633       --  such functions to those with inherently limited result subtypes, but
634       --  eventually we plan to expand the functions that are treated as
635       --  build-in-place to include other composite result types.
636
637       if Ada_Version >= Ada_05
638         and then Is_Build_In_Place_Function_Call (Pref)
639       then
640          Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
641       end if;
642
643       --  If prefix is a protected type name, this is a reference to
644       --  the current instance of the type.
645
646       if Is_Protected_Self_Reference (Pref) then
647          Rewrite (Pref, Concurrent_Ref (Pref));
648          Analyze (Pref);
649       end if;
650
651       --  Remaining processing depends on specific attribute
652
653       case Id is
654
655       ------------
656       -- Access --
657       ------------
658
659       when Attribute_Access              |
660            Attribute_Unchecked_Access    |
661            Attribute_Unrestricted_Access =>
662
663          Access_Cases : declare
664             Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
665             Btyp_DDT   : Entity_Id;
666
667             function Enclosing_Object (N : Node_Id) return Node_Id;
668             --  If N denotes a compound name (selected component, indexed
669             --  component, or slice), returns the name of the outermost
670             --  such enclosing object. Otherwise returns N. If the object
671             --  is a renaming, then the renamed object is returned.
672
673             ----------------------
674             -- Enclosing_Object --
675             ----------------------
676
677             function Enclosing_Object (N : Node_Id) return Node_Id is
678                Obj_Name : Node_Id;
679
680             begin
681                Obj_Name := N;
682                while Nkind_In (Obj_Name, N_Selected_Component,
683                                          N_Indexed_Component,
684                                          N_Slice)
685                loop
686                   Obj_Name := Prefix (Obj_Name);
687                end loop;
688
689                return Get_Referenced_Object (Obj_Name);
690             end Enclosing_Object;
691
692             --  Local declarations
693
694             Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
695
696          --  Start of processing for Access_Cases
697
698          begin
699             Btyp_DDT := Designated_Type (Btyp);
700
701             --  Handle designated types that come from the limited view
702
703             if Ekind (Btyp_DDT) = E_Incomplete_Type
704               and then From_With_Type (Btyp_DDT)
705               and then Present (Non_Limited_View (Btyp_DDT))
706             then
707                Btyp_DDT := Non_Limited_View (Btyp_DDT);
708
709             elsif Is_Class_Wide_Type (Btyp_DDT)
710                and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type
711                and then From_With_Type (Etype (Btyp_DDT))
712                and then Present (Non_Limited_View (Etype (Btyp_DDT)))
713                and then Present (Class_Wide_Type
714                                   (Non_Limited_View (Etype (Btyp_DDT))))
715             then
716                Btyp_DDT :=
717                  Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT)));
718             end if;
719
720             --  In order to improve the text of error messages, the designated
721             --  type of access-to-subprogram itypes is set by the semantics as
722             --  the associated subprogram entity (see sem_attr). Now we replace
723             --  such node with the proper E_Subprogram_Type itype.
724
725             if Id = Attribute_Unrestricted_Access
726               and then Is_Subprogram (Directly_Designated_Type (Typ))
727             then
728                --  The following conditions ensure that this special management
729                --  is done only for "Address!(Prim'Unrestricted_Access)" nodes.
730                --  At this stage other cases in which the designated type is
731                --  still a subprogram (instead of an E_Subprogram_Type) are
732                --  wrong because the semantics must have overridden the type of
733                --  the node with the type imposed by the context.
734
735                if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
736                  and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
737                then
738                   Set_Etype (N, RTE (RE_Prim_Ptr));
739
740                else
741                   declare
742                      Subp       : constant Entity_Id :=
743                                     Directly_Designated_Type (Typ);
744                      Etyp       : Entity_Id;
745                      Extra      : Entity_Id := Empty;
746                      New_Formal : Entity_Id;
747                      Old_Formal : Entity_Id := First_Formal (Subp);
748                      Subp_Typ   : Entity_Id;
749
750                   begin
751                      Subp_Typ := Create_Itype (E_Subprogram_Type, N);
752                      Set_Etype (Subp_Typ, Etype (Subp));
753                      Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
754
755                      if Present (Old_Formal) then
756                         New_Formal := New_Copy (Old_Formal);
757                         Set_First_Entity (Subp_Typ, New_Formal);
758
759                         loop
760                            Set_Scope (New_Formal, Subp_Typ);
761                            Etyp := Etype (New_Formal);
762
763                            --  Handle itypes. There is no need to duplicate
764                            --  here the itypes associated with record types
765                            --  (i.e the implicit full view of private types).
766
767                            if Is_Itype (Etyp)
768                              and then Ekind (Base_Type (Etyp)) /= E_Record_Type
769                            then
770                               Extra := New_Copy (Etyp);
771                               Set_Parent (Extra, New_Formal);
772                               Set_Etype (New_Formal, Extra);
773                               Set_Scope (Extra, Subp_Typ);
774                            end if;
775
776                            Extra := New_Formal;
777                            Next_Formal (Old_Formal);
778                            exit when No (Old_Formal);
779
780                            Set_Next_Entity (New_Formal,
781                              New_Copy (Old_Formal));
782                            Next_Entity (New_Formal);
783                         end loop;
784
785                         Set_Next_Entity (New_Formal, Empty);
786                         Set_Last_Entity (Subp_Typ, Extra);
787                      end if;
788
789                      --  Now that the explicit formals have been duplicated,
790                      --  any extra formals needed by the subprogram must be
791                      --  created.
792
793                      if Present (Extra) then
794                         Set_Extra_Formal (Extra, Empty);
795                      end if;
796
797                      Create_Extra_Formals (Subp_Typ);
798                      Set_Directly_Designated_Type (Typ, Subp_Typ);
799                   end;
800                end if;
801             end if;
802
803             if Is_Access_Protected_Subprogram_Type (Btyp) then
804                Expand_Access_To_Protected_Op (N, Pref, Typ);
805
806             --  If prefix is a type name, this is a reference to the current
807             --  instance of the type, within its initialization procedure.
808
809             elsif Is_Entity_Name (Pref)
810               and then Is_Type (Entity (Pref))
811             then
812                declare
813                   Par    : Node_Id;
814                   Formal : Entity_Id;
815
816                begin
817                   --  If the current instance name denotes a task type, then
818                   --  the access attribute is rewritten to be the name of the
819                   --  "_task" parameter associated with the task type's task
820                   --  procedure. An unchecked conversion is applied to ensure
821                   --  a type match in cases of expander-generated calls (e.g.
822                   --  init procs).
823
824                   if Is_Task_Type (Entity (Pref)) then
825                      Formal :=
826                        First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
827                      while Present (Formal) loop
828                         exit when Chars (Formal) = Name_uTask;
829                         Next_Entity (Formal);
830                      end loop;
831
832                      pragma Assert (Present (Formal));
833
834                      Rewrite (N,
835                        Unchecked_Convert_To (Typ,
836                          New_Occurrence_Of (Formal, Loc)));
837                      Set_Etype (N, Typ);
838
839                      --  The expression must appear in a default expression,
840                      --  (which in the initialization procedure is the
841                      --  right-hand side of an assignment), and not in a
842                      --  discriminant constraint.
843
844                   else
845                      Par := Parent (N);
846                      while Present (Par) loop
847                         exit when Nkind (Par) = N_Assignment_Statement;
848
849                         if Nkind (Par) = N_Component_Declaration then
850                            return;
851                         end if;
852
853                         Par := Parent (Par);
854                      end loop;
855
856                      if Present (Par) then
857                         Rewrite (N,
858                           Make_Attribute_Reference (Loc,
859                             Prefix => Make_Identifier (Loc, Name_uInit),
860                             Attribute_Name  => Attribute_Name (N)));
861
862                         Analyze_And_Resolve (N, Typ);
863                      end if;
864                   end if;
865                end;
866
867             --  If the prefix of an Access attribute is a dereference of an
868             --  access parameter (or a renaming of such a dereference, or a
869             --  subcomponent of such a dereference) and the context is a
870             --  general access type (but not an anonymous access type), then
871             --  apply an accessibility check to the access parameter. We used
872             --  to rewrite the access parameter as a type conversion, but that
873             --  could only be done if the immediate prefix of the Access
874             --  attribute was the dereference, and didn't handle cases where
875             --  the attribute is applied to a subcomponent of the dereference,
876             --  since there's generally no available, appropriate access type
877             --  to convert to in that case. The attribute is passed as the
878             --  point to insert the check, because the access parameter may
879             --  come from a renaming, possibly in a different scope, and the
880             --  check must be associated with the attribute itself.
881
882             elsif Id = Attribute_Access
883               and then Nkind (Enc_Object) = N_Explicit_Dereference
884               and then Is_Entity_Name (Prefix (Enc_Object))
885               and then Ekind (Btyp) = E_General_Access_Type
886               and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
887               and then Ekind (Etype (Entity (Prefix (Enc_Object))))
888                          = E_Anonymous_Access_Type
889               and then Present (Extra_Accessibility
890                                 (Entity (Prefix (Enc_Object))))
891             then
892                Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
893
894             --  Ada 2005 (AI-251): If the designated type is an interface we
895             --  add an implicit conversion to force the displacement of the
896             --  pointer to reference the secondary dispatch table.
897
898             elsif Is_Interface (Btyp_DDT)
899               and then (Comes_From_Source (N)
900                          or else Comes_From_Source (Ref_Object)
901                          or else (Nkind (Ref_Object) in N_Has_Chars
902                                    and then Chars (Ref_Object) = Name_uInit))
903             then
904                if Nkind (Ref_Object) /= N_Explicit_Dereference then
905
906                   --  No implicit conversion required if types match
907
908                   if Btyp_DDT /= Etype (Ref_Object) then
909                      Rewrite (Prefix (N),
910                        Convert_To (Btyp_DDT,
911                          New_Copy_Tree (Prefix (N))));
912
913                      Analyze_And_Resolve (Prefix (N), Btyp_DDT);
914                   end if;
915
916                --  When the object is an explicit dereference, convert the
917                --  dereference's prefix.
918
919                else
920                   declare
921                      Obj_DDT : constant Entity_Id :=
922                                  Base_Type
923                                    (Directly_Designated_Type
924                                      (Etype (Prefix (Ref_Object))));
925                   begin
926                      --  No implicit conversion required if designated types
927                      --  match.
928
929                      if Obj_DDT /= Btyp_DDT
930                        and then not (Is_Class_Wide_Type (Obj_DDT)
931                                        and then Etype (Obj_DDT) = Btyp_DDT)
932                      then
933                         Rewrite (N,
934                           Convert_To (Typ,
935                             New_Copy_Tree (Prefix (Ref_Object))));
936                         Analyze_And_Resolve (N, Typ);
937                      end if;
938                   end;
939                end if;
940             end if;
941          end Access_Cases;
942
943       --------------
944       -- Adjacent --
945       --------------
946
947       --  Transforms 'Adjacent into a call to the floating-point attribute
948       --  function Adjacent in Fat_xxx (where xxx is the root type)
949
950       when Attribute_Adjacent =>
951          Expand_Fpt_Attribute_RR (N);
952
953       -------------
954       -- Address --
955       -------------
956
957       when Attribute_Address => Address : declare
958          Task_Proc : Entity_Id;
959
960       begin
961          --  If the prefix is a task or a task type, the useful address is that
962          --  of the procedure for the task body, i.e. the actual program unit.
963          --  We replace the original entity with that of the procedure.
964
965          if Is_Entity_Name (Pref)
966            and then Is_Task_Type (Entity (Pref))
967          then
968             Task_Proc := Next_Entity (Root_Type (Ptyp));
969
970             while Present (Task_Proc) loop
971                exit when Ekind (Task_Proc) = E_Procedure
972                  and then Etype (First_Formal (Task_Proc)) =
973                                   Corresponding_Record_Type (Ptyp);
974                Next_Entity (Task_Proc);
975             end loop;
976
977             if Present (Task_Proc) then
978                Set_Entity (Pref, Task_Proc);
979                Set_Etype  (Pref, Etype (Task_Proc));
980             end if;
981
982          --  Similarly, the address of a protected operation is the address
983          --  of the corresponding protected body, regardless of the protected
984          --  object from which it is selected.
985
986          elsif Nkind (Pref) = N_Selected_Component
987            and then Is_Subprogram (Entity (Selector_Name (Pref)))
988            and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
989          then
990             Rewrite (Pref,
991               New_Occurrence_Of (
992                 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
993
994          elsif Nkind (Pref) = N_Explicit_Dereference
995            and then Ekind (Ptyp) = E_Subprogram_Type
996            and then Convention (Ptyp) = Convention_Protected
997          then
998             --  The prefix is be a dereference of an access_to_protected_
999             --  subprogram. The desired address is the second component of
1000             --  the record that represents the access.
1001
1002             declare
1003                Addr : constant Entity_Id := Etype (N);
1004                Ptr  : constant Node_Id   := Prefix (Pref);
1005                T    : constant Entity_Id :=
1006                         Equivalent_Type (Base_Type (Etype (Ptr)));
1007
1008             begin
1009                Rewrite (N,
1010                  Unchecked_Convert_To (Addr,
1011                    Make_Selected_Component (Loc,
1012                      Prefix => Unchecked_Convert_To (T, Ptr),
1013                      Selector_Name => New_Occurrence_Of (
1014                        Next_Entity (First_Entity (T)), Loc))));
1015
1016                Analyze_And_Resolve (N, Addr);
1017             end;
1018
1019          --  Ada 2005 (AI-251): Class-wide interface objects are always
1020          --  "displaced" to reference the tag associated with the interface
1021          --  type. In order to obtain the real address of such objects we
1022          --  generate a call to a run-time subprogram that returns the base
1023          --  address of the object.
1024
1025          --  This processing is not needed in the VM case, where dispatching
1026          --  issues are taken care of by the virtual machine.
1027
1028          elsif Is_Class_Wide_Type (Ptyp)
1029            and then Is_Interface (Ptyp)
1030            and then VM_Target = No_VM
1031            and then not (Nkind (Pref) in N_Has_Entity
1032                           and then Is_Subprogram (Entity (Pref)))
1033          then
1034             Rewrite (N,
1035               Make_Function_Call (Loc,
1036                 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1037                 Parameter_Associations => New_List (
1038                   Relocate_Node (N))));
1039             Analyze (N);
1040             return;
1041          end if;
1042
1043          --  Deal with packed array reference, other cases are handled by
1044          --  the back end.
1045
1046          if Involves_Packed_Array_Reference (Pref) then
1047             Expand_Packed_Address_Reference (N);
1048          end if;
1049       end Address;
1050
1051       ---------------
1052       -- Alignment --
1053       ---------------
1054
1055       when Attribute_Alignment => Alignment : declare
1056          New_Node : Node_Id;
1057
1058       begin
1059          --  For class-wide types, X'Class'Alignment is transformed into a
1060          --  direct reference to the Alignment of the class type, so that the
1061          --  back end does not have to deal with the X'Class'Alignment
1062          --  reference.
1063
1064          if Is_Entity_Name (Pref)
1065            and then Is_Class_Wide_Type (Entity (Pref))
1066          then
1067             Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
1068             return;
1069
1070          --  For x'Alignment applied to an object of a class wide type,
1071          --  transform X'Alignment into a call to the predefined primitive
1072          --  operation _Alignment applied to X.
1073
1074          elsif Is_Class_Wide_Type (Ptyp) then
1075
1076             --  No need to do anything else compiling under restriction
1077             --  No_Dispatching_Calls. During the semantic analysis we
1078             --  already notified such violation.
1079
1080             if Restriction_Active (No_Dispatching_Calls) then
1081                return;
1082             end if;
1083
1084             New_Node :=
1085               Make_Function_Call (Loc,
1086                 Name => New_Reference_To
1087                   (Find_Prim_Op (Ptyp, Name_uAlignment), Loc),
1088                 Parameter_Associations => New_List (Pref));
1089
1090             if Typ /= Standard_Integer then
1091
1092                --  The context is a specific integer type with which the
1093                --  original attribute was compatible. The function has a
1094                --  specific type as well, so to preserve the compatibility
1095                --  we must convert explicitly.
1096
1097                New_Node := Convert_To (Typ, New_Node);
1098             end if;
1099
1100             Rewrite (N, New_Node);
1101             Analyze_And_Resolve (N, Typ);
1102             return;
1103
1104          --  For all other cases, we just have to deal with the case of
1105          --  the fact that the result can be universal.
1106
1107          else
1108             Apply_Universal_Integer_Attribute_Checks (N);
1109          end if;
1110       end Alignment;
1111
1112       ---------------
1113       -- AST_Entry --
1114       ---------------
1115
1116       when Attribute_AST_Entry => AST_Entry : declare
1117          Ttyp : Entity_Id;
1118          T_Id : Node_Id;
1119          Eent : Entity_Id;
1120
1121          Entry_Ref : Node_Id;
1122          --  The reference to the entry or entry family
1123
1124          Index : Node_Id;
1125          --  The index expression for an entry family reference, or
1126          --  the Empty if Entry_Ref references a simple entry.
1127
1128       begin
1129          if Nkind (Pref) = N_Indexed_Component then
1130             Entry_Ref := Prefix (Pref);
1131             Index := First (Expressions (Pref));
1132          else
1133             Entry_Ref := Pref;
1134             Index := Empty;
1135          end if;
1136
1137          --  Get expression for Task_Id and the entry entity
1138
1139          if Nkind (Entry_Ref) = N_Selected_Component then
1140             T_Id :=
1141               Make_Attribute_Reference (Loc,
1142                 Attribute_Name => Name_Identity,
1143                 Prefix         => Prefix (Entry_Ref));
1144
1145             Ttyp := Etype (Prefix (Entry_Ref));
1146             Eent := Entity (Selector_Name (Entry_Ref));
1147
1148          else
1149             T_Id :=
1150               Make_Function_Call (Loc,
1151                 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
1152
1153             Eent  := Entity (Entry_Ref);
1154
1155             --  We have to find the enclosing task to get the task type
1156             --  There must be one, since we already validated this earlier
1157
1158             Ttyp := Current_Scope;
1159             while not Is_Task_Type (Ttyp) loop
1160                Ttyp := Scope (Ttyp);
1161             end loop;
1162          end if;
1163
1164          --  Now rewrite the attribute with a call to Create_AST_Handler
1165
1166          Rewrite (N,
1167            Make_Function_Call (Loc,
1168              Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
1169              Parameter_Associations => New_List (
1170                T_Id,
1171                Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
1172
1173          Analyze_And_Resolve (N, RTE (RE_AST_Handler));
1174       end AST_Entry;
1175
1176       ------------------
1177       -- Bit_Position --
1178       ------------------
1179
1180       --  We compute this if a component clause was present, otherwise we leave
1181       --  the computation up to the back end, since we don't know what layout
1182       --  will be chosen.
1183
1184       --  Note that the attribute can apply to a naked record component
1185       --  in generated code (i.e. the prefix is an identifier that
1186       --  references the component or discriminant entity).
1187
1188       when Attribute_Bit_Position => Bit_Position :
1189       declare
1190          CE : Entity_Id;
1191
1192       begin
1193          if Nkind (Pref) = N_Identifier then
1194             CE := Entity (Pref);
1195          else
1196             CE := Entity (Selector_Name (Pref));
1197          end if;
1198
1199          if Known_Static_Component_Bit_Offset (CE) then
1200             Rewrite (N,
1201               Make_Integer_Literal (Loc,
1202                 Intval => Component_Bit_Offset (CE)));
1203             Analyze_And_Resolve (N, Typ);
1204
1205          else
1206             Apply_Universal_Integer_Attribute_Checks (N);
1207          end if;
1208       end Bit_Position;
1209
1210       ------------------
1211       -- Body_Version --
1212       ------------------
1213
1214       --  A reference to P'Body_Version or P'Version is expanded to
1215
1216       --     Vnn : Unsigned;
1217       --     pragma Import (C, Vnn, "uuuuT";
1218       --     ...
1219       --     Get_Version_String (Vnn)
1220
1221       --  where uuuu is the unit name (dots replaced by double underscore)
1222       --  and T is B for the cases of Body_Version, or Version applied to a
1223       --  subprogram acting as its own spec, and S for Version applied to a
1224       --  subprogram spec or package. This sequence of code references the
1225       --  the unsigned constant created in the main program by the binder.
1226
1227       --  A special exception occurs for Standard, where the string
1228       --  returned is a copy of the library string in gnatvsn.ads.
1229
1230       when Attribute_Body_Version | Attribute_Version => Version : declare
1231          E    : constant Entity_Id :=
1232                   Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1233          Pent : Entity_Id;
1234          S    : String_Id;
1235
1236       begin
1237          --  If not library unit, get to containing library unit
1238
1239          Pent := Entity (Pref);
1240          while Pent /= Standard_Standard
1241            and then Scope (Pent) /= Standard_Standard
1242            and then not Is_Child_Unit (Pent)
1243          loop
1244             Pent := Scope (Pent);
1245          end loop;
1246
1247          --  Special case Standard and Standard.ASCII
1248
1249          if Pent = Standard_Standard or else Pent = Standard_ASCII then
1250             Rewrite (N,
1251               Make_String_Literal (Loc,
1252                 Strval => Verbose_Library_Version));
1253
1254          --  All other cases
1255
1256          else
1257             --  Build required string constant
1258
1259             Get_Name_String (Get_Unit_Name (Pent));
1260
1261             Start_String;
1262             for J in 1 .. Name_Len - 2 loop
1263                if Name_Buffer (J) = '.' then
1264                   Store_String_Chars ("__");
1265                else
1266                   Store_String_Char (Get_Char_Code (Name_Buffer (J)));
1267                end if;
1268             end loop;
1269
1270             --  Case of subprogram acting as its own spec, always use body
1271
1272             if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
1273               and then Nkind (Parent (Declaration_Node (Pent))) =
1274                                                           N_Subprogram_Body
1275               and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
1276             then
1277                Store_String_Chars ("B");
1278
1279             --  Case of no body present, always use spec
1280
1281             elsif not Unit_Requires_Body (Pent) then
1282                Store_String_Chars ("S");
1283
1284             --  Otherwise use B for Body_Version, S for spec
1285
1286             elsif Id = Attribute_Body_Version then
1287                Store_String_Chars ("B");
1288             else
1289                Store_String_Chars ("S");
1290             end if;
1291
1292             S := End_String;
1293             Lib.Version_Referenced (S);
1294
1295             --  Insert the object declaration
1296
1297             Insert_Actions (N, New_List (
1298               Make_Object_Declaration (Loc,
1299                 Defining_Identifier => E,
1300                 Object_Definition   =>
1301                   New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
1302
1303             --  Set entity as imported with correct external name
1304
1305             Set_Is_Imported (E);
1306             Set_Interface_Name (E, Make_String_Literal (Loc, S));
1307
1308             --  Set entity as internal to ensure proper Sprint output of its
1309             --  implicit importation.
1310
1311             Set_Is_Internal (E);
1312
1313             --  And now rewrite original reference
1314
1315             Rewrite (N,
1316               Make_Function_Call (Loc,
1317                 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
1318                 Parameter_Associations => New_List (
1319                   New_Occurrence_Of (E, Loc))));
1320          end if;
1321
1322          Analyze_And_Resolve (N, RTE (RE_Version_String));
1323       end Version;
1324
1325       -------------
1326       -- Ceiling --
1327       -------------
1328
1329       --  Transforms 'Ceiling into a call to the floating-point attribute
1330       --  function Ceiling in Fat_xxx (where xxx is the root type)
1331
1332       when Attribute_Ceiling =>
1333          Expand_Fpt_Attribute_R (N);
1334
1335       --------------
1336       -- Callable --
1337       --------------
1338
1339       --  Transforms 'Callable attribute into a call to the Callable function
1340
1341       when Attribute_Callable => Callable :
1342       begin
1343          --  We have an object of a task interface class-wide type as a prefix
1344          --  to Callable. Generate:
1345
1346          --    callable (Task_Id (Pref._disp_get_task_id));
1347
1348          if Ada_Version >= Ada_05
1349            and then Ekind (Ptyp) = E_Class_Wide_Type
1350            and then Is_Interface (Ptyp)
1351            and then Is_Task_Interface (Ptyp)
1352          then
1353             Rewrite (N,
1354               Make_Function_Call (Loc,
1355                 Name =>
1356                   New_Reference_To (RTE (RE_Callable), Loc),
1357                 Parameter_Associations => New_List (
1358                   Make_Unchecked_Type_Conversion (Loc,
1359                     Subtype_Mark =>
1360                       New_Reference_To (RTE (RO_ST_Task_Id), Loc),
1361                     Expression =>
1362                       Make_Selected_Component (Loc,
1363                         Prefix =>
1364                           New_Copy_Tree (Pref),
1365                         Selector_Name =>
1366                           Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
1367
1368          else
1369             Rewrite (N,
1370               Build_Call_With_Task (Pref, RTE (RE_Callable)));
1371          end if;
1372
1373          Analyze_And_Resolve (N, Standard_Boolean);
1374       end Callable;
1375
1376       ------------
1377       -- Caller --
1378       ------------
1379
1380       --  Transforms 'Caller attribute into a call to either the
1381       --  Task_Entry_Caller or the Protected_Entry_Caller function.
1382
1383       when Attribute_Caller => Caller : declare
1384          Id_Kind    : constant Entity_Id := RTE (RO_AT_Task_Id);
1385          Ent        : constant Entity_Id := Entity (Pref);
1386          Conctype   : constant Entity_Id := Scope (Ent);
1387          Nest_Depth : Integer := 0;
1388          Name       : Node_Id;
1389          S          : Entity_Id;
1390
1391       begin
1392          --  Protected case
1393
1394          if Is_Protected_Type (Conctype) then
1395             case Corresponding_Runtime_Package (Conctype) is
1396                when System_Tasking_Protected_Objects_Entries =>
1397                   Name :=
1398                     New_Reference_To
1399                       (RTE (RE_Protected_Entry_Caller), Loc);
1400
1401                when System_Tasking_Protected_Objects_Single_Entry =>
1402                   Name :=
1403                     New_Reference_To
1404                       (RTE (RE_Protected_Single_Entry_Caller), Loc);
1405
1406                when others =>
1407                   raise Program_Error;
1408             end case;
1409
1410             Rewrite (N,
1411               Unchecked_Convert_To (Id_Kind,
1412                 Make_Function_Call (Loc,
1413                   Name => Name,
1414                   Parameter_Associations => New_List (
1415                     New_Reference_To
1416                       (Find_Protection_Object (Current_Scope), Loc)))));
1417
1418          --  Task case
1419
1420          else
1421             --  Determine the nesting depth of the E'Caller attribute, that
1422             --  is, how many accept statements are nested within the accept
1423             --  statement for E at the point of E'Caller. The runtime uses
1424             --  this depth to find the specified entry call.
1425
1426             for J in reverse 0 .. Scope_Stack.Last loop
1427                S := Scope_Stack.Table (J).Entity;
1428
1429                --  We should not reach the scope of the entry, as it should
1430                --  already have been checked in Sem_Attr that this attribute
1431                --  reference is within a matching accept statement.
1432
1433                pragma Assert (S /= Conctype);
1434
1435                if S = Ent then
1436                   exit;
1437
1438                elsif Is_Entry (S) then
1439                   Nest_Depth := Nest_Depth + 1;
1440                end if;
1441             end loop;
1442
1443             Rewrite (N,
1444               Unchecked_Convert_To (Id_Kind,
1445                 Make_Function_Call (Loc,
1446                   Name =>
1447                     New_Reference_To (RTE (RE_Task_Entry_Caller), Loc),
1448                   Parameter_Associations => New_List (
1449                     Make_Integer_Literal (Loc,
1450                       Intval => Int (Nest_Depth))))));
1451          end if;
1452
1453          Analyze_And_Resolve (N, Id_Kind);
1454       end Caller;
1455
1456       -------------
1457       -- Compose --
1458       -------------
1459
1460       --  Transforms 'Compose into a call to the floating-point attribute
1461       --  function Compose in Fat_xxx (where xxx is the root type)
1462
1463       --  Note: we strictly should have special code here to deal with the
1464       --  case of absurdly negative arguments (less than Integer'First)
1465       --  which will return a (signed) zero value, but it hardly seems
1466       --  worth the effort. Absurdly large positive arguments will raise
1467       --  constraint error which is fine.
1468
1469       when Attribute_Compose =>
1470          Expand_Fpt_Attribute_RI (N);
1471
1472       -----------------
1473       -- Constrained --
1474       -----------------
1475
1476       when Attribute_Constrained => Constrained : declare
1477          Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1478
1479          function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
1480          --  Ada 2005 (AI-363): Returns True if the object name Obj denotes a
1481          --  view of an aliased object whose subtype is constrained.
1482
1483          ---------------------------------
1484          -- Is_Constrained_Aliased_View --
1485          ---------------------------------
1486
1487          function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
1488             E : Entity_Id;
1489
1490          begin
1491             if Is_Entity_Name (Obj) then
1492                E := Entity (Obj);
1493
1494                if Present (Renamed_Object (E)) then
1495                   return Is_Constrained_Aliased_View (Renamed_Object (E));
1496                else
1497                   return Is_Aliased (E) and then Is_Constrained (Etype (E));
1498                end if;
1499
1500             else
1501                return Is_Aliased_View (Obj)
1502                         and then
1503                       (Is_Constrained (Etype (Obj))
1504                          or else (Nkind (Obj) = N_Explicit_Dereference
1505                                     and then
1506                                       not Has_Constrained_Partial_View
1507                                             (Base_Type (Etype (Obj)))));
1508             end if;
1509          end Is_Constrained_Aliased_View;
1510
1511       --  Start of processing for Constrained
1512
1513       begin
1514          --  Reference to a parameter where the value is passed as an extra
1515          --  actual, corresponding to the extra formal referenced by the
1516          --  Extra_Constrained field of the corresponding formal. If this
1517          --  is an entry in-parameter, it is replaced by a constant renaming
1518          --  for which Extra_Constrained is never created.
1519
1520          if Present (Formal_Ent)
1521            and then Ekind (Formal_Ent) /= E_Constant
1522            and then Present (Extra_Constrained (Formal_Ent))
1523          then
1524             Rewrite (N,
1525               New_Occurrence_Of
1526                 (Extra_Constrained (Formal_Ent), Sloc (N)));
1527
1528          --  For variables with a Extra_Constrained field, we use the
1529          --  corresponding entity.
1530
1531          elsif Nkind (Pref) = N_Identifier
1532            and then Ekind (Entity (Pref)) = E_Variable
1533            and then Present (Extra_Constrained (Entity (Pref)))
1534          then
1535             Rewrite (N,
1536               New_Occurrence_Of
1537                 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1538
1539          --  For all other entity names, we can tell at compile time
1540
1541          elsif Is_Entity_Name (Pref) then
1542             declare
1543                Ent : constant Entity_Id   := Entity (Pref);
1544                Res : Boolean;
1545
1546             begin
1547                --  (RM J.4) obsolescent cases
1548
1549                if Is_Type (Ent) then
1550
1551                   --  Private type
1552
1553                   if Is_Private_Type (Ent) then
1554                      Res := not Has_Discriminants (Ent)
1555                               or else Is_Constrained (Ent);
1556
1557                   --  It not a private type, must be a generic actual type
1558                   --  that corresponded to a private type. We know that this
1559                   --  correspondence holds, since otherwise the reference
1560                   --  within the generic template would have been illegal.
1561
1562                   else
1563                      if Is_Composite_Type (Underlying_Type (Ent)) then
1564                         Res := Is_Constrained (Ent);
1565                      else
1566                         Res := True;
1567                      end if;
1568                   end if;
1569
1570                --  If the prefix is not a variable or is aliased, then
1571                --  definitely true; if it's a formal parameter without an
1572                --  associated extra formal, then treat it as constrained.
1573
1574                --  Ada 2005 (AI-363): An aliased prefix must be known to be
1575                --  constrained in order to set the attribute to True.
1576
1577                elsif not Is_Variable (Pref)
1578                  or else Present (Formal_Ent)
1579                  or else (Ada_Version < Ada_05
1580                             and then Is_Aliased_View (Pref))
1581                  or else (Ada_Version >= Ada_05
1582                             and then Is_Constrained_Aliased_View (Pref))
1583                then
1584                   Res := True;
1585
1586                --  Variable case, look at type to see if it is constrained.
1587                --  Note that the one case where this is not accurate (the
1588                --  procedure formal case), has been handled above.
1589
1590                --  We use the Underlying_Type here (and below) in case the
1591                --  type is private without discriminants, but the full type
1592                --  has discriminants. This case is illegal, but we generate it
1593                --  internally for passing to the Extra_Constrained parameter.
1594
1595                else
1596                   Res := Is_Constrained (Underlying_Type (Etype (Ent)));
1597                end if;
1598
1599                Rewrite (N,
1600                  New_Reference_To (Boolean_Literals (Res), Loc));
1601             end;
1602
1603          --  Prefix is not an entity name. These are also cases where we can
1604          --  always tell at compile time by looking at the form and type of the
1605          --  prefix. If an explicit dereference of an object with constrained
1606          --  partial view, this is unconstrained (Ada 2005 AI-363).
1607
1608          else
1609             Rewrite (N,
1610               New_Reference_To (
1611                 Boolean_Literals (
1612                   not Is_Variable (Pref)
1613                     or else
1614                      (Nkind (Pref) = N_Explicit_Dereference
1615                         and then
1616                           not Has_Constrained_Partial_View (Base_Type (Ptyp)))
1617                     or else Is_Constrained (Underlying_Type (Ptyp))),
1618                 Loc));
1619          end if;
1620
1621          Analyze_And_Resolve (N, Standard_Boolean);
1622       end Constrained;
1623
1624       ---------------
1625       -- Copy_Sign --
1626       ---------------
1627
1628       --  Transforms 'Copy_Sign into a call to the floating-point attribute
1629       --  function Copy_Sign in Fat_xxx (where xxx is the root type)
1630
1631       when Attribute_Copy_Sign =>
1632          Expand_Fpt_Attribute_RR (N);
1633
1634       -----------
1635       -- Count --
1636       -----------
1637
1638       --  Transforms 'Count attribute into a call to the Count function
1639
1640       when Attribute_Count => Count : declare
1641          Call     : Node_Id;
1642          Conctyp  : Entity_Id;
1643          Entnam   : Node_Id;
1644          Entry_Id : Entity_Id;
1645          Index    : Node_Id;
1646          Name     : Node_Id;
1647
1648       begin
1649          --  If the prefix is a member of an entry family, retrieve both
1650          --  entry name and index. For a simple entry there is no index.
1651
1652          if Nkind (Pref) = N_Indexed_Component then
1653             Entnam := Prefix (Pref);
1654             Index := First (Expressions (Pref));
1655          else
1656             Entnam := Pref;
1657             Index := Empty;
1658          end if;
1659
1660          Entry_Id := Entity (Entnam);
1661
1662          --  Find the concurrent type in which this attribute is referenced
1663          --  (there had better be one).
1664
1665          Conctyp := Current_Scope;
1666          while not Is_Concurrent_Type (Conctyp) loop
1667             Conctyp := Scope (Conctyp);
1668          end loop;
1669
1670          --  Protected case
1671
1672          if Is_Protected_Type (Conctyp) then
1673             case Corresponding_Runtime_Package (Conctyp) is
1674                when System_Tasking_Protected_Objects_Entries =>
1675                   Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1676
1677                   Call :=
1678                     Make_Function_Call (Loc,
1679                       Name => Name,
1680                       Parameter_Associations => New_List (
1681                         New_Reference_To
1682                           (Find_Protection_Object (Current_Scope), Loc),
1683                         Entry_Index_Expression
1684                           (Loc, Entry_Id, Index, Scope (Entry_Id))));
1685
1686                when System_Tasking_Protected_Objects_Single_Entry =>
1687                   Name :=
1688                     New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1689
1690                   Call :=
1691                     Make_Function_Call (Loc,
1692                       Name => Name,
1693                       Parameter_Associations => New_List (
1694                         New_Reference_To
1695                           (Find_Protection_Object (Current_Scope), Loc)));
1696
1697                when others =>
1698                   raise Program_Error;
1699             end case;
1700
1701          --  Task case
1702
1703          else
1704             Call :=
1705               Make_Function_Call (Loc,
1706                 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1707                 Parameter_Associations => New_List (
1708                   Entry_Index_Expression (Loc,
1709                     Entry_Id, Index, Scope (Entry_Id))));
1710          end if;
1711
1712          --  The call returns type Natural but the context is universal integer
1713          --  so any integer type is allowed. The attribute was already resolved
1714          --  so its Etype is the required result type. If the base type of the
1715          --  context type is other than Standard.Integer we put in a conversion
1716          --  to the required type. This can be a normal typed conversion since
1717          --  both input and output types of the conversion are integer types
1718
1719          if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1720             Rewrite (N, Convert_To (Typ, Call));
1721          else
1722             Rewrite (N, Call);
1723          end if;
1724
1725          Analyze_And_Resolve (N, Typ);
1726       end Count;
1727
1728       ---------------
1729       -- Elab_Body --
1730       ---------------
1731
1732       --  This processing is shared by Elab_Spec
1733
1734       --  What we do is to insert the following declarations
1735
1736       --     procedure tnn;
1737       --     pragma Import (C, enn, "name___elabb/s");
1738
1739       --  and then the Elab_Body/Spec attribute is replaced by a reference
1740       --  to this defining identifier.
1741
1742       when Attribute_Elab_Body |
1743            Attribute_Elab_Spec =>
1744
1745          Elab_Body : declare
1746             Ent  : constant Entity_Id :=
1747                      Make_Defining_Identifier (Loc,
1748                        New_Internal_Name ('E'));
1749             Str  : String_Id;
1750             Lang : Node_Id;
1751
1752             procedure Make_Elab_String (Nod : Node_Id);
1753             --  Given Nod, an identifier, or a selected component, put the
1754             --  image into the current string literal, with double underline
1755             --  between components.
1756
1757             ----------------------
1758             -- Make_Elab_String --
1759             ----------------------
1760
1761             procedure Make_Elab_String (Nod : Node_Id) is
1762             begin
1763                if Nkind (Nod) = N_Selected_Component then
1764                   Make_Elab_String (Prefix (Nod));
1765
1766                   case VM_Target is
1767                      when JVM_Target =>
1768                         Store_String_Char ('$');
1769                      when CLI_Target =>
1770                         Store_String_Char ('.');
1771                      when No_VM =>
1772                         Store_String_Char ('_');
1773                         Store_String_Char ('_');
1774                   end case;
1775
1776                   Get_Name_String (Chars (Selector_Name (Nod)));
1777
1778                else
1779                   pragma Assert (Nkind (Nod) = N_Identifier);
1780                   Get_Name_String (Chars (Nod));
1781                end if;
1782
1783                Store_String_Chars (Name_Buffer (1 .. Name_Len));
1784             end Make_Elab_String;
1785
1786          --  Start of processing for Elab_Body/Elab_Spec
1787
1788          begin
1789             --  First we need to prepare the string literal for the name of
1790             --  the elaboration routine to be referenced.
1791
1792             Start_String;
1793             Make_Elab_String (Pref);
1794
1795             if VM_Target = No_VM then
1796                Store_String_Chars ("___elab");
1797                Lang := Make_Identifier (Loc, Name_C);
1798             else
1799                Store_String_Chars ("._elab");
1800                Lang := Make_Identifier (Loc, Name_Ada);
1801             end if;
1802
1803             if Id = Attribute_Elab_Body then
1804                Store_String_Char ('b');
1805             else
1806                Store_String_Char ('s');
1807             end if;
1808
1809             Str := End_String;
1810
1811             Insert_Actions (N, New_List (
1812               Make_Subprogram_Declaration (Loc,
1813                 Specification =>
1814                   Make_Procedure_Specification (Loc,
1815                     Defining_Unit_Name => Ent)),
1816
1817               Make_Pragma (Loc,
1818                 Chars => Name_Import,
1819                 Pragma_Argument_Associations => New_List (
1820                   Make_Pragma_Argument_Association (Loc,
1821                     Expression => Lang),
1822
1823                   Make_Pragma_Argument_Association (Loc,
1824                     Expression =>
1825                       Make_Identifier (Loc, Chars (Ent))),
1826
1827                   Make_Pragma_Argument_Association (Loc,
1828                     Expression =>
1829                       Make_String_Literal (Loc, Str))))));
1830
1831             Set_Entity (N, Ent);
1832             Rewrite (N, New_Occurrence_Of (Ent, Loc));
1833          end Elab_Body;
1834
1835       ----------------
1836       -- Elaborated --
1837       ----------------
1838
1839       --  Elaborated is always True for preelaborated units, predefined units,
1840       --  pure units and units which have Elaborate_Body pragmas. These units
1841       --  have no elaboration entity.
1842
1843       --  Note: The Elaborated attribute is never passed to the back end
1844
1845       when Attribute_Elaborated => Elaborated : declare
1846          Ent : constant Entity_Id := Entity (Pref);
1847
1848       begin
1849          if Present (Elaboration_Entity (Ent)) then
1850             Rewrite (N,
1851               New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1852          else
1853             Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1854          end if;
1855       end Elaborated;
1856
1857       --------------
1858       -- Enum_Rep --
1859       --------------
1860
1861       when Attribute_Enum_Rep => Enum_Rep :
1862       begin
1863          --  X'Enum_Rep (Y) expands to
1864
1865          --    target-type (Y)
1866
1867          --  This is simply a direct conversion from the enumeration type to
1868          --  the target integer type, which is treated by the back end as a
1869          --  normal integer conversion, treating the enumeration type as an
1870          --  integer, which is exactly what we want! We set Conversion_OK to
1871          --  make sure that the analyzer does not complain about what otherwise
1872          --  might be an illegal conversion.
1873
1874          if Is_Non_Empty_List (Exprs) then
1875             Rewrite (N,
1876               OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1877
1878          --  X'Enum_Rep where X is an enumeration literal is replaced by
1879          --  the literal value.
1880
1881          elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1882             Rewrite (N,
1883               Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1884
1885          --  If this is a renaming of a literal, recover the representation
1886          --  of the original.
1887
1888          elsif Ekind (Entity (Pref)) = E_Constant
1889            and then Present (Renamed_Object (Entity (Pref)))
1890            and then
1891              Ekind (Entity (Renamed_Object (Entity (Pref))))
1892                = E_Enumeration_Literal
1893          then
1894             Rewrite (N,
1895               Make_Integer_Literal (Loc,
1896                 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
1897
1898          --  X'Enum_Rep where X is an object does a direct unchecked conversion
1899          --  of the object value, as described for the type case above.
1900
1901          else
1902             Rewrite (N,
1903               OK_Convert_To (Typ, Relocate_Node (Pref)));
1904          end if;
1905
1906          Set_Etype (N, Typ);
1907          Analyze_And_Resolve (N, Typ);
1908       end Enum_Rep;
1909
1910       --------------
1911       -- Enum_Val --
1912       --------------
1913
1914       when Attribute_Enum_Val => Enum_Val : declare
1915          Expr : Node_Id;
1916          Btyp : constant Entity_Id  := Base_Type (Ptyp);
1917
1918       begin
1919          --  X'Enum_Val (Y) expands to
1920
1921          --    [constraint_error when _rep_to_pos (Y, False) = -1, msg]
1922          --    X!(Y);
1923
1924          Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
1925
1926          Insert_Action (N,
1927            Make_Raise_Constraint_Error (Loc,
1928              Condition =>
1929                Make_Op_Eq (Loc,
1930                  Left_Opnd =>
1931                    Make_Function_Call (Loc,
1932                      Name =>
1933                        New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
1934                      Parameter_Associations => New_List (
1935                        Relocate_Node (Duplicate_Subexpr (Expr)),
1936                          New_Occurrence_Of (Standard_False, Loc))),
1937
1938                  Right_Opnd => Make_Integer_Literal (Loc, -1)),
1939              Reason => CE_Range_Check_Failed));
1940
1941          Rewrite (N, Expr);
1942          Analyze_And_Resolve (N, Ptyp);
1943       end Enum_Val;
1944
1945       --------------
1946       -- Exponent --
1947       --------------
1948
1949       --  Transforms 'Exponent into a call to the floating-point attribute
1950       --  function Exponent in Fat_xxx (where xxx is the root type)
1951
1952       when Attribute_Exponent =>
1953          Expand_Fpt_Attribute_R (N);
1954
1955       ------------------
1956       -- External_Tag --
1957       ------------------
1958
1959       --  transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1960
1961       when Attribute_External_Tag => External_Tag :
1962       begin
1963          Rewrite (N,
1964            Make_Function_Call (Loc,
1965              Name => New_Reference_To (RTE (RE_External_Tag), Loc),
1966              Parameter_Associations => New_List (
1967                Make_Attribute_Reference (Loc,
1968                  Attribute_Name => Name_Tag,
1969                  Prefix => Prefix (N)))));
1970
1971          Analyze_And_Resolve (N, Standard_String);
1972       end External_Tag;
1973
1974       -----------
1975       -- First --
1976       -----------
1977
1978       when Attribute_First =>
1979
1980          --  If the prefix type is a constrained packed array type which
1981          --  already has a Packed_Array_Type representation defined, then
1982          --  replace this attribute with a direct reference to 'First of the
1983          --  appropriate index subtype (since otherwise the back end will try
1984          --  to give us the value of 'First for this implementation type).
1985
1986          if Is_Constrained_Packed_Array (Ptyp) then
1987             Rewrite (N,
1988               Make_Attribute_Reference (Loc,
1989                 Attribute_Name => Name_First,
1990                 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1991             Analyze_And_Resolve (N, Typ);
1992
1993          elsif Is_Access_Type (Ptyp) then
1994             Apply_Access_Check (N);
1995          end if;
1996
1997       ---------------
1998       -- First_Bit --
1999       ---------------
2000
2001       --  Compute this if component clause was present, otherwise we leave the
2002       --  computation to be completed in the back-end, since we don't know what
2003       --  layout will be chosen.
2004
2005       when Attribute_First_Bit => First_Bit : declare
2006          CE : constant Entity_Id := Entity (Selector_Name (Pref));
2007
2008       begin
2009          if Known_Static_Component_Bit_Offset (CE) then
2010             Rewrite (N,
2011               Make_Integer_Literal (Loc,
2012                 Component_Bit_Offset (CE) mod System_Storage_Unit));
2013
2014             Analyze_And_Resolve (N, Typ);
2015
2016          else
2017             Apply_Universal_Integer_Attribute_Checks (N);
2018          end if;
2019       end First_Bit;
2020
2021       -----------------
2022       -- Fixed_Value --
2023       -----------------
2024
2025       --  We transform:
2026
2027       --     fixtype'Fixed_Value (integer-value)
2028
2029       --  into
2030
2031       --     fixtype(integer-value)
2032
2033       --  We do all the required analysis of the conversion here, because we do
2034       --  not want this to go through the fixed-point conversion circuits. Note
2035       --  that the back end always treats fixed-point as equivalent to the
2036       --  corresponding integer type anyway.
2037
2038       when Attribute_Fixed_Value => Fixed_Value :
2039       begin
2040          Rewrite (N,
2041            Make_Type_Conversion (Loc,
2042              Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2043              Expression   => Relocate_Node (First (Exprs))));
2044          Set_Etype (N, Entity (Pref));
2045          Set_Analyzed (N);
2046
2047       --  Note: it might appear that a properly analyzed unchecked conversion
2048       --  would be just fine here, but that's not the case, since the full
2049       --  range checks performed by the following call are critical!
2050
2051          Apply_Type_Conversion_Checks (N);
2052       end Fixed_Value;
2053
2054       -----------
2055       -- Floor --
2056       -----------
2057
2058       --  Transforms 'Floor into a call to the floating-point attribute
2059       --  function Floor in Fat_xxx (where xxx is the root type)
2060
2061       when Attribute_Floor =>
2062          Expand_Fpt_Attribute_R (N);
2063
2064       ----------
2065       -- Fore --
2066       ----------
2067
2068       --  For the fixed-point type Typ:
2069
2070       --    Typ'Fore
2071
2072       --  expands into
2073
2074       --    Result_Type (System.Fore (Universal_Real (Type'First)),
2075       --                              Universal_Real (Type'Last))
2076
2077       --  Note that we know that the type is a non-static subtype, or Fore
2078       --  would have itself been computed dynamically in Eval_Attribute.
2079
2080       when Attribute_Fore => Fore : begin
2081          Rewrite (N,
2082            Convert_To (Typ,
2083              Make_Function_Call (Loc,
2084                Name => New_Reference_To (RTE (RE_Fore), Loc),
2085
2086                Parameter_Associations => New_List (
2087                  Convert_To (Universal_Real,
2088                    Make_Attribute_Reference (Loc,
2089                      Prefix => New_Reference_To (Ptyp, Loc),
2090                      Attribute_Name => Name_First)),
2091
2092                  Convert_To (Universal_Real,
2093                    Make_Attribute_Reference (Loc,
2094                      Prefix => New_Reference_To (Ptyp, Loc),
2095                      Attribute_Name => Name_Last))))));
2096
2097          Analyze_And_Resolve (N, Typ);
2098       end Fore;
2099
2100       --------------
2101       -- Fraction --
2102       --------------
2103
2104       --  Transforms 'Fraction into a call to the floating-point attribute
2105       --  function Fraction in Fat_xxx (where xxx is the root type)
2106
2107       when Attribute_Fraction =>
2108          Expand_Fpt_Attribute_R (N);
2109
2110       --------------
2111       -- From_Any --
2112       --------------
2113
2114       when Attribute_From_Any => From_Any : declare
2115          P_Type : constant Entity_Id := Etype (Pref);
2116          Decls  : constant List_Id   := New_List;
2117       begin
2118          Rewrite (N,
2119            Build_From_Any_Call (P_Type,
2120              Relocate_Node (First (Exprs)),
2121              Decls));
2122          Insert_Actions (N, Decls);
2123          Analyze_And_Resolve (N, P_Type);
2124       end From_Any;
2125
2126       --------------
2127       -- Identity --
2128       --------------
2129
2130       --  For an exception returns a reference to the exception data:
2131       --      Exception_Id!(Prefix'Reference)
2132
2133       --  For a task it returns a reference to the _task_id component of
2134       --  corresponding record:
2135
2136       --    taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
2137
2138       --  in Ada.Task_Identification
2139
2140       when Attribute_Identity => Identity : declare
2141          Id_Kind : Entity_Id;
2142
2143       begin
2144          if Ptyp = Standard_Exception_Type then
2145             Id_Kind := RTE (RE_Exception_Id);
2146
2147             if Present (Renamed_Object (Entity (Pref))) then
2148                Set_Entity (Pref, Renamed_Object (Entity (Pref)));
2149             end if;
2150
2151             Rewrite (N,
2152               Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
2153          else
2154             Id_Kind := RTE (RO_AT_Task_Id);
2155
2156             --  If the prefix is a task interface, the Task_Id is obtained
2157             --  dynamically through a dispatching call, as for other task
2158             --  attributes applied to interfaces.
2159
2160             if Ada_Version >= Ada_05
2161               and then Ekind (Ptyp) = E_Class_Wide_Type
2162               and then Is_Interface (Ptyp)
2163               and then Is_Task_Interface (Ptyp)
2164             then
2165                Rewrite (N,
2166                  Unchecked_Convert_To (Id_Kind,
2167                    Make_Selected_Component (Loc,
2168                      Prefix =>
2169                        New_Copy_Tree (Pref),
2170                      Selector_Name =>
2171                        Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
2172
2173             else
2174                Rewrite (N,
2175                  Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
2176             end if;
2177          end if;
2178
2179          Analyze_And_Resolve (N, Id_Kind);
2180       end Identity;
2181
2182       -----------
2183       -- Image --
2184       -----------
2185
2186       --  Image attribute is handled in separate unit Exp_Imgv
2187
2188       when Attribute_Image =>
2189          Exp_Imgv.Expand_Image_Attribute (N);
2190
2191       ---------
2192       -- Img --
2193       ---------
2194
2195       --  X'Img is expanded to typ'Image (X), where typ is the type of X
2196
2197       when Attribute_Img => Img :
2198       begin
2199          Rewrite (N,
2200            Make_Attribute_Reference (Loc,
2201              Prefix => New_Reference_To (Ptyp, Loc),
2202              Attribute_Name => Name_Image,
2203              Expressions => New_List (Relocate_Node (Pref))));
2204
2205          Analyze_And_Resolve (N, Standard_String);
2206       end Img;
2207
2208       -----------
2209       -- Input --
2210       -----------
2211
2212       when Attribute_Input => Input : declare
2213          P_Type : constant Entity_Id := Entity (Pref);
2214          B_Type : constant Entity_Id := Base_Type (P_Type);
2215          U_Type : constant Entity_Id := Underlying_Type (P_Type);
2216          Strm   : constant Node_Id   := First (Exprs);
2217          Fname  : Entity_Id;
2218          Decl   : Node_Id;
2219          Call   : Node_Id;
2220          Prag   : Node_Id;
2221          Arg2   : Node_Id;
2222          Rfunc  : Node_Id;
2223
2224          Cntrl  : Node_Id := Empty;
2225          --  Value for controlling argument in call. Always Empty except in
2226          --  the dispatching (class-wide type) case, where it is a reference
2227          --  to the dummy object initialized to the right internal tag.
2228
2229          procedure Freeze_Stream_Subprogram (F : Entity_Id);
2230          --  The expansion of the attribute reference may generate a call to
2231          --  a user-defined stream subprogram that is frozen by the call. This
2232          --  can lead to access-before-elaboration problem if the reference
2233          --  appears in an object declaration and the subprogram body has not
2234          --  been seen. The freezing of the subprogram requires special code
2235          --  because it appears in an expanded context where expressions do
2236          --  not freeze their constituents.
2237
2238          ------------------------------
2239          -- Freeze_Stream_Subprogram --
2240          ------------------------------
2241
2242          procedure Freeze_Stream_Subprogram (F : Entity_Id) is
2243             Decl : constant Node_Id := Unit_Declaration_Node (F);
2244             Bod  : Node_Id;
2245
2246          begin
2247             --  If this is user-defined subprogram, the corresponding
2248             --  stream function appears as a renaming-as-body, and the
2249             --  user subprogram must be retrieved by tree traversal.
2250
2251             if Present (Decl)
2252               and then Nkind (Decl) = N_Subprogram_Declaration
2253               and then Present (Corresponding_Body (Decl))
2254             then
2255                Bod := Corresponding_Body (Decl);
2256
2257                if Nkind (Unit_Declaration_Node (Bod)) =
2258                  N_Subprogram_Renaming_Declaration
2259                then
2260                   Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
2261                end if;
2262             end if;
2263          end Freeze_Stream_Subprogram;
2264
2265       --  Start of processing for Input
2266
2267       begin
2268          --  If no underlying type, we have an error that will be diagnosed
2269          --  elsewhere, so here we just completely ignore the expansion.
2270
2271          if No (U_Type) then
2272             return;
2273          end if;
2274
2275          --  If there is a TSS for Input, just call it
2276
2277          Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
2278
2279          if Present (Fname) then
2280             null;
2281
2282          else
2283             --  If there is a Stream_Convert pragma, use it, we rewrite
2284
2285             --     sourcetyp'Input (stream)
2286
2287             --  as
2288
2289             --     sourcetyp (streamread (strmtyp'Input (stream)));
2290
2291             --  where streamread is the given Read function that converts an
2292             --  argument of type strmtyp to type sourcetyp or a type from which
2293             --  it is derived (extra conversion required for the derived case).
2294
2295             Prag := Get_Stream_Convert_Pragma (P_Type);
2296
2297             if Present (Prag) then
2298                Arg2  := Next (First (Pragma_Argument_Associations (Prag)));
2299                Rfunc := Entity (Expression (Arg2));
2300
2301                Rewrite (N,
2302                  Convert_To (B_Type,
2303                    Make_Function_Call (Loc,
2304                      Name => New_Occurrence_Of (Rfunc, Loc),
2305                      Parameter_Associations => New_List (
2306                        Make_Attribute_Reference (Loc,
2307                          Prefix =>
2308                            New_Occurrence_Of
2309                              (Etype (First_Formal (Rfunc)), Loc),
2310                          Attribute_Name => Name_Input,
2311                          Expressions => Exprs)))));
2312
2313                Analyze_And_Resolve (N, B_Type);
2314                return;
2315
2316             --  Elementary types
2317
2318             elsif Is_Elementary_Type (U_Type) then
2319
2320                --  A special case arises if we have a defined _Read routine,
2321                --  since in this case we are required to call this routine.
2322
2323                if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
2324                   Build_Record_Or_Elementary_Input_Function
2325                     (Loc, U_Type, Decl, Fname);
2326                   Insert_Action (N, Decl);
2327
2328                --  For normal cases, we call the I_xxx routine directly
2329
2330                else
2331                   Rewrite (N, Build_Elementary_Input_Call (N));
2332                   Analyze_And_Resolve (N, P_Type);
2333                   return;
2334                end if;
2335
2336             --  Array type case
2337
2338             elsif Is_Array_Type (U_Type) then
2339                Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
2340                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2341
2342             --  Dispatching case with class-wide type
2343
2344             elsif Is_Class_Wide_Type (P_Type) then
2345
2346                --  No need to do anything else compiling under restriction
2347                --  No_Dispatching_Calls. During the semantic analysis we
2348                --  already notified such violation.
2349
2350                if Restriction_Active (No_Dispatching_Calls) then
2351                   return;
2352                end if;
2353
2354                declare
2355                   Rtyp : constant Entity_Id := Root_Type (P_Type);
2356                   Dnn  : Entity_Id;
2357                   Decl : Node_Id;
2358
2359                begin
2360                   --  Read the internal tag (RM 13.13.2(34)) and use it to
2361                   --  initialize a dummy tag object:
2362
2363                   --    Dnn : Ada.Tags.Tag
2364                   --           := Descendant_Tag (String'Input (Strm), P_Type);
2365
2366                   --  This dummy object is used only to provide a controlling
2367                   --  argument for the eventual _Input call. Descendant_Tag is
2368                   --  called rather than Internal_Tag to ensure that we have a
2369                   --  tag for a type that is descended from the prefix type and
2370                   --  declared at the same accessibility level (the exception
2371                   --  Tag_Error will be raised otherwise). The level check is
2372                   --  required for Ada 2005 because tagged types can be
2373                   --  extended in nested scopes (AI-344).
2374
2375                   Dnn :=
2376                     Make_Defining_Identifier (Loc,
2377                       Chars => New_Internal_Name ('D'));
2378
2379                   Decl :=
2380                     Make_Object_Declaration (Loc,
2381                       Defining_Identifier => Dnn,
2382                       Object_Definition =>
2383                         New_Occurrence_Of (RTE (RE_Tag), Loc),
2384                       Expression =>
2385                         Make_Function_Call (Loc,
2386                           Name =>
2387                             New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
2388                           Parameter_Associations => New_List (
2389                             Make_Attribute_Reference (Loc,
2390                               Prefix =>
2391                                 New_Occurrence_Of (Standard_String, Loc),
2392                               Attribute_Name => Name_Input,
2393                               Expressions => New_List (
2394                                 Relocate_Node
2395                                   (Duplicate_Subexpr (Strm)))),
2396                             Make_Attribute_Reference (Loc,
2397                               Prefix => New_Reference_To (P_Type, Loc),
2398                               Attribute_Name => Name_Tag))));
2399
2400                   Insert_Action (N, Decl);
2401
2402                   --  Now we need to get the entity for the call, and construct
2403                   --  a function call node, where we preset a reference to Dnn
2404                   --  as the controlling argument (doing an unchecked convert
2405                   --  to the class-wide tagged type to make it look like a real
2406                   --  tagged object).
2407
2408                   Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
2409                   Cntrl := Unchecked_Convert_To (P_Type,
2410                              New_Occurrence_Of (Dnn, Loc));
2411                   Set_Etype (Cntrl, P_Type);
2412                   Set_Parent (Cntrl, N);
2413                end;
2414
2415             --  For tagged types, use the primitive Input function
2416
2417             elsif Is_Tagged_Type (U_Type) then
2418                Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
2419
2420             --  All other record type cases, including protected records. The
2421             --  latter only arise for expander generated code for handling
2422             --  shared passive partition access.
2423
2424             else
2425                pragma Assert
2426                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2427
2428                --  Ada 2005 (AI-216): Program_Error is raised executing default
2429                --  implementation of the Input attribute of an unchecked union
2430                --  type if the type lacks default discriminant values.
2431
2432                if Is_Unchecked_Union (Base_Type (U_Type))
2433                  and then No (Discriminant_Constraint (U_Type))
2434                then
2435                   Insert_Action (N,
2436                     Make_Raise_Program_Error (Loc,
2437                       Reason => PE_Unchecked_Union_Restriction));
2438
2439                   return;
2440                end if;
2441
2442                Build_Record_Or_Elementary_Input_Function
2443                  (Loc, Base_Type (U_Type), Decl, Fname);
2444                Insert_Action (N, Decl);
2445
2446                if Nkind (Parent (N)) = N_Object_Declaration
2447                  and then Is_Record_Type (U_Type)
2448                then
2449                   --  The stream function may contain calls to user-defined
2450                   --  Read procedures for individual components.
2451
2452                   declare
2453                      Comp : Entity_Id;
2454                      Func : Entity_Id;
2455
2456                   begin
2457                      Comp := First_Component (U_Type);
2458                      while Present (Comp) loop
2459                         Func :=
2460                           Find_Stream_Subprogram
2461                             (Etype (Comp), TSS_Stream_Read);
2462
2463                         if Present (Func) then
2464                            Freeze_Stream_Subprogram (Func);
2465                         end if;
2466
2467                         Next_Component (Comp);
2468                      end loop;
2469                   end;
2470                end if;
2471             end if;
2472          end if;
2473
2474          --  If we fall through, Fname is the function to be called. The result
2475          --  is obtained by calling the appropriate function, then converting
2476          --  the result. The conversion does a subtype check.
2477
2478          Call :=
2479            Make_Function_Call (Loc,
2480              Name => New_Occurrence_Of (Fname, Loc),
2481              Parameter_Associations => New_List (
2482                 Relocate_Node (Strm)));
2483
2484          Set_Controlling_Argument (Call, Cntrl);
2485          Rewrite (N, Unchecked_Convert_To (P_Type, Call));
2486          Analyze_And_Resolve (N, P_Type);
2487
2488          if Nkind (Parent (N)) = N_Object_Declaration then
2489             Freeze_Stream_Subprogram (Fname);
2490          end if;
2491       end Input;
2492
2493       -------------------
2494       -- Integer_Value --
2495       -------------------
2496
2497       --  We transform
2498
2499       --    inttype'Fixed_Value (fixed-value)
2500
2501       --  into
2502
2503       --    inttype(integer-value))
2504
2505       --  we do all the required analysis of the conversion here, because we do
2506       --  not want this to go through the fixed-point conversion circuits. Note
2507       --  that the back end always treats fixed-point as equivalent to the
2508       --  corresponding integer type anyway.
2509
2510       when Attribute_Integer_Value => Integer_Value :
2511       begin
2512          Rewrite (N,
2513            Make_Type_Conversion (Loc,
2514              Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2515              Expression   => Relocate_Node (First (Exprs))));
2516          Set_Etype (N, Entity (Pref));
2517          Set_Analyzed (N);
2518
2519       --  Note: it might appear that a properly analyzed unchecked conversion
2520       --  would be just fine here, but that's not the case, since the full
2521       --  range checks performed by the following call are critical!
2522
2523          Apply_Type_Conversion_Checks (N);
2524       end Integer_Value;
2525
2526       -------------------
2527       -- Invalid_Value --
2528       -------------------
2529
2530       when Attribute_Invalid_Value =>
2531          Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
2532
2533       ----------
2534       -- Last --
2535       ----------
2536
2537       when Attribute_Last =>
2538
2539          --  If the prefix type is a constrained packed array type which
2540          --  already has a Packed_Array_Type representation defined, then
2541          --  replace this attribute with a direct reference to 'Last of the
2542          --  appropriate index subtype (since otherwise the back end will try
2543          --  to give us the value of 'Last for this implementation type).
2544
2545          if Is_Constrained_Packed_Array (Ptyp) then
2546             Rewrite (N,
2547               Make_Attribute_Reference (Loc,
2548                 Attribute_Name => Name_Last,
2549                 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2550             Analyze_And_Resolve (N, Typ);
2551
2552          elsif Is_Access_Type (Ptyp) then
2553             Apply_Access_Check (N);
2554          end if;
2555
2556       --------------
2557       -- Last_Bit --
2558       --------------
2559
2560       --  We compute this if a component clause was present, otherwise we leave
2561       --  the computation up to the back end, since we don't know what layout
2562       --  will be chosen.
2563
2564       when Attribute_Last_Bit => Last_Bit : declare
2565          CE : constant Entity_Id := Entity (Selector_Name (Pref));
2566
2567       begin
2568          if Known_Static_Component_Bit_Offset (CE)
2569            and then Known_Static_Esize (CE)
2570          then
2571             Rewrite (N,
2572               Make_Integer_Literal (Loc,
2573                Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2574                                 + Esize (CE) - 1));
2575
2576             Analyze_And_Resolve (N, Typ);
2577
2578          else
2579             Apply_Universal_Integer_Attribute_Checks (N);
2580          end if;
2581       end Last_Bit;
2582
2583       ------------------
2584       -- Leading_Part --
2585       ------------------
2586
2587       --  Transforms 'Leading_Part into a call to the floating-point attribute
2588       --  function Leading_Part in Fat_xxx (where xxx is the root type)
2589
2590       --  Note: strictly, we should generate special case code to deal with
2591       --  absurdly large positive arguments (greater than Integer'Last), which
2592       --  result in returning the first argument unchanged, but it hardly seems
2593       --  worth the effort. We raise constraint error for absurdly negative
2594       --  arguments which is fine.
2595
2596       when Attribute_Leading_Part =>
2597          Expand_Fpt_Attribute_RI (N);
2598
2599       ------------
2600       -- Length --
2601       ------------
2602
2603       when Attribute_Length => declare
2604          Ityp : Entity_Id;
2605          Xnum : Uint;
2606
2607       begin
2608          --  Processing for packed array types
2609
2610          if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2611             Ityp := Get_Index_Subtype (N);
2612
2613             --  If the index type, Ityp, is an enumeration type with holes,
2614             --  then we calculate X'Length explicitly using
2615
2616             --     Typ'Max
2617             --       (0, Ityp'Pos (X'Last  (N)) -
2618             --           Ityp'Pos (X'First (N)) + 1);
2619
2620             --  Since the bounds in the template are the representation values
2621             --  and the back end would get the wrong value.
2622
2623             if Is_Enumeration_Type (Ityp)
2624               and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2625             then
2626                if No (Exprs) then
2627                   Xnum := Uint_1;
2628                else
2629                   Xnum := Expr_Value (First (Expressions (N)));
2630                end if;
2631
2632                Rewrite (N,
2633                  Make_Attribute_Reference (Loc,
2634                    Prefix         => New_Occurrence_Of (Typ, Loc),
2635                    Attribute_Name => Name_Max,
2636                    Expressions    => New_List
2637                      (Make_Integer_Literal (Loc, 0),
2638
2639                       Make_Op_Add (Loc,
2640                         Left_Opnd =>
2641                           Make_Op_Subtract (Loc,
2642                             Left_Opnd =>
2643                               Make_Attribute_Reference (Loc,
2644                                 Prefix => New_Occurrence_Of (Ityp, Loc),
2645                                 Attribute_Name => Name_Pos,
2646
2647                                 Expressions => New_List (
2648                                   Make_Attribute_Reference (Loc,
2649                                     Prefix => Duplicate_Subexpr (Pref),
2650                                    Attribute_Name => Name_Last,
2651                                     Expressions => New_List (
2652                                       Make_Integer_Literal (Loc, Xnum))))),
2653
2654                             Right_Opnd =>
2655                               Make_Attribute_Reference (Loc,
2656                                 Prefix => New_Occurrence_Of (Ityp, Loc),
2657                                 Attribute_Name => Name_Pos,
2658
2659                                 Expressions => New_List (
2660                                   Make_Attribute_Reference (Loc,
2661                                     Prefix =>
2662                                       Duplicate_Subexpr_No_Checks (Pref),
2663                                    Attribute_Name => Name_First,
2664                                     Expressions => New_List (
2665                                       Make_Integer_Literal (Loc, Xnum)))))),
2666
2667                         Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2668
2669                Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2670                return;
2671
2672             --  If the prefix type is a constrained packed array type which
2673             --  already has a Packed_Array_Type representation defined, then
2674             --  replace this attribute with a direct reference to 'Range_Length
2675             --  of the appropriate index subtype (since otherwise the back end
2676             --  will try to give us the value of 'Length for this
2677             --  implementation type).
2678
2679             elsif Is_Constrained (Ptyp) then
2680                Rewrite (N,
2681                  Make_Attribute_Reference (Loc,
2682                    Attribute_Name => Name_Range_Length,
2683                    Prefix => New_Reference_To (Ityp, Loc)));
2684                Analyze_And_Resolve (N, Typ);
2685             end if;
2686
2687          --  Access type case
2688
2689          elsif Is_Access_Type (Ptyp) then
2690             Apply_Access_Check (N);
2691
2692             --  If the designated type is a packed array type, then we convert
2693             --  the reference to:
2694
2695             --    typ'Max (0, 1 +
2696             --                xtyp'Pos (Pref'Last (Expr)) -
2697             --                xtyp'Pos (Pref'First (Expr)));
2698
2699             --  This is a bit complex, but it is the easiest thing to do that
2700             --  works in all cases including enum types with holes xtyp here
2701             --  is the appropriate index type.
2702
2703             declare
2704                Dtyp : constant Entity_Id := Designated_Type (Ptyp);
2705                Xtyp : Entity_Id;
2706
2707             begin
2708                if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
2709                   Xtyp := Get_Index_Subtype (N);
2710
2711                   Rewrite (N,
2712                     Make_Attribute_Reference (Loc,
2713                       Prefix         => New_Occurrence_Of (Typ, Loc),
2714                       Attribute_Name => Name_Max,
2715                       Expressions    => New_List (
2716                         Make_Integer_Literal (Loc, 0),
2717
2718                         Make_Op_Add (Loc,
2719                           Make_Integer_Literal (Loc, 1),
2720                           Make_Op_Subtract (Loc,
2721                             Left_Opnd =>
2722                               Make_Attribute_Reference (Loc,
2723                                 Prefix => New_Occurrence_Of (Xtyp, Loc),
2724                                 Attribute_Name => Name_Pos,
2725                                 Expressions    => New_List (
2726                                   Make_Attribute_Reference (Loc,
2727                                     Prefix => Duplicate_Subexpr (Pref),
2728                                     Attribute_Name => Name_Last,
2729                                     Expressions =>
2730                                       New_Copy_List (Exprs)))),
2731
2732                             Right_Opnd =>
2733                               Make_Attribute_Reference (Loc,
2734                                 Prefix => New_Occurrence_Of (Xtyp, Loc),
2735                                 Attribute_Name => Name_Pos,
2736                                 Expressions    => New_List (
2737                                   Make_Attribute_Reference (Loc,
2738                                     Prefix =>
2739                                       Duplicate_Subexpr_No_Checks (Pref),
2740                                     Attribute_Name => Name_First,
2741                                     Expressions =>
2742                                       New_Copy_List (Exprs)))))))));
2743
2744                   Analyze_And_Resolve (N, Typ);
2745                end if;
2746             end;
2747
2748          --  Otherwise leave it to the back end
2749
2750          else
2751             Apply_Universal_Integer_Attribute_Checks (N);
2752          end if;
2753       end;
2754
2755       -------------
2756       -- Machine --
2757       -------------
2758
2759       --  Transforms 'Machine into a call to the floating-point attribute
2760       --  function Machine in Fat_xxx (where xxx is the root type)
2761
2762       when Attribute_Machine =>
2763          Expand_Fpt_Attribute_R (N);
2764
2765       ----------------------
2766       -- Machine_Rounding --
2767       ----------------------
2768
2769       --  Transforms 'Machine_Rounding into a call to the floating-point
2770       --  attribute function Machine_Rounding in Fat_xxx (where xxx is the root
2771       --  type). Expansion is avoided for cases the back end can handle
2772       --  directly.
2773
2774       when Attribute_Machine_Rounding =>
2775          if not Is_Inline_Floating_Point_Attribute (N) then
2776             Expand_Fpt_Attribute_R (N);
2777          end if;
2778
2779       ------------------
2780       -- Machine_Size --
2781       ------------------
2782
2783       --  Machine_Size is equivalent to Object_Size, so transform it into
2784       --  Object_Size and that way the back end never sees Machine_Size.
2785
2786       when Attribute_Machine_Size =>
2787          Rewrite (N,
2788            Make_Attribute_Reference (Loc,
2789              Prefix => Prefix (N),
2790              Attribute_Name => Name_Object_Size));
2791
2792          Analyze_And_Resolve (N, Typ);
2793
2794       --------------
2795       -- Mantissa --
2796       --------------
2797
2798       --  The only case that can get this far is the dynamic case of the old
2799       --  Ada 83 Mantissa attribute for the fixed-point case. For this case,
2800       --  we expand:
2801
2802       --    typ'Mantissa
2803
2804       --  into
2805
2806       --    ityp (System.Mantissa.Mantissa_Value
2807       --           (Integer'Integer_Value (typ'First),
2808       --            Integer'Integer_Value (typ'Last)));
2809
2810       when Attribute_Mantissa => Mantissa : begin
2811          Rewrite (N,
2812            Convert_To (Typ,
2813              Make_Function_Call (Loc,
2814                Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2815
2816                Parameter_Associations => New_List (
2817
2818                  Make_Attribute_Reference (Loc,
2819                    Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2820                    Attribute_Name => Name_Integer_Value,
2821                    Expressions => New_List (
2822
2823                      Make_Attribute_Reference (Loc,
2824                        Prefix => New_Occurrence_Of (Ptyp, Loc),
2825                        Attribute_Name => Name_First))),
2826
2827                  Make_Attribute_Reference (Loc,
2828                    Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2829                    Attribute_Name => Name_Integer_Value,
2830                    Expressions => New_List (
2831
2832                      Make_Attribute_Reference (Loc,
2833                        Prefix => New_Occurrence_Of (Ptyp, Loc),
2834                        Attribute_Name => Name_Last)))))));
2835
2836          Analyze_And_Resolve (N, Typ);
2837       end Mantissa;
2838
2839       --------------------
2840       -- Mechanism_Code --
2841       --------------------
2842
2843       when Attribute_Mechanism_Code =>
2844
2845          --  We must replace the prefix in the renamed case
2846
2847          if Is_Entity_Name (Pref)
2848            and then Present (Alias (Entity (Pref)))
2849          then
2850             Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
2851          end if;
2852
2853       ---------
2854       -- Mod --
2855       ---------
2856
2857       when Attribute_Mod => Mod_Case : declare
2858          Arg  : constant Node_Id := Relocate_Node (First (Exprs));
2859          Hi   : constant Node_Id := Type_High_Bound (Etype (Arg));
2860          Modv : constant Uint    := Modulus (Btyp);
2861
2862       begin
2863
2864          --  This is not so simple. The issue is what type to use for the
2865          --  computation of the modular value.
2866
2867          --  The easy case is when the modulus value is within the bounds
2868          --  of the signed integer type of the argument. In this case we can
2869          --  just do the computation in that signed integer type, and then
2870          --  do an ordinary conversion to the target type.
2871
2872          if Modv <= Expr_Value (Hi) then
2873             Rewrite (N,
2874               Convert_To (Btyp,
2875                 Make_Op_Mod (Loc,
2876                   Left_Opnd  => Arg,
2877                   Right_Opnd => Make_Integer_Literal (Loc, Modv))));
2878
2879          --  Here we know that the modulus is larger than type'Last of the
2880          --  integer type. There are two cases to consider:
2881
2882          --    a) The integer value is non-negative. In this case, it is
2883          --    returned as the result (since it is less than the modulus).
2884
2885          --    b) The integer value is negative. In this case, we know that the
2886          --    result is modulus + value, where the value might be as small as
2887          --    -modulus. The trouble is what type do we use to do the subtract.
2888          --    No type will do, since modulus can be as big as 2**64, and no
2889          --    integer type accommodates this value. Let's do bit of algebra
2890
2891          --         modulus + value
2892          --      =  modulus - (-value)
2893          --      =  (modulus - 1) - (-value - 1)
2894
2895          --    Now modulus - 1 is certainly in range of the modular type.
2896          --    -value is in the range 1 .. modulus, so -value -1 is in the
2897          --    range 0 .. modulus-1 which is in range of the modular type.
2898          --    Furthermore, (-value - 1) can be expressed as -(value + 1)
2899          --    which we can compute using the integer base type.
2900
2901          --  Once this is done we analyze the conditional expression without
2902          --  range checks, because we know everything is in range, and we
2903          --  want to prevent spurious warnings on either branch.
2904
2905          else
2906             Rewrite (N,
2907               Make_Conditional_Expression (Loc,
2908                 Expressions => New_List (
2909                   Make_Op_Ge (Loc,
2910                     Left_Opnd  => Duplicate_Subexpr (Arg),
2911                     Right_Opnd => Make_Integer_Literal (Loc, 0)),
2912
2913                   Convert_To (Btyp,
2914                     Duplicate_Subexpr_No_Checks (Arg)),
2915
2916                   Make_Op_Subtract (Loc,
2917                     Left_Opnd =>
2918                       Make_Integer_Literal (Loc,
2919                         Intval => Modv - 1),
2920                     Right_Opnd =>
2921                       Convert_To (Btyp,
2922                         Make_Op_Minus (Loc,
2923                           Right_Opnd =>
2924                             Make_Op_Add (Loc,
2925                               Left_Opnd  => Duplicate_Subexpr_No_Checks (Arg),
2926                               Right_Opnd =>
2927                                 Make_Integer_Literal (Loc,
2928                                   Intval => 1))))))));
2929
2930          end if;
2931
2932          Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
2933       end Mod_Case;
2934
2935       -----------
2936       -- Model --
2937       -----------
2938
2939       --  Transforms 'Model into a call to the floating-point attribute
2940       --  function Model in Fat_xxx (where xxx is the root type)
2941
2942       when Attribute_Model =>
2943          Expand_Fpt_Attribute_R (N);
2944
2945       -----------------
2946       -- Object_Size --
2947       -----------------
2948
2949       --  The processing for Object_Size shares the processing for Size
2950
2951       ---------
2952       -- Old --
2953       ---------
2954
2955       when Attribute_Old => Old : declare
2956          Tnn     : constant Entity_Id :=
2957                      Make_Defining_Identifier (Loc,
2958                        Chars => New_Internal_Name ('T'));
2959          Subp    : Node_Id;
2960          Asn_Stm : Node_Id;
2961
2962       begin
2963          --  Find the nearest subprogram body, ignoring _Preconditions
2964
2965          Subp := N;
2966          loop
2967             Subp := Parent (Subp);
2968             exit when Nkind (Subp) = N_Subprogram_Body
2969               and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions;
2970          end loop;
2971
2972          --  Insert the assignment at the start of the declarations
2973
2974          Asn_Stm :=
2975            Make_Object_Declaration (Loc,
2976              Defining_Identifier => Tnn,
2977              Constant_Present    => True,
2978              Object_Definition   => New_Occurrence_Of (Etype (N), Loc),
2979              Expression          => Pref);
2980
2981          if Is_Empty_List (Declarations (Subp)) then
2982             Set_Declarations (Subp, New_List (Asn_Stm));
2983             Analyze (Asn_Stm);
2984          else
2985             Insert_Action (First (Declarations (Subp)), Asn_Stm);
2986          end if;
2987
2988          Rewrite (N, New_Occurrence_Of (Tnn, Loc));
2989       end Old;
2990
2991       ------------
2992       -- Output --
2993       ------------
2994
2995       when Attribute_Output => Output : declare
2996          P_Type : constant Entity_Id := Entity (Pref);
2997          U_Type : constant Entity_Id := Underlying_Type (P_Type);
2998          Pname  : Entity_Id;
2999          Decl   : Node_Id;
3000          Prag   : Node_Id;
3001          Arg3   : Node_Id;
3002          Wfunc  : Node_Id;
3003
3004       begin
3005          --  If no underlying type, we have an error that will be diagnosed
3006          --  elsewhere, so here we just completely ignore the expansion.
3007
3008          if No (U_Type) then
3009             return;
3010          end if;
3011
3012          --  If TSS for Output is present, just call it
3013
3014          Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
3015
3016          if Present (Pname) then
3017             null;
3018
3019          else
3020             --  If there is a Stream_Convert pragma, use it, we rewrite
3021
3022             --     sourcetyp'Output (stream, Item)
3023
3024             --  as
3025
3026             --     strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3027
3028             --  where strmwrite is the given Write function that converts an
3029             --  argument of type sourcetyp or a type acctyp, from which it is
3030             --  derived to type strmtyp. The conversion to acttyp is required
3031             --  for the derived case.
3032
3033             Prag := Get_Stream_Convert_Pragma (P_Type);
3034
3035             if Present (Prag) then
3036                Arg3 :=
3037                  Next (Next (First (Pragma_Argument_Associations (Prag))));
3038                Wfunc := Entity (Expression (Arg3));
3039
3040                Rewrite (N,
3041                  Make_Attribute_Reference (Loc,
3042                    Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3043                    Attribute_Name => Name_Output,
3044                    Expressions => New_List (
3045                    Relocate_Node (First (Exprs)),
3046                      Make_Function_Call (Loc,
3047                        Name => New_Occurrence_Of (Wfunc, Loc),
3048                        Parameter_Associations => New_List (
3049                          OK_Convert_To (Etype (First_Formal (Wfunc)),
3050                            Relocate_Node (Next (First (Exprs)))))))));
3051
3052                Analyze (N);
3053                return;
3054
3055             --  For elementary types, we call the W_xxx routine directly.
3056             --  Note that the effect of Write and Output is identical for
3057             --  the case of an elementary type, since there are no
3058             --  discriminants or bounds.
3059
3060             elsif Is_Elementary_Type (U_Type) then
3061
3062                --  A special case arises if we have a defined _Write routine,
3063                --  since in this case we are required to call this routine.
3064
3065                if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
3066                   Build_Record_Or_Elementary_Output_Procedure
3067                     (Loc, U_Type, Decl, Pname);
3068                   Insert_Action (N, Decl);
3069
3070                --  For normal cases, we call the W_xxx routine directly
3071
3072                else
3073                   Rewrite (N, Build_Elementary_Write_Call (N));
3074                   Analyze (N);
3075                   return;
3076                end if;
3077
3078             --  Array type case
3079
3080             elsif Is_Array_Type (U_Type) then
3081                Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
3082                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3083
3084             --  Class-wide case, first output external tag, then dispatch
3085             --  to the appropriate primitive Output function (RM 13.13.2(31)).
3086
3087             elsif Is_Class_Wide_Type (P_Type) then
3088
3089                --  No need to do anything else compiling under restriction
3090                --  No_Dispatching_Calls. During the semantic analysis we
3091                --  already notified such violation.
3092
3093                if Restriction_Active (No_Dispatching_Calls) then
3094                   return;
3095                end if;
3096
3097                Tag_Write : declare
3098                   Strm : constant Node_Id := First (Exprs);
3099                   Item : constant Node_Id := Next (Strm);
3100
3101                begin
3102                   --  Ada 2005 (AI-344): Check that the accessibility level
3103                   --  of the type of the output object is not deeper than
3104                   --  that of the attribute's prefix type.
3105
3106                   --  if Get_Access_Level (Item'Tag)
3107                   --       /= Get_Access_Level (P_Type'Tag)
3108                   --  then
3109                   --     raise Tag_Error;
3110                   --  end if;
3111
3112                   --  String'Output (Strm, External_Tag (Item'Tag));
3113
3114                   --  We cannot figure out a practical way to implement this
3115                   --  accessibility check on virtual machines, so we omit it.
3116
3117                   if Ada_Version >= Ada_05
3118                     and then VM_Target = No_VM
3119                   then
3120                      Insert_Action (N,
3121                        Make_Implicit_If_Statement (N,
3122                          Condition =>
3123                            Make_Op_Ne (Loc,
3124                              Left_Opnd  =>
3125                                Build_Get_Access_Level (Loc,
3126                                  Make_Attribute_Reference (Loc,
3127                                    Prefix         =>
3128                                      Relocate_Node (
3129                                        Duplicate_Subexpr (Item,
3130                                          Name_Req => True)),
3131                                    Attribute_Name => Name_Tag)),
3132
3133                              Right_Opnd =>
3134                                Make_Integer_Literal (Loc,
3135                                  Type_Access_Level (P_Type))),
3136
3137                          Then_Statements =>
3138                            New_List (Make_Raise_Statement (Loc,
3139                                        New_Occurrence_Of (
3140                                          RTE (RE_Tag_Error), Loc)))));
3141                   end if;
3142
3143                   Insert_Action (N,
3144                     Make_Attribute_Reference (Loc,
3145                       Prefix => New_Occurrence_Of (Standard_String, Loc),
3146                       Attribute_Name => Name_Output,
3147                       Expressions => New_List (
3148                         Relocate_Node (Duplicate_Subexpr (Strm)),
3149                         Make_Function_Call (Loc,
3150                           Name =>
3151                             New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3152                           Parameter_Associations => New_List (
3153                            Make_Attribute_Reference (Loc,
3154                              Prefix =>
3155                                Relocate_Node
3156                                  (Duplicate_Subexpr (Item, Name_Req => True)),
3157                              Attribute_Name => Name_Tag))))));
3158                end Tag_Write;
3159
3160                Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3161
3162             --  Tagged type case, use the primitive Output function
3163
3164             elsif Is_Tagged_Type (U_Type) then
3165                Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3166
3167             --  All other record type cases, including protected records.
3168             --  The latter only arise for expander generated code for
3169             --  handling shared passive partition access.
3170
3171             else
3172                pragma Assert
3173                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3174
3175                --  Ada 2005 (AI-216): Program_Error is raised when executing
3176                --  the default implementation of the Output attribute of an
3177                --  unchecked union type if the type lacks default discriminant
3178                --  values.
3179
3180                if Is_Unchecked_Union (Base_Type (U_Type))
3181                  and then No (Discriminant_Constraint (U_Type))
3182                then
3183                   Insert_Action (N,
3184                     Make_Raise_Program_Error (Loc,
3185                       Reason => PE_Unchecked_Union_Restriction));
3186
3187                   return;
3188                end if;
3189
3190                Build_Record_Or_Elementary_Output_Procedure
3191                  (Loc, Base_Type (U_Type), Decl, Pname);
3192                Insert_Action (N, Decl);
3193             end if;
3194          end if;
3195
3196          --  If we fall through, Pname is the name of the procedure to call
3197
3198          Rewrite_Stream_Proc_Call (Pname);
3199       end Output;
3200
3201       ---------
3202       -- Pos --
3203       ---------
3204
3205       --  For enumeration types with a standard representation, Pos is
3206       --  handled by the back end.
3207
3208       --  For enumeration types, with a non-standard representation we
3209       --  generate a call to the _Rep_To_Pos function created when the
3210       --  type was frozen. The call has the form
3211
3212       --    _rep_to_pos (expr, flag)
3213
3214       --  The parameter flag is True if range checks are enabled, causing
3215       --  Program_Error to be raised if the expression has an invalid
3216       --  representation, and False if range checks are suppressed.
3217
3218       --  For integer types, Pos is equivalent to a simple integer
3219       --  conversion and we rewrite it as such
3220
3221       when Attribute_Pos => Pos :
3222       declare
3223          Etyp : Entity_Id := Base_Type (Entity (Pref));
3224
3225       begin
3226          --  Deal with zero/non-zero boolean values
3227
3228          if Is_Boolean_Type (Etyp) then
3229             Adjust_Condition (First (Exprs));
3230             Etyp := Standard_Boolean;
3231             Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
3232          end if;
3233
3234          --  Case of enumeration type
3235
3236          if Is_Enumeration_Type (Etyp) then
3237
3238             --  Non-standard enumeration type (generate call)
3239
3240             if Present (Enum_Pos_To_Rep (Etyp)) then
3241                Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
3242                Rewrite (N,
3243                  Convert_To (Typ,
3244                    Make_Function_Call (Loc,
3245                      Name =>
3246                        New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3247                      Parameter_Associations => Exprs)));
3248
3249                Analyze_And_Resolve (N, Typ);
3250
3251             --  Standard enumeration type (do universal integer check)
3252
3253             else
3254                Apply_Universal_Integer_Attribute_Checks (N);
3255             end if;
3256
3257          --  Deal with integer types (replace by conversion)
3258
3259          elsif Is_Integer_Type (Etyp) then
3260             Rewrite (N, Convert_To (Typ, First (Exprs)));
3261             Analyze_And_Resolve (N, Typ);
3262          end if;
3263
3264       end Pos;
3265
3266       --------------
3267       -- Position --
3268       --------------
3269
3270       --  We compute this if a component clause was present, otherwise we leave
3271       --  the computation up to the back end, since we don't know what layout
3272       --  will be chosen.
3273
3274       when Attribute_Position => Position :
3275       declare
3276          CE : constant Entity_Id := Entity (Selector_Name (Pref));
3277
3278       begin
3279          if Present (Component_Clause (CE)) then
3280             Rewrite (N,
3281               Make_Integer_Literal (Loc,
3282                 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
3283             Analyze_And_Resolve (N, Typ);
3284
3285          else
3286             Apply_Universal_Integer_Attribute_Checks (N);
3287          end if;
3288       end Position;
3289
3290       ----------
3291       -- Pred --
3292       ----------
3293
3294       --  1. Deal with enumeration types with holes
3295       --  2. For floating-point, generate call to attribute function
3296       --  3. For other cases, deal with constraint checking
3297
3298       when Attribute_Pred => Pred :
3299       declare
3300          Etyp : constant Entity_Id := Base_Type (Ptyp);
3301
3302       begin
3303
3304          --  For enumeration types with non-standard representations, we
3305          --  expand typ'Pred (x) into
3306
3307          --    Pos_To_Rep (Rep_To_Pos (x) - 1)
3308
3309          --    If the representation is contiguous, we compute instead
3310          --    Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
3311          --    The conversion function Enum_Pos_To_Rep is defined on the
3312          --    base type, not the subtype, so we have to use the base type
3313          --    explicitly for this and other enumeration attributes.
3314
3315          if Is_Enumeration_Type (Ptyp)
3316            and then Present (Enum_Pos_To_Rep (Etyp))
3317          then
3318             if Has_Contiguous_Rep (Etyp) then
3319                Rewrite (N,
3320                   Unchecked_Convert_To (Ptyp,
3321                      Make_Op_Add (Loc,
3322                         Left_Opnd  =>
3323                          Make_Integer_Literal (Loc,
3324                            Enumeration_Rep (First_Literal (Ptyp))),
3325                         Right_Opnd =>
3326                           Make_Function_Call (Loc,
3327                             Name =>
3328                               New_Reference_To
3329                                (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3330
3331                             Parameter_Associations =>
3332                               New_List (
3333                                 Unchecked_Convert_To (Ptyp,
3334                                   Make_Op_Subtract (Loc,
3335                                     Left_Opnd =>
3336                                      Unchecked_Convert_To (Standard_Integer,
3337                                        Relocate_Node (First (Exprs))),
3338                                     Right_Opnd =>
3339                                       Make_Integer_Literal (Loc, 1))),
3340                                 Rep_To_Pos_Flag (Ptyp, Loc))))));
3341
3342             else
3343                --  Add Boolean parameter True, to request program errror if
3344                --  we have a bad representation on our hands. If checks are
3345                --  suppressed, then add False instead
3346
3347                Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3348                Rewrite (N,
3349                  Make_Indexed_Component (Loc,
3350                    Prefix =>
3351                      New_Reference_To
3352                        (Enum_Pos_To_Rep (Etyp), Loc),
3353                    Expressions => New_List (
3354                      Make_Op_Subtract (Loc,
3355                     Left_Opnd =>
3356                       Make_Function_Call (Loc,
3357                         Name =>
3358                           New_Reference_To
3359                             (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3360                           Parameter_Associations => Exprs),
3361                     Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3362             end if;
3363
3364             Analyze_And_Resolve (N, Typ);
3365
3366          --  For floating-point, we transform 'Pred into a call to the Pred
3367          --  floating-point attribute function in Fat_xxx (xxx is root type)
3368
3369          elsif Is_Floating_Point_Type (Ptyp) then
3370             Expand_Fpt_Attribute_R (N);
3371             Analyze_And_Resolve (N, Typ);
3372
3373          --  For modular types, nothing to do (no overflow, since wraps)
3374
3375          elsif Is_Modular_Integer_Type (Ptyp) then
3376             null;
3377
3378          --  For other types, if range checking is enabled, we must generate
3379          --  a check if overflow checking is enabled.
3380
3381          elsif not Overflow_Checks_Suppressed (Ptyp) then
3382             Expand_Pred_Succ (N);
3383          end if;
3384       end Pred;
3385
3386       --------------
3387       -- Priority --
3388       --------------
3389
3390       --  Ada 2005 (AI-327): Dynamic ceiling priorities
3391
3392       --  We rewrite X'Priority as the following run-time call:
3393
3394       --     Get_Ceiling (X._Object)
3395
3396       --  Note that although X'Priority is notionally an object, it is quite
3397       --  deliberately not defined as an aliased object in the RM. This means
3398       --  that it works fine to rewrite it as a call, without having to worry
3399       --  about complications that would other arise from X'Priority'Access,
3400       --  which is illegal, because of the lack of aliasing.
3401
3402       when Attribute_Priority =>
3403          declare
3404             Call           : Node_Id;
3405             Conctyp        : Entity_Id;
3406             Object_Parm    : Node_Id;
3407             Subprg         : Entity_Id;
3408             RT_Subprg_Name : Node_Id;
3409
3410          begin
3411             --  Look for the enclosing concurrent type
3412
3413             Conctyp := Current_Scope;
3414             while not Is_Concurrent_Type (Conctyp) loop
3415                Conctyp := Scope (Conctyp);
3416             end loop;
3417
3418             pragma Assert (Is_Protected_Type (Conctyp));
3419
3420             --  Generate the actual of the call
3421
3422             Subprg := Current_Scope;
3423             while not Present (Protected_Body_Subprogram (Subprg)) loop
3424                Subprg := Scope (Subprg);
3425             end loop;
3426
3427             --  Use of 'Priority inside protected entries and barriers (in
3428             --  both cases the type of the first formal of their expanded
3429             --  subprogram is Address)
3430
3431             if Etype (First_Entity (Protected_Body_Subprogram (Subprg)))
3432               = RTE (RE_Address)
3433             then
3434                declare
3435                   New_Itype : Entity_Id;
3436
3437                begin
3438                   --  In the expansion of protected entries the type of the
3439                   --  first formal of the Protected_Body_Subprogram is an
3440                   --  Address. In order to reference the _object component
3441                   --  we generate:
3442
3443                   --    type T is access p__ptTV;
3444                   --    freeze T []
3445
3446                   New_Itype := Create_Itype (E_Access_Type, N);
3447                   Set_Etype (New_Itype, New_Itype);
3448                   Set_Directly_Designated_Type (New_Itype,
3449                     Corresponding_Record_Type (Conctyp));
3450                   Freeze_Itype (New_Itype, N);
3451
3452                   --  Generate:
3453                   --    T!(O)._object'unchecked_access
3454
3455                   Object_Parm :=
3456                     Make_Attribute_Reference (Loc,
3457                        Prefix =>
3458                          Make_Selected_Component (Loc,
3459                            Prefix =>
3460                              Unchecked_Convert_To (New_Itype,
3461                                New_Reference_To
3462                                  (First_Entity
3463                                    (Protected_Body_Subprogram (Subprg)),
3464                                   Loc)),
3465                            Selector_Name =>
3466                              Make_Identifier (Loc, Name_uObject)),
3467                        Attribute_Name => Name_Unchecked_Access);
3468                end;
3469
3470             --  Use of 'Priority inside a protected subprogram
3471
3472             else
3473                Object_Parm :=
3474                  Make_Attribute_Reference (Loc,
3475                     Prefix =>
3476                       Make_Selected_Component (Loc,
3477                         Prefix => New_Reference_To
3478                                     (First_Entity
3479                                       (Protected_Body_Subprogram (Subprg)),
3480                                        Loc),
3481                         Selector_Name =>
3482                           Make_Identifier (Loc, Name_uObject)),
3483                     Attribute_Name => Name_Unchecked_Access);
3484             end if;
3485
3486             --  Select the appropriate run-time subprogram
3487
3488             if Number_Entries (Conctyp) = 0 then
3489                RT_Subprg_Name :=
3490                  New_Reference_To (RTE (RE_Get_Ceiling), Loc);
3491             else
3492                RT_Subprg_Name :=
3493                  New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc);
3494             end if;
3495
3496             Call :=
3497               Make_Function_Call (Loc,
3498                 Name => RT_Subprg_Name,
3499                 Parameter_Associations => New_List (Object_Parm));
3500
3501             Rewrite (N, Call);
3502
3503             --  Avoid the generation of extra checks on the pointer to the
3504             --  protected object.
3505
3506             Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
3507          end;
3508
3509       ------------------
3510       -- Range_Length --
3511       ------------------
3512
3513       when Attribute_Range_Length => Range_Length : begin
3514          --  The only special processing required is for the case where
3515          --  Range_Length is applied to an enumeration type with holes.
3516          --  In this case we transform
3517
3518          --     X'Range_Length
3519
3520          --  to
3521
3522          --     X'Pos (X'Last) - X'Pos (X'First) + 1
3523
3524          --  So that the result reflects the proper Pos values instead
3525          --  of the underlying representations.
3526
3527          if Is_Enumeration_Type (Ptyp)
3528            and then Has_Non_Standard_Rep (Ptyp)
3529          then
3530             Rewrite (N,
3531               Make_Op_Add (Loc,
3532                 Left_Opnd =>
3533                   Make_Op_Subtract (Loc,
3534                     Left_Opnd =>
3535                       Make_Attribute_Reference (Loc,
3536                         Attribute_Name => Name_Pos,
3537                         Prefix => New_Occurrence_Of (Ptyp, Loc),
3538                         Expressions => New_List (
3539                           Make_Attribute_Reference (Loc,
3540                             Attribute_Name => Name_Last,
3541                             Prefix => New_Occurrence_Of (Ptyp, Loc)))),
3542
3543                     Right_Opnd =>
3544                       Make_Attribute_Reference (Loc,
3545                         Attribute_Name => Name_Pos,
3546                         Prefix => New_Occurrence_Of (Ptyp, Loc),
3547                         Expressions => New_List (
3548                           Make_Attribute_Reference (Loc,
3549                             Attribute_Name => Name_First,
3550                             Prefix => New_Occurrence_Of (Ptyp, Loc))))),
3551
3552                 Right_Opnd =>
3553                   Make_Integer_Literal (Loc, 1)));
3554
3555             Analyze_And_Resolve (N, Typ);
3556
3557          --  For all other cases, the attribute is handled by the back end, but
3558          --  we need to deal with the case of the range check on a universal
3559          --  integer.
3560
3561          else
3562             Apply_Universal_Integer_Attribute_Checks (N);
3563          end if;
3564       end Range_Length;
3565
3566       ----------
3567       -- Read --
3568       ----------
3569
3570       when Attribute_Read => Read : declare
3571          P_Type : constant Entity_Id := Entity (Pref);
3572          B_Type : constant Entity_Id := Base_Type (P_Type);
3573          U_Type : constant Entity_Id := Underlying_Type (P_Type);
3574          Pname  : Entity_Id;
3575          Decl   : Node_Id;
3576          Prag   : Node_Id;
3577          Arg2   : Node_Id;
3578          Rfunc  : Node_Id;
3579          Lhs    : Node_Id;
3580          Rhs    : Node_Id;
3581
3582       begin
3583          --  If no underlying type, we have an error that will be diagnosed
3584          --  elsewhere, so here we just completely ignore the expansion.
3585
3586          if No (U_Type) then
3587             return;
3588          end if;
3589
3590          --  The simple case, if there is a TSS for Read, just call it
3591
3592          Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
3593
3594          if Present (Pname) then
3595             null;
3596
3597          else
3598             --  If there is a Stream_Convert pragma, use it, we rewrite
3599
3600             --     sourcetyp'Read (stream, Item)
3601
3602             --  as
3603
3604             --     Item := sourcetyp (strmread (strmtyp'Input (Stream)));
3605
3606             --  where strmread is the given Read function that converts an
3607             --  argument of type strmtyp to type sourcetyp or a type from which
3608             --  it is derived. The conversion to sourcetyp is required in the
3609             --  latter case.
3610
3611             --  A special case arises if Item is a type conversion in which
3612             --  case, we have to expand to:
3613
3614             --     Itemx := typex (strmread (strmtyp'Input (Stream)));
3615
3616             --  where Itemx is the expression of the type conversion (i.e.
3617             --  the actual object), and typex is the type of Itemx.
3618
3619             Prag := Get_Stream_Convert_Pragma (P_Type);
3620
3621             if Present (Prag) then
3622                Arg2  := Next (First (Pragma_Argument_Associations (Prag)));
3623                Rfunc := Entity (Expression (Arg2));
3624                Lhs := Relocate_Node (Next (First (Exprs)));
3625                Rhs :=
3626                  OK_Convert_To (B_Type,
3627                    Make_Function_Call (Loc,
3628                      Name => New_Occurrence_Of (Rfunc, Loc),
3629                      Parameter_Associations => New_List (
3630                        Make_Attribute_Reference (Loc,
3631                          Prefix =>
3632                            New_Occurrence_Of
3633                              (Etype (First_Formal (Rfunc)), Loc),
3634                          Attribute_Name => Name_Input,
3635                          Expressions => New_List (
3636                            Relocate_Node (First (Exprs)))))));
3637
3638                if Nkind (Lhs) = N_Type_Conversion then
3639                   Lhs := Expression (Lhs);
3640                   Rhs := Convert_To (Etype (Lhs), Rhs);
3641                end if;
3642
3643                Rewrite (N,
3644                  Make_Assignment_Statement (Loc,
3645                    Name       => Lhs,
3646                    Expression => Rhs));
3647                Set_Assignment_OK (Lhs);
3648                Analyze (N);
3649                return;
3650
3651             --  For elementary types, we call the I_xxx routine using the first
3652             --  parameter and then assign the result into the second parameter.
3653             --  We set Assignment_OK to deal with the conversion case.
3654
3655             elsif Is_Elementary_Type (U_Type) then
3656                declare
3657                   Lhs : Node_Id;
3658                   Rhs : Node_Id;
3659
3660                begin
3661                   Lhs := Relocate_Node (Next (First (Exprs)));
3662                   Rhs := Build_Elementary_Input_Call (N);
3663
3664                   if Nkind (Lhs) = N_Type_Conversion then
3665                      Lhs := Expression (Lhs);
3666                      Rhs := Convert_To (Etype (Lhs), Rhs);
3667                   end if;
3668
3669                   Set_Assignment_OK (Lhs);
3670
3671                   Rewrite (N,
3672                     Make_Assignment_Statement (Loc,
3673                       Name => Lhs,
3674                       Expression => Rhs));
3675
3676                   Analyze (N);
3677                   return;
3678                end;
3679
3680             --  Array type case
3681
3682             elsif Is_Array_Type (U_Type) then
3683                Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
3684                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3685
3686             --  Tagged type case, use the primitive Read function. Note that
3687             --  this will dispatch in the class-wide case which is what we want
3688
3689             elsif Is_Tagged_Type (U_Type) then
3690                Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
3691
3692             --  All other record type cases, including protected records. The
3693             --  latter only arise for expander generated code for handling
3694             --  shared passive partition access.
3695
3696             else
3697                pragma Assert
3698                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3699
3700                --  Ada 2005 (AI-216): Program_Error is raised when executing
3701                --  the default implementation of the Read attribute of an
3702                --  Unchecked_Union type.
3703
3704                if Is_Unchecked_Union (Base_Type (U_Type)) then
3705                   Insert_Action (N,
3706                     Make_Raise_Program_Error (Loc,
3707                       Reason => PE_Unchecked_Union_Restriction));
3708                end if;
3709
3710                if Has_Discriminants (U_Type)
3711                  and then Present
3712                    (Discriminant_Default_Value (First_Discriminant (U_Type)))
3713                then
3714                   Build_Mutable_Record_Read_Procedure
3715                     (Loc, Base_Type (U_Type), Decl, Pname);
3716                else
3717                   Build_Record_Read_Procedure
3718                     (Loc, Base_Type (U_Type), Decl, Pname);
3719                end if;
3720
3721                --  Suppress checks, uninitialized or otherwise invalid
3722                --  data does not cause constraint errors to be raised for
3723                --  a complete record read.
3724
3725                Insert_Action (N, Decl, All_Checks);
3726             end if;
3727          end if;
3728
3729          Rewrite_Stream_Proc_Call (Pname);
3730       end Read;
3731
3732       ---------------
3733       -- Remainder --
3734       ---------------
3735
3736       --  Transforms 'Remainder into a call to the floating-point attribute
3737       --  function Remainder in Fat_xxx (where xxx is the root type)
3738
3739       when Attribute_Remainder =>
3740          Expand_Fpt_Attribute_RR (N);
3741
3742       ------------
3743       -- Result --
3744       ------------
3745
3746       --  Transform 'Result into reference to _Result formal. At the point
3747       --  where a legal 'Result attribute is expanded, we know that we are in
3748       --  the context of a _Postcondition function with a _Result parameter.
3749
3750       when Attribute_Result =>
3751          Rewrite (N,
3752            Make_Identifier (Loc,
3753             Chars => Name_uResult));
3754          Analyze_And_Resolve (N, Typ);
3755
3756       -----------
3757       -- Round --
3758       -----------
3759
3760       --  The handling of the Round attribute is quite delicate. The processing
3761       --  in Sem_Attr introduced a conversion to universal real, reflecting the
3762       --  semantics of Round, but we do not want anything to do with universal
3763       --  real at runtime, since this corresponds to using floating-point
3764       --  arithmetic.
3765
3766       --  What we have now is that the Etype of the Round attribute correctly
3767       --  indicates the final result type. The operand of the Round is the
3768       --  conversion to universal real, described above, and the operand of
3769       --  this conversion is the actual operand of Round, which may be the
3770       --  special case of a fixed point multiplication or division (Etype =
3771       --  universal fixed)
3772
3773       --  The exapander will expand first the operand of the conversion, then
3774       --  the conversion, and finally the round attribute itself, since we
3775       --  always work inside out. But we cannot simply process naively in this
3776       --  order. In the semantic world where universal fixed and real really
3777       --  exist and have infinite precision, there is no problem, but in the
3778       --  implementation world, where universal real is a floating-point type,
3779       --  we would get the wrong result.
3780
3781       --  So the approach is as follows. First, when expanding a multiply or
3782       --  divide whose type is universal fixed, we do nothing at all, instead
3783       --  deferring the operation till later.
3784
3785       --  The actual processing is done in Expand_N_Type_Conversion which
3786       --  handles the special case of Round by looking at its parent to see if
3787       --  it is a Round attribute, and if it is, handling the conversion (or
3788       --  its fixed multiply/divide child) in an appropriate manner.
3789
3790       --  This means that by the time we get to expanding the Round attribute
3791       --  itself, the Round is nothing more than a type conversion (and will
3792       --  often be a null type conversion), so we just replace it with the
3793       --  appropriate conversion operation.
3794
3795       when Attribute_Round =>
3796          Rewrite (N,
3797            Convert_To (Etype (N), Relocate_Node (First (Exprs))));
3798          Analyze_And_Resolve (N);
3799
3800       --------------
3801       -- Rounding --
3802       --------------
3803
3804       --  Transforms 'Rounding into a call to the floating-point attribute
3805       --  function Rounding in Fat_xxx (where xxx is the root type)
3806
3807       when Attribute_Rounding =>
3808          Expand_Fpt_Attribute_R (N);
3809
3810       -------------
3811       -- Scaling --
3812       -------------
3813
3814       --  Transforms 'Scaling into a call to the floating-point attribute
3815       --  function Scaling in Fat_xxx (where xxx is the root type)
3816
3817       when Attribute_Scaling =>
3818          Expand_Fpt_Attribute_RI (N);
3819
3820       ----------
3821       -- Size --
3822       ----------
3823
3824       when Attribute_Size        |
3825            Attribute_Object_Size |
3826            Attribute_Value_Size  |
3827            Attribute_VADS_Size   => Size :
3828
3829       declare
3830          Siz      : Uint;
3831          New_Node : Node_Id;
3832
3833       begin
3834          --  Processing for VADS_Size case. Note that this processing removes
3835          --  all traces of VADS_Size from the tree, and completes all required
3836          --  processing for VADS_Size by translating the attribute reference
3837          --  to an appropriate Size or Object_Size reference.
3838
3839          if Id = Attribute_VADS_Size
3840            or else (Use_VADS_Size and then Id = Attribute_Size)
3841          then
3842             --  If the size is specified, then we simply use the specified
3843             --  size. This applies to both types and objects. The size of an
3844             --  object can be specified in the following ways:
3845
3846             --    An explicit size object is given for an object
3847             --    A component size is specified for an indexed component
3848             --    A component clause is specified for a selected component
3849             --    The object is a component of a packed composite object
3850
3851             --  If the size is specified, then VADS_Size of an object
3852
3853             if (Is_Entity_Name (Pref)
3854                  and then Present (Size_Clause (Entity (Pref))))
3855               or else
3856                 (Nkind (Pref) = N_Component_Clause
3857                   and then (Present (Component_Clause
3858                                      (Entity (Selector_Name (Pref))))
3859                              or else Is_Packed (Etype (Prefix (Pref)))))
3860               or else
3861                 (Nkind (Pref) = N_Indexed_Component
3862                   and then (Component_Size (Etype (Prefix (Pref))) /= 0
3863                              or else Is_Packed (Etype (Prefix (Pref)))))
3864             then
3865                Set_Attribute_Name (N, Name_Size);
3866
3867             --  Otherwise if we have an object rather than a type, then the
3868             --  VADS_Size attribute applies to the type of the object, rather
3869             --  than the object itself. This is one of the respects in which
3870             --  VADS_Size differs from Size.
3871
3872             else
3873                if (not Is_Entity_Name (Pref)
3874                     or else not Is_Type (Entity (Pref)))
3875                  and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
3876                then
3877                   Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
3878                end if;
3879
3880                --  For a scalar type for which no size was explicitly given,
3881                --  VADS_Size means Object_Size. This is the other respect in
3882                --  which VADS_Size differs from Size.
3883
3884                if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
3885                   Set_Attribute_Name (N, Name_Object_Size);
3886
3887                --  In all other cases, Size and VADS_Size are the sane
3888
3889                else
3890                   Set_Attribute_Name (N, Name_Size);
3891                end if;
3892             end if;
3893          end if;
3894
3895          --  For class-wide types, X'Class'Size is transformed into a direct
3896          --  reference to the Size of the class type, so that the back end does
3897          --  not have to deal with the X'Class'Size reference.
3898
3899          if Is_Entity_Name (Pref)
3900            and then Is_Class_Wide_Type (Entity (Pref))
3901          then
3902             Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
3903             return;
3904
3905          --  For X'Size applied to an object of a class-wide type, transform
3906          --  X'Size into a call to the primitive operation _Size applied to X.
3907
3908          elsif Is_Class_Wide_Type (Ptyp) then
3909
3910             --  No need to do anything else compiling under restriction
3911             --  No_Dispatching_Calls. During the semantic analysis we
3912             --  already notified such violation.
3913
3914             if Restriction_Active (No_Dispatching_Calls) then
3915                return;
3916             end if;
3917
3918             New_Node :=
3919               Make_Function_Call (Loc,
3920                 Name => New_Reference_To
3921                   (Find_Prim_Op (Ptyp, Name_uSize), Loc),
3922                 Parameter_Associations => New_List (Pref));
3923
3924             if Typ /= Standard_Long_Long_Integer then
3925
3926                --  The context is a specific integer type with which the
3927                --  original attribute was compatible. The function has a
3928                --  specific type as well, so to preserve the compatibility
3929                --  we must convert explicitly.
3930
3931                New_Node := Convert_To (Typ, New_Node);
3932             end if;
3933
3934             Rewrite (N, New_Node);
3935             Analyze_And_Resolve (N, Typ);
3936                return;
3937
3938          --  Case of known RM_Size of a type
3939
3940          elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
3941            and then Is_Entity_Name (Pref)
3942            and then Is_Type (Entity (Pref))
3943            and then Known_Static_RM_Size (Entity (Pref))
3944          then
3945             Siz := RM_Size (Entity (Pref));
3946
3947          --  Case of known Esize of a type
3948
3949          elsif Id = Attribute_Object_Size
3950            and then Is_Entity_Name (Pref)
3951            and then Is_Type (Entity (Pref))
3952            and then Known_Static_Esize (Entity (Pref))
3953          then
3954             Siz := Esize (Entity (Pref));
3955
3956          --  Case of known size of object
3957
3958          elsif Id = Attribute_Size
3959            and then Is_Entity_Name (Pref)
3960            and then Is_Object (Entity (Pref))
3961            and then Known_Esize (Entity (Pref))
3962            and then Known_Static_Esize (Entity (Pref))
3963          then
3964             Siz := Esize (Entity (Pref));
3965
3966          --  For an array component, we can do Size in the front end
3967          --  if the component_size of the array is set.
3968
3969          elsif Nkind (Pref) = N_Indexed_Component then
3970             Siz := Component_Size (Etype (Prefix (Pref)));
3971
3972          --  For a record component, we can do Size in the front end if there
3973          --  is a component clause, or if the record is packed and the
3974          --  component's size is known at compile time.
3975
3976          elsif Nkind (Pref) = N_Selected_Component then
3977             declare
3978                Rec  : constant Entity_Id := Etype (Prefix (Pref));
3979                Comp : constant Entity_Id := Entity (Selector_Name (Pref));
3980
3981             begin
3982                if Present (Component_Clause (Comp)) then
3983                   Siz := Esize (Comp);
3984
3985                elsif Is_Packed (Rec) then
3986                   Siz := RM_Size (Ptyp);
3987
3988                else
3989                   Apply_Universal_Integer_Attribute_Checks (N);
3990                   return;
3991                end if;
3992             end;
3993
3994          --  All other cases are handled by the back end
3995
3996          else
3997             Apply_Universal_Integer_Attribute_Checks (N);
3998
3999             --  If Size is applied to a formal parameter that is of a packed
4000             --  array subtype, then apply Size to the actual subtype.
4001
4002             if Is_Entity_Name (Pref)
4003               and then Is_Formal (Entity (Pref))
4004               and then Is_Array_Type (Ptyp)
4005               and then Is_Packed (Ptyp)
4006             then
4007                Rewrite (N,
4008                  Make_Attribute_Reference (Loc,
4009                    Prefix =>
4010                      New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
4011                    Attribute_Name => Name_Size));
4012                Analyze_And_Resolve (N, Typ);
4013             end if;
4014
4015             --  If Size applies to a dereference of an access to unconstrained
4016             --  packed array, the back end needs to see its unconstrained
4017             --  nominal type, but also a hint to the actual constrained type.
4018
4019             if Nkind (Pref) = N_Explicit_Dereference
4020               and then Is_Array_Type (Ptyp)
4021               and then not Is_Constrained (Ptyp)
4022               and then Is_Packed (Ptyp)
4023             then
4024                Set_Actual_Designated_Subtype (Pref,
4025                  Get_Actual_Subtype (Pref));
4026             end if;
4027
4028             return;
4029          end if;
4030
4031          --  Common processing for record and array component case
4032
4033          if Siz /= No_Uint and then Siz /= 0 then
4034             declare
4035                CS : constant Boolean := Comes_From_Source (N);
4036
4037             begin
4038                Rewrite (N, Make_Integer_Literal (Loc, Siz));
4039
4040                --  This integer literal is not a static expression. We do not
4041                --  call Analyze_And_Resolve here, because this would activate
4042                --  the circuit for deciding that a static value was out of
4043                --  range, and we don't want that.
4044
4045                --  So just manually set the type, mark the expression as non-
4046                --  static, and then ensure that the result is checked properly
4047                --  if the attribute comes from source (if it was internally
4048                --  generated, we never need a constraint check).
4049
4050                Set_Etype (N, Typ);
4051                Set_Is_Static_Expression (N, False);
4052
4053                if CS then
4054                   Apply_Constraint_Check (N, Typ);
4055                end if;
4056             end;
4057          end if;
4058       end Size;
4059
4060       ------------------
4061       -- Storage_Pool --
4062       ------------------
4063
4064       when Attribute_Storage_Pool =>
4065          Rewrite (N,
4066            Make_Type_Conversion (Loc,
4067              Subtype_Mark => New_Reference_To (Etype (N), Loc),
4068              Expression   => New_Reference_To (Entity (N), Loc)));
4069          Analyze_And_Resolve (N, Typ);
4070
4071       ------------------
4072       -- Storage_Size --
4073       ------------------
4074
4075       when Attribute_Storage_Size => Storage_Size : begin
4076
4077          --  Access type case, always go to the root type
4078
4079          --  The case of access types results in a value of zero for the case
4080          --  where no storage size attribute clause has been given. If a
4081          --  storage size has been given, then the attribute is converted
4082          --  to a reference to the variable used to hold this value.
4083
4084          if Is_Access_Type (Ptyp) then
4085             if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
4086                Rewrite (N,
4087                  Make_Attribute_Reference (Loc,
4088                    Prefix => New_Reference_To (Typ, Loc),
4089                    Attribute_Name => Name_Max,
4090                    Expressions => New_List (
4091                      Make_Integer_Literal (Loc, 0),
4092                      Convert_To (Typ,
4093                        New_Reference_To
4094                          (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
4095
4096             elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
4097                Rewrite (N,
4098                  OK_Convert_To (Typ,
4099                    Make_Function_Call (Loc,
4100                      Name =>
4101                        New_Reference_To
4102                          (Find_Prim_Op
4103                            (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
4104                             Attribute_Name (N)),
4105                           Loc),
4106
4107                      Parameter_Associations => New_List (
4108                        New_Reference_To
4109                          (Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
4110
4111             else
4112                Rewrite (N, Make_Integer_Literal (Loc, 0));
4113             end if;
4114
4115             Analyze_And_Resolve (N, Typ);
4116
4117          --  For tasks, we retrieve the size directly from the TCB. The
4118          --  size may depend on a discriminant of the type, and therefore
4119          --  can be a per-object expression, so type-level information is
4120          --  not sufficient in general. There are four cases to consider:
4121
4122          --  a) If the attribute appears within a task body, the designated
4123          --    TCB is obtained by a call to Self.
4124
4125          --  b) If the prefix of the attribute is the name of a task object,
4126          --  the designated TCB is the one stored in the corresponding record.
4127
4128          --  c) If the prefix is a task type, the size is obtained from the
4129          --  size variable created for each task type
4130
4131          --  d) If no storage_size was specified for the type , there is no
4132          --  size variable, and the value is a system-specific default.
4133
4134          else
4135             if In_Open_Scopes (Ptyp) then
4136
4137                --  Storage_Size (Self)
4138
4139                Rewrite (N,
4140                  Convert_To (Typ,
4141                    Make_Function_Call (Loc,
4142                      Name =>
4143                        New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4144                      Parameter_Associations =>
4145                        New_List (
4146                          Make_Function_Call (Loc,
4147                            Name =>
4148                              New_Reference_To (RTE (RE_Self), Loc))))));
4149
4150             elsif not Is_Entity_Name (Pref)
4151               or else not Is_Type (Entity (Pref))
4152             then
4153                --  Storage_Size (Rec (Obj).Size)
4154
4155                Rewrite (N,
4156                  Convert_To (Typ,
4157                    Make_Function_Call (Loc,
4158                      Name =>
4159                        New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4160                        Parameter_Associations =>
4161                           New_List (
4162                             Make_Selected_Component (Loc,
4163                               Prefix =>
4164                                 Unchecked_Convert_To (
4165                                   Corresponding_Record_Type (Ptyp),
4166                                     New_Copy_Tree (Pref)),
4167                               Selector_Name =>
4168                                  Make_Identifier (Loc, Name_uTask_Id))))));
4169
4170             elsif Present (Storage_Size_Variable (Ptyp)) then
4171
4172                --  Static storage size pragma given for type: retrieve value
4173                --  from its allocated storage variable.
4174
4175                Rewrite (N,
4176                  Convert_To (Typ,
4177                    Make_Function_Call (Loc,
4178                      Name => New_Occurrence_Of (
4179                        RTE (RE_Adjust_Storage_Size), Loc),
4180                      Parameter_Associations =>
4181                        New_List (
4182                          New_Reference_To (
4183                            Storage_Size_Variable (Ptyp), Loc)))));
4184             else
4185                --  Get system default
4186
4187                Rewrite (N,
4188                  Convert_To (Typ,
4189                    Make_Function_Call (Loc,
4190                      Name =>
4191                        New_Occurrence_Of (
4192                         RTE (RE_Default_Stack_Size), Loc))));
4193             end if;
4194
4195             Analyze_And_Resolve (N, Typ);
4196          end if;
4197       end Storage_Size;
4198
4199       -----------------
4200       -- Stream_Size --
4201       -----------------
4202
4203       when Attribute_Stream_Size => Stream_Size : declare
4204          Size : Int;
4205
4206       begin
4207          --  If we have a Stream_Size clause for this type use it, otherwise
4208          --  the Stream_Size if the size of the type.
4209
4210          if Has_Stream_Size_Clause (Ptyp) then
4211             Size :=
4212               UI_To_Int
4213                 (Static_Integer (Expression (Stream_Size_Clause (Ptyp))));
4214          else
4215             Size := UI_To_Int (Esize (Ptyp));
4216          end if;
4217
4218          Rewrite (N, Make_Integer_Literal (Loc, Intval => Size));
4219          Analyze_And_Resolve (N, Typ);
4220       end Stream_Size;
4221
4222       ----------
4223       -- Succ --
4224       ----------
4225
4226       --  1. Deal with enumeration types with holes
4227       --  2. For floating-point, generate call to attribute function
4228       --  3. For other cases, deal with constraint checking
4229
4230       when Attribute_Succ => Succ :
4231       declare
4232          Etyp : constant Entity_Id := Base_Type (Ptyp);
4233
4234       begin
4235
4236          --  For enumeration types with non-standard representations, we
4237          --  expand typ'Succ (x) into
4238
4239          --    Pos_To_Rep (Rep_To_Pos (x) + 1)
4240
4241          --    If the representation is contiguous, we compute instead
4242          --    Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
4243
4244          if Is_Enumeration_Type (Ptyp)
4245            and then Present (Enum_Pos_To_Rep (Etyp))
4246          then
4247             if Has_Contiguous_Rep (Etyp) then
4248                Rewrite (N,
4249                   Unchecked_Convert_To (Ptyp,
4250                      Make_Op_Add (Loc,
4251                         Left_Opnd  =>
4252                          Make_Integer_Literal (Loc,
4253                            Enumeration_Rep (First_Literal (Ptyp))),
4254                         Right_Opnd =>
4255                           Make_Function_Call (Loc,
4256                             Name =>
4257                               New_Reference_To
4258                                (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4259
4260                             Parameter_Associations =>
4261                               New_List (
4262                                 Unchecked_Convert_To (Ptyp,
4263                                   Make_Op_Add (Loc,
4264                                   Left_Opnd =>
4265                                     Unchecked_Convert_To (Standard_Integer,
4266                                       Relocate_Node (First (Exprs))),
4267                                   Right_Opnd =>
4268                                     Make_Integer_Literal (Loc, 1))),
4269                                 Rep_To_Pos_Flag (Ptyp, Loc))))));
4270             else
4271                --  Add Boolean parameter True, to request program errror if
4272                --  we have a bad representation on our hands. Add False if
4273                --  checks are suppressed.
4274
4275                Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4276                Rewrite (N,
4277                  Make_Indexed_Component (Loc,
4278                    Prefix =>
4279                      New_Reference_To
4280                        (Enum_Pos_To_Rep (Etyp), Loc),
4281                    Expressions => New_List (
4282                      Make_Op_Add (Loc,
4283                        Left_Opnd =>
4284                          Make_Function_Call (Loc,
4285                            Name =>
4286                              New_Reference_To
4287                                (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4288                            Parameter_Associations => Exprs),
4289                        Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4290             end if;
4291
4292             Analyze_And_Resolve (N, Typ);
4293
4294          --  For floating-point, we transform 'Succ into a call to the Succ
4295          --  floating-point attribute function in Fat_xxx (xxx is root type)
4296
4297          elsif Is_Floating_Point_Type (Ptyp) then
4298             Expand_Fpt_Attribute_R (N);
4299             Analyze_And_Resolve (N, Typ);
4300
4301          --  For modular types, nothing to do (no overflow, since wraps)
4302
4303          elsif Is_Modular_Integer_Type (Ptyp) then
4304             null;
4305
4306          --  For other types, if range checking is enabled, we must generate
4307          --  a check if overflow checking is enabled.
4308
4309          elsif not Overflow_Checks_Suppressed (Ptyp) then
4310             Expand_Pred_Succ (N);
4311          end if;
4312       end Succ;
4313
4314       ---------
4315       -- Tag --
4316       ---------
4317
4318       --  Transforms X'Tag into a direct reference to the tag of X
4319
4320       when Attribute_Tag => Tag :
4321       declare
4322          Ttyp           : Entity_Id;
4323          Prefix_Is_Type : Boolean;
4324
4325       begin
4326          if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
4327             Ttyp := Entity (Pref);
4328             Prefix_Is_Type := True;
4329          else
4330             Ttyp := Ptyp;
4331             Prefix_Is_Type := False;
4332          end if;
4333
4334          if Is_Class_Wide_Type (Ttyp) then
4335             Ttyp := Root_Type (Ttyp);
4336          end if;
4337
4338          Ttyp := Underlying_Type (Ttyp);
4339
4340          if Prefix_Is_Type then
4341
4342             --  For VMs we leave the type attribute unexpanded because
4343             --  there's not a dispatching table to reference.
4344
4345             if VM_Target = No_VM then
4346                Rewrite (N,
4347                  Unchecked_Convert_To (RTE (RE_Tag),
4348                    New_Reference_To
4349                      (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
4350                Analyze_And_Resolve (N, RTE (RE_Tag));
4351             end if;
4352
4353          --  (Ada 2005 (AI-251): The use of 'Tag in the sources always
4354          --  references the primary tag of the actual object. If 'Tag is
4355          --  applied to class-wide interface objects we generate code that
4356          --  displaces "this" to reference the base of the object.
4357
4358          elsif Comes_From_Source (N)
4359             and then Is_Class_Wide_Type (Etype (Prefix (N)))
4360             and then Is_Interface (Etype (Prefix (N)))
4361          then
4362             --  Generate:
4363             --    (To_Tag_Ptr (Prefix'Address)).all
4364
4365             --  Note that Prefix'Address is recursively expanded into a call
4366             --  to Base_Address (Obj.Tag)
4367
4368             --  Not needed for VM targets, since all handled by the VM
4369
4370             if VM_Target = No_VM then
4371                Rewrite (N,
4372                  Make_Explicit_Dereference (Loc,
4373                    Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4374                      Make_Attribute_Reference (Loc,
4375                        Prefix => Relocate_Node (Pref),
4376                        Attribute_Name => Name_Address))));
4377                Analyze_And_Resolve (N, RTE (RE_Tag));
4378             end if;
4379
4380          else
4381             Rewrite (N,
4382               Make_Selected_Component (Loc,
4383                 Prefix => Relocate_Node (Pref),
4384                 Selector_Name =>
4385                   New_Reference_To (First_Tag_Component (Ttyp), Loc)));
4386             Analyze_And_Resolve (N, RTE (RE_Tag));
4387          end if;
4388       end Tag;
4389
4390       ----------------
4391       -- Terminated --
4392       ----------------
4393
4394       --  Transforms 'Terminated attribute into a call to Terminated function
4395
4396       when Attribute_Terminated => Terminated :
4397       begin
4398          --  The prefix of Terminated is of a task interface class-wide type.
4399          --  Generate:
4400
4401          --    terminated (Task_Id (Pref._disp_get_task_id));
4402
4403          if Ada_Version >= Ada_05
4404            and then Ekind (Ptyp) = E_Class_Wide_Type
4405            and then Is_Interface (Ptyp)
4406            and then Is_Task_Interface (Ptyp)
4407          then
4408             Rewrite (N,
4409               Make_Function_Call (Loc,
4410                 Name =>
4411                   New_Reference_To (RTE (RE_Terminated), Loc),
4412                 Parameter_Associations => New_List (
4413                   Make_Unchecked_Type_Conversion (Loc,
4414                     Subtype_Mark =>
4415                       New_Reference_To (RTE (RO_ST_Task_Id), Loc),
4416                     Expression =>
4417                       Make_Selected_Component (Loc,
4418                         Prefix =>
4419                           New_Copy_Tree (Pref),
4420                         Selector_Name =>
4421                           Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
4422
4423          elsif Restricted_Profile then
4424             Rewrite (N,
4425               Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
4426
4427          else
4428             Rewrite (N,
4429               Build_Call_With_Task (Pref, RTE (RE_Terminated)));
4430          end if;
4431
4432          Analyze_And_Resolve (N, Standard_Boolean);
4433       end Terminated;
4434
4435       ----------------
4436       -- To_Address --
4437       ----------------
4438
4439       --  Transforms System'To_Address (X) into unchecked conversion
4440       --  from (integral) type of X to type address.
4441
4442       when Attribute_To_Address =>
4443          Rewrite (N,
4444            Unchecked_Convert_To (RTE (RE_Address),
4445              Relocate_Node (First (Exprs))));
4446          Analyze_And_Resolve (N, RTE (RE_Address));
4447
4448       ------------
4449       -- To_Any --
4450       ------------
4451
4452       when Attribute_To_Any => To_Any : declare
4453          P_Type : constant Entity_Id := Etype (Pref);
4454          Decls  : constant List_Id   := New_List;
4455       begin
4456          Rewrite (N,
4457            Build_To_Any_Call
4458              (Convert_To (P_Type,
4459               Relocate_Node (First (Exprs))), Decls));
4460          Insert_Actions (N, Decls);
4461          Analyze_And_Resolve (N, RTE (RE_Any));
4462       end To_Any;
4463
4464       ----------------
4465       -- Truncation --
4466       ----------------
4467
4468       --  Transforms 'Truncation into a call to the floating-point attribute
4469       --  function Truncation in Fat_xxx (where xxx is the root type).
4470       --  Expansion is avoided for cases the back end can handle directly.
4471
4472       when Attribute_Truncation =>
4473          if not Is_Inline_Floating_Point_Attribute (N) then
4474             Expand_Fpt_Attribute_R (N);
4475          end if;
4476
4477       --------------
4478       -- TypeCode --
4479       --------------
4480
4481       when Attribute_TypeCode => TypeCode : declare
4482          P_Type : constant Entity_Id := Etype (Pref);
4483          Decls  : constant List_Id   := New_List;
4484       begin
4485          Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
4486          Insert_Actions (N, Decls);
4487          Analyze_And_Resolve (N, RTE (RE_TypeCode));
4488       end TypeCode;
4489
4490       -----------------------
4491       -- Unbiased_Rounding --
4492       -----------------------
4493
4494       --  Transforms 'Unbiased_Rounding into a call to the floating-point
4495       --  attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
4496       --  root type). Expansion is avoided for cases the back end can handle
4497       --  directly.
4498
4499       when Attribute_Unbiased_Rounding =>
4500          if not Is_Inline_Floating_Point_Attribute (N) then
4501             Expand_Fpt_Attribute_R (N);
4502          end if;
4503
4504       -----------------
4505       -- UET_Address --
4506       -----------------
4507
4508       when Attribute_UET_Address => UET_Address : declare
4509          Ent : constant Entity_Id :=
4510                  Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4511
4512       begin
4513          Insert_Action (N,
4514            Make_Object_Declaration (Loc,
4515              Defining_Identifier => Ent,
4516              Aliased_Present     => True,
4517              Object_Definition   =>
4518                New_Occurrence_Of (RTE (RE_Address), Loc)));
4519
4520          --  Construct name __gnat_xxx__SDP, where xxx is the unit name
4521          --  in normal external form.
4522
4523          Get_External_Unit_Name_String (Get_Unit_Name (Pref));
4524          Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
4525          Name_Len := Name_Len + 7;
4526          Name_Buffer (1 .. 7) := "__gnat_";
4527          Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
4528          Name_Len := Name_Len + 5;
4529
4530          Set_Is_Imported (Ent);
4531          Set_Interface_Name (Ent,
4532            Make_String_Literal (Loc,
4533              Strval => String_From_Name_Buffer));
4534
4535          --  Set entity as internal to ensure proper Sprint output of its
4536          --  implicit importation.
4537
4538          Set_Is_Internal (Ent);
4539
4540          Rewrite (N,
4541            Make_Attribute_Reference (Loc,
4542              Prefix => New_Occurrence_Of (Ent, Loc),
4543              Attribute_Name => Name_Address));
4544
4545          Analyze_And_Resolve (N, Typ);
4546       end UET_Address;
4547
4548       ---------------
4549       -- VADS_Size --
4550       ---------------
4551
4552       --  The processing for VADS_Size is shared with Size
4553
4554       ---------
4555       -- Val --
4556       ---------
4557
4558       --  For enumeration types with a standard representation, and for all
4559       --  other types, Val is handled by the back end. For enumeration types
4560       --  with a non-standard representation we use the _Pos_To_Rep array that
4561       --  was created when the type was frozen.
4562
4563       when Attribute_Val => Val :
4564       declare
4565          Etyp : constant Entity_Id := Base_Type (Entity (Pref));
4566
4567       begin
4568          if Is_Enumeration_Type (Etyp)
4569            and then Present (Enum_Pos_To_Rep (Etyp))
4570          then
4571             if Has_Contiguous_Rep (Etyp) then
4572                declare
4573                   Rep_Node : constant Node_Id :=
4574                     Unchecked_Convert_To (Etyp,
4575                        Make_Op_Add (Loc,
4576                          Left_Opnd =>
4577                             Make_Integer_Literal (Loc,
4578                               Enumeration_Rep (First_Literal (Etyp))),
4579                          Right_Opnd =>
4580                           (Convert_To (Standard_Integer,
4581                              Relocate_Node (First (Exprs))))));
4582
4583                begin
4584                   Rewrite (N,
4585                      Unchecked_Convert_To (Etyp,
4586                          Make_Op_Add (Loc,
4587                            Left_Opnd =>
4588                              Make_Integer_Literal (Loc,
4589                                Enumeration_Rep (First_Literal (Etyp))),
4590                            Right_Opnd =>
4591                              Make_Function_Call (Loc,
4592                                Name =>
4593                                  New_Reference_To
4594                                    (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4595                                Parameter_Associations => New_List (
4596                                  Rep_Node,
4597                                  Rep_To_Pos_Flag (Etyp, Loc))))));
4598                end;
4599
4600             else
4601                Rewrite (N,
4602                  Make_Indexed_Component (Loc,
4603                    Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
4604                    Expressions => New_List (
4605                      Convert_To (Standard_Integer,
4606                        Relocate_Node (First (Exprs))))));
4607             end if;
4608
4609             Analyze_And_Resolve (N, Typ);
4610          end if;
4611       end Val;
4612
4613       -----------
4614       -- Valid --
4615       -----------
4616
4617       --  The code for valid is dependent on the particular types involved.
4618       --  See separate sections below for the generated code in each case.
4619
4620       when Attribute_Valid => Valid :
4621       declare
4622          Btyp : Entity_Id := Base_Type (Ptyp);
4623          Tst  : Node_Id;
4624
4625          Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
4626          --  Save the validity checking mode. We always turn off validity
4627          --  checking during process of 'Valid since this is one place
4628          --  where we do not want the implicit validity checks to intefere
4629          --  with the explicit validity check that the programmer is doing.
4630
4631          function Make_Range_Test return Node_Id;
4632          --  Build the code for a range test of the form
4633          --    Btyp!(Pref) >= Btyp!(Ptyp'First)
4634          --      and then
4635          --    Btyp!(Pref) <= Btyp!(Ptyp'Last)
4636
4637          ---------------------
4638          -- Make_Range_Test --
4639          ---------------------
4640
4641          function Make_Range_Test return Node_Id is
4642          begin
4643             return
4644               Make_And_Then (Loc,
4645                 Left_Opnd =>
4646                   Make_Op_Ge (Loc,
4647                     Left_Opnd =>
4648                       Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
4649
4650                     Right_Opnd =>
4651                       Unchecked_Convert_To (Btyp,
4652                         Make_Attribute_Reference (Loc,
4653                           Prefix => New_Occurrence_Of (Ptyp, Loc),
4654                           Attribute_Name => Name_First))),
4655
4656                 Right_Opnd =>
4657                   Make_Op_Le (Loc,
4658                     Left_Opnd =>
4659                       Unchecked_Convert_To (Btyp,
4660                         Duplicate_Subexpr_No_Checks (Pref)),
4661
4662                     Right_Opnd =>
4663                       Unchecked_Convert_To (Btyp,
4664                         Make_Attribute_Reference (Loc,
4665                           Prefix => New_Occurrence_Of (Ptyp, Loc),
4666                           Attribute_Name => Name_Last))));
4667          end Make_Range_Test;
4668
4669       --  Start of processing for Attribute_Valid
4670
4671       begin
4672          --  Turn off validity checks. We do not want any implicit validity
4673          --  checks to intefere with the explicit check from the attribute
4674
4675          Validity_Checks_On := False;
4676
4677          --  Floating-point case. This case is handled by the Valid attribute
4678          --  code in the floating-point attribute run-time library.
4679
4680          if Is_Floating_Point_Type (Ptyp) then
4681             declare
4682                Pkg : RE_Id;
4683                Ftp : Entity_Id;
4684
4685             begin
4686                --  For vax fpt types, call appropriate routine in special vax
4687                --  floating point unit. We do not have to worry about loads in
4688                --  this case, since these types have no signalling NaN's.
4689
4690                if Vax_Float (Btyp) then
4691                   Expand_Vax_Valid (N);
4692
4693                --  The AAMP back end handles Valid for floating-point types
4694
4695                elsif Is_AAMP_Float (Btyp) then
4696                   Analyze_And_Resolve (Pref, Ptyp);
4697                   Set_Etype (N, Standard_Boolean);
4698                   Set_Analyzed (N);
4699
4700                --  Non VAX float case
4701
4702                else
4703                   Find_Fat_Info (Ptyp, Ftp, Pkg);
4704
4705                   --  If the floating-point object might be unaligned, we need
4706                   --  to call the special routine Unaligned_Valid, which makes
4707                   --  the needed copy, being careful not to load the value into
4708                   --  any floating-point register. The argument in this case is
4709                   --  obj'Address (see Unaligned_Valid routine in Fat_Gen).
4710
4711                   if Is_Possibly_Unaligned_Object (Pref) then
4712                      Expand_Fpt_Attribute
4713                        (N, Pkg, Name_Unaligned_Valid,
4714                         New_List (
4715                           Make_Attribute_Reference (Loc,
4716                             Prefix => Relocate_Node (Pref),
4717                             Attribute_Name => Name_Address)));
4718
4719                   --  In the normal case where we are sure the object is
4720                   --  aligned, we generate a call to Valid, and the argument in
4721                   --  this case is obj'Unrestricted_Access (after converting
4722                   --  obj to the right floating-point type).
4723
4724                   else
4725                      Expand_Fpt_Attribute
4726                        (N, Pkg, Name_Valid,
4727                         New_List (
4728                           Make_Attribute_Reference (Loc,
4729                             Prefix => Unchecked_Convert_To (Ftp, Pref),
4730                             Attribute_Name => Name_Unrestricted_Access)));
4731                   end if;
4732                end if;
4733
4734                --  One more task, we still need a range check. Required
4735                --  only if we have a constraint, since the Valid routine
4736                --  catches infinities properly (infinities are never valid).
4737
4738                --  The way we do the range check is simply to create the
4739                --  expression: Valid (N) and then Base_Type(Pref) in Typ.
4740
4741                if not Subtypes_Statically_Match (Ptyp, Btyp) then
4742                   Rewrite (N,
4743                     Make_And_Then (Loc,
4744                       Left_Opnd  => Relocate_Node (N),
4745                       Right_Opnd =>
4746                         Make_In (Loc,
4747                           Left_Opnd => Convert_To (Btyp, Pref),
4748                           Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
4749                end if;
4750             end;
4751
4752          --  Enumeration type with holes
4753
4754          --  For enumeration types with holes, the Pos value constructed by
4755          --  the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
4756          --  second argument of False returns minus one for an invalid value,
4757          --  and the non-negative pos value for a valid value, so the
4758          --  expansion of X'Valid is simply:
4759
4760          --     type(X)'Pos (X) >= 0
4761
4762          --  We can't quite generate it that way because of the requirement
4763          --  for the non-standard second argument of False in the resulting
4764          --  rep_to_pos call, so we have to explicitly create:
4765
4766          --     _rep_to_pos (X, False) >= 0
4767
4768          --  If we have an enumeration subtype, we also check that the
4769          --  value is in range:
4770
4771          --    _rep_to_pos (X, False) >= 0
4772          --      and then
4773          --       (X >= type(X)'First and then type(X)'Last <= X)
4774
4775          elsif Is_Enumeration_Type (Ptyp)
4776            and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
4777          then
4778             Tst :=
4779               Make_Op_Ge (Loc,
4780                 Left_Opnd =>
4781                   Make_Function_Call (Loc,
4782                     Name =>
4783                       New_Reference_To
4784                         (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc),
4785                     Parameter_Associations => New_List (
4786                       Pref,
4787                       New_Occurrence_Of (Standard_False, Loc))),
4788                 Right_Opnd => Make_Integer_Literal (Loc, 0));
4789
4790             if Ptyp /= Btyp
4791               and then
4792                 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
4793                   or else
4794                  Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
4795             then
4796                --  The call to Make_Range_Test will create declarations
4797                --  that need a proper insertion point, but Pref is now
4798                --  attached to a node with no ancestor. Attach to tree
4799                --  even if it is to be rewritten below.
4800
4801                Set_Parent (Tst, Parent (N));
4802
4803                Tst :=
4804                  Make_And_Then (Loc,
4805                    Left_Opnd  => Make_Range_Test,
4806                    Right_Opnd => Tst);
4807             end if;
4808
4809             Rewrite (N, Tst);
4810
4811          --  Fortran convention booleans
4812
4813          --  For the very special case of Fortran convention booleans, the
4814          --  value is always valid, since it is an integer with the semantics
4815          --  that non-zero is true, and any value is permissible.
4816
4817          elsif Is_Boolean_Type (Ptyp)
4818            and then Convention (Ptyp) = Convention_Fortran
4819          then
4820             Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4821
4822          --  For biased representations, we will be doing an unchecked
4823          --  conversion without unbiasing the result. That means that the range
4824          --  test has to take this into account, and the proper form of the
4825          --  test is:
4826
4827          --    Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
4828
4829          elsif Has_Biased_Representation (Ptyp) then
4830             Btyp := RTE (RE_Unsigned_32);
4831             Rewrite (N,
4832               Make_Op_Lt (Loc,
4833                 Left_Opnd =>
4834                   Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
4835                 Right_Opnd =>
4836                   Unchecked_Convert_To (Btyp,
4837                     Make_Attribute_Reference (Loc,
4838                       Prefix => New_Occurrence_Of (Ptyp, Loc),
4839                       Attribute_Name => Name_Range_Length))));
4840
4841          --  For all other scalar types, what we want logically is a
4842          --  range test:
4843
4844          --     X in type(X)'First .. type(X)'Last
4845
4846          --  But that's precisely what won't work because of possible
4847          --  unwanted optimization (and indeed the basic motivation for
4848          --  the Valid attribute is exactly that this test does not work!)
4849          --  What will work is:
4850
4851          --     Btyp!(X) >= Btyp!(type(X)'First)
4852          --       and then
4853          --     Btyp!(X) <= Btyp!(type(X)'Last)
4854
4855          --  where Btyp is an integer type large enough to cover the full
4856          --  range of possible stored values (i.e. it is chosen on the basis
4857          --  of the size of the type, not the range of the values). We write
4858          --  this as two tests, rather than a range check, so that static
4859          --  evaluation will easily remove either or both of the checks if
4860          --  they can be -statically determined to be true (this happens
4861          --  when the type of X is static and the range extends to the full
4862          --  range of stored values).
4863
4864          --  Unsigned types. Note: it is safe to consider only whether the
4865          --  subtype is unsigned, since we will in that case be doing all
4866          --  unsigned comparisons based on the subtype range. Since we use the
4867          --  actual subtype object size, this is appropriate.
4868
4869          --  For example, if we have
4870
4871          --    subtype x is integer range 1 .. 200;
4872          --    for x'Object_Size use 8;
4873
4874          --  Now the base type is signed, but objects of this type are bits
4875          --  unsigned, and doing an unsigned test of the range 1 to 200 is
4876          --  correct, even though a value greater than 127 looks signed to a
4877          --  signed comparison.
4878
4879          elsif Is_Unsigned_Type (Ptyp) then
4880             if Esize (Ptyp) <= 32 then
4881                Btyp := RTE (RE_Unsigned_32);
4882             else
4883                Btyp := RTE (RE_Unsigned_64);
4884             end if;
4885
4886             Rewrite (N, Make_Range_Test);
4887
4888          --  Signed types
4889
4890          else
4891             if Esize (Ptyp) <= Esize (Standard_Integer) then
4892                Btyp := Standard_Integer;
4893             else
4894                Btyp := Universal_Integer;
4895             end if;
4896
4897             Rewrite (N, Make_Range_Test);
4898          end if;
4899
4900          Analyze_And_Resolve (N, Standard_Boolean);
4901          Validity_Checks_On := Save_Validity_Checks_On;
4902       end Valid;
4903
4904       -----------
4905       -- Value --
4906       -----------
4907
4908       --  Value attribute is handled in separate unti Exp_Imgv
4909
4910       when Attribute_Value =>
4911          Exp_Imgv.Expand_Value_Attribute (N);
4912
4913       -----------------
4914       -- Value_Size --
4915       -----------------
4916
4917       --  The processing for Value_Size shares the processing for Size
4918
4919       -------------
4920       -- Version --
4921       -------------
4922
4923       --  The processing for Version shares the processing for Body_Version
4924
4925       ----------------
4926       -- Wide_Image --
4927       ----------------
4928
4929       --  Wide_Image attribute is handled in separate unit Exp_Imgv
4930
4931       when Attribute_Wide_Image =>
4932          Exp_Imgv.Expand_Wide_Image_Attribute (N);
4933
4934       ---------------------
4935       -- Wide_Wide_Image --
4936       ---------------------
4937
4938       --  Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
4939
4940       when Attribute_Wide_Wide_Image =>
4941          Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
4942
4943       ----------------
4944       -- Wide_Value --
4945       ----------------
4946
4947       --  We expand typ'Wide_Value (X) into
4948
4949       --    typ'Value
4950       --      (Wide_String_To_String (X, Wide_Character_Encoding_Method))
4951
4952       --  Wide_String_To_String is a runtime function that converts its wide
4953       --  string argument to String, converting any non-translatable characters
4954       --  into appropriate escape sequences. This preserves the required
4955       --  semantics of Wide_Value in all cases, and results in a very simple
4956       --  implementation approach.
4957
4958       --  Note: for this approach to be fully standard compliant for the cases
4959       --  where typ is Wide_Character and Wide_Wide_Character, the encoding
4960       --  method must cover the entire character range (e.g. UTF-8). But that
4961       --  is a reasonable requirement when dealing with encoded character
4962       --  sequences. Presumably if one of the restrictive encoding mechanisms
4963       --  is in use such as Shift-JIS, then characters that cannot be
4964       --  represented using this encoding will not appear in any case.
4965
4966       when Attribute_Wide_Value => Wide_Value :
4967       begin
4968          Rewrite (N,
4969            Make_Attribute_Reference (Loc,
4970              Prefix         => Pref,
4971              Attribute_Name => Name_Value,
4972
4973              Expressions    => New_List (
4974                Make_Function_Call (Loc,
4975                  Name =>
4976                    New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
4977
4978                  Parameter_Associations => New_List (
4979                    Relocate_Node (First (Exprs)),
4980                    Make_Integer_Literal (Loc,
4981                      Intval => Int (Wide_Character_Encoding_Method)))))));
4982
4983          Analyze_And_Resolve (N, Typ);
4984       end Wide_Value;
4985
4986       ---------------------
4987       -- Wide_Wide_Value --
4988       ---------------------
4989
4990       --  We expand typ'Wide_Value_Value (X) into
4991
4992       --    typ'Value
4993       --      (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
4994
4995       --  Wide_Wide_String_To_String is a runtime function that converts its
4996       --  wide string argument to String, converting any non-translatable
4997       --  characters into appropriate escape sequences. This preserves the
4998       --  required semantics of Wide_Wide_Value in all cases, and results in a
4999       --  very simple implementation approach.
5000
5001       --  It's not quite right where typ = Wide_Wide_Character, because the
5002       --  encoding method may not cover the whole character type ???
5003
5004       when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5005       begin
5006          Rewrite (N,
5007            Make_Attribute_Reference (Loc,
5008              Prefix         => Pref,
5009              Attribute_Name => Name_Value,
5010
5011              Expressions    => New_List (
5012                Make_Function_Call (Loc,
5013                  Name =>
5014                    New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc),
5015
5016                  Parameter_Associations => New_List (
5017                    Relocate_Node (First (Exprs)),
5018                    Make_Integer_Literal (Loc,
5019                      Intval => Int (Wide_Character_Encoding_Method)))))));
5020
5021          Analyze_And_Resolve (N, Typ);
5022       end Wide_Wide_Value;
5023
5024       ---------------------
5025       -- Wide_Wide_Width --
5026       ---------------------
5027
5028       --  Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
5029
5030       when Attribute_Wide_Wide_Width =>
5031          Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
5032
5033       ----------------
5034       -- Wide_Width --
5035       ----------------
5036
5037       --  Wide_Width attribute is handled in separate unit Exp_Imgv
5038
5039       when Attribute_Wide_Width =>
5040          Exp_Imgv.Expand_Width_Attribute (N, Wide);
5041
5042       -----------
5043       -- Width --
5044       -----------
5045
5046       --  Width attribute is handled in separate unit Exp_Imgv
5047
5048       when Attribute_Width =>
5049          Exp_Imgv.Expand_Width_Attribute (N, Normal);
5050
5051       -----------
5052       -- Write --
5053       -----------
5054
5055       when Attribute_Write => Write : declare
5056          P_Type : constant Entity_Id := Entity (Pref);
5057          U_Type : constant Entity_Id := Underlying_Type (P_Type);
5058          Pname  : Entity_Id;
5059          Decl   : Node_Id;
5060          Prag   : Node_Id;
5061          Arg3   : Node_Id;
5062          Wfunc  : Node_Id;
5063
5064       begin
5065          --  If no underlying type, we have an error that will be diagnosed
5066          --  elsewhere, so here we just completely ignore the expansion.
5067
5068          if No (U_Type) then
5069             return;
5070          end if;
5071
5072          --  The simple case, if there is a TSS for Write, just call it
5073
5074          Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
5075
5076          if Present (Pname) then
5077             null;
5078
5079          else
5080             --  If there is a Stream_Convert pragma, use it, we rewrite
5081
5082             --     sourcetyp'Output (stream, Item)
5083
5084             --  as
5085
5086             --     strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5087
5088             --  where strmwrite is the given Write function that converts an
5089             --  argument of type sourcetyp or a type acctyp, from which it is
5090             --  derived to type strmtyp. The conversion to acttyp is required
5091             --  for the derived case.
5092
5093             Prag := Get_Stream_Convert_Pragma (P_Type);
5094
5095             if Present (Prag) then
5096                Arg3 :=
5097                  Next (Next (First (Pragma_Argument_Associations (Prag))));
5098                Wfunc := Entity (Expression (Arg3));
5099
5100                Rewrite (N,
5101                  Make_Attribute_Reference (Loc,
5102                    Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5103                    Attribute_Name => Name_Output,
5104                    Expressions => New_List (
5105                      Relocate_Node (First (Exprs)),
5106                      Make_Function_Call (Loc,
5107                        Name => New_Occurrence_Of (Wfunc, Loc),
5108                        Parameter_Associations => New_List (
5109                          OK_Convert_To (Etype (First_Formal (Wfunc)),
5110                            Relocate_Node (Next (First (Exprs)))))))));
5111
5112                Analyze (N);
5113                return;
5114
5115             --  For elementary types, we call the W_xxx routine directly
5116
5117             elsif Is_Elementary_Type (U_Type) then
5118                Rewrite (N, Build_Elementary_Write_Call (N));
5119                Analyze (N);
5120                return;
5121
5122             --  Array type case
5123
5124             elsif Is_Array_Type (U_Type) then
5125                Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
5126                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5127
5128             --  Tagged type case, use the primitive Write function. Note that
5129             --  this will dispatch in the class-wide case which is what we want
5130
5131             elsif Is_Tagged_Type (U_Type) then
5132                Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
5133
5134             --  All other record type cases, including protected records.
5135             --  The latter only arise for expander generated code for
5136             --  handling shared passive partition access.
5137
5138             else
5139                pragma Assert
5140                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5141
5142                --  Ada 2005 (AI-216): Program_Error is raised when executing
5143                --  the default implementation of the Write attribute of an
5144                --  Unchecked_Union type. However, if the 'Write reference is
5145                --  within the generated Output stream procedure, Write outputs
5146                --  the components, and the default values of the discriminant
5147                --  are streamed by the Output procedure itself.
5148
5149                if Is_Unchecked_Union (Base_Type (U_Type))
5150                  and not Is_TSS (Current_Scope, TSS_Stream_Output)
5151                then
5152                   Insert_Action (N,
5153                     Make_Raise_Program_Error (Loc,
5154                       Reason => PE_Unchecked_Union_Restriction));
5155                end if;
5156
5157                if Has_Discriminants (U_Type)
5158                  and then Present
5159                    (Discriminant_Default_Value (First_Discriminant (U_Type)))
5160                then
5161                   Build_Mutable_Record_Write_Procedure
5162                     (Loc, Base_Type (U_Type), Decl, Pname);
5163                else
5164                   Build_Record_Write_Procedure
5165                     (Loc, Base_Type (U_Type), Decl, Pname);
5166                end if;
5167
5168                Insert_Action (N, Decl);
5169             end if;
5170          end if;
5171
5172          --  If we fall through, Pname is the procedure to be called
5173
5174          Rewrite_Stream_Proc_Call (Pname);
5175       end Write;
5176
5177       --  Component_Size is handled by the back end, unless the component size
5178       --  is known at compile time, which is always true in the packed array
5179       --  case. It is important that the packed array case is handled in the
5180       --  front end (see Eval_Attribute) since the back end would otherwise get
5181       --  confused by the equivalent packed array type.
5182
5183       when Attribute_Component_Size =>
5184          null;
5185
5186       --  The following attributes are handled by the back end (except that
5187       --  static cases have already been evaluated during semantic processing,
5188       --  but in any case the back end should not count on this). The one bit
5189       --  of special processing required is that these attributes typically
5190       --  generate conditionals in the code, so we need to check the relevant
5191       --  restriction.
5192
5193       when Attribute_Max                          |
5194            Attribute_Min                          =>
5195          Check_Restriction (No_Implicit_Conditionals, N);
5196
5197       --  The following attributes are handled by the back end (except that
5198       --  static cases have already been evaluated during semantic processing,
5199       --  but in any case the back end should not count on this).
5200
5201       --  The back end also handles the non-class-wide cases of Size
5202
5203       when Attribute_Bit_Order                    |
5204            Attribute_Code_Address                 |
5205            Attribute_Definite                     |
5206            Attribute_Null_Parameter               |
5207            Attribute_Passed_By_Reference          |
5208            Attribute_Pool_Address                 =>
5209          null;
5210
5211       --  The following attributes are also handled by the back end, but return
5212       --  a universal integer result, so may need a conversion for checking
5213       --  that the result is in range.
5214
5215       when Attribute_Aft                          |
5216            Attribute_Bit                          |
5217            Attribute_Max_Size_In_Storage_Elements
5218       =>
5219          Apply_Universal_Integer_Attribute_Checks (N);
5220
5221       --  The following attributes should not appear at this stage, since they
5222       --  have already been handled by the analyzer (and properly rewritten
5223       --  with corresponding values or entities to represent the right values)
5224
5225       when Attribute_Abort_Signal                 |
5226            Attribute_Address_Size                 |
5227            Attribute_Base                         |
5228            Attribute_Class                        |
5229            Attribute_Default_Bit_Order            |
5230            Attribute_Delta                        |
5231            Attribute_Denorm                       |
5232            Attribute_Digits                       |
5233            Attribute_Emax                         |
5234            Attribute_Enabled                      |
5235            Attribute_Epsilon                      |
5236            Attribute_Fast_Math                    |
5237            Attribute_Has_Access_Values            |
5238            Attribute_Has_Discriminants            |
5239            Attribute_Has_Tagged_Values            |
5240            Attribute_Large                        |
5241            Attribute_Machine_Emax                 |
5242            Attribute_Machine_Emin                 |
5243            Attribute_Machine_Mantissa             |
5244            Attribute_Machine_Overflows            |
5245            Attribute_Machine_Radix                |
5246            Attribute_Machine_Rounds               |
5247            Attribute_Maximum_Alignment            |
5248            Attribute_Model_Emin                   |
5249            Attribute_Model_Epsilon                |
5250            Attribute_Model_Mantissa               |
5251            Attribute_Model_Small                  |
5252            Attribute_Modulus                      |
5253            Attribute_Partition_ID                 |
5254            Attribute_Range                        |
5255            Attribute_Safe_Emax                    |
5256            Attribute_Safe_First                   |
5257            Attribute_Safe_Large                   |
5258            Attribute_Safe_Last                    |
5259            Attribute_Safe_Small                   |
5260            Attribute_Scale                        |
5261            Attribute_Signed_Zeros                 |
5262            Attribute_Small                        |
5263            Attribute_Storage_Unit                 |
5264            Attribute_Stub_Type                    |
5265            Attribute_Target_Name                  |
5266            Attribute_Type_Class                   |
5267            Attribute_Unconstrained_Array          |
5268            Attribute_Universal_Literal_String     |
5269            Attribute_Wchar_T_Size                 |
5270            Attribute_Word_Size                    =>
5271
5272          raise Program_Error;
5273
5274       --  The Asm_Input and Asm_Output attributes are not expanded at this
5275       --  stage, but will be eliminated in the expansion of the Asm call, see
5276       --  Exp_Intr for details. So the back end will never see these either.
5277
5278       when Attribute_Asm_Input                    |
5279            Attribute_Asm_Output                   =>
5280
5281          null;
5282
5283       end case;
5284
5285    exception
5286       when RE_Not_Available =>
5287          return;
5288    end Expand_N_Attribute_Reference;
5289
5290    ----------------------
5291    -- Expand_Pred_Succ --
5292    ----------------------
5293
5294    --  For typ'Pred (exp), we generate the check
5295
5296    --    [constraint_error when exp = typ'Base'First]
5297
5298    --  Similarly, for typ'Succ (exp), we generate the check
5299
5300    --    [constraint_error when exp = typ'Base'Last]
5301
5302    --  These checks are not generated for modular types, since the proper
5303    --  semantics for Succ and Pred on modular types is to wrap, not raise CE.
5304
5305    procedure Expand_Pred_Succ (N : Node_Id) is
5306       Loc  : constant Source_Ptr := Sloc (N);
5307       Cnam : Name_Id;
5308
5309    begin
5310       if Attribute_Name (N) = Name_Pred then
5311          Cnam := Name_First;
5312       else
5313          Cnam := Name_Last;
5314       end if;
5315
5316       Insert_Action (N,
5317         Make_Raise_Constraint_Error (Loc,
5318           Condition =>
5319             Make_Op_Eq (Loc,
5320               Left_Opnd =>
5321                 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
5322               Right_Opnd =>
5323                 Make_Attribute_Reference (Loc,
5324                   Prefix =>
5325                     New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
5326                   Attribute_Name => Cnam)),
5327           Reason => CE_Overflow_Check_Failed));
5328    end Expand_Pred_Succ;
5329
5330    -------------------
5331    -- Find_Fat_Info --
5332    -------------------
5333
5334    procedure Find_Fat_Info
5335      (T        : Entity_Id;
5336       Fat_Type : out Entity_Id;
5337       Fat_Pkg  : out RE_Id)
5338    is
5339       Btyp : constant Entity_Id := Base_Type (T);
5340       Rtyp : constant Entity_Id := Root_Type (T);
5341       Digs : constant Nat       := UI_To_Int (Digits_Value (Btyp));
5342
5343    begin
5344       --  If the base type is VAX float, then get appropriate VAX float type
5345
5346       if Vax_Float (Btyp) then
5347          case Digs is
5348             when 6 =>
5349                Fat_Type := RTE (RE_Fat_VAX_F);
5350                Fat_Pkg  := RE_Attr_VAX_F_Float;
5351
5352             when 9 =>
5353                Fat_Type := RTE (RE_Fat_VAX_D);
5354                Fat_Pkg  := RE_Attr_VAX_D_Float;
5355
5356             when 15 =>
5357                Fat_Type := RTE (RE_Fat_VAX_G);
5358                Fat_Pkg  := RE_Attr_VAX_G_Float;
5359
5360             when others =>
5361                raise Program_Error;
5362          end case;
5363
5364       --  If root type is VAX float, this is the case where the library has
5365       --  been recompiled in VAX float mode, and we have an IEEE float type.
5366       --  This is when we use the special IEEE Fat packages.
5367
5368       elsif Vax_Float (Rtyp) then
5369          case Digs is
5370             when 6 =>
5371                Fat_Type := RTE (RE_Fat_IEEE_Short);
5372                Fat_Pkg  := RE_Attr_IEEE_Short;
5373
5374             when 15 =>
5375                Fat_Type := RTE (RE_Fat_IEEE_Long);
5376                Fat_Pkg  := RE_Attr_IEEE_Long;
5377
5378             when others =>
5379                raise Program_Error;
5380          end case;
5381
5382       --  If neither the base type nor the root type is VAX_Float then VAX
5383       --  float is out of the picture, and we can just use the root type.
5384
5385       else
5386          Fat_Type := Rtyp;
5387
5388          if Fat_Type = Standard_Short_Float then
5389             Fat_Pkg := RE_Attr_Short_Float;
5390
5391          elsif Fat_Type = Standard_Float then
5392             Fat_Pkg := RE_Attr_Float;
5393
5394          elsif Fat_Type = Standard_Long_Float then
5395             Fat_Pkg := RE_Attr_Long_Float;
5396
5397          elsif Fat_Type = Standard_Long_Long_Float then
5398             Fat_Pkg := RE_Attr_Long_Long_Float;
5399
5400          --  Universal real (which is its own root type) is treated as being
5401          --  equivalent to Standard.Long_Long_Float, since it is defined to
5402          --  have the same precision as the longest Float type.
5403
5404          elsif Fat_Type = Universal_Real then
5405             Fat_Type := Standard_Long_Long_Float;
5406             Fat_Pkg := RE_Attr_Long_Long_Float;
5407
5408          else
5409             raise Program_Error;
5410          end if;
5411       end if;
5412    end Find_Fat_Info;
5413
5414    ----------------------------
5415    -- Find_Stream_Subprogram --
5416    ----------------------------
5417
5418    function Find_Stream_Subprogram
5419      (Typ : Entity_Id;
5420       Nam : TSS_Name_Type) return Entity_Id
5421    is
5422       Base_Typ : constant Entity_Id := Base_Type (Typ);
5423       Ent      : constant Entity_Id := TSS (Typ, Nam);
5424
5425    begin
5426       if Present (Ent) then
5427          return Ent;
5428       end if;
5429
5430       --  Stream attributes for strings are expanded into library calls. The
5431       --  following checks are disabled when the run-time is not available or
5432       --  when compiling predefined types due to bootstrap issues. As a result,
5433       --  the compiler will generate in-place stream routines for string types
5434       --  that appear in GNAT's library, but will generate calls via rtsfind
5435       --  to library routines for user code.
5436       --  ??? For now, disable this code for JVM, since this generates a
5437       --  VerifyError exception at run-time on e.g. c330001.
5438       --  This is disabled for AAMP, to avoid making dependences on files not
5439       --  supported in the AAMP library (such as s-fileio.adb).
5440
5441       if VM_Target /= JVM_Target
5442         and then not AAMP_On_Target
5443         and then
5444           not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5445       then
5446          --  String as defined in package Ada
5447
5448          if Base_Typ = Standard_String then
5449             if Restriction_Active (No_Stream_Optimizations) then
5450                if Nam = TSS_Stream_Input then
5451                   return RTE (RE_String_Input);
5452
5453                elsif Nam = TSS_Stream_Output then
5454                   return RTE (RE_String_Output);
5455
5456                elsif Nam = TSS_Stream_Read then
5457                   return RTE (RE_String_Read);
5458
5459                else pragma Assert (Nam = TSS_Stream_Write);
5460                   return RTE (RE_String_Write);
5461                end if;
5462
5463             else
5464                if Nam = TSS_Stream_Input then
5465                   return RTE (RE_String_Input_Blk_IO);
5466
5467                elsif Nam = TSS_Stream_Output then
5468                   return RTE (RE_String_Output_Blk_IO);
5469
5470                elsif Nam = TSS_Stream_Read then
5471                   return RTE (RE_String_Read_Blk_IO);
5472
5473                else pragma Assert (Nam = TSS_Stream_Write);
5474                   return RTE (RE_String_Write_Blk_IO);
5475                end if;
5476             end if;
5477
5478          --  Wide_String as defined in package Ada
5479
5480          elsif Base_Typ = Standard_Wide_String then
5481             if Restriction_Active (No_Stream_Optimizations) then
5482                if Nam = TSS_Stream_Input then
5483                   return RTE (RE_Wide_String_Input);
5484
5485                elsif Nam = TSS_Stream_Output then
5486                   return RTE (RE_Wide_String_Output);
5487
5488                elsif Nam = TSS_Stream_Read then
5489                   return RTE (RE_Wide_String_Read);
5490
5491                else pragma Assert (Nam = TSS_Stream_Write);
5492                   return RTE (RE_Wide_String_Write);
5493                end if;
5494
5495             else
5496                if Nam = TSS_Stream_Input then
5497                   return RTE (RE_Wide_String_Input_Blk_IO);
5498
5499                elsif Nam = TSS_Stream_Output then
5500                   return RTE (RE_Wide_String_Output_Blk_IO);
5501
5502                elsif Nam = TSS_Stream_Read then
5503                   return RTE (RE_Wide_String_Read_Blk_IO);
5504
5505                else pragma Assert (Nam = TSS_Stream_Write);
5506                   return RTE (RE_Wide_String_Write_Blk_IO);
5507                end if;
5508             end if;
5509
5510          --  Wide_Wide_String as defined in package Ada
5511
5512          elsif Base_Typ = Standard_Wide_Wide_String then
5513             if Restriction_Active (No_Stream_Optimizations) then
5514                if Nam = TSS_Stream_Input then
5515                   return RTE (RE_Wide_Wide_String_Input);
5516
5517                elsif Nam = TSS_Stream_Output then
5518                   return RTE (RE_Wide_Wide_String_Output);
5519
5520                elsif Nam = TSS_Stream_Read then
5521                   return RTE (RE_Wide_Wide_String_Read);
5522
5523                else pragma Assert (Nam = TSS_Stream_Write);
5524                   return RTE (RE_Wide_Wide_String_Write);
5525                end if;
5526
5527             else
5528                if Nam = TSS_Stream_Input then
5529                   return RTE (RE_Wide_Wide_String_Input_Blk_IO);
5530
5531                elsif Nam = TSS_Stream_Output then
5532                   return RTE (RE_Wide_Wide_String_Output_Blk_IO);
5533
5534                elsif Nam = TSS_Stream_Read then
5535                   return RTE (RE_Wide_Wide_String_Read_Blk_IO);
5536
5537                else pragma Assert (Nam = TSS_Stream_Write);
5538                   return RTE (RE_Wide_Wide_String_Write_Blk_IO);
5539                end if;
5540             end if;
5541          end if;
5542       end if;
5543
5544       if Is_Tagged_Type (Typ)
5545         and then Is_Derived_Type (Typ)
5546       then
5547          return Find_Prim_Op (Typ, Nam);
5548       else
5549          return Find_Inherited_TSS (Typ, Nam);
5550       end if;
5551    end Find_Stream_Subprogram;
5552
5553    -----------------------
5554    -- Get_Index_Subtype --
5555    -----------------------
5556
5557    function Get_Index_Subtype (N : Node_Id) return Node_Id is
5558       P_Type : Entity_Id := Etype (Prefix (N));
5559       Indx   : Node_Id;
5560       J      : Int;
5561
5562    begin
5563       if Is_Access_Type (P_Type) then
5564          P_Type := Designated_Type (P_Type);
5565       end if;
5566
5567       if No (Expressions (N)) then
5568          J := 1;
5569       else
5570          J := UI_To_Int (Expr_Value (First (Expressions (N))));
5571       end if;
5572
5573       Indx := First_Index (P_Type);
5574       while J > 1 loop
5575          Next_Index (Indx);
5576          J := J - 1;
5577       end loop;
5578
5579       return Etype (Indx);
5580    end Get_Index_Subtype;
5581
5582    -------------------------------
5583    -- Get_Stream_Convert_Pragma --
5584    -------------------------------
5585
5586    function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
5587       Typ : Entity_Id;
5588       N   : Node_Id;
5589
5590    begin
5591       --  Note: we cannot use Get_Rep_Pragma here because of the peculiarity
5592       --  that a stream convert pragma for a tagged type is not inherited from
5593       --  its parent. Probably what is wrong here is that it is basically
5594       --  incorrect to consider a stream convert pragma to be a representation
5595       --  pragma at all ???
5596
5597       N := First_Rep_Item (Implementation_Base_Type (T));
5598       while Present (N) loop
5599          if Nkind (N) = N_Pragma
5600            and then Pragma_Name (N) = Name_Stream_Convert
5601          then
5602             --  For tagged types this pragma is not inherited, so we
5603             --  must verify that it is defined for the given type and
5604             --  not an ancestor.
5605
5606             Typ :=
5607               Entity (Expression (First (Pragma_Argument_Associations (N))));
5608
5609             if not Is_Tagged_Type (T)
5610               or else T = Typ
5611               or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
5612             then
5613                return N;
5614             end if;
5615          end if;
5616
5617          Next_Rep_Item (N);
5618       end loop;
5619
5620       return Empty;
5621    end Get_Stream_Convert_Pragma;
5622
5623    ---------------------------------
5624    -- Is_Constrained_Packed_Array --
5625    ---------------------------------
5626
5627    function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
5628       Arr : Entity_Id := Typ;
5629
5630    begin
5631       if Is_Access_Type (Arr) then
5632          Arr := Designated_Type (Arr);
5633       end if;
5634
5635       return Is_Array_Type (Arr)
5636         and then Is_Constrained (Arr)
5637         and then Present (Packed_Array_Type (Arr));
5638    end Is_Constrained_Packed_Array;
5639
5640    ----------------------------------------
5641    -- Is_Inline_Floating_Point_Attribute --
5642    ----------------------------------------
5643
5644    function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
5645       Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
5646
5647    begin
5648       if Nkind (Parent (N)) /= N_Type_Conversion
5649         or else not Is_Integer_Type (Etype (Parent (N)))
5650       then
5651          return False;
5652       end if;
5653
5654       --  Should also support 'Machine_Rounding and 'Unbiased_Rounding, but
5655       --  required back end support has not been implemented yet ???
5656
5657       return Id = Attribute_Truncation;
5658    end Is_Inline_Floating_Point_Attribute;
5659
5660 end Exp_Attr;