OSDN Git Service

Fix copyright problems reported by Doug Evans.
[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-2002 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Einfo;    use Einfo;
30 with Exp_Ch2;  use Exp_Ch2;
31 with Exp_Ch9;  use Exp_Ch9;
32 with Exp_Imgv; use Exp_Imgv;
33 with Exp_Pakd; use Exp_Pakd;
34 with Exp_Strm; use Exp_Strm;
35 with Exp_Tss;  use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Gnatvsn;  use Gnatvsn;
38 with Hostparm; use Hostparm;
39 with Lib;      use Lib;
40 with Namet;    use Namet;
41 with Nmake;    use Nmake;
42 with Nlists;   use Nlists;
43 with Opt;      use Opt;
44 with Restrict; use Restrict;
45 with Rtsfind;  use Rtsfind;
46 with Sem;      use Sem;
47 with Sem_Ch7;  use Sem_Ch7;
48 with Sem_Ch8;  use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res;  use Sem_Res;
51 with Sem_Util; use Sem_Util;
52 with Sinfo;    use Sinfo;
53 with Snames;   use Snames;
54 with Stand;    use Stand;
55 with Stringt;  use Stringt;
56 with Tbuild;   use Tbuild;
57 with Ttypes;   use Ttypes;
58 with Uintp;    use Uintp;
59 with Uname;    use Uname;
60 with Validsw;  use Validsw;
61
62 package body Exp_Attr is
63
64    -----------------------
65    -- Local Subprograms --
66    -----------------------
67
68    procedure Compile_Stream_Body_In_Scope
69      (N     : Node_Id;
70       Decl  : Node_Id;
71       Arr   : Entity_Id;
72       Check : Boolean);
73    --  The body for a stream subprogram may be generated outside of the scope
74    --  of the type. If the type is fully private, it may depend on the full
75    --  view of other types (e.g. indices) that are currently private as well.
76    --  We install the declarations of the package in which the type is declared
77    --  before compiling the body in what is its proper environment. The Check
78    --  parameter indicates if checks are to be suppressed for the stream body.
79    --  We suppress checks for array/record reads, since the rule is that these
80    --  are like assignments, out of range values due to uninitialized storage,
81    --  or other invalid values do NOT cause a Constraint_Error to be raised.
82
83    procedure Expand_Fpt_Attribute
84      (N   : Node_Id;
85       Rtp : Entity_Id;
86       Args : List_Id);
87    --  This procedure expands a call to a floating-point attribute function.
88    --  N is the attribute reference node, and Args is a list of arguments to
89    --  be passed to the function call. Rtp is the root type of the floating
90    --  point type involved (used to select the proper generic instantiation
91    --  of the package containing the attribute routines).
92
93    procedure Expand_Fpt_Attribute_R (N : Node_Id);
94    --  This procedure expands a call to a floating-point attribute function
95    --  that takes a single floating-point argument.
96
97    procedure Expand_Fpt_Attribute_RI (N : Node_Id);
98    --  This procedure expands a call to a floating-point attribute function
99    --  that takes one floating-point argument and one integer argument.
100
101    procedure Expand_Fpt_Attribute_RR (N : Node_Id);
102    --  This procedure expands a call to a floating-point attribute function
103    --  that takes two floating-point arguments.
104
105    procedure Expand_Pred_Succ (N : Node_Id);
106    --  Handles expansion of Pred or Succ attributes for case of non-real
107    --  operand with overflow checking required.
108
109    function Get_Index_Subtype (N : Node_Id) return Entity_Id;
110    --  Used for Last, Last, and Length, when the prefix is an array type,
111    --  Obtains the corresponding index subtype.
112
113    procedure Expand_Access_To_Type (N : Node_Id);
114    --  A reference to a type within its own scope is resolved to a reference
115    --  to the current instance of the type in its initialization procedure.
116
117    function Find_Inherited_TSS
118      (Typ : Entity_Id;
119       Nam : Name_Id) return Entity_Id;
120
121    function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
122    --  Utility for array attributes, returns true on packed constrained
123    --  arrays, and on access to same.
124
125    ----------------------------------
126    -- Compile_Stream_Body_In_Scope --
127    ----------------------------------
128
129    procedure Compile_Stream_Body_In_Scope
130      (N     : Node_Id;
131       Decl  : Node_Id;
132       Arr   : Entity_Id;
133       Check : Boolean)
134    is
135       Installed : Boolean := False;
136       Scop      : constant Entity_Id := Scope (Arr);
137       Curr      : constant Entity_Id := Current_Scope;
138
139    begin
140       if Is_Hidden (Arr)
141         and then not In_Open_Scopes (Scop)
142         and then Ekind (Scop) = E_Package
143       then
144          New_Scope (Scop);
145          Install_Visible_Declarations (Scop);
146          Install_Private_Declarations (Scop);
147          Installed := True;
148
149          --  The entities in the package are now visible, but the generated
150          --  stream entity must appear in the current scope (usually an
151          --  enclosing stream function) so that itypes all have their proper
152          --  scopes.
153
154          New_Scope (Curr);
155       end if;
156
157       if Check then
158          Insert_Action (N, Decl);
159       else
160          Insert_Action (N, Decl, All_Checks);
161       end if;
162
163       if Installed then
164
165          --  Remove extra copy of current scope, and package itself
166
167          Pop_Scope;
168          End_Package_Scope (Scop);
169       end if;
170    end Compile_Stream_Body_In_Scope;
171
172    ---------------------------
173    -- Expand_Access_To_Type --
174    ---------------------------
175
176    procedure Expand_Access_To_Type (N : Node_Id) is
177       Loc    : constant Source_Ptr   := Sloc (N);
178       Typ    : constant Entity_Id    := Etype (N);
179       Pref   : constant Node_Id      := Prefix (N);
180       Par    : Node_Id;
181       Formal : Entity_Id;
182
183    begin
184       if Is_Entity_Name (Pref)
185         and then Is_Type (Entity (Pref))
186       then
187          --  If the current instance name denotes a task type,
188          --  then the access attribute is rewritten to be the
189          --  name of the "_task" parameter associated with the
190          --  task type's task body procedure. An unchecked
191          --  conversion is applied to ensure a type match in
192          --  cases of expander-generated calls (e.g., init procs).
193
194          if Is_Task_Type (Entity (Pref)) then
195             Formal :=
196               First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
197
198             while Present (Formal) loop
199                exit when Chars (Formal) = Name_uTask;
200                Next_Entity (Formal);
201             end loop;
202
203             pragma Assert (Present (Formal));
204
205             Rewrite (N,
206               Unchecked_Convert_To (Typ, New_Occurrence_Of (Formal, Loc)));
207             Set_Etype (N, Typ);
208
209          --  The expression must appear in a default expression,
210          --  (which in the initialization procedure is the rhs of
211          --  an assignment), and not in a discriminant constraint.
212
213          else
214             Par := Parent (N);
215
216             while Present (Par) loop
217                exit when Nkind (Par) = N_Assignment_Statement;
218
219                if Nkind (Par) = N_Component_Declaration then
220                   return;
221                end if;
222
223                Par := Parent (Par);
224             end loop;
225
226             if Present (Par) then
227                Rewrite (N,
228                  Make_Attribute_Reference (Loc,
229                    Prefix => Make_Identifier (Loc, Name_uInit),
230                    Attribute_Name  => Attribute_Name (N)));
231
232                Analyze_And_Resolve (N, Typ);
233             end if;
234          end if;
235       end if;
236    end Expand_Access_To_Type;
237
238    --------------------------
239    -- Expand_Fpt_Attribute --
240    --------------------------
241
242    procedure Expand_Fpt_Attribute
243      (N    : Node_Id;
244       Rtp  : Entity_Id;
245       Args : List_Id)
246    is
247       Loc : constant Source_Ptr := Sloc (N);
248       Typ : constant Entity_Id  := Etype (N);
249       Pkg : RE_Id;
250       Fnm : Node_Id;
251
252    begin
253       --  The function name is the selected component Fat_xxx.yyy where xxx
254       --  is the floating-point root type, and yyy is the attribute name
255
256       --  Note: it would be more usual to have separate RE entries for each
257       --  of the entities in the Fat packages, but first they have identical
258       --  names (so we would have to have lots of renaming declarations to
259       --  meet the normal RE rule of separate names for all runtime entities),
260       --  and second there would be an awful lot of them!
261
262       if Rtp = Standard_Short_Float then
263          Pkg := RE_Fat_Short_Float;
264       elsif Rtp = Standard_Float then
265          Pkg := RE_Fat_Float;
266       elsif Rtp = Standard_Long_Float then
267          Pkg := RE_Fat_Long_Float;
268       else
269          Pkg := RE_Fat_Long_Long_Float;
270       end if;
271
272       Fnm :=
273         Make_Selected_Component (Loc,
274           Prefix        => New_Reference_To (RTE (Pkg), Loc),
275           Selector_Name => Make_Identifier (Loc, Attribute_Name (N)));
276
277       --  The generated call is given the provided set of parameters, and then
278       --  wrapped in a conversion which converts the result to the target type
279
280       Rewrite (N,
281         Unchecked_Convert_To (Etype (N),
282           Make_Function_Call (Loc,
283             Name => Fnm,
284             Parameter_Associations => Args)));
285
286       Analyze_And_Resolve (N, Typ);
287
288    end Expand_Fpt_Attribute;
289
290    ----------------------------
291    -- Expand_Fpt_Attribute_R --
292    ----------------------------
293
294    --  The single argument is converted to its root type to call the
295    --  appropriate runtime function, with the actual call being built
296    --  by Expand_Fpt_Attribute
297
298    procedure Expand_Fpt_Attribute_R (N : Node_Id) is
299       E1  : constant Node_Id    := First (Expressions (N));
300       Rtp : constant Entity_Id  := Root_Type (Etype (E1));
301
302    begin
303       Expand_Fpt_Attribute (N, Rtp, New_List (
304         Unchecked_Convert_To (Rtp, Relocate_Node (E1))));
305    end Expand_Fpt_Attribute_R;
306
307    -----------------------------
308    -- Expand_Fpt_Attribute_RI --
309    -----------------------------
310
311    --  The first argument is converted to its root type and the second
312    --  argument is converted to standard long long integer to call the
313    --  appropriate runtime function, with the actual call being built
314    --  by Expand_Fpt_Attribute
315
316    procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
317       E1  : constant Node_Id   := First (Expressions (N));
318       Rtp : constant Entity_Id := Root_Type (Etype (E1));
319       E2  : constant Node_Id   := Next (E1);
320
321    begin
322       Expand_Fpt_Attribute (N, Rtp, New_List (
323         Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
324         Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
325    end Expand_Fpt_Attribute_RI;
326
327    -----------------------------
328    -- Expand_Fpt_Attribute_RR --
329    -----------------------------
330
331    --  The two arguments is converted to their root types to call the
332    --  appropriate runtime function, with the actual call being built
333    --  by Expand_Fpt_Attribute
334
335    procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
336       E1  : constant Node_Id   := First (Expressions (N));
337       Rtp : constant Entity_Id := Root_Type (Etype (E1));
338       E2  : constant Node_Id   := Next (E1);
339
340    begin
341       Expand_Fpt_Attribute (N, Rtp, New_List (
342         Unchecked_Convert_To (Rtp, Relocate_Node (E1)),
343         Unchecked_Convert_To (Rtp, Relocate_Node (E2))));
344    end Expand_Fpt_Attribute_RR;
345
346    ----------------------------------
347    -- Expand_N_Attribute_Reference --
348    ----------------------------------
349
350    procedure Expand_N_Attribute_Reference (N : Node_Id) is
351       Loc   : constant Source_Ptr   := Sloc (N);
352       Typ   : constant Entity_Id    := Etype (N);
353       Btyp  : constant Entity_Id    := Base_Type (Typ);
354       Pref  : constant Node_Id      := Prefix (N);
355       Exprs : constant List_Id      := Expressions (N);
356       Id    : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
357
358       procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
359       --  Rewrites a stream attribute for Read, Write or Output with the
360       --  procedure call. Pname is the entity for the procedure to call.
361
362       ------------------------------
363       -- Rewrite_Stream_Proc_Call --
364       ------------------------------
365
366       procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
367          Item       : constant Node_Id   := Next (First (Exprs));
368          Formal_Typ : constant Entity_Id :=
369                         Etype (Next_Formal (First_Formal (Pname)));
370
371       begin
372          --  We have to worry about the type of the second argument
373
374          --  For the class-wide dispatching cases, and for cases in which
375          --  the base type of the second argument matches the base type of
376          --  the corresponding formal parameter, we are all set, and can use
377          --  the argument unchanged.
378
379          --  For all other cases we do an unchecked conversion of the second
380          --  parameter to the type of the formal of the procedure we are
381          --  calling. This deals with the private type cases, and with going
382          --  to the root type as required in elementary type case.
383
384          if not Is_Class_Wide_Type (Entity (Pref))
385            and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
386          then
387             Rewrite (Item,
388               Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
389
390             --  For untagged derived types set Assignment_OK, to prevent
391             --  copies from being created when the unchecked conversion
392             --  is expanded (which would happen in Remove_Side_Effects
393             --  if Expand_N_Unchecked_Conversion were allowed to call
394             --  Force_Evaluation). The copy could violate Ada semantics
395             --  in cases such as an actual that is an out parameter.
396             --  Note that this approach is also used in exp_ch7 for calls
397             --  to controlled type operations to prevent problems with
398             --  actuals wrapped in unchecked conversions.
399
400             if Is_Untagged_Derivation (Etype (Expression (Item))) then
401                Set_Assignment_OK (Item);
402             end if;
403          end if;
404
405          --  And now rewrite the call
406
407          Rewrite (N,
408            Make_Procedure_Call_Statement (Loc,
409              Name => New_Occurrence_Of (Pname, Loc),
410              Parameter_Associations => Exprs));
411
412          Analyze (N);
413       end Rewrite_Stream_Proc_Call;
414
415    --  Start of processing for Expand_N_Attribute_Reference
416
417    begin
418       --  Do required validity checking
419
420       if Validity_Checks_On and Validity_Check_Operands then
421          declare
422             Expr : Node_Id;
423
424          begin
425             Expr := First (Expressions (N));
426             while Present (Expr) loop
427                Ensure_Valid (Expr);
428                Next (Expr);
429             end loop;
430          end;
431       end if;
432
433       --  Remaining processing depends on specific attribute
434
435       case Id is
436
437       ------------
438       -- Access --
439       ------------
440
441       when Attribute_Access =>
442
443          if Ekind (Btyp) = E_Access_Protected_Subprogram_Type then
444
445             --  The value of the attribute_reference is a record containing
446             --  two fields: an access to the protected object, and an access
447             --  to the subprogram itself. The prefix is a selected component.
448
449             declare
450                Agg     : Node_Id;
451                Sub     : Entity_Id;
452                E_T     : constant Entity_Id := Equivalent_Type (Btyp);
453                Acc     : constant Entity_Id :=
454                            Etype (Next_Component (First_Component (E_T)));
455                Obj_Ref : Node_Id;
456                Curr    : Entity_Id;
457
458             begin
459                --  Within the body of the protected type, the prefix
460                --  designates a local operation, and the object is the first
461                --  parameter of the corresponding protected body of the
462                --  current enclosing operation.
463
464                if Is_Entity_Name (Pref) then
465                   pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
466                   Sub :=
467                     New_Occurrence_Of
468                       (Protected_Body_Subprogram (Entity (Pref)), Loc);
469                   Curr := Current_Scope;
470
471                   while Scope (Curr) /= Scope (Entity (Pref)) loop
472                      Curr := Scope (Curr);
473                   end loop;
474
475                   Obj_Ref :=
476                     Make_Attribute_Reference (Loc,
477                       Prefix =>
478                          New_Occurrence_Of
479                            (First_Formal
480                               (Protected_Body_Subprogram (Curr)), Loc),
481                       Attribute_Name => Name_Address);
482
483                --  Case where the prefix is not an entity name. Find the
484                --  version of the protected operation to be called from
485                --  outside the protected object.
486
487                else
488                   Sub :=
489                     New_Occurrence_Of
490                       (External_Subprogram
491                         (Entity (Selector_Name (Pref))), Loc);
492
493                   Obj_Ref :=
494                     Make_Attribute_Reference (Loc,
495                       Prefix => Relocate_Node (Prefix (Pref)),
496                         Attribute_Name => Name_Address);
497                end if;
498
499                Agg :=
500                  Make_Aggregate (Loc,
501                    Expressions =>
502                      New_List (
503                        Obj_Ref,
504                        Unchecked_Convert_To (Acc,
505                          Make_Attribute_Reference (Loc,
506                            Prefix => Sub,
507                            Attribute_Name => Name_Address))));
508
509                Rewrite (N, Agg);
510
511                Analyze_And_Resolve (N, E_T);
512
513                --  For subsequent analysis,  the node must retain its type.
514                --  The backend will replace it with the equivalent type where
515                --  needed.
516
517                Set_Etype (N, Typ);
518             end;
519
520          elsif Ekind (Btyp) = E_General_Access_Type then
521             declare
522                Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
523                Parm_Ent   : Entity_Id;
524                Conversion : Node_Id;
525
526             begin
527                --  If the prefix of an Access attribute is a dereference of an
528                --  access parameter (or a renaming of such a dereference) and
529                --  the context is a general access type (but not an anonymous
530                --  access type), then rewrite the attribute as a conversion of
531                --  the access parameter to the context access type.  This will
532                --  result in an accessibility check being performed, if needed.
533
534                --    (X.all'Access => Acc_Type (X))
535
536                if Nkind (Ref_Object) = N_Explicit_Dereference
537                  and then Is_Entity_Name (Prefix (Ref_Object))
538                then
539                   Parm_Ent := Entity (Prefix (Ref_Object));
540
541                   if Ekind (Parm_Ent) in Formal_Kind
542                     and then Ekind (Etype (Parm_Ent)) = E_Anonymous_Access_Type
543                     and then Present (Extra_Accessibility (Parm_Ent))
544                   then
545                      Conversion :=
546                         Convert_To (Typ, New_Copy_Tree (Prefix (Ref_Object)));
547
548                      Rewrite (N, Conversion);
549                      Analyze_And_Resolve (N, Typ);
550                   end if;
551                end if;
552             end;
553
554          --  If the prefix is a type name, this is a reference to the current
555          --  instance of the type, within its initialization procedure.
556
557          else
558             Expand_Access_To_Type (N);
559          end if;
560
561       --------------
562       -- Adjacent --
563       --------------
564
565       --  Transforms 'Adjacent into a call to the floating-point attribute
566       --  function Adjacent in Fat_xxx (where xxx is the root type)
567
568       when Attribute_Adjacent =>
569          Expand_Fpt_Attribute_RR (N);
570
571       -------------
572       -- Address --
573       -------------
574
575       when Attribute_Address => Address : declare
576          Task_Proc : Entity_Id;
577
578       begin
579          --  If the prefix is a task or a task type, the useful address
580          --  is that of the procedure for the task body, i.e. the actual
581          --  program unit. We replace the original entity with that of
582          --  the procedure.
583
584          if Is_Entity_Name (Pref)
585            and then Is_Task_Type (Entity (Pref))
586          then
587             Task_Proc := Next_Entity (Root_Type (Etype (Pref)));
588
589             while Present (Task_Proc) loop
590                exit when Ekind (Task_Proc) = E_Procedure
591                  and then Etype (First_Formal (Task_Proc)) =
592                                   Corresponding_Record_Type (Etype (Pref));
593                Next_Entity (Task_Proc);
594             end loop;
595
596             if Present (Task_Proc) then
597                Set_Entity (Pref, Task_Proc);
598                Set_Etype  (Pref, Etype (Task_Proc));
599             end if;
600
601          --  Similarly, the address of a protected operation is the address
602          --  of the corresponding protected body, regardless of the protected
603          --  object from which it is selected.
604
605          elsif Nkind (Pref) = N_Selected_Component
606            and then Is_Subprogram (Entity (Selector_Name (Pref)))
607            and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
608          then
609             Rewrite (Pref,
610               New_Occurrence_Of (
611                 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
612
613          elsif Nkind (Pref) = N_Explicit_Dereference
614            and then Ekind (Etype (Pref)) = E_Subprogram_Type
615            and then Convention (Etype (Pref)) = Convention_Protected
616          then
617             --  The prefix is be a dereference of an access_to_protected_
618             --  subprogram. The desired address is the second component of
619             --  the record that represents the access.
620
621             declare
622                Addr : constant Entity_Id := Etype (N);
623                Ptr  : constant Node_Id   := Prefix (Pref);
624                T    : constant Entity_Id :=
625                         Equivalent_Type (Base_Type (Etype (Ptr)));
626
627             begin
628                Rewrite (N,
629                  Unchecked_Convert_To (Addr,
630                    Make_Selected_Component (Loc,
631                      Prefix => Unchecked_Convert_To (T, Ptr),
632                      Selector_Name => New_Occurrence_Of (
633                        Next_Entity (First_Entity (T)), Loc))));
634
635                Analyze_And_Resolve (N, Addr);
636             end;
637          end if;
638
639          --  Deal with packed array reference, other cases are handled by gigi
640
641          if Involves_Packed_Array_Reference (Pref) then
642             Expand_Packed_Address_Reference (N);
643          end if;
644       end Address;
645
646       ---------------
647       -- AST_Entry --
648       ---------------
649
650       when Attribute_AST_Entry => AST_Entry : declare
651          Ttyp : Entity_Id;
652          T_Id : Node_Id;
653          Eent : Entity_Id;
654
655          Entry_Ref : Node_Id;
656          --  The reference to the entry or entry family
657
658          Index : Node_Id;
659          --  The index expression for an entry family reference, or
660          --  the Empty if Entry_Ref references a simple entry.
661
662       begin
663          if Nkind (Pref) = N_Indexed_Component then
664             Entry_Ref := Prefix (Pref);
665             Index := First (Expressions (Pref));
666          else
667             Entry_Ref := Pref;
668             Index := Empty;
669          end if;
670
671          --  Get expression for Task_Id and the entry entity
672
673          if Nkind (Entry_Ref) = N_Selected_Component then
674             T_Id :=
675               Make_Attribute_Reference (Loc,
676                 Attribute_Name => Name_Identity,
677                 Prefix         => Prefix (Entry_Ref));
678
679             Ttyp := Etype (Prefix (Entry_Ref));
680             Eent := Entity (Selector_Name (Entry_Ref));
681
682          else
683             T_Id :=
684               Make_Function_Call (Loc,
685                 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
686
687             Eent  := Entity (Entry_Ref);
688
689             --  We have to find the enclosing task to get the task type
690             --  There must be one, since we already validated this earlier
691
692             Ttyp := Current_Scope;
693             while not Is_Task_Type (Ttyp) loop
694                Ttyp := Scope (Ttyp);
695             end loop;
696          end if;
697
698          --  Now rewrite the attribute with a call to Create_AST_Handler
699
700          Rewrite (N,
701            Make_Function_Call (Loc,
702              Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
703              Parameter_Associations => New_List (
704                T_Id,
705                Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
706
707          Analyze_And_Resolve (N, RTE (RE_AST_Handler));
708       end AST_Entry;
709
710       ------------------
711       -- Bit_Position --
712       ------------------
713
714       --  We compute this if a component clause was present, otherwise
715       --  we leave the computation up to Gigi, since we don't know what
716       --  layout will be chosen.
717
718       --  Note that the attribute can apply to a naked record component
719       --  in generated code (i.e. the prefix is an identifier that
720       --  references the component or discriminant entity).
721
722       when Attribute_Bit_Position => Bit_Position :
723       declare
724          CE : Entity_Id;
725
726       begin
727          if Nkind (Pref) = N_Identifier then
728             CE := Entity (Pref);
729          else
730             CE := Entity (Selector_Name (Pref));
731          end if;
732
733          if Known_Static_Component_Bit_Offset (CE) then
734             Rewrite (N,
735               Make_Integer_Literal (Loc,
736                 Intval => Component_Bit_Offset (CE)));
737             Analyze_And_Resolve (N, Typ);
738
739          else
740             Apply_Universal_Integer_Attribute_Checks (N);
741          end if;
742       end Bit_Position;
743
744       ------------------
745       -- Body_Version --
746       ------------------
747
748       --  A reference to P'Body_Version or P'Version is expanded to
749
750       --     Vnn : Unsigned;
751       --     pragma Import (C, Vnn, "uuuuT";
752       --     ...
753       --     Get_Version_String (Vnn)
754
755       --  where uuuu is the unit name (dots replaced by double underscore)
756       --  and T is B for the cases of Body_Version, or Version applied to a
757       --  subprogram acting as its own spec, and S for Version applied to a
758       --  subprogram spec or package. This sequence of code references the
759       --  the unsigned constant created in the main program by the binder.
760
761       --  A special exception occurs for Standard, where the string
762       --  returned is a copy of the library  string in gnatvsn.ads.
763
764       when Attribute_Body_Version | Attribute_Version => Version : declare
765          E    : constant Entity_Id :=
766                   Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
767          Pent : Entity_Id := Entity (Pref);
768          S    : String_Id;
769
770       begin
771          --  If not library unit, get to containing library unit
772
773          while Pent /= Standard_Standard
774            and then Scope (Pent) /= Standard_Standard
775          loop
776             Pent := Scope (Pent);
777          end loop;
778
779          --  Special case Standard
780
781          if Pent = Standard_Standard
782            or else Pent = Standard_ASCII
783          then
784             Name_Buffer (1 .. Library_Version'Length) := Library_Version;
785             Name_Len := Library_Version'Length;
786             Rewrite (N,
787               Make_String_Literal (Loc,
788                 Strval => String_From_Name_Buffer));
789
790          --  All other cases
791
792          else
793             --  Build required string constant
794
795             Get_Name_String (Get_Unit_Name (Pent));
796
797             Start_String;
798             for J in 1 .. Name_Len - 2 loop
799                if Name_Buffer (J) = '.' then
800                   Store_String_Chars ("__");
801                else
802                   Store_String_Char (Get_Char_Code (Name_Buffer (J)));
803                end if;
804             end loop;
805
806             --  Case of subprogram acting as its own spec, always use body
807
808             if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
809               and then Nkind (Parent (Declaration_Node (Pent))) =
810                                                           N_Subprogram_Body
811               and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
812             then
813                Store_String_Chars ("B");
814
815             --  Case of no body present, always use spec
816
817             elsif not Unit_Requires_Body (Pent) then
818                Store_String_Chars ("S");
819
820             --  Otherwise use B for Body_Version, S for spec
821
822             elsif Id = Attribute_Body_Version then
823                Store_String_Chars ("B");
824             else
825                Store_String_Chars ("S");
826             end if;
827
828             S := End_String;
829             Lib.Version_Referenced (S);
830
831             --  Insert the object declaration
832
833             Insert_Actions (N, New_List (
834               Make_Object_Declaration (Loc,
835                 Defining_Identifier => E,
836                 Object_Definition   =>
837                   New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
838
839             --  Set entity as imported with correct external name
840
841             Set_Is_Imported (E);
842             Set_Interface_Name (E, Make_String_Literal (Loc, S));
843
844             --  And now rewrite original reference
845
846             Rewrite (N,
847               Make_Function_Call (Loc,
848                 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
849                 Parameter_Associations => New_List (
850                   New_Occurrence_Of (E, Loc))));
851          end if;
852
853          Analyze_And_Resolve (N, RTE (RE_Version_String));
854       end Version;
855
856       -------------
857       -- Ceiling --
858       -------------
859
860       --  Transforms 'Ceiling into a call to the floating-point attribute
861       --  function Ceiling in Fat_xxx (where xxx is the root type)
862
863       when Attribute_Ceiling =>
864          Expand_Fpt_Attribute_R (N);
865
866       --------------
867       -- Callable --
868       --------------
869
870       --  Transforms 'Callable attribute into a call to the Callable function.
871
872       when Attribute_Callable => Callable :
873       begin
874          Rewrite (N,
875            Build_Call_With_Task (Pref, RTE (RE_Callable)));
876          Analyze_And_Resolve (N, Standard_Boolean);
877       end Callable;
878
879       ------------
880       -- Caller --
881       ------------
882
883       --  Transforms 'Caller attribute into a call to either the
884       --  Task_Entry_Caller or the Protected_Entry_Caller function.
885
886       when Attribute_Caller => Caller : declare
887          Id_Kind    : Entity_Id := RTE (RO_AT_Task_ID);
888          Ent        : Entity_Id := Entity (Pref);
889          Conctype   : Entity_Id := Scope (Ent);
890          Nest_Depth : Integer   := 0;
891          Name       : Node_Id;
892          S          : Entity_Id;
893
894       begin
895          --  Protected case
896
897          if Is_Protected_Type (Conctype) then
898             if Abort_Allowed
899               or else Restrictions (No_Entry_Queue) = False
900               or else Number_Entries (Conctype) > 1
901             then
902                Name :=
903                  New_Reference_To
904                    (RTE (RE_Protected_Entry_Caller), Loc);
905             else
906                Name :=
907                  New_Reference_To
908                    (RTE (RE_Protected_Single_Entry_Caller), Loc);
909             end if;
910
911             Rewrite (N,
912               Unchecked_Convert_To (Id_Kind,
913                 Make_Function_Call (Loc,
914                   Name => Name,
915                   Parameter_Associations => New_List
916                     (New_Reference_To (
917                       Object_Ref
918                         (Corresponding_Body (Parent (Conctype))), Loc)))));
919
920          --  Task case
921
922          else
923             --  Determine the nesting depth of the E'Caller attribute, that
924             --  is, how many accept statements are nested within the accept
925             --  statement for E at the point of E'Caller. The runtime uses
926             --  this depth to find the specified entry call.
927
928             for J in reverse 0 .. Scope_Stack.Last loop
929                S := Scope_Stack.Table (J).Entity;
930
931                --  We should not reach the scope of the entry, as it should
932                --  already have been checked in Sem_Attr that this attribute
933                --  reference is within a matching accept statement.
934
935                pragma Assert (S /= Conctype);
936
937                if S = Ent then
938                   exit;
939
940                elsif Is_Entry (S) then
941                   Nest_Depth := Nest_Depth + 1;
942                end if;
943             end loop;
944
945             Rewrite (N,
946               Unchecked_Convert_To (Id_Kind,
947                 Make_Function_Call (Loc,
948                   Name => New_Reference_To (
949                     RTE (RE_Task_Entry_Caller), Loc),
950                   Parameter_Associations => New_List (
951                     Make_Integer_Literal (Loc,
952                       Intval => Int (Nest_Depth))))));
953          end if;
954
955          Analyze_And_Resolve (N, Id_Kind);
956       end Caller;
957
958       -------------
959       -- Compose --
960       -------------
961
962       --  Transforms 'Compose into a call to the floating-point attribute
963       --  function Compose in Fat_xxx (where xxx is the root type)
964
965       --  Note: we strictly should have special code here to deal with the
966       --  case of absurdly negative arguments (less than Integer'First)
967       --  which will return a (signed) zero value, but it hardly seems
968       --  worth the effort. Absurdly large positive arguments will raise
969       --  constraint error which is fine.
970
971       when Attribute_Compose =>
972          Expand_Fpt_Attribute_RI (N);
973
974       -----------------
975       -- Constrained --
976       -----------------
977
978       when Attribute_Constrained => Constrained : declare
979          Formal_Ent : constant Entity_Id := Param_Entity (Pref);
980
981       begin
982          --  Reference to a parameter where the value is passed as an extra
983          --  actual, corresponding to the extra formal referenced by the
984          --  Extra_Constrained field of the corresponding formal.
985
986          if Present (Formal_Ent)
987            and then Present (Extra_Constrained (Formal_Ent))
988          then
989             Rewrite (N,
990               New_Occurrence_Of
991                 (Extra_Constrained (Formal_Ent), Sloc (N)));
992
993          --  For variables with a Extra_Constrained field, we use the
994          --  corresponding entity.
995
996          elsif Nkind (Pref) = N_Identifier
997            and then Ekind (Entity (Pref)) = E_Variable
998            and then Present (Extra_Constrained (Entity (Pref)))
999          then
1000             Rewrite (N,
1001               New_Occurrence_Of
1002                 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1003
1004          --  For all other entity names, we can tell at compile time
1005
1006          elsif Is_Entity_Name (Pref) then
1007             declare
1008                Ent : constant Entity_Id   := Entity (Pref);
1009                Res : Boolean;
1010
1011             begin
1012                --  (RM J.4) obsolescent cases
1013
1014                if Is_Type (Ent) then
1015
1016                   --  Private type
1017
1018                   if Is_Private_Type (Ent) then
1019                      Res := not Has_Discriminants (Ent)
1020                               or else Is_Constrained (Ent);
1021
1022                   --  It not a private type, must be a generic actual type
1023                   --  that corresponded to a private type. We know that this
1024                   --  correspondence holds, since otherwise the reference
1025                   --  within the generic template would have been illegal.
1026
1027                   else
1028                      declare
1029                         UT : Entity_Id := Underlying_Type (Ent);
1030
1031                      begin
1032                         if Is_Composite_Type (UT) then
1033                            Res := Is_Constrained (Ent);
1034                         else
1035                            Res := True;
1036                         end if;
1037                      end;
1038                   end if;
1039
1040                --  If the prefix is not a variable or is aliased, then
1041                --  definitely true; if it's a formal parameter without
1042                --  an associated extra formal, then treat it as constrained.
1043
1044                elsif not Is_Variable (Pref)
1045                  or else Present (Formal_Ent)
1046                  or else Is_Aliased_View (Pref)
1047                then
1048                   Res := True;
1049
1050                --  Variable case, just look at type to see if it is
1051                --  constrained. Note that the one case where this is
1052                --  not accurate (the procedure formal case), has been
1053                --  handled above.
1054
1055                else
1056                   Res := Is_Constrained (Etype (Ent));
1057                end if;
1058
1059                if Res then
1060                   Rewrite (N,
1061                     New_Reference_To (Standard_True, Loc));
1062                else
1063                   Rewrite (N,
1064                     New_Reference_To (Standard_False, Loc));
1065                end if;
1066             end;
1067
1068          --  Prefix is not an entity name. These are also cases where
1069          --  we can always tell at compile time by looking at the form
1070          --  and type of the prefix.
1071
1072          else
1073             if not Is_Variable (Pref)
1074               or else Nkind (Pref) = N_Explicit_Dereference
1075               or else Is_Constrained (Etype (Pref))
1076             then
1077                Rewrite (N,
1078                  New_Reference_To (Standard_True, Loc));
1079             else
1080                Rewrite (N,
1081                  New_Reference_To (Standard_False, Loc));
1082             end if;
1083          end if;
1084
1085          Analyze_And_Resolve (N, Standard_Boolean);
1086       end Constrained;
1087
1088       ---------------
1089       -- Copy_Sign --
1090       ---------------
1091
1092       --  Transforms 'Copy_Sign into a call to the floating-point attribute
1093       --  function Copy_Sign in Fat_xxx (where xxx is the root type)
1094
1095       when Attribute_Copy_Sign =>
1096          Expand_Fpt_Attribute_RR (N);
1097
1098       -----------
1099       -- Count --
1100       -----------
1101
1102       --  Transforms 'Count attribute into a call to the Count function
1103
1104       when Attribute_Count => Count :
1105       declare
1106          Entnam  : Node_Id;
1107          Index   : Node_Id;
1108          Name    : Node_Id;
1109          Call    : Node_Id;
1110          Conctyp : Entity_Id;
1111
1112       begin
1113          --  If the prefix is a member of an entry family, retrieve both
1114          --  entry name and index. For a simple entry there is no index.
1115
1116          if Nkind (Pref) = N_Indexed_Component then
1117             Entnam := Prefix (Pref);
1118             Index := First (Expressions (Pref));
1119          else
1120             Entnam := Pref;
1121             Index := Empty;
1122          end if;
1123
1124          --  Find the concurrent type in which this attribute is referenced
1125          --  (there had better be one).
1126
1127          Conctyp := Current_Scope;
1128          while not Is_Concurrent_Type (Conctyp) loop
1129             Conctyp := Scope (Conctyp);
1130          end loop;
1131
1132          --  Protected case
1133
1134          if Is_Protected_Type (Conctyp) then
1135
1136             if Abort_Allowed
1137               or else Restrictions (No_Entry_Queue) = False
1138               or else Number_Entries (Conctyp) > 1
1139             then
1140                Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1141
1142                Call :=
1143                  Make_Function_Call (Loc,
1144                    Name => Name,
1145                    Parameter_Associations => New_List (
1146                      New_Reference_To (
1147                        Object_Ref (
1148                          Corresponding_Body (Parent (Conctyp))), Loc),
1149                      Entry_Index_Expression (
1150                        Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1151             else
1152                Name := New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1153
1154                Call := Make_Function_Call (Loc,
1155                    Name => Name,
1156                    Parameter_Associations => New_List (
1157                      New_Reference_To (
1158                        Object_Ref (
1159                          Corresponding_Body (Parent (Conctyp))), Loc)));
1160             end if;
1161
1162          --  Task case
1163
1164          else
1165             Call :=
1166               Make_Function_Call (Loc,
1167                 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1168                 Parameter_Associations => New_List (
1169                   Entry_Index_Expression
1170                     (Loc, Entity (Entnam), Index, Scope (Entity (Entnam)))));
1171          end if;
1172
1173          --  The call returns type Natural but the context is universal integer
1174          --  so any integer type is allowed. The attribute was already resolved
1175          --  so its Etype is the required result type. If the base type of the
1176          --  context type is other than Standard.Integer we put in a conversion
1177          --  to the required type. This can be a normal typed conversion since
1178          --  both input and output types of the conversion are integer types
1179
1180          if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1181             Rewrite (N, Convert_To (Typ, Call));
1182          else
1183             Rewrite (N, Call);
1184          end if;
1185
1186          Analyze_And_Resolve (N, Typ);
1187       end Count;
1188
1189       ---------------
1190       -- Elab_Body --
1191       ---------------
1192
1193       --  This processing is shared by Elab_Spec
1194
1195       --  What we do is to insert the following declarations
1196
1197       --     procedure tnn;
1198       --     pragma Import (C, enn, "name___elabb/s");
1199
1200       --  and then the Elab_Body/Spec attribute is replaced by a reference
1201       --  to this defining identifier.
1202
1203       when Attribute_Elab_Body |
1204            Attribute_Elab_Spec =>
1205
1206          Elab_Body : declare
1207             Ent  : constant Entity_Id :=
1208                      Make_Defining_Identifier (Loc,
1209                        New_Internal_Name ('E'));
1210             Str  : String_Id;
1211             Lang : Node_Id;
1212
1213             procedure Make_Elab_String (Nod : Node_Id);
1214             --  Given Nod, an identifier, or a selected component, put the
1215             --  image into the current string literal, with double underline
1216             --  between components.
1217
1218             procedure Make_Elab_String (Nod : Node_Id) is
1219             begin
1220                if Nkind (Nod) = N_Selected_Component then
1221                   Make_Elab_String (Prefix (Nod));
1222                   if Java_VM then
1223                      Store_String_Char ('$');
1224                   else
1225                      Store_String_Char ('_');
1226                      Store_String_Char ('_');
1227                   end if;
1228
1229                   Get_Name_String (Chars (Selector_Name (Nod)));
1230
1231                else
1232                   pragma Assert (Nkind (Nod) = N_Identifier);
1233                   Get_Name_String (Chars (Nod));
1234                end if;
1235
1236                Store_String_Chars (Name_Buffer (1 .. Name_Len));
1237             end Make_Elab_String;
1238
1239          --  Start of processing for Elab_Body/Elab_Spec
1240
1241          begin
1242             --  First we need to prepare the string literal for the name of
1243             --  the elaboration routine to be referenced.
1244
1245             Start_String;
1246             Make_Elab_String (Pref);
1247
1248             if Java_VM then
1249                Store_String_Chars ("._elab");
1250                Lang := Make_Identifier (Loc, Name_Ada);
1251             else
1252                Store_String_Chars ("___elab");
1253                Lang := Make_Identifier (Loc, Name_C);
1254             end if;
1255
1256             if Id = Attribute_Elab_Body then
1257                Store_String_Char ('b');
1258             else
1259                Store_String_Char ('s');
1260             end if;
1261
1262             Str := End_String;
1263
1264             Insert_Actions (N, New_List (
1265               Make_Subprogram_Declaration (Loc,
1266                 Specification =>
1267                   Make_Procedure_Specification (Loc,
1268                     Defining_Unit_Name => Ent)),
1269
1270               Make_Pragma (Loc,
1271                 Chars => Name_Import,
1272                 Pragma_Argument_Associations => New_List (
1273                   Make_Pragma_Argument_Association (Loc,
1274                     Expression => Lang),
1275
1276                   Make_Pragma_Argument_Association (Loc,
1277                     Expression =>
1278                       Make_Identifier (Loc, Chars (Ent))),
1279
1280                   Make_Pragma_Argument_Association (Loc,
1281                     Expression =>
1282                       Make_String_Literal (Loc, Str))))));
1283
1284             Set_Entity (N, Ent);
1285             Rewrite (N, New_Occurrence_Of (Ent, Loc));
1286          end Elab_Body;
1287
1288       ----------------
1289       -- Elaborated --
1290       ----------------
1291
1292       --  Elaborated is always True for preelaborated units, predefined
1293       --  units, pure units and units which have Elaborate_Body pragmas.
1294       --  These units have no elaboration entity.
1295
1296       --  Note: The Elaborated attribute is never passed through to Gigi
1297
1298       when Attribute_Elaborated => Elaborated : declare
1299          Ent : constant Entity_Id := Entity (Pref);
1300
1301       begin
1302          if Present (Elaboration_Entity (Ent)) then
1303             Rewrite (N,
1304               New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1305          else
1306             Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1307          end if;
1308       end Elaborated;
1309
1310       --------------
1311       -- Enum_Rep --
1312       --------------
1313
1314       when Attribute_Enum_Rep => Enum_Rep :
1315       begin
1316          --  X'Enum_Rep (Y) expands to
1317
1318          --    target-type (Y)
1319
1320          --  This is simply a direct conversion from the enumeration type
1321          --  to the target integer type, which is treated by Gigi as a normal
1322          --  integer conversion, treating the enumeration type as an integer,
1323          --  which is exactly what we want! We set Conversion_OK to make sure
1324          --  that the analyzer does not complain about what otherwise might
1325          --  be an illegal conversion.
1326
1327          if Is_Non_Empty_List (Exprs) then
1328             Rewrite (N,
1329               OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1330
1331          --  X'Enum_Rep where X is an enumeration literal is replaced by
1332          --  the literal value.
1333
1334          elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1335             Rewrite (N,
1336               Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1337
1338          --  X'Enum_Rep where X is an object does a direct unchecked conversion
1339          --  of the object value, as described for the type case above.
1340
1341          else
1342             Rewrite (N,
1343               OK_Convert_To (Typ, Relocate_Node (Pref)));
1344          end if;
1345
1346          Set_Etype (N, Typ);
1347          Analyze_And_Resolve (N, Typ);
1348
1349       end Enum_Rep;
1350
1351       --------------
1352       -- Exponent --
1353       --------------
1354
1355       --  Transforms 'Exponent into a call to the floating-point attribute
1356       --  function Exponent in Fat_xxx (where xxx is the root type)
1357
1358       when Attribute_Exponent =>
1359          Expand_Fpt_Attribute_R (N);
1360
1361       ------------------
1362       -- External_Tag --
1363       ------------------
1364
1365       --  transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1366
1367       when Attribute_External_Tag => External_Tag :
1368       begin
1369          Rewrite (N,
1370            Make_Function_Call (Loc,
1371              Name => New_Reference_To (RTE (RE_External_Tag), Loc),
1372              Parameter_Associations => New_List (
1373                Make_Attribute_Reference (Loc,
1374                  Attribute_Name => Name_Tag,
1375                  Prefix => Prefix (N)))));
1376
1377          Analyze_And_Resolve (N, Standard_String);
1378       end External_Tag;
1379
1380       -----------
1381       -- First --
1382       -----------
1383
1384       when Attribute_First => declare
1385          Ptyp : constant Entity_Id := Etype (Pref);
1386
1387       begin
1388          --  If the prefix type is a constrained packed array type which
1389          --  already has a Packed_Array_Type representation defined, then
1390          --  replace this attribute with a direct reference to 'First of the
1391          --  appropriate index subtype (since otherwise Gigi will try to give
1392          --  us the value of 'First for this implementation type).
1393
1394          if Is_Constrained_Packed_Array (Ptyp) then
1395             Rewrite (N,
1396               Make_Attribute_Reference (Loc,
1397                 Attribute_Name => Name_First,
1398                 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1399             Analyze_And_Resolve (N, Typ);
1400
1401          elsif Is_Access_Type (Ptyp) then
1402             Apply_Access_Check (N);
1403          end if;
1404       end;
1405
1406       ---------------
1407       -- First_Bit --
1408       ---------------
1409
1410       --  We compute this if a component clause was present, otherwise
1411       --  we leave the computation up to Gigi, since we don't know what
1412       --  layout will be chosen.
1413
1414       when Attribute_First_Bit => First_Bit :
1415       declare
1416          CE : constant Entity_Id := Entity (Selector_Name (Pref));
1417
1418       begin
1419          if Known_Static_Component_Bit_Offset (CE) then
1420             Rewrite (N,
1421               Make_Integer_Literal (Loc,
1422                 Component_Bit_Offset (CE) mod System_Storage_Unit));
1423
1424             Analyze_And_Resolve (N, Typ);
1425
1426          else
1427             Apply_Universal_Integer_Attribute_Checks (N);
1428          end if;
1429       end First_Bit;
1430
1431       -----------------
1432       -- Fixed_Value --
1433       -----------------
1434
1435       --  We transform:
1436
1437       --     fixtype'Fixed_Value (integer-value)
1438
1439       --  into
1440
1441       --     fixtype(integer-value)
1442
1443       --  we do all the required analysis of the conversion here, because
1444       --  we do not want this to go through the fixed-point conversion
1445       --  circuits. Note that gigi always treats fixed-point as equivalent
1446       --  to the corresponding integer type anyway.
1447
1448       when Attribute_Fixed_Value => Fixed_Value :
1449       begin
1450          Rewrite (N,
1451            Make_Type_Conversion (Loc,
1452              Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1453              Expression   => Relocate_Node (First (Exprs))));
1454          Set_Etype (N, Entity (Pref));
1455          Set_Analyzed (N);
1456          Apply_Type_Conversion_Checks (N);
1457       end Fixed_Value;
1458
1459       -----------
1460       -- Floor --
1461       -----------
1462
1463       --  Transforms 'Floor into a call to the floating-point attribute
1464       --  function Floor in Fat_xxx (where xxx is the root type)
1465
1466       when Attribute_Floor =>
1467          Expand_Fpt_Attribute_R (N);
1468
1469       ----------
1470       -- Fore --
1471       ----------
1472
1473       --  For the fixed-point type Typ:
1474
1475       --    Typ'Fore
1476
1477       --  expands into
1478
1479       --    Result_Type (System.Fore (Long_Long_Float (Type'First)),
1480       --                              Long_Long_Float (Type'Last))
1481
1482       --  Note that we know that the type is a non-static subtype, or Fore
1483       --  would have itself been computed dynamically in Eval_Attribute.
1484
1485       when Attribute_Fore => Fore :
1486       declare
1487          Ptyp : constant Entity_Id := Etype (Pref);
1488
1489       begin
1490          Rewrite (N,
1491            Convert_To (Typ,
1492              Make_Function_Call (Loc,
1493                Name => New_Reference_To (RTE (RE_Fore), Loc),
1494
1495                Parameter_Associations => New_List (
1496                  Convert_To (Standard_Long_Long_Float,
1497                    Make_Attribute_Reference (Loc,
1498                      Prefix => New_Reference_To (Ptyp, Loc),
1499                      Attribute_Name => Name_First)),
1500
1501                  Convert_To (Standard_Long_Long_Float,
1502                    Make_Attribute_Reference (Loc,
1503                      Prefix => New_Reference_To (Ptyp, Loc),
1504                      Attribute_Name => Name_Last))))));
1505
1506          Analyze_And_Resolve (N, Typ);
1507       end Fore;
1508
1509       --------------
1510       -- Fraction --
1511       --------------
1512
1513       --  Transforms 'Fraction into a call to the floating-point attribute
1514       --  function Fraction in Fat_xxx (where xxx is the root type)
1515
1516       when Attribute_Fraction =>
1517          Expand_Fpt_Attribute_R (N);
1518
1519       --------------
1520       -- Identity --
1521       --------------
1522
1523       --  For an exception returns a reference to the exception data:
1524       --      Exception_Id!(Prefix'Reference)
1525
1526       --  For a task it returns a reference to the _task_id component of
1527       --  corresponding record:
1528
1529       --    taskV!(Prefix)._Task_Id, converted to the type Task_ID defined
1530
1531       --  in Ada.Task_Identification.
1532
1533       when Attribute_Identity => Identity : declare
1534          Id_Kind : Entity_Id;
1535
1536       begin
1537          if Etype (Pref) = Standard_Exception_Type then
1538             Id_Kind := RTE (RE_Exception_Id);
1539
1540             if Present (Renamed_Object (Entity (Pref))) then
1541                Set_Entity (Pref, Renamed_Object (Entity (Pref)));
1542             end if;
1543
1544             Rewrite (N,
1545               Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
1546          else
1547             Id_Kind := RTE (RO_AT_Task_ID);
1548
1549             Rewrite (N,
1550               Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
1551          end if;
1552
1553          Analyze_And_Resolve (N, Id_Kind);
1554       end Identity;
1555
1556       -----------
1557       -- Image --
1558       -----------
1559
1560       --  Image attribute is handled in separate unit Exp_Imgv
1561
1562       when Attribute_Image =>
1563          Exp_Imgv.Expand_Image_Attribute (N);
1564
1565       ---------
1566       -- Img --
1567       ---------
1568
1569       --  X'Img is expanded to typ'Image (X), where typ is the type of X
1570
1571       when Attribute_Img => Img :
1572       begin
1573          Rewrite (N,
1574            Make_Attribute_Reference (Loc,
1575              Prefix => New_Reference_To (Etype (Pref), Loc),
1576              Attribute_Name => Name_Image,
1577              Expressions => New_List (Relocate_Node (Pref))));
1578
1579          Analyze_And_Resolve (N, Standard_String);
1580       end Img;
1581
1582       -----------
1583       -- Input --
1584       -----------
1585
1586       when Attribute_Input => Input : declare
1587          P_Type : constant Entity_Id := Entity (Pref);
1588          B_Type : constant Entity_Id := Base_Type (P_Type);
1589          U_Type : constant Entity_Id := Underlying_Type (P_Type);
1590          Strm   : constant Node_Id   := First (Exprs);
1591          Fname  : Entity_Id;
1592          Decl   : Node_Id;
1593          Call   : Node_Id;
1594          Prag   : Node_Id;
1595          Arg2   : Node_Id;
1596          Rfunc  : Node_Id;
1597
1598          Cntrl  : Node_Id := Empty;
1599          --  Value for controlling argument in call. Always Empty except in
1600          --  the dispatching (class-wide type) case, where it is a reference
1601          --  to the dummy object initialized to the right internal tag.
1602
1603       begin
1604          --  If no underlying type, we have an error that will be diagnosed
1605          --  elsewhere, so here we just completely ignore the expansion.
1606
1607          if No (U_Type) then
1608             return;
1609          end if;
1610
1611          --  If there is a TSS for Input, just call it
1612
1613          Fname := Find_Inherited_TSS (P_Type, Name_uInput);
1614
1615          if Present (Fname) then
1616             null;
1617
1618          else
1619             --  If there is a Stream_Convert pragma, use it, we rewrite
1620
1621             --     sourcetyp'Input (stream)
1622
1623             --  as
1624
1625             --     sourcetyp (streamread (strmtyp'Input (stream)));
1626
1627             --  where stmrearead is the given Read function that converts
1628             --  an argument of type strmtyp to type sourcetyp or a type
1629             --  from which it is derived. The extra conversion is required
1630             --  for the derived case.
1631
1632             Prag :=
1633               Get_Rep_Pragma
1634                 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
1635
1636             if Present (Prag) then
1637                Arg2  := Next (First (Pragma_Argument_Associations (Prag)));
1638                Rfunc := Entity (Expression (Arg2));
1639
1640                Rewrite (N,
1641                  Convert_To (B_Type,
1642                    Make_Function_Call (Loc,
1643                      Name => New_Occurrence_Of (Rfunc, Loc),
1644                      Parameter_Associations => New_List (
1645                        Make_Attribute_Reference (Loc,
1646                          Prefix =>
1647                            New_Occurrence_Of
1648                              (Etype (First_Formal (Rfunc)), Loc),
1649                          Attribute_Name => Name_Input,
1650                          Expressions => Exprs)))));
1651
1652                Analyze_And_Resolve (N, B_Type);
1653                return;
1654
1655             --  Elementary types
1656
1657             elsif Is_Elementary_Type (U_Type) then
1658
1659                --  A special case arises if we have a defined _Read routine,
1660                --  since in this case we are required to call this routine.
1661
1662                if Present (TSS (B_Type, Name_uRead)) then
1663                   Build_Record_Or_Elementary_Input_Function
1664                     (Loc, U_Type, Decl, Fname);
1665                   Insert_Action (N, Decl);
1666
1667                --  For normal cases, we call the I_xxx routine directly
1668
1669                else
1670                   Rewrite (N, Build_Elementary_Input_Call (N));
1671                   Analyze_And_Resolve (N, P_Type);
1672                   return;
1673                end if;
1674
1675             --  Array type case
1676
1677             elsif Is_Array_Type (U_Type) then
1678                Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
1679                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
1680
1681             --  Dispatching case with class-wide type
1682
1683             elsif Is_Class_Wide_Type (P_Type) then
1684
1685                declare
1686                   Rtyp : constant Entity_Id := Root_Type (P_Type);
1687                   Dnn  : Entity_Id;
1688                   Decl : Node_Id;
1689
1690                begin
1691                   --  Read the internal tag (RM 13.13.2(34)) and use it to
1692                   --  initialize a dummy tag object:
1693
1694                   --    Dnn : Ada.Tags.Tag
1695                   --             := Internal_Tag (String'Input (Strm));
1696
1697                   --  This dummy object is used only to provide a controlling
1698                   --  argument for the eventual _Input call.
1699
1700                   Dnn :=
1701                     Make_Defining_Identifier (Loc,
1702                       Chars => New_Internal_Name ('D'));
1703
1704                   Decl :=
1705                     Make_Object_Declaration (Loc,
1706                       Defining_Identifier => Dnn,
1707                       Object_Definition =>
1708                         New_Occurrence_Of (RTE (RE_Tag), Loc),
1709                       Expression =>
1710                         Make_Function_Call (Loc,
1711                           Name =>
1712                             New_Occurrence_Of (RTE (RE_Internal_Tag), Loc),
1713                           Parameter_Associations => New_List (
1714                             Make_Attribute_Reference (Loc,
1715                               Prefix =>
1716                                 New_Occurrence_Of (Standard_String, Loc),
1717                               Attribute_Name => Name_Input,
1718                               Expressions => New_List (
1719                                 Relocate_Node
1720                                   (Duplicate_Subexpr (Strm)))))));
1721
1722                   Insert_Action (N, Decl);
1723
1724                   --  Now we need to get the entity for the call, and construct
1725                   --  a function call node, where we preset a reference to Dnn
1726                   --  as the controlling argument (doing an unchecked
1727                   --  conversion to the tagged type to make it look like
1728                   --  a real tagged object).
1729
1730                   Fname := Find_Prim_Op (Rtyp, Name_uInput);
1731                   Cntrl := Unchecked_Convert_To (Rtyp,
1732                              New_Occurrence_Of (Dnn, Loc));
1733                   Set_Etype (Cntrl, Rtyp);
1734                   Set_Parent (Cntrl, N);
1735                end;
1736
1737             --  For tagged types, use the primitive Input function
1738
1739             elsif Is_Tagged_Type (U_Type) then
1740                Fname := Find_Prim_Op (U_Type, Name_uInput);
1741
1742             --  All other record type cases, including protected records.
1743             --  The latter only arise for expander generated code for
1744             --  handling shared passive partition access.
1745
1746             else
1747                pragma Assert
1748                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
1749
1750                Build_Record_Or_Elementary_Input_Function
1751                  (Loc, Base_Type (U_Type), Decl, Fname);
1752                Insert_Action (N, Decl);
1753             end if;
1754          end if;
1755
1756          --  If we fall through, Fname is the function to be called. The
1757          --  result is obtained by calling the appropriate function, then
1758          --  converting the result. The conversion does a subtype check.
1759
1760          Call :=
1761            Make_Function_Call (Loc,
1762              Name => New_Occurrence_Of (Fname, Loc),
1763              Parameter_Associations => New_List (
1764                 Relocate_Node (Strm)));
1765
1766          Set_Controlling_Argument (Call, Cntrl);
1767          Rewrite (N, Unchecked_Convert_To (P_Type, Call));
1768          Analyze_And_Resolve (N, P_Type);
1769       end Input;
1770
1771       -------------------
1772       -- Integer_Value --
1773       -------------------
1774
1775       --  We transform
1776
1777       --    inttype'Fixed_Value (fixed-value)
1778
1779       --  into
1780
1781       --    inttype(integer-value))
1782
1783       --  we do all the required analysis of the conversion here, because
1784       --  we do not want this to go through the fixed-point conversion
1785       --  circuits. Note that gigi always treats fixed-point as equivalent
1786       --  to the corresponding integer type anyway.
1787
1788       when Attribute_Integer_Value => Integer_Value :
1789       begin
1790          Rewrite (N,
1791            Make_Type_Conversion (Loc,
1792              Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
1793              Expression   => Relocate_Node (First (Exprs))));
1794          Set_Etype (N, Entity (Pref));
1795          Set_Analyzed (N);
1796          Apply_Type_Conversion_Checks (N);
1797       end Integer_Value;
1798
1799       ----------
1800       -- Last --
1801       ----------
1802
1803       when Attribute_Last => declare
1804          Ptyp : constant Entity_Id := Etype (Pref);
1805
1806       begin
1807          --  If the prefix type is a constrained packed array type which
1808          --  already has a Packed_Array_Type representation defined, then
1809          --  replace this attribute with a direct reference to 'Last of the
1810          --  appropriate index subtype (since otherwise Gigi will try to give
1811          --  us the value of 'Last for this implementation type).
1812
1813          if Is_Constrained_Packed_Array (Ptyp) then
1814             Rewrite (N,
1815               Make_Attribute_Reference (Loc,
1816                 Attribute_Name => Name_Last,
1817                 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
1818             Analyze_And_Resolve (N, Typ);
1819
1820          elsif Is_Access_Type (Ptyp) then
1821             Apply_Access_Check (N);
1822          end if;
1823       end;
1824
1825       --------------
1826       -- Last_Bit --
1827       --------------
1828
1829       --  We compute this if a component clause was present, otherwise
1830       --  we leave the computation up to Gigi, since we don't know what
1831       --  layout will be chosen.
1832
1833       when Attribute_Last_Bit => Last_Bit :
1834       declare
1835          CE : constant Entity_Id := Entity (Selector_Name (Pref));
1836
1837       begin
1838          if Known_Static_Component_Bit_Offset (CE)
1839            and then Known_Static_Esize (CE)
1840          then
1841             Rewrite (N,
1842               Make_Integer_Literal (Loc,
1843                Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
1844                                 + Esize (CE) - 1));
1845
1846             Analyze_And_Resolve (N, Typ);
1847
1848          else
1849             Apply_Universal_Integer_Attribute_Checks (N);
1850          end if;
1851       end Last_Bit;
1852
1853       ------------------
1854       -- Leading_Part --
1855       ------------------
1856
1857       --  Transforms 'Leading_Part into a call to the floating-point attribute
1858       --  function Leading_Part in Fat_xxx (where xxx is the root type)
1859
1860       --  Note: strictly, we should have special case code to deal with
1861       --  absurdly large positive arguments (greater than Integer'Last),
1862       --  which result in returning the first argument unchanged, but it
1863       --  hardly seems worth the effort. We raise constraint error for
1864       --  absurdly negative arguments which is fine.
1865
1866       when Attribute_Leading_Part =>
1867          Expand_Fpt_Attribute_RI (N);
1868
1869       ------------
1870       -- Length --
1871       ------------
1872
1873       when Attribute_Length => declare
1874          Ptyp : constant Entity_Id := Etype (Pref);
1875          Ityp : Entity_Id;
1876          Xnum : Uint;
1877
1878       begin
1879          --  Processing for packed array types
1880
1881          if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
1882             Ityp := Get_Index_Subtype (N);
1883
1884             --  If the index type, Ityp, is an enumeration type with
1885             --  holes, then we calculate X'Length explicitly using
1886
1887             --     Typ'Max
1888             --       (0, Ityp'Pos (X'Last  (N)) -
1889             --           Ityp'Pos (X'First (N)) + 1);
1890
1891             --  Since the bounds in the template are the representation
1892             --  values and gigi would get the wrong value.
1893
1894             if Is_Enumeration_Type (Ityp)
1895               and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
1896             then
1897                if No (Exprs) then
1898                   Xnum := Uint_1;
1899                else
1900                   Xnum := Expr_Value (First (Expressions (N)));
1901                end if;
1902
1903                Rewrite (N,
1904                  Make_Attribute_Reference (Loc,
1905                    Prefix         => New_Occurrence_Of (Typ, Loc),
1906                    Attribute_Name => Name_Max,
1907                    Expressions    => New_List
1908                      (Make_Integer_Literal (Loc, 0),
1909
1910                       Make_Op_Add (Loc,
1911                         Left_Opnd =>
1912                           Make_Op_Subtract (Loc,
1913                             Left_Opnd =>
1914                               Make_Attribute_Reference (Loc,
1915                                 Prefix => New_Occurrence_Of (Ityp, Loc),
1916                                 Attribute_Name => Name_Pos,
1917
1918                                 Expressions => New_List (
1919                                   Make_Attribute_Reference (Loc,
1920                                     Prefix => Duplicate_Subexpr (Pref),
1921                                    Attribute_Name => Name_Last,
1922                                     Expressions => New_List (
1923                                       Make_Integer_Literal (Loc, Xnum))))),
1924
1925                             Right_Opnd =>
1926                               Make_Attribute_Reference (Loc,
1927                                 Prefix => New_Occurrence_Of (Ityp, Loc),
1928                                 Attribute_Name => Name_Pos,
1929
1930                                 Expressions => New_List (
1931                                   Make_Attribute_Reference (Loc,
1932                                     Prefix => Duplicate_Subexpr (Pref),
1933                                    Attribute_Name => Name_First,
1934                                     Expressions => New_List (
1935                                       Make_Integer_Literal (Loc, Xnum)))))),
1936
1937                         Right_Opnd => Make_Integer_Literal (Loc, 1)))));
1938
1939                Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1940                return;
1941
1942             --  If the prefix type is a constrained packed array type which
1943             --  already has a Packed_Array_Type representation defined, then
1944             --  replace this attribute with a direct reference to 'Range_Length
1945             --  of the appropriate index subtype (since otherwise Gigi will try
1946             --  to give us the value of 'Length for this implementation type).
1947
1948             elsif Is_Constrained (Ptyp) then
1949                Rewrite (N,
1950                  Make_Attribute_Reference (Loc,
1951                    Attribute_Name => Name_Range_Length,
1952                    Prefix => New_Reference_To (Ityp, Loc)));
1953                Analyze_And_Resolve (N, Typ);
1954             end if;
1955
1956          --  If we have a packed array that is not bit packed, which was
1957
1958          --  Access type case
1959
1960          elsif Is_Access_Type (Ptyp) then
1961             Apply_Access_Check (N);
1962
1963             --  If the designated type is a packed array type, then we
1964             --  convert the reference to:
1965
1966             --    typ'Max (0, 1 +
1967             --                xtyp'Pos (Pref'Last (Expr)) -
1968             --                xtyp'Pos (Pref'First (Expr)));
1969
1970             --  This is a bit complex, but it is the easiest thing to do
1971             --  that works in all cases including enum types with holes
1972             --  xtyp here is the appropriate index type.
1973
1974             declare
1975                Dtyp : constant Entity_Id := Designated_Type (Ptyp);
1976                Xtyp : Entity_Id;
1977
1978             begin
1979                if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
1980                   Xtyp := Get_Index_Subtype (N);
1981
1982                   Rewrite (N,
1983                     Make_Attribute_Reference (Loc,
1984                       Prefix         => New_Occurrence_Of (Typ, Loc),
1985                       Attribute_Name => Name_Max,
1986                       Expressions    => New_List (
1987                         Make_Integer_Literal (Loc, 0),
1988
1989                         Make_Op_Add (Loc,
1990                           Make_Integer_Literal (Loc, 1),
1991                           Make_Op_Subtract (Loc,
1992                             Left_Opnd =>
1993                               Make_Attribute_Reference (Loc,
1994                                 Prefix => New_Occurrence_Of (Xtyp, Loc),
1995                                 Attribute_Name => Name_Pos,
1996                                 Expressions    => New_List (
1997                                   Make_Attribute_Reference (Loc,
1998                                     Prefix => Duplicate_Subexpr (Pref),
1999                                     Attribute_Name => Name_Last,
2000                                     Expressions =>
2001                                       New_Copy_List (Exprs)))),
2002
2003                             Right_Opnd =>
2004                               Make_Attribute_Reference (Loc,
2005                                 Prefix => New_Occurrence_Of (Xtyp, Loc),
2006                                 Attribute_Name => Name_Pos,
2007                                 Expressions    => New_List (
2008                                   Make_Attribute_Reference (Loc,
2009                                     Prefix => Duplicate_Subexpr (Pref),
2010                                     Attribute_Name => Name_First,
2011                                     Expressions =>
2012                                       New_Copy_List (Exprs)))))))));
2013
2014                   Analyze_And_Resolve (N, Typ);
2015                end if;
2016             end;
2017
2018          --  Otherwise leave it to gigi
2019
2020          else
2021             Apply_Universal_Integer_Attribute_Checks (N);
2022          end if;
2023       end;
2024
2025       -------------
2026       -- Machine --
2027       -------------
2028
2029       --  Transforms 'Machine into a call to the floating-point attribute
2030       --  function Machine in Fat_xxx (where xxx is the root type)
2031
2032       when Attribute_Machine =>
2033          Expand_Fpt_Attribute_R (N);
2034
2035       ------------------
2036       -- Machine_Size --
2037       ------------------
2038
2039       --  Machine_Size is equivalent to Object_Size, so transform it into
2040       --  Object_Size and that way Gigi never sees Machine_Size.
2041
2042       when Attribute_Machine_Size =>
2043          Rewrite (N,
2044            Make_Attribute_Reference (Loc,
2045              Prefix => Prefix (N),
2046              Attribute_Name => Name_Object_Size));
2047
2048          Analyze_And_Resolve (N, Typ);
2049
2050       --------------
2051       -- Mantissa --
2052       --------------
2053
2054       --  The only case that can get this far is the dynamic case of the
2055       --  old Ada 83 Mantissa attribute for the fixed-point case. For this
2056       --  case, we expand:
2057
2058       --    typ'Mantissa
2059
2060       --  into
2061
2062       --    ityp (System.Mantissa.Mantissa_Value
2063       --           (Integer'Integer_Value (typ'First),
2064       --            Integer'Integer_Value (typ'Last)));
2065
2066       when Attribute_Mantissa => Mantissa : declare
2067          Ptyp : constant Entity_Id := Etype (Pref);
2068
2069       begin
2070          Rewrite (N,
2071            Convert_To (Typ,
2072              Make_Function_Call (Loc,
2073                Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2074
2075                Parameter_Associations => New_List (
2076
2077                  Make_Attribute_Reference (Loc,
2078                    Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2079                    Attribute_Name => Name_Integer_Value,
2080                    Expressions => New_List (
2081
2082                      Make_Attribute_Reference (Loc,
2083                        Prefix => New_Occurrence_Of (Ptyp, Loc),
2084                        Attribute_Name => Name_First))),
2085
2086                  Make_Attribute_Reference (Loc,
2087                    Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2088                    Attribute_Name => Name_Integer_Value,
2089                    Expressions => New_List (
2090
2091                      Make_Attribute_Reference (Loc,
2092                        Prefix => New_Occurrence_Of (Ptyp, Loc),
2093                        Attribute_Name => Name_Last)))))));
2094
2095          Analyze_And_Resolve (N, Typ);
2096       end Mantissa;
2097
2098       -----------
2099       -- Model --
2100       -----------
2101
2102       --  Transforms 'Model into a call to the floating-point attribute
2103       --  function Model in Fat_xxx (where xxx is the root type)
2104
2105       when Attribute_Model =>
2106          Expand_Fpt_Attribute_R (N);
2107
2108       -----------------
2109       -- Object_Size --
2110       -----------------
2111
2112       --  The processing for Object_Size shares the processing for Size
2113
2114       ------------
2115       -- Output --
2116       ------------
2117
2118       when Attribute_Output => Output : declare
2119          P_Type : constant Entity_Id := Entity (Pref);
2120          B_Type : constant Entity_Id := Base_Type (P_Type);
2121          U_Type : constant Entity_Id := Underlying_Type (P_Type);
2122          Pname  : Entity_Id;
2123          Decl   : Node_Id;
2124          Prag   : Node_Id;
2125          Arg3   : Node_Id;
2126          Wfunc  : Node_Id;
2127
2128       begin
2129          --  If no underlying type, we have an error that will be diagnosed
2130          --  elsewhere, so here we just completely ignore the expansion.
2131
2132          if No (U_Type) then
2133             return;
2134          end if;
2135
2136          --  If TSS for Output is present, just call it
2137
2138          Pname := Find_Inherited_TSS (P_Type, Name_uOutput);
2139
2140          if Present (Pname) then
2141             null;
2142
2143          else
2144             --  If there is a Stream_Convert pragma, use it, we rewrite
2145
2146             --     sourcetyp'Output (stream, Item)
2147
2148             --  as
2149
2150             --     strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2151
2152             --  where strmwrite is the given Write function that converts
2153             --  an argument of type sourcetyp or a type acctyp, from which
2154             --  it is derived to type strmtyp. The conversion to acttyp is
2155             --  required for the derived case.
2156
2157             Prag :=
2158               Get_Rep_Pragma
2159                 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2160
2161             if Present (Prag) then
2162                Arg3 :=
2163                  Next (Next (First (Pragma_Argument_Associations (Prag))));
2164                Wfunc := Entity (Expression (Arg3));
2165
2166                Rewrite (N,
2167                  Make_Attribute_Reference (Loc,
2168                    Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
2169                    Attribute_Name => Name_Output,
2170                    Expressions => New_List (
2171                    Relocate_Node (First (Exprs)),
2172                      Make_Function_Call (Loc,
2173                        Name => New_Occurrence_Of (Wfunc, Loc),
2174                        Parameter_Associations => New_List (
2175                          Convert_To (Etype (First_Formal (Wfunc)),
2176                            Relocate_Node (Next (First (Exprs)))))))));
2177
2178                Analyze (N);
2179                return;
2180
2181             --  For elementary types, we call the W_xxx routine directly.
2182             --  Note that the effect of Write and Output is identical for
2183             --  the case of an elementary type, since there are no
2184             --  discriminants or bounds.
2185
2186             elsif Is_Elementary_Type (U_Type) then
2187
2188                --  A special case arises if we have a defined _Write routine,
2189                --  since in this case we are required to call this routine.
2190
2191                if Present (TSS (B_Type, Name_uWrite)) then
2192                   Build_Record_Or_Elementary_Output_Procedure
2193                     (Loc, U_Type, Decl, Pname);
2194                   Insert_Action (N, Decl);
2195
2196                --  For normal cases, we call the W_xxx routine directly
2197
2198                else
2199                   Rewrite (N, Build_Elementary_Write_Call (N));
2200                   Analyze (N);
2201                   return;
2202                end if;
2203
2204             --  Array type case
2205
2206             elsif Is_Array_Type (U_Type) then
2207                Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
2208                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2209
2210             --  Class-wide case, first output external tag, then dispatch
2211             --  to the appropriate primitive Output function (RM 13.13.2(31)).
2212
2213             elsif Is_Class_Wide_Type (P_Type) then
2214                Tag_Write : declare
2215                   Strm : constant Node_Id := First (Exprs);
2216                   Item : constant Node_Id := Next (Strm);
2217
2218                begin
2219                   --  The code is:
2220                   --  String'Output (Strm, External_Tag (Item'Tag))
2221
2222                   Insert_Action (N,
2223                     Make_Attribute_Reference (Loc,
2224                       Prefix => New_Occurrence_Of (Standard_String, Loc),
2225                       Attribute_Name => Name_Output,
2226                       Expressions => New_List (
2227                         Relocate_Node (Duplicate_Subexpr (Strm)),
2228                         Make_Function_Call (Loc,
2229                           Name =>
2230                             New_Occurrence_Of (RTE (RE_External_Tag), Loc),
2231                           Parameter_Associations => New_List (
2232                            Make_Attribute_Reference (Loc,
2233                              Prefix =>
2234                                Relocate_Node
2235                                  (Duplicate_Subexpr (Item, Name_Req => True)),
2236                              Attribute_Name => Name_Tag))))));
2237                end Tag_Write;
2238
2239                Pname := Find_Prim_Op (U_Type, Name_uOutput);
2240
2241             --  Tagged type case, use the primitive Output function
2242
2243             elsif Is_Tagged_Type (U_Type) then
2244                Pname := Find_Prim_Op (U_Type, Name_uOutput);
2245
2246             --  All other record type cases, including protected records.
2247             --  The latter only arise for expander generated code for
2248             --  handling shared passive partition access.
2249
2250             else
2251                pragma Assert
2252                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2253
2254                Build_Record_Or_Elementary_Output_Procedure
2255                  (Loc, Base_Type (U_Type), Decl, Pname);
2256                Insert_Action (N, Decl);
2257             end if;
2258          end if;
2259
2260          --  If we fall through, Pname is the name of the procedure to call
2261
2262          Rewrite_Stream_Proc_Call (Pname);
2263       end Output;
2264
2265       ---------
2266       -- Pos --
2267       ---------
2268
2269       --  For enumeration types with a standard representation, Pos is
2270       --  handled by Gigi.
2271
2272       --  For enumeration types, with a non-standard representation we
2273       --  generate a call to the _Rep_To_Pos function created when the
2274       --  type was frozen. The call has the form
2275
2276       --    _rep_to_pos (expr, True)
2277
2278       --  The parameter True causes Program_Error to be raised if the
2279       --  expression has an invalid representation.
2280
2281       --  For integer types, Pos is equivalent to a simple integer
2282       --  conversion and we rewrite it as such
2283
2284       when Attribute_Pos => Pos :
2285       declare
2286          Etyp : Entity_Id := Base_Type (Entity (Pref));
2287
2288       begin
2289          --  Deal with zero/non-zero boolean values
2290
2291          if Is_Boolean_Type (Etyp) then
2292             Adjust_Condition (First (Exprs));
2293             Etyp := Standard_Boolean;
2294             Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
2295          end if;
2296
2297          --  Case of enumeration type
2298
2299          if Is_Enumeration_Type (Etyp) then
2300
2301             --  Non-standard enumeration type (generate call)
2302
2303             if Present (Enum_Pos_To_Rep (Etyp)) then
2304                Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2305
2306                Rewrite (N,
2307                  Convert_To (Typ,
2308                    Make_Function_Call (Loc,
2309                      Name =>
2310                        New_Reference_To (TSS (Etyp, Name_uRep_To_Pos), Loc),
2311                      Parameter_Associations => Exprs)));
2312
2313                Analyze_And_Resolve (N, Typ);
2314
2315             --  Standard enumeration type (do universal integer check)
2316
2317             else
2318                Apply_Universal_Integer_Attribute_Checks (N);
2319             end if;
2320
2321          --  Deal with integer types (replace by conversion)
2322
2323          elsif Is_Integer_Type (Etyp) then
2324             Rewrite (N, Convert_To (Typ, First (Exprs)));
2325             Analyze_And_Resolve (N, Typ);
2326          end if;
2327
2328       end Pos;
2329
2330       --------------
2331       -- Position --
2332       --------------
2333
2334       --  We compute this if a component clause was present, otherwise
2335       --  we leave the computation up to Gigi, since we don't know what
2336       --  layout will be chosen.
2337
2338       when Attribute_Position => Position :
2339       declare
2340          CE : constant Entity_Id := Entity (Selector_Name (Pref));
2341
2342       begin
2343          if Present (Component_Clause (CE)) then
2344             Rewrite (N,
2345               Make_Integer_Literal (Loc,
2346                 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
2347             Analyze_And_Resolve (N, Typ);
2348
2349          else
2350             Apply_Universal_Integer_Attribute_Checks (N);
2351          end if;
2352       end Position;
2353
2354       ----------
2355       -- Pred --
2356       ----------
2357
2358       --  1. Deal with enumeration types with holes
2359       --  2. For floating-point, generate call to attribute function
2360       --  3. For other cases, deal with constraint checking
2361
2362       when Attribute_Pred => Pred :
2363       declare
2364          Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
2365
2366       begin
2367          --  For enumeration types with non-standard representations, we
2368          --  expand typ'Pred (x) into
2369
2370          --    Pos_To_Rep (Rep_To_Pos (x) - 1)
2371
2372          if Is_Enumeration_Type (Ptyp)
2373            and then Present (Enum_Pos_To_Rep (Ptyp))
2374          then
2375             --  Add Boolean parameter True, to request program errror if
2376             --  we have a bad representation on our hands.
2377
2378             Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
2379
2380             Rewrite (N,
2381               Make_Indexed_Component (Loc,
2382                 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
2383                 Expressions => New_List (
2384                   Make_Op_Subtract (Loc,
2385                     Left_Opnd =>
2386                       Make_Function_Call (Loc,
2387                         Name =>
2388                           New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
2389                         Parameter_Associations => Exprs),
2390                     Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2391
2392             Analyze_And_Resolve (N, Typ);
2393
2394          --  For floating-point, we transform 'Pred into a call to the Pred
2395          --  floating-point attribute function in Fat_xxx (xxx is root type)
2396
2397          elsif Is_Floating_Point_Type (Ptyp) then
2398             Expand_Fpt_Attribute_R (N);
2399             Analyze_And_Resolve (N, Typ);
2400
2401          --  For modular types, nothing to do (no overflow, since wraps)
2402
2403          elsif Is_Modular_Integer_Type (Ptyp) then
2404             null;
2405
2406          --  For other types, if range checking is enabled, we must generate
2407          --  a check if overflow checking is enabled.
2408
2409          elsif not Overflow_Checks_Suppressed (Ptyp) then
2410             Expand_Pred_Succ (N);
2411          end if;
2412
2413       end Pred;
2414
2415       ------------------
2416       -- Range_Length --
2417       ------------------
2418
2419       when Attribute_Range_Length => Range_Length : declare
2420          P_Type : constant Entity_Id := Etype (Pref);
2421
2422       begin
2423          --  The only special processing required is for the case where
2424          --  Range_Length is applied to an enumeration type with holes.
2425          --  In this case we transform
2426
2427          --     X'Range_Length
2428
2429          --  to
2430
2431          --     X'Pos (X'Last) - X'Pos (X'First) + 1
2432
2433          --  So that the result reflects the proper Pos values instead
2434          --  of the underlying representations.
2435
2436          if Is_Enumeration_Type (P_Type)
2437            and then Has_Non_Standard_Rep (P_Type)
2438          then
2439             Rewrite (N,
2440               Make_Op_Add (Loc,
2441                 Left_Opnd =>
2442                   Make_Op_Subtract (Loc,
2443                     Left_Opnd =>
2444                       Make_Attribute_Reference (Loc,
2445                         Attribute_Name => Name_Pos,
2446                         Prefix => New_Occurrence_Of (P_Type, Loc),
2447                         Expressions => New_List (
2448                           Make_Attribute_Reference (Loc,
2449                             Attribute_Name => Name_Last,
2450                             Prefix => New_Occurrence_Of (P_Type, Loc)))),
2451
2452                     Right_Opnd =>
2453                       Make_Attribute_Reference (Loc,
2454                         Attribute_Name => Name_Pos,
2455                         Prefix => New_Occurrence_Of (P_Type, Loc),
2456                         Expressions => New_List (
2457                           Make_Attribute_Reference (Loc,
2458                             Attribute_Name => Name_First,
2459                             Prefix => New_Occurrence_Of (P_Type, Loc))))),
2460
2461                 Right_Opnd =>
2462                   Make_Integer_Literal (Loc, 1)));
2463
2464             Analyze_And_Resolve (N, Typ);
2465
2466          --  For all other cases, attribute is handled by Gigi, but we need
2467          --  to deal with the case of the range check on a universal integer.
2468
2469          else
2470             Apply_Universal_Integer_Attribute_Checks (N);
2471          end if;
2472
2473       end Range_Length;
2474
2475       ----------
2476       -- Read --
2477       ----------
2478
2479       when Attribute_Read => Read : declare
2480          P_Type : constant Entity_Id := Entity (Pref);
2481          B_Type : constant Entity_Id := Base_Type (P_Type);
2482          U_Type : constant Entity_Id := Underlying_Type (P_Type);
2483          Pname  : Entity_Id;
2484          Decl   : Node_Id;
2485          Prag   : Node_Id;
2486          Arg2   : Node_Id;
2487          Rfunc  : Node_Id;
2488          Lhs    : Node_Id;
2489          Rhs    : Node_Id;
2490
2491       begin
2492          --  If no underlying type, we have an error that will be diagnosed
2493          --  elsewhere, so here we just completely ignore the expansion.
2494
2495          if No (U_Type) then
2496             return;
2497          end if;
2498
2499          --  The simple case, if there is a TSS for Read, just call it
2500
2501          Pname := Find_Inherited_TSS (P_Type, Name_uRead);
2502
2503          if Present (Pname) then
2504             null;
2505
2506          else
2507             --  If there is a Stream_Convert pragma, use it, we rewrite
2508
2509             --     sourcetyp'Read (stream, Item)
2510
2511             --  as
2512
2513             --     Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2514
2515             --  where strmread is the given Read function that converts
2516             --  an argument of type strmtyp to type sourcetyp or a type
2517             --  from which it is derived. The conversion to sourcetyp
2518             --  is required in the latter case.
2519
2520             --  A special case arises if Item is a type conversion in which
2521             --  case, we have to expand to:
2522
2523             --     Itemx := typex (strmread (strmtyp'Input (Stream)));
2524
2525             --  where Itemx is the expression of the type conversion (i.e.
2526             --  the actual object), and typex is the type of Itemx.
2527
2528             Prag :=
2529               Get_Rep_Pragma
2530                 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
2531
2532             if Present (Prag) then
2533                Arg2  := Next (First (Pragma_Argument_Associations (Prag)));
2534                Rfunc := Entity (Expression (Arg2));
2535                Lhs := Relocate_Node (Next (First (Exprs)));
2536                Rhs :=
2537                  Convert_To (B_Type,
2538                    Make_Function_Call (Loc,
2539                      Name => New_Occurrence_Of (Rfunc, Loc),
2540                      Parameter_Associations => New_List (
2541                        Make_Attribute_Reference (Loc,
2542                          Prefix =>
2543                            New_Occurrence_Of
2544                              (Etype (First_Formal (Rfunc)), Loc),
2545                          Attribute_Name => Name_Input,
2546                          Expressions => New_List (
2547                            Relocate_Node (First (Exprs)))))));
2548
2549                if Nkind (Lhs) = N_Type_Conversion then
2550                   Lhs := Expression (Lhs);
2551                   Rhs := Convert_To (Etype (Lhs), Rhs);
2552                end if;
2553
2554                Rewrite (N,
2555                  Make_Assignment_Statement (Loc,
2556                    Name => Lhs,
2557                    Expression => Rhs));
2558                Set_Assignment_OK (Lhs);
2559                Analyze (N);
2560                return;
2561
2562             --  For elementary types, we call the I_xxx routine using the first
2563             --  parameter and then assign the result into the second parameter.
2564             --  We set Assignment_OK to deal with the conversion case.
2565
2566             elsif Is_Elementary_Type (U_Type) then
2567                declare
2568                   Lhs : Node_Id;
2569                   Rhs : Node_Id;
2570
2571                begin
2572                   Lhs := Relocate_Node (Next (First (Exprs)));
2573                   Rhs := Build_Elementary_Input_Call (N);
2574
2575                   if Nkind (Lhs) = N_Type_Conversion then
2576                      Lhs := Expression (Lhs);
2577                      Rhs := Convert_To (Etype (Lhs), Rhs);
2578                   end if;
2579
2580                   Set_Assignment_OK (Lhs);
2581
2582                   Rewrite (N,
2583                     Make_Assignment_Statement (Loc,
2584                       Name => Lhs,
2585                       Expression => Rhs));
2586
2587                   Analyze (N);
2588                   return;
2589                end;
2590
2591             --  Array type case
2592
2593             elsif Is_Array_Type (U_Type) then
2594                Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
2595                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2596
2597             --  Tagged type case, use the primitive Read function. Note that
2598             --  this will dispatch in the class-wide case which is what we want
2599
2600             elsif Is_Tagged_Type (U_Type) then
2601                Pname := Find_Prim_Op (U_Type, Name_uRead);
2602
2603             --  All other record type cases, including protected records.
2604             --  The latter only arise for expander generated code for
2605             --  handling shared passive partition access.
2606
2607             else
2608                pragma Assert
2609                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2610
2611                if Has_Discriminants (U_Type)
2612                  and then Present
2613                    (Discriminant_Default_Value (First_Discriminant (U_Type)))
2614                then
2615                   Build_Mutable_Record_Read_Procedure
2616                     (Loc, Base_Type (U_Type), Decl, Pname);
2617
2618                else
2619                   Build_Record_Read_Procedure
2620                     (Loc, Base_Type (U_Type), Decl, Pname);
2621                end if;
2622
2623                --  Suppress checks, uninitialized or otherwise invalid
2624                --  data does not cause constraint errors to be raised for
2625                --  a complete record read.
2626
2627                Insert_Action (N, Decl, All_Checks);
2628             end if;
2629          end if;
2630
2631          Rewrite_Stream_Proc_Call (Pname);
2632       end Read;
2633
2634       ---------------
2635       -- Remainder --
2636       ---------------
2637
2638       --  Transforms 'Remainder into a call to the floating-point attribute
2639       --  function Remainder in Fat_xxx (where xxx is the root type)
2640
2641       when Attribute_Remainder =>
2642          Expand_Fpt_Attribute_RR (N);
2643
2644       -----------
2645       -- Round --
2646       -----------
2647
2648       --  The handling of the Round attribute is quite delicate. The
2649       --  processing in Sem_Attr introduced a conversion to universal
2650       --  real, reflecting the semantics of Round, but we do not want
2651       --  anything to do with universal real at runtime, since this
2652       --  corresponds to using floating-point arithmetic.
2653
2654       --  What we have now is that the Etype of the Round attribute
2655       --  correctly indicates the final result type. The operand of
2656       --  the Round is the conversion to universal real, described
2657       --  above, and the operand of this conversion is the actual
2658       --  operand of Round, which may be the special case of a fixed
2659       --  point multiplication or division (Etype = universal fixed)
2660
2661       --  The exapander will expand first the operand of the conversion,
2662       --  then the conversion, and finally the round attribute itself,
2663       --  since we always work inside out. But we cannot simply process
2664       --  naively in this order. In the semantic world where universal
2665       --  fixed and real really exist and have infinite precision, there
2666       --  is no problem, but in the implementation world, where universal
2667       --  real is a floating-point type, we would get the wrong result.
2668
2669       --  So the approach is as follows. First, when expanding a multiply
2670       --  or divide whose type is universal fixed, we do nothing at all,
2671       --  instead deferring the operation till later.
2672
2673       --  The actual processing is done in Expand_N_Type_Conversion which
2674       --  handles the special case of Round by looking at its parent to
2675       --  see if it is a Round attribute, and if it is, handling the
2676       --  conversion (or its fixed multiply/divide child) in an appropriate
2677       --  manner.
2678
2679       --  This means that by the time we get to expanding the Round attribute
2680       --  itself, the Round is nothing more than a type conversion (and will
2681       --  often be a null type conversion), so we just replace it with the
2682       --  appropriate conversion operation.
2683
2684       when Attribute_Round =>
2685          Rewrite (N,
2686            Convert_To (Etype (N), Relocate_Node (First (Exprs))));
2687          Analyze_And_Resolve (N);
2688
2689       --------------
2690       -- Rounding --
2691       --------------
2692
2693       --  Transforms 'Rounding into a call to the floating-point attribute
2694       --  function Rounding in Fat_xxx (where xxx is the root type)
2695
2696       when Attribute_Rounding =>
2697          Expand_Fpt_Attribute_R (N);
2698
2699       -------------
2700       -- Scaling --
2701       -------------
2702
2703       --  Transforms 'Scaling into a call to the floating-point attribute
2704       --  function Scaling in Fat_xxx (where xxx is the root type)
2705
2706       when Attribute_Scaling =>
2707          Expand_Fpt_Attribute_RI (N);
2708
2709       ----------
2710       -- Size --
2711       ----------
2712
2713       when Attribute_Size        |
2714            Attribute_Object_Size |
2715            Attribute_Value_Size  |
2716            Attribute_VADS_Size   => Size :
2717
2718       declare
2719          Ptyp     : constant Entity_Id := Etype (Pref);
2720          New_Node : Node_Id;
2721          Siz      : Uint;
2722
2723       begin
2724          --  Processing for VADS_Size case. Note that this processing removes
2725          --  all traces of VADS_Size from the tree, and completes all required
2726          --  processing for VADS_Size by translating the attribute reference
2727          --  to an appropriate Size or Object_Size reference.
2728
2729          if Id = Attribute_VADS_Size
2730            or else (Use_VADS_Size and then Id = Attribute_Size)
2731          then
2732             --  If the size is specified, then we simply use the specified
2733             --  size. This applies to both types and objects. The size of an
2734             --  object can be specified in the following ways:
2735
2736             --    An explicit size object is given for an object
2737             --    A component size is specified for an indexed component
2738             --    A component clause is specified for a selected component
2739             --    The object is a component of a packed composite object
2740
2741             --  If the size is specified, then VADS_Size of an object
2742
2743             if (Is_Entity_Name (Pref)
2744                  and then Present (Size_Clause (Entity (Pref))))
2745               or else
2746                 (Nkind (Pref) = N_Component_Clause
2747                   and then (Present (Component_Clause
2748                                      (Entity (Selector_Name (Pref))))
2749                              or else Is_Packed (Etype (Prefix (Pref)))))
2750               or else
2751                 (Nkind (Pref) = N_Indexed_Component
2752                   and then (Component_Size (Etype (Prefix (Pref))) /= 0
2753                              or else Is_Packed (Etype (Prefix (Pref)))))
2754             then
2755                Set_Attribute_Name (N, Name_Size);
2756
2757             --  Otherwise if we have an object rather than a type, then the
2758             --  VADS_Size attribute applies to the type of the object, rather
2759             --  than the object itself. This is one of the respects in which
2760             --  VADS_Size differs from Size.
2761
2762             else
2763                if (not Is_Entity_Name (Pref)
2764                     or else not Is_Type (Entity (Pref)))
2765                  and then (Is_Scalar_Type (Etype (Pref))
2766                             or else Is_Constrained (Etype (Pref)))
2767                then
2768                   Rewrite (Pref, New_Occurrence_Of (Etype (Pref), Loc));
2769                end if;
2770
2771                --  For a scalar type for which no size was
2772                --  explicitly given, VADS_Size means Object_Size. This is the
2773                --  other respect in which VADS_Size differs from Size.
2774
2775                if Is_Scalar_Type (Etype (Pref))
2776                  and then No (Size_Clause (Etype (Pref)))
2777                then
2778                   Set_Attribute_Name (N, Name_Object_Size);
2779
2780                --  In all other cases, Size and VADS_Size are the sane
2781
2782                else
2783                   Set_Attribute_Name (N, Name_Size);
2784                end if;
2785             end if;
2786          end if;
2787
2788          --  For class-wide types, transform X'Size into a call to
2789          --  the primitive operation _Size
2790
2791          if Is_Class_Wide_Type (Ptyp) then
2792             New_Node :=
2793               Make_Function_Call (Loc,
2794                 Name => New_Reference_To
2795                   (Find_Prim_Op (Ptyp, Name_uSize), Loc),
2796                 Parameter_Associations => New_List (Pref));
2797
2798             if Typ /= Standard_Long_Long_Integer then
2799
2800                --  The context is a specific integer type with which the
2801                --  original attribute was compatible. The function has a
2802                --  specific type as well, so to preserve the compatibility
2803                --  we must convert explicitly.
2804
2805                New_Node := Convert_To (Typ, New_Node);
2806             end if;
2807
2808             Rewrite (N, New_Node);
2809             Analyze_And_Resolve (N, Typ);
2810             return;
2811
2812          --  For an array component, we can do Size in the front end
2813          --  if the component_size of the array is set.
2814
2815          elsif Nkind (Pref) = N_Indexed_Component then
2816             Siz := Component_Size (Etype (Prefix (Pref)));
2817
2818          --  For a record component, we can do Size in the front end
2819          --  if there is a component clause, or if the record is packed
2820          --  and the component's size is known at compile time.
2821
2822          elsif Nkind (Pref) = N_Selected_Component then
2823             declare
2824                Rec  : constant Entity_Id := Etype (Prefix (Pref));
2825                Comp : constant Entity_Id := Entity (Selector_Name (Pref));
2826
2827             begin
2828                if Present (Component_Clause (Comp)) then
2829                   Siz := Esize (Comp);
2830
2831                elsif Is_Packed (Rec) then
2832                   Siz := RM_Size (Ptyp);
2833
2834                else
2835                   Apply_Universal_Integer_Attribute_Checks (N);
2836                   return;
2837                end if;
2838             end;
2839
2840          --  All other cases are handled by Gigi
2841
2842          else
2843             Apply_Universal_Integer_Attribute_Checks (N);
2844
2845             --  If we have Size applied to a formal parameter, that is a
2846             --  packed array subtype, then apply size to the actual subtype.
2847
2848             if Is_Entity_Name (Pref)
2849               and then Is_Formal (Entity (Pref))
2850               and then Is_Array_Type (Etype (Pref))
2851               and then Is_Packed (Etype (Pref))
2852             then
2853                Rewrite (N,
2854                  Make_Attribute_Reference (Loc,
2855                    Prefix =>
2856                      New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
2857                    Attribute_Name => Name_Size));
2858                Analyze_And_Resolve (N, Typ);
2859             end if;
2860
2861             return;
2862          end if;
2863
2864          --  Common processing for record and array component case
2865
2866          if Siz /= 0 then
2867             Rewrite (N,
2868               Make_Integer_Literal (Loc, Siz));
2869
2870             Analyze_And_Resolve (N, Typ);
2871
2872             --  The result is not a static expression
2873
2874             Set_Is_Static_Expression (N, False);
2875          end if;
2876       end Size;
2877
2878       ------------------
2879       -- Storage_Pool --
2880       ------------------
2881
2882       when Attribute_Storage_Pool =>
2883          Rewrite (N,
2884            Make_Type_Conversion (Loc,
2885              Subtype_Mark => New_Reference_To (Etype (N), Loc),
2886              Expression   => New_Reference_To (Entity (N), Loc)));
2887          Analyze_And_Resolve (N, Typ);
2888
2889       ------------------
2890       -- Storage_Size --
2891       ------------------
2892
2893       when Attribute_Storage_Size => Storage_Size :
2894       declare
2895          Ptyp : constant Entity_Id := Etype (Pref);
2896
2897       begin
2898          --  Access type case, always go to the root type
2899
2900          --  The case of access types results in a value of zero for the case
2901          --  where no storage size attribute clause has been given. If a
2902          --  storage size has been given, then the attribute is converted
2903          --  to a reference to the variable used to hold this value.
2904
2905          if Is_Access_Type (Ptyp) then
2906             if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
2907                Rewrite (N,
2908                  Make_Attribute_Reference (Loc,
2909                    Prefix => New_Reference_To (Typ, Loc),
2910                    Attribute_Name => Name_Max,
2911                    Expressions => New_List (
2912                      Make_Integer_Literal (Loc, 0),
2913                      Convert_To (Typ,
2914                        New_Reference_To
2915                          (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
2916
2917             elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
2918                Rewrite (N,
2919                  OK_Convert_To (Typ,
2920                    Make_Function_Call (Loc,
2921                      Name => New_Reference_To (Find_Prim_Op (Etype (
2922                        Associated_Storage_Pool (Root_Type (Ptyp))),
2923                        Attribute_Name (N)), Loc),
2924
2925                      Parameter_Associations => New_List (New_Reference_To (
2926                        Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
2927             else
2928                Rewrite (N, Make_Integer_Literal (Loc, 0));
2929             end if;
2930
2931             Analyze_And_Resolve (N, Typ);
2932
2933          --  The case of a task type (an obsolescent feature) is handled the
2934          --  same way, seems as reasonable as anything, and it is what the
2935          --  ACVC tests (e.g. CD1009K) seem to expect.
2936
2937          --  If there is no Storage_Size variable, then we return the default
2938          --  task stack size, otherwise, expand a Storage_Size attribute as
2939          --  follows:
2940
2941          --  Typ (Adjust_Storage_Size (taskZ))
2942
2943          --  except for the case of a task object which has a Storage_Size
2944          --  pragma:
2945
2946          --  Typ (Adjust_Storage_Size (taskV!(name)._Size))
2947
2948          else
2949             if not Present (Storage_Size_Variable (Ptyp)) then
2950                Rewrite (N,
2951                  Convert_To (Typ,
2952                    Make_Function_Call (Loc,
2953                      Name =>
2954                        New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc))));
2955
2956             else
2957                if not (Is_Entity_Name (Pref) and then
2958                  Is_Task_Type (Entity (Pref))) and then
2959                    Chars (Last_Entity (Corresponding_Record_Type (Ptyp))) =
2960                      Name_uSize
2961                then
2962                   Rewrite (N,
2963                     Convert_To (Typ,
2964                       Make_Function_Call (Loc,
2965                         Name => New_Occurrence_Of (
2966                           RTE (RE_Adjust_Storage_Size), Loc),
2967                         Parameter_Associations =>
2968                           New_List (
2969                             Make_Selected_Component (Loc,
2970                               Prefix =>
2971                                 Unchecked_Convert_To (
2972                                   Corresponding_Record_Type (Ptyp),
2973                                   New_Copy_Tree (Pref)),
2974                               Selector_Name =>
2975                                 Make_Identifier (Loc, Name_uSize))))));
2976
2977                --  Task not having Storage_Size pragma
2978
2979                else
2980                   Rewrite (N,
2981                     Convert_To (Typ,
2982                       Make_Function_Call (Loc,
2983                         Name => New_Occurrence_Of (
2984                           RTE (RE_Adjust_Storage_Size), Loc),
2985                         Parameter_Associations =>
2986                           New_List (
2987                             New_Reference_To (
2988                               Storage_Size_Variable (Ptyp), Loc)))));
2989                end if;
2990
2991                Analyze_And_Resolve (N, Typ);
2992             end if;
2993          end if;
2994       end Storage_Size;
2995
2996       ----------
2997       -- Succ --
2998       ----------
2999
3000       --  1. Deal with enumeration types with holes
3001       --  2. For floating-point, generate call to attribute function
3002       --  3. For other cases, deal with constraint checking
3003
3004       when Attribute_Succ => Succ :
3005       declare
3006          Ptyp : constant Entity_Id := Base_Type (Etype (Pref));
3007
3008       begin
3009          --  For enumeration types with non-standard representations, we
3010          --  expand typ'Succ (x) into
3011
3012          --    Pos_To_Rep (Rep_To_Pos (x) + 1)
3013
3014          if Is_Enumeration_Type (Ptyp)
3015            and then Present (Enum_Pos_To_Rep (Ptyp))
3016          then
3017             --  Add Boolean parameter True, to request program errror if
3018             --  we have a bad representation on our hands.
3019
3020             Append_To (Exprs, New_Occurrence_Of (Standard_True, Loc));
3021
3022             Rewrite (N,
3023               Make_Indexed_Component (Loc,
3024                 Prefix => New_Reference_To (Enum_Pos_To_Rep (Ptyp), Loc),
3025                 Expressions => New_List (
3026                   Make_Op_Add (Loc,
3027                     Left_Opnd =>
3028                       Make_Function_Call (Loc,
3029                         Name =>
3030                           New_Reference_To (TSS (Ptyp, Name_uRep_To_Pos), Loc),
3031                         Parameter_Associations => Exprs),
3032                     Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3033
3034             Analyze_And_Resolve (N, Typ);
3035
3036          --  For floating-point, we transform 'Succ into a call to the Succ
3037          --  floating-point attribute function in Fat_xxx (xxx is root type)
3038
3039          elsif Is_Floating_Point_Type (Ptyp) then
3040             Expand_Fpt_Attribute_R (N);
3041             Analyze_And_Resolve (N, Typ);
3042
3043          --  For modular types, nothing to do (no overflow, since wraps)
3044
3045          elsif Is_Modular_Integer_Type (Ptyp) then
3046             null;
3047
3048          --  For other types, if range checking is enabled, we must generate
3049          --  a check if overflow checking is enabled.
3050
3051          elsif not Overflow_Checks_Suppressed (Ptyp) then
3052             Expand_Pred_Succ (N);
3053          end if;
3054       end Succ;
3055
3056       ---------
3057       -- Tag --
3058       ---------
3059
3060       --  Transforms X'Tag into a direct reference to the tag of X
3061
3062       when Attribute_Tag => Tag :
3063       declare
3064          Ttyp           : Entity_Id;
3065          Prefix_Is_Type : Boolean;
3066
3067       begin
3068          if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
3069             Ttyp := Entity (Pref);
3070             Prefix_Is_Type := True;
3071          else
3072             Ttyp := Etype (Pref);
3073             Prefix_Is_Type := False;
3074          end if;
3075
3076          if Is_Class_Wide_Type (Ttyp) then
3077             Ttyp := Root_Type (Ttyp);
3078          end if;
3079
3080          Ttyp := Underlying_Type (Ttyp);
3081
3082          if Prefix_Is_Type then
3083
3084             --  For JGNAT we leave the type attribute unexpanded because
3085             --  there's not a dispatching table to reference.
3086
3087             if not Java_VM then
3088                Rewrite (N,
3089                  Unchecked_Convert_To (RTE (RE_Tag),
3090                    New_Reference_To (Access_Disp_Table (Ttyp), Loc)));
3091                Analyze_And_Resolve (N, RTE (RE_Tag));
3092             end if;
3093
3094          else
3095             Rewrite (N,
3096               Make_Selected_Component (Loc,
3097                 Prefix => Relocate_Node (Pref),
3098                 Selector_Name =>
3099                   New_Reference_To (Tag_Component (Ttyp), Loc)));
3100             Analyze_And_Resolve (N, RTE (RE_Tag));
3101          end if;
3102       end Tag;
3103
3104       ----------------
3105       -- Terminated --
3106       ----------------
3107
3108       --  Transforms 'Terminated attribute into a call to Terminated function.
3109
3110       when Attribute_Terminated => Terminated :
3111       begin
3112          if Restricted_Profile then
3113             Rewrite (N,
3114               Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
3115
3116          else
3117             Rewrite (N,
3118               Build_Call_With_Task (Pref, RTE (RE_Terminated)));
3119          end if;
3120
3121          Analyze_And_Resolve (N, Standard_Boolean);
3122       end Terminated;
3123
3124       ----------------
3125       -- To_Address --
3126       ----------------
3127
3128       --  Transforms System'To_Address (X) into unchecked conversion
3129       --  from (integral) type of X to type address.
3130
3131       when Attribute_To_Address =>
3132          Rewrite (N,
3133            Unchecked_Convert_To (RTE (RE_Address),
3134              Relocate_Node (First (Exprs))));
3135          Analyze_And_Resolve (N, RTE (RE_Address));
3136
3137       ----------------
3138       -- Truncation --
3139       ----------------
3140
3141       --  Transforms 'Truncation into a call to the floating-point attribute
3142       --  function Truncation in Fat_xxx (where xxx is the root type)
3143
3144       when Attribute_Truncation =>
3145          Expand_Fpt_Attribute_R (N);
3146
3147       -----------------------
3148       -- Unbiased_Rounding --
3149       -----------------------
3150
3151       --  Transforms 'Unbiased_Rounding into a call to the floating-point
3152       --  attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3153       --  root type)
3154
3155       when Attribute_Unbiased_Rounding =>
3156          Expand_Fpt_Attribute_R (N);
3157
3158       ----------------------
3159       -- Unchecked_Access --
3160       ----------------------
3161
3162       when Attribute_Unchecked_Access =>
3163          Expand_Access_To_Type (N);
3164
3165       -----------------
3166       -- UET_Address --
3167       -----------------
3168
3169       when Attribute_UET_Address => UET_Address : declare
3170          Ent : constant Entity_Id :=
3171                  Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3172
3173       begin
3174          Insert_Action (N,
3175            Make_Object_Declaration (Loc,
3176              Defining_Identifier => Ent,
3177              Aliased_Present     => True,
3178              Object_Definition   =>
3179                New_Occurrence_Of (RTE (RE_Address), Loc)));
3180
3181          --  Construct name __gnat_xxx__SDP, where xxx is the unit name
3182          --  in normal external form.
3183
3184          Get_External_Unit_Name_String (Get_Unit_Name (Pref));
3185          Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
3186          Name_Len := Name_Len + 7;
3187          Name_Buffer (1 .. 7) := "__gnat_";
3188          Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
3189          Name_Len := Name_Len + 5;
3190
3191          Set_Is_Imported (Ent);
3192          Set_Interface_Name (Ent,
3193            Make_String_Literal (Loc,
3194              Strval => String_From_Name_Buffer));
3195
3196          Rewrite (N,
3197            Make_Attribute_Reference (Loc,
3198              Prefix => New_Occurrence_Of (Ent, Loc),
3199              Attribute_Name => Name_Address));
3200
3201          Analyze_And_Resolve (N, Typ);
3202       end UET_Address;
3203
3204       -------------------------
3205       -- Unrestricted_Access --
3206       -------------------------
3207
3208       when Attribute_Unrestricted_Access =>
3209          Expand_Access_To_Type (N);
3210
3211       ---------------
3212       -- VADS_Size --
3213       ---------------
3214
3215       --  The processing for VADS_Size is shared with Size
3216
3217       ---------
3218       -- Val --
3219       ---------
3220
3221       --  For enumeration types with a standard representation, and for all
3222       --  other types, Val is handled by Gigi. For enumeration types with
3223       --  a non-standard representation we use the _Pos_To_Rep array that
3224       --  was created when the type was frozen.
3225
3226       when Attribute_Val => Val :
3227       declare
3228          Etyp : constant Entity_Id := Base_Type (Entity (Pref));
3229
3230       begin
3231          if Is_Enumeration_Type (Etyp)
3232            and then Present (Enum_Pos_To_Rep (Etyp))
3233          then
3234             Rewrite (N,
3235               Make_Indexed_Component (Loc,
3236                 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
3237                 Expressions => New_List (
3238                   Convert_To (Standard_Integer,
3239                     Relocate_Node (First (Exprs))))));
3240
3241             Analyze_And_Resolve (N, Typ);
3242          end if;
3243       end Val;
3244
3245       -----------
3246       -- Valid --
3247       -----------
3248
3249       --  The code for valid is dependent on the particular types involved.
3250       --  See separate sections below for the generated code in each case.
3251
3252       when Attribute_Valid => Valid :
3253       declare
3254          Ptyp : constant Entity_Id  := Etype (Pref);
3255          Btyp : Entity_Id := Base_Type (Ptyp);
3256          Tst  : Node_Id;
3257
3258          function Make_Range_Test return Node_Id;
3259          --  Build the code for a range test of the form
3260          --    Btyp!(Pref) >= Btyp!(Ptyp'First)
3261          --      and then
3262          --    Btyp!(Pref) <= Btyp!(Ptyp'Last)
3263
3264          function Make_Range_Test return Node_Id is
3265          begin
3266             return
3267               Make_And_Then (Loc,
3268                 Left_Opnd =>
3269                   Make_Op_Ge (Loc,
3270                     Left_Opnd =>
3271                       Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3272
3273                     Right_Opnd =>
3274                       Unchecked_Convert_To (Btyp,
3275                         Make_Attribute_Reference (Loc,
3276                           Prefix => New_Occurrence_Of (Ptyp, Loc),
3277                           Attribute_Name => Name_First))),
3278
3279                 Right_Opnd =>
3280                   Make_Op_Le (Loc,
3281                     Left_Opnd =>
3282                       Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3283
3284                     Right_Opnd =>
3285                       Unchecked_Convert_To (Btyp,
3286                         Make_Attribute_Reference (Loc,
3287                           Prefix => New_Occurrence_Of (Ptyp, Loc),
3288                           Attribute_Name => Name_Last))));
3289          end Make_Range_Test;
3290
3291       --  Start of processing for Attribute_Valid
3292
3293       begin
3294          --  Floating-point case. This case is handled by the Valid attribute
3295          --  code in the floating-point attribute run-time library.
3296
3297          if Is_Floating_Point_Type (Ptyp) then
3298             declare
3299                Rtp : constant Entity_Id := Root_Type (Etype (Pref));
3300
3301             begin
3302                Expand_Fpt_Attribute (N, Rtp, New_List (
3303                  Make_Attribute_Reference (Loc,
3304                    Prefix => Unchecked_Convert_To (Rtp, Pref),
3305                    Attribute_Name => Name_Unrestricted_Access)));
3306
3307                --  One more task, we still need a range check. Required
3308                --  only if we have a constraint, since the Valid routine
3309                --  catches infinities properly (infinities are never valid).
3310
3311                --  The way we do the range check is simply to create the
3312                --  expression: Valid (N) and then Base_Type(Pref) in Typ.
3313
3314                if not Subtypes_Statically_Match (Ptyp, Btyp) then
3315                   Rewrite (N,
3316                     Make_And_Then (Loc,
3317                       Left_Opnd  => Relocate_Node (N),
3318                       Right_Opnd =>
3319                         Make_In (Loc,
3320                           Left_Opnd => Convert_To (Btyp, Pref),
3321                           Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
3322                end if;
3323             end;
3324
3325          --  Enumeration type with holes
3326
3327          --  For enumeration types with holes, the Pos value constructed by
3328          --  the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3329          --  second argument of False returns minus one for an invalid value,
3330          --  and the non-negative pos value for a valid value, so the
3331          --  expansion of X'Valid is simply:
3332
3333          --     type(X)'Pos (X) >= 0
3334
3335          --  We can't quite generate it that way because of the requirement
3336          --  for the non-standard second argument of False, so we have to
3337          --  explicitly create:
3338
3339          --     _rep_to_pos (X, False) >= 0
3340
3341          --  If we have an enumeration subtype, we also check that the
3342          --  value is in range:
3343
3344          --    _rep_to_pos (X, False) >= 0
3345          --      and then
3346          --     (X >= type(X)'First and then type(X)'Last <= X)
3347
3348          elsif Is_Enumeration_Type (Ptyp)
3349            and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
3350          then
3351             Tst :=
3352               Make_Op_Ge (Loc,
3353                 Left_Opnd =>
3354                   Make_Function_Call (Loc,
3355                     Name =>
3356                       New_Reference_To
3357                         (TSS (Base_Type (Ptyp), Name_uRep_To_Pos), Loc),
3358                     Parameter_Associations => New_List (
3359                       Pref,
3360                       New_Occurrence_Of (Standard_False, Loc))),
3361                 Right_Opnd => Make_Integer_Literal (Loc, 0));
3362
3363             if Ptyp /= Btyp
3364               and then
3365                 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
3366                   or else
3367                  Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
3368             then
3369                --  The call to Make_Range_Test will create declarations
3370                --  that need a proper insertion point, but Pref is now
3371                --  attached to a node with no ancestor. Attach to tree
3372                --  even if it is to be rewritten below.
3373
3374                Set_Parent (Tst, Parent (N));
3375
3376                Tst :=
3377                  Make_And_Then (Loc,
3378                    Left_Opnd  => Make_Range_Test,
3379                    Right_Opnd => Tst);
3380             end if;
3381
3382             Rewrite (N, Tst);
3383
3384          --  Fortran convention booleans
3385
3386          --  For the very special case of Fortran convention booleans, the
3387          --  value is always valid, since it is an integer with the semantics
3388          --  that non-zero is true, and any value is permissible.
3389
3390          elsif Is_Boolean_Type (Ptyp)
3391            and then Convention (Ptyp) = Convention_Fortran
3392          then
3393             Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3394
3395          --  For biased representations, we will be doing an unchecked
3396          --  conversion without unbiasing the result. That means that
3397          --  the range test has to take this into account, and the
3398          --  proper form of the test is:
3399
3400          --    Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3401
3402          elsif Has_Biased_Representation (Ptyp) then
3403             Btyp := RTE (RE_Unsigned_32);
3404             Rewrite (N,
3405               Make_Op_Lt (Loc,
3406                 Left_Opnd =>
3407                   Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
3408                 Right_Opnd =>
3409                   Unchecked_Convert_To (Btyp,
3410                     Make_Attribute_Reference (Loc,
3411                       Prefix => New_Occurrence_Of (Ptyp, Loc),
3412                       Attribute_Name => Name_Range_Length))));
3413
3414          --  For all other scalar types, what we want logically is a
3415          --  range test:
3416
3417          --     X in type(X)'First .. type(X)'Last
3418
3419          --  But that's precisely what won't work because of possible
3420          --  unwanted optimization (and indeed the basic motivation for
3421          --  the Valid attribute -is exactly that this test does not work.
3422          --  What will work is:
3423
3424          --     Btyp!(X) >= Btyp!(type(X)'First)
3425          --       and then
3426          --     Btyp!(X) <= Btyp!(type(X)'Last)
3427
3428          --  where Btyp is an integer type large enough to cover the full
3429          --  range of possible stored values (i.e. it is chosen on the basis
3430          --  of the size of the type, not the range of the values). We write
3431          --  this as two tests, rather than a range check, so that static
3432          --  evaluation will easily remove either or both of the checks if
3433          --  they can be -statically determined to be true (this happens
3434          --  when the type of X is static and the range extends to the full
3435          --  range of stored values).
3436
3437          --  Unsigned types. Note: it is safe to consider only whether the
3438          --  subtype is unsigned, since we will in that case be doing all
3439          --  unsigned comparisons based on the subtype range. Since we use
3440          --  the actual subtype object size, this is appropriate.
3441
3442          --  For example, if we have
3443
3444          --    subtype x is integer range 1 .. 200;
3445          --    for x'Object_Size use 8;
3446
3447          --  Now the base type is signed, but objects of this type are 8
3448          --  bits unsigned, and doing an unsigned test of the range 1 to
3449          --  200 is correct, even though a value greater than 127 looks
3450          --  signed to a signed comparison.
3451
3452          elsif Is_Unsigned_Type (Ptyp) then
3453             if Esize (Ptyp) <= 32 then
3454                Btyp := RTE (RE_Unsigned_32);
3455             else
3456                Btyp := RTE (RE_Unsigned_64);
3457             end if;
3458
3459             Rewrite (N, Make_Range_Test);
3460
3461          --  Signed types
3462
3463          else
3464             if Esize (Ptyp) <= Esize (Standard_Integer) then
3465                Btyp := Standard_Integer;
3466             else
3467                Btyp := Universal_Integer;
3468             end if;
3469
3470             Rewrite (N, Make_Range_Test);
3471          end if;
3472
3473          Analyze_And_Resolve (N, Standard_Boolean);
3474       end Valid;
3475
3476       -----------
3477       -- Value --
3478       -----------
3479
3480       --  Value attribute is handled in separate unti Exp_Imgv
3481
3482       when Attribute_Value =>
3483          Exp_Imgv.Expand_Value_Attribute (N);
3484
3485       -----------------
3486       -- Value_Size --
3487       -----------------
3488
3489       --  The processing for Value_Size shares the processing for Size
3490
3491       -------------
3492       -- Version --
3493       -------------
3494
3495       --  The processing for Version shares the processing for Body_Version
3496
3497       ----------------
3498       -- Wide_Image --
3499       ----------------
3500
3501       --  We expand typ'Wide_Image (X) into
3502
3503       --    String_To_Wide_String
3504       --      (typ'Image (X), Wide_Character_Encoding_Method)
3505
3506       --  This works in all cases because String_To_Wide_String converts any
3507       --  wide character escape sequences resulting from the Image call to the
3508       --  proper Wide_Character equivalent
3509
3510       --  not quite right for typ = Wide_Character ???
3511
3512       when Attribute_Wide_Image => Wide_Image :
3513       begin
3514          Rewrite (N,
3515            Make_Function_Call (Loc,
3516              Name => New_Reference_To (RTE (RE_String_To_Wide_String), Loc),
3517              Parameter_Associations => New_List (
3518                Make_Attribute_Reference (Loc,
3519                  Prefix         => Pref,
3520                  Attribute_Name => Name_Image,
3521                  Expressions    => Exprs),
3522
3523                Make_Integer_Literal (Loc,
3524                  Intval => Int (Wide_Character_Encoding_Method)))));
3525
3526          Analyze_And_Resolve (N, Standard_Wide_String);
3527       end Wide_Image;
3528
3529       ----------------
3530       -- Wide_Value --
3531       ----------------
3532
3533       --  We expand typ'Wide_Value (X) into
3534
3535       --    typ'Value
3536       --      (Wide_String_To_String (X, Wide_Character_Encoding_Method))
3537
3538       --  Wide_String_To_String is a runtime function that converts its wide
3539       --  string argument to String, converting any non-translatable characters
3540       --  into appropriate escape sequences. This preserves the required
3541       --  semantics of Wide_Value in all cases, and results in a very simple
3542       --  implementation approach.
3543
3544       --  It's not quite right where typ = Wide_Character, because the encoding
3545       --  method may not cover the whole character type ???
3546
3547       when Attribute_Wide_Value => Wide_Value :
3548       begin
3549          Rewrite (N,
3550            Make_Attribute_Reference (Loc,
3551              Prefix         => Pref,
3552              Attribute_Name => Name_Value,
3553
3554              Expressions    => New_List (
3555                Make_Function_Call (Loc,
3556                  Name =>
3557                    New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
3558
3559                  Parameter_Associations => New_List (
3560                    Relocate_Node (First (Exprs)),
3561                    Make_Integer_Literal (Loc,
3562                      Intval => Int (Wide_Character_Encoding_Method)))))));
3563
3564          Analyze_And_Resolve (N, Typ);
3565       end Wide_Value;
3566
3567       ----------------
3568       -- Wide_Width --
3569       ----------------
3570
3571       --  Wide_Width attribute is handled in separate unit Exp_Imgv
3572
3573       when Attribute_Wide_Width =>
3574          Exp_Imgv.Expand_Width_Attribute (N, Wide => True);
3575
3576       -----------
3577       -- Width --
3578       -----------
3579
3580       --  Width attribute is handled in separate unit Exp_Imgv
3581
3582       when Attribute_Width =>
3583          Exp_Imgv.Expand_Width_Attribute (N, Wide => False);
3584
3585       -----------
3586       -- Write --
3587       -----------
3588
3589       when Attribute_Write => Write : declare
3590          P_Type : constant Entity_Id := Entity (Pref);
3591          U_Type : constant Entity_Id := Underlying_Type (P_Type);
3592          Pname  : Entity_Id;
3593          Decl   : Node_Id;
3594          Prag   : Node_Id;
3595          Arg3   : Node_Id;
3596          Wfunc  : Node_Id;
3597
3598       begin
3599          --  If no underlying type, we have an error that will be diagnosed
3600          --  elsewhere, so here we just completely ignore the expansion.
3601
3602          if No (U_Type) then
3603             return;
3604          end if;
3605
3606          --  The simple case, if there is a TSS for Write, just call it
3607
3608          Pname := Find_Inherited_TSS (P_Type, Name_uWrite);
3609
3610          if Present (Pname) then
3611             null;
3612
3613          else
3614             --  If there is a Stream_Convert pragma, use it, we rewrite
3615
3616             --     sourcetyp'Output (stream, Item)
3617
3618             --  as
3619
3620             --     strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3621
3622             --  where strmwrite is the given Write function that converts
3623             --  an argument of type sourcetyp or a type acctyp, from which
3624             --  it is derived to type strmtyp. The conversion to acttyp is
3625             --  required for the derived case.
3626
3627             Prag :=
3628               Get_Rep_Pragma
3629                 (Implementation_Base_Type (P_Type), Name_Stream_Convert);
3630
3631             if Present (Prag) then
3632                Arg3 :=
3633                  Next (Next (First (Pragma_Argument_Associations (Prag))));
3634                Wfunc := Entity (Expression (Arg3));
3635
3636                Rewrite (N,
3637                  Make_Attribute_Reference (Loc,
3638                    Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3639                    Attribute_Name => Name_Output,
3640                    Expressions => New_List (
3641                      Relocate_Node (First (Exprs)),
3642                      Make_Function_Call (Loc,
3643                        Name => New_Occurrence_Of (Wfunc, Loc),
3644                        Parameter_Associations => New_List (
3645                          Convert_To (Etype (First_Formal (Wfunc)),
3646                            Relocate_Node (Next (First (Exprs)))))))));
3647
3648                Analyze (N);
3649                return;
3650
3651             --  For elementary types, we call the W_xxx routine directly
3652
3653             elsif Is_Elementary_Type (U_Type) then
3654                Rewrite (N, Build_Elementary_Write_Call (N));
3655                Analyze (N);
3656                return;
3657
3658             --  Array type case
3659
3660             elsif Is_Array_Type (U_Type) then
3661                Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
3662                Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3663
3664             --  Tagged type case, use the primitive Write function. Note that
3665             --  this will dispatch in the class-wide case which is what we want
3666
3667             elsif Is_Tagged_Type (U_Type) then
3668                Pname := Find_Prim_Op (U_Type, Name_uWrite);
3669
3670             --  All other record type cases, including protected records.
3671             --  The latter only arise for expander generated code for
3672             --  handling shared passive partition access.
3673
3674             else
3675                pragma Assert
3676                  (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3677
3678                if Has_Discriminants (U_Type)
3679                  and then Present
3680                    (Discriminant_Default_Value (First_Discriminant (U_Type)))
3681                then
3682                   Build_Mutable_Record_Write_Procedure
3683                     (Loc, Base_Type (U_Type), Decl, Pname);
3684
3685                else
3686                   Build_Record_Write_Procedure
3687                     (Loc, Base_Type (U_Type), Decl, Pname);
3688                end if;
3689
3690                Insert_Action (N, Decl);
3691             end if;
3692          end if;
3693
3694          --  If we fall through, Pname is the procedure to be called
3695
3696          Rewrite_Stream_Proc_Call (Pname);
3697       end Write;
3698
3699       --  Component_Size is handled by Gigi, unless the component size is
3700       --  known at compile time, which is always true in the packed array
3701       --  case. It is important that the packed array case is handled in
3702       --  the front end (see Eval_Attribute) since Gigi would otherwise
3703       --  get confused by the equivalent packed array type.
3704
3705       when Attribute_Component_Size =>
3706          null;
3707
3708       --  The following attributes are handled by Gigi (except that static
3709       --  cases have already been evaluated by the semantics, but in any
3710       --  case Gigi should not count on that).
3711
3712       --  In addition Gigi handles the non-floating-point cases of Pred
3713       --  and Succ (including the fixed-point cases, which can just be
3714       --  treated as integer increment/decrement operations)
3715
3716       --  Gigi also handles the non-class-wide cases of Size
3717
3718       when Attribute_Bit_Order                    |
3719            Attribute_Code_Address                 |
3720            Attribute_Definite                     |
3721            Attribute_Max                          |
3722            Attribute_Mechanism_Code               |
3723            Attribute_Min                          |
3724            Attribute_Null_Parameter               |
3725            Attribute_Passed_By_Reference          =>
3726          null;
3727
3728       --  The following attributes are also handled by Gigi, but return a
3729       --  universal integer result, so may need a conversion for checking
3730       --  that the result is in range.
3731
3732       when Attribute_Aft                          |
3733            Attribute_Alignment                    |
3734            Attribute_Bit                          |
3735            Attribute_Max_Size_In_Storage_Elements
3736       =>
3737          Apply_Universal_Integer_Attribute_Checks (N);
3738
3739       --  The following attributes should not appear at this stage, since they
3740       --  have already been handled by the analyzer (and properly rewritten
3741       --  with corresponding values or entities to represent the right values)
3742
3743       when Attribute_Abort_Signal                 |
3744            Attribute_Address_Size                 |
3745            Attribute_Base                         |
3746            Attribute_Class                        |
3747            Attribute_Default_Bit_Order            |
3748            Attribute_Delta                        |
3749            Attribute_Denorm                       |
3750            Attribute_Digits                       |
3751            Attribute_Emax                         |
3752            Attribute_Epsilon                      |
3753            Attribute_Has_Discriminants            |
3754            Attribute_Large                        |
3755            Attribute_Machine_Emax                 |
3756            Attribute_Machine_Emin                 |
3757            Attribute_Machine_Mantissa             |
3758            Attribute_Machine_Overflows            |
3759            Attribute_Machine_Radix                |
3760            Attribute_Machine_Rounds               |
3761            Attribute_Maximum_Alignment            |
3762            Attribute_Model_Emin                   |
3763            Attribute_Model_Epsilon                |
3764            Attribute_Model_Mantissa               |
3765            Attribute_Model_Small                  |
3766            Attribute_Modulus                      |
3767            Attribute_Partition_ID                 |
3768            Attribute_Range                        |
3769            Attribute_Safe_Emax                    |
3770            Attribute_Safe_First                   |
3771            Attribute_Safe_Large                   |
3772            Attribute_Safe_Last                    |
3773            Attribute_Safe_Small                   |
3774            Attribute_Scale                        |
3775            Attribute_Signed_Zeros                 |
3776            Attribute_Small                        |
3777            Attribute_Storage_Unit                 |
3778            Attribute_Type_Class                   |
3779            Attribute_Universal_Literal_String     |
3780            Attribute_Wchar_T_Size                 |
3781            Attribute_Word_Size                    =>
3782
3783          raise Program_Error;
3784
3785       --  The Asm_Input and Asm_Output attributes are not expanded at this
3786       --  stage, but will be eliminated in the expansion of the Asm call,
3787       --  see Exp_Intr for details. So Gigi will never see these either.
3788
3789       when Attribute_Asm_Input                    |
3790            Attribute_Asm_Output                   =>
3791
3792          null;
3793
3794       end case;
3795
3796    end Expand_N_Attribute_Reference;
3797
3798    ----------------------
3799    -- Expand_Pred_Succ --
3800    ----------------------
3801
3802    --  For typ'Pred (exp), we generate the check
3803
3804    --    [constraint_error when exp = typ'Base'First]
3805
3806    --  Similarly, for typ'Succ (exp), we generate the check
3807
3808    --    [constraint_error when exp = typ'Base'Last]
3809
3810    --  These checks are not generated for modular types, since the proper
3811    --  semantics for Succ and Pred on modular types is to wrap, not raise CE.
3812
3813    procedure Expand_Pred_Succ (N : Node_Id) is
3814       Loc  : constant Source_Ptr := Sloc (N);
3815       Cnam : Name_Id;
3816
3817    begin
3818       if Attribute_Name (N) = Name_Pred then
3819          Cnam := Name_First;
3820       else
3821          Cnam := Name_Last;
3822       end if;
3823
3824       Insert_Action (N,
3825         Make_Raise_Constraint_Error (Loc,
3826           Condition =>
3827             Make_Op_Eq (Loc,
3828               Left_Opnd => Duplicate_Subexpr (First (Expressions (N))),
3829               Right_Opnd =>
3830                 Make_Attribute_Reference (Loc,
3831                   Prefix =>
3832                     New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
3833                   Attribute_Name => Cnam)),
3834           Reason => CE_Overflow_Check_Failed));
3835
3836    end Expand_Pred_Succ;
3837
3838    ------------------------
3839    -- Find_Inherited_TSS --
3840    ------------------------
3841
3842    function Find_Inherited_TSS
3843      (Typ : Entity_Id;
3844       Nam : Name_Id) return Entity_Id
3845    is
3846       P_Type : Entity_Id := Typ;
3847       Proc   : Entity_Id;
3848
3849    begin
3850       Proc :=  TSS (Base_Type (Typ), Nam);
3851
3852       --  Check first if there is a TSS given for the type itself.
3853
3854       if Present (Proc) then
3855          return Proc;
3856       end if;
3857
3858       --  If Typ is a derived type, it may inherit attributes from some
3859       --  ancestor which is not the ultimate underlying one.
3860       --  If Typ is a derived tagged type, the corresponding primitive
3861       --  operation has been created explicitly.
3862
3863       if Is_Derived_Type (P_Type) then
3864          if Is_Tagged_Type (P_Type) then
3865             return Find_Prim_Op (P_Type, Nam);
3866          else
3867             while Is_Derived_Type (P_Type) loop
3868                Proc :=  TSS (Base_Type (Etype (Typ)), Nam);
3869
3870                if Present (Proc) then
3871                   return Proc;
3872                else
3873                   P_Type := Base_Type (Etype (P_Type));
3874                end if;
3875             end loop;
3876          end if;
3877       end if;
3878
3879       --  If nothing else, use the TSS of the root type.
3880
3881       return TSS (Base_Type (Underlying_Type (Typ)), Nam);
3882    end Find_Inherited_TSS;
3883
3884    -----------------------
3885    -- Get_Index_Subtype --
3886    -----------------------
3887
3888    function Get_Index_Subtype (N : Node_Id) return Node_Id is
3889       P_Type : Entity_Id := Etype (Prefix (N));
3890       Indx   : Node_Id;
3891       J      : Int;
3892
3893    begin
3894       if Is_Access_Type (P_Type) then
3895          P_Type := Designated_Type (P_Type);
3896       end if;
3897
3898       if No (Expressions (N)) then
3899          J := 1;
3900       else
3901          J := UI_To_Int (Expr_Value (First (Expressions (N))));
3902       end if;
3903
3904       Indx := First_Index (P_Type);
3905       while J > 1 loop
3906          Next_Index (Indx);
3907          J := J - 1;
3908       end loop;
3909
3910       return Etype (Indx);
3911    end Get_Index_Subtype;
3912
3913    ---------------------------------
3914    -- Is_Constrained_Packed_Array --
3915    ---------------------------------
3916
3917    function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
3918       Arr : Entity_Id := Typ;
3919
3920    begin
3921       if Is_Access_Type (Arr) then
3922          Arr := Designated_Type (Arr);
3923       end if;
3924
3925       return Is_Array_Type (Arr)
3926         and then Is_Constrained (Arr)
3927         and then Present (Packed_Array_Type (Arr));
3928    end Is_Constrained_Packed_Array;
3929
3930 end Exp_Attr;