OSDN Git Service

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