OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [exp_attr.adb] - Blame information for rev 473

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.