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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [exp_ch3.adb] - Blame information for rev 717

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

Line No. Rev Author Line
1 706 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                              E X P _ C H 3                               --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2012, 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.  See the GNU General Public License --
17
-- for  more details.  You should have  received  a copy of the GNU General --
18
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19
-- http://www.gnu.org/licenses for a complete copy of the license.          --
20
--                                                                          --
21
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23
--                                                                          --
24
------------------------------------------------------------------------------
25
 
26
with Atree;    use Atree;
27
with Checks;   use Checks;
28
with Einfo;    use Einfo;
29
with Errout;   use Errout;
30
with Exp_Aggr; use Exp_Aggr;
31
with Exp_Atag; use Exp_Atag;
32
with Exp_Ch4;  use Exp_Ch4;
33
with Exp_Ch6;  use Exp_Ch6;
34
with Exp_Ch7;  use Exp_Ch7;
35
with Exp_Ch9;  use Exp_Ch9;
36
with Exp_Ch11; use Exp_Ch11;
37
with Exp_Dbug; use Exp_Dbug;
38
with Exp_Disp; use Exp_Disp;
39
with Exp_Dist; use Exp_Dist;
40
with Exp_Smem; use Exp_Smem;
41
with Exp_Strm; use Exp_Strm;
42
with Exp_Tss;  use Exp_Tss;
43
with Exp_Util; use Exp_Util;
44
with Freeze;   use Freeze;
45
with Namet;    use Namet;
46
with Nlists;   use Nlists;
47
with Nmake;    use Nmake;
48
with Opt;      use Opt;
49
with Restrict; use Restrict;
50
with Rident;   use Rident;
51
with Rtsfind;  use Rtsfind;
52
with Sem;      use Sem;
53
with Sem_Aux;  use Sem_Aux;
54
with Sem_Attr; use Sem_Attr;
55
with Sem_Cat;  use Sem_Cat;
56
with Sem_Ch3;  use Sem_Ch3;
57
with Sem_Ch6;  use Sem_Ch6;
58
with Sem_Ch8;  use Sem_Ch8;
59
with Sem_Disp; use Sem_Disp;
60
with Sem_Eval; use Sem_Eval;
61
with Sem_Mech; use Sem_Mech;
62
with Sem_Res;  use Sem_Res;
63
with Sem_SCIL; use Sem_SCIL;
64
with Sem_Type; use Sem_Type;
65
with Sem_Util; use Sem_Util;
66
with Sinfo;    use Sinfo;
67
with Stand;    use Stand;
68
with Snames;   use Snames;
69
with Targparm; use Targparm;
70
with Tbuild;   use Tbuild;
71
with Ttypes;   use Ttypes;
72
with Validsw;  use Validsw;
73
 
74
package body Exp_Ch3 is
75
 
76
   -----------------------
77
   -- Local Subprograms --
78
   -----------------------
79
 
80
   procedure Adjust_Discriminants (Rtype : Entity_Id);
81
   --  This is used when freezing a record type. It attempts to construct
82
   --  more restrictive subtypes for discriminants so that the max size of
83
   --  the record can be calculated more accurately. See the body of this
84
   --  procedure for details.
85
 
86
   procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
87
   --  Build initialization procedure for given array type. Nod is a node
88
   --  used for attachment of any actions required in its construction.
89
   --  It also supplies the source location used for the procedure.
90
 
91
   function Build_Discriminant_Formals
92
     (Rec_Id : Entity_Id;
93
      Use_Dl : Boolean) return List_Id;
94
   --  This function uses the discriminants of a type to build a list of
95
   --  formal parameters, used in Build_Init_Procedure among other places.
96
   --  If the flag Use_Dl is set, the list is built using the already
97
   --  defined discriminals of the type, as is the case for concurrent
98
   --  types with discriminants. Otherwise new identifiers are created,
99
   --  with the source names of the discriminants.
100
 
101
   function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
102
   --  This function builds a static aggregate that can serve as the initial
103
   --  value for an array type whose bounds are static, and whose component
104
   --  type is a composite type that has a static equivalent aggregate.
105
   --  The equivalent array aggregate is used both for object initialization
106
   --  and for component initialization, when used in the following function.
107
 
108
   function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
109
   --  This function builds a static aggregate that can serve as the initial
110
   --  value for a record type whose components are scalar and initialized
111
   --  with compile-time values, or arrays with similar initialization or
112
   --  defaults. When possible, initialization of an object of the type can
113
   --  be achieved by using a copy of the aggregate as an initial value, thus
114
   --  removing the implicit call that would otherwise constitute elaboration
115
   --  code.
116
 
117
   procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
118
   --  Build record initialization procedure. N is the type declaration
119
   --  node, and Rec_Ent is the corresponding entity for the record type.
120
 
121
   procedure Build_Slice_Assignment (Typ : Entity_Id);
122
   --  Build assignment procedure for one-dimensional arrays of controlled
123
   --  types. Other array and slice assignments are expanded in-line, but
124
   --  the code expansion for controlled components (when control actions
125
   --  are active) can lead to very large blocks that GCC3 handles poorly.
126
 
127
   procedure Build_Untagged_Equality (Typ : Entity_Id);
128
   --  AI05-0123: Equality on untagged records composes. This procedure
129
   --  builds the equality routine for an untagged record that has components
130
   --  of a record type that has user-defined primitive equality operations.
131
   --  The resulting operation is a TSS subprogram.
132
 
133
   procedure Build_Variant_Record_Equality (Typ  : Entity_Id);
134
   --  Create An Equality function for the non-tagged variant record 'Typ'
135
   --  and attach it to the TSS list
136
 
137
   procedure Check_Stream_Attributes (Typ : Entity_Id);
138
   --  Check that if a limited extension has a parent with user-defined stream
139
   --  attributes, and does not itself have user-defined stream-attributes,
140
   --  then any limited component of the extension also has the corresponding
141
   --  user-defined stream attributes.
142
 
143
   procedure Clean_Task_Names
144
     (Typ     : Entity_Id;
145
      Proc_Id : Entity_Id);
146
   --  If an initialization procedure includes calls to generate names
147
   --  for task subcomponents, indicate that secondary stack cleanup is
148
   --  needed after an initialization. Typ is the component type, and Proc_Id
149
   --  the initialization procedure for the enclosing composite type.
150
 
151
   procedure Expand_Tagged_Root (T : Entity_Id);
152
   --  Add a field _Tag at the beginning of the record. This field carries
153
   --  the value of the access to the Dispatch table. This procedure is only
154
   --  called on root type, the _Tag field being inherited by the descendants.
155
 
156
   procedure Expand_Freeze_Array_Type (N : Node_Id);
157
   --  Freeze an array type. Deals with building the initialization procedure,
158
   --  creating the packed array type for a packed array and also with the
159
   --  creation of the controlling procedures for the controlled case. The
160
   --  argument N is the N_Freeze_Entity node for the type.
161
 
162
   procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
163
   --  Freeze a class-wide type. Build routine Finalize_Address for the purpose
164
   --  of finalizing controlled derivations from the class-wide's root type.
165
 
166
   procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
167
   --  Freeze enumeration type with non-standard representation. Builds the
168
   --  array and function needed to convert between enumeration pos and
169
   --  enumeration representation values. N is the N_Freeze_Entity node
170
   --  for the type.
171
 
172
   procedure Expand_Freeze_Record_Type (N : Node_Id);
173
   --  Freeze record type. Builds all necessary discriminant checking
174
   --  and other ancillary functions, and builds dispatch tables where
175
   --  needed. The argument N is the N_Freeze_Entity node. This processing
176
   --  applies only to E_Record_Type entities, not to class wide types,
177
   --  record subtypes, or private types.
178
 
179
   procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
180
   --  Treat user-defined stream operations as renaming_as_body if the
181
   --  subprogram they rename is not frozen when the type is frozen.
182
 
183
   procedure Initialization_Warning (E : Entity_Id);
184
   --  If static elaboration of the package is requested, indicate
185
   --  when a type does meet the conditions for static initialization. If
186
   --  E is a type, it has components that have no static initialization.
187
   --  if E is an entity, its initial expression is not compile-time known.
188
 
189
   function Init_Formals (Typ : Entity_Id) return List_Id;
190
   --  This function builds the list of formals for an initialization routine.
191
   --  The first formal is always _Init with the given type. For task value
192
   --  record types and types containing tasks, three additional formals are
193
   --  added:
194
   --
195
   --    _Master    : Master_Id
196
   --    _Chain     : in out Activation_Chain
197
   --    _Task_Name : String
198
   --
199
   --  The caller must append additional entries for discriminants if required.
200
 
201
   function In_Runtime (E : Entity_Id) return Boolean;
202
   --  Check if E is defined in the RTL (in a child of Ada or System). Used
203
   --  to avoid to bring in the overhead of _Input, _Output for tagged types.
204
 
205
   function Is_Variable_Size_Array (E : Entity_Id) return Boolean;
206
   --  Returns true if E has variable size components
207
 
208
   function Is_Variable_Size_Record (E : Entity_Id) return Boolean;
209
   --  Returns true if E has variable size components
210
 
211
   function Make_Eq_Body
212
     (Typ     : Entity_Id;
213
      Eq_Name : Name_Id) return Node_Id;
214
   --  Build the body of a primitive equality operation for a tagged record
215
   --  type, or in Ada 2012 for any record type that has components with a
216
   --  user-defined equality. Factored out of Predefined_Primitive_Bodies.
217
 
218
   function Make_Eq_Case
219
     (E     : Entity_Id;
220
      CL    : Node_Id;
221
      Discr : Entity_Id := Empty) return List_Id;
222
   --  Building block for variant record equality. Defined to share the code
223
   --  between the tagged and non-tagged case. Given a Component_List node CL,
224
   --  it generates an 'if' followed by a 'case' statement that compares all
225
   --  components of local temporaries named X and Y (that are declared as
226
   --  formals at some upper level). E provides the Sloc to be used for the
227
   --  generated code. Discr is used as the case statement switch in the case
228
   --  of Unchecked_Union equality.
229
 
230
   function Make_Eq_If
231
     (E : Entity_Id;
232
      L : List_Id) return Node_Id;
233
   --  Building block for variant record equality. Defined to share the code
234
   --  between the tagged and non-tagged case. Given the list of components
235
   --  (or discriminants) L, it generates a return statement that compares all
236
   --  components of local temporaries named X and Y (that are declared as
237
   --  formals at some upper level). E provides the Sloc to be used for the
238
   --  generated code.
239
 
240
   procedure Make_Predefined_Primitive_Specs
241
     (Tag_Typ     : Entity_Id;
242
      Predef_List : out List_Id;
243
      Renamed_Eq  : out Entity_Id);
244
   --  Create a list with the specs of the predefined primitive operations.
245
   --  For tagged types that are interfaces all these primitives are defined
246
   --  abstract.
247
   --
248
   --  The following entries are present for all tagged types, and provide
249
   --  the results of the corresponding attribute applied to the object.
250
   --  Dispatching is required in general, since the result of the attribute
251
   --  will vary with the actual object subtype.
252
   --
253
   --     _size          provides result of 'Size attribute
254
   --     typSR          provides result of 'Read attribute
255
   --     typSW          provides result of 'Write attribute
256
   --     typSI          provides result of 'Input attribute
257
   --     typSO          provides result of 'Output attribute
258
   --
259
   --  The following entries are additionally present for non-limited tagged
260
   --  types, and implement additional dispatching operations for predefined
261
   --  operations:
262
   --
263
   --     _equality      implements "=" operator
264
   --     _assign        implements assignment operation
265
   --     typDF          implements deep finalization
266
   --     typDA          implements deep adjust
267
   --
268
   --  The latter two are empty procedures unless the type contains some
269
   --  controlled components that require finalization actions (the deep
270
   --  in the name refers to the fact that the action applies to components).
271
   --
272
   --  The list is returned in Predef_List. The Parameter Renamed_Eq either
273
   --  returns the value Empty, or else the defining unit name for the
274
   --  predefined equality function in the case where the type has a primitive
275
   --  operation that is a renaming of predefined equality (but only if there
276
   --  is also an overriding user-defined equality function). The returned
277
   --  Renamed_Eq will be passed to the corresponding parameter of
278
   --  Predefined_Primitive_Bodies.
279
 
280
   function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
281
   --  returns True if there are representation clauses for type T that are not
282
   --  inherited. If the result is false, the init_proc and the discriminant
283
   --  checking functions of the parent can be reused by a derived type.
284
 
285
   procedure Make_Controlling_Function_Wrappers
286
     (Tag_Typ   : Entity_Id;
287
      Decl_List : out List_Id;
288
      Body_List : out List_Id);
289
   --  Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
290
   --  associated with inherited functions with controlling results which
291
   --  are not overridden. The body of each wrapper function consists solely
292
   --  of a return statement whose expression is an extension aggregate
293
   --  invoking the inherited subprogram's parent subprogram and extended
294
   --  with a null association list.
295
 
296
   function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
297
   --  Ada 2005 (AI-251): Makes specs for null procedures associated with any
298
   --  null procedures inherited from an interface type that have not been
299
   --  overridden. Only one null procedure will be created for a given set of
300
   --  inherited null procedures with homographic profiles.
301
 
302
   function Predef_Spec_Or_Body
303
     (Loc      : Source_Ptr;
304
      Tag_Typ  : Entity_Id;
305
      Name     : Name_Id;
306
      Profile  : List_Id;
307
      Ret_Type : Entity_Id := Empty;
308
      For_Body : Boolean   := False) return Node_Id;
309
   --  This function generates the appropriate expansion for a predefined
310
   --  primitive operation specified by its name, parameter profile and
311
   --  return type (Empty means this is a procedure). If For_Body is false,
312
   --  then the returned node is a subprogram declaration. If For_Body is
313
   --  true, then the returned node is a empty subprogram body containing
314
   --  no declarations and no statements.
315
 
316
   function Predef_Stream_Attr_Spec
317
     (Loc      : Source_Ptr;
318
      Tag_Typ  : Entity_Id;
319
      Name     : TSS_Name_Type;
320
      For_Body : Boolean := False) return Node_Id;
321
   --  Specialized version of Predef_Spec_Or_Body that apply to read, write,
322
   --  input and output attribute whose specs are constructed in Exp_Strm.
323
 
324
   function Predef_Deep_Spec
325
     (Loc      : Source_Ptr;
326
      Tag_Typ  : Entity_Id;
327
      Name     : TSS_Name_Type;
328
      For_Body : Boolean := False) return Node_Id;
329
   --  Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
330
   --  and _deep_finalize
331
 
332
   function Predefined_Primitive_Bodies
333
     (Tag_Typ    : Entity_Id;
334
      Renamed_Eq : Entity_Id) return List_Id;
335
   --  Create the bodies of the predefined primitives that are described in
336
   --  Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
337
   --  the defining unit name of the type's predefined equality as returned
338
   --  by Make_Predefined_Primitive_Specs.
339
 
340
   function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
341
   --  Freeze entities of all predefined primitive operations. This is needed
342
   --  because the bodies of these operations do not normally do any freezing.
343
 
344
   function Stream_Operation_OK
345
     (Typ       : Entity_Id;
346
      Operation : TSS_Name_Type) return Boolean;
347
   --  Check whether the named stream operation must be emitted for a given
348
   --  type. The rules for inheritance of stream attributes by type extensions
349
   --  are enforced by this function. Furthermore, various restrictions prevent
350
   --  the generation of these operations, as a useful optimization or for
351
   --  certification purposes.
352
 
353
   --------------------------
354
   -- Adjust_Discriminants --
355
   --------------------------
356
 
357
   --  This procedure attempts to define subtypes for discriminants that are
358
   --  more restrictive than those declared. Such a replacement is possible if
359
   --  we can demonstrate that values outside the restricted range would cause
360
   --  constraint errors in any case. The advantage of restricting the
361
   --  discriminant types in this way is that the maximum size of the variant
362
   --  record can be calculated more conservatively.
363
 
364
   --  An example of a situation in which we can perform this type of
365
   --  restriction is the following:
366
 
367
   --    subtype B is range 1 .. 10;
368
   --    type Q is array (B range <>) of Integer;
369
 
370
   --    type V (N : Natural) is record
371
   --       C : Q (1 .. N);
372
   --    end record;
373
 
374
   --  In this situation, we can restrict the upper bound of N to 10, since
375
   --  any larger value would cause a constraint error in any case.
376
 
377
   --  There are many situations in which such restriction is possible, but
378
   --  for now, we just look for cases like the above, where the component
379
   --  in question is a one dimensional array whose upper bound is one of
380
   --  the record discriminants. Also the component must not be part of
381
   --  any variant part, since then the component does not always exist.
382
 
383
   procedure Adjust_Discriminants (Rtype : Entity_Id) is
384
      Loc   : constant Source_Ptr := Sloc (Rtype);
385
      Comp  : Entity_Id;
386
      Ctyp  : Entity_Id;
387
      Ityp  : Entity_Id;
388
      Lo    : Node_Id;
389
      Hi    : Node_Id;
390
      P     : Node_Id;
391
      Loval : Uint;
392
      Discr : Entity_Id;
393
      Dtyp  : Entity_Id;
394
      Dhi   : Node_Id;
395
      Dhiv  : Uint;
396
      Ahi   : Node_Id;
397
      Ahiv  : Uint;
398
      Tnn   : Entity_Id;
399
 
400
   begin
401
      Comp := First_Component (Rtype);
402
      while Present (Comp) loop
403
 
404
         --  If our parent is a variant, quit, we do not look at components
405
         --  that are in variant parts, because they may not always exist.
406
 
407
         P := Parent (Comp);   -- component declaration
408
         P := Parent (P);      -- component list
409
 
410
         exit when Nkind (Parent (P)) = N_Variant;
411
 
412
         --  We are looking for a one dimensional array type
413
 
414
         Ctyp := Etype (Comp);
415
 
416
         if not Is_Array_Type (Ctyp)
417
           or else Number_Dimensions (Ctyp) > 1
418
         then
419
            goto Continue;
420
         end if;
421
 
422
         --  The lower bound must be constant, and the upper bound is a
423
         --  discriminant (which is a discriminant of the current record).
424
 
425
         Ityp := Etype (First_Index (Ctyp));
426
         Lo := Type_Low_Bound (Ityp);
427
         Hi := Type_High_Bound (Ityp);
428
 
429
         if not Compile_Time_Known_Value (Lo)
430
           or else Nkind (Hi) /= N_Identifier
431
           or else No (Entity (Hi))
432
           or else Ekind (Entity (Hi)) /= E_Discriminant
433
         then
434
            goto Continue;
435
         end if;
436
 
437
         --  We have an array with appropriate bounds
438
 
439
         Loval := Expr_Value (Lo);
440
         Discr := Entity (Hi);
441
         Dtyp  := Etype (Discr);
442
 
443
         --  See if the discriminant has a known upper bound
444
 
445
         Dhi := Type_High_Bound (Dtyp);
446
 
447
         if not Compile_Time_Known_Value (Dhi) then
448
            goto Continue;
449
         end if;
450
 
451
         Dhiv := Expr_Value (Dhi);
452
 
453
         --  See if base type of component array has known upper bound
454
 
455
         Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
456
 
457
         if not Compile_Time_Known_Value (Ahi) then
458
            goto Continue;
459
         end if;
460
 
461
         Ahiv := Expr_Value (Ahi);
462
 
463
         --  The condition for doing the restriction is that the high bound
464
         --  of the discriminant is greater than the low bound of the array,
465
         --  and is also greater than the high bound of the base type index.
466
 
467
         if Dhiv > Loval and then Dhiv > Ahiv then
468
 
469
            --  We can reset the upper bound of the discriminant type to
470
            --  whichever is larger, the low bound of the component, or
471
            --  the high bound of the base type array index.
472
 
473
            --  We build a subtype that is declared as
474
 
475
            --     subtype Tnn is discr_type range discr_type'First .. max;
476
 
477
            --  And insert this declaration into the tree. The type of the
478
            --  discriminant is then reset to this more restricted subtype.
479
 
480
            Tnn := Make_Temporary (Loc, 'T');
481
 
482
            Insert_Action (Declaration_Node (Rtype),
483
              Make_Subtype_Declaration (Loc,
484
                Defining_Identifier => Tnn,
485
                Subtype_Indication =>
486
                  Make_Subtype_Indication (Loc,
487
                    Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
488
                    Constraint   =>
489
                      Make_Range_Constraint (Loc,
490
                        Range_Expression =>
491
                          Make_Range (Loc,
492
                            Low_Bound =>
493
                              Make_Attribute_Reference (Loc,
494
                                Attribute_Name => Name_First,
495
                                Prefix => New_Occurrence_Of (Dtyp, Loc)),
496
                            High_Bound =>
497
                              Make_Integer_Literal (Loc,
498
                                Intval => UI_Max (Loval, Ahiv)))))));
499
 
500
            Set_Etype (Discr, Tnn);
501
         end if;
502
 
503
      <<Continue>>
504
         Next_Component (Comp);
505
      end loop;
506
   end Adjust_Discriminants;
507
 
508
   ---------------------------
509
   -- Build_Array_Init_Proc --
510
   ---------------------------
511
 
512
   procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
513
      Loc              : constant Source_Ptr := Sloc (Nod);
514
      Comp_Type        : constant Entity_Id  := Component_Type (A_Type);
515
      Body_Stmts       : List_Id;
516
      Has_Default_Init : Boolean;
517
      Index_List       : List_Id;
518
      Proc_Id          : Entity_Id;
519
 
520
      function Init_Component return List_Id;
521
      --  Create one statement to initialize one array component, designated
522
      --  by a full set of indexes.
523
 
524
      function Init_One_Dimension (N : Int) return List_Id;
525
      --  Create loop to initialize one dimension of the array. The single
526
      --  statement in the loop body initializes the inner dimensions if any,
527
      --  or else the single component. Note that this procedure is called
528
      --  recursively, with N being the dimension to be initialized. A call
529
      --  with N greater than the number of dimensions simply generates the
530
      --  component initialization, terminating the recursion.
531
 
532
      --------------------
533
      -- Init_Component --
534
      --------------------
535
 
536
      function Init_Component return List_Id is
537
         Comp : Node_Id;
538
 
539
      begin
540
         Comp :=
541
           Make_Indexed_Component (Loc,
542
             Prefix      => Make_Identifier (Loc, Name_uInit),
543
             Expressions => Index_List);
544
 
545
         if Has_Default_Aspect (A_Type) then
546
            Set_Assignment_OK (Comp);
547
            return New_List (
548
              Make_Assignment_Statement (Loc,
549
                Name       => Comp,
550
                Expression =>
551
                  Convert_To (Comp_Type,
552
                    Default_Aspect_Component_Value (First_Subtype (A_Type)))));
553
 
554
         elsif Needs_Simple_Initialization (Comp_Type) then
555
            Set_Assignment_OK (Comp);
556
            return New_List (
557
              Make_Assignment_Statement (Loc,
558
                Name       => Comp,
559
                Expression =>
560
                  Get_Simple_Init_Val
561
                    (Comp_Type, Nod, Component_Size (A_Type))));
562
 
563
         else
564
            Clean_Task_Names (Comp_Type, Proc_Id);
565
            return
566
              Build_Initialization_Call
567
                (Loc, Comp, Comp_Type,
568
                 In_Init_Proc => True,
569
                 Enclos_Type  => A_Type);
570
         end if;
571
      end Init_Component;
572
 
573
      ------------------------
574
      -- Init_One_Dimension --
575
      ------------------------
576
 
577
      function Init_One_Dimension (N : Int) return List_Id is
578
         Index : Entity_Id;
579
 
580
      begin
581
         --  If the component does not need initializing, then there is nothing
582
         --  to do here, so we return a null body. This occurs when generating
583
         --  the dummy Init_Proc needed for Initialize_Scalars processing.
584
 
585
         if not Has_Non_Null_Base_Init_Proc (Comp_Type)
586
           and then not Needs_Simple_Initialization (Comp_Type)
587
           and then not Has_Task (Comp_Type)
588
           and then not Has_Default_Aspect (A_Type)
589
         then
590
            return New_List (Make_Null_Statement (Loc));
591
 
592
         --  If all dimensions dealt with, we simply initialize the component
593
 
594
         elsif N > Number_Dimensions (A_Type) then
595
            return Init_Component;
596
 
597
         --  Here we generate the required loop
598
 
599
         else
600
            Index :=
601
              Make_Defining_Identifier (Loc, New_External_Name ('J', N));
602
 
603
            Append (New_Reference_To (Index, Loc), Index_List);
604
 
605
            return New_List (
606
              Make_Implicit_Loop_Statement (Nod,
607
                Identifier => Empty,
608
                Iteration_Scheme =>
609
                  Make_Iteration_Scheme (Loc,
610
                    Loop_Parameter_Specification =>
611
                      Make_Loop_Parameter_Specification (Loc,
612
                        Defining_Identifier => Index,
613
                        Discrete_Subtype_Definition =>
614
                          Make_Attribute_Reference (Loc,
615
                            Prefix => Make_Identifier (Loc, Name_uInit),
616
                            Attribute_Name  => Name_Range,
617
                            Expressions     => New_List (
618
                              Make_Integer_Literal (Loc, N))))),
619
                Statements =>  Init_One_Dimension (N + 1)));
620
         end if;
621
      end Init_One_Dimension;
622
 
623
   --  Start of processing for Build_Array_Init_Proc
624
 
625
   begin
626
      --  Nothing to generate in the following cases:
627
 
628
      --    1. Initialization is suppressed for the type
629
      --    2. The type is a value type, in the CIL sense.
630
      --    3. The type has CIL/JVM convention.
631
      --    4. An initialization already exists for the base type
632
 
633
      if Initialization_Suppressed (A_Type)
634
        or else Is_Value_Type (Comp_Type)
635
        or else Convention (A_Type) = Convention_CIL
636
        or else Convention (A_Type) = Convention_Java
637
        or else Present (Base_Init_Proc (A_Type))
638
      then
639
         return;
640
      end if;
641
 
642
      Index_List := New_List;
643
 
644
      --  We need an initialization procedure if any of the following is true:
645
 
646
      --    1. The component type has an initialization procedure
647
      --    2. The component type needs simple initialization
648
      --    3. Tasks are present
649
      --    4. The type is marked as a public entity
650
      --    5. The array type has a Default_Component_Value aspect
651
 
652
      --  The reason for the public entity test is to deal properly with the
653
      --  Initialize_Scalars pragma. This pragma can be set in the client and
654
      --  not in the declaring package, this means the client will make a call
655
      --  to the initialization procedure (because one of conditions 1-3 must
656
      --  apply in this case), and we must generate a procedure (even if it is
657
      --  null) to satisfy the call in this case.
658
 
659
      --  Exception: do not build an array init_proc for a type whose root
660
      --  type is Standard.String or Standard.Wide_[Wide_]String, since there
661
      --  is no place to put the code, and in any case we handle initialization
662
      --  of such types (in the Initialize_Scalars case, that's the only time
663
      --  the issue arises) in a special manner anyway which does not need an
664
      --  init_proc.
665
 
666
      Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
667
                            or else Needs_Simple_Initialization (Comp_Type)
668
                            or else Has_Task (Comp_Type)
669
                            or else Has_Default_Aspect (A_Type);
670
 
671
      if Has_Default_Init
672
        or else (not Restriction_Active (No_Initialize_Scalars)
673
                  and then Is_Public (A_Type)
674
                  and then Root_Type (A_Type) /= Standard_String
675
                  and then Root_Type (A_Type) /= Standard_Wide_String
676
                  and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
677
      then
678
         Proc_Id :=
679
           Make_Defining_Identifier (Loc,
680
             Chars => Make_Init_Proc_Name (A_Type));
681
 
682
         --  If No_Default_Initialization restriction is active, then we don't
683
         --  want to build an init_proc, but we need to mark that an init_proc
684
         --  would be needed if this restriction was not active (so that we can
685
         --  detect attempts to call it), so set a dummy init_proc in place.
686
         --  This is only done though when actual default initialization is
687
         --  needed (and not done when only Is_Public is True), since otherwise
688
         --  objects such as arrays of scalars could be wrongly flagged as
689
         --  violating the restriction.
690
 
691
         if Restriction_Active (No_Default_Initialization) then
692
            if Has_Default_Init then
693
               Set_Init_Proc (A_Type, Proc_Id);
694
            end if;
695
 
696
            return;
697
         end if;
698
 
699
         Body_Stmts := Init_One_Dimension (1);
700
 
701
         Discard_Node (
702
           Make_Subprogram_Body (Loc,
703
             Specification =>
704
               Make_Procedure_Specification (Loc,
705
                 Defining_Unit_Name => Proc_Id,
706
                 Parameter_Specifications => Init_Formals (A_Type)),
707
             Declarations => New_List,
708
             Handled_Statement_Sequence =>
709
               Make_Handled_Sequence_Of_Statements (Loc,
710
                 Statements => Body_Stmts)));
711
 
712
         Set_Ekind          (Proc_Id, E_Procedure);
713
         Set_Is_Public      (Proc_Id, Is_Public (A_Type));
714
         Set_Is_Internal    (Proc_Id);
715
         Set_Has_Completion (Proc_Id);
716
 
717
         if not Debug_Generated_Code then
718
            Set_Debug_Info_Off (Proc_Id);
719
         end if;
720
 
721
         --  Set inlined unless controlled stuff or tasks around, in which
722
         --  case we do not want to inline, because nested stuff may cause
723
         --  difficulties in inter-unit inlining, and furthermore there is
724
         --  in any case no point in inlining such complex init procs.
725
 
726
         if not Has_Task (Proc_Id)
727
           and then not Needs_Finalization (Proc_Id)
728
         then
729
            Set_Is_Inlined (Proc_Id);
730
         end if;
731
 
732
         --  Associate Init_Proc with type, and determine if the procedure
733
         --  is null (happens because of the Initialize_Scalars pragma case,
734
         --  where we have to generate a null procedure in case it is called
735
         --  by a client with Initialize_Scalars set). Such procedures have
736
         --  to be generated, but do not have to be called, so we mark them
737
         --  as null to suppress the call.
738
 
739
         Set_Init_Proc (A_Type, Proc_Id);
740
 
741
         if List_Length (Body_Stmts) = 1
742
 
743
           --  We must skip SCIL nodes because they may have been added to this
744
           --  list by Insert_Actions.
745
 
746
           and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
747
         then
748
            Set_Is_Null_Init_Proc (Proc_Id);
749
 
750
         else
751
            --  Try to build a static aggregate to statically initialize
752
            --  objects of the type. This can only be done for constrained
753
            --  one-dimensional arrays with static bounds.
754
 
755
            Set_Static_Initialization
756
              (Proc_Id,
757
               Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
758
         end if;
759
      end if;
760
   end Build_Array_Init_Proc;
761
 
762
   --------------------------------
763
   -- Build_Discr_Checking_Funcs --
764
   --------------------------------
765
 
766
   procedure Build_Discr_Checking_Funcs (N : Node_Id) is
767
      Rec_Id            : Entity_Id;
768
      Loc               : Source_Ptr;
769
      Enclosing_Func_Id : Entity_Id;
770
      Sequence          : Nat     := 1;
771
      Type_Def          : Node_Id;
772
      V                 : Node_Id;
773
 
774
      function Build_Case_Statement
775
        (Case_Id : Entity_Id;
776
         Variant : Node_Id) return Node_Id;
777
      --  Build a case statement containing only two alternatives. The first
778
      --  alternative corresponds exactly to the discrete choices given on the
779
      --  variant with contains the components that we are generating the
780
      --  checks for. If the discriminant is one of these return False. The
781
      --  second alternative is an OTHERS choice that will return True
782
      --  indicating the discriminant did not match.
783
 
784
      function Build_Dcheck_Function
785
        (Case_Id : Entity_Id;
786
         Variant : Node_Id) return Entity_Id;
787
      --  Build the discriminant checking function for a given variant
788
 
789
      procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
790
      --  Builds the discriminant checking function for each variant of the
791
      --  given variant part of the record type.
792
 
793
      --------------------------
794
      -- Build_Case_Statement --
795
      --------------------------
796
 
797
      function Build_Case_Statement
798
        (Case_Id : Entity_Id;
799
         Variant : Node_Id) return Node_Id
800
      is
801
         Alt_List       : constant List_Id := New_List;
802
         Actuals_List   : List_Id;
803
         Case_Node      : Node_Id;
804
         Case_Alt_Node  : Node_Id;
805
         Choice         : Node_Id;
806
         Choice_List    : List_Id;
807
         D              : Entity_Id;
808
         Return_Node    : Node_Id;
809
 
810
      begin
811
         Case_Node := New_Node (N_Case_Statement, Loc);
812
 
813
         --  Replace the discriminant which controls the variant, with the name
814
         --  of the formal of the checking function.
815
 
816
         Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
817
 
818
         Choice := First (Discrete_Choices (Variant));
819
 
820
         if Nkind (Choice) = N_Others_Choice then
821
            Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
822
         else
823
            Choice_List := New_Copy_List (Discrete_Choices (Variant));
824
         end if;
825
 
826
         if not Is_Empty_List (Choice_List) then
827
            Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
828
            Set_Discrete_Choices (Case_Alt_Node, Choice_List);
829
 
830
            --  In case this is a nested variant, we need to return the result
831
            --  of the discriminant checking function for the immediately
832
            --  enclosing variant.
833
 
834
            if Present (Enclosing_Func_Id) then
835
               Actuals_List := New_List;
836
 
837
               D := First_Discriminant (Rec_Id);
838
               while Present (D) loop
839
                  Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
840
                  Next_Discriminant (D);
841
               end loop;
842
 
843
               Return_Node :=
844
                 Make_Simple_Return_Statement (Loc,
845
                   Expression =>
846
                     Make_Function_Call (Loc,
847
                       Name =>
848
                         New_Reference_To (Enclosing_Func_Id,  Loc),
849
                       Parameter_Associations =>
850
                         Actuals_List));
851
 
852
            else
853
               Return_Node :=
854
                 Make_Simple_Return_Statement (Loc,
855
                   Expression =>
856
                     New_Reference_To (Standard_False, Loc));
857
            end if;
858
 
859
            Set_Statements (Case_Alt_Node, New_List (Return_Node));
860
            Append (Case_Alt_Node, Alt_List);
861
         end if;
862
 
863
         Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
864
         Choice_List := New_List (New_Node (N_Others_Choice, Loc));
865
         Set_Discrete_Choices (Case_Alt_Node, Choice_List);
866
 
867
         Return_Node :=
868
           Make_Simple_Return_Statement (Loc,
869
             Expression =>
870
               New_Reference_To (Standard_True, Loc));
871
 
872
         Set_Statements (Case_Alt_Node, New_List (Return_Node));
873
         Append (Case_Alt_Node, Alt_List);
874
 
875
         Set_Alternatives (Case_Node, Alt_List);
876
         return Case_Node;
877
      end Build_Case_Statement;
878
 
879
      ---------------------------
880
      -- Build_Dcheck_Function --
881
      ---------------------------
882
 
883
      function Build_Dcheck_Function
884
        (Case_Id : Entity_Id;
885
         Variant : Node_Id) return Entity_Id
886
      is
887
         Body_Node           : Node_Id;
888
         Func_Id             : Entity_Id;
889
         Parameter_List      : List_Id;
890
         Spec_Node           : Node_Id;
891
 
892
      begin
893
         Body_Node := New_Node (N_Subprogram_Body, Loc);
894
         Sequence := Sequence + 1;
895
 
896
         Func_Id :=
897
           Make_Defining_Identifier (Loc,
898
             Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
899
 
900
         Spec_Node := New_Node (N_Function_Specification, Loc);
901
         Set_Defining_Unit_Name (Spec_Node, Func_Id);
902
 
903
         Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
904
 
905
         Set_Parameter_Specifications (Spec_Node, Parameter_List);
906
         Set_Result_Definition (Spec_Node,
907
                                New_Reference_To (Standard_Boolean,  Loc));
908
         Set_Specification (Body_Node, Spec_Node);
909
         Set_Declarations (Body_Node, New_List);
910
 
911
         Set_Handled_Statement_Sequence (Body_Node,
912
           Make_Handled_Sequence_Of_Statements (Loc,
913
             Statements => New_List (
914
               Build_Case_Statement (Case_Id, Variant))));
915
 
916
         Set_Ekind       (Func_Id, E_Function);
917
         Set_Mechanism   (Func_Id, Default_Mechanism);
918
         Set_Is_Inlined  (Func_Id, True);
919
         Set_Is_Pure     (Func_Id, True);
920
         Set_Is_Public   (Func_Id, Is_Public (Rec_Id));
921
         Set_Is_Internal (Func_Id, True);
922
 
923
         if not Debug_Generated_Code then
924
            Set_Debug_Info_Off (Func_Id);
925
         end if;
926
 
927
         Analyze (Body_Node);
928
 
929
         Append_Freeze_Action (Rec_Id, Body_Node);
930
         Set_Dcheck_Function (Variant, Func_Id);
931
         return Func_Id;
932
      end Build_Dcheck_Function;
933
 
934
      ----------------------------
935
      -- Build_Dcheck_Functions --
936
      ----------------------------
937
 
938
      procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
939
         Component_List_Node : Node_Id;
940
         Decl                : Entity_Id;
941
         Discr_Name          : Entity_Id;
942
         Func_Id             : Entity_Id;
943
         Variant             : Node_Id;
944
         Saved_Enclosing_Func_Id : Entity_Id;
945
 
946
      begin
947
         --  Build the discriminant-checking function for each variant, and
948
         --  label all components of that variant with the function's name.
949
         --  We only Generate a discriminant-checking function when the
950
         --  variant is not empty, to prevent the creation of dead code.
951
         --  The exception to that is when Frontend_Layout_On_Target is set,
952
         --  because the variant record size function generated in package
953
         --  Layout needs to generate calls to all discriminant-checking
954
         --  functions, including those for empty variants.
955
 
956
         Discr_Name := Entity (Name (Variant_Part_Node));
957
         Variant := First_Non_Pragma (Variants (Variant_Part_Node));
958
 
959
         while Present (Variant) loop
960
            Component_List_Node := Component_List (Variant);
961
 
962
            if not Null_Present (Component_List_Node)
963
              or else Frontend_Layout_On_Target
964
            then
965
               Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
966
               Decl :=
967
                 First_Non_Pragma (Component_Items (Component_List_Node));
968
 
969
               while Present (Decl) loop
970
                  Set_Discriminant_Checking_Func
971
                    (Defining_Identifier (Decl), Func_Id);
972
 
973
                  Next_Non_Pragma (Decl);
974
               end loop;
975
 
976
               if Present (Variant_Part (Component_List_Node)) then
977
                  Saved_Enclosing_Func_Id := Enclosing_Func_Id;
978
                  Enclosing_Func_Id := Func_Id;
979
                  Build_Dcheck_Functions (Variant_Part (Component_List_Node));
980
                  Enclosing_Func_Id := Saved_Enclosing_Func_Id;
981
               end if;
982
            end if;
983
 
984
            Next_Non_Pragma (Variant);
985
         end loop;
986
      end Build_Dcheck_Functions;
987
 
988
   --  Start of processing for Build_Discr_Checking_Funcs
989
 
990
   begin
991
      --  Only build if not done already
992
 
993
      if not Discr_Check_Funcs_Built (N) then
994
         Type_Def := Type_Definition (N);
995
 
996
         if Nkind (Type_Def) = N_Record_Definition then
997
            if No (Component_List (Type_Def)) then   -- null record.
998
               return;
999
            else
1000
               V := Variant_Part (Component_List (Type_Def));
1001
            end if;
1002
 
1003
         else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1004
            if No (Component_List (Record_Extension_Part (Type_Def))) then
1005
               return;
1006
            else
1007
               V := Variant_Part
1008
                      (Component_List (Record_Extension_Part (Type_Def)));
1009
            end if;
1010
         end if;
1011
 
1012
         Rec_Id := Defining_Identifier (N);
1013
 
1014
         if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1015
            Loc := Sloc (N);
1016
            Enclosing_Func_Id := Empty;
1017
            Build_Dcheck_Functions (V);
1018
         end if;
1019
 
1020
         Set_Discr_Check_Funcs_Built (N);
1021
      end if;
1022
   end Build_Discr_Checking_Funcs;
1023
 
1024
   --------------------------------
1025
   -- Build_Discriminant_Formals --
1026
   --------------------------------
1027
 
1028
   function Build_Discriminant_Formals
1029
     (Rec_Id : Entity_Id;
1030
      Use_Dl : Boolean) return List_Id
1031
   is
1032
      Loc             : Source_Ptr       := Sloc (Rec_Id);
1033
      Parameter_List  : constant List_Id := New_List;
1034
      D               : Entity_Id;
1035
      Formal          : Entity_Id;
1036
      Formal_Type     : Entity_Id;
1037
      Param_Spec_Node : Node_Id;
1038
 
1039
   begin
1040
      if Has_Discriminants (Rec_Id) then
1041
         D := First_Discriminant (Rec_Id);
1042
         while Present (D) loop
1043
            Loc := Sloc (D);
1044
 
1045
            if Use_Dl then
1046
               Formal := Discriminal (D);
1047
               Formal_Type := Etype (Formal);
1048
            else
1049
               Formal := Make_Defining_Identifier (Loc, Chars (D));
1050
               Formal_Type := Etype (D);
1051
            end if;
1052
 
1053
            Param_Spec_Node :=
1054
              Make_Parameter_Specification (Loc,
1055
                  Defining_Identifier => Formal,
1056
                Parameter_Type =>
1057
                  New_Reference_To (Formal_Type, Loc));
1058
            Append (Param_Spec_Node, Parameter_List);
1059
            Next_Discriminant (D);
1060
         end loop;
1061
      end if;
1062
 
1063
      return Parameter_List;
1064
   end Build_Discriminant_Formals;
1065
 
1066
   --------------------------------------
1067
   -- Build_Equivalent_Array_Aggregate --
1068
   --------------------------------------
1069
 
1070
   function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1071
      Loc        : constant Source_Ptr := Sloc (T);
1072
      Comp_Type  : constant Entity_Id := Component_Type (T);
1073
      Index_Type : constant Entity_Id := Etype (First_Index (T));
1074
      Proc       : constant Entity_Id := Base_Init_Proc (T);
1075
      Lo, Hi     : Node_Id;
1076
      Aggr       : Node_Id;
1077
      Expr       : Node_Id;
1078
 
1079
   begin
1080
      if not Is_Constrained (T)
1081
        or else Number_Dimensions (T) > 1
1082
        or else No (Proc)
1083
      then
1084
         Initialization_Warning (T);
1085
         return Empty;
1086
      end if;
1087
 
1088
      Lo := Type_Low_Bound  (Index_Type);
1089
      Hi := Type_High_Bound (Index_Type);
1090
 
1091
      if not Compile_Time_Known_Value (Lo)
1092
        or else not Compile_Time_Known_Value (Hi)
1093
      then
1094
         Initialization_Warning (T);
1095
         return Empty;
1096
      end if;
1097
 
1098
      if Is_Record_Type (Comp_Type)
1099
        and then Present (Base_Init_Proc (Comp_Type))
1100
      then
1101
         Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1102
 
1103
         if No (Expr) then
1104
            Initialization_Warning (T);
1105
            return Empty;
1106
         end if;
1107
 
1108
      else
1109
         Initialization_Warning (T);
1110
         return Empty;
1111
      end if;
1112
 
1113
      Aggr := Make_Aggregate (Loc, No_List, New_List);
1114
      Set_Etype (Aggr, T);
1115
      Set_Aggregate_Bounds (Aggr,
1116
        Make_Range (Loc,
1117
          Low_Bound  => New_Copy (Lo),
1118
          High_Bound => New_Copy (Hi)));
1119
      Set_Parent (Aggr, Parent (Proc));
1120
 
1121
      Append_To (Component_Associations (Aggr),
1122
         Make_Component_Association (Loc,
1123
              Choices =>
1124
                 New_List (
1125
                   Make_Range (Loc,
1126
                     Low_Bound  => New_Copy (Lo),
1127
                     High_Bound => New_Copy (Hi))),
1128
              Expression => Expr));
1129
 
1130
      if Static_Array_Aggregate (Aggr) then
1131
         return Aggr;
1132
      else
1133
         Initialization_Warning (T);
1134
         return Empty;
1135
      end if;
1136
   end Build_Equivalent_Array_Aggregate;
1137
 
1138
   ---------------------------------------
1139
   -- Build_Equivalent_Record_Aggregate --
1140
   ---------------------------------------
1141
 
1142
   function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1143
      Agg       : Node_Id;
1144
      Comp      : Entity_Id;
1145
      Comp_Type : Entity_Id;
1146
 
1147
      --  Start of processing for Build_Equivalent_Record_Aggregate
1148
 
1149
   begin
1150
      if not Is_Record_Type (T)
1151
        or else Has_Discriminants (T)
1152
        or else Is_Limited_Type (T)
1153
        or else Has_Non_Standard_Rep (T)
1154
      then
1155
         Initialization_Warning (T);
1156
         return Empty;
1157
      end if;
1158
 
1159
      Comp := First_Component (T);
1160
 
1161
      --  A null record needs no warning
1162
 
1163
      if No (Comp) then
1164
         return Empty;
1165
      end if;
1166
 
1167
      while Present (Comp) loop
1168
 
1169
         --  Array components are acceptable if initialized by a positional
1170
         --  aggregate with static components.
1171
 
1172
         if Is_Array_Type (Etype (Comp)) then
1173
            Comp_Type := Component_Type (Etype (Comp));
1174
 
1175
            if Nkind (Parent (Comp)) /= N_Component_Declaration
1176
              or else No (Expression (Parent (Comp)))
1177
              or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1178
            then
1179
               Initialization_Warning (T);
1180
               return Empty;
1181
 
1182
            elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1183
               and then
1184
                 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1185
                   or else
1186
                  not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1187
            then
1188
               Initialization_Warning (T);
1189
               return Empty;
1190
 
1191
            elsif
1192
              not Static_Array_Aggregate (Expression (Parent (Comp)))
1193
            then
1194
               Initialization_Warning (T);
1195
               return Empty;
1196
            end if;
1197
 
1198
         elsif Is_Scalar_Type (Etype (Comp)) then
1199
            Comp_Type := Etype (Comp);
1200
 
1201
            if Nkind (Parent (Comp)) /= N_Component_Declaration
1202
              or else No (Expression (Parent (Comp)))
1203
              or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1204
              or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1205
              or else not
1206
                Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1207
            then
1208
               Initialization_Warning (T);
1209
               return Empty;
1210
            end if;
1211
 
1212
         --  For now, other types are excluded
1213
 
1214
         else
1215
            Initialization_Warning (T);
1216
            return Empty;
1217
         end if;
1218
 
1219
         Next_Component (Comp);
1220
      end loop;
1221
 
1222
      --  All components have static initialization. Build positional aggregate
1223
      --  from the given expressions or defaults.
1224
 
1225
      Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1226
      Set_Parent (Agg, Parent (T));
1227
 
1228
      Comp := First_Component (T);
1229
      while Present (Comp) loop
1230
         Append
1231
           (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1232
         Next_Component (Comp);
1233
      end loop;
1234
 
1235
      Analyze_And_Resolve (Agg, T);
1236
      return Agg;
1237
   end Build_Equivalent_Record_Aggregate;
1238
 
1239
   -------------------------------
1240
   -- Build_Initialization_Call --
1241
   -------------------------------
1242
 
1243
   --  References to a discriminant inside the record type declaration can
1244
   --  appear either in the subtype_indication to constrain a record or an
1245
   --  array, or as part of a larger expression given for the initial value
1246
   --  of a component. In both of these cases N appears in the record
1247
   --  initialization procedure and needs to be replaced by the formal
1248
   --  parameter of the initialization procedure which corresponds to that
1249
   --  discriminant.
1250
 
1251
   --  In the example below, references to discriminants D1 and D2 in proc_1
1252
   --  are replaced by references to formals with the same name
1253
   --  (discriminals)
1254
 
1255
   --  A similar replacement is done for calls to any record initialization
1256
   --  procedure for any components that are themselves of a record type.
1257
 
1258
   --  type R (D1, D2 : Integer) is record
1259
   --     X : Integer := F * D1;
1260
   --     Y : Integer := F * D2;
1261
   --  end record;
1262
 
1263
   --  procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1264
   --  begin
1265
   --     Out_2.D1 := D1;
1266
   --     Out_2.D2 := D2;
1267
   --     Out_2.X := F * D1;
1268
   --     Out_2.Y := F * D2;
1269
   --  end;
1270
 
1271
   function Build_Initialization_Call
1272
     (Loc               : Source_Ptr;
1273
      Id_Ref            : Node_Id;
1274
      Typ               : Entity_Id;
1275
      In_Init_Proc      : Boolean := False;
1276
      Enclos_Type       : Entity_Id := Empty;
1277
      Discr_Map         : Elist_Id := New_Elmt_List;
1278
      With_Default_Init : Boolean := False;
1279
      Constructor_Ref   : Node_Id := Empty) return List_Id
1280
   is
1281
      Res            : constant List_Id := New_List;
1282
      Arg            : Node_Id;
1283
      Args           : List_Id;
1284
      Decls          : List_Id;
1285
      Decl           : Node_Id;
1286
      Discr          : Entity_Id;
1287
      First_Arg      : Node_Id;
1288
      Full_Init_Type : Entity_Id;
1289
      Full_Type      : Entity_Id := Typ;
1290
      Init_Type      : Entity_Id;
1291
      Proc           : Entity_Id;
1292
 
1293
   begin
1294
      pragma Assert (Constructor_Ref = Empty
1295
        or else Is_CPP_Constructor_Call (Constructor_Ref));
1296
 
1297
      if No (Constructor_Ref) then
1298
         Proc := Base_Init_Proc (Typ);
1299
      else
1300
         Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1301
      end if;
1302
 
1303
      pragma Assert (Present (Proc));
1304
      Init_Type      := Etype (First_Formal (Proc));
1305
      Full_Init_Type := Underlying_Type (Init_Type);
1306
 
1307
      --  Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1308
      --  is active (in which case we make the call anyway, since in the
1309
      --  actual compiled client it may be non null).
1310
      --  Also nothing to do for value types.
1311
 
1312
      if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1313
        or else Is_Value_Type (Typ)
1314
        or else
1315
          (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1316
      then
1317
         return Empty_List;
1318
      end if;
1319
 
1320
      --  Go to full view if private type. In the case of successive
1321
      --  private derivations, this can require more than one step.
1322
 
1323
      while Is_Private_Type (Full_Type)
1324
        and then Present (Full_View (Full_Type))
1325
      loop
1326
         Full_Type := Full_View (Full_Type);
1327
      end loop;
1328
 
1329
      --  If Typ is derived, the procedure is the initialization procedure for
1330
      --  the root type. Wrap the argument in an conversion to make it type
1331
      --  honest. Actually it isn't quite type honest, because there can be
1332
      --  conflicts of views in the private type case. That is why we set
1333
      --  Conversion_OK in the conversion node.
1334
 
1335
      if (Is_Record_Type (Typ)
1336
           or else Is_Array_Type (Typ)
1337
           or else Is_Private_Type (Typ))
1338
        and then Init_Type /= Base_Type (Typ)
1339
      then
1340
         First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1341
         Set_Etype (First_Arg, Init_Type);
1342
 
1343
      else
1344
         First_Arg := Id_Ref;
1345
      end if;
1346
 
1347
      Args := New_List (Convert_Concurrent (First_Arg, Typ));
1348
 
1349
      --  In the tasks case, add _Master as the value of the _Master parameter
1350
      --  and _Chain as the value of the _Chain parameter. At the outer level,
1351
      --  these will be variables holding the corresponding values obtained
1352
      --  from GNARL. At inner levels, they will be the parameters passed down
1353
      --  through the outer routines.
1354
 
1355
      if Has_Task (Full_Type) then
1356
         if Restriction_Active (No_Task_Hierarchy) then
1357
            Append_To (Args,
1358
              New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1359
         else
1360
            Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1361
         end if;
1362
 
1363
         Append_To (Args, Make_Identifier (Loc, Name_uChain));
1364
 
1365
         --  Ada 2005 (AI-287): In case of default initialized components
1366
         --  with tasks, we generate a null string actual parameter.
1367
         --  This is just a workaround that must be improved later???
1368
 
1369
         if With_Default_Init then
1370
            Append_To (Args,
1371
              Make_String_Literal (Loc,
1372
                Strval => ""));
1373
 
1374
         else
1375
            Decls :=
1376
              Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1377
            Decl  := Last (Decls);
1378
 
1379
            Append_To (Args,
1380
              New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1381
            Append_List (Decls, Res);
1382
         end if;
1383
 
1384
      else
1385
         Decls := No_List;
1386
         Decl  := Empty;
1387
      end if;
1388
 
1389
      --  Add discriminant values if discriminants are present
1390
 
1391
      if Has_Discriminants (Full_Init_Type) then
1392
         Discr := First_Discriminant (Full_Init_Type);
1393
 
1394
         while Present (Discr) loop
1395
 
1396
            --  If this is a discriminated concurrent type, the init_proc
1397
            --  for the corresponding record is being called. Use that type
1398
            --  directly to find the discriminant value, to handle properly
1399
            --  intervening renamed discriminants.
1400
 
1401
            declare
1402
               T : Entity_Id := Full_Type;
1403
 
1404
            begin
1405
               if Is_Protected_Type (T) then
1406
                  T := Corresponding_Record_Type (T);
1407
 
1408
               elsif Is_Private_Type (T)
1409
                 and then Present (Underlying_Full_View (T))
1410
                 and then Is_Protected_Type (Underlying_Full_View (T))
1411
               then
1412
                  T := Corresponding_Record_Type (Underlying_Full_View (T));
1413
               end if;
1414
 
1415
               Arg :=
1416
                 Get_Discriminant_Value (
1417
                   Discr,
1418
                   T,
1419
                   Discriminant_Constraint (Full_Type));
1420
            end;
1421
 
1422
            --  If the target has access discriminants, and is constrained by
1423
            --  an access to the enclosing construct, i.e. a current instance,
1424
            --  replace the reference to the type by a reference to the object.
1425
 
1426
            if Nkind (Arg) = N_Attribute_Reference
1427
              and then Is_Access_Type (Etype (Arg))
1428
              and then Is_Entity_Name (Prefix (Arg))
1429
              and then Is_Type (Entity (Prefix (Arg)))
1430
            then
1431
               Arg :=
1432
                 Make_Attribute_Reference (Loc,
1433
                   Prefix         => New_Copy (Prefix (Id_Ref)),
1434
                   Attribute_Name => Name_Unrestricted_Access);
1435
 
1436
            elsif In_Init_Proc then
1437
 
1438
               --  Replace any possible references to the discriminant in the
1439
               --  call to the record initialization procedure with references
1440
               --  to the appropriate formal parameter.
1441
 
1442
               if Nkind (Arg) = N_Identifier
1443
                  and then Ekind (Entity (Arg)) = E_Discriminant
1444
               then
1445
                  Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1446
 
1447
               --  Otherwise make a copy of the default expression. Note that
1448
               --  we use the current Sloc for this, because we do not want the
1449
               --  call to appear to be at the declaration point. Within the
1450
               --  expression, replace discriminants with their discriminals.
1451
 
1452
               else
1453
                  Arg :=
1454
                    New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1455
               end if;
1456
 
1457
            else
1458
               if Is_Constrained (Full_Type) then
1459
                  Arg := Duplicate_Subexpr_No_Checks (Arg);
1460
               else
1461
                  --  The constraints come from the discriminant default exps,
1462
                  --  they must be reevaluated, so we use New_Copy_Tree but we
1463
                  --  ensure the proper Sloc (for any embedded calls).
1464
 
1465
                  Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1466
               end if;
1467
            end if;
1468
 
1469
            --  Ada 2005 (AI-287): In case of default initialized components,
1470
            --  if the component is constrained with a discriminant of the
1471
            --  enclosing type, we need to generate the corresponding selected
1472
            --  component node to access the discriminant value. In other cases
1473
            --  this is not required, either  because we are inside the init
1474
            --  proc and we use the corresponding formal, or else because the
1475
            --  component is constrained by an expression.
1476
 
1477
            if With_Default_Init
1478
              and then Nkind (Id_Ref) = N_Selected_Component
1479
              and then Nkind (Arg) = N_Identifier
1480
              and then Ekind (Entity (Arg)) = E_Discriminant
1481
            then
1482
               Append_To (Args,
1483
                 Make_Selected_Component (Loc,
1484
                   Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1485
                   Selector_Name => Arg));
1486
            else
1487
               Append_To (Args, Arg);
1488
            end if;
1489
 
1490
            Next_Discriminant (Discr);
1491
         end loop;
1492
      end if;
1493
 
1494
      --  If this is a call to initialize the parent component of a derived
1495
      --  tagged type, indicate that the tag should not be set in the parent.
1496
 
1497
      if Is_Tagged_Type (Full_Init_Type)
1498
        and then not Is_CPP_Class (Full_Init_Type)
1499
        and then Nkind (Id_Ref) = N_Selected_Component
1500
        and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1501
      then
1502
         Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1503
 
1504
      elsif Present (Constructor_Ref) then
1505
         Append_List_To (Args,
1506
           New_Copy_List (Parameter_Associations (Constructor_Ref)));
1507
      end if;
1508
 
1509
      Append_To (Res,
1510
        Make_Procedure_Call_Statement (Loc,
1511
          Name => New_Occurrence_Of (Proc, Loc),
1512
          Parameter_Associations => Args));
1513
 
1514
      if Needs_Finalization (Typ)
1515
        and then Nkind (Id_Ref) = N_Selected_Component
1516
      then
1517
         if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1518
            Append_To (Res,
1519
              Make_Init_Call
1520
                (Obj_Ref => New_Copy_Tree (First_Arg),
1521
                 Typ     => Typ));
1522
         end if;
1523
      end if;
1524
 
1525
      return Res;
1526
 
1527
   exception
1528
      when RE_Not_Available =>
1529
         return Empty_List;
1530
   end Build_Initialization_Call;
1531
 
1532
   ----------------------------
1533
   -- Build_Record_Init_Proc --
1534
   ----------------------------
1535
 
1536
   procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1537
      Decls     : constant List_Id  := New_List;
1538
      Discr_Map : constant Elist_Id := New_Elmt_List;
1539
      Loc       : constant Source_Ptr := Sloc (Rec_Ent);
1540
      Counter   : Int := 0;
1541
      Proc_Id   : Entity_Id;
1542
      Rec_Type  : Entity_Id;
1543
      Set_Tag   : Entity_Id := Empty;
1544
 
1545
      function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1546
      --  Build an assignment statement which assigns the default expression
1547
      --  to its corresponding record component if defined. The left hand side
1548
      --  of the assignment is marked Assignment_OK so that initialization of
1549
      --  limited private records works correctly. This routine may also build
1550
      --  an adjustment call if the component is controlled.
1551
 
1552
      procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1553
      --  If the record has discriminants, add assignment statements to
1554
      --  Statement_List to initialize the discriminant values from the
1555
      --  arguments of the initialization procedure.
1556
 
1557
      function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1558
      --  Build a list representing a sequence of statements which initialize
1559
      --  components of the given component list. This may involve building
1560
      --  case statements for the variant parts. Append any locally declared
1561
      --  objects on list Decls.
1562
 
1563
      function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1564
      --  Given a non-tagged type-derivation that declares discriminants,
1565
      --  such as
1566
      --
1567
      --  type R (R1, R2 : Integer) is record ... end record;
1568
      --
1569
      --  type D (D1 : Integer) is new R (1, D1);
1570
      --
1571
      --  we make the _init_proc of D be
1572
      --
1573
      --       procedure _init_proc (X : D; D1 : Integer) is
1574
      --       begin
1575
      --          _init_proc (R (X), 1, D1);
1576
      --       end _init_proc;
1577
      --
1578
      --  This function builds the call statement in this _init_proc.
1579
 
1580
      procedure Build_CPP_Init_Procedure;
1581
      --  Build the tree corresponding to the procedure specification and body
1582
      --  of the IC procedure that initializes the C++ part of the dispatch
1583
      --  table of an Ada tagged type that is a derivation of a CPP type.
1584
      --  Install it as the CPP_Init TSS.
1585
 
1586
      procedure Build_Init_Procedure;
1587
      --  Build the tree corresponding to the procedure specification and body
1588
      --  of the initialization procedure and install it as the _init TSS.
1589
 
1590
      procedure Build_Offset_To_Top_Functions;
1591
      --  Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1592
      --  and body of Offset_To_Top, a function used in conjuction with types
1593
      --  having secondary dispatch tables.
1594
 
1595
      procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1596
      --  Add range checks to components of discriminated records. S is a
1597
      --  subtype indication of a record component. Check_List is a list
1598
      --  to which the check actions are appended.
1599
 
1600
      function Component_Needs_Simple_Initialization
1601
        (T : Entity_Id) return Boolean;
1602
      --  Determine if a component needs simple initialization, given its type
1603
      --  T. This routine is the same as Needs_Simple_Initialization except for
1604
      --  components of type Tag and Interface_Tag. These two access types do
1605
      --  not require initialization since they are explicitly initialized by
1606
      --  other means.
1607
 
1608
      function Parent_Subtype_Renaming_Discrims return Boolean;
1609
      --  Returns True for base types N that rename discriminants, else False
1610
 
1611
      function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1612
      --  Determine whether a record initialization procedure needs to be
1613
      --  generated for the given record type.
1614
 
1615
      ----------------------
1616
      -- Build_Assignment --
1617
      ----------------------
1618
 
1619
      function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1620
         N_Loc : constant Source_Ptr := Sloc (N);
1621
         Typ   : constant Entity_Id := Underlying_Type (Etype (Id));
1622
         Exp   : Node_Id := N;
1623
         Kind  : Node_Kind := Nkind (N);
1624
         Lhs   : Node_Id;
1625
         Res   : List_Id;
1626
 
1627
      begin
1628
         Lhs :=
1629
           Make_Selected_Component (N_Loc,
1630
             Prefix        => Make_Identifier (Loc, Name_uInit),
1631
             Selector_Name => New_Occurrence_Of (Id, N_Loc));
1632
         Set_Assignment_OK (Lhs);
1633
 
1634
         --  Case of an access attribute applied to the current instance.
1635
         --  Replace the reference to the type by a reference to the actual
1636
         --  object. (Note that this handles the case of the top level of
1637
         --  the expression being given by such an attribute, but does not
1638
         --  cover uses nested within an initial value expression. Nested
1639
         --  uses are unlikely to occur in practice, but are theoretically
1640
         --  possible.) It is not clear how to handle them without fully
1641
         --  traversing the expression. ???
1642
 
1643
         if Kind = N_Attribute_Reference
1644
           and then (Attribute_Name (N) = Name_Unchecked_Access
1645
                       or else
1646
                     Attribute_Name (N) = Name_Unrestricted_Access)
1647
           and then Is_Entity_Name (Prefix (N))
1648
           and then Is_Type (Entity (Prefix (N)))
1649
           and then Entity (Prefix (N)) = Rec_Type
1650
         then
1651
            Exp :=
1652
              Make_Attribute_Reference (N_Loc,
1653
                Prefix         =>
1654
                  Make_Identifier (N_Loc, Name_uInit),
1655
                Attribute_Name => Name_Unrestricted_Access);
1656
         end if;
1657
 
1658
         --  Take a copy of Exp to ensure that later copies of this component
1659
         --  declaration in derived types see the original tree, not a node
1660
         --  rewritten during expansion of the init_proc. If the copy contains
1661
         --  itypes, the scope of the new itypes is the init_proc being built.
1662
 
1663
         Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1664
 
1665
         Res := New_List (
1666
           Make_Assignment_Statement (Loc,
1667
             Name       => Lhs,
1668
             Expression => Exp));
1669
 
1670
         Set_No_Ctrl_Actions (First (Res));
1671
 
1672
         --  Adjust the tag if tagged (because of possible view conversions).
1673
         --  Suppress the tag adjustment when VM_Target because VM tags are
1674
         --  represented implicitly in objects.
1675
 
1676
         if Is_Tagged_Type (Typ)
1677
           and then Tagged_Type_Expansion
1678
         then
1679
            Append_To (Res,
1680
              Make_Assignment_Statement (N_Loc,
1681
                Name       =>
1682
                  Make_Selected_Component (N_Loc,
1683
                    Prefix        =>
1684
                      New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1685
                    Selector_Name =>
1686
                      New_Reference_To (First_Tag_Component (Typ), N_Loc)),
1687
 
1688
                Expression =>
1689
                  Unchecked_Convert_To (RTE (RE_Tag),
1690
                    New_Reference_To
1691
                      (Node
1692
                        (First_Elmt
1693
                          (Access_Disp_Table (Underlying_Type (Typ)))),
1694
                       N_Loc))));
1695
         end if;
1696
 
1697
         --  Adjust the component if controlled except if it is an aggregate
1698
         --  that will be expanded inline.
1699
 
1700
         if Kind = N_Qualified_Expression then
1701
            Kind := Nkind (Expression (N));
1702
         end if;
1703
 
1704
         if Needs_Finalization (Typ)
1705
           and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1706
           and then not Is_Immutably_Limited_Type (Typ)
1707
         then
1708
            Append_To (Res,
1709
              Make_Adjust_Call
1710
                (Obj_Ref => New_Copy_Tree (Lhs),
1711
                 Typ     => Etype (Id)));
1712
         end if;
1713
 
1714
         return Res;
1715
 
1716
      exception
1717
         when RE_Not_Available =>
1718
            return Empty_List;
1719
      end Build_Assignment;
1720
 
1721
      ------------------------------------
1722
      -- Build_Discriminant_Assignments --
1723
      ------------------------------------
1724
 
1725
      procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1726
         Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1727
         D         : Entity_Id;
1728
         D_Loc     : Source_Ptr;
1729
 
1730
      begin
1731
         if Has_Discriminants (Rec_Type)
1732
           and then not Is_Unchecked_Union (Rec_Type)
1733
         then
1734
            D := First_Discriminant (Rec_Type);
1735
            while Present (D) loop
1736
 
1737
               --  Don't generate the assignment for discriminants in derived
1738
               --  tagged types if the discriminant is a renaming of some
1739
               --  ancestor discriminant. This initialization will be done
1740
               --  when initializing the _parent field of the derived record.
1741
 
1742
               if Is_Tagged
1743
                 and then Present (Corresponding_Discriminant (D))
1744
               then
1745
                  null;
1746
 
1747
               else
1748
                  D_Loc := Sloc (D);
1749
                  Append_List_To (Statement_List,
1750
                    Build_Assignment (D,
1751
                      New_Reference_To (Discriminal (D), D_Loc)));
1752
               end if;
1753
 
1754
               Next_Discriminant (D);
1755
            end loop;
1756
         end if;
1757
      end Build_Discriminant_Assignments;
1758
 
1759
      --------------------------
1760
      -- Build_Init_Call_Thru --
1761
      --------------------------
1762
 
1763
      function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1764
         Parent_Proc : constant Entity_Id :=
1765
                         Base_Init_Proc (Etype (Rec_Type));
1766
 
1767
         Parent_Type : constant Entity_Id :=
1768
                         Etype (First_Formal (Parent_Proc));
1769
 
1770
         Uparent_Type : constant Entity_Id :=
1771
                          Underlying_Type (Parent_Type);
1772
 
1773
         First_Discr_Param : Node_Id;
1774
 
1775
         Arg          : Node_Id;
1776
         Args         : List_Id;
1777
         First_Arg    : Node_Id;
1778
         Parent_Discr : Entity_Id;
1779
         Res          : List_Id;
1780
 
1781
      begin
1782
         --  First argument (_Init) is the object to be initialized.
1783
         --  ??? not sure where to get a reasonable Loc for First_Arg
1784
 
1785
         First_Arg :=
1786
           OK_Convert_To (Parent_Type,
1787
             New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1788
 
1789
         Set_Etype (First_Arg, Parent_Type);
1790
 
1791
         Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1792
 
1793
         --  In the tasks case,
1794
         --    add _Master as the value of the _Master parameter
1795
         --    add _Chain as the value of the _Chain parameter.
1796
         --    add _Task_Name as the value of the _Task_Name parameter.
1797
         --  At the outer level, these will be variables holding the
1798
         --  corresponding values obtained from GNARL or the expander.
1799
         --
1800
         --  At inner levels, they will be the parameters passed down through
1801
         --  the outer routines.
1802
 
1803
         First_Discr_Param := Next (First (Parameters));
1804
 
1805
         if Has_Task (Rec_Type) then
1806
            if Restriction_Active (No_Task_Hierarchy) then
1807
               Append_To (Args,
1808
                 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1809
            else
1810
               Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1811
            end if;
1812
 
1813
            Append_To (Args, Make_Identifier (Loc, Name_uChain));
1814
            Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1815
            First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1816
         end if;
1817
 
1818
         --  Append discriminant values
1819
 
1820
         if Has_Discriminants (Uparent_Type) then
1821
            pragma Assert (not Is_Tagged_Type (Uparent_Type));
1822
 
1823
            Parent_Discr := First_Discriminant (Uparent_Type);
1824
            while Present (Parent_Discr) loop
1825
 
1826
               --  Get the initial value for this discriminant
1827
               --  ??? needs to be cleaned up to use parent_Discr_Constr
1828
               --  directly.
1829
 
1830
               declare
1831
                  Discr       : Entity_Id :=
1832
                                  First_Stored_Discriminant (Uparent_Type);
1833
 
1834
                  Discr_Value : Elmt_Id :=
1835
                                  First_Elmt (Stored_Constraint (Rec_Type));
1836
 
1837
               begin
1838
                  while Original_Record_Component (Parent_Discr) /= Discr loop
1839
                     Next_Stored_Discriminant (Discr);
1840
                     Next_Elmt (Discr_Value);
1841
                  end loop;
1842
 
1843
                  Arg := Node (Discr_Value);
1844
               end;
1845
 
1846
               --  Append it to the list
1847
 
1848
               if Nkind (Arg) = N_Identifier
1849
                  and then Ekind (Entity (Arg)) = E_Discriminant
1850
               then
1851
                  Append_To (Args,
1852
                    New_Reference_To (Discriminal (Entity (Arg)), Loc));
1853
 
1854
               --  Case of access discriminants. We replace the reference
1855
               --  to the type by a reference to the actual object.
1856
 
1857
               --  Is above comment right??? Use of New_Copy below seems mighty
1858
               --  suspicious ???
1859
 
1860
               else
1861
                  Append_To (Args, New_Copy (Arg));
1862
               end if;
1863
 
1864
               Next_Discriminant (Parent_Discr);
1865
            end loop;
1866
         end if;
1867
 
1868
         Res :=
1869
           New_List (
1870
             Make_Procedure_Call_Statement (Loc,
1871
               Name                   =>
1872
                 New_Occurrence_Of (Parent_Proc, Loc),
1873
               Parameter_Associations => Args));
1874
 
1875
         return Res;
1876
      end Build_Init_Call_Thru;
1877
 
1878
      -----------------------------------
1879
      -- Build_Offset_To_Top_Functions --
1880
      -----------------------------------
1881
 
1882
      procedure Build_Offset_To_Top_Functions is
1883
 
1884
         procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
1885
         --  Generate:
1886
         --    function Fxx (O : in Rec_Typ) return Storage_Offset is
1887
         --    begin
1888
         --       return O.Iface_Comp'Position;
1889
         --    end Fxx;
1890
 
1891
         ----------------------------------
1892
         -- Build_Offset_To_Top_Function --
1893
         ----------------------------------
1894
 
1895
         procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
1896
            Body_Node : Node_Id;
1897
            Func_Id   : Entity_Id;
1898
            Spec_Node : Node_Id;
1899
 
1900
         begin
1901
            Func_Id := Make_Temporary (Loc, 'F');
1902
            Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
1903
 
1904
            --  Generate
1905
            --    function Fxx (O : in Rec_Typ) return Storage_Offset;
1906
 
1907
            Spec_Node := New_Node (N_Function_Specification, Loc);
1908
            Set_Defining_Unit_Name (Spec_Node, Func_Id);
1909
            Set_Parameter_Specifications (Spec_Node, New_List (
1910
              Make_Parameter_Specification (Loc,
1911
                Defining_Identifier =>
1912
                  Make_Defining_Identifier (Loc, Name_uO),
1913
                In_Present          => True,
1914
                Parameter_Type      =>
1915
                  New_Reference_To (Rec_Type, Loc))));
1916
            Set_Result_Definition (Spec_Node,
1917
              New_Reference_To (RTE (RE_Storage_Offset), Loc));
1918
 
1919
            --  Generate
1920
            --    function Fxx (O : in Rec_Typ) return Storage_Offset is
1921
            --    begin
1922
            --       return O.Iface_Comp'Position;
1923
            --    end Fxx;
1924
 
1925
            Body_Node := New_Node (N_Subprogram_Body, Loc);
1926
            Set_Specification (Body_Node, Spec_Node);
1927
            Set_Declarations (Body_Node, New_List);
1928
            Set_Handled_Statement_Sequence (Body_Node,
1929
              Make_Handled_Sequence_Of_Statements (Loc,
1930
                Statements     => New_List (
1931
                  Make_Simple_Return_Statement (Loc,
1932
                    Expression =>
1933
                      Make_Attribute_Reference (Loc,
1934
                        Prefix         =>
1935
                          Make_Selected_Component (Loc,
1936
                            Prefix        => Make_Identifier (Loc, Name_uO),
1937
                            Selector_Name =>
1938
                              New_Reference_To (Iface_Comp, Loc)),
1939
                        Attribute_Name => Name_Position)))));
1940
 
1941
            Set_Ekind       (Func_Id, E_Function);
1942
            Set_Mechanism   (Func_Id, Default_Mechanism);
1943
            Set_Is_Internal (Func_Id, True);
1944
 
1945
            if not Debug_Generated_Code then
1946
               Set_Debug_Info_Off (Func_Id);
1947
            end if;
1948
 
1949
            Analyze (Body_Node);
1950
 
1951
            Append_Freeze_Action (Rec_Type, Body_Node);
1952
         end Build_Offset_To_Top_Function;
1953
 
1954
         --  Local variables
1955
 
1956
         Iface_Comp       : Node_Id;
1957
         Iface_Comp_Elmt  : Elmt_Id;
1958
         Ifaces_Comp_List : Elist_Id;
1959
 
1960
      --  Start of processing for Build_Offset_To_Top_Functions
1961
 
1962
      begin
1963
         --  Offset_To_Top_Functions are built only for derivations of types
1964
         --  with discriminants that cover interface types.
1965
         --  Nothing is needed either in case of virtual machines, since
1966
         --  interfaces are handled directly by the VM.
1967
 
1968
         if not Is_Tagged_Type (Rec_Type)
1969
           or else Etype (Rec_Type) = Rec_Type
1970
           or else not Has_Discriminants (Etype (Rec_Type))
1971
           or else not Tagged_Type_Expansion
1972
         then
1973
            return;
1974
         end if;
1975
 
1976
         Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
1977
 
1978
         --  For each interface type with secondary dispatch table we generate
1979
         --  the Offset_To_Top_Functions (required to displace the pointer in
1980
         --  interface conversions)
1981
 
1982
         Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
1983
         while Present (Iface_Comp_Elmt) loop
1984
            Iface_Comp := Node (Iface_Comp_Elmt);
1985
            pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
1986
 
1987
            --  If the interface is a parent of Rec_Type it shares the primary
1988
            --  dispatch table and hence there is no need to build the function
1989
 
1990
            if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
1991
                                Use_Full_View => True)
1992
            then
1993
               Build_Offset_To_Top_Function (Iface_Comp);
1994
            end if;
1995
 
1996
            Next_Elmt (Iface_Comp_Elmt);
1997
         end loop;
1998
      end Build_Offset_To_Top_Functions;
1999
 
2000
      ------------------------------
2001
      -- Build_CPP_Init_Procedure --
2002
      ------------------------------
2003
 
2004
      procedure Build_CPP_Init_Procedure is
2005
         Body_Node         : Node_Id;
2006
         Body_Stmts        : List_Id;
2007
         Flag_Id           : Entity_Id;
2008
         Flag_Decl         : Node_Id;
2009
         Handled_Stmt_Node : Node_Id;
2010
         Init_Tags_List    : List_Id;
2011
         Proc_Id           : Entity_Id;
2012
         Proc_Spec_Node    : Node_Id;
2013
 
2014
      begin
2015
         --  Check cases requiring no IC routine
2016
 
2017
         if not Is_CPP_Class (Root_Type (Rec_Type))
2018
           or else Is_CPP_Class (Rec_Type)
2019
           or else CPP_Num_Prims (Rec_Type) = 0
2020
           or else not Tagged_Type_Expansion
2021
           or else No_Run_Time_Mode
2022
         then
2023
            return;
2024
         end if;
2025
 
2026
         --  Generate:
2027
 
2028
         --     Flag : Boolean := False;
2029
         --
2030
         --     procedure Typ_IC is
2031
         --     begin
2032
         --        if not Flag then
2033
         --           Copy C++ dispatch table slots from parent
2034
         --           Update C++ slots of overridden primitives
2035
         --        end if;
2036
         --     end;
2037
 
2038
         Flag_Id := Make_Temporary (Loc, 'F');
2039
 
2040
         Flag_Decl :=
2041
           Make_Object_Declaration (Loc,
2042
             Defining_Identifier => Flag_Id,
2043
             Object_Definition =>
2044
               New_Reference_To (Standard_Boolean, Loc),
2045
             Expression =>
2046
               New_Reference_To (Standard_True, Loc));
2047
 
2048
         Analyze (Flag_Decl);
2049
         Append_Freeze_Action (Rec_Type, Flag_Decl);
2050
 
2051
         Body_Stmts := New_List;
2052
         Body_Node := New_Node (N_Subprogram_Body, Loc);
2053
 
2054
         Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2055
 
2056
         Proc_Id :=
2057
           Make_Defining_Identifier (Loc,
2058
             Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2059
 
2060
         Set_Ekind       (Proc_Id, E_Procedure);
2061
         Set_Is_Internal (Proc_Id);
2062
 
2063
         Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2064
 
2065
         Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2066
         Set_Specification (Body_Node, Proc_Spec_Node);
2067
         Set_Declarations (Body_Node, New_List);
2068
 
2069
         Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2070
 
2071
         Append_To (Init_Tags_List,
2072
           Make_Assignment_Statement (Loc,
2073
             Name =>
2074
               New_Reference_To (Flag_Id, Loc),
2075
             Expression =>
2076
               New_Reference_To (Standard_False, Loc)));
2077
 
2078
         Append_To (Body_Stmts,
2079
           Make_If_Statement (Loc,
2080
             Condition => New_Occurrence_Of (Flag_Id, Loc),
2081
             Then_Statements => Init_Tags_List));
2082
 
2083
         Handled_Stmt_Node :=
2084
           New_Node (N_Handled_Sequence_Of_Statements, Loc);
2085
         Set_Statements (Handled_Stmt_Node, Body_Stmts);
2086
         Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2087
         Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2088
 
2089
         if not Debug_Generated_Code then
2090
            Set_Debug_Info_Off (Proc_Id);
2091
         end if;
2092
 
2093
         --  Associate CPP_Init_Proc with type
2094
 
2095
         Set_Init_Proc (Rec_Type, Proc_Id);
2096
      end Build_CPP_Init_Procedure;
2097
 
2098
      --------------------------
2099
      -- Build_Init_Procedure --
2100
      --------------------------
2101
 
2102
      procedure Build_Init_Procedure is
2103
         Body_Stmts            : List_Id;
2104
         Body_Node             : Node_Id;
2105
         Handled_Stmt_Node     : Node_Id;
2106
         Init_Tags_List        : List_Id;
2107
         Parameters            : List_Id;
2108
         Proc_Spec_Node        : Node_Id;
2109
         Record_Extension_Node : Node_Id;
2110
 
2111
      begin
2112
         Body_Stmts := New_List;
2113
         Body_Node := New_Node (N_Subprogram_Body, Loc);
2114
         Set_Ekind (Proc_Id, E_Procedure);
2115
 
2116
         Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2117
         Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2118
 
2119
         Parameters := Init_Formals (Rec_Type);
2120
         Append_List_To (Parameters,
2121
           Build_Discriminant_Formals (Rec_Type, True));
2122
 
2123
         --  For tagged types, we add a flag to indicate whether the routine
2124
         --  is called to initialize a parent component in the init_proc of
2125
         --  a type extension. If the flag is false, we do not set the tag
2126
         --  because it has been set already in the extension.
2127
 
2128
         if Is_Tagged_Type (Rec_Type) then
2129
            Set_Tag := Make_Temporary (Loc, 'P');
2130
 
2131
            Append_To (Parameters,
2132
              Make_Parameter_Specification (Loc,
2133
                Defining_Identifier => Set_Tag,
2134
                Parameter_Type =>
2135
                  New_Occurrence_Of (Standard_Boolean, Loc),
2136
                Expression =>
2137
                  New_Occurrence_Of (Standard_True, Loc)));
2138
         end if;
2139
 
2140
         Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2141
         Set_Specification (Body_Node, Proc_Spec_Node);
2142
         Set_Declarations (Body_Node, Decls);
2143
 
2144
         --  N is a Derived_Type_Definition that renames the parameters of the
2145
         --  ancestor type. We initialize it by expanding our discriminants and
2146
         --  call the ancestor _init_proc with a type-converted object.
2147
 
2148
         if Parent_Subtype_Renaming_Discrims then
2149
            Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2150
 
2151
         elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2152
            Build_Discriminant_Assignments (Body_Stmts);
2153
 
2154
            if not Null_Present (Type_Definition (N)) then
2155
               Append_List_To (Body_Stmts,
2156
                 Build_Init_Statements (
2157
                   Component_List (Type_Definition (N))));
2158
            end if;
2159
 
2160
         --  N is a Derived_Type_Definition with a possible non-empty
2161
         --  extension. The initialization of a type extension consists in the
2162
         --  initialization of the components in the extension.
2163
 
2164
         else
2165
            Build_Discriminant_Assignments (Body_Stmts);
2166
 
2167
            Record_Extension_Node :=
2168
              Record_Extension_Part (Type_Definition (N));
2169
 
2170
            if not Null_Present (Record_Extension_Node) then
2171
               declare
2172
                  Stmts : constant List_Id :=
2173
                            Build_Init_Statements (
2174
                              Component_List (Record_Extension_Node));
2175
 
2176
               begin
2177
                  --  The parent field must be initialized first because
2178
                  --  the offset of the new discriminants may depend on it
2179
 
2180
                  Prepend_To (Body_Stmts, Remove_Head (Stmts));
2181
                  Append_List_To (Body_Stmts, Stmts);
2182
               end;
2183
            end if;
2184
         end if;
2185
 
2186
         --  Add here the assignment to instantiate the Tag
2187
 
2188
         --  The assignment corresponds to the code:
2189
 
2190
         --     _Init._Tag := Typ'Tag;
2191
 
2192
         --  Suppress the tag assignment when VM_Target because VM tags are
2193
         --  represented implicitly in objects. It is also suppressed in case
2194
         --  of CPP_Class types because in this case the tag is initialized in
2195
         --  the C++ side.
2196
 
2197
         if Is_Tagged_Type (Rec_Type)
2198
           and then Tagged_Type_Expansion
2199
           and then not No_Run_Time_Mode
2200
         then
2201
            --  Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2202
            --  the actual object and invoke the IP of the parent (in this
2203
            --  order). The tag must be initialized before the call to the IP
2204
            --  of the parent and the assignments to other components because
2205
            --  the initial value of the components may depend on the tag (eg.
2206
            --  through a dispatching operation on an access to the current
2207
            --  type). The tag assignment is not done when initializing the
2208
            --  parent component of a type extension, because in that case the
2209
            --  tag is set in the extension.
2210
 
2211
            if not Is_CPP_Class (Root_Type (Rec_Type)) then
2212
 
2213
               --  Initialize the primary tag component
2214
 
2215
               Init_Tags_List := New_List (
2216
                 Make_Assignment_Statement (Loc,
2217
                   Name =>
2218
                     Make_Selected_Component (Loc,
2219
                       Prefix        => Make_Identifier (Loc, Name_uInit),
2220
                       Selector_Name =>
2221
                         New_Reference_To
2222
                           (First_Tag_Component (Rec_Type), Loc)),
2223
                   Expression =>
2224
                     New_Reference_To
2225
                       (Node
2226
                         (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2227
 
2228
               --  Ada 2005 (AI-251): Initialize the secondary tags components
2229
               --  located at fixed positions (tags whose position depends on
2230
               --  variable size components are initialized later ---see below)
2231
 
2232
               if Ada_Version >= Ada_2005
2233
                 and then not Is_Interface (Rec_Type)
2234
                 and then Has_Interfaces (Rec_Type)
2235
               then
2236
                  Init_Secondary_Tags
2237
                    (Typ            => Rec_Type,
2238
                     Target         => Make_Identifier (Loc, Name_uInit),
2239
                     Stmts_List     => Init_Tags_List,
2240
                     Fixed_Comps    => True,
2241
                     Variable_Comps => False);
2242
               end if;
2243
 
2244
               Prepend_To (Body_Stmts,
2245
                 Make_If_Statement (Loc,
2246
                   Condition => New_Occurrence_Of (Set_Tag, Loc),
2247
                   Then_Statements => Init_Tags_List));
2248
 
2249
            --  Case 2: CPP type. The imported C++ constructor takes care of
2250
            --  tags initialization. No action needed here because the IP
2251
            --  is built by Set_CPP_Constructors; in this case the IP is a
2252
            --  wrapper that invokes the C++ constructor and copies the C++
2253
            --  tags locally. Done to inherit the C++ slots in Ada derivations
2254
            --  (see case 3).
2255
 
2256
            elsif Is_CPP_Class (Rec_Type) then
2257
               pragma Assert (False);
2258
               null;
2259
 
2260
            --  Case 3: Combined hierarchy containing C++ types and Ada tagged
2261
            --  type derivations. Derivations of imported C++ classes add a
2262
            --  complication, because we cannot inhibit tag setting in the
2263
            --  constructor for the parent. Hence we initialize the tag after
2264
            --  the call to the parent IP (that is, in reverse order compared
2265
            --  with pure Ada hierarchies ---see comment on case 1).
2266
 
2267
            else
2268
               --  Initialize the primary tag
2269
 
2270
               Init_Tags_List := New_List (
2271
                 Make_Assignment_Statement (Loc,
2272
                   Name =>
2273
                     Make_Selected_Component (Loc,
2274
                       Prefix        => Make_Identifier (Loc, Name_uInit),
2275
                       Selector_Name =>
2276
                         New_Reference_To
2277
                           (First_Tag_Component (Rec_Type), Loc)),
2278
                   Expression =>
2279
                     New_Reference_To
2280
                       (Node
2281
                         (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2282
 
2283
               --  Ada 2005 (AI-251): Initialize the secondary tags components
2284
               --  located at fixed positions (tags whose position depends on
2285
               --  variable size components are initialized later ---see below)
2286
 
2287
               if Ada_Version >= Ada_2005
2288
                 and then not Is_Interface (Rec_Type)
2289
                 and then Has_Interfaces (Rec_Type)
2290
               then
2291
                  Init_Secondary_Tags
2292
                    (Typ            => Rec_Type,
2293
                     Target         => Make_Identifier (Loc, Name_uInit),
2294
                     Stmts_List     => Init_Tags_List,
2295
                     Fixed_Comps    => True,
2296
                     Variable_Comps => False);
2297
               end if;
2298
 
2299
               --  Initialize the tag component after invocation of parent IP.
2300
 
2301
               --  Generate:
2302
               --     parent_IP(_init.parent); // Invokes the C++ constructor
2303
               --     [ typIC; ]               // Inherit C++ slots from parent
2304
               --     init_tags
2305
 
2306
               declare
2307
                  Ins_Nod : Node_Id;
2308
 
2309
               begin
2310
                  --  Search for the call to the IP of the parent. We assume
2311
                  --  that the first init_proc call is for the parent.
2312
 
2313
                  Ins_Nod := First (Body_Stmts);
2314
                  while Present (Next (Ins_Nod))
2315
                     and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2316
                                or else not Is_Init_Proc (Name (Ins_Nod)))
2317
                  loop
2318
                     Next (Ins_Nod);
2319
                  end loop;
2320
 
2321
                  --  The IC routine copies the inherited slots of the C+ part
2322
                  --  of the dispatch table from the parent and updates the
2323
                  --  overridden C++ slots.
2324
 
2325
                  if CPP_Num_Prims (Rec_Type) > 0 then
2326
                     declare
2327
                        Init_DT : Entity_Id;
2328
                        New_Nod : Node_Id;
2329
 
2330
                     begin
2331
                        Init_DT := CPP_Init_Proc (Rec_Type);
2332
                        pragma Assert (Present (Init_DT));
2333
 
2334
                        New_Nod :=
2335
                          Make_Procedure_Call_Statement (Loc,
2336
                            New_Reference_To (Init_DT, Loc));
2337
                        Insert_After (Ins_Nod, New_Nod);
2338
 
2339
                        --  Update location of init tag statements
2340
 
2341
                        Ins_Nod := New_Nod;
2342
                     end;
2343
                  end if;
2344
 
2345
                  Insert_List_After (Ins_Nod, Init_Tags_List);
2346
               end;
2347
            end if;
2348
 
2349
            --  Ada 2005 (AI-251): Initialize the secondary tag components
2350
            --  located at variable positions. We delay the generation of this
2351
            --  code until here because the value of the attribute 'Position
2352
            --  applied to variable size components of the parent type that
2353
            --  depend on discriminants is only safely read at runtime after
2354
            --  the parent components have been initialized.
2355
 
2356
            if Ada_Version >= Ada_2005
2357
              and then not Is_Interface (Rec_Type)
2358
              and then Has_Interfaces (Rec_Type)
2359
              and then Has_Discriminants (Etype (Rec_Type))
2360
              and then Is_Variable_Size_Record (Etype (Rec_Type))
2361
            then
2362
               Init_Tags_List := New_List;
2363
 
2364
               Init_Secondary_Tags
2365
                 (Typ            => Rec_Type,
2366
                  Target         => Make_Identifier (Loc, Name_uInit),
2367
                  Stmts_List     => Init_Tags_List,
2368
                  Fixed_Comps    => False,
2369
                  Variable_Comps => True);
2370
 
2371
               if Is_Non_Empty_List (Init_Tags_List) then
2372
                  Append_List_To (Body_Stmts, Init_Tags_List);
2373
               end if;
2374
            end if;
2375
         end if;
2376
 
2377
         Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2378
         Set_Statements (Handled_Stmt_Node, Body_Stmts);
2379
 
2380
         --  Generate:
2381
         --    Local_DF_Id (_init, C1, ..., CN);
2382
         --    raise;
2383
 
2384
         if Counter > 0
2385
           and then Needs_Finalization (Rec_Type)
2386
           and then not Is_Abstract_Type (Rec_Type)
2387
           and then not Restriction_Active (No_Exception_Propagation)
2388
         then
2389
            declare
2390
               Local_DF_Id : Entity_Id;
2391
 
2392
            begin
2393
               --  Create a local version of Deep_Finalize which has indication
2394
               --  of partial initialization state.
2395
 
2396
               Local_DF_Id := Make_Temporary (Loc, 'F');
2397
 
2398
               Append_To (Decls,
2399
                 Make_Local_Deep_Finalize (Rec_Type, Local_DF_Id));
2400
 
2401
               Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2402
                 Make_Exception_Handler (Loc,
2403
                   Exception_Choices => New_List (
2404
                     Make_Others_Choice (Loc)),
2405
 
2406
                   Statements => New_List (
2407
                     Make_Procedure_Call_Statement (Loc,
2408
                       Name =>
2409
                         New_Reference_To (Local_DF_Id, Loc),
2410
 
2411
                       Parameter_Associations => New_List (
2412
                         Make_Identifier (Loc, Name_uInit),
2413
                         New_Reference_To (Standard_False, Loc))),
2414
 
2415
                     Make_Raise_Statement (Loc)))));
2416
            end;
2417
         else
2418
            Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2419
         end if;
2420
 
2421
         Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2422
 
2423
         if not Debug_Generated_Code then
2424
            Set_Debug_Info_Off (Proc_Id);
2425
         end if;
2426
 
2427
         --  Associate Init_Proc with type, and determine if the procedure
2428
         --  is null (happens because of the Initialize_Scalars pragma case,
2429
         --  where we have to generate a null procedure in case it is called
2430
         --  by a client with Initialize_Scalars set). Such procedures have
2431
         --  to be generated, but do not have to be called, so we mark them
2432
         --  as null to suppress the call.
2433
 
2434
         Set_Init_Proc (Rec_Type, Proc_Id);
2435
 
2436
         if List_Length (Body_Stmts) = 1
2437
 
2438
           --  We must skip SCIL nodes because they may have been added to this
2439
           --  list by Insert_Actions.
2440
 
2441
           and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2442
           and then VM_Target = No_VM
2443
         then
2444
            --  Even though the init proc may be null at this time it might get
2445
            --  some stuff added to it later by the VM backend.
2446
 
2447
            Set_Is_Null_Init_Proc (Proc_Id);
2448
         end if;
2449
      end Build_Init_Procedure;
2450
 
2451
      ---------------------------
2452
      -- Build_Init_Statements --
2453
      ---------------------------
2454
 
2455
      function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2456
         Checks     : constant List_Id := New_List;
2457
         Actions    : List_Id   := No_List;
2458
         Comp_Loc   : Source_Ptr;
2459
         Counter_Id : Entity_Id := Empty;
2460
         Decl       : Node_Id;
2461
         Has_POC    : Boolean;
2462
         Id         : Entity_Id;
2463
         Names      : Node_Id;
2464
         Stmts      : List_Id;
2465
         Typ        : Entity_Id;
2466
 
2467
         procedure Increment_Counter (Loc : Source_Ptr);
2468
         --  Generate an "increment by one" statement for the current counter
2469
         --  and append it to the list Stmts.
2470
 
2471
         procedure Make_Counter (Loc : Source_Ptr);
2472
         --  Create a new counter for the current component list. The routine
2473
         --  creates a new defining Id, adds an object declaration and sets
2474
         --  the Id generator for the next variant.
2475
 
2476
         -----------------------
2477
         -- Increment_Counter --
2478
         -----------------------
2479
 
2480
         procedure Increment_Counter (Loc : Source_Ptr) is
2481
         begin
2482
            --  Generate:
2483
            --    Counter := Counter + 1;
2484
 
2485
            Append_To (Stmts,
2486
              Make_Assignment_Statement (Loc,
2487
                Name       => New_Reference_To (Counter_Id, Loc),
2488
                Expression =>
2489
                  Make_Op_Add (Loc,
2490
                    Left_Opnd  => New_Reference_To (Counter_Id, Loc),
2491
                    Right_Opnd => Make_Integer_Literal (Loc, 1))));
2492
         end Increment_Counter;
2493
 
2494
         ------------------
2495
         -- Make_Counter --
2496
         ------------------
2497
 
2498
         procedure Make_Counter (Loc : Source_Ptr) is
2499
         begin
2500
            --  Increment the Id generator
2501
 
2502
            Counter := Counter + 1;
2503
 
2504
            --  Create the entity and declaration
2505
 
2506
            Counter_Id :=
2507
              Make_Defining_Identifier (Loc,
2508
                Chars => New_External_Name ('C', Counter));
2509
 
2510
            --  Generate:
2511
            --    Cnn : Integer := 0;
2512
 
2513
            Append_To (Decls,
2514
              Make_Object_Declaration (Loc,
2515
                Defining_Identifier => Counter_Id,
2516
                Object_Definition   =>
2517
                  New_Reference_To (Standard_Integer, Loc),
2518
                Expression          =>
2519
                  Make_Integer_Literal (Loc, 0)));
2520
         end Make_Counter;
2521
 
2522
      --  Start of processing for Build_Init_Statements
2523
 
2524
      begin
2525
         if Null_Present (Comp_List) then
2526
            return New_List (Make_Null_Statement (Loc));
2527
         end if;
2528
 
2529
         Stmts := New_List;
2530
 
2531
         --  Loop through visible declarations of task types and protected
2532
         --  types moving any expanded code from the spec to the body of the
2533
         --  init procedure.
2534
 
2535
         if Is_Task_Record_Type (Rec_Type)
2536
           or else Is_Protected_Record_Type (Rec_Type)
2537
         then
2538
            declare
2539
               Decl : constant Node_Id :=
2540
                        Parent (Corresponding_Concurrent_Type (Rec_Type));
2541
               Def  : Node_Id;
2542
               N1   : Node_Id;
2543
               N2   : Node_Id;
2544
 
2545
            begin
2546
               if Is_Task_Record_Type (Rec_Type) then
2547
                  Def := Task_Definition (Decl);
2548
               else
2549
                  Def := Protected_Definition (Decl);
2550
               end if;
2551
 
2552
               if Present (Def) then
2553
                  N1 := First (Visible_Declarations (Def));
2554
                  while Present (N1) loop
2555
                     N2 := N1;
2556
                     N1 := Next (N1);
2557
 
2558
                     if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2559
                       or else Nkind (N2) in N_Raise_xxx_Error
2560
                       or else Nkind (N2) = N_Procedure_Call_Statement
2561
                     then
2562
                        Append_To (Stmts,
2563
                          New_Copy_Tree (N2, New_Scope => Proc_Id));
2564
                        Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2565
                        Analyze (N2);
2566
                     end if;
2567
                  end loop;
2568
               end if;
2569
            end;
2570
         end if;
2571
 
2572
         --  Loop through components, skipping pragmas, in 2 steps. The first
2573
         --  step deals with regular components. The second step deals with
2574
         --  components have per object constraints, and no explicit initia-
2575
         --  lization.
2576
 
2577
         Has_POC := False;
2578
 
2579
         --  First pass : regular components
2580
 
2581
         Decl := First_Non_Pragma (Component_Items (Comp_List));
2582
         while Present (Decl) loop
2583
            Comp_Loc := Sloc (Decl);
2584
            Build_Record_Checks
2585
              (Subtype_Indication (Component_Definition (Decl)), Checks);
2586
 
2587
            Id  := Defining_Identifier (Decl);
2588
            Typ := Etype (Id);
2589
 
2590
            --  Leave any processing of per-object constrained component for
2591
            --  the second pass.
2592
 
2593
            if Has_Access_Constraint (Id)
2594
              and then No (Expression (Decl))
2595
            then
2596
               Has_POC := True;
2597
 
2598
            --  Regular component cases
2599
 
2600
            else
2601
               --  Explicit initialization
2602
 
2603
               if Present (Expression (Decl)) then
2604
                  if Is_CPP_Constructor_Call (Expression (Decl)) then
2605
                     Actions :=
2606
                       Build_Initialization_Call
2607
                         (Comp_Loc,
2608
                          Id_Ref          =>
2609
                            Make_Selected_Component (Comp_Loc,
2610
                              Prefix        =>
2611
                                Make_Identifier (Comp_Loc, Name_uInit),
2612
                              Selector_Name =>
2613
                                New_Occurrence_Of (Id, Comp_Loc)),
2614
                          Typ             => Typ,
2615
                          In_Init_Proc    => True,
2616
                          Enclos_Type     => Rec_Type,
2617
                          Discr_Map       => Discr_Map,
2618
                          Constructor_Ref => Expression (Decl));
2619
                  else
2620
                     Actions := Build_Assignment (Id, Expression (Decl));
2621
                  end if;
2622
 
2623
               --  Composite component with its own Init_Proc
2624
 
2625
               elsif not Is_Interface (Typ)
2626
                 and then Has_Non_Null_Base_Init_Proc (Typ)
2627
               then
2628
                  Actions :=
2629
                    Build_Initialization_Call
2630
                      (Comp_Loc,
2631
                       Make_Selected_Component (Comp_Loc,
2632
                         Prefix        =>
2633
                           Make_Identifier (Comp_Loc, Name_uInit),
2634
                         Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2635
                       Typ,
2636
                       In_Init_Proc => True,
2637
                       Enclos_Type  => Rec_Type,
2638
                       Discr_Map    => Discr_Map);
2639
 
2640
                  Clean_Task_Names (Typ, Proc_Id);
2641
 
2642
               --  Simple initialization
2643
 
2644
               elsif Component_Needs_Simple_Initialization (Typ) then
2645
                  Actions :=
2646
                    Build_Assignment
2647
                      (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2648
 
2649
               --  Nothing needed for this case
2650
 
2651
               else
2652
                  Actions := No_List;
2653
               end if;
2654
 
2655
               if Present (Checks) then
2656
                  Append_List_To (Stmts, Checks);
2657
               end if;
2658
 
2659
               if Present (Actions) then
2660
                  Append_List_To (Stmts, Actions);
2661
 
2662
                  --  Preserve the initialization state in the current counter
2663
 
2664
                  if Chars (Id) /= Name_uParent
2665
                    and then Needs_Finalization (Typ)
2666
                  then
2667
                     if No (Counter_Id) then
2668
                        Make_Counter (Comp_Loc);
2669
                     end if;
2670
 
2671
                     Increment_Counter (Comp_Loc);
2672
                  end if;
2673
               end if;
2674
            end if;
2675
 
2676
            Next_Non_Pragma (Decl);
2677
         end loop;
2678
 
2679
         --  Set up tasks and protected object support. This needs to be done
2680
         --  before any component with a per-object access discriminant
2681
         --  constraint, or any variant part (which may contain such
2682
         --  components) is initialized, because the initialization of these
2683
         --  components may reference the enclosing concurrent object.
2684
 
2685
         --  For a task record type, add the task create call and calls to bind
2686
         --  any interrupt (signal) entries.
2687
 
2688
         if Is_Task_Record_Type (Rec_Type) then
2689
 
2690
            --  In the case of the restricted run time the ATCB has already
2691
            --  been preallocated.
2692
 
2693
            if Restricted_Profile then
2694
               Append_To (Stmts,
2695
                 Make_Assignment_Statement (Loc,
2696
                   Name       =>
2697
                     Make_Selected_Component (Loc,
2698
                       Prefix        => Make_Identifier (Loc, Name_uInit),
2699
                       Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2700
                   Expression =>
2701
                     Make_Attribute_Reference (Loc,
2702
                       Prefix         =>
2703
                         Make_Selected_Component (Loc,
2704
                           Prefix        => Make_Identifier (Loc, Name_uInit),
2705
                           Selector_Name => Make_Identifier (Loc, Name_uATCB)),
2706
                       Attribute_Name => Name_Unchecked_Access)));
2707
            end if;
2708
 
2709
            Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
2710
 
2711
            --  Generate the statements which map a string entry name to a
2712
            --  task entry index. Note that the task may not have entries.
2713
 
2714
            if Entry_Names_OK then
2715
               Names := Build_Entry_Names (Rec_Type);
2716
 
2717
               if Present (Names) then
2718
                  Append_To (Stmts, Names);
2719
               end if;
2720
            end if;
2721
 
2722
            declare
2723
               Task_Type : constant Entity_Id :=
2724
                             Corresponding_Concurrent_Type (Rec_Type);
2725
               Task_Decl : constant Node_Id := Parent (Task_Type);
2726
               Task_Def  : constant Node_Id := Task_Definition (Task_Decl);
2727
               Decl_Loc  : Source_Ptr;
2728
               Ent       : Entity_Id;
2729
               Vis_Decl  : Node_Id;
2730
 
2731
            begin
2732
               if Present (Task_Def) then
2733
                  Vis_Decl := First (Visible_Declarations (Task_Def));
2734
                  while Present (Vis_Decl) loop
2735
                     Decl_Loc := Sloc (Vis_Decl);
2736
 
2737
                     if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2738
                        if Get_Attribute_Id (Chars (Vis_Decl)) =
2739
                                                       Attribute_Address
2740
                        then
2741
                           Ent := Entity (Name (Vis_Decl));
2742
 
2743
                           if Ekind (Ent) = E_Entry then
2744
                              Append_To (Stmts,
2745
                                Make_Procedure_Call_Statement (Decl_Loc,
2746
                                  Name =>
2747
                                    New_Reference_To (RTE (
2748
                                      RE_Bind_Interrupt_To_Entry), Decl_Loc),
2749
                                  Parameter_Associations => New_List (
2750
                                    Make_Selected_Component (Decl_Loc,
2751
                                      Prefix        =>
2752
                                        Make_Identifier (Decl_Loc, Name_uInit),
2753
                                      Selector_Name =>
2754
                                        Make_Identifier
2755
                                         (Decl_Loc, Name_uTask_Id)),
2756
                                    Entry_Index_Expression
2757
                                      (Decl_Loc, Ent, Empty, Task_Type),
2758
                                    Expression (Vis_Decl))));
2759
                           end if;
2760
                        end if;
2761
                     end if;
2762
 
2763
                     Next (Vis_Decl);
2764
                  end loop;
2765
               end if;
2766
            end;
2767
         end if;
2768
 
2769
         --  For a protected type, add statements generated by
2770
         --  Make_Initialize_Protection.
2771
 
2772
         if Is_Protected_Record_Type (Rec_Type) then
2773
            Append_List_To (Stmts,
2774
              Make_Initialize_Protection (Rec_Type));
2775
 
2776
            --  Generate the statements which map a string entry name to a
2777
            --  protected entry index. Note that the protected type may not
2778
            --  have entries.
2779
 
2780
            if Entry_Names_OK then
2781
               Names := Build_Entry_Names (Rec_Type);
2782
 
2783
               if Present (Names) then
2784
                  Append_To (Stmts, Names);
2785
               end if;
2786
            end if;
2787
         end if;
2788
 
2789
         --  Second pass: components with per-object constraints
2790
 
2791
         if Has_POC then
2792
            Decl := First_Non_Pragma (Component_Items (Comp_List));
2793
            while Present (Decl) loop
2794
               Comp_Loc := Sloc (Decl);
2795
               Id := Defining_Identifier (Decl);
2796
               Typ := Etype (Id);
2797
 
2798
               if Has_Access_Constraint (Id)
2799
                 and then No (Expression (Decl))
2800
               then
2801
                  if Has_Non_Null_Base_Init_Proc (Typ) then
2802
                     Append_List_To (Stmts,
2803
                       Build_Initialization_Call (Comp_Loc,
2804
                         Make_Selected_Component (Comp_Loc,
2805
                           Prefix        =>
2806
                             Make_Identifier (Comp_Loc, Name_uInit),
2807
                           Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2808
                         Typ,
2809
                         In_Init_Proc => True,
2810
                         Enclos_Type  => Rec_Type,
2811
                         Discr_Map    => Discr_Map));
2812
 
2813
                     Clean_Task_Names (Typ, Proc_Id);
2814
 
2815
                     --  Preserve the initialization state in the current
2816
                     --  counter.
2817
 
2818
                     if Needs_Finalization (Typ) then
2819
                        if No (Counter_Id) then
2820
                           Make_Counter (Comp_Loc);
2821
                        end if;
2822
 
2823
                        Increment_Counter (Comp_Loc);
2824
                     end if;
2825
 
2826
                  elsif Component_Needs_Simple_Initialization (Typ) then
2827
                     Append_List_To (Stmts,
2828
                       Build_Assignment
2829
                         (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
2830
                  end if;
2831
               end if;
2832
 
2833
               Next_Non_Pragma (Decl);
2834
            end loop;
2835
         end if;
2836
 
2837
         --  Process the variant part
2838
 
2839
         if Present (Variant_Part (Comp_List)) then
2840
            declare
2841
               Variant_Alts : constant List_Id := New_List;
2842
               Var_Loc      : Source_Ptr;
2843
               Variant      : Node_Id;
2844
 
2845
            begin
2846
               Variant :=
2847
                 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2848
               while Present (Variant) loop
2849
                  Var_Loc := Sloc (Variant);
2850
                  Append_To (Variant_Alts,
2851
                    Make_Case_Statement_Alternative (Var_Loc,
2852
                      Discrete_Choices =>
2853
                        New_Copy_List (Discrete_Choices (Variant)),
2854
                      Statements =>
2855
                        Build_Init_Statements (Component_List (Variant))));
2856
                  Next_Non_Pragma (Variant);
2857
               end loop;
2858
 
2859
               --  The expression of the case statement which is a reference
2860
               --  to one of the discriminants is replaced by the appropriate
2861
               --  formal parameter of the initialization procedure.
2862
 
2863
               Append_To (Stmts,
2864
                 Make_Case_Statement (Var_Loc,
2865
                   Expression =>
2866
                     New_Reference_To (Discriminal (
2867
                       Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
2868
                   Alternatives => Variant_Alts));
2869
            end;
2870
         end if;
2871
 
2872
         --  If no initializations when generated for component declarations
2873
         --  corresponding to this Stmts, append a null statement to Stmts to
2874
         --  to make it a valid Ada tree.
2875
 
2876
         if Is_Empty_List (Stmts) then
2877
            Append (New_Node (N_Null_Statement, Loc), Stmts);
2878
         end if;
2879
 
2880
         return Stmts;
2881
 
2882
      exception
2883
         when RE_Not_Available =>
2884
         return Empty_List;
2885
      end Build_Init_Statements;
2886
 
2887
      -------------------------
2888
      -- Build_Record_Checks --
2889
      -------------------------
2890
 
2891
      procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2892
         Subtype_Mark_Id : Entity_Id;
2893
 
2894
         procedure Constrain_Array
2895
           (SI         : Node_Id;
2896
            Check_List : List_Id);
2897
         --  Apply a list of index constraints to an unconstrained array type.
2898
         --  The first parameter is the entity for the resulting subtype.
2899
         --  Check_List is a list to which the check actions are appended.
2900
 
2901
         ---------------------
2902
         -- Constrain_Array --
2903
         ---------------------
2904
 
2905
         procedure Constrain_Array
2906
           (SI         : Node_Id;
2907
            Check_List : List_Id)
2908
         is
2909
            C                     : constant Node_Id := Constraint (SI);
2910
            Number_Of_Constraints : Nat := 0;
2911
            Index                 : Node_Id;
2912
            S, T                  : Entity_Id;
2913
 
2914
            procedure Constrain_Index
2915
              (Index      : Node_Id;
2916
               S          : Node_Id;
2917
               Check_List : List_Id);
2918
            --  Process an index constraint in a constrained array declaration.
2919
            --  The constraint can be either a subtype name or a range with or
2920
            --  without an explicit subtype mark. Index is the corresponding
2921
            --  index of the unconstrained array. S is the range expression.
2922
            --  Check_List is a list to which the check actions are appended.
2923
 
2924
            ---------------------
2925
            -- Constrain_Index --
2926
            ---------------------
2927
 
2928
            procedure Constrain_Index
2929
              (Index        : Node_Id;
2930
               S            : Node_Id;
2931
               Check_List   : List_Id)
2932
            is
2933
               T : constant Entity_Id := Etype (Index);
2934
 
2935
            begin
2936
               if Nkind (S) = N_Range then
2937
                  Process_Range_Expr_In_Decl (S, T, Check_List);
2938
               end if;
2939
            end Constrain_Index;
2940
 
2941
         --  Start of processing for Constrain_Array
2942
 
2943
         begin
2944
            T := Entity (Subtype_Mark (SI));
2945
 
2946
            if Ekind (T) in Access_Kind then
2947
               T := Designated_Type (T);
2948
            end if;
2949
 
2950
            S := First (Constraints (C));
2951
 
2952
            while Present (S) loop
2953
               Number_Of_Constraints := Number_Of_Constraints + 1;
2954
               Next (S);
2955
            end loop;
2956
 
2957
            --  In either case, the index constraint must provide a discrete
2958
            --  range for each index of the array type and the type of each
2959
            --  discrete range must be the same as that of the corresponding
2960
            --  index. (RM 3.6.1)
2961
 
2962
            S := First (Constraints (C));
2963
            Index := First_Index (T);
2964
            Analyze (Index);
2965
 
2966
            --  Apply constraints to each index type
2967
 
2968
            for J in 1 .. Number_Of_Constraints loop
2969
               Constrain_Index (Index, S, Check_List);
2970
               Next (Index);
2971
               Next (S);
2972
            end loop;
2973
         end Constrain_Array;
2974
 
2975
      --  Start of processing for Build_Record_Checks
2976
 
2977
      begin
2978
         if Nkind (S) = N_Subtype_Indication then
2979
            Find_Type (Subtype_Mark (S));
2980
            Subtype_Mark_Id := Entity (Subtype_Mark (S));
2981
 
2982
            --  Remaining processing depends on type
2983
 
2984
            case Ekind (Subtype_Mark_Id) is
2985
 
2986
               when Array_Kind =>
2987
                  Constrain_Array (S, Check_List);
2988
 
2989
               when others =>
2990
                  null;
2991
            end case;
2992
         end if;
2993
      end Build_Record_Checks;
2994
 
2995
      -------------------------------------------
2996
      -- Component_Needs_Simple_Initialization --
2997
      -------------------------------------------
2998
 
2999
      function Component_Needs_Simple_Initialization
3000
        (T : Entity_Id) return Boolean
3001
      is
3002
      begin
3003
         return
3004
           Needs_Simple_Initialization (T)
3005
             and then not Is_RTE (T, RE_Tag)
3006
 
3007
               --  Ada 2005 (AI-251): Check also the tag of abstract interfaces
3008
 
3009
             and then not Is_RTE (T, RE_Interface_Tag);
3010
      end Component_Needs_Simple_Initialization;
3011
 
3012
      --------------------------------------
3013
      -- Parent_Subtype_Renaming_Discrims --
3014
      --------------------------------------
3015
 
3016
      function Parent_Subtype_Renaming_Discrims return Boolean is
3017
         De : Entity_Id;
3018
         Dp : Entity_Id;
3019
 
3020
      begin
3021
         if Base_Type (Rec_Ent) /= Rec_Ent then
3022
            return False;
3023
         end if;
3024
 
3025
         if Etype (Rec_Ent) = Rec_Ent
3026
           or else not Has_Discriminants (Rec_Ent)
3027
           or else Is_Constrained (Rec_Ent)
3028
           or else Is_Tagged_Type (Rec_Ent)
3029
         then
3030
            return False;
3031
         end if;
3032
 
3033
         --  If there are no explicit stored discriminants we have inherited
3034
         --  the root type discriminants so far, so no renamings occurred.
3035
 
3036
         if First_Discriminant (Rec_Ent) =
3037
              First_Stored_Discriminant (Rec_Ent)
3038
         then
3039
            return False;
3040
         end if;
3041
 
3042
         --  Check if we have done some trivial renaming of the parent
3043
         --  discriminants, i.e. something like
3044
         --
3045
         --    type DT (X1, X2: int) is new PT (X1, X2);
3046
 
3047
         De := First_Discriminant (Rec_Ent);
3048
         Dp := First_Discriminant (Etype (Rec_Ent));
3049
         while Present (De) loop
3050
            pragma Assert (Present (Dp));
3051
 
3052
            if Corresponding_Discriminant (De) /= Dp then
3053
               return True;
3054
            end if;
3055
 
3056
            Next_Discriminant (De);
3057
            Next_Discriminant (Dp);
3058
         end loop;
3059
 
3060
         return Present (Dp);
3061
      end Parent_Subtype_Renaming_Discrims;
3062
 
3063
      ------------------------
3064
      -- Requires_Init_Proc --
3065
      ------------------------
3066
 
3067
      function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3068
         Comp_Decl : Node_Id;
3069
         Id        : Entity_Id;
3070
         Typ       : Entity_Id;
3071
 
3072
      begin
3073
         --  Definitely do not need one if specifically suppressed
3074
 
3075
         if Initialization_Suppressed (Rec_Id) then
3076
            return False;
3077
         end if;
3078
 
3079
         --  If it is a type derived from a type with unknown discriminants,
3080
         --  we cannot build an initialization procedure for it.
3081
 
3082
         if Has_Unknown_Discriminants (Rec_Id)
3083
           or else Has_Unknown_Discriminants (Etype (Rec_Id))
3084
         then
3085
            return False;
3086
         end if;
3087
 
3088
         --  Otherwise we need to generate an initialization procedure if
3089
         --  Is_CPP_Class is False and at least one of the following applies:
3090
 
3091
         --  1. Discriminants are present, since they need to be initialized
3092
         --     with the appropriate discriminant constraint expressions.
3093
         --     However, the discriminant of an unchecked union does not
3094
         --     count, since the discriminant is not present.
3095
 
3096
         --  2. The type is a tagged type, since the implicit Tag component
3097
         --     needs to be initialized with a pointer to the dispatch table.
3098
 
3099
         --  3. The type contains tasks
3100
 
3101
         --  4. One or more components has an initial value
3102
 
3103
         --  5. One or more components is for a type which itself requires
3104
         --     an initialization procedure.
3105
 
3106
         --  6. One or more components is a type that requires simple
3107
         --     initialization (see Needs_Simple_Initialization), except
3108
         --     that types Tag and Interface_Tag are excluded, since fields
3109
         --     of these types are initialized by other means.
3110
 
3111
         --  7. The type is the record type built for a task type (since at
3112
         --     the very least, Create_Task must be called)
3113
 
3114
         --  8. The type is the record type built for a protected type (since
3115
         --     at least Initialize_Protection must be called)
3116
 
3117
         --  9. The type is marked as a public entity. The reason we add this
3118
         --     case (even if none of the above apply) is to properly handle
3119
         --     Initialize_Scalars. If a package is compiled without an IS
3120
         --     pragma, and the client is compiled with an IS pragma, then
3121
         --     the client will think an initialization procedure is present
3122
         --     and call it, when in fact no such procedure is required, but
3123
         --     since the call is generated, there had better be a routine
3124
         --     at the other end of the call, even if it does nothing!)
3125
 
3126
         --  Note: the reason we exclude the CPP_Class case is because in this
3127
         --  case the initialization is performed by the C++ constructors, and
3128
         --  the IP is built by Set_CPP_Constructors.
3129
 
3130
         if Is_CPP_Class (Rec_Id) then
3131
            return False;
3132
 
3133
         elsif Is_Interface (Rec_Id) then
3134
            return False;
3135
 
3136
         elsif (Has_Discriminants (Rec_Id)
3137
                  and then not Is_Unchecked_Union (Rec_Id))
3138
           or else Is_Tagged_Type (Rec_Id)
3139
           or else Is_Concurrent_Record_Type (Rec_Id)
3140
           or else Has_Task (Rec_Id)
3141
         then
3142
            return True;
3143
         end if;
3144
 
3145
         Id := First_Component (Rec_Id);
3146
         while Present (Id) loop
3147
            Comp_Decl := Parent (Id);
3148
            Typ := Etype (Id);
3149
 
3150
            if Present (Expression (Comp_Decl))
3151
              or else Has_Non_Null_Base_Init_Proc (Typ)
3152
              or else Component_Needs_Simple_Initialization (Typ)
3153
            then
3154
               return True;
3155
            end if;
3156
 
3157
            Next_Component (Id);
3158
         end loop;
3159
 
3160
         --  As explained above, a record initialization procedure is needed
3161
         --  for public types in case Initialize_Scalars applies to a client.
3162
         --  However, such a procedure is not needed in the case where either
3163
         --  of restrictions No_Initialize_Scalars or No_Default_Initialization
3164
         --  applies. No_Initialize_Scalars excludes the possibility of using
3165
         --  Initialize_Scalars in any partition, and No_Default_Initialization
3166
         --  implies that no initialization should ever be done for objects of
3167
         --  the type, so is incompatible with Initialize_Scalars.
3168
 
3169
         if not Restriction_Active (No_Initialize_Scalars)
3170
           and then not Restriction_Active (No_Default_Initialization)
3171
           and then Is_Public (Rec_Id)
3172
         then
3173
            return True;
3174
         end if;
3175
 
3176
         return False;
3177
      end Requires_Init_Proc;
3178
 
3179
   --  Start of processing for Build_Record_Init_Proc
3180
 
3181
   begin
3182
      --  Check for value type, which means no initialization required
3183
 
3184
      Rec_Type := Defining_Identifier (N);
3185
 
3186
      if Is_Value_Type (Rec_Type) then
3187
         return;
3188
      end if;
3189
 
3190
      --  This may be full declaration of a private type, in which case
3191
      --  the visible entity is a record, and the private entity has been
3192
      --  exchanged with it in the private part of the current package.
3193
      --  The initialization procedure is built for the record type, which
3194
      --  is retrievable from the private entity.
3195
 
3196
      if Is_Incomplete_Or_Private_Type (Rec_Type) then
3197
         Rec_Type := Underlying_Type (Rec_Type);
3198
      end if;
3199
 
3200
      --  If there are discriminants, build the discriminant map to replace
3201
      --  discriminants by their discriminals in complex bound expressions.
3202
      --  These only arise for the corresponding records of synchronized types.
3203
 
3204
      if Is_Concurrent_Record_Type (Rec_Type)
3205
        and then Has_Discriminants (Rec_Type)
3206
      then
3207
         declare
3208
            Disc : Entity_Id;
3209
         begin
3210
            Disc := First_Discriminant (Rec_Type);
3211
            while Present (Disc) loop
3212
               Append_Elmt (Disc, Discr_Map);
3213
               Append_Elmt (Discriminal (Disc), Discr_Map);
3214
               Next_Discriminant (Disc);
3215
            end loop;
3216
         end;
3217
      end if;
3218
 
3219
      --  Derived types that have no type extension can use the initialization
3220
      --  procedure of their parent and do not need a procedure of their own.
3221
      --  This is only correct if there are no representation clauses for the
3222
      --  type or its parent, and if the parent has in fact been frozen so
3223
      --  that its initialization procedure exists.
3224
 
3225
      if Is_Derived_Type (Rec_Type)
3226
        and then not Is_Tagged_Type (Rec_Type)
3227
        and then not Is_Unchecked_Union (Rec_Type)
3228
        and then not Has_New_Non_Standard_Rep (Rec_Type)
3229
        and then not Parent_Subtype_Renaming_Discrims
3230
        and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3231
      then
3232
         Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3233
 
3234
      --  Otherwise if we need an initialization procedure, then build one,
3235
      --  mark it as public and inlinable and as having a completion.
3236
 
3237
      elsif Requires_Init_Proc (Rec_Type)
3238
        or else Is_Unchecked_Union (Rec_Type)
3239
      then
3240
         Proc_Id :=
3241
           Make_Defining_Identifier (Loc,
3242
             Chars => Make_Init_Proc_Name (Rec_Type));
3243
 
3244
         --  If No_Default_Initialization restriction is active, then we don't
3245
         --  want to build an init_proc, but we need to mark that an init_proc
3246
         --  would be needed if this restriction was not active (so that we can
3247
         --  detect attempts to call it), so set a dummy init_proc in place.
3248
 
3249
         if Restriction_Active (No_Default_Initialization) then
3250
            Set_Init_Proc (Rec_Type, Proc_Id);
3251
            return;
3252
         end if;
3253
 
3254
         Build_Offset_To_Top_Functions;
3255
         Build_CPP_Init_Procedure;
3256
         Build_Init_Procedure;
3257
         Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3258
 
3259
         --  The initialization of protected records is not worth inlining.
3260
         --  In addition, when compiled for another unit for inlining purposes,
3261
         --  it may make reference to entities that have not been elaborated
3262
         --  yet. The initialization of controlled records contains a nested
3263
         --  clean-up procedure that makes it impractical to inline as well,
3264
         --  and leads to undefined symbols if inlined in a different unit.
3265
         --  Similar considerations apply to task types.
3266
 
3267
         if not Is_Concurrent_Type (Rec_Type)
3268
           and then not Has_Task (Rec_Type)
3269
           and then not Needs_Finalization (Rec_Type)
3270
         then
3271
            Set_Is_Inlined  (Proc_Id);
3272
         end if;
3273
 
3274
         Set_Is_Internal    (Proc_Id);
3275
         Set_Has_Completion (Proc_Id);
3276
 
3277
         if not Debug_Generated_Code then
3278
            Set_Debug_Info_Off (Proc_Id);
3279
         end if;
3280
 
3281
         declare
3282
            Agg : constant Node_Id :=
3283
                    Build_Equivalent_Record_Aggregate (Rec_Type);
3284
 
3285
            procedure Collect_Itypes (Comp : Node_Id);
3286
            --  Generate references to itypes in the aggregate, because
3287
            --  the first use of the aggregate may be in a nested scope.
3288
 
3289
            --------------------
3290
            -- Collect_Itypes --
3291
            --------------------
3292
 
3293
            procedure Collect_Itypes (Comp : Node_Id) is
3294
               Ref      : Node_Id;
3295
               Sub_Aggr : Node_Id;
3296
               Typ      : constant Entity_Id := Etype (Comp);
3297
 
3298
            begin
3299
               if Is_Array_Type (Typ)
3300
                 and then Is_Itype (Typ)
3301
               then
3302
                  Ref := Make_Itype_Reference (Loc);
3303
                  Set_Itype (Ref, Typ);
3304
                  Append_Freeze_Action (Rec_Type, Ref);
3305
 
3306
                  Ref := Make_Itype_Reference (Loc);
3307
                  Set_Itype (Ref, Etype (First_Index (Typ)));
3308
                  Append_Freeze_Action (Rec_Type, Ref);
3309
 
3310
                  Sub_Aggr := First (Expressions (Comp));
3311
 
3312
                  --  Recurse on nested arrays
3313
 
3314
                  while Present (Sub_Aggr) loop
3315
                     Collect_Itypes (Sub_Aggr);
3316
                     Next (Sub_Aggr);
3317
                  end loop;
3318
               end if;
3319
            end Collect_Itypes;
3320
 
3321
         begin
3322
            --  If there is a static initialization aggregate for the type,
3323
            --  generate itype references for the types of its (sub)components,
3324
            --  to prevent out-of-scope errors in the resulting tree.
3325
            --  The aggregate may have been rewritten as a Raise node, in which
3326
            --  case there are no relevant itypes.
3327
 
3328
            if Present (Agg)
3329
              and then Nkind (Agg) = N_Aggregate
3330
            then
3331
               Set_Static_Initialization (Proc_Id, Agg);
3332
 
3333
               declare
3334
                  Comp  : Node_Id;
3335
               begin
3336
                  Comp := First (Component_Associations (Agg));
3337
                  while Present (Comp) loop
3338
                     Collect_Itypes (Expression (Comp));
3339
                     Next (Comp);
3340
                  end loop;
3341
               end;
3342
            end if;
3343
         end;
3344
      end if;
3345
   end Build_Record_Init_Proc;
3346
 
3347
   ----------------------------
3348
   -- Build_Slice_Assignment --
3349
   ----------------------------
3350
 
3351
   --  Generates the following subprogram:
3352
 
3353
   --    procedure Assign
3354
   --     (Source,  Target    : Array_Type,
3355
   --      Left_Lo, Left_Hi   : Index;
3356
   --      Right_Lo, Right_Hi : Index;
3357
   --      Rev                : Boolean)
3358
   --    is
3359
   --       Li1 : Index;
3360
   --       Ri1 : Index;
3361
 
3362
   --    begin
3363
 
3364
   --       if Left_Hi < Left_Lo then
3365
   --          return;
3366
   --       end if;
3367
 
3368
   --       if Rev  then
3369
   --          Li1 := Left_Hi;
3370
   --          Ri1 := Right_Hi;
3371
   --       else
3372
   --          Li1 := Left_Lo;
3373
   --          Ri1 := Right_Lo;
3374
   --       end if;
3375
 
3376
   --       loop
3377
   --          Target (Li1) := Source (Ri1);
3378
 
3379
   --          if Rev then
3380
   --             exit when Li1 = Left_Lo;
3381
   --             Li1 := Index'pred (Li1);
3382
   --             Ri1 := Index'pred (Ri1);
3383
   --          else
3384
   --             exit when Li1 = Left_Hi;
3385
   --             Li1 := Index'succ (Li1);
3386
   --             Ri1 := Index'succ (Ri1);
3387
   --          end if;
3388
   --       end loop;
3389
   --    end Assign;
3390
 
3391
   procedure Build_Slice_Assignment (Typ : Entity_Id) is
3392
      Loc   : constant Source_Ptr := Sloc (Typ);
3393
      Index : constant Entity_Id  := Base_Type (Etype (First_Index (Typ)));
3394
 
3395
      Larray    : constant Entity_Id := Make_Temporary (Loc, 'A');
3396
      Rarray    : constant Entity_Id := Make_Temporary (Loc, 'R');
3397
      Left_Lo   : constant Entity_Id := Make_Temporary (Loc, 'L');
3398
      Left_Hi   : constant Entity_Id := Make_Temporary (Loc, 'L');
3399
      Right_Lo  : constant Entity_Id := Make_Temporary (Loc, 'R');
3400
      Right_Hi  : constant Entity_Id := Make_Temporary (Loc, 'R');
3401
      Rev       : constant Entity_Id := Make_Temporary (Loc, 'D');
3402
      --  Formal parameters of procedure
3403
 
3404
      Proc_Name : constant Entity_Id :=
3405
                    Make_Defining_Identifier (Loc,
3406
                      Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3407
 
3408
      Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3409
      Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3410
      --  Subscripts for left and right sides
3411
 
3412
      Decls : List_Id;
3413
      Loops : Node_Id;
3414
      Stats : List_Id;
3415
 
3416
   begin
3417
      --  Build declarations for indexes
3418
 
3419
      Decls := New_List;
3420
 
3421
      Append_To (Decls,
3422
         Make_Object_Declaration (Loc,
3423
           Defining_Identifier => Lnn,
3424
           Object_Definition  =>
3425
             New_Occurrence_Of (Index, Loc)));
3426
 
3427
      Append_To (Decls,
3428
        Make_Object_Declaration (Loc,
3429
          Defining_Identifier => Rnn,
3430
          Object_Definition  =>
3431
            New_Occurrence_Of (Index, Loc)));
3432
 
3433
      Stats := New_List;
3434
 
3435
      --  Build test for empty slice case
3436
 
3437
      Append_To (Stats,
3438
        Make_If_Statement (Loc,
3439
          Condition =>
3440
             Make_Op_Lt (Loc,
3441
               Left_Opnd  => New_Occurrence_Of (Left_Hi, Loc),
3442
               Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3443
          Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3444
 
3445
      --  Build initializations for indexes
3446
 
3447
      declare
3448
         F_Init : constant List_Id := New_List;
3449
         B_Init : constant List_Id := New_List;
3450
 
3451
      begin
3452
         Append_To (F_Init,
3453
           Make_Assignment_Statement (Loc,
3454
             Name => New_Occurrence_Of (Lnn, Loc),
3455
             Expression => New_Occurrence_Of (Left_Lo, Loc)));
3456
 
3457
         Append_To (F_Init,
3458
           Make_Assignment_Statement (Loc,
3459
             Name => New_Occurrence_Of (Rnn, Loc),
3460
             Expression => New_Occurrence_Of (Right_Lo, Loc)));
3461
 
3462
         Append_To (B_Init,
3463
           Make_Assignment_Statement (Loc,
3464
             Name => New_Occurrence_Of (Lnn, Loc),
3465
             Expression => New_Occurrence_Of (Left_Hi, Loc)));
3466
 
3467
         Append_To (B_Init,
3468
           Make_Assignment_Statement (Loc,
3469
             Name => New_Occurrence_Of (Rnn, Loc),
3470
             Expression => New_Occurrence_Of (Right_Hi, Loc)));
3471
 
3472
         Append_To (Stats,
3473
           Make_If_Statement (Loc,
3474
             Condition => New_Occurrence_Of (Rev, Loc),
3475
             Then_Statements => B_Init,
3476
             Else_Statements => F_Init));
3477
      end;
3478
 
3479
      --  Now construct the assignment statement
3480
 
3481
      Loops :=
3482
        Make_Loop_Statement (Loc,
3483
          Statements => New_List (
3484
            Make_Assignment_Statement (Loc,
3485
              Name =>
3486
                Make_Indexed_Component (Loc,
3487
                  Prefix => New_Occurrence_Of (Larray, Loc),
3488
                  Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3489
              Expression =>
3490
                Make_Indexed_Component (Loc,
3491
                  Prefix => New_Occurrence_Of (Rarray, Loc),
3492
                  Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3493
          End_Label  => Empty);
3494
 
3495
      --  Build the exit condition and increment/decrement statements
3496
 
3497
      declare
3498
         F_Ass : constant List_Id := New_List;
3499
         B_Ass : constant List_Id := New_List;
3500
 
3501
      begin
3502
         Append_To (F_Ass,
3503
           Make_Exit_Statement (Loc,
3504
             Condition =>
3505
               Make_Op_Eq (Loc,
3506
                 Left_Opnd  => New_Occurrence_Of (Lnn, Loc),
3507
                 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3508
 
3509
         Append_To (F_Ass,
3510
           Make_Assignment_Statement (Loc,
3511
             Name => New_Occurrence_Of (Lnn, Loc),
3512
             Expression =>
3513
               Make_Attribute_Reference (Loc,
3514
                 Prefix =>
3515
                   New_Occurrence_Of (Index, Loc),
3516
                 Attribute_Name => Name_Succ,
3517
                 Expressions => New_List (
3518
                   New_Occurrence_Of (Lnn, Loc)))));
3519
 
3520
         Append_To (F_Ass,
3521
           Make_Assignment_Statement (Loc,
3522
             Name => New_Occurrence_Of (Rnn, Loc),
3523
             Expression =>
3524
               Make_Attribute_Reference (Loc,
3525
                 Prefix =>
3526
                   New_Occurrence_Of (Index, Loc),
3527
                 Attribute_Name => Name_Succ,
3528
                 Expressions => New_List (
3529
                   New_Occurrence_Of (Rnn, Loc)))));
3530
 
3531
         Append_To (B_Ass,
3532
           Make_Exit_Statement (Loc,
3533
             Condition =>
3534
               Make_Op_Eq (Loc,
3535
                 Left_Opnd  => New_Occurrence_Of (Lnn, Loc),
3536
                 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3537
 
3538
         Append_To (B_Ass,
3539
           Make_Assignment_Statement (Loc,
3540
             Name => New_Occurrence_Of (Lnn, Loc),
3541
             Expression =>
3542
               Make_Attribute_Reference (Loc,
3543
                 Prefix =>
3544
                   New_Occurrence_Of (Index, Loc),
3545
                 Attribute_Name => Name_Pred,
3546
                   Expressions => New_List (
3547
                     New_Occurrence_Of (Lnn, Loc)))));
3548
 
3549
         Append_To (B_Ass,
3550
           Make_Assignment_Statement (Loc,
3551
             Name => New_Occurrence_Of (Rnn, Loc),
3552
             Expression =>
3553
               Make_Attribute_Reference (Loc,
3554
                 Prefix =>
3555
                   New_Occurrence_Of (Index, Loc),
3556
                 Attribute_Name => Name_Pred,
3557
                 Expressions => New_List (
3558
                   New_Occurrence_Of (Rnn, Loc)))));
3559
 
3560
         Append_To (Statements (Loops),
3561
           Make_If_Statement (Loc,
3562
             Condition => New_Occurrence_Of (Rev, Loc),
3563
             Then_Statements => B_Ass,
3564
             Else_Statements => F_Ass));
3565
      end;
3566
 
3567
      Append_To (Stats, Loops);
3568
 
3569
      declare
3570
         Spec    : Node_Id;
3571
         Formals : List_Id := New_List;
3572
 
3573
      begin
3574
         Formals := New_List (
3575
           Make_Parameter_Specification (Loc,
3576
             Defining_Identifier => Larray,
3577
             Out_Present => True,
3578
             Parameter_Type =>
3579
               New_Reference_To (Base_Type (Typ), Loc)),
3580
 
3581
           Make_Parameter_Specification (Loc,
3582
             Defining_Identifier => Rarray,
3583
             Parameter_Type =>
3584
               New_Reference_To (Base_Type (Typ), Loc)),
3585
 
3586
           Make_Parameter_Specification (Loc,
3587
             Defining_Identifier => Left_Lo,
3588
             Parameter_Type =>
3589
               New_Reference_To (Index, Loc)),
3590
 
3591
           Make_Parameter_Specification (Loc,
3592
             Defining_Identifier => Left_Hi,
3593
             Parameter_Type =>
3594
               New_Reference_To (Index, Loc)),
3595
 
3596
           Make_Parameter_Specification (Loc,
3597
             Defining_Identifier => Right_Lo,
3598
             Parameter_Type =>
3599
               New_Reference_To (Index, Loc)),
3600
 
3601
           Make_Parameter_Specification (Loc,
3602
             Defining_Identifier => Right_Hi,
3603
             Parameter_Type =>
3604
               New_Reference_To (Index, Loc)));
3605
 
3606
         Append_To (Formals,
3607
           Make_Parameter_Specification (Loc,
3608
             Defining_Identifier => Rev,
3609
             Parameter_Type =>
3610
               New_Reference_To (Standard_Boolean, Loc)));
3611
 
3612
         Spec :=
3613
           Make_Procedure_Specification (Loc,
3614
             Defining_Unit_Name       => Proc_Name,
3615
             Parameter_Specifications => Formals);
3616
 
3617
         Discard_Node (
3618
           Make_Subprogram_Body (Loc,
3619
             Specification              => Spec,
3620
             Declarations               => Decls,
3621
             Handled_Statement_Sequence =>
3622
               Make_Handled_Sequence_Of_Statements (Loc,
3623
                 Statements => Stats)));
3624
      end;
3625
 
3626
      Set_TSS (Typ, Proc_Name);
3627
      Set_Is_Pure (Proc_Name);
3628
   end Build_Slice_Assignment;
3629
 
3630
   -----------------------------
3631
   -- Build_Untagged_Equality --
3632
   -----------------------------
3633
 
3634
   procedure Build_Untagged_Equality (Typ : Entity_Id) is
3635
      Build_Eq : Boolean;
3636
      Comp     : Entity_Id;
3637
      Decl     : Node_Id;
3638
      Op       : Entity_Id;
3639
      Prim     : Elmt_Id;
3640
      Eq_Op    : Entity_Id;
3641
 
3642
      function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3643
      --  Check whether the type T has a user-defined primitive equality. If so
3644
      --  return it, else return Empty. If true for a component of Typ, we have
3645
      --  to build the primitive equality for it.
3646
 
3647
      ---------------------
3648
      -- User_Defined_Eq --
3649
      ---------------------
3650
 
3651
      function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3652
         Prim : Elmt_Id;
3653
         Op   : Entity_Id;
3654
 
3655
      begin
3656
         Op := TSS (T, TSS_Composite_Equality);
3657
 
3658
         if Present (Op) then
3659
            return Op;
3660
         end if;
3661
 
3662
         Prim := First_Elmt (Collect_Primitive_Operations (T));
3663
         while Present (Prim) loop
3664
            Op := Node (Prim);
3665
 
3666
            if Chars (Op) = Name_Op_Eq
3667
              and then Etype (Op) = Standard_Boolean
3668
              and then Etype (First_Formal (Op)) = T
3669
              and then Etype (Next_Formal (First_Formal (Op))) = T
3670
            then
3671
               return Op;
3672
            end if;
3673
 
3674
            Next_Elmt (Prim);
3675
         end loop;
3676
 
3677
         return Empty;
3678
      end User_Defined_Eq;
3679
 
3680
   --  Start of processing for Build_Untagged_Equality
3681
 
3682
   begin
3683
      --  If a record component has a primitive equality operation, we must
3684
      --  build the corresponding one for the current type.
3685
 
3686
      Build_Eq := False;
3687
      Comp := First_Component (Typ);
3688
      while Present (Comp) loop
3689
         if Is_Record_Type (Etype (Comp))
3690
           and then Present (User_Defined_Eq (Etype (Comp)))
3691
         then
3692
            Build_Eq := True;
3693
         end if;
3694
 
3695
         Next_Component (Comp);
3696
      end loop;
3697
 
3698
      --  If there is a user-defined equality for the type, we do not create
3699
      --  the implicit one.
3700
 
3701
      Prim := First_Elmt (Collect_Primitive_Operations (Typ));
3702
      Eq_Op := Empty;
3703
      while Present (Prim) loop
3704
         if Chars (Node (Prim)) = Name_Op_Eq
3705
              and then Comes_From_Source (Node (Prim))
3706
 
3707
         --  Don't we also need to check formal types and return type as in
3708
         --  User_Defined_Eq above???
3709
 
3710
         then
3711
            Eq_Op := Node (Prim);
3712
            Build_Eq := False;
3713
            exit;
3714
         end if;
3715
 
3716
         Next_Elmt (Prim);
3717
      end loop;
3718
 
3719
      --  If the type is derived, inherit the operation, if present, from the
3720
      --  parent type. It may have been declared after the type derivation. If
3721
      --  the parent type itself is derived, it may have inherited an operation
3722
      --  that has itself been overridden, so update its alias and related
3723
      --  flags. Ditto for inequality.
3724
 
3725
      if No (Eq_Op) and then Is_Derived_Type (Typ) then
3726
         Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
3727
         while Present (Prim) loop
3728
            if Chars (Node (Prim)) = Name_Op_Eq then
3729
               Copy_TSS (Node (Prim), Typ);
3730
               Build_Eq := False;
3731
 
3732
               declare
3733
                  Op    : constant Entity_Id := User_Defined_Eq (Typ);
3734
                  Eq_Op : constant Entity_Id := Node (Prim);
3735
                  NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
3736
 
3737
               begin
3738
                  if Present (Op) then
3739
                     Set_Alias (Op, Eq_Op);
3740
                     Set_Is_Abstract_Subprogram
3741
                       (Op, Is_Abstract_Subprogram (Eq_Op));
3742
 
3743
                     if Chars (Next_Entity (Op)) = Name_Op_Ne then
3744
                        Set_Is_Abstract_Subprogram
3745
                          (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
3746
                     end if;
3747
                  end if;
3748
               end;
3749
 
3750
               exit;
3751
            end if;
3752
 
3753
            Next_Elmt (Prim);
3754
         end loop;
3755
      end if;
3756
 
3757
      --  If not inherited and not user-defined, build body as for a type with
3758
      --  tagged components.
3759
 
3760
      if Build_Eq then
3761
         Decl :=
3762
           Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
3763
         Op := Defining_Entity (Decl);
3764
         Set_TSS (Typ, Op);
3765
         Set_Is_Pure (Op);
3766
 
3767
         if Is_Library_Level_Entity (Typ) then
3768
            Set_Is_Public (Op);
3769
         end if;
3770
      end if;
3771
   end Build_Untagged_Equality;
3772
 
3773
   ------------------------------------
3774
   -- Build_Variant_Record_Equality --
3775
   ------------------------------------
3776
 
3777
   --  Generates:
3778
 
3779
   --    function _Equality (X, Y : T) return Boolean is
3780
   --    begin
3781
   --       --  Compare discriminants
3782
 
3783
   --       if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3784
   --          return False;
3785
   --       end if;
3786
 
3787
   --       --  Compare components
3788
 
3789
   --       if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3790
   --          return False;
3791
   --       end if;
3792
 
3793
   --       --  Compare variant part
3794
 
3795
   --       case X.D1 is
3796
   --          when V1 =>
3797
   --             if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3798
   --                return False;
3799
   --             end if;
3800
   --          ...
3801
   --          when Vn =>
3802
   --             if False or else X.Cn /= Y.Cn then
3803
   --                return False;
3804
   --             end if;
3805
   --       end case;
3806
 
3807
   --       return True;
3808
   --    end _Equality;
3809
 
3810
   procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3811
      Loc : constant Source_Ptr := Sloc (Typ);
3812
 
3813
      F : constant Entity_Id :=
3814
            Make_Defining_Identifier (Loc,
3815
              Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3816
 
3817
      X : constant Entity_Id :=
3818
           Make_Defining_Identifier (Loc,
3819
             Chars => Name_X);
3820
 
3821
      Y : constant Entity_Id :=
3822
            Make_Defining_Identifier (Loc,
3823
              Chars => Name_Y);
3824
 
3825
      Def    : constant Node_Id := Parent (Typ);
3826
      Comps  : constant Node_Id := Component_List (Type_Definition (Def));
3827
      Stmts  : constant List_Id := New_List;
3828
      Pspecs : constant List_Id := New_List;
3829
 
3830
   begin
3831
      --  Derived Unchecked_Union types no longer inherit the equality function
3832
      --  of their parent.
3833
 
3834
      if Is_Derived_Type (Typ)
3835
        and then not Is_Unchecked_Union (Typ)
3836
        and then not Has_New_Non_Standard_Rep (Typ)
3837
      then
3838
         declare
3839
            Parent_Eq : constant Entity_Id :=
3840
                          TSS (Root_Type (Typ), TSS_Composite_Equality);
3841
 
3842
         begin
3843
            if Present (Parent_Eq) then
3844
               Copy_TSS (Parent_Eq, Typ);
3845
               return;
3846
            end if;
3847
         end;
3848
      end if;
3849
 
3850
      Discard_Node (
3851
        Make_Subprogram_Body (Loc,
3852
          Specification =>
3853
            Make_Function_Specification (Loc,
3854
              Defining_Unit_Name       => F,
3855
              Parameter_Specifications => Pspecs,
3856
              Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3857
          Declarations               => New_List,
3858
          Handled_Statement_Sequence =>
3859
            Make_Handled_Sequence_Of_Statements (Loc,
3860
              Statements => Stmts)));
3861
 
3862
      Append_To (Pspecs,
3863
        Make_Parameter_Specification (Loc,
3864
          Defining_Identifier => X,
3865
          Parameter_Type      => New_Reference_To (Typ, Loc)));
3866
 
3867
      Append_To (Pspecs,
3868
        Make_Parameter_Specification (Loc,
3869
          Defining_Identifier => Y,
3870
          Parameter_Type      => New_Reference_To (Typ, Loc)));
3871
 
3872
      --  Unchecked_Unions require additional machinery to support equality.
3873
      --  Two extra parameters (A and B) are added to the equality function
3874
      --  parameter list in order to capture the inferred values of the
3875
      --  discriminants in later calls.
3876
 
3877
      if Is_Unchecked_Union (Typ) then
3878
         declare
3879
            Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3880
 
3881
            A : constant Node_Id :=
3882
                  Make_Defining_Identifier (Loc,
3883
                    Chars => Name_A);
3884
 
3885
            B : constant Node_Id :=
3886
                  Make_Defining_Identifier (Loc,
3887
                    Chars => Name_B);
3888
 
3889
         begin
3890
            --  Add A and B to the parameter list
3891
 
3892
            Append_To (Pspecs,
3893
              Make_Parameter_Specification (Loc,
3894
                Defining_Identifier => A,
3895
                Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3896
 
3897
            Append_To (Pspecs,
3898
              Make_Parameter_Specification (Loc,
3899
                Defining_Identifier => B,
3900
                Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3901
 
3902
            --  Generate the following header code to compare the inferred
3903
            --  discriminants:
3904
 
3905
            --  if a /= b then
3906
            --     return False;
3907
            --  end if;
3908
 
3909
            Append_To (Stmts,
3910
              Make_If_Statement (Loc,
3911
                Condition =>
3912
                  Make_Op_Ne (Loc,
3913
                    Left_Opnd => New_Reference_To (A, Loc),
3914
                    Right_Opnd => New_Reference_To (B, Loc)),
3915
                Then_Statements => New_List (
3916
                  Make_Simple_Return_Statement (Loc,
3917
                    Expression => New_Occurrence_Of (Standard_False, Loc)))));
3918
 
3919
            --  Generate component-by-component comparison. Note that we must
3920
            --  propagate one of the inferred discriminant formals to act as
3921
            --  the case statement switch.
3922
 
3923
            Append_List_To (Stmts,
3924
              Make_Eq_Case (Typ, Comps, A));
3925
         end;
3926
 
3927
      --  Normal case (not unchecked union)
3928
 
3929
      else
3930
         Append_To (Stmts,
3931
           Make_Eq_If (Typ,
3932
             Discriminant_Specifications (Def)));
3933
 
3934
         Append_List_To (Stmts,
3935
           Make_Eq_Case (Typ, Comps));
3936
      end if;
3937
 
3938
      Append_To (Stmts,
3939
        Make_Simple_Return_Statement (Loc,
3940
          Expression => New_Reference_To (Standard_True, Loc)));
3941
 
3942
      Set_TSS (Typ, F);
3943
      Set_Is_Pure (F);
3944
 
3945
      if not Debug_Generated_Code then
3946
         Set_Debug_Info_Off (F);
3947
      end if;
3948
   end Build_Variant_Record_Equality;
3949
 
3950
   -----------------------------
3951
   -- Check_Stream_Attributes --
3952
   -----------------------------
3953
 
3954
   procedure Check_Stream_Attributes (Typ : Entity_Id) is
3955
      Comp      : Entity_Id;
3956
      Par_Read  : constant Boolean :=
3957
                    Stream_Attribute_Available (Typ, TSS_Stream_Read)
3958
                      and then not Has_Specified_Stream_Read (Typ);
3959
      Par_Write : constant Boolean :=
3960
                    Stream_Attribute_Available (Typ, TSS_Stream_Write)
3961
                      and then not Has_Specified_Stream_Write (Typ);
3962
 
3963
      procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3964
      --  Check that Comp has a user-specified Nam stream attribute
3965
 
3966
      ----------------
3967
      -- Check_Attr --
3968
      ----------------
3969
 
3970
      procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3971
      begin
3972
         if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3973
            Error_Msg_Name_1 := Nam;
3974
            Error_Msg_N
3975
              ("|component& in limited extension must have% attribute", Comp);
3976
         end if;
3977
      end Check_Attr;
3978
 
3979
   --  Start of processing for Check_Stream_Attributes
3980
 
3981
   begin
3982
      if Par_Read or else Par_Write then
3983
         Comp := First_Component (Typ);
3984
         while Present (Comp) loop
3985
            if Comes_From_Source (Comp)
3986
              and then Original_Record_Component (Comp) = Comp
3987
              and then Is_Limited_Type (Etype (Comp))
3988
            then
3989
               if Par_Read then
3990
                  Check_Attr (Name_Read, TSS_Stream_Read);
3991
               end if;
3992
 
3993
               if Par_Write then
3994
                  Check_Attr (Name_Write, TSS_Stream_Write);
3995
               end if;
3996
            end if;
3997
 
3998
            Next_Component (Comp);
3999
         end loop;
4000
      end if;
4001
   end Check_Stream_Attributes;
4002
 
4003
   -----------------------------
4004
   -- Expand_Record_Extension --
4005
   -----------------------------
4006
 
4007
   --  Add a field _parent at the beginning of the record extension. This is
4008
   --  used to implement inheritance. Here are some examples of expansion:
4009
 
4010
   --  1. no discriminants
4011
   --      type T2 is new T1 with null record;
4012
   --   gives
4013
   --      type T2 is new T1 with record
4014
   --        _Parent : T1;
4015
   --      end record;
4016
 
4017
   --  2. renamed discriminants
4018
   --    type T2 (B, C : Int) is new T1 (A => B) with record
4019
   --       _Parent : T1 (A => B);
4020
   --       D : Int;
4021
   --    end;
4022
 
4023
   --  3. inherited discriminants
4024
   --    type T2 is new T1 with record -- discriminant A inherited
4025
   --       _Parent : T1 (A);
4026
   --       D : Int;
4027
   --    end;
4028
 
4029
   procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4030
      Indic        : constant Node_Id    := Subtype_Indication (Def);
4031
      Loc          : constant Source_Ptr := Sloc (Def);
4032
      Rec_Ext_Part : Node_Id             := Record_Extension_Part (Def);
4033
      Par_Subtype  : Entity_Id;
4034
      Comp_List    : Node_Id;
4035
      Comp_Decl    : Node_Id;
4036
      Parent_N     : Node_Id;
4037
      D            : Entity_Id;
4038
      List_Constr  : constant List_Id    := New_List;
4039
 
4040
   begin
4041
      --  Expand_Record_Extension is called directly from the semantics, so
4042
      --  we must check to see whether expansion is active before proceeding
4043
 
4044
      if not Expander_Active then
4045
         return;
4046
      end if;
4047
 
4048
      --  This may be a derivation of an untagged private type whose full
4049
      --  view is tagged, in which case the Derived_Type_Definition has no
4050
      --  extension part. Build an empty one now.
4051
 
4052
      if No (Rec_Ext_Part) then
4053
         Rec_Ext_Part :=
4054
           Make_Record_Definition (Loc,
4055
             End_Label      => Empty,
4056
             Component_List => Empty,
4057
             Null_Present   => True);
4058
 
4059
         Set_Record_Extension_Part (Def, Rec_Ext_Part);
4060
         Mark_Rewrite_Insertion (Rec_Ext_Part);
4061
      end if;
4062
 
4063
      Comp_List := Component_List (Rec_Ext_Part);
4064
 
4065
      Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4066
 
4067
      --  If the derived type inherits its discriminants the type of the
4068
      --  _parent field must be constrained by the inherited discriminants
4069
 
4070
      if Has_Discriminants (T)
4071
        and then Nkind (Indic) /= N_Subtype_Indication
4072
        and then not Is_Constrained (Entity (Indic))
4073
      then
4074
         D := First_Discriminant (T);
4075
         while Present (D) loop
4076
            Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4077
            Next_Discriminant (D);
4078
         end loop;
4079
 
4080
         Par_Subtype :=
4081
           Process_Subtype (
4082
             Make_Subtype_Indication (Loc,
4083
               Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
4084
               Constraint   =>
4085
                 Make_Index_Or_Discriminant_Constraint (Loc,
4086
                   Constraints => List_Constr)),
4087
             Def);
4088
 
4089
      --  Otherwise the original subtype_indication is just what is needed
4090
 
4091
      else
4092
         Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4093
      end if;
4094
 
4095
      Set_Parent_Subtype (T, Par_Subtype);
4096
 
4097
      Comp_Decl :=
4098
        Make_Component_Declaration (Loc,
4099
          Defining_Identifier => Parent_N,
4100
          Component_Definition =>
4101
            Make_Component_Definition (Loc,
4102
              Aliased_Present => False,
4103
              Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
4104
 
4105
      if Null_Present (Rec_Ext_Part) then
4106
         Set_Component_List (Rec_Ext_Part,
4107
           Make_Component_List (Loc,
4108
             Component_Items => New_List (Comp_Decl),
4109
             Variant_Part => Empty,
4110
             Null_Present => False));
4111
         Set_Null_Present (Rec_Ext_Part, False);
4112
 
4113
      elsif Null_Present (Comp_List)
4114
        or else Is_Empty_List (Component_Items (Comp_List))
4115
      then
4116
         Set_Component_Items (Comp_List, New_List (Comp_Decl));
4117
         Set_Null_Present (Comp_List, False);
4118
 
4119
      else
4120
         Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4121
      end if;
4122
 
4123
      Analyze (Comp_Decl);
4124
   end Expand_Record_Extension;
4125
 
4126
   ------------------------------------
4127
   -- Expand_N_Full_Type_Declaration --
4128
   ------------------------------------
4129
 
4130
   procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4131
 
4132
      procedure Build_Master (Ptr_Typ : Entity_Id);
4133
      --  Create the master associated with Ptr_Typ
4134
 
4135
      ------------------
4136
      -- Build_Master --
4137
      ------------------
4138
 
4139
      procedure Build_Master (Ptr_Typ : Entity_Id) is
4140
         Desig_Typ : constant Entity_Id := Designated_Type (Ptr_Typ);
4141
 
4142
      begin
4143
         --  Anonymous access types are created for the components of the
4144
         --  record parameter for an entry declaration. No master is created
4145
         --  for such a type.
4146
 
4147
         if Comes_From_Source (N)
4148
           and then Has_Task (Desig_Typ)
4149
         then
4150
            Build_Master_Entity (Ptr_Typ);
4151
            Build_Master_Renaming (Ptr_Typ);
4152
 
4153
         --  Create a class-wide master because a Master_Id must be generated
4154
         --  for access-to-limited-class-wide types whose root may be extended
4155
         --  with task components.
4156
 
4157
         --  Note: This code covers access-to-limited-interfaces because they
4158
         --        can be used to reference tasks implementing them.
4159
 
4160
         elsif Is_Limited_Class_Wide_Type (Desig_Typ)
4161
           and then Tasking_Allowed
4162
 
4163
           --  Do not create a class-wide master for types whose convention is
4164
           --  Java since these types cannot embed Ada tasks anyway. Note that
4165
           --  the following test cannot catch the following case:
4166
 
4167
           --      package java.lang.Object is
4168
           --         type Typ is tagged limited private;
4169
           --         type Ref is access all Typ'Class;
4170
           --      private
4171
           --         type Typ is tagged limited ...;
4172
           --         pragma Convention (Typ, Java)
4173
           --      end;
4174
 
4175
           --  Because the convention appears after we have done the
4176
           --  processing for type Ref.
4177
 
4178
           and then Convention (Desig_Typ) /= Convention_Java
4179
           and then Convention (Desig_Typ) /= Convention_CIL
4180
         then
4181
            Build_Class_Wide_Master (Ptr_Typ);
4182
         end if;
4183
      end Build_Master;
4184
 
4185
      --  Local declarations
4186
 
4187
      Def_Id : constant Entity_Id := Defining_Identifier (N);
4188
      B_Id   : constant Entity_Id := Base_Type (Def_Id);
4189
      FN     : Node_Id;
4190
      Par_Id : Entity_Id;
4191
 
4192
   --  Start of processing for Expand_N_Full_Type_Declaration
4193
 
4194
   begin
4195
      if Is_Access_Type (Def_Id) then
4196
         Build_Master (Def_Id);
4197
 
4198
         if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4199
            Expand_Access_Protected_Subprogram_Type (N);
4200
         end if;
4201
 
4202
      --  Array of anonymous access-to-task pointers
4203
 
4204
      elsif Ada_Version >= Ada_2005
4205
        and then Is_Array_Type (Def_Id)
4206
        and then Is_Access_Type (Component_Type (Def_Id))
4207
        and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4208
      then
4209
         Build_Master (Component_Type (Def_Id));
4210
 
4211
      elsif Has_Task (Def_Id) then
4212
         Expand_Previous_Access_Type (Def_Id);
4213
 
4214
      --  Check the components of a record type or array of records for
4215
      --  anonymous access-to-task pointers.
4216
 
4217
      elsif Ada_Version >= Ada_2005
4218
        and then (Is_Record_Type (Def_Id)
4219
                   or else
4220
                     (Is_Array_Type (Def_Id)
4221
                       and then Is_Record_Type (Component_Type (Def_Id))))
4222
      then
4223
         declare
4224
            Comp  : Entity_Id;
4225
            First : Boolean;
4226
            M_Id  : Entity_Id;
4227
            Typ   : Entity_Id;
4228
 
4229
         begin
4230
            if Is_Array_Type (Def_Id) then
4231
               Comp := First_Entity (Component_Type (Def_Id));
4232
            else
4233
               Comp := First_Entity (Def_Id);
4234
            end if;
4235
 
4236
            --  Examine all components looking for anonymous access-to-task
4237
            --  types.
4238
 
4239
            First := True;
4240
            while Present (Comp) loop
4241
               Typ := Etype (Comp);
4242
 
4243
               if Ekind (Typ) = E_Anonymous_Access_Type
4244
                 and then Has_Task (Available_View (Designated_Type (Typ)))
4245
                 and then No (Master_Id (Typ))
4246
               then
4247
                  --  Ensure that the record or array type have a _master
4248
 
4249
                  if First then
4250
                     Build_Master_Entity (Def_Id);
4251
                     Build_Master_Renaming (Typ);
4252
                     M_Id := Master_Id (Typ);
4253
 
4254
                     First := False;
4255
 
4256
                  --  Reuse the same master to service any additional types
4257
 
4258
                  else
4259
                     Set_Master_Id (Typ, M_Id);
4260
                  end if;
4261
               end if;
4262
 
4263
               Next_Entity (Comp);
4264
            end loop;
4265
         end;
4266
      end if;
4267
 
4268
      Par_Id := Etype (B_Id);
4269
 
4270
      --  The parent type is private then we need to inherit any TSS operations
4271
      --  from the full view.
4272
 
4273
      if Ekind (Par_Id) in Private_Kind
4274
        and then Present (Full_View (Par_Id))
4275
      then
4276
         Par_Id := Base_Type (Full_View (Par_Id));
4277
      end if;
4278
 
4279
      if Nkind (Type_Definition (Original_Node (N))) =
4280
                                                   N_Derived_Type_Definition
4281
        and then not Is_Tagged_Type (Def_Id)
4282
        and then Present (Freeze_Node (Par_Id))
4283
        and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4284
      then
4285
         Ensure_Freeze_Node (B_Id);
4286
         FN := Freeze_Node (B_Id);
4287
 
4288
         if No (TSS_Elist (FN)) then
4289
            Set_TSS_Elist (FN, New_Elmt_List);
4290
         end if;
4291
 
4292
         declare
4293
            T_E  : constant Elist_Id := TSS_Elist (FN);
4294
            Elmt : Elmt_Id;
4295
 
4296
         begin
4297
            Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4298
            while Present (Elmt) loop
4299
               if Chars (Node (Elmt)) /= Name_uInit then
4300
                  Append_Elmt (Node (Elmt), T_E);
4301
               end if;
4302
 
4303
               Next_Elmt (Elmt);
4304
            end loop;
4305
 
4306
            --  If the derived type itself is private with a full view, then
4307
            --  associate the full view with the inherited TSS_Elist as well.
4308
 
4309
            if Ekind (B_Id) in Private_Kind
4310
              and then Present (Full_View (B_Id))
4311
            then
4312
               Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4313
               Set_TSS_Elist
4314
                 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4315
            end if;
4316
         end;
4317
      end if;
4318
   end Expand_N_Full_Type_Declaration;
4319
 
4320
   ---------------------------------
4321
   -- Expand_N_Object_Declaration --
4322
   ---------------------------------
4323
 
4324
   procedure Expand_N_Object_Declaration (N : Node_Id) is
4325
      Def_Id   : constant Entity_Id  := Defining_Identifier (N);
4326
      Expr     : constant Node_Id    := Expression (N);
4327
      Loc      : constant Source_Ptr := Sloc (N);
4328
      Typ      : constant Entity_Id  := Etype (Def_Id);
4329
      Base_Typ : constant Entity_Id  := Base_Type (Typ);
4330
      Expr_Q   : Node_Id;
4331
      Id_Ref   : Node_Id;
4332
      New_Ref  : Node_Id;
4333
 
4334
      Init_After : Node_Id := N;
4335
      --  Node after which the init proc call is to be inserted. This is
4336
      --  normally N, except for the case of a shared passive variable, in
4337
      --  which case the init proc call must be inserted only after the bodies
4338
      --  of the shared variable procedures have been seen.
4339
 
4340
      function Rewrite_As_Renaming return Boolean;
4341
      --  Indicate whether to rewrite a declaration with initialization into an
4342
      --  object renaming declaration (see below).
4343
 
4344
      -------------------------
4345
      -- Rewrite_As_Renaming --
4346
      -------------------------
4347
 
4348
      function Rewrite_As_Renaming return Boolean is
4349
      begin
4350
         return not Aliased_Present (N)
4351
           and then Is_Entity_Name (Expr_Q)
4352
           and then Ekind (Entity (Expr_Q)) = E_Variable
4353
           and then OK_To_Rename (Entity (Expr_Q))
4354
           and then Is_Entity_Name (Object_Definition (N));
4355
      end Rewrite_As_Renaming;
4356
 
4357
   --  Start of processing for Expand_N_Object_Declaration
4358
 
4359
   begin
4360
      --  Don't do anything for deferred constants. All proper actions will be
4361
      --  expanded during the full declaration.
4362
 
4363
      if No (Expr) and Constant_Present (N) then
4364
         return;
4365
      end if;
4366
 
4367
      --  First we do special processing for objects of a tagged type where
4368
      --  this is the point at which the type is frozen. The creation of the
4369
      --  dispatch table and the initialization procedure have to be deferred
4370
      --  to this point, since we reference previously declared primitive
4371
      --  subprograms.
4372
 
4373
      --  Force construction of dispatch tables of library level tagged types
4374
 
4375
      if Tagged_Type_Expansion
4376
        and then Static_Dispatch_Tables
4377
        and then Is_Library_Level_Entity (Def_Id)
4378
        and then Is_Library_Level_Tagged_Type (Base_Typ)
4379
        and then (Ekind (Base_Typ) = E_Record_Type
4380
                    or else Ekind (Base_Typ) = E_Protected_Type
4381
                    or else Ekind (Base_Typ) = E_Task_Type)
4382
        and then not Has_Dispatch_Table (Base_Typ)
4383
      then
4384
         declare
4385
            New_Nodes : List_Id := No_List;
4386
 
4387
         begin
4388
            if Is_Concurrent_Type (Base_Typ) then
4389
               New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4390
            else
4391
               New_Nodes := Make_DT (Base_Typ, N);
4392
            end if;
4393
 
4394
            if not Is_Empty_List (New_Nodes) then
4395
               Insert_List_Before (N, New_Nodes);
4396
            end if;
4397
         end;
4398
      end if;
4399
 
4400
      --  Make shared memory routines for shared passive variable
4401
 
4402
      if Is_Shared_Passive (Def_Id) then
4403
         Init_After := Make_Shared_Var_Procs (N);
4404
      end if;
4405
 
4406
      --  If tasks being declared, make sure we have an activation chain
4407
      --  defined for the tasks (has no effect if we already have one), and
4408
      --  also that a Master variable is established and that the appropriate
4409
      --  enclosing construct is established as a task master.
4410
 
4411
      if Has_Task (Typ) then
4412
         Build_Activation_Chain_Entity (N);
4413
         Build_Master_Entity (Def_Id);
4414
      end if;
4415
 
4416
      --  Default initialization required, and no expression present
4417
 
4418
      if No (Expr) then
4419
 
4420
         --  For the default initialization case, if we have a private type
4421
         --  with invariants, and invariant checks are enabled, then insert an
4422
         --  invariant check after the object declaration. Note that it is OK
4423
         --  to clobber the object with an invalid value since if the exception
4424
         --  is raised, then the object will go out of scope.
4425
 
4426
         if Has_Invariants (Typ)
4427
           and then Present (Invariant_Procedure (Typ))
4428
         then
4429
            Insert_After (N,
4430
              Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
4431
         end if;
4432
 
4433
         --  Expand Initialize call for controlled objects. One may wonder why
4434
         --  the Initialize Call is not done in the regular Init procedure
4435
         --  attached to the record type. That's because the init procedure is
4436
         --  recursively called on each component, including _Parent, thus the
4437
         --  Init call for a controlled object would generate not only one
4438
         --  Initialize call as it is required but one for each ancestor of
4439
         --  its type. This processing is suppressed if No_Initialization set.
4440
 
4441
         if not Needs_Finalization (Typ)
4442
           or else No_Initialization (N)
4443
         then
4444
            null;
4445
 
4446
         elsif not Abort_Allowed
4447
           or else not Comes_From_Source (N)
4448
         then
4449
            Insert_Action_After (Init_After,
4450
              Make_Init_Call
4451
                (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
4452
                 Typ     => Base_Type (Typ)));
4453
 
4454
         --  Abort allowed
4455
 
4456
         else
4457
            --  We need to protect the initialize call
4458
 
4459
            --  begin
4460
            --     Defer_Abort.all;
4461
            --     Initialize (...);
4462
            --  at end
4463
            --     Undefer_Abort.all;
4464
            --  end;
4465
 
4466
            --  ??? this won't protect the initialize call for controlled
4467
            --  components which are part of the init proc, so this block
4468
            --  should probably also contain the call to _init_proc but this
4469
            --  requires some code reorganization...
4470
 
4471
            declare
4472
               L   : constant List_Id := New_List (
4473
                       Make_Init_Call
4474
                         (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
4475
                          Typ     => Base_Type (Typ)));
4476
 
4477
               Blk : constant Node_Id :=
4478
                       Make_Block_Statement (Loc,
4479
                         Handled_Statement_Sequence =>
4480
                           Make_Handled_Sequence_Of_Statements (Loc, L));
4481
 
4482
            begin
4483
               Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4484
               Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4485
                 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4486
               Insert_Actions_After (Init_After, New_List (Blk));
4487
               Expand_At_End_Handler
4488
                 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4489
            end;
4490
         end if;
4491
 
4492
         --  Call type initialization procedure if there is one. We build the
4493
         --  call and put it immediately after the object declaration, so that
4494
         --  it will be expanded in the usual manner. Note that this will
4495
         --  result in proper handling of defaulted discriminants.
4496
 
4497
         --  Need call if there is a base init proc
4498
 
4499
         if Has_Non_Null_Base_Init_Proc (Typ)
4500
 
4501
            --  Suppress call if No_Initialization set on declaration
4502
 
4503
            and then not No_Initialization (N)
4504
 
4505
            --  Suppress call for special case of value type for VM
4506
 
4507
            and then not Is_Value_Type (Typ)
4508
 
4509
            --  Suppress call if initialization suppressed for the type
4510
 
4511
            and then not Initialization_Suppressed (Typ)
4512
         then
4513
            --  Return without initializing when No_Default_Initialization
4514
            --  applies. Note that the actual restriction check occurs later,
4515
            --  when the object is frozen, because we don't know yet whether
4516
            --  the object is imported, which is a case where the check does
4517
            --  not apply.
4518
 
4519
            if Restriction_Active (No_Default_Initialization) then
4520
               return;
4521
            end if;
4522
 
4523
            --  The call to the initialization procedure does NOT freeze the
4524
            --  object being initialized. This is because the call is not a
4525
            --  source level call. This works fine, because the only possible
4526
            --  statements depending on freeze status that can appear after the
4527
            --  Init_Proc call are rep clauses which can safely appear after
4528
            --  actual references to the object. Note that this call may
4529
            --  subsequently be removed (if a pragma Import is encountered),
4530
            --  or moved to the freeze actions for the object (e.g. if an
4531
            --  address clause is applied to the object, causing it to get
4532
            --  delayed freezing).
4533
 
4534
            Id_Ref := New_Reference_To (Def_Id, Loc);
4535
            Set_Must_Not_Freeze (Id_Ref);
4536
            Set_Assignment_OK (Id_Ref);
4537
 
4538
            declare
4539
               Init_Expr : constant Node_Id :=
4540
                             Static_Initialization (Base_Init_Proc (Typ));
4541
 
4542
            begin
4543
               if Present (Init_Expr) then
4544
                  Set_Expression
4545
                    (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4546
                  return;
4547
 
4548
               else
4549
                  Initialization_Warning (Id_Ref);
4550
 
4551
                  Insert_Actions_After (Init_After,
4552
                    Build_Initialization_Call (Loc, Id_Ref, Typ));
4553
               end if;
4554
            end;
4555
 
4556
         --  If simple initialization is required, then set an appropriate
4557
         --  simple initialization expression in place. This special
4558
         --  initialization is required even though No_Init_Flag is present,
4559
         --  but is not needed if there was an explicit initialization.
4560
 
4561
         --  An internally generated temporary needs no initialization because
4562
         --  it will be assigned subsequently. In particular, there is no point
4563
         --  in applying Initialize_Scalars to such a temporary.
4564
 
4565
         elsif Needs_Simple_Initialization
4566
                 (Typ,
4567
                  Initialize_Scalars
4568
                    and then not Has_Following_Address_Clause (N))
4569
           and then not Is_Internal (Def_Id)
4570
           and then not Has_Init_Expression (N)
4571
         then
4572
            Set_No_Initialization (N, False);
4573
            Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4574
            Analyze_And_Resolve (Expression (N), Typ);
4575
         end if;
4576
 
4577
         --  Generate attribute for Persistent_BSS if needed
4578
 
4579
         if Persistent_BSS_Mode
4580
           and then Comes_From_Source (N)
4581
           and then Is_Potentially_Persistent_Type (Typ)
4582
           and then not Has_Init_Expression (N)
4583
           and then Is_Library_Level_Entity (Def_Id)
4584
         then
4585
            declare
4586
               Prag : Node_Id;
4587
            begin
4588
               Prag :=
4589
                 Make_Linker_Section_Pragma
4590
                   (Def_Id, Sloc (N), ".persistent.bss");
4591
               Insert_After (N, Prag);
4592
               Analyze (Prag);
4593
            end;
4594
         end if;
4595
 
4596
         --  If access type, then we know it is null if not initialized
4597
 
4598
         if Is_Access_Type (Typ) then
4599
            Set_Is_Known_Null (Def_Id);
4600
         end if;
4601
 
4602
      --  Explicit initialization present
4603
 
4604
      else
4605
         --  Obtain actual expression from qualified expression
4606
 
4607
         if Nkind (Expr) = N_Qualified_Expression then
4608
            Expr_Q := Expression (Expr);
4609
         else
4610
            Expr_Q := Expr;
4611
         end if;
4612
 
4613
         --  When we have the appropriate type of aggregate in the expression
4614
         --  (it has been determined during analysis of the aggregate by
4615
         --  setting the delay flag), let's perform in place assignment and
4616
         --  thus avoid creating a temporary.
4617
 
4618
         if Is_Delayed_Aggregate (Expr_Q) then
4619
            Convert_Aggr_In_Object_Decl (N);
4620
 
4621
         --  Ada 2005 (AI-318-02): If the initialization expression is a call
4622
         --  to a build-in-place function, then access to the declared object
4623
         --  must be passed to the function. Currently we limit such functions
4624
         --  to those with constrained limited result subtypes, but eventually
4625
         --  plan to expand the allowed forms of functions that are treated as
4626
         --  build-in-place.
4627
 
4628
         elsif Ada_Version >= Ada_2005
4629
           and then Is_Build_In_Place_Function_Call (Expr_Q)
4630
         then
4631
            Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4632
 
4633
            --  The previous call expands the expression initializing the
4634
            --  built-in-place object into further code that will be analyzed
4635
            --  later. No further expansion needed here.
4636
 
4637
            return;
4638
 
4639
         --  Ada 2005 (AI-251): Rewrite the expression that initializes a
4640
         --  class-wide interface object to ensure that we copy the full
4641
         --  object, unless we are targetting a VM where interfaces are handled
4642
         --  by VM itself. Note that if the root type of Typ is an ancestor of
4643
         --  Expr's type, both types share the same dispatch table and there is
4644
         --  no need to displace the pointer.
4645
 
4646
         elsif Comes_From_Source (N)
4647
           and then Is_Interface (Typ)
4648
         then
4649
            pragma Assert (Is_Class_Wide_Type (Typ));
4650
 
4651
            --  If the object is a return object of an inherently limited type,
4652
            --  which implies build-in-place treatment, bypass the special
4653
            --  treatment of class-wide interface initialization below. In this
4654
            --  case, the expansion of the return statement will take care of
4655
            --  creating the object (via allocator) and initializing it.
4656
 
4657
            if Is_Return_Object (Def_Id)
4658
              and then Is_Immutably_Limited_Type (Typ)
4659
            then
4660
               null;
4661
 
4662
            elsif Tagged_Type_Expansion then
4663
               declare
4664
                  Iface    : constant Entity_Id := Root_Type (Typ);
4665
                  Expr_N   : Node_Id := Expr;
4666
                  Expr_Typ : Entity_Id;
4667
                  New_Expr : Node_Id;
4668
                  Obj_Id   : Entity_Id;
4669
                  Tag_Comp : Node_Id;
4670
 
4671
               begin
4672
                  --  If the original node of the expression was a conversion
4673
                  --  to this specific class-wide interface type then restore
4674
                  --  the original node because we must copy the object before
4675
                  --  displacing the pointer to reference the secondary tag
4676
                  --  component. This code must be kept synchronized with the
4677
                  --  expansion done by routine Expand_Interface_Conversion
4678
 
4679
                  if not Comes_From_Source (Expr_N)
4680
                    and then Nkind (Expr_N) = N_Explicit_Dereference
4681
                    and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
4682
                    and then Etype (Original_Node (Expr_N)) = Typ
4683
                  then
4684
                     Rewrite (Expr_N, Original_Node (Expression (N)));
4685
                  end if;
4686
 
4687
                  --  Avoid expansion of redundant interface conversion
4688
 
4689
                  if Is_Interface (Etype (Expr_N))
4690
                    and then Nkind (Expr_N) = N_Type_Conversion
4691
                    and then Etype (Expr_N) = Typ
4692
                  then
4693
                     Expr_N := Expression (Expr_N);
4694
                     Set_Expression (N, Expr_N);
4695
                  end if;
4696
 
4697
                  Obj_Id   := Make_Temporary (Loc, 'D', Expr_N);
4698
                  Expr_Typ := Base_Type (Etype (Expr_N));
4699
 
4700
                  if Is_Class_Wide_Type (Expr_Typ) then
4701
                     Expr_Typ := Root_Type (Expr_Typ);
4702
                  end if;
4703
 
4704
                  --  Replace
4705
                  --     CW : I'Class := Obj;
4706
                  --  by
4707
                  --     Tmp : T := Obj;
4708
                  --     type Ityp is not null access I'Class;
4709
                  --     CW  : I'Class renames Ityp(Tmp.I_Tag'Address).all;
4710
 
4711
                  if Comes_From_Source (Expr_N)
4712
                    and then Nkind (Expr_N) = N_Identifier
4713
                    and then not Is_Interface (Expr_Typ)
4714
                    and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
4715
                    and then (Expr_Typ = Etype (Expr_Typ)
4716
                               or else not
4717
                              Is_Variable_Size_Record (Etype (Expr_Typ)))
4718
                  then
4719
                     --  Copy the object
4720
 
4721
                     Insert_Action (N,
4722
                       Make_Object_Declaration (Loc,
4723
                         Defining_Identifier => Obj_Id,
4724
                         Object_Definition =>
4725
                           New_Occurrence_Of (Expr_Typ, Loc),
4726
                         Expression =>
4727
                           Relocate_Node (Expr_N)));
4728
 
4729
                     --  Statically reference the tag associated with the
4730
                     --  interface
4731
 
4732
                     Tag_Comp :=
4733
                       Make_Selected_Component (Loc,
4734
                         Prefix => New_Occurrence_Of (Obj_Id, Loc),
4735
                         Selector_Name =>
4736
                           New_Reference_To
4737
                             (Find_Interface_Tag (Expr_Typ, Iface), Loc));
4738
 
4739
                  --  Replace
4740
                  --     IW : I'Class := Obj;
4741
                  --  by
4742
                  --     type Equiv_Record is record ... end record;
4743
                  --     implicit subtype CW is <Class_Wide_Subtype>;
4744
                  --     Tmp : CW := CW!(Obj);
4745
                  --     type Ityp is not null access I'Class;
4746
                  --     IW : I'Class renames
4747
                  --            Ityp!(Displace (Temp'Address, I'Tag)).all;
4748
 
4749
                  else
4750
                     --  Generate the equivalent record type and update the
4751
                     --  subtype indication to reference it.
4752
 
4753
                     Expand_Subtype_From_Expr
4754
                       (N             => N,
4755
                        Unc_Type      => Typ,
4756
                        Subtype_Indic => Object_Definition (N),
4757
                        Exp           => Expr_N);
4758
 
4759
                     if not Is_Interface (Etype (Expr_N)) then
4760
                        New_Expr := Relocate_Node (Expr_N);
4761
 
4762
                     --  For interface types we use 'Address which displaces
4763
                     --  the pointer to the base of the object (if required)
4764
 
4765
                     else
4766
                        New_Expr :=
4767
                          Unchecked_Convert_To (Etype (Object_Definition (N)),
4768
                            Make_Explicit_Dereference (Loc,
4769
                              Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4770
                                Make_Attribute_Reference (Loc,
4771
                                  Prefix => Relocate_Node (Expr_N),
4772
                                  Attribute_Name => Name_Address))));
4773
                     end if;
4774
 
4775
                     --  Copy the object
4776
 
4777
                     if not Is_Limited_Record (Expr_Typ) then
4778
                        Insert_Action (N,
4779
                          Make_Object_Declaration (Loc,
4780
                            Defining_Identifier => Obj_Id,
4781
                            Object_Definition   =>
4782
                              New_Occurrence_Of
4783
                                (Etype (Object_Definition (N)), Loc),
4784
                            Expression => New_Expr));
4785
 
4786
                     --  Rename limited type object since they cannot be copied
4787
                     --  This case occurs when the initialization expression
4788
                     --  has been previously expanded into a temporary object.
4789
 
4790
                     else pragma Assert (not Comes_From_Source (Expr_Q));
4791
                        Insert_Action (N,
4792
                          Make_Object_Renaming_Declaration (Loc,
4793
                            Defining_Identifier => Obj_Id,
4794
                            Subtype_Mark        =>
4795
                              New_Occurrence_Of
4796
                                (Etype (Object_Definition (N)), Loc),
4797
                            Name                =>
4798
                              Unchecked_Convert_To
4799
                                (Etype (Object_Definition (N)), New_Expr)));
4800
                     end if;
4801
 
4802
                     --  Dynamically reference the tag associated with the
4803
                     --  interface.
4804
 
4805
                     Tag_Comp :=
4806
                       Make_Function_Call (Loc,
4807
                         Name => New_Reference_To (RTE (RE_Displace), Loc),
4808
                         Parameter_Associations => New_List (
4809
                           Make_Attribute_Reference (Loc,
4810
                             Prefix => New_Occurrence_Of (Obj_Id, Loc),
4811
                             Attribute_Name => Name_Address),
4812
                           New_Reference_To
4813
                             (Node (First_Elmt (Access_Disp_Table (Iface))),
4814
                              Loc)));
4815
                  end if;
4816
 
4817
                  Rewrite (N,
4818
                    Make_Object_Renaming_Declaration (Loc,
4819
                      Defining_Identifier => Make_Temporary (Loc, 'D'),
4820
                      Subtype_Mark        => New_Occurrence_Of (Typ, Loc),
4821
                      Name => Convert_Tag_To_Interface (Typ, Tag_Comp)));
4822
 
4823
                  Analyze (N, Suppress => All_Checks);
4824
 
4825
                  --  Replace internal identifier of rewritten node by the
4826
                  --  identifier found in the sources. We also have to exchange
4827
                  --  entities containing their defining identifiers to ensure
4828
                  --  the correct replacement of the object declaration by this
4829
                  --  object renaming declaration ---because these identifiers
4830
                  --  were previously added by Enter_Name to the current scope.
4831
                  --  We must preserve the homonym chain of the source entity
4832
                  --  as well.
4833
 
4834
                  Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4835
                  Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4836
                  Exchange_Entities (Defining_Identifier (N), Def_Id);
4837
               end;
4838
            end if;
4839
 
4840
            return;
4841
 
4842
         --  Common case of explicit object initialization
4843
 
4844
         else
4845
            --  In most cases, we must check that the initial value meets any
4846
            --  constraint imposed by the declared type. However, there is one
4847
            --  very important exception to this rule. If the entity has an
4848
            --  unconstrained nominal subtype, then it acquired its constraints
4849
            --  from the expression in the first place, and not only does this
4850
            --  mean that the constraint check is not needed, but an attempt to
4851
            --  perform the constraint check can cause order of elaboration
4852
            --  problems.
4853
 
4854
            if not Is_Constr_Subt_For_U_Nominal (Typ) then
4855
 
4856
               --  If this is an allocator for an aggregate that has been
4857
               --  allocated in place, delay checks until assignments are
4858
               --  made, because the discriminants are not initialized.
4859
 
4860
               if Nkind (Expr) = N_Allocator
4861
                 and then No_Initialization (Expr)
4862
               then
4863
                  null;
4864
 
4865
               --  Otherwise apply a constraint check now if no prev error
4866
 
4867
               elsif Nkind (Expr) /= N_Error then
4868
                  Apply_Constraint_Check (Expr, Typ);
4869
 
4870
                  --  If the expression has been marked as requiring a range
4871
                  --  generate it now and reset the flag.
4872
 
4873
                  if Do_Range_Check (Expr) then
4874
                     Set_Do_Range_Check (Expr, False);
4875
 
4876
                     if not Suppress_Assignment_Checks (N) then
4877
                        Generate_Range_Check
4878
                          (Expr, Typ, CE_Range_Check_Failed);
4879
                     end if;
4880
                  end if;
4881
               end if;
4882
            end if;
4883
 
4884
            --  If the type is controlled and not inherently limited, then
4885
            --  the target is adjusted after the copy and attached to the
4886
            --  finalization list. However, no adjustment is done in the case
4887
            --  where the object was initialized by a call to a function whose
4888
            --  result is built in place, since no copy occurred. (Eventually
4889
            --  we plan to support in-place function results for some cases
4890
            --  of nonlimited types. ???) Similarly, no adjustment is required
4891
            --  if we are going to rewrite the object declaration into a
4892
            --  renaming declaration.
4893
 
4894
            if Needs_Finalization (Typ)
4895
              and then not Is_Immutably_Limited_Type (Typ)
4896
              and then not Rewrite_As_Renaming
4897
            then
4898
               Insert_Action_After (Init_After,
4899
                 Make_Adjust_Call (
4900
                   Obj_Ref => New_Reference_To (Def_Id, Loc),
4901
                   Typ     => Base_Type (Typ)));
4902
            end if;
4903
 
4904
            --  For tagged types, when an init value is given, the tag has to
4905
            --  be re-initialized separately in order to avoid the propagation
4906
            --  of a wrong tag coming from a view conversion unless the type
4907
            --  is class wide (in this case the tag comes from the init value).
4908
            --  Suppress the tag assignment when VM_Target because VM tags are
4909
            --  represented implicitly in objects. Ditto for types that are
4910
            --  CPP_CLASS, and for initializations that are aggregates, because
4911
            --  they have to have the right tag.
4912
 
4913
            if Is_Tagged_Type (Typ)
4914
              and then not Is_Class_Wide_Type (Typ)
4915
              and then not Is_CPP_Class (Typ)
4916
              and then Tagged_Type_Expansion
4917
              and then Nkind (Expr) /= N_Aggregate
4918
            then
4919
               declare
4920
                  Full_Typ : constant Entity_Id := Underlying_Type (Typ);
4921
 
4922
               begin
4923
                  --  The re-assignment of the tag has to be done even if the
4924
                  --  object is a constant. The assignment must be analyzed
4925
                  --  after the declaration.
4926
 
4927
                  New_Ref :=
4928
                    Make_Selected_Component (Loc,
4929
                       Prefix => New_Occurrence_Of (Def_Id, Loc),
4930
                       Selector_Name =>
4931
                         New_Reference_To (First_Tag_Component (Full_Typ),
4932
                                           Loc));
4933
                  Set_Assignment_OK (New_Ref);
4934
 
4935
                  Insert_Action_After (Init_After,
4936
                    Make_Assignment_Statement (Loc,
4937
                      Name       => New_Ref,
4938
                      Expression =>
4939
                        Unchecked_Convert_To (RTE (RE_Tag),
4940
                          New_Reference_To
4941
                            (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
4942
                             Loc))));
4943
               end;
4944
 
4945
            --  Handle C++ constructor calls. Note that we do not check that
4946
            --  Typ is a tagged type since the equivalent Ada type of a C++
4947
            --  class that has no virtual methods is a non-tagged limited
4948
            --  record type.
4949
 
4950
            elsif Is_CPP_Constructor_Call (Expr) then
4951
 
4952
               --  The call to the initialization procedure does NOT freeze the
4953
               --  object being initialized.
4954
 
4955
               Id_Ref := New_Reference_To (Def_Id, Loc);
4956
               Set_Must_Not_Freeze (Id_Ref);
4957
               Set_Assignment_OK (Id_Ref);
4958
 
4959
               Insert_Actions_After (Init_After,
4960
                 Build_Initialization_Call (Loc, Id_Ref, Typ,
4961
                   Constructor_Ref => Expr));
4962
 
4963
               --  We remove here the original call to the constructor
4964
               --  to avoid its management in the backend
4965
 
4966
               Set_Expression (N, Empty);
4967
               return;
4968
 
4969
            --  For discrete types, set the Is_Known_Valid flag if the
4970
            --  initializing value is known to be valid.
4971
 
4972
            elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
4973
               Set_Is_Known_Valid (Def_Id);
4974
 
4975
            elsif Is_Access_Type (Typ) then
4976
 
4977
               --  For access types set the Is_Known_Non_Null flag if the
4978
               --  initializing value is known to be non-null. We can also set
4979
               --  Can_Never_Be_Null if this is a constant.
4980
 
4981
               if Known_Non_Null (Expr) then
4982
                  Set_Is_Known_Non_Null (Def_Id, True);
4983
 
4984
                  if Constant_Present (N) then
4985
                     Set_Can_Never_Be_Null (Def_Id);
4986
                  end if;
4987
               end if;
4988
            end if;
4989
 
4990
            --  If validity checking on copies, validate initial expression.
4991
            --  But skip this if declaration is for a generic type, since it
4992
            --  makes no sense to validate generic types. Not clear if this
4993
            --  can happen for legal programs, but it definitely can arise
4994
            --  from previous instantiation errors.
4995
 
4996
            if Validity_Checks_On
4997
              and then Validity_Check_Copies
4998
              and then not Is_Generic_Type (Etype (Def_Id))
4999
            then
5000
               Ensure_Valid (Expr);
5001
               Set_Is_Known_Valid (Def_Id);
5002
            end if;
5003
         end if;
5004
 
5005
         --  Cases where the back end cannot handle the initialization directly
5006
         --  In such cases, we expand an assignment that will be appropriately
5007
         --  handled by Expand_N_Assignment_Statement.
5008
 
5009
         --  The exclusion of the unconstrained case is wrong, but for now it
5010
         --  is too much trouble ???
5011
 
5012
         if (Is_Possibly_Unaligned_Slice (Expr)
5013
               or else (Is_Possibly_Unaligned_Object (Expr)
5014
                          and then not Represented_As_Scalar (Etype (Expr))))
5015
           and then not (Is_Array_Type (Etype (Expr))
5016
                           and then not Is_Constrained (Etype (Expr)))
5017
         then
5018
            declare
5019
               Stat : constant Node_Id :=
5020
                       Make_Assignment_Statement (Loc,
5021
                         Name       => New_Reference_To (Def_Id, Loc),
5022
                         Expression => Relocate_Node (Expr));
5023
            begin
5024
               Set_Expression (N, Empty);
5025
               Set_No_Initialization (N);
5026
               Set_Assignment_OK (Name (Stat));
5027
               Set_No_Ctrl_Actions (Stat);
5028
               Insert_After_And_Analyze (Init_After, Stat);
5029
            end;
5030
         end if;
5031
 
5032
         --  Final transformation, if the initializing expression is an entity
5033
         --  for a variable with OK_To_Rename set, then we transform:
5034
 
5035
         --     X : typ := expr;
5036
 
5037
         --  into
5038
 
5039
         --     X : typ renames expr
5040
 
5041
         --  provided that X is not aliased. The aliased case has to be
5042
         --  excluded in general because Expr will not be aliased in general.
5043
 
5044
         if Rewrite_As_Renaming then
5045
            Rewrite (N,
5046
              Make_Object_Renaming_Declaration (Loc,
5047
                Defining_Identifier => Defining_Identifier (N),
5048
                Subtype_Mark        => Object_Definition (N),
5049
                Name                => Expr_Q));
5050
 
5051
            --  We do not analyze this renaming declaration, because all its
5052
            --  components have already been analyzed, and if we were to go
5053
            --  ahead and analyze it, we would in effect be trying to generate
5054
            --  another declaration of X, which won't do!
5055
 
5056
            Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5057
            Set_Analyzed (N);
5058
 
5059
            --  We do need to deal with debug issues for this renaming
5060
 
5061
            --  First, if entity comes from source, then mark it as needing
5062
            --  debug information, even though it is defined by a generated
5063
            --  renaming that does not come from source.
5064
 
5065
            if Comes_From_Source (Defining_Identifier (N)) then
5066
               Set_Needs_Debug_Info (Defining_Identifier (N));
5067
            end if;
5068
 
5069
            --  Now call the routine to generate debug info for the renaming
5070
 
5071
            declare
5072
               Decl : constant Node_Id := Debug_Renaming_Declaration (N);
5073
            begin
5074
               if Present (Decl) then
5075
                  Insert_Action (N, Decl);
5076
               end if;
5077
            end;
5078
         end if;
5079
      end if;
5080
 
5081
      if Nkind (N) = N_Object_Declaration
5082
        and then Nkind (Object_Definition (N)) = N_Access_Definition
5083
        and then not Is_Local_Anonymous_Access (Etype (Def_Id))
5084
      then
5085
         --  An Ada 2012 stand-alone object of an anonymous access type
5086
 
5087
         declare
5088
            Loc : constant Source_Ptr := Sloc (N);
5089
 
5090
            Level : constant Entity_Id :=
5091
                      Make_Defining_Identifier (Sloc (N),
5092
                        Chars =>
5093
                          New_External_Name (Chars (Def_Id), Suffix => "L"));
5094
 
5095
            Level_Expr : Node_Id;
5096
            Level_Decl : Node_Id;
5097
 
5098
         begin
5099
            Set_Ekind (Level, Ekind (Def_Id));
5100
            Set_Etype (Level, Standard_Natural);
5101
            Set_Scope (Level, Scope (Def_Id));
5102
 
5103
            if No (Expr) then
5104
 
5105
               --  Set accessibility level of null
5106
 
5107
               Level_Expr :=
5108
                 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
5109
 
5110
            else
5111
               Level_Expr := Dynamic_Accessibility_Level (Expr);
5112
            end if;
5113
 
5114
            Level_Decl := Make_Object_Declaration (Loc,
5115
             Defining_Identifier => Level,
5116
             Object_Definition => New_Occurrence_Of (Standard_Natural, Loc),
5117
             Expression => Level_Expr,
5118
             Constant_Present => Constant_Present (N),
5119
             Has_Init_Expression => True);
5120
 
5121
            Insert_Action_After (Init_After, Level_Decl);
5122
 
5123
            Set_Extra_Accessibility (Def_Id, Level);
5124
         end;
5125
      end if;
5126
 
5127
   --  Exception on library entity not available
5128
 
5129
   exception
5130
      when RE_Not_Available =>
5131
         return;
5132
   end Expand_N_Object_Declaration;
5133
 
5134
   ---------------------------------
5135
   -- Expand_N_Subtype_Indication --
5136
   ---------------------------------
5137
 
5138
   --  Add a check on the range of the subtype. The static case is partially
5139
   --  duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
5140
   --  to check here for the static case in order to avoid generating
5141
   --  extraneous expanded code. Also deal with validity checking.
5142
 
5143
   procedure Expand_N_Subtype_Indication (N : Node_Id) is
5144
      Ran : constant Node_Id   := Range_Expression (Constraint (N));
5145
      Typ : constant Entity_Id := Entity (Subtype_Mark (N));
5146
 
5147
   begin
5148
      if Nkind (Constraint (N)) = N_Range_Constraint then
5149
         Validity_Check_Range (Range_Expression (Constraint (N)));
5150
      end if;
5151
 
5152
      if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
5153
         Apply_Range_Check (Ran, Typ);
5154
      end if;
5155
   end Expand_N_Subtype_Indication;
5156
 
5157
   ---------------------------
5158
   -- Expand_N_Variant_Part --
5159
   ---------------------------
5160
 
5161
   --  If the last variant does not contain the Others choice, replace it with
5162
   --  an N_Others_Choice node since Gigi always wants an Others. Note that we
5163
   --  do not bother to call Analyze on the modified variant part, since its
5164
   --  only effect would be to compute the Others_Discrete_Choices node
5165
   --  laboriously, and of course we already know the list of choices that
5166
   --  corresponds to the others choice (it's the list we are replacing!)
5167
 
5168
   procedure Expand_N_Variant_Part (N : Node_Id) is
5169
      Last_Var    : constant Node_Id := Last_Non_Pragma (Variants (N));
5170
      Others_Node : Node_Id;
5171
   begin
5172
      if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
5173
         Others_Node := Make_Others_Choice (Sloc (Last_Var));
5174
         Set_Others_Discrete_Choices
5175
           (Others_Node, Discrete_Choices (Last_Var));
5176
         Set_Discrete_Choices (Last_Var, New_List (Others_Node));
5177
      end if;
5178
   end Expand_N_Variant_Part;
5179
 
5180
   ---------------------------------
5181
   -- Expand_Previous_Access_Type --
5182
   ---------------------------------
5183
 
5184
   procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
5185
      Ptr_Typ : Entity_Id;
5186
 
5187
   begin
5188
      --  Find all access types in the current scope whose designated type is
5189
      --  Def_Id and build master renamings for them.
5190
 
5191
      Ptr_Typ := First_Entity (Current_Scope);
5192
      while Present (Ptr_Typ) loop
5193
         if Is_Access_Type (Ptr_Typ)
5194
           and then Designated_Type (Ptr_Typ) = Def_Id
5195
           and then No (Master_Id (Ptr_Typ))
5196
         then
5197
            --  Ensure that the designated type has a master
5198
 
5199
            Build_Master_Entity (Def_Id);
5200
 
5201
            --  Private and incomplete types complicate the insertion of master
5202
            --  renamings because the access type may precede the full view of
5203
            --  the designated type. For this reason, the master renamings are
5204
            --  inserted relative to the designated type.
5205
 
5206
            Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
5207
         end if;
5208
 
5209
         Next_Entity (Ptr_Typ);
5210
      end loop;
5211
   end Expand_Previous_Access_Type;
5212
 
5213
   ------------------------
5214
   -- Expand_Tagged_Root --
5215
   ------------------------
5216
 
5217
   procedure Expand_Tagged_Root (T : Entity_Id) is
5218
      Def       : constant Node_Id := Type_Definition (Parent (T));
5219
      Comp_List : Node_Id;
5220
      Comp_Decl : Node_Id;
5221
      Sloc_N    : Source_Ptr;
5222
 
5223
   begin
5224
      if Null_Present (Def) then
5225
         Set_Component_List (Def,
5226
           Make_Component_List (Sloc (Def),
5227
             Component_Items => Empty_List,
5228
             Variant_Part => Empty,
5229
             Null_Present => True));
5230
      end if;
5231
 
5232
      Comp_List := Component_List (Def);
5233
 
5234
      if Null_Present (Comp_List)
5235
        or else Is_Empty_List (Component_Items (Comp_List))
5236
      then
5237
         Sloc_N := Sloc (Comp_List);
5238
      else
5239
         Sloc_N := Sloc (First (Component_Items (Comp_List)));
5240
      end if;
5241
 
5242
      Comp_Decl :=
5243
        Make_Component_Declaration (Sloc_N,
5244
          Defining_Identifier => First_Tag_Component (T),
5245
          Component_Definition =>
5246
            Make_Component_Definition (Sloc_N,
5247
              Aliased_Present => False,
5248
              Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5249
 
5250
      if Null_Present (Comp_List)
5251
        or else Is_Empty_List (Component_Items (Comp_List))
5252
      then
5253
         Set_Component_Items (Comp_List, New_List (Comp_Decl));
5254
         Set_Null_Present (Comp_List, False);
5255
 
5256
      else
5257
         Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5258
      end if;
5259
 
5260
      --  We don't Analyze the whole expansion because the tag component has
5261
      --  already been analyzed previously. Here we just insure that the tree
5262
      --  is coherent with the semantic decoration
5263
 
5264
      Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5265
 
5266
   exception
5267
      when RE_Not_Available =>
5268
         return;
5269
   end Expand_Tagged_Root;
5270
 
5271
   ----------------------
5272
   -- Clean_Task_Names --
5273
   ----------------------
5274
 
5275
   procedure Clean_Task_Names
5276
     (Typ     : Entity_Id;
5277
      Proc_Id : Entity_Id)
5278
   is
5279
   begin
5280
      if Has_Task (Typ)
5281
        and then not Restriction_Active (No_Implicit_Heap_Allocations)
5282
        and then not Global_Discard_Names
5283
        and then Tagged_Type_Expansion
5284
      then
5285
         Set_Uses_Sec_Stack (Proc_Id);
5286
      end if;
5287
   end Clean_Task_Names;
5288
 
5289
   ------------------------------
5290
   -- Expand_Freeze_Array_Type --
5291
   ------------------------------
5292
 
5293
   procedure Expand_Freeze_Array_Type (N : Node_Id) is
5294
      Typ      : constant Entity_Id := Entity (N);
5295
      Comp_Typ : constant Entity_Id := Component_Type (Typ);
5296
      Base     : constant Entity_Id := Base_Type (Typ);
5297
 
5298
   begin
5299
      if not Is_Bit_Packed_Array (Typ) then
5300
 
5301
         --  If the component contains tasks, so does the array type. This may
5302
         --  not be indicated in the array type because the component may have
5303
         --  been a private type at the point of definition. Same if component
5304
         --  type is controlled.
5305
 
5306
         Set_Has_Task (Base, Has_Task (Comp_Typ));
5307
         Set_Has_Controlled_Component (Base,
5308
           Has_Controlled_Component (Comp_Typ)
5309
             or else Is_Controlled (Comp_Typ));
5310
 
5311
         if No (Init_Proc (Base)) then
5312
 
5313
            --  If this is an anonymous array created for a declaration with
5314
            --  an initial value, its init_proc will never be called. The
5315
            --  initial value itself may have been expanded into assignments,
5316
            --  in which case the object declaration is carries the
5317
            --  No_Initialization flag.
5318
 
5319
            if Is_Itype (Base)
5320
              and then Nkind (Associated_Node_For_Itype (Base)) =
5321
                                                    N_Object_Declaration
5322
              and then (Present (Expression (Associated_Node_For_Itype (Base)))
5323
                          or else
5324
                        No_Initialization (Associated_Node_For_Itype (Base)))
5325
            then
5326
               null;
5327
 
5328
            --  We do not need an init proc for string or wide [wide] string,
5329
            --  since the only time these need initialization in normalize or
5330
            --  initialize scalars mode, and these types are treated specially
5331
            --  and do not need initialization procedures.
5332
 
5333
            elsif Root_Type (Base) = Standard_String
5334
              or else Root_Type (Base) = Standard_Wide_String
5335
              or else Root_Type (Base) = Standard_Wide_Wide_String
5336
            then
5337
               null;
5338
 
5339
            --  Otherwise we have to build an init proc for the subtype
5340
 
5341
            else
5342
               Build_Array_Init_Proc (Base, N);
5343
            end if;
5344
         end if;
5345
 
5346
         if Typ = Base then
5347
            if Has_Controlled_Component (Base) then
5348
               Build_Controlling_Procs (Base);
5349
 
5350
               if not Is_Limited_Type (Comp_Typ)
5351
                 and then Number_Dimensions (Typ) = 1
5352
               then
5353
                  Build_Slice_Assignment (Typ);
5354
               end if;
5355
            end if;
5356
 
5357
            --  Create a finalization master to service the anonymous access
5358
            --  components of the array.
5359
 
5360
            if Ekind (Comp_Typ) = E_Anonymous_Access_Type
5361
              and then Needs_Finalization (Designated_Type (Comp_Typ))
5362
            then
5363
               Build_Finalization_Master
5364
                 (Typ        => Comp_Typ,
5365
                  Ins_Node   => Parent (Typ),
5366
                  Encl_Scope => Scope (Typ));
5367
            end if;
5368
         end if;
5369
 
5370
      --  For packed case, default initialization, except if the component type
5371
      --  is itself a packed structure with an initialization procedure, or
5372
      --  initialize/normalize scalars active, and we have a base type, or the
5373
      --  type is public, because in that case a client might specify
5374
      --  Normalize_Scalars and there better be a public Init_Proc for it.
5375
 
5376
      elsif (Present (Init_Proc (Component_Type (Base)))
5377
               and then No (Base_Init_Proc (Base)))
5378
        or else (Init_Or_Norm_Scalars and then Base = Typ)
5379
        or else Is_Public (Typ)
5380
      then
5381
         Build_Array_Init_Proc (Base, N);
5382
      end if;
5383
   end Expand_Freeze_Array_Type;
5384
 
5385
   -----------------------------------
5386
   -- Expand_Freeze_Class_Wide_Type --
5387
   -----------------------------------
5388
 
5389
   procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
5390
      Typ  : constant Entity_Id := Entity (N);
5391
      Root : constant Entity_Id := Root_Type (Typ);
5392
 
5393
      function Is_C_Derivation (Typ : Entity_Id) return Boolean;
5394
      --  Given a type, determine whether it is derived from a C or C++ root
5395
 
5396
      ---------------------
5397
      -- Is_C_Derivation --
5398
      ---------------------
5399
 
5400
      function Is_C_Derivation (Typ : Entity_Id) return Boolean is
5401
         T : Entity_Id := Typ;
5402
 
5403
      begin
5404
         loop
5405
            if Is_CPP_Class (T)
5406
              or else Convention (T) = Convention_C
5407
              or else Convention (T) = Convention_CPP
5408
            then
5409
               return True;
5410
            end if;
5411
 
5412
            exit when T = Etype (T);
5413
 
5414
            T := Etype (T);
5415
         end loop;
5416
 
5417
         return False;
5418
      end Is_C_Derivation;
5419
 
5420
   --  Start of processing for Expand_Freeze_Class_Wide_Type
5421
 
5422
   begin
5423
      --  Certain run-time configurations and targets do not provide support
5424
      --  for controlled types.
5425
 
5426
      if Restriction_Active (No_Finalization) then
5427
         return;
5428
 
5429
      --  Do not create TSS routine Finalize_Address when dispatching calls are
5430
      --  disabled since the core of the routine is a dispatching call.
5431
 
5432
      elsif Restriction_Active (No_Dispatching_Calls) then
5433
         return;
5434
 
5435
      --  Do not create TSS routine Finalize_Address for concurrent class-wide
5436
      --  types. Ignore C, C++, CIL and Java types since it is assumed that the
5437
      --  non-Ada side will handle their destruction.
5438
 
5439
      elsif Is_Concurrent_Type (Root)
5440
        or else Is_C_Derivation (Root)
5441
        or else Convention (Typ) = Convention_CIL
5442
        or else Convention (Typ) = Convention_CPP
5443
        or else Convention (Typ) = Convention_Java
5444
      then
5445
         return;
5446
 
5447
      --  Do not create TSS routine Finalize_Address for .NET/JVM because these
5448
      --  targets do not support address arithmetic and unchecked conversions.
5449
 
5450
      elsif VM_Target /= No_VM then
5451
         return;
5452
 
5453
      --  Do not create TSS routine Finalize_Address when compiling in CodePeer
5454
      --  mode since the routine contains an Unchecked_Conversion.
5455
 
5456
      elsif CodePeer_Mode then
5457
         return;
5458
 
5459
      --  Do not create TSS routine Finalize_Address when compiling in Alfa
5460
      --  mode because it is not necessary and results in useless expansion.
5461
 
5462
      elsif Alfa_Mode then
5463
         return;
5464
      end if;
5465
 
5466
      --  Create the body of TSS primitive Finalize_Address. This automatically
5467
      --  sets the TSS entry for the class-wide type.
5468
 
5469
      Make_Finalize_Address_Body (Typ);
5470
   end Expand_Freeze_Class_Wide_Type;
5471
 
5472
   ------------------------------------
5473
   -- Expand_Freeze_Enumeration_Type --
5474
   ------------------------------------
5475
 
5476
   procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5477
      Typ           : constant Entity_Id  := Entity (N);
5478
      Loc           : constant Source_Ptr := Sloc (Typ);
5479
      Ent           : Entity_Id;
5480
      Lst           : List_Id;
5481
      Num           : Nat;
5482
      Arr           : Entity_Id;
5483
      Fent          : Entity_Id;
5484
      Ityp          : Entity_Id;
5485
      Is_Contiguous : Boolean;
5486
      Pos_Expr      : Node_Id;
5487
      Last_Repval   : Uint;
5488
 
5489
      Func : Entity_Id;
5490
      pragma Warnings (Off, Func);
5491
 
5492
   begin
5493
      --  Various optimizations possible if given representation is contiguous
5494
 
5495
      Is_Contiguous := True;
5496
 
5497
      Ent := First_Literal (Typ);
5498
      Last_Repval := Enumeration_Rep (Ent);
5499
 
5500
      Next_Literal (Ent);
5501
      while Present (Ent) loop
5502
         if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5503
            Is_Contiguous := False;
5504
            exit;
5505
         else
5506
            Last_Repval := Enumeration_Rep (Ent);
5507
         end if;
5508
 
5509
         Next_Literal (Ent);
5510
      end loop;
5511
 
5512
      if Is_Contiguous then
5513
         Set_Has_Contiguous_Rep (Typ);
5514
         Ent := First_Literal (Typ);
5515
         Num := 1;
5516
         Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5517
 
5518
      else
5519
         --  Build list of literal references
5520
 
5521
         Lst := New_List;
5522
         Num := 0;
5523
 
5524
         Ent := First_Literal (Typ);
5525
         while Present (Ent) loop
5526
            Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5527
            Num := Num + 1;
5528
            Next_Literal (Ent);
5529
         end loop;
5530
      end if;
5531
 
5532
      --  Now build an array declaration
5533
 
5534
      --    typA : array (Natural range 0 .. num - 1) of ctype :=
5535
      --             (v, v, v, v, v, ....)
5536
 
5537
      --  where ctype is the corresponding integer type. If the representation
5538
      --  is contiguous, we only keep the first literal, which provides the
5539
      --  offset for Pos_To_Rep computations.
5540
 
5541
      Arr :=
5542
        Make_Defining_Identifier (Loc,
5543
          Chars => New_External_Name (Chars (Typ), 'A'));
5544
 
5545
      Append_Freeze_Action (Typ,
5546
        Make_Object_Declaration (Loc,
5547
          Defining_Identifier => Arr,
5548
          Constant_Present    => True,
5549
 
5550
          Object_Definition   =>
5551
            Make_Constrained_Array_Definition (Loc,
5552
              Discrete_Subtype_Definitions => New_List (
5553
                Make_Subtype_Indication (Loc,
5554
                  Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5555
                  Constraint =>
5556
                    Make_Range_Constraint (Loc,
5557
                      Range_Expression =>
5558
                        Make_Range (Loc,
5559
                          Low_Bound  =>
5560
                            Make_Integer_Literal (Loc, 0),
5561
                          High_Bound =>
5562
                            Make_Integer_Literal (Loc, Num - 1))))),
5563
 
5564
              Component_Definition =>
5565
                Make_Component_Definition (Loc,
5566
                  Aliased_Present => False,
5567
                  Subtype_Indication => New_Reference_To (Typ, Loc))),
5568
 
5569
          Expression =>
5570
            Make_Aggregate (Loc,
5571
              Expressions => Lst)));
5572
 
5573
      Set_Enum_Pos_To_Rep (Typ, Arr);
5574
 
5575
      --  Now we build the function that converts representation values to
5576
      --  position values. This function has the form:
5577
 
5578
      --    function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5579
      --    begin
5580
      --       case ityp!(A) is
5581
      --         when enum-lit'Enum_Rep => return posval;
5582
      --         when enum-lit'Enum_Rep => return posval;
5583
      --         ...
5584
      --         when others   =>
5585
      --           [raise Constraint_Error when F "invalid data"]
5586
      --           return -1;
5587
      --       end case;
5588
      --    end;
5589
 
5590
      --  Note: the F parameter determines whether the others case (no valid
5591
      --  representation) raises Constraint_Error or returns a unique value
5592
      --  of minus one. The latter case is used, e.g. in 'Valid code.
5593
 
5594
      --  Note: the reason we use Enum_Rep values in the case here is to avoid
5595
      --  the code generator making inappropriate assumptions about the range
5596
      --  of the values in the case where the value is invalid. ityp is a
5597
      --  signed or unsigned integer type of appropriate width.
5598
 
5599
      --  Note: if exceptions are not supported, then we suppress the raise
5600
      --  and return -1 unconditionally (this is an erroneous program in any
5601
      --  case and there is no obligation to raise Constraint_Error here!) We
5602
      --  also do this if pragma Restrictions (No_Exceptions) is active.
5603
 
5604
      --  Is this right??? What about No_Exception_Propagation???
5605
 
5606
      --  Representations are signed
5607
 
5608
      if Enumeration_Rep (First_Literal (Typ)) < 0 then
5609
 
5610
         --  The underlying type is signed. Reset the Is_Unsigned_Type
5611
         --  explicitly, because it might have been inherited from
5612
         --  parent type.
5613
 
5614
         Set_Is_Unsigned_Type (Typ, False);
5615
 
5616
         if Esize (Typ) <= Standard_Integer_Size then
5617
            Ityp := Standard_Integer;
5618
         else
5619
            Ityp := Universal_Integer;
5620
         end if;
5621
 
5622
      --  Representations are unsigned
5623
 
5624
      else
5625
         if Esize (Typ) <= Standard_Integer_Size then
5626
            Ityp := RTE (RE_Unsigned);
5627
         else
5628
            Ityp := RTE (RE_Long_Long_Unsigned);
5629
         end if;
5630
      end if;
5631
 
5632
      --  The body of the function is a case statement. First collect case
5633
      --  alternatives, or optimize the contiguous case.
5634
 
5635
      Lst := New_List;
5636
 
5637
      --  If representation is contiguous, Pos is computed by subtracting
5638
      --  the representation of the first literal.
5639
 
5640
      if Is_Contiguous then
5641
         Ent := First_Literal (Typ);
5642
 
5643
         if Enumeration_Rep (Ent) = Last_Repval then
5644
 
5645
            --  Another special case: for a single literal, Pos is zero
5646
 
5647
            Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5648
 
5649
         else
5650
            Pos_Expr :=
5651
              Convert_To (Standard_Integer,
5652
                Make_Op_Subtract (Loc,
5653
                  Left_Opnd  =>
5654
                    Unchecked_Convert_To
5655
                     (Ityp, Make_Identifier (Loc, Name_uA)),
5656
                  Right_Opnd =>
5657
                    Make_Integer_Literal (Loc,
5658
                      Intval => Enumeration_Rep (First_Literal (Typ)))));
5659
         end if;
5660
 
5661
         Append_To (Lst,
5662
              Make_Case_Statement_Alternative (Loc,
5663
                Discrete_Choices => New_List (
5664
                  Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5665
                    Low_Bound =>
5666
                      Make_Integer_Literal (Loc,
5667
                       Intval =>  Enumeration_Rep (Ent)),
5668
                    High_Bound =>
5669
                      Make_Integer_Literal (Loc, Intval => Last_Repval))),
5670
 
5671
                Statements => New_List (
5672
                  Make_Simple_Return_Statement (Loc,
5673
                    Expression => Pos_Expr))));
5674
 
5675
      else
5676
         Ent := First_Literal (Typ);
5677
         while Present (Ent) loop
5678
            Append_To (Lst,
5679
              Make_Case_Statement_Alternative (Loc,
5680
                Discrete_Choices => New_List (
5681
                  Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5682
                    Intval => Enumeration_Rep (Ent))),
5683
 
5684
                Statements => New_List (
5685
                  Make_Simple_Return_Statement (Loc,
5686
                    Expression =>
5687
                      Make_Integer_Literal (Loc,
5688
                        Intval => Enumeration_Pos (Ent))))));
5689
 
5690
            Next_Literal (Ent);
5691
         end loop;
5692
      end if;
5693
 
5694
      --  In normal mode, add the others clause with the test
5695
 
5696
      if not No_Exception_Handlers_Set then
5697
         Append_To (Lst,
5698
           Make_Case_Statement_Alternative (Loc,
5699
             Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5700
             Statements => New_List (
5701
               Make_Raise_Constraint_Error (Loc,
5702
                 Condition => Make_Identifier (Loc, Name_uF),
5703
                 Reason    => CE_Invalid_Data),
5704
               Make_Simple_Return_Statement (Loc,
5705
                 Expression =>
5706
                   Make_Integer_Literal (Loc, -1)))));
5707
 
5708
      --  If either of the restrictions No_Exceptions_Handlers/Propagation is
5709
      --  active then return -1 (we cannot usefully raise Constraint_Error in
5710
      --  this case). See description above for further details.
5711
 
5712
      else
5713
         Append_To (Lst,
5714
           Make_Case_Statement_Alternative (Loc,
5715
             Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5716
             Statements => New_List (
5717
               Make_Simple_Return_Statement (Loc,
5718
                 Expression =>
5719
                   Make_Integer_Literal (Loc, -1)))));
5720
      end if;
5721
 
5722
      --  Now we can build the function body
5723
 
5724
      Fent :=
5725
        Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5726
 
5727
      Func :=
5728
        Make_Subprogram_Body (Loc,
5729
          Specification =>
5730
            Make_Function_Specification (Loc,
5731
              Defining_Unit_Name       => Fent,
5732
              Parameter_Specifications => New_List (
5733
                Make_Parameter_Specification (Loc,
5734
                  Defining_Identifier =>
5735
                    Make_Defining_Identifier (Loc, Name_uA),
5736
                  Parameter_Type => New_Reference_To (Typ, Loc)),
5737
                Make_Parameter_Specification (Loc,
5738
                  Defining_Identifier =>
5739
                    Make_Defining_Identifier (Loc, Name_uF),
5740
                  Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5741
 
5742
              Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5743
 
5744
            Declarations => Empty_List,
5745
 
5746
            Handled_Statement_Sequence =>
5747
              Make_Handled_Sequence_Of_Statements (Loc,
5748
                Statements => New_List (
5749
                  Make_Case_Statement (Loc,
5750
                    Expression =>
5751
                      Unchecked_Convert_To
5752
                        (Ityp, Make_Identifier (Loc, Name_uA)),
5753
                    Alternatives => Lst))));
5754
 
5755
      Set_TSS (Typ, Fent);
5756
 
5757
      --  Set Pure flag (it will be reset if the current context is not Pure).
5758
      --  We also pretend there was a pragma Pure_Function so that for purposes
5759
      --  of optimization and constant-folding, we will consider the function
5760
      --  Pure even if we are not in a Pure context).
5761
 
5762
      Set_Is_Pure (Fent);
5763
      Set_Has_Pragma_Pure_Function (Fent);
5764
 
5765
      --  Unless we are in -gnatD mode, where we are debugging generated code,
5766
      --  this is an internal entity for which we don't need debug info.
5767
 
5768
      if not Debug_Generated_Code then
5769
         Set_Debug_Info_Off (Fent);
5770
      end if;
5771
 
5772
   exception
5773
      when RE_Not_Available =>
5774
         return;
5775
   end Expand_Freeze_Enumeration_Type;
5776
 
5777
   -------------------------------
5778
   -- Expand_Freeze_Record_Type --
5779
   -------------------------------
5780
 
5781
   procedure Expand_Freeze_Record_Type (N : Node_Id) is
5782
      Def_Id      : constant Node_Id := Entity (N);
5783
      Type_Decl   : constant Node_Id := Parent (Def_Id);
5784
      Comp        : Entity_Id;
5785
      Comp_Typ    : Entity_Id;
5786
      Has_AACC    : Boolean;
5787
      Predef_List : List_Id;
5788
 
5789
      Renamed_Eq : Node_Id := Empty;
5790
      --  Defining unit name for the predefined equality function in the case
5791
      --  where the type has a primitive operation that is a renaming of
5792
      --  predefined equality (but only if there is also an overriding
5793
      --  user-defined equality function). Used to pass this entity from
5794
      --  Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5795
 
5796
      Wrapper_Decl_List : List_Id := No_List;
5797
      Wrapper_Body_List : List_Id := No_List;
5798
 
5799
   --  Start of processing for Expand_Freeze_Record_Type
5800
 
5801
   begin
5802
      --  Build discriminant checking functions if not a derived type (for
5803
      --  derived types that are not tagged types, always use the discriminant
5804
      --  checking functions of the parent type). However, for untagged types
5805
      --  the derivation may have taken place before the parent was frozen, so
5806
      --  we copy explicitly the discriminant checking functions from the
5807
      --  parent into the components of the derived type.
5808
 
5809
      if not Is_Derived_Type (Def_Id)
5810
        or else Has_New_Non_Standard_Rep (Def_Id)
5811
        or else Is_Tagged_Type (Def_Id)
5812
      then
5813
         Build_Discr_Checking_Funcs (Type_Decl);
5814
 
5815
      elsif Is_Derived_Type (Def_Id)
5816
        and then not Is_Tagged_Type (Def_Id)
5817
 
5818
        --  If we have a derived Unchecked_Union, we do not inherit the
5819
        --  discriminant checking functions from the parent type since the
5820
        --  discriminants are non existent.
5821
 
5822
        and then not Is_Unchecked_Union (Def_Id)
5823
        and then Has_Discriminants (Def_Id)
5824
      then
5825
         declare
5826
            Old_Comp : Entity_Id;
5827
 
5828
         begin
5829
            Old_Comp :=
5830
              First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5831
            Comp := First_Component (Def_Id);
5832
            while Present (Comp) loop
5833
               if Ekind (Comp) = E_Component
5834
                 and then Chars (Comp) = Chars (Old_Comp)
5835
               then
5836
                  Set_Discriminant_Checking_Func (Comp,
5837
                    Discriminant_Checking_Func (Old_Comp));
5838
               end if;
5839
 
5840
               Next_Component (Old_Comp);
5841
               Next_Component (Comp);
5842
            end loop;
5843
         end;
5844
      end if;
5845
 
5846
      if Is_Derived_Type (Def_Id)
5847
        and then Is_Limited_Type (Def_Id)
5848
        and then Is_Tagged_Type (Def_Id)
5849
      then
5850
         Check_Stream_Attributes (Def_Id);
5851
      end if;
5852
 
5853
      --  Update task and controlled component flags, because some of the
5854
      --  component types may have been private at the point of the record
5855
      --  declaration. Detect anonymous access-to-controlled components.
5856
 
5857
      Has_AACC := False;
5858
 
5859
      Comp := First_Component (Def_Id);
5860
      while Present (Comp) loop
5861
         Comp_Typ := Etype (Comp);
5862
 
5863
         if Has_Task (Comp_Typ) then
5864
            Set_Has_Task (Def_Id);
5865
 
5866
         --  Do not set Has_Controlled_Component on a class-wide equivalent
5867
         --  type. See Make_CW_Equivalent_Type.
5868
 
5869
         elsif not Is_Class_Wide_Equivalent_Type (Def_Id)
5870
           and then (Has_Controlled_Component (Comp_Typ)
5871
                      or else (Chars (Comp) /= Name_uParent
5872
                                and then Is_Controlled (Comp_Typ)))
5873
         then
5874
            Set_Has_Controlled_Component (Def_Id);
5875
 
5876
         --  Non-self-referential anonymous access-to-controlled component
5877
 
5878
         elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5879
           and then Needs_Finalization (Designated_Type (Comp_Typ))
5880
           and then Designated_Type (Comp_Typ) /= Def_Id
5881
         then
5882
            Has_AACC := True;
5883
         end if;
5884
 
5885
         Next_Component (Comp);
5886
      end loop;
5887
 
5888
      --  Handle constructors of non-tagged CPP_Class types
5889
 
5890
      if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
5891
         Set_CPP_Constructors (Def_Id);
5892
      end if;
5893
 
5894
      --  Creation of the Dispatch Table. Note that a Dispatch Table is built
5895
      --  for regular tagged types as well as for Ada types deriving from a C++
5896
      --  Class, but not for tagged types directly corresponding to C++ classes
5897
      --  In the later case we assume that it is created in the C++ side and we
5898
      --  just use it.
5899
 
5900
      if Is_Tagged_Type (Def_Id) then
5901
 
5902
         --  Add the _Tag component
5903
 
5904
         if Underlying_Type (Etype (Def_Id)) = Def_Id then
5905
            Expand_Tagged_Root (Def_Id);
5906
         end if;
5907
 
5908
         if Is_CPP_Class (Def_Id) then
5909
            Set_All_DT_Position (Def_Id);
5910
 
5911
            --  Create the tag entities with a minimum decoration
5912
 
5913
            if Tagged_Type_Expansion then
5914
               Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5915
            end if;
5916
 
5917
            Set_CPP_Constructors (Def_Id);
5918
 
5919
         else
5920
            if not Building_Static_DT (Def_Id) then
5921
 
5922
               --  Usually inherited primitives are not delayed but the first
5923
               --  Ada extension of a CPP_Class is an exception since the
5924
               --  address of the inherited subprogram has to be inserted in
5925
               --  the new Ada Dispatch Table and this is a freezing action.
5926
 
5927
               --  Similarly, if this is an inherited operation whose parent is
5928
               --  not frozen yet, it is not in the DT of the parent, and we
5929
               --  generate an explicit freeze node for the inherited operation
5930
               --  so it is properly inserted in the DT of the current type.
5931
 
5932
               declare
5933
                  Elmt : Elmt_Id;
5934
                  Subp : Entity_Id;
5935
 
5936
               begin
5937
                  Elmt := First_Elmt (Primitive_Operations (Def_Id));
5938
                  while Present (Elmt) loop
5939
                     Subp := Node (Elmt);
5940
 
5941
                     if Present (Alias (Subp)) then
5942
                        if Is_CPP_Class (Etype (Def_Id)) then
5943
                           Set_Has_Delayed_Freeze (Subp);
5944
 
5945
                        elsif Has_Delayed_Freeze (Alias (Subp))
5946
                          and then not Is_Frozen (Alias (Subp))
5947
                        then
5948
                           Set_Is_Frozen (Subp, False);
5949
                           Set_Has_Delayed_Freeze (Subp);
5950
                        end if;
5951
                     end if;
5952
 
5953
                     Next_Elmt (Elmt);
5954
                  end loop;
5955
               end;
5956
            end if;
5957
 
5958
            --  Unfreeze momentarily the type to add the predefined primitives
5959
            --  operations. The reason we unfreeze is so that these predefined
5960
            --  operations will indeed end up as primitive operations (which
5961
            --  must be before the freeze point).
5962
 
5963
            Set_Is_Frozen (Def_Id, False);
5964
 
5965
            --  Do not add the spec of predefined primitives in case of
5966
            --  CPP tagged type derivations that have convention CPP.
5967
 
5968
            if Is_CPP_Class (Root_Type (Def_Id))
5969
              and then Convention (Def_Id) = Convention_CPP
5970
            then
5971
               null;
5972
 
5973
            --  Do not add the spec of predefined primitives in case of
5974
            --  CIL and Java tagged types
5975
 
5976
            elsif Convention (Def_Id) = Convention_CIL
5977
              or else Convention (Def_Id) = Convention_Java
5978
            then
5979
               null;
5980
 
5981
            --  Do not add the spec of the predefined primitives if we are
5982
            --  compiling under restriction No_Dispatching_Calls.
5983
 
5984
            elsif not Restriction_Active (No_Dispatching_Calls) then
5985
               Make_Predefined_Primitive_Specs
5986
                 (Def_Id, Predef_List, Renamed_Eq);
5987
               Insert_List_Before_And_Analyze (N, Predef_List);
5988
            end if;
5989
 
5990
            --  Ada 2005 (AI-391): For a nonabstract null extension, create
5991
            --  wrapper functions for each nonoverridden inherited function
5992
            --  with a controlling result of the type. The wrapper for such
5993
            --  a function returns an extension aggregate that invokes the
5994
            --  parent function.
5995
 
5996
            if Ada_Version >= Ada_2005
5997
              and then not Is_Abstract_Type (Def_Id)
5998
              and then Is_Null_Extension (Def_Id)
5999
            then
6000
               Make_Controlling_Function_Wrappers
6001
                 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6002
               Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6003
            end if;
6004
 
6005
            --  Ada 2005 (AI-251): For a nonabstract type extension, build
6006
            --  null procedure declarations for each set of homographic null
6007
            --  procedures that are inherited from interface types but not
6008
            --  overridden. This is done to ensure that the dispatch table
6009
            --  entry associated with such null primitives are properly filled.
6010
 
6011
            if Ada_Version >= Ada_2005
6012
              and then Etype (Def_Id) /= Def_Id
6013
              and then not Is_Abstract_Type (Def_Id)
6014
              and then Has_Interfaces (Def_Id)
6015
            then
6016
               Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6017
            end if;
6018
 
6019
            Set_Is_Frozen (Def_Id);
6020
            if not Is_Derived_Type (Def_Id)
6021
              or else Is_Tagged_Type (Etype (Def_Id))
6022
            then
6023
               Set_All_DT_Position (Def_Id);
6024
            end if;
6025
 
6026
            --  Create and decorate the tags. Suppress their creation when
6027
            --  VM_Target because the dispatching mechanism is handled
6028
            --  internally by the VMs.
6029
 
6030
            if Tagged_Type_Expansion then
6031
               Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6032
 
6033
               --  Generate dispatch table of locally defined tagged type.
6034
               --  Dispatch tables of library level tagged types are built
6035
               --  later (see Analyze_Declarations).
6036
 
6037
               if not Building_Static_DT (Def_Id) then
6038
                  Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
6039
               end if;
6040
 
6041
            elsif VM_Target /= No_VM then
6042
               Append_Freeze_Actions (Def_Id, Make_VM_TSD (Def_Id));
6043
            end if;
6044
 
6045
            --  If the type has unknown discriminants, propagate dispatching
6046
            --  information to its underlying record view, which does not get
6047
            --  its own dispatch table.
6048
 
6049
            if Is_Derived_Type (Def_Id)
6050
              and then Has_Unknown_Discriminants (Def_Id)
6051
              and then Present (Underlying_Record_View (Def_Id))
6052
            then
6053
               declare
6054
                  Rep : constant Entity_Id := Underlying_Record_View (Def_Id);
6055
               begin
6056
                  Set_Access_Disp_Table
6057
                    (Rep, Access_Disp_Table       (Def_Id));
6058
                  Set_Dispatch_Table_Wrappers
6059
                    (Rep, Dispatch_Table_Wrappers (Def_Id));
6060
                  Set_Direct_Primitive_Operations
6061
                    (Rep, Direct_Primitive_Operations (Def_Id));
6062
               end;
6063
            end if;
6064
 
6065
            --  Make sure that the primitives Initialize, Adjust and Finalize
6066
            --  are Frozen before other TSS subprograms. We don't want them
6067
            --  Frozen inside.
6068
 
6069
            if Is_Controlled (Def_Id) then
6070
               if not Is_Limited_Type (Def_Id) then
6071
                  Append_Freeze_Actions (Def_Id,
6072
                    Freeze_Entity
6073
                      (Find_Prim_Op (Def_Id, Name_Adjust), Def_Id));
6074
               end if;
6075
 
6076
               Append_Freeze_Actions (Def_Id,
6077
                 Freeze_Entity
6078
                   (Find_Prim_Op (Def_Id, Name_Initialize), Def_Id));
6079
 
6080
               Append_Freeze_Actions (Def_Id,
6081
                 Freeze_Entity
6082
                   (Find_Prim_Op (Def_Id, Name_Finalize), Def_Id));
6083
            end if;
6084
 
6085
            --  Freeze rest of primitive operations. There is no need to handle
6086
            --  the predefined primitives if we are compiling under restriction
6087
            --  No_Dispatching_Calls.
6088
 
6089
            if not Restriction_Active (No_Dispatching_Calls) then
6090
               Append_Freeze_Actions
6091
                 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
6092
            end if;
6093
         end if;
6094
 
6095
      --  In the non-tagged case, ever since Ada 83 an equality function must
6096
      --  be  provided for variant records that are not unchecked unions.
6097
      --  In Ada 2012 the equality function composes, and thus must be built
6098
      --  explicitly just as for tagged records.
6099
 
6100
      elsif Has_Discriminants (Def_Id)
6101
        and then not Is_Limited_Type (Def_Id)
6102
      then
6103
         declare
6104
            Comps : constant Node_Id :=
6105
                      Component_List (Type_Definition (Type_Decl));
6106
         begin
6107
            if Present (Comps)
6108
              and then Present (Variant_Part (Comps))
6109
            then
6110
               Build_Variant_Record_Equality (Def_Id);
6111
            end if;
6112
         end;
6113
 
6114
      --  Otherwise create primitive equality operation (AI05-0123)
6115
 
6116
      --  This is done unconditionally to ensure that tools can be linked
6117
      --  properly with user programs compiled with older language versions.
6118
      --  It might be worth including a switch to revert to a non-composable
6119
      --  equality for untagged records, even though no program depending on
6120
      --  non-composability has surfaced ???
6121
 
6122
      elsif Comes_From_Source (Def_Id)
6123
        and then Convention (Def_Id) = Convention_Ada
6124
        and then not Is_Limited_Type (Def_Id)
6125
      then
6126
         Build_Untagged_Equality (Def_Id);
6127
      end if;
6128
 
6129
      --  Before building the record initialization procedure, if we are
6130
      --  dealing with a concurrent record value type, then we must go through
6131
      --  the discriminants, exchanging discriminals between the concurrent
6132
      --  type and the concurrent record value type. See the section "Handling
6133
      --  of Discriminants" in the Einfo spec for details.
6134
 
6135
      if Is_Concurrent_Record_Type (Def_Id)
6136
        and then Has_Discriminants (Def_Id)
6137
      then
6138
         declare
6139
            Ctyp       : constant Entity_Id :=
6140
                           Corresponding_Concurrent_Type (Def_Id);
6141
            Conc_Discr : Entity_Id;
6142
            Rec_Discr  : Entity_Id;
6143
            Temp       : Entity_Id;
6144
 
6145
         begin
6146
            Conc_Discr := First_Discriminant (Ctyp);
6147
            Rec_Discr  := First_Discriminant (Def_Id);
6148
            while Present (Conc_Discr) loop
6149
               Temp := Discriminal (Conc_Discr);
6150
               Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6151
               Set_Discriminal (Rec_Discr, Temp);
6152
 
6153
               Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6154
               Set_Discriminal_Link (Discriminal (Rec_Discr),  Rec_Discr);
6155
 
6156
               Next_Discriminant (Conc_Discr);
6157
               Next_Discriminant (Rec_Discr);
6158
            end loop;
6159
         end;
6160
      end if;
6161
 
6162
      if Has_Controlled_Component (Def_Id) then
6163
         Build_Controlling_Procs (Def_Id);
6164
      end if;
6165
 
6166
      Adjust_Discriminants (Def_Id);
6167
 
6168
      if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
6169
 
6170
         --  Do not need init for interfaces on e.g. CIL since they're
6171
         --  abstract. Helps operation of peverify (the PE Verify tool).
6172
 
6173
         Build_Record_Init_Proc (Type_Decl, Def_Id);
6174
      end if;
6175
 
6176
      --  For tagged type that are not interfaces, build bodies of primitive
6177
      --  operations. Note: do this after building the record initialization
6178
      --  procedure, since the primitive operations may need the initialization
6179
      --  routine. There is no need to add predefined primitives of interfaces
6180
      --  because all their predefined primitives are abstract.
6181
 
6182
      if Is_Tagged_Type (Def_Id)
6183
        and then not Is_Interface (Def_Id)
6184
      then
6185
         --  Do not add the body of predefined primitives in case of
6186
         --  CPP tagged type derivations that have convention CPP.
6187
 
6188
         if Is_CPP_Class (Root_Type (Def_Id))
6189
           and then Convention (Def_Id) = Convention_CPP
6190
         then
6191
            null;
6192
 
6193
         --  Do not add the body of predefined primitives in case of
6194
         --  CIL and Java tagged types.
6195
 
6196
         elsif Convention (Def_Id) = Convention_CIL
6197
           or else Convention (Def_Id) = Convention_Java
6198
         then
6199
            null;
6200
 
6201
         --  Do not add the body of the predefined primitives if we are
6202
         --  compiling under restriction No_Dispatching_Calls or if we are
6203
         --  compiling a CPP tagged type.
6204
 
6205
         elsif not Restriction_Active (No_Dispatching_Calls) then
6206
 
6207
            --  Create the body of TSS primitive Finalize_Address. This must
6208
            --  be done before the bodies of all predefined primitives are
6209
            --  created. If Def_Id is limited, Stream_Input and Stream_Read
6210
            --  may produce build-in-place allocations and for those the
6211
            --  expander needs Finalize_Address. Do not create the body of
6212
            --  Finalize_Address in Alfa mode since it is not needed.
6213
 
6214
            if not Alfa_Mode then
6215
               Make_Finalize_Address_Body (Def_Id);
6216
            end if;
6217
 
6218
            Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6219
            Append_Freeze_Actions (Def_Id, Predef_List);
6220
         end if;
6221
 
6222
         --  Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6223
         --  inherited functions, then add their bodies to the freeze actions.
6224
 
6225
         if Present (Wrapper_Body_List) then
6226
            Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6227
         end if;
6228
 
6229
         --  Create extra formals for the primitive operations of the type.
6230
         --  This must be done before analyzing the body of the initialization
6231
         --  procedure, because a self-referential type might call one of these
6232
         --  primitives in the body of the init_proc itself.
6233
 
6234
         declare
6235
            Elmt : Elmt_Id;
6236
            Subp : Entity_Id;
6237
 
6238
         begin
6239
            Elmt := First_Elmt (Primitive_Operations (Def_Id));
6240
            while Present (Elmt) loop
6241
               Subp := Node (Elmt);
6242
               if not Has_Foreign_Convention (Subp)
6243
                 and then not Is_Predefined_Dispatching_Operation (Subp)
6244
               then
6245
                  Create_Extra_Formals (Subp);
6246
               end if;
6247
 
6248
               Next_Elmt (Elmt);
6249
            end loop;
6250
         end;
6251
      end if;
6252
 
6253
      --  Create a heterogeneous finalization master to service the anonymous
6254
      --  access-to-controlled components of the record type.
6255
 
6256
      if Has_AACC then
6257
         declare
6258
            Encl_Scope : constant Entity_Id  := Scope (Def_Id);
6259
            Ins_Node   : constant Node_Id    := Parent (Def_Id);
6260
            Loc        : constant Source_Ptr := Sloc (Def_Id);
6261
            Fin_Mas_Id : Entity_Id;
6262
 
6263
            Attributes_Set : Boolean := False;
6264
            Master_Built   : Boolean := False;
6265
            --  Two flags which control the creation and initialization of a
6266
            --  common heterogeneous master.
6267
 
6268
         begin
6269
            Comp := First_Component (Def_Id);
6270
            while Present (Comp) loop
6271
               Comp_Typ := Etype (Comp);
6272
 
6273
               --  A non-self-referential anonymous access-to-controlled
6274
               --  component.
6275
 
6276
               if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6277
                 and then Needs_Finalization (Designated_Type (Comp_Typ))
6278
                 and then Designated_Type (Comp_Typ) /= Def_Id
6279
               then
6280
                  if VM_Target = No_VM then
6281
 
6282
                     --  Build a homogeneous master for the first anonymous
6283
                     --  access-to-controlled component. This master may be
6284
                     --  converted into a heterogeneous collection if more
6285
                     --  components are to follow.
6286
 
6287
                     if not Master_Built then
6288
                        Master_Built := True;
6289
 
6290
                        --  All anonymous access-to-controlled types allocate
6291
                        --  on the global pool.
6292
 
6293
                        Set_Associated_Storage_Pool (Comp_Typ,
6294
                          Get_Global_Pool_For_Access_Type (Comp_Typ));
6295
 
6296
                        Build_Finalization_Master
6297
                          (Typ        => Comp_Typ,
6298
                           Ins_Node   => Ins_Node,
6299
                           Encl_Scope => Encl_Scope);
6300
 
6301
                        Fin_Mas_Id := Finalization_Master (Comp_Typ);
6302
 
6303
                     --  Subsequent anonymous access-to-controlled components
6304
                     --  reuse the already available master.
6305
 
6306
                     else
6307
                        --  All anonymous access-to-controlled types allocate
6308
                        --  on the global pool.
6309
 
6310
                        Set_Associated_Storage_Pool (Comp_Typ,
6311
                          Get_Global_Pool_For_Access_Type (Comp_Typ));
6312
 
6313
                        --  Shared the master among multiple components
6314
 
6315
                        Set_Finalization_Master (Comp_Typ, Fin_Mas_Id);
6316
 
6317
                        --  Convert the master into a heterogeneous collection.
6318
                        --  Generate:
6319
                        --
6320
                        --    Set_Is_Heterogeneous (<Fin_Mas_Id>);
6321
 
6322
                        if not Attributes_Set then
6323
                           Attributes_Set := True;
6324
 
6325
                           Insert_Action (Ins_Node,
6326
                             Make_Procedure_Call_Statement (Loc,
6327
                               Name =>
6328
                                 New_Reference_To
6329
                                   (RTE (RE_Set_Is_Heterogeneous), Loc),
6330
                               Parameter_Associations => New_List (
6331
                                 New_Reference_To (Fin_Mas_Id, Loc))));
6332
                        end if;
6333
                     end if;
6334
 
6335
                  --  Since .NET/JVM targets do not support heterogeneous
6336
                  --  masters, each component must have its own master.
6337
 
6338
                  else
6339
                     Build_Finalization_Master
6340
                       (Typ        => Comp_Typ,
6341
                        Ins_Node   => Ins_Node,
6342
                        Encl_Scope => Encl_Scope);
6343
                  end if;
6344
               end if;
6345
 
6346
               Next_Component (Comp);
6347
            end loop;
6348
         end;
6349
      end if;
6350
   end Expand_Freeze_Record_Type;
6351
 
6352
   ------------------------------
6353
   -- Freeze_Stream_Operations --
6354
   ------------------------------
6355
 
6356
   procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6357
      Names     : constant array (1 .. 4) of TSS_Name_Type :=
6358
                    (TSS_Stream_Input,
6359
                     TSS_Stream_Output,
6360
                     TSS_Stream_Read,
6361
                     TSS_Stream_Write);
6362
      Stream_Op : Entity_Id;
6363
 
6364
   begin
6365
      --  Primitive operations of tagged types are frozen when the dispatch
6366
      --  table is constructed.
6367
 
6368
      if not Comes_From_Source (Typ)
6369
        or else Is_Tagged_Type (Typ)
6370
      then
6371
         return;
6372
      end if;
6373
 
6374
      for J in Names'Range loop
6375
         Stream_Op := TSS (Typ, Names (J));
6376
 
6377
         if Present (Stream_Op)
6378
           and then Is_Subprogram (Stream_Op)
6379
           and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6380
                      N_Subprogram_Declaration
6381
           and then not Is_Frozen (Stream_Op)
6382
         then
6383
            Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
6384
         end if;
6385
      end loop;
6386
   end Freeze_Stream_Operations;
6387
 
6388
   -----------------
6389
   -- Freeze_Type --
6390
   -----------------
6391
 
6392
   --  Full type declarations are expanded at the point at which the type is
6393
   --  frozen. The formal N is the Freeze_Node for the type. Any statements or
6394
   --  declarations generated by the freezing (e.g. the procedure generated
6395
   --  for initialization) are chained in the Actions field list of the freeze
6396
   --  node using Append_Freeze_Actions.
6397
 
6398
   function Freeze_Type (N : Node_Id) return Boolean is
6399
      Def_Id    : constant Entity_Id := Entity (N);
6400
      RACW_Seen : Boolean := False;
6401
      Result    : Boolean := False;
6402
 
6403
   begin
6404
      --  Process associated access types needing special processing
6405
 
6406
      if Present (Access_Types_To_Process (N)) then
6407
         declare
6408
            E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
6409
         begin
6410
            while Present (E) loop
6411
 
6412
               if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6413
                  Validate_RACW_Primitives (Node (E));
6414
                  RACW_Seen := True;
6415
               end if;
6416
 
6417
               E := Next_Elmt (E);
6418
            end loop;
6419
         end;
6420
 
6421
         if RACW_Seen then
6422
 
6423
            --  If there are RACWs designating this type, make stubs now
6424
 
6425
            Remote_Types_Tagged_Full_View_Encountered (Def_Id);
6426
         end if;
6427
      end if;
6428
 
6429
      --  Freeze processing for record types
6430
 
6431
      if Is_Record_Type (Def_Id) then
6432
         if Ekind (Def_Id) = E_Record_Type then
6433
            Expand_Freeze_Record_Type (N);
6434
 
6435
         elsif Is_Class_Wide_Type (Def_Id) then
6436
            Expand_Freeze_Class_Wide_Type (N);
6437
         end if;
6438
 
6439
      --  Freeze processing for array types
6440
 
6441
      elsif Is_Array_Type (Def_Id) then
6442
         Expand_Freeze_Array_Type (N);
6443
 
6444
      --  Freeze processing for access types
6445
 
6446
      --  For pool-specific access types, find out the pool object used for
6447
      --  this type, needs actual expansion of it in some cases. Here are the
6448
      --  different cases :
6449
 
6450
      --  1. Rep Clause "for Def_Id'Storage_Size use 0;"
6451
      --      ---> don't use any storage pool
6452
 
6453
      --  2. Rep Clause : for Def_Id'Storage_Size use Expr.
6454
      --     Expand:
6455
      --      Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
6456
 
6457
      --  3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6458
      --      ---> Storage Pool is the specified one
6459
 
6460
      --  See GNAT Pool packages in the Run-Time for more details
6461
 
6462
      elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
6463
         declare
6464
            Loc         : constant Source_Ptr := Sloc (N);
6465
            Desig_Type  : constant Entity_Id  := Designated_Type (Def_Id);
6466
            Pool_Object : Entity_Id;
6467
 
6468
            Freeze_Action_Typ : Entity_Id;
6469
 
6470
         begin
6471
            --  Case 1
6472
 
6473
            --    Rep Clause "for Def_Id'Storage_Size use 0;"
6474
            --    ---> don't use any storage pool
6475
 
6476
            if No_Pool_Assigned (Def_Id) then
6477
               null;
6478
 
6479
            --  Case 2
6480
 
6481
            --    Rep Clause : for Def_Id'Storage_Size use Expr.
6482
            --    ---> Expand:
6483
            --           Def_Id__Pool : Stack_Bounded_Pool
6484
            --                            (Expr, DT'Size, DT'Alignment);
6485
 
6486
            elsif Has_Storage_Size_Clause (Def_Id) then
6487
               declare
6488
                  DT_Size  : Node_Id;
6489
                  DT_Align : Node_Id;
6490
 
6491
               begin
6492
                  --  For unconstrained composite types we give a size of zero
6493
                  --  so that the pool knows that it needs a special algorithm
6494
                  --  for variable size object allocation.
6495
 
6496
                  if Is_Composite_Type (Desig_Type)
6497
                    and then not Is_Constrained (Desig_Type)
6498
                  then
6499
                     DT_Size :=
6500
                       Make_Integer_Literal (Loc, 0);
6501
 
6502
                     DT_Align :=
6503
                       Make_Integer_Literal (Loc, Maximum_Alignment);
6504
 
6505
                  else
6506
                     DT_Size :=
6507
                       Make_Attribute_Reference (Loc,
6508
                         Prefix => New_Reference_To (Desig_Type, Loc),
6509
                         Attribute_Name => Name_Max_Size_In_Storage_Elements);
6510
 
6511
                     DT_Align :=
6512
                       Make_Attribute_Reference (Loc,
6513
                         Prefix => New_Reference_To (Desig_Type, Loc),
6514
                         Attribute_Name => Name_Alignment);
6515
                  end if;
6516
 
6517
                  Pool_Object :=
6518
                    Make_Defining_Identifier (Loc,
6519
                      Chars => New_External_Name (Chars (Def_Id), 'P'));
6520
 
6521
                  --  We put the code associated with the pools in the entity
6522
                  --  that has the later freeze node, usually the access type
6523
                  --  but it can also be the designated_type; because the pool
6524
                  --  code requires both those types to be frozen
6525
 
6526
                  if Is_Frozen (Desig_Type)
6527
                    and then (No (Freeze_Node (Desig_Type))
6528
                               or else Analyzed (Freeze_Node (Desig_Type)))
6529
                  then
6530
                     Freeze_Action_Typ := Def_Id;
6531
 
6532
                  --  A Taft amendment type cannot get the freeze actions
6533
                  --  since the full view is not there.
6534
 
6535
                  elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6536
                    and then No (Full_View (Desig_Type))
6537
                  then
6538
                     Freeze_Action_Typ := Def_Id;
6539
 
6540
                  else
6541
                     Freeze_Action_Typ := Desig_Type;
6542
                  end if;
6543
 
6544
                  Append_Freeze_Action (Freeze_Action_Typ,
6545
                    Make_Object_Declaration (Loc,
6546
                      Defining_Identifier => Pool_Object,
6547
                      Object_Definition =>
6548
                        Make_Subtype_Indication (Loc,
6549
                          Subtype_Mark =>
6550
                            New_Reference_To
6551
                              (RTE (RE_Stack_Bounded_Pool), Loc),
6552
 
6553
                          Constraint =>
6554
                            Make_Index_Or_Discriminant_Constraint (Loc,
6555
                              Constraints => New_List (
6556
 
6557
                              --  First discriminant is the Pool Size
6558
 
6559
                                New_Reference_To (
6560
                                  Storage_Size_Variable (Def_Id), Loc),
6561
 
6562
                              --  Second discriminant is the element size
6563
 
6564
                                DT_Size,
6565
 
6566
                              --  Third discriminant is the alignment
6567
 
6568
                                DT_Align)))));
6569
               end;
6570
 
6571
               Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6572
 
6573
            --  Case 3
6574
 
6575
            --    Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6576
            --    ---> Storage Pool is the specified one
6577
 
6578
            --  When compiling in Ada 2012 mode, ensure that the accessibility
6579
            --  level of the subpool access type is not deeper than that of the
6580
            --  pool_with_subpools. This check is not performed on .NET/JVM
6581
            --  since those targets do not support pools.
6582
 
6583
            elsif Ada_Version >= Ada_2012
6584
              and then Present (Associated_Storage_Pool (Def_Id))
6585
              and then VM_Target = No_VM
6586
            then
6587
               declare
6588
                  Loc   : constant Source_Ptr := Sloc (Def_Id);
6589
                  Pool  : constant Entity_Id :=
6590
                            Associated_Storage_Pool (Def_Id);
6591
                  RSPWS : constant Entity_Id :=
6592
                            RTE (RE_Root_Storage_Pool_With_Subpools);
6593
 
6594
               begin
6595
                  --  It is known that the accessibility level of the access
6596
                  --  type is deeper than that of the pool.
6597
 
6598
                  if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
6599
                    and then not Accessibility_Checks_Suppressed (Def_Id)
6600
                    and then not Accessibility_Checks_Suppressed (Pool)
6601
                  then
6602
                     --  Static case: the pool is known to be a descendant of
6603
                     --  Root_Storage_Pool_With_Subpools.
6604
 
6605
                     if Is_Ancestor (RSPWS, Etype (Pool)) then
6606
                        Error_Msg_N
6607
                          ("?subpool access type has deeper accessibility " &
6608
                           "level than pool", Def_Id);
6609
 
6610
                        Append_Freeze_Action (Def_Id,
6611
                          Make_Raise_Program_Error (Loc,
6612
                            Reason => PE_Accessibility_Check_Failed));
6613
 
6614
                     --  Dynamic case: when the pool is of a class-wide type,
6615
                     --  it may or may not support subpools depending on the
6616
                     --  path of derivation. Generate:
6617
 
6618
                     --    if Def_Id in RSPWS'Class then
6619
                     --       raise Program_Error;
6620
                     --    end if;
6621
 
6622
                     elsif Is_Class_Wide_Type (Etype (Pool)) then
6623
                        Append_Freeze_Action (Def_Id,
6624
                          Make_If_Statement (Loc,
6625
                            Condition =>
6626
                              Make_In (Loc,
6627
                                Left_Opnd =>
6628
                                  New_Reference_To (Pool, Loc),
6629
                                Right_Opnd =>
6630
                                  New_Reference_To
6631
                                    (Class_Wide_Type (RSPWS), Loc)),
6632
 
6633
                            Then_Statements => New_List (
6634
                              Make_Raise_Program_Error (Loc,
6635
                                Reason => PE_Accessibility_Check_Failed))));
6636
                     end if;
6637
                  end if;
6638
               end;
6639
            end if;
6640
 
6641
            --  For access-to-controlled types (including class-wide types and
6642
            --  Taft-amendment types, which potentially have controlled
6643
            --  components), expand the list controller object that will store
6644
            --  the dynamically allocated objects. Don't do this transformation
6645
            --  for expander-generated access types, but do it for types that
6646
            --  are the full view of types derived from other private types.
6647
            --  Also suppress the list controller in the case of a designated
6648
            --  type with convention Java, since this is used when binding to
6649
            --  Java API specs, where there's no equivalent of a finalization
6650
            --  list and we don't want to pull in the finalization support if
6651
            --  not needed.
6652
 
6653
            if not Comes_From_Source (Def_Id)
6654
              and then not Has_Private_Declaration (Def_Id)
6655
            then
6656
               null;
6657
 
6658
            --  An exception is made for types defined in the run-time because
6659
            --  Ada.Tags.Tag itself is such a type and cannot afford this
6660
            --  unnecessary overhead that would generates a loop in the
6661
            --  expansion scheme. Another exception is if Restrictions
6662
            --  (No_Finalization) is active, since then we know nothing is
6663
            --  controlled.
6664
 
6665
            elsif Restriction_Active (No_Finalization)
6666
              or else In_Runtime (Def_Id)
6667
            then
6668
               null;
6669
 
6670
            --  Assume that incomplete and private types are always completed
6671
            --  by a controlled full view.
6672
 
6673
            elsif Needs_Finalization (Desig_Type)
6674
              or else
6675
                (Is_Incomplete_Or_Private_Type (Desig_Type)
6676
                  and then No (Full_View (Desig_Type)))
6677
              or else
6678
                (Is_Array_Type (Desig_Type)
6679
                  and then Needs_Finalization (Component_Type (Desig_Type)))
6680
            then
6681
               Build_Finalization_Master (Def_Id);
6682
            end if;
6683
         end;
6684
 
6685
      --  Freeze processing for enumeration types
6686
 
6687
      elsif Ekind (Def_Id) = E_Enumeration_Type then
6688
 
6689
         --  We only have something to do if we have a non-standard
6690
         --  representation (i.e. at least one literal whose pos value
6691
         --  is not the same as its representation)
6692
 
6693
         if Has_Non_Standard_Rep (Def_Id) then
6694
            Expand_Freeze_Enumeration_Type (N);
6695
         end if;
6696
 
6697
      --  Private types that are completed by a derivation from a private
6698
      --  type have an internally generated full view, that needs to be
6699
      --  frozen. This must be done explicitly because the two views share
6700
      --  the freeze node, and the underlying full view is not visible when
6701
      --  the freeze node is analyzed.
6702
 
6703
      elsif Is_Private_Type (Def_Id)
6704
        and then Is_Derived_Type (Def_Id)
6705
        and then Present (Full_View (Def_Id))
6706
        and then Is_Itype (Full_View (Def_Id))
6707
        and then Has_Private_Declaration (Full_View (Def_Id))
6708
        and then Freeze_Node (Full_View (Def_Id)) = N
6709
      then
6710
         Set_Entity (N, Full_View (Def_Id));
6711
         Result := Freeze_Type (N);
6712
         Set_Entity (N, Def_Id);
6713
 
6714
      --  All other types require no expander action. There are such cases
6715
      --  (e.g. task types and protected types). In such cases, the freeze
6716
      --  nodes are there for use by Gigi.
6717
 
6718
      end if;
6719
 
6720
      Freeze_Stream_Operations (N, Def_Id);
6721
      return Result;
6722
 
6723
   exception
6724
      when RE_Not_Available =>
6725
         return False;
6726
   end Freeze_Type;
6727
 
6728
   -------------------------
6729
   -- Get_Simple_Init_Val --
6730
   -------------------------
6731
 
6732
   function Get_Simple_Init_Val
6733
     (T    : Entity_Id;
6734
      N    : Node_Id;
6735
      Size : Uint := No_Uint) return Node_Id
6736
   is
6737
      Loc    : constant Source_Ptr := Sloc (N);
6738
      Val    : Node_Id;
6739
      Result : Node_Id;
6740
      Val_RE : RE_Id;
6741
 
6742
      Size_To_Use : Uint;
6743
      --  This is the size to be used for computation of the appropriate
6744
      --  initial value for the Normalize_Scalars and Initialize_Scalars case.
6745
 
6746
      IV_Attribute : constant Boolean :=
6747
                       Nkind (N) = N_Attribute_Reference
6748
                         and then Attribute_Name (N) = Name_Invalid_Value;
6749
 
6750
      Lo_Bound : Uint;
6751
      Hi_Bound : Uint;
6752
      --  These are the values computed by the procedure Check_Subtype_Bounds
6753
 
6754
      procedure Check_Subtype_Bounds;
6755
      --  This procedure examines the subtype T, and its ancestor subtypes and
6756
      --  derived types to determine the best known information about the
6757
      --  bounds of the subtype. After the call Lo_Bound is set either to
6758
      --  No_Uint if no information can be determined, or to a value which
6759
      --  represents a known low bound, i.e. a valid value of the subtype can
6760
      --  not be less than this value. Hi_Bound is similarly set to a known
6761
      --  high bound (valid value cannot be greater than this).
6762
 
6763
      --------------------------
6764
      -- Check_Subtype_Bounds --
6765
      --------------------------
6766
 
6767
      procedure Check_Subtype_Bounds is
6768
         ST1  : Entity_Id;
6769
         ST2  : Entity_Id;
6770
         Lo   : Node_Id;
6771
         Hi   : Node_Id;
6772
         Loval : Uint;
6773
         Hival : Uint;
6774
 
6775
      begin
6776
         Lo_Bound := No_Uint;
6777
         Hi_Bound := No_Uint;
6778
 
6779
         --  Loop to climb ancestor subtypes and derived types
6780
 
6781
         ST1 := T;
6782
         loop
6783
            if not Is_Discrete_Type (ST1) then
6784
               return;
6785
            end if;
6786
 
6787
            Lo := Type_Low_Bound (ST1);
6788
            Hi := Type_High_Bound (ST1);
6789
 
6790
            if Compile_Time_Known_Value (Lo) then
6791
               Loval := Expr_Value (Lo);
6792
 
6793
               if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6794
                  Lo_Bound := Loval;
6795
               end if;
6796
            end if;
6797
 
6798
            if Compile_Time_Known_Value (Hi) then
6799
               Hival := Expr_Value (Hi);
6800
 
6801
               if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6802
                  Hi_Bound := Hival;
6803
               end if;
6804
            end if;
6805
 
6806
            ST2 := Ancestor_Subtype (ST1);
6807
 
6808
            if No (ST2) then
6809
               ST2 := Etype (ST1);
6810
            end if;
6811
 
6812
            exit when ST1 = ST2;
6813
            ST1 := ST2;
6814
         end loop;
6815
      end Check_Subtype_Bounds;
6816
 
6817
   --  Start of processing for Get_Simple_Init_Val
6818
 
6819
   begin
6820
      --  For a private type, we should always have an underlying type
6821
      --  (because this was already checked in Needs_Simple_Initialization).
6822
      --  What we do is to get the value for the underlying type and then do
6823
      --  an Unchecked_Convert to the private type.
6824
 
6825
      if Is_Private_Type (T) then
6826
         Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6827
 
6828
         --  A special case, if the underlying value is null, then qualify it
6829
         --  with the underlying type, so that the null is properly typed
6830
         --  Similarly, if it is an aggregate it must be qualified, because an
6831
         --  unchecked conversion does not provide a context for it.
6832
 
6833
         if Nkind_In (Val, N_Null, N_Aggregate) then
6834
            Val :=
6835
              Make_Qualified_Expression (Loc,
6836
                Subtype_Mark =>
6837
                  New_Occurrence_Of (Underlying_Type (T), Loc),
6838
                Expression => Val);
6839
         end if;
6840
 
6841
         Result := Unchecked_Convert_To (T, Val);
6842
 
6843
         --  Don't truncate result (important for Initialize/Normalize_Scalars)
6844
 
6845
         if Nkind (Result) = N_Unchecked_Type_Conversion
6846
           and then Is_Scalar_Type (Underlying_Type (T))
6847
         then
6848
            Set_No_Truncation (Result);
6849
         end if;
6850
 
6851
         return Result;
6852
 
6853
      --  Scalars with Default_Value aspect. The first subtype may now be
6854
      --   private, so retrieve value from underlying type.
6855
 
6856
      elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
6857
         if Is_Private_Type (First_Subtype (T)) then
6858
            return Unchecked_Convert_To (T,
6859
              Default_Aspect_Value (Full_View (First_Subtype (T))));
6860
         else
6861
            return
6862
              Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
6863
         end if;
6864
 
6865
      --  Otherwise, for scalars, we must have normalize/initialize scalars
6866
      --  case, or if the node N is an 'Invalid_Value attribute node.
6867
 
6868
      elsif Is_Scalar_Type (T) then
6869
         pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6870
 
6871
         --  Compute size of object. If it is given by the caller, we can use
6872
         --  it directly, otherwise we use Esize (T) as an estimate. As far as
6873
         --  we know this covers all cases correctly.
6874
 
6875
         if Size = No_Uint or else Size <= Uint_0 then
6876
            Size_To_Use := UI_Max (Uint_1, Esize (T));
6877
         else
6878
            Size_To_Use := Size;
6879
         end if;
6880
 
6881
         --  Maximum size to use is 64 bits, since we will create values of
6882
         --  type Unsigned_64 and the range must fit this type.
6883
 
6884
         if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6885
            Size_To_Use := Uint_64;
6886
         end if;
6887
 
6888
         --  Check known bounds of subtype
6889
 
6890
         Check_Subtype_Bounds;
6891
 
6892
         --  Processing for Normalize_Scalars case
6893
 
6894
         if Normalize_Scalars and then not IV_Attribute then
6895
 
6896
            --  If zero is invalid, it is a convenient value to use that is
6897
            --  for sure an appropriate invalid value in all situations.
6898
 
6899
            if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6900
               Val := Make_Integer_Literal (Loc, 0);
6901
 
6902
            --  Cases where all one bits is the appropriate invalid value
6903
 
6904
            --  For modular types, all 1 bits is either invalid or valid. If
6905
            --  it is valid, then there is nothing that can be done since there
6906
            --  are no invalid values (we ruled out zero already).
6907
 
6908
            --  For signed integer types that have no negative values, either
6909
            --  there is room for negative values, or there is not. If there
6910
            --  is, then all 1-bits may be interpreted as minus one, which is
6911
            --  certainly invalid. Alternatively it is treated as the largest
6912
            --  positive value, in which case the observation for modular types
6913
            --  still applies.
6914
 
6915
            --  For float types, all 1-bits is a NaN (not a number), which is
6916
            --  certainly an appropriately invalid value.
6917
 
6918
            elsif Is_Unsigned_Type (T)
6919
              or else Is_Floating_Point_Type (T)
6920
              or else Is_Enumeration_Type (T)
6921
            then
6922
               Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6923
 
6924
               --  Resolve as Unsigned_64, because the largest number we can
6925
               --  generate is out of range of universal integer.
6926
 
6927
               Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6928
 
6929
            --  Case of signed types
6930
 
6931
            else
6932
               declare
6933
                  Signed_Size : constant Uint :=
6934
                                  UI_Min (Uint_63, Size_To_Use - 1);
6935
 
6936
               begin
6937
                  --  Normally we like to use the most negative number. The one
6938
                  --  exception is when this number is in the known subtype
6939
                  --  range and the largest positive number is not in the known
6940
                  --  subtype range.
6941
 
6942
                  --  For this exceptional case, use largest positive value
6943
 
6944
                  if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6945
                    and then Lo_Bound <= (-(2 ** Signed_Size))
6946
                    and then Hi_Bound < 2 ** Signed_Size
6947
                  then
6948
                     Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6949
 
6950
                  --  Normal case of largest negative value
6951
 
6952
                  else
6953
                     Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6954
                  end if;
6955
               end;
6956
            end if;
6957
 
6958
         --  Here for Initialize_Scalars case (or Invalid_Value attribute used)
6959
 
6960
         else
6961
            --  For float types, use float values from System.Scalar_Values
6962
 
6963
            if Is_Floating_Point_Type (T) then
6964
               if Root_Type (T) = Standard_Short_Float then
6965
                  Val_RE := RE_IS_Isf;
6966
               elsif Root_Type (T) = Standard_Float then
6967
                  Val_RE := RE_IS_Ifl;
6968
               elsif Root_Type (T) = Standard_Long_Float then
6969
                  Val_RE := RE_IS_Ilf;
6970
               else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6971
                  Val_RE := RE_IS_Ill;
6972
               end if;
6973
 
6974
            --  If zero is invalid, use zero values from System.Scalar_Values
6975
 
6976
            elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6977
               if Size_To_Use <= 8 then
6978
                  Val_RE := RE_IS_Iz1;
6979
               elsif Size_To_Use <= 16 then
6980
                  Val_RE := RE_IS_Iz2;
6981
               elsif Size_To_Use <= 32 then
6982
                  Val_RE := RE_IS_Iz4;
6983
               else
6984
                  Val_RE := RE_IS_Iz8;
6985
               end if;
6986
 
6987
            --  For unsigned, use unsigned values from System.Scalar_Values
6988
 
6989
            elsif Is_Unsigned_Type (T) then
6990
               if Size_To_Use <= 8 then
6991
                  Val_RE := RE_IS_Iu1;
6992
               elsif Size_To_Use <= 16 then
6993
                  Val_RE := RE_IS_Iu2;
6994
               elsif Size_To_Use <= 32 then
6995
                  Val_RE := RE_IS_Iu4;
6996
               else
6997
                  Val_RE := RE_IS_Iu8;
6998
               end if;
6999
 
7000
            --  For signed, use signed values from System.Scalar_Values
7001
 
7002
            else
7003
               if Size_To_Use <= 8 then
7004
                  Val_RE := RE_IS_Is1;
7005
               elsif Size_To_Use <= 16 then
7006
                  Val_RE := RE_IS_Is2;
7007
               elsif Size_To_Use <= 32 then
7008
                  Val_RE := RE_IS_Is4;
7009
               else
7010
                  Val_RE := RE_IS_Is8;
7011
               end if;
7012
            end if;
7013
 
7014
            Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7015
         end if;
7016
 
7017
         --  The final expression is obtained by doing an unchecked conversion
7018
         --  of this result to the base type of the required subtype. We use
7019
         --  the base type to prevent the unchecked conversion from chopping
7020
         --  bits, and then we set Kill_Range_Check to preserve the "bad"
7021
         --  value.
7022
 
7023
         Result := Unchecked_Convert_To (Base_Type (T), Val);
7024
 
7025
         --  Ensure result is not truncated, since we want the "bad" bits, and
7026
         --  also kill range check on result.
7027
 
7028
         if Nkind (Result) = N_Unchecked_Type_Conversion then
7029
            Set_No_Truncation (Result);
7030
            Set_Kill_Range_Check (Result, True);
7031
         end if;
7032
 
7033
         return Result;
7034
 
7035
      --  String or Wide_[Wide]_String (must have Initialize_Scalars set)
7036
 
7037
      elsif Root_Type (T) = Standard_String
7038
              or else
7039
            Root_Type (T) = Standard_Wide_String
7040
              or else
7041
            Root_Type (T) = Standard_Wide_Wide_String
7042
      then
7043
         pragma Assert (Init_Or_Norm_Scalars);
7044
 
7045
         return
7046
           Make_Aggregate (Loc,
7047
             Component_Associations => New_List (
7048
               Make_Component_Association (Loc,
7049
                 Choices => New_List (
7050
                   Make_Others_Choice (Loc)),
7051
                 Expression =>
7052
                   Get_Simple_Init_Val
7053
                     (Component_Type (T), N, Esize (Root_Type (T))))));
7054
 
7055
      --  Access type is initialized to null
7056
 
7057
      elsif Is_Access_Type (T) then
7058
         return Make_Null (Loc);
7059
 
7060
      --  No other possibilities should arise, since we should only be calling
7061
      --  Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7062
      --  indicating one of the above cases held.
7063
 
7064
      else
7065
         raise Program_Error;
7066
      end if;
7067
 
7068
   exception
7069
      when RE_Not_Available =>
7070
         return Empty;
7071
   end Get_Simple_Init_Val;
7072
 
7073
   ------------------------------
7074
   -- Has_New_Non_Standard_Rep --
7075
   ------------------------------
7076
 
7077
   function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7078
   begin
7079
      if not Is_Derived_Type (T) then
7080
         return Has_Non_Standard_Rep (T)
7081
           or else Has_Non_Standard_Rep (Root_Type (T));
7082
 
7083
      --  If Has_Non_Standard_Rep is not set on the derived type, the
7084
      --  representation is fully inherited.
7085
 
7086
      elsif not Has_Non_Standard_Rep (T) then
7087
         return False;
7088
 
7089
      else
7090
         return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7091
 
7092
         --  May need a more precise check here: the First_Rep_Item may
7093
         --  be a stream attribute, which does not affect the representation
7094
         --  of the type ???
7095
      end if;
7096
   end Has_New_Non_Standard_Rep;
7097
 
7098
   ----------------
7099
   -- In_Runtime --
7100
   ----------------
7101
 
7102
   function In_Runtime (E : Entity_Id) return Boolean is
7103
      S1 : Entity_Id;
7104
 
7105
   begin
7106
      S1 := Scope (E);
7107
      while Scope (S1) /= Standard_Standard loop
7108
         S1 := Scope (S1);
7109
      end loop;
7110
 
7111
      return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7112
   end In_Runtime;
7113
 
7114
   ----------------------------
7115
   -- Initialization_Warning --
7116
   ----------------------------
7117
 
7118
   procedure Initialization_Warning (E : Entity_Id) is
7119
      Warning_Needed : Boolean;
7120
 
7121
   begin
7122
      Warning_Needed := False;
7123
 
7124
      if Ekind (Current_Scope) = E_Package
7125
        and then Static_Elaboration_Desired (Current_Scope)
7126
      then
7127
         if Is_Type (E) then
7128
            if Is_Record_Type (E) then
7129
               if Has_Discriminants (E)
7130
                 or else Is_Limited_Type (E)
7131
                 or else Has_Non_Standard_Rep (E)
7132
               then
7133
                  Warning_Needed := True;
7134
 
7135
               else
7136
                  --  Verify that at least one component has an initialization
7137
                  --  expression. No need for a warning on a type if all its
7138
                  --  components have no initialization.
7139
 
7140
                  declare
7141
                     Comp : Entity_Id;
7142
 
7143
                  begin
7144
                     Comp := First_Component (E);
7145
                     while Present (Comp) loop
7146
                        if Ekind (Comp) = E_Discriminant
7147
                          or else
7148
                            (Nkind (Parent (Comp)) = N_Component_Declaration
7149
                               and then Present (Expression (Parent (Comp))))
7150
                        then
7151
                           Warning_Needed := True;
7152
                           exit;
7153
                        end if;
7154
 
7155
                        Next_Component (Comp);
7156
                     end loop;
7157
                  end;
7158
               end if;
7159
 
7160
               if Warning_Needed then
7161
                  Error_Msg_N
7162
                    ("Objects of the type cannot be initialized " &
7163
                       "statically by default?",
7164
                       Parent (E));
7165
               end if;
7166
            end if;
7167
 
7168
         else
7169
            Error_Msg_N ("Object cannot be initialized statically?", E);
7170
         end if;
7171
      end if;
7172
   end Initialization_Warning;
7173
 
7174
   ------------------
7175
   -- Init_Formals --
7176
   ------------------
7177
 
7178
   function Init_Formals (Typ : Entity_Id) return List_Id is
7179
      Loc     : constant Source_Ptr := Sloc (Typ);
7180
      Formals : List_Id;
7181
 
7182
   begin
7183
      --  First parameter is always _Init : in out typ. Note that we need
7184
      --  this to be in/out because in the case of the task record value,
7185
      --  there are default record fields (_Priority, _Size, -Task_Info)
7186
      --  that may be referenced in the generated initialization routine.
7187
 
7188
      Formals := New_List (
7189
        Make_Parameter_Specification (Loc,
7190
          Defining_Identifier =>
7191
            Make_Defining_Identifier (Loc, Name_uInit),
7192
          In_Present  => True,
7193
          Out_Present => True,
7194
          Parameter_Type => New_Reference_To (Typ, Loc)));
7195
 
7196
      --  For task record value, or type that contains tasks, add two more
7197
      --  formals, _Master : Master_Id and _Chain : in out Activation_Chain
7198
      --  We also add these parameters for the task record type case.
7199
 
7200
      if Has_Task (Typ)
7201
        or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7202
      then
7203
         Append_To (Formals,
7204
           Make_Parameter_Specification (Loc,
7205
             Defining_Identifier =>
7206
               Make_Defining_Identifier (Loc, Name_uMaster),
7207
             Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
7208
 
7209
         Append_To (Formals,
7210
           Make_Parameter_Specification (Loc,
7211
             Defining_Identifier =>
7212
               Make_Defining_Identifier (Loc, Name_uChain),
7213
             In_Present => True,
7214
             Out_Present => True,
7215
             Parameter_Type =>
7216
               New_Reference_To (RTE (RE_Activation_Chain), Loc)));
7217
 
7218
         Append_To (Formals,
7219
           Make_Parameter_Specification (Loc,
7220
             Defining_Identifier =>
7221
               Make_Defining_Identifier (Loc, Name_uTask_Name),
7222
             In_Present => True,
7223
             Parameter_Type =>
7224
               New_Reference_To (Standard_String, Loc)));
7225
      end if;
7226
 
7227
      return Formals;
7228
 
7229
   exception
7230
      when RE_Not_Available =>
7231
         return Empty_List;
7232
   end Init_Formals;
7233
 
7234
   -------------------------
7235
   -- Init_Secondary_Tags --
7236
   -------------------------
7237
 
7238
   procedure Init_Secondary_Tags
7239
     (Typ            : Entity_Id;
7240
      Target         : Node_Id;
7241
      Stmts_List     : List_Id;
7242
      Fixed_Comps    : Boolean := True;
7243
      Variable_Comps : Boolean := True)
7244
   is
7245
      Loc : constant Source_Ptr := Sloc (Target);
7246
 
7247
      --  Inherit the C++ tag of the secondary dispatch table of Typ associated
7248
      --  with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7249
 
7250
      procedure Initialize_Tag
7251
        (Typ       : Entity_Id;
7252
         Iface     : Entity_Id;
7253
         Tag_Comp  : Entity_Id;
7254
         Iface_Tag : Node_Id);
7255
      --  Initialize the tag of the secondary dispatch table of Typ associated
7256
      --  with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7257
      --  Compiling under the CPP full ABI compatibility mode, if the ancestor
7258
      --  of Typ CPP tagged type we generate code to inherit the contents of
7259
      --  the dispatch table directly from the ancestor.
7260
 
7261
      --------------------
7262
      -- Initialize_Tag --
7263
      --------------------
7264
 
7265
      procedure Initialize_Tag
7266
        (Typ       : Entity_Id;
7267
         Iface     : Entity_Id;
7268
         Tag_Comp  : Entity_Id;
7269
         Iface_Tag : Node_Id)
7270
      is
7271
         Comp_Typ           : Entity_Id;
7272
         Offset_To_Top_Comp : Entity_Id := Empty;
7273
 
7274
      begin
7275
         --  Initialize the pointer to the secondary DT associated with the
7276
         --  interface.
7277
 
7278
         if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7279
            Append_To (Stmts_List,
7280
              Make_Assignment_Statement (Loc,
7281
                Name =>
7282
                  Make_Selected_Component (Loc,
7283
                    Prefix => New_Copy_Tree (Target),
7284
                    Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7285
                Expression =>
7286
                  New_Reference_To (Iface_Tag, Loc)));
7287
         end if;
7288
 
7289
         Comp_Typ := Scope (Tag_Comp);
7290
 
7291
         --  Initialize the entries of the table of interfaces. We generate a
7292
         --  different call when the parent of the type has variable size
7293
         --  components.
7294
 
7295
         if Comp_Typ /= Etype (Comp_Typ)
7296
           and then Is_Variable_Size_Record (Etype (Comp_Typ))
7297
           and then Chars (Tag_Comp) /= Name_uTag
7298
         then
7299
            pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7300
 
7301
            --  Issue error if Set_Dynamic_Offset_To_Top is not available in a
7302
            --  configurable run-time environment.
7303
 
7304
            if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7305
               Error_Msg_CRT
7306
                 ("variable size record with interface types", Typ);
7307
               return;
7308
            end if;
7309
 
7310
            --  Generate:
7311
            --    Set_Dynamic_Offset_To_Top
7312
            --      (This         => Init,
7313
            --       Interface_T  => Iface'Tag,
7314
            --       Offset_Value => n,
7315
            --       Offset_Func  => Fn'Address)
7316
 
7317
            Append_To (Stmts_List,
7318
              Make_Procedure_Call_Statement (Loc,
7319
                Name => New_Reference_To
7320
                          (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7321
                Parameter_Associations => New_List (
7322
                  Make_Attribute_Reference (Loc,
7323
                    Prefix => New_Copy_Tree (Target),
7324
                    Attribute_Name => Name_Address),
7325
 
7326
                  Unchecked_Convert_To (RTE (RE_Tag),
7327
                    New_Reference_To
7328
                      (Node (First_Elmt (Access_Disp_Table (Iface))),
7329
                       Loc)),
7330
 
7331
                  Unchecked_Convert_To
7332
                    (RTE (RE_Storage_Offset),
7333
                     Make_Attribute_Reference (Loc,
7334
                       Prefix         =>
7335
                         Make_Selected_Component (Loc,
7336
                           Prefix => New_Copy_Tree (Target),
7337
                           Selector_Name =>
7338
                             New_Reference_To (Tag_Comp, Loc)),
7339
                       Attribute_Name => Name_Position)),
7340
 
7341
                  Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7342
                    Make_Attribute_Reference (Loc,
7343
                      Prefix => New_Reference_To
7344
                                  (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7345
                      Attribute_Name => Name_Address)))));
7346
 
7347
            --  In this case the next component stores the value of the
7348
            --  offset to the top.
7349
 
7350
            Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7351
            pragma Assert (Present (Offset_To_Top_Comp));
7352
 
7353
            Append_To (Stmts_List,
7354
              Make_Assignment_Statement (Loc,
7355
                Name =>
7356
                  Make_Selected_Component (Loc,
7357
                    Prefix => New_Copy_Tree (Target),
7358
                    Selector_Name => New_Reference_To
7359
                                       (Offset_To_Top_Comp, Loc)),
7360
                Expression =>
7361
                  Make_Attribute_Reference (Loc,
7362
                    Prefix         =>
7363
                      Make_Selected_Component (Loc,
7364
                        Prefix => New_Copy_Tree (Target),
7365
                        Selector_Name =>
7366
                          New_Reference_To (Tag_Comp, Loc)),
7367
                  Attribute_Name => Name_Position)));
7368
 
7369
         --  Normal case: No discriminants in the parent type
7370
 
7371
         else
7372
            --  Don't need to set any value if this interface shares
7373
            --  the primary dispatch table.
7374
 
7375
            if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7376
               Append_To (Stmts_List,
7377
                 Build_Set_Static_Offset_To_Top (Loc,
7378
                   Iface_Tag    => New_Reference_To (Iface_Tag, Loc),
7379
                   Offset_Value =>
7380
                     Unchecked_Convert_To (RTE (RE_Storage_Offset),
7381
                       Make_Attribute_Reference (Loc,
7382
                         Prefix =>
7383
                           Make_Selected_Component (Loc,
7384
                             Prefix        => New_Copy_Tree (Target),
7385
                             Selector_Name =>
7386
                               New_Reference_To (Tag_Comp, Loc)),
7387
                         Attribute_Name => Name_Position))));
7388
            end if;
7389
 
7390
            --  Generate:
7391
            --    Register_Interface_Offset
7392
            --      (This         => Init,
7393
            --       Interface_T  => Iface'Tag,
7394
            --       Is_Constant  => True,
7395
            --       Offset_Value => n,
7396
            --       Offset_Func  => null);
7397
 
7398
            if RTE_Available (RE_Register_Interface_Offset) then
7399
               Append_To (Stmts_List,
7400
                 Make_Procedure_Call_Statement (Loc,
7401
                   Name => New_Reference_To
7402
                             (RTE (RE_Register_Interface_Offset), Loc),
7403
                   Parameter_Associations => New_List (
7404
                     Make_Attribute_Reference (Loc,
7405
                       Prefix         => New_Copy_Tree (Target),
7406
                       Attribute_Name => Name_Address),
7407
 
7408
                     Unchecked_Convert_To (RTE (RE_Tag),
7409
                       New_Reference_To
7410
                         (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
7411
 
7412
                     New_Occurrence_Of (Standard_True, Loc),
7413
 
7414
                     Unchecked_Convert_To
7415
                       (RTE (RE_Storage_Offset),
7416
                        Make_Attribute_Reference (Loc,
7417
                          Prefix =>
7418
                            Make_Selected_Component (Loc,
7419
                              Prefix         => New_Copy_Tree (Target),
7420
                              Selector_Name  =>
7421
                                New_Reference_To (Tag_Comp, Loc)),
7422
                         Attribute_Name => Name_Position)),
7423
 
7424
                     Make_Null (Loc))));
7425
            end if;
7426
         end if;
7427
      end Initialize_Tag;
7428
 
7429
      --  Local variables
7430
 
7431
      Full_Typ         : Entity_Id;
7432
      Ifaces_List      : Elist_Id;
7433
      Ifaces_Comp_List : Elist_Id;
7434
      Ifaces_Tag_List  : Elist_Id;
7435
      Iface_Elmt       : Elmt_Id;
7436
      Iface_Comp_Elmt  : Elmt_Id;
7437
      Iface_Tag_Elmt   : Elmt_Id;
7438
      Tag_Comp         : Node_Id;
7439
      In_Variable_Pos  : Boolean;
7440
 
7441
   --  Start of processing for Init_Secondary_Tags
7442
 
7443
   begin
7444
      --  Handle private types
7445
 
7446
      if Present (Full_View (Typ)) then
7447
         Full_Typ := Full_View (Typ);
7448
      else
7449
         Full_Typ := Typ;
7450
      end if;
7451
 
7452
      Collect_Interfaces_Info
7453
        (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
7454
 
7455
      Iface_Elmt      := First_Elmt (Ifaces_List);
7456
      Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
7457
      Iface_Tag_Elmt  := First_Elmt (Ifaces_Tag_List);
7458
      while Present (Iface_Elmt) loop
7459
         Tag_Comp := Node (Iface_Comp_Elmt);
7460
 
7461
         --  Check if parent of record type has variable size components
7462
 
7463
         In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
7464
           and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
7465
 
7466
         --  If we are compiling under the CPP full ABI compatibility mode and
7467
         --  the ancestor is a CPP_Pragma tagged type then we generate code to
7468
         --  initialize the secondary tag components from tags that reference
7469
         --  secondary tables filled with copy of parent slots.
7470
 
7471
         if Is_CPP_Class (Root_Type (Full_Typ)) then
7472
 
7473
            --  Reject interface components located at variable offset in
7474
            --  C++ derivations. This is currently unsupported.
7475
 
7476
            if not Fixed_Comps and then In_Variable_Pos then
7477
 
7478
               --  Locate the first dynamic component of the record. Done to
7479
               --  improve the text of the warning.
7480
 
7481
               declare
7482
                  Comp     : Entity_Id;
7483
                  Comp_Typ : Entity_Id;
7484
 
7485
               begin
7486
                  Comp := First_Entity (Typ);
7487
                  while Present (Comp) loop
7488
                     Comp_Typ := Etype (Comp);
7489
 
7490
                     if Ekind (Comp) /= E_Discriminant
7491
                       and then not Is_Tag (Comp)
7492
                     then
7493
                        exit when
7494
                          (Is_Record_Type (Comp_Typ)
7495
                             and then Is_Variable_Size_Record
7496
                                        (Base_Type (Comp_Typ)))
7497
                         or else
7498
                           (Is_Array_Type (Comp_Typ)
7499
                              and then Is_Variable_Size_Array (Comp_Typ));
7500
                     end if;
7501
 
7502
                     Next_Entity (Comp);
7503
                  end loop;
7504
 
7505
                  pragma Assert (Present (Comp));
7506
                  Error_Msg_Node_2 := Comp;
7507
                  Error_Msg_NE
7508
                    ("parent type & with dynamic component & cannot be parent"
7509
                       & " of 'C'P'P derivation if new interfaces are present",
7510
                     Typ, Scope (Original_Record_Component (Comp)));
7511
 
7512
                  Error_Msg_Sloc :=
7513
                    Sloc (Scope (Original_Record_Component (Comp)));
7514
                  Error_Msg_NE
7515
                    ("type derived from 'C'P'P type & defined #",
7516
                     Typ, Scope (Original_Record_Component (Comp)));
7517
 
7518
                  --  Avoid duplicated warnings
7519
 
7520
                  exit;
7521
               end;
7522
 
7523
            --  Initialize secondary tags
7524
 
7525
            else
7526
               Append_To (Stmts_List,
7527
                 Make_Assignment_Statement (Loc,
7528
                   Name =>
7529
                     Make_Selected_Component (Loc,
7530
                       Prefix => New_Copy_Tree (Target),
7531
                       Selector_Name =>
7532
                         New_Reference_To (Node (Iface_Comp_Elmt), Loc)),
7533
                   Expression =>
7534
                     New_Reference_To (Node (Iface_Tag_Elmt), Loc)));
7535
            end if;
7536
 
7537
         --  Otherwise generate code to initialize the tag
7538
 
7539
         else
7540
            if (In_Variable_Pos and then Variable_Comps)
7541
              or else (not In_Variable_Pos and then Fixed_Comps)
7542
            then
7543
               Initialize_Tag (Full_Typ,
7544
                 Iface     => Node (Iface_Elmt),
7545
                 Tag_Comp  => Tag_Comp,
7546
                 Iface_Tag => Node (Iface_Tag_Elmt));
7547
            end if;
7548
         end if;
7549
 
7550
         Next_Elmt (Iface_Elmt);
7551
         Next_Elmt (Iface_Comp_Elmt);
7552
         Next_Elmt (Iface_Tag_Elmt);
7553
      end loop;
7554
   end Init_Secondary_Tags;
7555
 
7556
   ----------------------------
7557
   -- Is_Variable_Size_Array --
7558
   ----------------------------
7559
 
7560
   function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
7561
      Idx : Node_Id;
7562
 
7563
   begin
7564
      pragma Assert (Is_Array_Type (E));
7565
 
7566
      --  Check if some index is initialized with a non-constant value
7567
 
7568
      Idx := First_Index (E);
7569
      while Present (Idx) loop
7570
         if Nkind (Idx) = N_Range then
7571
            if not Is_Constant_Bound (Low_Bound (Idx))
7572
              or else not Is_Constant_Bound (High_Bound (Idx))
7573
            then
7574
               return True;
7575
            end if;
7576
         end if;
7577
 
7578
         Idx := Next_Index (Idx);
7579
      end loop;
7580
 
7581
      return False;
7582
   end Is_Variable_Size_Array;
7583
 
7584
   -----------------------------
7585
   -- Is_Variable_Size_Record --
7586
   -----------------------------
7587
 
7588
   function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7589
      Comp     : Entity_Id;
7590
      Comp_Typ : Entity_Id;
7591
 
7592
   begin
7593
      pragma Assert (Is_Record_Type (E));
7594
 
7595
      Comp := First_Entity (E);
7596
      while Present (Comp) loop
7597
         Comp_Typ := Etype (Comp);
7598
 
7599
         --  Recursive call if the record type has discriminants
7600
 
7601
         if Is_Record_Type (Comp_Typ)
7602
           and then Has_Discriminants (Comp_Typ)
7603
           and then Is_Variable_Size_Record (Comp_Typ)
7604
         then
7605
            return True;
7606
 
7607
         elsif Is_Array_Type (Comp_Typ)
7608
           and then Is_Variable_Size_Array (Comp_Typ)
7609
         then
7610
            return True;
7611
         end if;
7612
 
7613
         Next_Entity (Comp);
7614
      end loop;
7615
 
7616
      return False;
7617
   end Is_Variable_Size_Record;
7618
 
7619
   ----------------------------------------
7620
   -- Make_Controlling_Function_Wrappers --
7621
   ----------------------------------------
7622
 
7623
   procedure Make_Controlling_Function_Wrappers
7624
     (Tag_Typ   : Entity_Id;
7625
      Decl_List : out List_Id;
7626
      Body_List : out List_Id)
7627
   is
7628
      Loc         : constant Source_Ptr := Sloc (Tag_Typ);
7629
      Prim_Elmt   : Elmt_Id;
7630
      Subp        : Entity_Id;
7631
      Actual_List : List_Id;
7632
      Formal_List : List_Id;
7633
      Formal      : Entity_Id;
7634
      Par_Formal  : Entity_Id;
7635
      Formal_Node : Node_Id;
7636
      Func_Body   : Node_Id;
7637
      Func_Decl   : Node_Id;
7638
      Func_Spec   : Node_Id;
7639
      Return_Stmt : Node_Id;
7640
 
7641
   begin
7642
      Decl_List := New_List;
7643
      Body_List := New_List;
7644
 
7645
      Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7646
 
7647
      while Present (Prim_Elmt) loop
7648
         Subp := Node (Prim_Elmt);
7649
 
7650
         --  If a primitive function with a controlling result of the type has
7651
         --  not been overridden by the user, then we must create a wrapper
7652
         --  function here that effectively overrides it and invokes the
7653
         --  (non-abstract) parent function. This can only occur for a null
7654
         --  extension. Note that functions with anonymous controlling access
7655
         --  results don't qualify and must be overridden. We also exclude
7656
         --  Input attributes, since each type will have its own version of
7657
         --  Input constructed by the expander. The test for Comes_From_Source
7658
         --  is needed to distinguish inherited operations from renamings
7659
         --  (which also have Alias set).
7660
 
7661
         --  The function may be abstract, or require_Overriding may be set
7662
         --  for it, because tests for null extensions may already have reset
7663
         --  the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7664
         --  set, functions that need wrappers are recognized by having an
7665
         --  alias that returns the parent type.
7666
 
7667
         if Comes_From_Source (Subp)
7668
           or else No (Alias (Subp))
7669
           or else Ekind (Subp) /= E_Function
7670
           or else not Has_Controlling_Result (Subp)
7671
           or else Is_Access_Type (Etype (Subp))
7672
           or else Is_Abstract_Subprogram (Alias (Subp))
7673
           or else Is_TSS (Subp, TSS_Stream_Input)
7674
         then
7675
            goto Next_Prim;
7676
 
7677
         elsif Is_Abstract_Subprogram (Subp)
7678
           or else Requires_Overriding (Subp)
7679
           or else
7680
             (Is_Null_Extension (Etype (Subp))
7681
               and then Etype (Alias (Subp)) /= Etype (Subp))
7682
         then
7683
            Formal_List := No_List;
7684
            Formal := First_Formal (Subp);
7685
 
7686
            if Present (Formal) then
7687
               Formal_List := New_List;
7688
 
7689
               while Present (Formal) loop
7690
                  Append
7691
                    (Make_Parameter_Specification
7692
                       (Loc,
7693
                        Defining_Identifier =>
7694
                          Make_Defining_Identifier (Sloc (Formal),
7695
                            Chars => Chars (Formal)),
7696
                        In_Present  => In_Present (Parent (Formal)),
7697
                        Out_Present => Out_Present (Parent (Formal)),
7698
                        Null_Exclusion_Present =>
7699
                          Null_Exclusion_Present (Parent (Formal)),
7700
                        Parameter_Type =>
7701
                          New_Reference_To (Etype (Formal), Loc),
7702
                        Expression =>
7703
                          New_Copy_Tree (Expression (Parent (Formal)))),
7704
                     Formal_List);
7705
 
7706
                  Next_Formal (Formal);
7707
               end loop;
7708
            end if;
7709
 
7710
            Func_Spec :=
7711
              Make_Function_Specification (Loc,
7712
                Defining_Unit_Name       =>
7713
                  Make_Defining_Identifier (Loc,
7714
                    Chars => Chars (Subp)),
7715
                Parameter_Specifications => Formal_List,
7716
                Result_Definition        =>
7717
                  New_Reference_To (Etype (Subp), Loc));
7718
 
7719
            Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7720
            Append_To (Decl_List, Func_Decl);
7721
 
7722
            --  Build a wrapper body that calls the parent function. The body
7723
            --  contains a single return statement that returns an extension
7724
            --  aggregate whose ancestor part is a call to the parent function,
7725
            --  passing the formals as actuals (with any controlling arguments
7726
            --  converted to the types of the corresponding formals of the
7727
            --  parent function, which might be anonymous access types), and
7728
            --  having a null extension.
7729
 
7730
            Formal      := First_Formal (Subp);
7731
            Par_Formal  := First_Formal (Alias (Subp));
7732
            Formal_Node := First (Formal_List);
7733
 
7734
            if Present (Formal) then
7735
               Actual_List := New_List;
7736
            else
7737
               Actual_List := No_List;
7738
            end if;
7739
 
7740
            while Present (Formal) loop
7741
               if Is_Controlling_Formal (Formal) then
7742
                  Append_To (Actual_List,
7743
                    Make_Type_Conversion (Loc,
7744
                      Subtype_Mark =>
7745
                        New_Occurrence_Of (Etype (Par_Formal), Loc),
7746
                      Expression   =>
7747
                        New_Reference_To
7748
                          (Defining_Identifier (Formal_Node), Loc)));
7749
               else
7750
                  Append_To
7751
                    (Actual_List,
7752
                     New_Reference_To
7753
                       (Defining_Identifier (Formal_Node), Loc));
7754
               end if;
7755
 
7756
               Next_Formal (Formal);
7757
               Next_Formal (Par_Formal);
7758
               Next (Formal_Node);
7759
            end loop;
7760
 
7761
            Return_Stmt :=
7762
              Make_Simple_Return_Statement (Loc,
7763
                Expression =>
7764
                  Make_Extension_Aggregate (Loc,
7765
                    Ancestor_Part =>
7766
                      Make_Function_Call (Loc,
7767
                        Name => New_Reference_To (Alias (Subp), Loc),
7768
                        Parameter_Associations => Actual_List),
7769
                    Null_Record_Present => True));
7770
 
7771
            Func_Body :=
7772
              Make_Subprogram_Body (Loc,
7773
                Specification => New_Copy_Tree (Func_Spec),
7774
                Declarations => Empty_List,
7775
                Handled_Statement_Sequence =>
7776
                  Make_Handled_Sequence_Of_Statements (Loc,
7777
                    Statements => New_List (Return_Stmt)));
7778
 
7779
            Set_Defining_Unit_Name
7780
              (Specification (Func_Body),
7781
                Make_Defining_Identifier (Loc, Chars (Subp)));
7782
 
7783
            Append_To (Body_List, Func_Body);
7784
 
7785
            --  Replace the inherited function with the wrapper function
7786
            --  in the primitive operations list.
7787
 
7788
            Override_Dispatching_Operation
7789
              (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7790
         end if;
7791
 
7792
      <<Next_Prim>>
7793
         Next_Elmt (Prim_Elmt);
7794
      end loop;
7795
   end Make_Controlling_Function_Wrappers;
7796
 
7797
   -------------------
7798
   --  Make_Eq_Body --
7799
   -------------------
7800
 
7801
   function Make_Eq_Body
7802
     (Typ     : Entity_Id;
7803
      Eq_Name : Name_Id) return Node_Id
7804
   is
7805
      Loc          : constant Source_Ptr := Sloc (Parent (Typ));
7806
      Decl         : Node_Id;
7807
      Def          : constant Node_Id := Parent (Typ);
7808
      Stmts        : constant List_Id := New_List;
7809
      Variant_Case : Boolean := Has_Discriminants (Typ);
7810
      Comps        : Node_Id := Empty;
7811
      Typ_Def      : Node_Id := Type_Definition (Def);
7812
 
7813
   begin
7814
      Decl :=
7815
        Predef_Spec_Or_Body (Loc,
7816
          Tag_Typ => Typ,
7817
          Name    => Eq_Name,
7818
          Profile => New_List (
7819
            Make_Parameter_Specification (Loc,
7820
              Defining_Identifier =>
7821
                Make_Defining_Identifier (Loc, Name_X),
7822
              Parameter_Type      => New_Reference_To (Typ, Loc)),
7823
 
7824
            Make_Parameter_Specification (Loc,
7825
              Defining_Identifier =>
7826
                Make_Defining_Identifier (Loc, Name_Y),
7827
              Parameter_Type      => New_Reference_To (Typ, Loc))),
7828
 
7829
          Ret_Type => Standard_Boolean,
7830
          For_Body => True);
7831
 
7832
      if Variant_Case then
7833
         if Nkind (Typ_Def) = N_Derived_Type_Definition then
7834
            Typ_Def := Record_Extension_Part (Typ_Def);
7835
         end if;
7836
 
7837
         if Present (Typ_Def) then
7838
            Comps := Component_List (Typ_Def);
7839
         end if;
7840
 
7841
         Variant_Case :=
7842
           Present (Comps) and then Present (Variant_Part (Comps));
7843
      end if;
7844
 
7845
      if Variant_Case then
7846
         Append_To (Stmts,
7847
           Make_Eq_If (Typ, Discriminant_Specifications (Def)));
7848
         Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
7849
         Append_To (Stmts,
7850
           Make_Simple_Return_Statement (Loc,
7851
             Expression => New_Reference_To (Standard_True, Loc)));
7852
 
7853
      else
7854
         Append_To (Stmts,
7855
           Make_Simple_Return_Statement (Loc,
7856
             Expression =>
7857
               Expand_Record_Equality
7858
                 (Typ,
7859
                  Typ    => Typ,
7860
                  Lhs    => Make_Identifier (Loc, Name_X),
7861
                  Rhs    => Make_Identifier (Loc, Name_Y),
7862
                  Bodies => Declarations (Decl))));
7863
      end if;
7864
 
7865
      Set_Handled_Statement_Sequence
7866
        (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7867
      return Decl;
7868
   end Make_Eq_Body;
7869
 
7870
   ------------------
7871
   -- Make_Eq_Case --
7872
   ------------------
7873
 
7874
   --  <Make_Eq_If shared components>
7875
   --  case X.D1 is
7876
   --     when V1 => <Make_Eq_Case> on subcomponents
7877
   --     ...
7878
   --     when Vn => <Make_Eq_Case> on subcomponents
7879
   --  end case;
7880
 
7881
   function Make_Eq_Case
7882
     (E     : Entity_Id;
7883
      CL    : Node_Id;
7884
      Discr : Entity_Id := Empty) return List_Id
7885
   is
7886
      Loc      : constant Source_Ptr := Sloc (E);
7887
      Result   : constant List_Id    := New_List;
7888
      Variant  : Node_Id;
7889
      Alt_List : List_Id;
7890
 
7891
   begin
7892
      Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7893
 
7894
      if No (Variant_Part (CL)) then
7895
         return Result;
7896
      end if;
7897
 
7898
      Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7899
 
7900
      if No (Variant) then
7901
         return Result;
7902
      end if;
7903
 
7904
      Alt_List := New_List;
7905
 
7906
      while Present (Variant) loop
7907
         Append_To (Alt_List,
7908
           Make_Case_Statement_Alternative (Loc,
7909
             Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7910
             Statements => Make_Eq_Case (E, Component_List (Variant))));
7911
 
7912
         Next_Non_Pragma (Variant);
7913
      end loop;
7914
 
7915
      --  If we have an Unchecked_Union, use one of the parameters that
7916
      --  captures the discriminants.
7917
 
7918
      if Is_Unchecked_Union (E) then
7919
         Append_To (Result,
7920
           Make_Case_Statement (Loc,
7921
             Expression => New_Reference_To (Discr, Loc),
7922
             Alternatives => Alt_List));
7923
 
7924
      else
7925
         Append_To (Result,
7926
           Make_Case_Statement (Loc,
7927
             Expression =>
7928
               Make_Selected_Component (Loc,
7929
                 Prefix        => Make_Identifier (Loc, Name_X),
7930
                 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7931
             Alternatives => Alt_List));
7932
      end if;
7933
 
7934
      return Result;
7935
   end Make_Eq_Case;
7936
 
7937
   ----------------
7938
   -- Make_Eq_If --
7939
   ----------------
7940
 
7941
   --  Generates:
7942
 
7943
   --    if
7944
   --      X.C1 /= Y.C1
7945
   --        or else
7946
   --      X.C2 /= Y.C2
7947
   --        ...
7948
   --    then
7949
   --       return False;
7950
   --    end if;
7951
 
7952
   --  or a null statement if the list L is empty
7953
 
7954
   function Make_Eq_If
7955
     (E : Entity_Id;
7956
      L : List_Id) return Node_Id
7957
   is
7958
      Loc        : constant Source_Ptr := Sloc (E);
7959
      C          : Node_Id;
7960
      Field_Name : Name_Id;
7961
      Cond       : Node_Id;
7962
 
7963
   begin
7964
      if No (L) then
7965
         return Make_Null_Statement (Loc);
7966
 
7967
      else
7968
         Cond := Empty;
7969
 
7970
         C := First_Non_Pragma (L);
7971
         while Present (C) loop
7972
            Field_Name := Chars (Defining_Identifier (C));
7973
 
7974
            --  The tags must not be compared: they are not part of the value.
7975
            --  Ditto for parent interfaces because their equality operator is
7976
            --  abstract.
7977
 
7978
            --  Note also that in the following, we use Make_Identifier for
7979
            --  the component names. Use of New_Reference_To to identify the
7980
            --  components would be incorrect because the wrong entities for
7981
            --  discriminants could be picked up in the private type case.
7982
 
7983
            if Field_Name = Name_uParent
7984
              and then Is_Interface (Etype (Defining_Identifier (C)))
7985
            then
7986
               null;
7987
 
7988
            elsif Field_Name /= Name_uTag then
7989
               Evolve_Or_Else (Cond,
7990
                 Make_Op_Ne (Loc,
7991
                   Left_Opnd =>
7992
                     Make_Selected_Component (Loc,
7993
                       Prefix        => Make_Identifier (Loc, Name_X),
7994
                       Selector_Name => Make_Identifier (Loc, Field_Name)),
7995
 
7996
                   Right_Opnd =>
7997
                     Make_Selected_Component (Loc,
7998
                       Prefix        => Make_Identifier (Loc, Name_Y),
7999
                       Selector_Name => Make_Identifier (Loc, Field_Name))));
8000
            end if;
8001
 
8002
            Next_Non_Pragma (C);
8003
         end loop;
8004
 
8005
         if No (Cond) then
8006
            return Make_Null_Statement (Loc);
8007
 
8008
         else
8009
            return
8010
              Make_Implicit_If_Statement (E,
8011
                Condition => Cond,
8012
                Then_Statements => New_List (
8013
                  Make_Simple_Return_Statement (Loc,
8014
                    Expression => New_Occurrence_Of (Standard_False, Loc))));
8015
         end if;
8016
      end if;
8017
   end Make_Eq_If;
8018
 
8019
   -------------------------------
8020
   -- Make_Null_Procedure_Specs --
8021
   -------------------------------
8022
 
8023
   function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8024
      Decl_List      : constant List_Id    := New_List;
8025
      Loc            : constant Source_Ptr := Sloc (Tag_Typ);
8026
      Formal         : Entity_Id;
8027
      Formal_List    : List_Id;
8028
      New_Param_Spec : Node_Id;
8029
      Parent_Subp    : Entity_Id;
8030
      Prim_Elmt      : Elmt_Id;
8031
      Subp           : Entity_Id;
8032
 
8033
   begin
8034
      Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8035
      while Present (Prim_Elmt) loop
8036
         Subp := Node (Prim_Elmt);
8037
 
8038
         --  If a null procedure inherited from an interface has not been
8039
         --  overridden, then we build a null procedure declaration to
8040
         --  override the inherited procedure.
8041
 
8042
         Parent_Subp := Alias (Subp);
8043
 
8044
         if Present (Parent_Subp)
8045
           and then Is_Null_Interface_Primitive (Parent_Subp)
8046
         then
8047
            Formal_List := No_List;
8048
            Formal := First_Formal (Subp);
8049
 
8050
            if Present (Formal) then
8051
               Formal_List := New_List;
8052
 
8053
               while Present (Formal) loop
8054
 
8055
                  --  Copy the parameter spec including default expressions
8056
 
8057
                  New_Param_Spec :=
8058
                    New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
8059
 
8060
                  --  Generate a new defining identifier for the new formal.
8061
                  --  required because New_Copy_Tree does not duplicate
8062
                  --  semantic fields (except itypes).
8063
 
8064
                  Set_Defining_Identifier (New_Param_Spec,
8065
                    Make_Defining_Identifier (Sloc (Formal),
8066
                      Chars => Chars (Formal)));
8067
 
8068
                  --  For controlling arguments we must change their
8069
                  --  parameter type to reference the tagged type (instead
8070
                  --  of the interface type)
8071
 
8072
                  if Is_Controlling_Formal (Formal) then
8073
                     if Nkind (Parameter_Type (Parent (Formal)))
8074
                       = N_Identifier
8075
                     then
8076
                        Set_Parameter_Type (New_Param_Spec,
8077
                          New_Occurrence_Of (Tag_Typ, Loc));
8078
 
8079
                     else pragma Assert
8080
                            (Nkind (Parameter_Type (Parent (Formal)))
8081
                               = N_Access_Definition);
8082
                        Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
8083
                          New_Occurrence_Of (Tag_Typ, Loc));
8084
                     end if;
8085
                  end if;
8086
 
8087
                  Append (New_Param_Spec, Formal_List);
8088
 
8089
                  Next_Formal (Formal);
8090
               end loop;
8091
            end if;
8092
 
8093
            Append_To (Decl_List,
8094
              Make_Subprogram_Declaration (Loc,
8095
                Make_Procedure_Specification (Loc,
8096
                  Defining_Unit_Name =>
8097
                    Make_Defining_Identifier (Loc, Chars (Subp)),
8098
                  Parameter_Specifications => Formal_List,
8099
                  Null_Present => True)));
8100
         end if;
8101
 
8102
         Next_Elmt (Prim_Elmt);
8103
      end loop;
8104
 
8105
      return Decl_List;
8106
   end Make_Null_Procedure_Specs;
8107
 
8108
   -------------------------------------
8109
   -- Make_Predefined_Primitive_Specs --
8110
   -------------------------------------
8111
 
8112
   procedure Make_Predefined_Primitive_Specs
8113
     (Tag_Typ     : Entity_Id;
8114
      Predef_List : out List_Id;
8115
      Renamed_Eq  : out Entity_Id)
8116
   is
8117
      Loc       : constant Source_Ptr := Sloc (Tag_Typ);
8118
      Res       : constant List_Id    := New_List;
8119
      Eq_Name   : Name_Id := Name_Op_Eq;
8120
      Eq_Needed : Boolean;
8121
      Eq_Spec   : Node_Id;
8122
      Prim      : Elmt_Id;
8123
 
8124
      function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
8125
      --  Returns true if Prim is a renaming of an unresolved predefined
8126
      --  equality operation.
8127
 
8128
      -------------------------------
8129
      -- Is_Predefined_Eq_Renaming --
8130
      -------------------------------
8131
 
8132
      function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
8133
      begin
8134
         return Chars (Prim) /= Name_Op_Eq
8135
           and then Present (Alias (Prim))
8136
           and then Comes_From_Source (Prim)
8137
           and then Is_Intrinsic_Subprogram (Alias (Prim))
8138
           and then Chars (Alias (Prim)) = Name_Op_Eq;
8139
      end Is_Predefined_Eq_Renaming;
8140
 
8141
   --  Start of processing for Make_Predefined_Primitive_Specs
8142
 
8143
   begin
8144
      Renamed_Eq := Empty;
8145
 
8146
      --  Spec of _Size
8147
 
8148
      Append_To (Res, Predef_Spec_Or_Body (Loc,
8149
        Tag_Typ => Tag_Typ,
8150
        Name    => Name_uSize,
8151
        Profile => New_List (
8152
          Make_Parameter_Specification (Loc,
8153
            Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8154
            Parameter_Type      => New_Reference_To (Tag_Typ, Loc))),
8155
 
8156
        Ret_Type => Standard_Long_Long_Integer));
8157
 
8158
      --  Specs for dispatching stream attributes
8159
 
8160
      declare
8161
         Stream_Op_TSS_Names :
8162
           constant array (Integer range <>) of TSS_Name_Type :=
8163
             (TSS_Stream_Read,
8164
              TSS_Stream_Write,
8165
              TSS_Stream_Input,
8166
              TSS_Stream_Output);
8167
 
8168
      begin
8169
         for Op in Stream_Op_TSS_Names'Range loop
8170
            if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
8171
               Append_To (Res,
8172
                 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
8173
                  Stream_Op_TSS_Names (Op)));
8174
            end if;
8175
         end loop;
8176
      end;
8177
 
8178
      --  Spec of "=" is expanded if the type is not limited and if a
8179
      --  user defined "=" was not already declared for the non-full
8180
      --  view of a private extension
8181
 
8182
      if not Is_Limited_Type (Tag_Typ) then
8183
         Eq_Needed := True;
8184
         Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8185
         while Present (Prim) loop
8186
 
8187
            --  If a primitive is encountered that renames the predefined
8188
            --  equality operator before reaching any explicit equality
8189
            --  primitive, then we still need to create a predefined equality
8190
            --  function, because calls to it can occur via the renaming. A new
8191
            --  name is created for the equality to avoid conflicting with any
8192
            --  user-defined equality. (Note that this doesn't account for
8193
            --  renamings of equality nested within subpackages???)
8194
 
8195
            if Is_Predefined_Eq_Renaming (Node (Prim)) then
8196
               Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
8197
 
8198
            --  User-defined equality
8199
 
8200
            elsif Chars (Node (Prim)) = Name_Op_Eq
8201
              and then Etype (First_Formal (Node (Prim))) =
8202
                         Etype (Next_Formal (First_Formal (Node (Prim))))
8203
              and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
8204
            then
8205
               if No (Alias (Node (Prim)))
8206
                 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
8207
                           N_Subprogram_Renaming_Declaration
8208
               then
8209
                  Eq_Needed := False;
8210
                  exit;
8211
 
8212
               --  If the parent is not an interface type and has an abstract
8213
               --  equality function, the inherited equality is abstract as
8214
               --  well, and no body can be created for it.
8215
 
8216
               elsif not Is_Interface (Etype (Tag_Typ))
8217
                 and then Present (Alias (Node (Prim)))
8218
                 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
8219
               then
8220
                  Eq_Needed := False;
8221
                  exit;
8222
 
8223
               --  If the type has an equality function corresponding with
8224
               --  a primitive defined in an interface type, the inherited
8225
               --  equality is abstract as well, and no body can be created
8226
               --  for it.
8227
 
8228
               elsif Present (Alias (Node (Prim)))
8229
                 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
8230
                 and then
8231
                   Is_Interface
8232
                     (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
8233
               then
8234
                  Eq_Needed := False;
8235
                  exit;
8236
               end if;
8237
            end if;
8238
 
8239
            Next_Elmt (Prim);
8240
         end loop;
8241
 
8242
         --  If a renaming of predefined equality was found but there was no
8243
         --  user-defined equality (so Eq_Needed is still true), then set the
8244
         --  name back to Name_Op_Eq. But in the case where a user-defined
8245
         --  equality was located after such a renaming, then the predefined
8246
         --  equality function is still needed, so Eq_Needed must be set back
8247
         --  to True.
8248
 
8249
         if Eq_Name /= Name_Op_Eq then
8250
            if Eq_Needed then
8251
               Eq_Name := Name_Op_Eq;
8252
            else
8253
               Eq_Needed := True;
8254
            end if;
8255
         end if;
8256
 
8257
         if Eq_Needed then
8258
            Eq_Spec := Predef_Spec_Or_Body (Loc,
8259
              Tag_Typ => Tag_Typ,
8260
              Name    => Eq_Name,
8261
              Profile => New_List (
8262
                Make_Parameter_Specification (Loc,
8263
                  Defining_Identifier =>
8264
                    Make_Defining_Identifier (Loc, Name_X),
8265
                    Parameter_Type      => New_Reference_To (Tag_Typ, Loc)),
8266
                Make_Parameter_Specification (Loc,
8267
                  Defining_Identifier =>
8268
                    Make_Defining_Identifier (Loc, Name_Y),
8269
                    Parameter_Type      => New_Reference_To (Tag_Typ, Loc))),
8270
                Ret_Type => Standard_Boolean);
8271
            Append_To (Res, Eq_Spec);
8272
 
8273
            if Eq_Name /= Name_Op_Eq then
8274
               Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
8275
 
8276
               Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8277
               while Present (Prim) loop
8278
 
8279
                  --  Any renamings of equality that appeared before an
8280
                  --  overriding equality must be updated to refer to the
8281
                  --  entity for the predefined equality, otherwise calls via
8282
                  --  the renaming would get incorrectly resolved to call the
8283
                  --  user-defined equality function.
8284
 
8285
                  if Is_Predefined_Eq_Renaming (Node (Prim)) then
8286
                     Set_Alias (Node (Prim), Renamed_Eq);
8287
 
8288
                  --  Exit upon encountering a user-defined equality
8289
 
8290
                  elsif Chars (Node (Prim)) = Name_Op_Eq
8291
                    and then No (Alias (Node (Prim)))
8292
                  then
8293
                     exit;
8294
                  end if;
8295
 
8296
                  Next_Elmt (Prim);
8297
               end loop;
8298
            end if;
8299
         end if;
8300
 
8301
         --  Spec for dispatching assignment
8302
 
8303
         Append_To (Res, Predef_Spec_Or_Body (Loc,
8304
           Tag_Typ => Tag_Typ,
8305
           Name    => Name_uAssign,
8306
           Profile => New_List (
8307
             Make_Parameter_Specification (Loc,
8308
               Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8309
               Out_Present         => True,
8310
               Parameter_Type      => New_Reference_To (Tag_Typ, Loc)),
8311
 
8312
             Make_Parameter_Specification (Loc,
8313
               Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8314
               Parameter_Type      => New_Reference_To (Tag_Typ, Loc)))));
8315
      end if;
8316
 
8317
      --  Ada 2005: Generate declarations for the following primitive
8318
      --  operations for limited interfaces and synchronized types that
8319
      --  implement a limited interface.
8320
 
8321
      --    Disp_Asynchronous_Select
8322
      --    Disp_Conditional_Select
8323
      --    Disp_Get_Prim_Op_Kind
8324
      --    Disp_Get_Task_Id
8325
      --    Disp_Requeue
8326
      --    Disp_Timed_Select
8327
 
8328
      --  Disable the generation of these bodies if No_Dispatching_Calls,
8329
      --  Ravenscar or ZFP is active.
8330
 
8331
      if Ada_Version >= Ada_2005
8332
        and then not Restriction_Active (No_Dispatching_Calls)
8333
        and then not Restriction_Active (No_Select_Statements)
8334
        and then RTE_Available (RE_Select_Specific_Data)
8335
      then
8336
         --  These primitives are defined abstract in interface types
8337
 
8338
         if Is_Interface (Tag_Typ)
8339
           and then Is_Limited_Record (Tag_Typ)
8340
         then
8341
            Append_To (Res,
8342
              Make_Abstract_Subprogram_Declaration (Loc,
8343
                Specification =>
8344
                  Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8345
 
8346
            Append_To (Res,
8347
              Make_Abstract_Subprogram_Declaration (Loc,
8348
                Specification =>
8349
                  Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8350
 
8351
            Append_To (Res,
8352
              Make_Abstract_Subprogram_Declaration (Loc,
8353
                Specification =>
8354
                  Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8355
 
8356
            Append_To (Res,
8357
              Make_Abstract_Subprogram_Declaration (Loc,
8358
                Specification =>
8359
                  Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8360
 
8361
            Append_To (Res,
8362
              Make_Abstract_Subprogram_Declaration (Loc,
8363
                Specification =>
8364
                  Make_Disp_Requeue_Spec (Tag_Typ)));
8365
 
8366
            Append_To (Res,
8367
              Make_Abstract_Subprogram_Declaration (Loc,
8368
                Specification =>
8369
                  Make_Disp_Timed_Select_Spec (Tag_Typ)));
8370
 
8371
         --  If the ancestor is an interface type we declare non-abstract
8372
         --  primitives to override the abstract primitives of the interface
8373
         --  type.
8374
 
8375
         --  In VM targets we define these primitives in all root tagged types
8376
         --  that are not interface types. Done because in VM targets we don't
8377
         --  have secondary dispatch tables and any derivation of Tag_Typ may
8378
         --  cover limited interfaces (which always have these primitives since
8379
         --  they may be ancestors of synchronized interface types).
8380
 
8381
         elsif (not Is_Interface (Tag_Typ)
8382
                 and then Is_Interface (Etype (Tag_Typ))
8383
                 and then Is_Limited_Record (Etype (Tag_Typ)))
8384
             or else
8385
               (Is_Concurrent_Record_Type (Tag_Typ)
8386
                 and then Has_Interfaces (Tag_Typ))
8387
             or else
8388
               (not Tagged_Type_Expansion
8389
                 and then not Is_Interface (Tag_Typ)
8390
                 and then Tag_Typ = Root_Type (Tag_Typ))
8391
         then
8392
            Append_To (Res,
8393
              Make_Subprogram_Declaration (Loc,
8394
                Specification =>
8395
                  Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8396
 
8397
            Append_To (Res,
8398
              Make_Subprogram_Declaration (Loc,
8399
                Specification =>
8400
                  Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8401
 
8402
            Append_To (Res,
8403
              Make_Subprogram_Declaration (Loc,
8404
                Specification =>
8405
                  Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8406
 
8407
            Append_To (Res,
8408
              Make_Subprogram_Declaration (Loc,
8409
                Specification =>
8410
                  Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8411
 
8412
            Append_To (Res,
8413
              Make_Subprogram_Declaration (Loc,
8414
                Specification =>
8415
                  Make_Disp_Requeue_Spec (Tag_Typ)));
8416
 
8417
            Append_To (Res,
8418
              Make_Subprogram_Declaration (Loc,
8419
                Specification =>
8420
                  Make_Disp_Timed_Select_Spec (Tag_Typ)));
8421
         end if;
8422
      end if;
8423
 
8424
      --  All tagged types receive their own Deep_Adjust and Deep_Finalize
8425
      --  regardless of whether they are controlled or may contain controlled
8426
      --  components.
8427
 
8428
      --  Do not generate the routines if finalization is disabled
8429
 
8430
      if Restriction_Active (No_Finalization) then
8431
         null;
8432
 
8433
      --  Finalization is not available for CIL value types
8434
 
8435
      elsif Is_Value_Type (Tag_Typ) then
8436
         null;
8437
 
8438
      else
8439
         if not Is_Limited_Type (Tag_Typ) then
8440
            Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
8441
         end if;
8442
 
8443
         Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
8444
      end if;
8445
 
8446
      Predef_List := Res;
8447
   end Make_Predefined_Primitive_Specs;
8448
 
8449
   ---------------------------------
8450
   -- Needs_Simple_Initialization --
8451
   ---------------------------------
8452
 
8453
   function Needs_Simple_Initialization
8454
     (T           : Entity_Id;
8455
      Consider_IS : Boolean := True) return Boolean
8456
   is
8457
      Consider_IS_NS : constant Boolean :=
8458
                         Normalize_Scalars
8459
                           or (Initialize_Scalars and Consider_IS);
8460
 
8461
   begin
8462
      --  Never need initialization if it is suppressed
8463
 
8464
      if Initialization_Suppressed (T) then
8465
         return False;
8466
      end if;
8467
 
8468
      --  Check for private type, in which case test applies to the underlying
8469
      --  type of the private type.
8470
 
8471
      if Is_Private_Type (T) then
8472
         declare
8473
            RT : constant Entity_Id := Underlying_Type (T);
8474
 
8475
         begin
8476
            if Present (RT) then
8477
               return Needs_Simple_Initialization (RT);
8478
            else
8479
               return False;
8480
            end if;
8481
         end;
8482
 
8483
      --  Scalar type with Default_Value aspect requires initialization
8484
 
8485
      elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
8486
         return True;
8487
 
8488
      --  Cases needing simple initialization are access types, and, if pragma
8489
      --  Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
8490
      --  types.
8491
 
8492
      elsif Is_Access_Type (T)
8493
        or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
8494
      then
8495
         return True;
8496
 
8497
      --  If Initialize/Normalize_Scalars is in effect, string objects also
8498
      --  need initialization, unless they are created in the course of
8499
      --  expanding an aggregate (since in the latter case they will be
8500
      --  filled with appropriate initializing values before they are used).
8501
 
8502
      elsif Consider_IS_NS
8503
        and then
8504
          (Root_Type (T) = Standard_String
8505
             or else Root_Type (T) = Standard_Wide_String
8506
             or else Root_Type (T) = Standard_Wide_Wide_String)
8507
        and then
8508
          (not Is_Itype (T)
8509
            or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
8510
      then
8511
         return True;
8512
 
8513
      else
8514
         return False;
8515
      end if;
8516
   end Needs_Simple_Initialization;
8517
 
8518
   ----------------------
8519
   -- Predef_Deep_Spec --
8520
   ----------------------
8521
 
8522
   function Predef_Deep_Spec
8523
     (Loc      : Source_Ptr;
8524
      Tag_Typ  : Entity_Id;
8525
      Name     : TSS_Name_Type;
8526
      For_Body : Boolean := False) return Node_Id
8527
   is
8528
      Formals : List_Id;
8529
 
8530
   begin
8531
      --  V : in out Tag_Typ
8532
 
8533
      Formals := New_List (
8534
        Make_Parameter_Specification (Loc,
8535
          Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8536
          In_Present          => True,
8537
          Out_Present         => True,
8538
          Parameter_Type      => New_Reference_To (Tag_Typ, Loc)));
8539
 
8540
      --  F : Boolean := True
8541
 
8542
      if Name = TSS_Deep_Adjust
8543
        or else Name = TSS_Deep_Finalize
8544
      then
8545
         Append_To (Formals,
8546
           Make_Parameter_Specification (Loc,
8547
             Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
8548
             Parameter_Type      => New_Reference_To (Standard_Boolean, Loc),
8549
             Expression          => New_Reference_To (Standard_True, Loc)));
8550
      end if;
8551
 
8552
      return
8553
        Predef_Spec_Or_Body (Loc,
8554
          Name     => Make_TSS_Name (Tag_Typ, Name),
8555
          Tag_Typ  => Tag_Typ,
8556
          Profile  => Formals,
8557
          For_Body => For_Body);
8558
 
8559
   exception
8560
      when RE_Not_Available =>
8561
         return Empty;
8562
   end Predef_Deep_Spec;
8563
 
8564
   -------------------------
8565
   -- Predef_Spec_Or_Body --
8566
   -------------------------
8567
 
8568
   function Predef_Spec_Or_Body
8569
     (Loc      : Source_Ptr;
8570
      Tag_Typ  : Entity_Id;
8571
      Name     : Name_Id;
8572
      Profile  : List_Id;
8573
      Ret_Type : Entity_Id := Empty;
8574
      For_Body : Boolean := False) return Node_Id
8575
   is
8576
      Id   : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
8577
      Spec : Node_Id;
8578
 
8579
   begin
8580
      Set_Is_Public (Id, Is_Public (Tag_Typ));
8581
 
8582
      --  The internal flag is set to mark these declarations because they have
8583
      --  specific properties. First, they are primitives even if they are not
8584
      --  defined in the type scope (the freezing point is not necessarily in
8585
      --  the same scope). Second, the predefined equality can be overridden by
8586
      --  a user-defined equality, no body will be generated in this case.
8587
 
8588
      Set_Is_Internal (Id);
8589
 
8590
      if not Debug_Generated_Code then
8591
         Set_Debug_Info_Off (Id);
8592
      end if;
8593
 
8594
      if No (Ret_Type) then
8595
         Spec :=
8596
           Make_Procedure_Specification (Loc,
8597
             Defining_Unit_Name       => Id,
8598
             Parameter_Specifications => Profile);
8599
      else
8600
         Spec :=
8601
           Make_Function_Specification (Loc,
8602
             Defining_Unit_Name       => Id,
8603
             Parameter_Specifications => Profile,
8604
             Result_Definition        => New_Reference_To (Ret_Type, Loc));
8605
      end if;
8606
 
8607
      if Is_Interface (Tag_Typ) then
8608
         return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8609
 
8610
      --  If body case, return empty subprogram body. Note that this is ill-
8611
      --  formed, because there is not even a null statement, and certainly not
8612
      --  a return in the function case. The caller is expected to do surgery
8613
      --  on the body to add the appropriate stuff.
8614
 
8615
      elsif For_Body then
8616
         return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8617
 
8618
      --  For the case of an Input attribute predefined for an abstract type,
8619
      --  generate an abstract specification. This will never be called, but we
8620
      --  need the slot allocated in the dispatching table so that attributes
8621
      --  typ'Class'Input and typ'Class'Output will work properly.
8622
 
8623
      elsif Is_TSS (Name, TSS_Stream_Input)
8624
        and then Is_Abstract_Type (Tag_Typ)
8625
      then
8626
         return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8627
 
8628
      --  Normal spec case, where we return a subprogram declaration
8629
 
8630
      else
8631
         return Make_Subprogram_Declaration (Loc, Spec);
8632
      end if;
8633
   end Predef_Spec_Or_Body;
8634
 
8635
   -----------------------------
8636
   -- Predef_Stream_Attr_Spec --
8637
   -----------------------------
8638
 
8639
   function Predef_Stream_Attr_Spec
8640
     (Loc      : Source_Ptr;
8641
      Tag_Typ  : Entity_Id;
8642
      Name     : TSS_Name_Type;
8643
      For_Body : Boolean := False) return Node_Id
8644
   is
8645
      Ret_Type : Entity_Id;
8646
 
8647
   begin
8648
      if Name = TSS_Stream_Input then
8649
         Ret_Type := Tag_Typ;
8650
      else
8651
         Ret_Type := Empty;
8652
      end if;
8653
 
8654
      return
8655
        Predef_Spec_Or_Body
8656
          (Loc,
8657
           Name     => Make_TSS_Name (Tag_Typ, Name),
8658
           Tag_Typ  => Tag_Typ,
8659
           Profile  => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8660
           Ret_Type => Ret_Type,
8661
           For_Body => For_Body);
8662
   end Predef_Stream_Attr_Spec;
8663
 
8664
   ---------------------------------
8665
   -- Predefined_Primitive_Bodies --
8666
   ---------------------------------
8667
 
8668
   function Predefined_Primitive_Bodies
8669
     (Tag_Typ    : Entity_Id;
8670
      Renamed_Eq : Entity_Id) return List_Id
8671
   is
8672
      Loc       : constant Source_Ptr := Sloc (Tag_Typ);
8673
      Res       : constant List_Id    := New_List;
8674
      Decl      : Node_Id;
8675
      Prim      : Elmt_Id;
8676
      Eq_Needed : Boolean;
8677
      Eq_Name   : Name_Id;
8678
      Ent       : Entity_Id;
8679
 
8680
      pragma Warnings (Off, Ent);
8681
 
8682
   begin
8683
      pragma Assert (not Is_Interface (Tag_Typ));
8684
 
8685
      --  See if we have a predefined "=" operator
8686
 
8687
      if Present (Renamed_Eq) then
8688
         Eq_Needed := True;
8689
         Eq_Name   := Chars (Renamed_Eq);
8690
 
8691
      --  If the parent is an interface type then it has defined all the
8692
      --  predefined primitives abstract and we need to check if the type
8693
      --  has some user defined "=" function to avoid generating it.
8694
 
8695
      elsif Is_Interface (Etype (Tag_Typ)) then
8696
         Eq_Needed := True;
8697
         Eq_Name := Name_Op_Eq;
8698
 
8699
         Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8700
         while Present (Prim) loop
8701
            if Chars (Node (Prim)) = Name_Op_Eq
8702
              and then not Is_Internal (Node (Prim))
8703
            then
8704
               Eq_Needed := False;
8705
               Eq_Name := No_Name;
8706
               exit;
8707
            end if;
8708
 
8709
            Next_Elmt (Prim);
8710
         end loop;
8711
 
8712
      else
8713
         Eq_Needed := False;
8714
         Eq_Name   := No_Name;
8715
 
8716
         Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8717
         while Present (Prim) loop
8718
            if Chars (Node (Prim)) = Name_Op_Eq
8719
              and then Is_Internal (Node (Prim))
8720
            then
8721
               Eq_Needed := True;
8722
               Eq_Name := Name_Op_Eq;
8723
               exit;
8724
            end if;
8725
 
8726
            Next_Elmt (Prim);
8727
         end loop;
8728
      end if;
8729
 
8730
      --  Body of _Size
8731
 
8732
      Decl := Predef_Spec_Or_Body (Loc,
8733
        Tag_Typ => Tag_Typ,
8734
        Name    => Name_uSize,
8735
        Profile => New_List (
8736
          Make_Parameter_Specification (Loc,
8737
            Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8738
            Parameter_Type      => New_Reference_To (Tag_Typ, Loc))),
8739
 
8740
        Ret_Type => Standard_Long_Long_Integer,
8741
        For_Body => True);
8742
 
8743
      Set_Handled_Statement_Sequence (Decl,
8744
        Make_Handled_Sequence_Of_Statements (Loc, New_List (
8745
          Make_Simple_Return_Statement (Loc,
8746
            Expression =>
8747
              Make_Attribute_Reference (Loc,
8748
                Prefix          => Make_Identifier (Loc, Name_X),
8749
                Attribute_Name  => Name_Size)))));
8750
 
8751
      Append_To (Res, Decl);
8752
 
8753
      --  Bodies for Dispatching stream IO routines. We need these only for
8754
      --  non-limited types (in the limited case there is no dispatching).
8755
      --  We also skip them if dispatching or finalization are not available.
8756
 
8757
      if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8758
        and then No (TSS (Tag_Typ, TSS_Stream_Read))
8759
      then
8760
         Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8761
         Append_To (Res, Decl);
8762
      end if;
8763
 
8764
      if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8765
        and then No (TSS (Tag_Typ, TSS_Stream_Write))
8766
      then
8767
         Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8768
         Append_To (Res, Decl);
8769
      end if;
8770
 
8771
      --  Skip body of _Input for the abstract case, since the corresponding
8772
      --  spec is abstract (see Predef_Spec_Or_Body).
8773
 
8774
      if not Is_Abstract_Type (Tag_Typ)
8775
        and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8776
        and then No (TSS (Tag_Typ, TSS_Stream_Input))
8777
      then
8778
         Build_Record_Or_Elementary_Input_Function
8779
           (Loc, Tag_Typ, Decl, Ent);
8780
         Append_To (Res, Decl);
8781
      end if;
8782
 
8783
      if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8784
        and then No (TSS (Tag_Typ, TSS_Stream_Output))
8785
      then
8786
         Build_Record_Or_Elementary_Output_Procedure
8787
           (Loc, Tag_Typ, Decl, Ent);
8788
         Append_To (Res, Decl);
8789
      end if;
8790
 
8791
      --  Ada 2005: Generate bodies for the following primitive operations for
8792
      --  limited interfaces and synchronized types that implement a limited
8793
      --  interface.
8794
 
8795
      --    disp_asynchronous_select
8796
      --    disp_conditional_select
8797
      --    disp_get_prim_op_kind
8798
      --    disp_get_task_id
8799
      --    disp_timed_select
8800
 
8801
      --  The interface versions will have null bodies
8802
 
8803
      --  Disable the generation of these bodies if No_Dispatching_Calls,
8804
      --  Ravenscar or ZFP is active.
8805
 
8806
      --  In VM targets we define these primitives in all root tagged types
8807
      --  that are not interface types. Done because in VM targets we don't
8808
      --  have secondary dispatch tables and any derivation of Tag_Typ may
8809
      --  cover limited interfaces (which always have these primitives since
8810
      --  they may be ancestors of synchronized interface types).
8811
 
8812
      if Ada_Version >= Ada_2005
8813
        and then not Is_Interface (Tag_Typ)
8814
        and then
8815
          ((Is_Interface (Etype (Tag_Typ))
8816
             and then Is_Limited_Record (Etype (Tag_Typ)))
8817
           or else
8818
             (Is_Concurrent_Record_Type (Tag_Typ)
8819
               and then Has_Interfaces (Tag_Typ))
8820
           or else
8821
             (not Tagged_Type_Expansion
8822
               and then Tag_Typ = Root_Type (Tag_Typ)))
8823
        and then not Restriction_Active (No_Dispatching_Calls)
8824
        and then not Restriction_Active (No_Select_Statements)
8825
        and then RTE_Available (RE_Select_Specific_Data)
8826
      then
8827
         Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8828
         Append_To (Res, Make_Disp_Conditional_Select_Body  (Tag_Typ));
8829
         Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body    (Tag_Typ));
8830
         Append_To (Res, Make_Disp_Get_Task_Id_Body         (Tag_Typ));
8831
         Append_To (Res, Make_Disp_Requeue_Body             (Tag_Typ));
8832
         Append_To (Res, Make_Disp_Timed_Select_Body        (Tag_Typ));
8833
      end if;
8834
 
8835
      if not Is_Limited_Type (Tag_Typ)
8836
        and then not Is_Interface (Tag_Typ)
8837
      then
8838
         --  Body for equality
8839
 
8840
         if Eq_Needed then
8841
            Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
8842
            Append_To (Res, Decl);
8843
         end if;
8844
 
8845
         --  Body for dispatching assignment
8846
 
8847
         Decl :=
8848
           Predef_Spec_Or_Body (Loc,
8849
             Tag_Typ => Tag_Typ,
8850
             Name    => Name_uAssign,
8851
             Profile => New_List (
8852
               Make_Parameter_Specification (Loc,
8853
                 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8854
                 Out_Present         => True,
8855
                 Parameter_Type      => New_Reference_To (Tag_Typ, Loc)),
8856
 
8857
               Make_Parameter_Specification (Loc,
8858
                 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8859
                 Parameter_Type      => New_Reference_To (Tag_Typ, Loc))),
8860
             For_Body => True);
8861
 
8862
         Set_Handled_Statement_Sequence (Decl,
8863
           Make_Handled_Sequence_Of_Statements (Loc, New_List (
8864
             Make_Assignment_Statement (Loc,
8865
               Name       => Make_Identifier (Loc, Name_X),
8866
               Expression => Make_Identifier (Loc, Name_Y)))));
8867
 
8868
         Append_To (Res, Decl);
8869
      end if;
8870
 
8871
      --  Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
8872
      --  tagged types which do not contain controlled components.
8873
 
8874
      --  Do not generate the routines if finalization is disabled
8875
 
8876
      if Restriction_Active (No_Finalization) then
8877
         null;
8878
 
8879
      elsif not Has_Controlled_Component (Tag_Typ) then
8880
         if not Is_Limited_Type (Tag_Typ) then
8881
            Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8882
 
8883
            if Is_Controlled (Tag_Typ) then
8884
               Set_Handled_Statement_Sequence (Decl,
8885
                 Make_Handled_Sequence_Of_Statements (Loc,
8886
                   Statements => New_List (
8887
                     Make_Adjust_Call (
8888
                       Obj_Ref => Make_Identifier (Loc, Name_V),
8889
                       Typ     => Tag_Typ))));
8890
            else
8891
               Set_Handled_Statement_Sequence (Decl,
8892
                 Make_Handled_Sequence_Of_Statements (Loc,
8893
                   Statements => New_List (
8894
                     Make_Null_Statement (Loc))));
8895
            end if;
8896
 
8897
            Append_To (Res, Decl);
8898
         end if;
8899
 
8900
         Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
8901
 
8902
         if Is_Controlled (Tag_Typ) then
8903
            Set_Handled_Statement_Sequence (Decl,
8904
              Make_Handled_Sequence_Of_Statements (Loc,
8905
                Statements => New_List (
8906
                  Make_Final_Call
8907
                    (Obj_Ref => Make_Identifier (Loc, Name_V),
8908
                     Typ     => Tag_Typ))));
8909
         else
8910
            Set_Handled_Statement_Sequence (Decl,
8911
              Make_Handled_Sequence_Of_Statements (Loc,
8912
                Statements => New_List (Make_Null_Statement (Loc))));
8913
         end if;
8914
 
8915
         Append_To (Res, Decl);
8916
      end if;
8917
 
8918
      return Res;
8919
   end Predefined_Primitive_Bodies;
8920
 
8921
   ---------------------------------
8922
   -- Predefined_Primitive_Freeze --
8923
   ---------------------------------
8924
 
8925
   function Predefined_Primitive_Freeze
8926
     (Tag_Typ : Entity_Id) return List_Id
8927
   is
8928
      Res     : constant List_Id := New_List;
8929
      Prim    : Elmt_Id;
8930
      Frnodes : List_Id;
8931
 
8932
   begin
8933
      Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8934
      while Present (Prim) loop
8935
         if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8936
            Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
8937
 
8938
            if Present (Frnodes) then
8939
               Append_List_To (Res, Frnodes);
8940
            end if;
8941
         end if;
8942
 
8943
         Next_Elmt (Prim);
8944
      end loop;
8945
 
8946
      return Res;
8947
   end Predefined_Primitive_Freeze;
8948
 
8949
   -------------------------
8950
   -- Stream_Operation_OK --
8951
   -------------------------
8952
 
8953
   function Stream_Operation_OK
8954
     (Typ       : Entity_Id;
8955
      Operation : TSS_Name_Type) return Boolean
8956
   is
8957
      Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
8958
 
8959
   begin
8960
      --  Special case of a limited type extension: a default implementation
8961
      --  of the stream attributes Read or Write exists if that attribute
8962
      --  has been specified or is available for an ancestor type; a default
8963
      --  implementation of the attribute Output (resp. Input) exists if the
8964
      --  attribute has been specified or Write (resp. Read) is available for
8965
      --  an ancestor type. The last condition only applies under Ada 2005.
8966
 
8967
      if Is_Limited_Type (Typ)
8968
        and then Is_Tagged_Type (Typ)
8969
      then
8970
         if Operation = TSS_Stream_Read then
8971
            Has_Predefined_Or_Specified_Stream_Attribute :=
8972
              Has_Specified_Stream_Read (Typ);
8973
 
8974
         elsif Operation = TSS_Stream_Write then
8975
            Has_Predefined_Or_Specified_Stream_Attribute :=
8976
              Has_Specified_Stream_Write (Typ);
8977
 
8978
         elsif Operation = TSS_Stream_Input then
8979
            Has_Predefined_Or_Specified_Stream_Attribute :=
8980
              Has_Specified_Stream_Input (Typ)
8981
                or else
8982
                  (Ada_Version >= Ada_2005
8983
                    and then Stream_Operation_OK (Typ, TSS_Stream_Read));
8984
 
8985
         elsif Operation = TSS_Stream_Output then
8986
            Has_Predefined_Or_Specified_Stream_Attribute :=
8987
              Has_Specified_Stream_Output (Typ)
8988
                or else
8989
                  (Ada_Version >= Ada_2005
8990
                    and then Stream_Operation_OK (Typ, TSS_Stream_Write));
8991
         end if;
8992
 
8993
         --  Case of inherited TSS_Stream_Read or TSS_Stream_Write
8994
 
8995
         if not Has_Predefined_Or_Specified_Stream_Attribute
8996
           and then Is_Derived_Type (Typ)
8997
           and then (Operation = TSS_Stream_Read
8998
                      or else Operation = TSS_Stream_Write)
8999
         then
9000
            Has_Predefined_Or_Specified_Stream_Attribute :=
9001
              Present
9002
                (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
9003
         end if;
9004
      end if;
9005
 
9006
      --  If the type is not limited, or else is limited but the attribute is
9007
      --  explicitly specified or is predefined for the type, then return True,
9008
      --  unless other conditions prevail, such as restrictions prohibiting
9009
      --  streams or dispatching operations. We also return True for limited
9010
      --  interfaces, because they may be extended by nonlimited types and
9011
      --  permit inheritance in this case (addresses cases where an abstract
9012
      --  extension doesn't get 'Input declared, as per comments below, but
9013
      --  'Class'Input must still be allowed). Note that attempts to apply
9014
      --  stream attributes to a limited interface or its class-wide type
9015
      --  (or limited extensions thereof) will still get properly rejected
9016
      --  by Check_Stream_Attribute.
9017
 
9018
      --  We exclude the Input operation from being a predefined subprogram in
9019
      --  the case where the associated type is an abstract extension, because
9020
      --  the attribute is not callable in that case, per 13.13.2(49/2). Also,
9021
      --  we don't want an abstract version created because types derived from
9022
      --  the abstract type may not even have Input available (for example if
9023
      --  derived from a private view of the abstract type that doesn't have
9024
      --  a visible Input), but a VM such as .NET or the Java VM can treat the
9025
      --  operation as inherited anyway, and we don't want an abstract function
9026
      --  to be (implicitly) inherited in that case because it can lead to a VM
9027
      --  exception.
9028
 
9029
      --  Do not generate stream routines for type Finalization_Master because
9030
      --  a master may never appear in types and therefore cannot be read or
9031
      --  written.
9032
 
9033
      return
9034
          (not Is_Limited_Type (Typ)
9035
            or else Is_Interface (Typ)
9036
            or else Has_Predefined_Or_Specified_Stream_Attribute)
9037
        and then
9038
          (Operation /= TSS_Stream_Input
9039
            or else not Is_Abstract_Type (Typ)
9040
            or else not Is_Derived_Type (Typ))
9041
        and then not Has_Unknown_Discriminants (Typ)
9042
        and then not
9043
          (Is_Interface (Typ)
9044
            and then
9045
              (Is_Task_Interface (Typ)
9046
                or else Is_Protected_Interface (Typ)
9047
                or else Is_Synchronized_Interface (Typ)))
9048
        and then not Restriction_Active (No_Streams)
9049
        and then not Restriction_Active (No_Dispatch)
9050
        and then not No_Run_Time_Mode
9051
        and then RTE_Available (RE_Tag)
9052
        and then No (Type_Without_Stream_Operation (Typ))
9053
        and then RTE_Available (RE_Root_Stream_Type)
9054
        and then not Is_RTE (Typ, RE_Finalization_Master);
9055
   end Stream_Operation_OK;
9056
 
9057
end Exp_Ch3;

powered by: WebSVN 2.1.0

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