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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [gcc-interface/] [decl.c] - Blame information for rev 706

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 706 jeremybenn
/****************************************************************************
2
 *                                                                          *
3
 *                         GNAT COMPILER COMPONENTS                         *
4
 *                                                                          *
5
 *                                 D E C L                                  *
6
 *                                                                          *
7
 *                          C Implementation File                           *
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 along with GCC; see the file COPYING3.  If not see        *
19
 * <http://www.gnu.org/licenses/>.                                          *
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
#include "config.h"
27
#include "system.h"
28
#include "coretypes.h"
29
#include "tm.h"
30
#include "tree.h"
31
#include "flags.h"
32
#include "toplev.h"
33
#include "ggc.h"
34
#include "target.h"
35
#include "tree-inline.h"
36
 
37
#include "ada.h"
38
#include "types.h"
39
#include "atree.h"
40
#include "elists.h"
41
#include "namet.h"
42
#include "nlists.h"
43
#include "repinfo.h"
44
#include "snames.h"
45
#include "stringt.h"
46
#include "uintp.h"
47
#include "fe.h"
48
#include "sinfo.h"
49
#include "einfo.h"
50
#include "ada-tree.h"
51
#include "gigi.h"
52
 
53
/* Convention_Stdcall should be processed in a specific way on 32 bits
54
   Windows targets only.  The macro below is a helper to avoid having to
55
   check for a Windows specific attribute throughout this unit.  */
56
 
57
#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
58
#ifdef TARGET_64BIT
59
#define Has_Stdcall_Convention(E) \
60
  (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
61
#else
62
#define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
63
#endif
64
#else
65
#define Has_Stdcall_Convention(E) 0
66
#endif
67
 
68
/* Stack realignment is necessary for functions with foreign conventions when
69
   the ABI doesn't mandate as much as what the compiler assumes - that is, up
70
   to PREFERRED_STACK_BOUNDARY.
71
 
72
   Such realignment can be requested with a dedicated function type attribute
73
   on the targets that support it.  We define FOREIGN_FORCE_REALIGN_STACK to
74
   characterize the situations where the attribute should be set.  We rely on
75
   compiler configuration settings for 'main' to decide.  */
76
 
77
#ifdef MAIN_STACK_BOUNDARY
78
#define FOREIGN_FORCE_REALIGN_STACK \
79
  (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
80
#else
81
#define FOREIGN_FORCE_REALIGN_STACK 0
82
#endif
83
 
84
struct incomplete
85
{
86
  struct incomplete *next;
87
  tree old_type;
88
  Entity_Id full_type;
89
};
90
 
91
/* These variables are used to defer recursively expanding incomplete types
92
   while we are processing an array, a record or a subprogram type.  */
93
static int defer_incomplete_level = 0;
94
static struct incomplete *defer_incomplete_list;
95
 
96
/* This variable is used to delay expanding From_With_Type types until the
97
   end of the spec.  */
98
static struct incomplete *defer_limited_with;
99
 
100
/* These variables are used to defer finalizing types.  The element of the
101
   list is the TYPE_DECL associated with the type.  */
102
static int defer_finalize_level = 0;
103
static VEC (tree,heap) *defer_finalize_list;
104
 
105
typedef struct subst_pair_d {
106
  tree discriminant;
107
  tree replacement;
108
} subst_pair;
109
 
110
DEF_VEC_O(subst_pair);
111
DEF_VEC_ALLOC_O(subst_pair,heap);
112
 
113
typedef struct variant_desc_d {
114
  /* The type of the variant.  */
115
  tree type;
116
 
117
  /* The associated field.  */
118
  tree field;
119
 
120
  /* The value of the qualifier.  */
121
  tree qual;
122
 
123
  /* The record associated with this variant.  */
124
  tree record;
125
} variant_desc;
126
 
127
DEF_VEC_O(variant_desc);
128
DEF_VEC_ALLOC_O(variant_desc,heap);
129
 
130
/* A hash table used to cache the result of annotate_value.  */
131
static GTY ((if_marked ("tree_int_map_marked_p"),
132
             param_is (struct tree_int_map))) htab_t annotate_value_cache;
133
 
134
enum alias_set_op
135
{
136
  ALIAS_SET_COPY,
137
  ALIAS_SET_SUBSET,
138
  ALIAS_SET_SUPERSET
139
};
140
 
141
static void relate_alias_sets (tree, tree, enum alias_set_op);
142
 
143
static bool allocatable_size_p (tree, bool);
144
static void prepend_one_attribute_to (struct attrib **,
145
                                      enum attr_type, tree, tree, Node_Id);
146
static void prepend_attributes (Entity_Id, struct attrib **);
147
static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
148
static bool type_has_variable_size (tree);
149
static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
150
static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
151
                                    unsigned int);
152
static tree make_packable_type (tree, bool);
153
static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
154
static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
155
                               bool *);
156
static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
157
static bool same_discriminant_p (Entity_Id, Entity_Id);
158
static bool array_type_has_nonaliased_component (tree, Entity_Id);
159
static bool compile_time_known_address_p (Node_Id);
160
static bool cannot_be_superflat_p (Node_Id);
161
static bool constructor_address_p (tree);
162
static void components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
163
                                  bool, bool, bool, bool, bool, tree, tree *);
164
static Uint annotate_value (tree);
165
static void annotate_rep (Entity_Id, tree);
166
static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
167
static VEC(subst_pair,heap) *build_subst_list (Entity_Id, Entity_Id, bool);
168
static VEC(variant_desc,heap) *build_variant_list (tree,
169
                                                   VEC(subst_pair,heap) *,
170
                                                   VEC(variant_desc,heap) *);
171
static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
172
static void set_rm_size (Uint, tree, Entity_Id);
173
static tree make_type_from_size (tree, tree, bool);
174
static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
175
static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
176
static void check_ok_for_atomic (tree, Entity_Id, bool);
177
static tree create_field_decl_from (tree, tree, tree, tree, tree,
178
                                    VEC(subst_pair,heap) *);
179
static tree create_rep_part (tree, tree, tree);
180
static tree get_rep_part (tree);
181
static tree create_variant_part_from (tree, VEC(variant_desc,heap) *, tree,
182
                                      tree, VEC(subst_pair,heap) *);
183
static void copy_and_substitute_in_size (tree, tree, VEC(subst_pair,heap) *);
184
static void rest_of_type_decl_compilation_no_defer (tree);
185
 
186
/* The relevant constituents of a subprogram binding to a GCC builtin.  Used
187
   to pass around calls performing profile compatibility checks.  */
188
 
189
typedef struct {
190
  Entity_Id gnat_entity;  /* The Ada subprogram entity.  */
191
  tree ada_fntype;        /* The corresponding GCC type node.  */
192
  tree btin_fntype;       /* The GCC builtin function type node.  */
193
} intrin_binding_t;
194
 
195
static bool intrin_profiles_compatible_p (intrin_binding_t *);
196
 
197
/* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
198
   entity, return the equivalent GCC tree for that entity (a ..._DECL node)
199
   and associate the ..._DECL node with the input GNAT defining identifier.
200
 
201
   If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
202
   initial value (in GCC tree form).  This is optional for a variable.  For
203
   a renamed entity, GNU_EXPR gives the object being renamed.
204
 
205
   DEFINITION is nonzero if this call is intended for a definition.  This is
206
   used for separate compilation where it is necessary to know whether an
207
   external declaration or a definition must be created if the GCC equivalent
208
   was not created previously.  The value of 1 is normally used for a nonzero
209
   DEFINITION, but a value of 2 is used in special circumstances, defined in
210
   the code.  */
211
 
212
tree
213
gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
214
{
215
  /* Contains the kind of the input GNAT node.  */
216
  const Entity_Kind kind = Ekind (gnat_entity);
217
  /* True if this is a type.  */
218
  const bool is_type = IN (kind, Type_Kind);
219
  /* True if debug info is requested for this entity.  */
220
  const bool debug_info_p = Needs_Debug_Info (gnat_entity);
221
  /* True if this entity is to be considered as imported.  */
222
  const bool imported_p
223
    = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
224
  /* For a type, contains the equivalent GNAT node to be used in gigi.  */
225
  Entity_Id gnat_equiv_type = Empty;
226
  /* Temporary used to walk the GNAT tree.  */
227
  Entity_Id gnat_temp;
228
  /* Contains the GCC DECL node which is equivalent to the input GNAT node.
229
     This node will be associated with the GNAT node by calling at the end
230
     of the `switch' statement.  */
231
  tree gnu_decl = NULL_TREE;
232
  /* Contains the GCC type to be used for the GCC node.  */
233
  tree gnu_type = NULL_TREE;
234
  /* Contains the GCC size tree to be used for the GCC node.  */
235
  tree gnu_size = NULL_TREE;
236
  /* Contains the GCC name to be used for the GCC node.  */
237
  tree gnu_entity_name;
238
  /* True if we have already saved gnu_decl as a GNAT association.  */
239
  bool saved = false;
240
  /* True if we incremented defer_incomplete_level.  */
241
  bool this_deferred = false;
242
  /* True if we incremented force_global.  */
243
  bool this_global = false;
244
  /* True if we should check to see if elaborated during processing.  */
245
  bool maybe_present = false;
246
  /* True if we made GNU_DECL and its type here.  */
247
  bool this_made_decl = false;
248
  /* Size and alignment of the GCC node, if meaningful.  */
249
  unsigned int esize = 0, align = 0;
250
  /* Contains the list of attributes directly attached to the entity.  */
251
  struct attrib *attr_list = NULL;
252
 
253
  /* Since a use of an Itype is a definition, process it as such if it
254
     is not in a with'ed unit.  */
255
  if (!definition
256
      && is_type
257
      && Is_Itype (gnat_entity)
258
      && !present_gnu_tree (gnat_entity)
259
      && In_Extended_Main_Code_Unit (gnat_entity))
260
    {
261
      /* Ensure that we are in a subprogram mentioned in the Scope chain of
262
         this entity, our current scope is global, or we encountered a task
263
         or entry (where we can't currently accurately check scoping).  */
264
      if (!current_function_decl
265
          || DECL_ELABORATION_PROC_P (current_function_decl))
266
        {
267
          process_type (gnat_entity);
268
          return get_gnu_tree (gnat_entity);
269
        }
270
 
271
      for (gnat_temp = Scope (gnat_entity);
272
           Present (gnat_temp);
273
           gnat_temp = Scope (gnat_temp))
274
        {
275
          if (Is_Type (gnat_temp))
276
            gnat_temp = Underlying_Type (gnat_temp);
277
 
278
          if (Ekind (gnat_temp) == E_Subprogram_Body)
279
            gnat_temp
280
              = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
281
 
282
          if (IN (Ekind (gnat_temp), Subprogram_Kind)
283
              && Present (Protected_Body_Subprogram (gnat_temp)))
284
            gnat_temp = Protected_Body_Subprogram (gnat_temp);
285
 
286
          if (Ekind (gnat_temp) == E_Entry
287
              || Ekind (gnat_temp) == E_Entry_Family
288
              || Ekind (gnat_temp) == E_Task_Type
289
              || (IN (Ekind (gnat_temp), Subprogram_Kind)
290
                  && present_gnu_tree (gnat_temp)
291
                  && (current_function_decl
292
                      == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
293
            {
294
              process_type (gnat_entity);
295
              return get_gnu_tree (gnat_entity);
296
            }
297
        }
298
 
299
      /* This abort means the Itype has an incorrect scope, i.e. that its
300
         scope does not correspond to the subprogram it is declared in.  */
301
      gcc_unreachable ();
302
    }
303
 
304
  /* If we've already processed this entity, return what we got last time.
305
     If we are defining the node, we should not have already processed it.
306
     In that case, we will abort below when we try to save a new GCC tree
307
     for this object.  We also need to handle the case of getting a dummy
308
     type when a Full_View exists.  */
309
  if ((!definition || (is_type && imported_p))
310
      && present_gnu_tree (gnat_entity))
311
    {
312
      gnu_decl = get_gnu_tree (gnat_entity);
313
 
314
      if (TREE_CODE (gnu_decl) == TYPE_DECL
315
          && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
316
          && IN (kind, Incomplete_Or_Private_Kind)
317
          && Present (Full_View (gnat_entity)))
318
        {
319
          gnu_decl
320
            = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
321
          save_gnu_tree (gnat_entity, NULL_TREE, false);
322
          save_gnu_tree (gnat_entity, gnu_decl, false);
323
        }
324
 
325
      return gnu_decl;
326
    }
327
 
328
  /* If this is a numeric or enumeral type, or an access type, a nonzero
329
     Esize must be specified unless it was specified by the programmer.  */
330
  gcc_assert (!Unknown_Esize (gnat_entity)
331
              || Has_Size_Clause (gnat_entity)
332
              || (!IN (kind, Numeric_Kind)
333
                  && !IN (kind, Enumeration_Kind)
334
                  && (!IN (kind, Access_Kind)
335
                      || kind == E_Access_Protected_Subprogram_Type
336
                      || kind == E_Anonymous_Access_Protected_Subprogram_Type
337
                      || kind == E_Access_Subtype)));
338
 
339
  /* The RM size must be specified for all discrete and fixed-point types.  */
340
  gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
341
                && Unknown_RM_Size (gnat_entity)));
342
 
343
  /* If we get here, it means we have not yet done anything with this entity.
344
     If we are not defining it, it must be a type or an entity that is defined
345
     elsewhere or externally, otherwise we should have defined it already.  */
346
  gcc_assert (definition
347
              || type_annotate_only
348
              || is_type
349
              || kind == E_Discriminant
350
              || kind == E_Component
351
              || kind == E_Label
352
              || (kind == E_Constant && Present (Full_View (gnat_entity)))
353
              || Is_Public (gnat_entity));
354
 
355
  /* Get the name of the entity and set up the line number and filename of
356
     the original definition for use in any decl we make.  */
357
  gnu_entity_name = get_entity_name (gnat_entity);
358
  Sloc_to_locus (Sloc (gnat_entity), &input_location);
359
 
360
  /* For cases when we are not defining (i.e., we are referencing from
361
     another compilation unit) public entities, show we are at global level
362
     for the purpose of computing scopes.  Don't do this for components or
363
     discriminants since the relevant test is whether or not the record is
364
     being defined.  Don't do this for constants either as we'll look into
365
     their defining expression in the local context.  */
366
  if (!definition
367
      && kind != E_Component
368
      && kind != E_Discriminant
369
      && kind != E_Constant
370
      && Is_Public (gnat_entity)
371
      && !Is_Statically_Allocated (gnat_entity))
372
    force_global++, this_global = true;
373
 
374
  /* Handle any attributes directly attached to the entity.  */
375
  if (Has_Gigi_Rep_Item (gnat_entity))
376
    prepend_attributes (gnat_entity, &attr_list);
377
 
378
  /* Do some common processing for types.  */
379
  if (is_type)
380
    {
381
      /* Compute the equivalent type to be used in gigi.  */
382
      gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
383
 
384
      /* Machine_Attributes on types are expected to be propagated to
385
         subtypes.  The corresponding Gigi_Rep_Items are only attached
386
         to the first subtype though, so we handle the propagation here.  */
387
      if (Base_Type (gnat_entity) != gnat_entity
388
          && !Is_First_Subtype (gnat_entity)
389
          && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
390
        prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
391
                            &attr_list);
392
 
393
      /* Compute a default value for the size of the type.  */
394
      if (Known_Esize (gnat_entity)
395
          && UI_Is_In_Int_Range (Esize (gnat_entity)))
396
        {
397
          unsigned int max_esize;
398
          esize = UI_To_Int (Esize (gnat_entity));
399
 
400
          if (IN (kind, Float_Kind))
401
            max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
402
          else if (IN (kind, Access_Kind))
403
            max_esize = POINTER_SIZE * 2;
404
          else
405
            max_esize = LONG_LONG_TYPE_SIZE;
406
 
407
          if (esize > max_esize)
408
           esize = max_esize;
409
        }
410
    }
411
 
412
  switch (kind)
413
    {
414
    case E_Constant:
415
      /* If this is a use of a deferred constant without address clause,
416
         get its full definition.  */
417
      if (!definition
418
          && No (Address_Clause (gnat_entity))
419
          && Present (Full_View (gnat_entity)))
420
        {
421
          gnu_decl
422
            = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
423
          saved = true;
424
          break;
425
        }
426
 
427
      /* If we have an external constant that we are not defining, get the
428
         expression that is was defined to represent.  We may throw it away
429
         later if it is not a constant.  But do not retrieve the expression
430
         if it is an allocator because the designated type might be dummy
431
         at this point.  */
432
      if (!definition
433
          && !No_Initialization (Declaration_Node (gnat_entity))
434
          && Present (Expression (Declaration_Node (gnat_entity)))
435
          && Nkind (Expression (Declaration_Node (gnat_entity)))
436
             != N_Allocator)
437
        {
438
          bool went_into_elab_proc = false;
439
 
440
          /* The expression may contain N_Expression_With_Actions nodes and
441
             thus object declarations from other units.  In this case, even
442
             though the expression will eventually be discarded since not a
443
             constant, the declarations would be stuck either in the global
444
             varpool or in the current scope.  Therefore we force the local
445
             context and create a fake scope that we'll zap at the end.  */
446
          if (!current_function_decl)
447
            {
448
              current_function_decl = get_elaboration_procedure ();
449
              went_into_elab_proc = true;
450
            }
451
          gnat_pushlevel ();
452
 
453
          gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
454
 
455
          gnat_zaplevel ();
456
          if (went_into_elab_proc)
457
            current_function_decl = NULL_TREE;
458
        }
459
 
460
      /* Ignore deferred constant definitions without address clause since
461
         they are processed fully in the front-end.  If No_Initialization
462
         is set, this is not a deferred constant but a constant whose value
463
         is built manually.  And constants that are renamings are handled
464
         like variables.  */
465
      if (definition
466
          && !gnu_expr
467
          && No (Address_Clause (gnat_entity))
468
          && !No_Initialization (Declaration_Node (gnat_entity))
469
          && No (Renamed_Object (gnat_entity)))
470
        {
471
          gnu_decl = error_mark_node;
472
          saved = true;
473
          break;
474
        }
475
 
476
      /* Ignore constant definitions already marked with the error node.  See
477
         the N_Object_Declaration case of gnat_to_gnu for the rationale.  */
478
      if (definition
479
          && gnu_expr
480
          && present_gnu_tree (gnat_entity)
481
          && get_gnu_tree (gnat_entity) == error_mark_node)
482
        {
483
          maybe_present = true;
484
          break;
485
        }
486
 
487
      goto object;
488
 
489
    case E_Exception:
490
      /* We used to special case VMS exceptions here to directly map them to
491
         their associated condition code.  Since this code had to be masked
492
         dynamically to strip off the severity bits, this caused trouble in
493
         the GCC/ZCX case because the "type" pointers we store in the tables
494
         have to be static.  We now don't special case here anymore, and let
495
         the regular processing take place, which leaves us with a regular
496
         exception data object for VMS exceptions too.  The condition code
497
         mapping is taken care of by the front end and the bitmasking by the
498
         run-time library.  */
499
      goto object;
500
 
501
    case E_Discriminant:
502
    case E_Component:
503
      {
504
        /* The GNAT record where the component was defined.  */
505
        Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
506
 
507
        /* If the variable is an inherited record component (in the case of
508
           extended record types), just return the inherited entity, which
509
           must be a FIELD_DECL.  Likewise for discriminants.
510
           For discriminants of untagged records which have explicit
511
           stored discriminants, return the entity for the corresponding
512
           stored discriminant.  Also use Original_Record_Component
513
           if the record has a private extension.  */
514
        if (Present (Original_Record_Component (gnat_entity))
515
            && Original_Record_Component (gnat_entity) != gnat_entity)
516
          {
517
            gnu_decl
518
              = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
519
                                    gnu_expr, definition);
520
            saved = true;
521
            break;
522
          }
523
 
524
        /* If the enclosing record has explicit stored discriminants,
525
           then it is an untagged record.  If the Corresponding_Discriminant
526
           is not empty then this must be a renamed discriminant and its
527
           Original_Record_Component must point to the corresponding explicit
528
           stored discriminant (i.e. we should have taken the previous
529
           branch).  */
530
        else if (Present (Corresponding_Discriminant (gnat_entity))
531
                 && Is_Tagged_Type (gnat_record))
532
          {
533
            /* A tagged record has no explicit stored discriminants.  */
534
            gcc_assert (First_Discriminant (gnat_record)
535
                       == First_Stored_Discriminant (gnat_record));
536
            gnu_decl
537
              = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
538
                                    gnu_expr, definition);
539
            saved = true;
540
            break;
541
          }
542
 
543
        else if (Present (CR_Discriminant (gnat_entity))
544
                 && type_annotate_only)
545
          {
546
            gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
547
                                           gnu_expr, definition);
548
            saved = true;
549
            break;
550
          }
551
 
552
        /* If the enclosing record has explicit stored discriminants, then
553
           it is an untagged record.  If the Corresponding_Discriminant
554
           is not empty then this must be a renamed discriminant and its
555
           Original_Record_Component must point to the corresponding explicit
556
           stored discriminant (i.e. we should have taken the first
557
           branch).  */
558
        else if (Present (Corresponding_Discriminant (gnat_entity))
559
                 && (First_Discriminant (gnat_record)
560
                     != First_Stored_Discriminant (gnat_record)))
561
          gcc_unreachable ();
562
 
563
        /* Otherwise, if we are not defining this and we have no GCC type
564
           for the containing record, make one for it.  Then we should
565
           have made our own equivalent.  */
566
        else if (!definition && !present_gnu_tree (gnat_record))
567
          {
568
            /* ??? If this is in a record whose scope is a protected
569
               type and we have an Original_Record_Component, use it.
570
               This is a workaround for major problems in protected type
571
               handling.  */
572
            Entity_Id Scop = Scope (Scope (gnat_entity));
573
            if ((Is_Protected_Type (Scop)
574
                 || (Is_Private_Type (Scop)
575
                     && Present (Full_View (Scop))
576
                     && Is_Protected_Type (Full_View (Scop))))
577
                && Present (Original_Record_Component (gnat_entity)))
578
              {
579
                gnu_decl
580
                  = gnat_to_gnu_entity (Original_Record_Component
581
                                        (gnat_entity),
582
                                        gnu_expr, 0);
583
                saved = true;
584
                break;
585
              }
586
 
587
            gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
588
            gnu_decl = get_gnu_tree (gnat_entity);
589
            saved = true;
590
            break;
591
          }
592
 
593
        else
594
          /* Here we have no GCC type and this is a reference rather than a
595
             definition.  This should never happen.  Most likely the cause is
596
             reference before declaration in the gnat tree for gnat_entity.  */
597
          gcc_unreachable ();
598
      }
599
 
600
    case E_Loop_Parameter:
601
    case E_Out_Parameter:
602
    case E_Variable:
603
 
604
      /* Simple variables, loop variables, Out parameters and exceptions.  */
605
    object:
606
      {
607
        bool const_flag
608
          = ((kind == E_Constant || kind == E_Variable)
609
             && Is_True_Constant (gnat_entity)
610
             && !Treat_As_Volatile (gnat_entity)
611
             && (((Nkind (Declaration_Node (gnat_entity))
612
                   == N_Object_Declaration)
613
                  && Present (Expression (Declaration_Node (gnat_entity))))
614
                 || Present (Renamed_Object (gnat_entity))
615
                 || imported_p));
616
        bool inner_const_flag = const_flag;
617
        bool static_p = Is_Statically_Allocated (gnat_entity);
618
        bool mutable_p = false;
619
        bool used_by_ref = false;
620
        tree gnu_ext_name = NULL_TREE;
621
        tree renamed_obj = NULL_TREE;
622
        tree gnu_object_size;
623
 
624
        if (Present (Renamed_Object (gnat_entity)) && !definition)
625
          {
626
            if (kind == E_Exception)
627
              gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
628
                                             NULL_TREE, 0);
629
            else
630
              gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
631
          }
632
 
633
        /* Get the type after elaborating the renamed object.  */
634
        gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
635
 
636
        /* If this is a standard exception definition, then use the standard
637
           exception type.  This is necessary to make sure that imported and
638
           exported views of exceptions are properly merged in LTO mode.  */
639
        if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
640
            && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
641
          gnu_type = except_type_node;
642
 
643
        /* For a debug renaming declaration, build a debug-only entity.  */
644
        if (Present (Debug_Renaming_Link (gnat_entity)))
645
          {
646
            /* Force a non-null value to make sure the symbol is retained.  */
647
            tree value = build1 (INDIRECT_REF, gnu_type,
648
                                 build1 (NOP_EXPR,
649
                                         build_pointer_type (gnu_type),
650
                                         integer_minus_one_node));
651
            gnu_decl = build_decl (input_location,
652
                                   VAR_DECL, gnu_entity_name, gnu_type);
653
            SET_DECL_VALUE_EXPR (gnu_decl, value);
654
            DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
655
            gnat_pushdecl (gnu_decl, gnat_entity);
656
            break;
657
          }
658
 
659
        /* If this is a loop variable, its type should be the base type.
660
           This is because the code for processing a loop determines whether
661
           a normal loop end test can be done by comparing the bounds of the
662
           loop against those of the base type, which is presumed to be the
663
           size used for computation.  But this is not correct when the size
664
           of the subtype is smaller than the type.  */
665
        if (kind == E_Loop_Parameter)
666
          gnu_type = get_base_type (gnu_type);
667
 
668
        /* Reject non-renamed objects whose type is an unconstrained array or
669
           any object whose type is a dummy type or void.  */
670
        if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
671
             && No (Renamed_Object (gnat_entity)))
672
            || TYPE_IS_DUMMY_P (gnu_type)
673
            || TREE_CODE (gnu_type) == VOID_TYPE)
674
          {
675
            gcc_assert (type_annotate_only);
676
            if (this_global)
677
              force_global--;
678
            return error_mark_node;
679
          }
680
 
681
        /* If an alignment is specified, use it if valid.  Note that exceptions
682
           are objects but don't have an alignment.  We must do this before we
683
           validate the size, since the alignment can affect the size.  */
684
        if (kind != E_Exception && Known_Alignment (gnat_entity))
685
          {
686
            gcc_assert (Present (Alignment (gnat_entity)));
687
 
688
            align = validate_alignment (Alignment (gnat_entity), gnat_entity,
689
                                        TYPE_ALIGN (gnu_type));
690
 
691
            /* No point in changing the type if there is an address clause
692
               as the final type of the object will be a reference type.  */
693
            if (Present (Address_Clause (gnat_entity)))
694
              align = 0;
695
            else
696
              {
697
                tree orig_type = gnu_type;
698
 
699
                gnu_type
700
                  = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
701
                                    false, false, definition, true);
702
 
703
                /* If a padding record was made, declare it now since it will
704
                   never be declared otherwise.  This is necessary to ensure
705
                   that its subtrees are properly marked.  */
706
                if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
707
                  create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
708
                                    debug_info_p, gnat_entity);
709
              }
710
          }
711
 
712
        /* If we are defining the object, see if it has a Size and validate it
713
           if so.  If we are not defining the object and a Size clause applies,
714
           simply retrieve the value.  We don't want to ignore the clause and
715
           it is expected to have been validated already.  Then get the new
716
           type, if any.  */
717
        if (definition)
718
          gnu_size = validate_size (Esize (gnat_entity), gnu_type,
719
                                    gnat_entity, VAR_DECL, false,
720
                                    Has_Size_Clause (gnat_entity));
721
        else if (Has_Size_Clause (gnat_entity))
722
          gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
723
 
724
        if (gnu_size)
725
          {
726
            gnu_type
727
              = make_type_from_size (gnu_type, gnu_size,
728
                                     Has_Biased_Representation (gnat_entity));
729
 
730
            if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
731
              gnu_size = NULL_TREE;
732
          }
733
 
734
        /* If this object has self-referential size, it must be a record with
735
           a default discriminant.  We are supposed to allocate an object of
736
           the maximum size in this case, unless it is a constant with an
737
           initializing expression, in which case we can get the size from
738
           that.  Note that the resulting size may still be a variable, so
739
           this may end up with an indirect allocation.  */
740
        if (No (Renamed_Object (gnat_entity))
741
            && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
742
          {
743
            if (gnu_expr && kind == E_Constant)
744
              {
745
                tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
746
                if (CONTAINS_PLACEHOLDER_P (size))
747
                  {
748
                    /* If the initializing expression is itself a constant,
749
                       despite having a nominal type with self-referential
750
                       size, we can get the size directly from it.  */
751
                    if (TREE_CODE (gnu_expr) == COMPONENT_REF
752
                        && TYPE_IS_PADDING_P
753
                           (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
754
                        && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
755
                        && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
756
                            || DECL_READONLY_ONCE_ELAB
757
                               (TREE_OPERAND (gnu_expr, 0))))
758
                      gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
759
                    else
760
                      gnu_size
761
                        = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
762
                  }
763
                else
764
                  gnu_size = size;
765
              }
766
            /* We may have no GNU_EXPR because No_Initialization is
767
               set even though there's an Expression.  */
768
            else if (kind == E_Constant
769
                     && (Nkind (Declaration_Node (gnat_entity))
770
                         == N_Object_Declaration)
771
                     && Present (Expression (Declaration_Node (gnat_entity))))
772
              gnu_size
773
                = TYPE_SIZE (gnat_to_gnu_type
774
                             (Etype
775
                              (Expression (Declaration_Node (gnat_entity)))));
776
            else
777
              {
778
                gnu_size = max_size (TYPE_SIZE (gnu_type), true);
779
                mutable_p = true;
780
              }
781
          }
782
 
783
        /* If the size is zero byte, make it one byte since some linkers have
784
           troubles with zero-sized objects.  If the object will have a
785
           template, that will make it nonzero so don't bother.  Also avoid
786
           doing that for an object renaming or an object with an address
787
           clause, as we would lose useful information on the view size
788
           (e.g. for null array slices) and we are not allocating the object
789
           here anyway.  */
790
        if (((gnu_size
791
              && integer_zerop (gnu_size)
792
              && !TREE_OVERFLOW (gnu_size))
793
             || (TYPE_SIZE (gnu_type)
794
                 && integer_zerop (TYPE_SIZE (gnu_type))
795
                 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
796
            && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
797
                || !Is_Array_Type (Etype (gnat_entity)))
798
            && No (Renamed_Object (gnat_entity))
799
            && No (Address_Clause (gnat_entity)))
800
          gnu_size = bitsize_unit_node;
801
 
802
        /* If this is an object with no specified size and alignment, and
803
           if either it is atomic or we are not optimizing alignment for
804
           space and it is composite and not an exception, an Out parameter
805
           or a reference to another object, and the size of its type is a
806
           constant, set the alignment to the smallest one which is not
807
           smaller than the size, with an appropriate cap.  */
808
        if (!gnu_size && align == 0
809
            && (Is_Atomic (gnat_entity)
810
                || (!Optimize_Alignment_Space (gnat_entity)
811
                    && kind != E_Exception
812
                    && kind != E_Out_Parameter
813
                    && Is_Composite_Type (Etype (gnat_entity))
814
                    && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
815
                    && !Is_Exported (gnat_entity)
816
                    && !imported_p
817
                    && No (Renamed_Object (gnat_entity))
818
                    && No (Address_Clause (gnat_entity))))
819
            && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
820
          {
821
            unsigned int size_cap, align_cap;
822
 
823
            /* No point in promoting the alignment if this doesn't prevent
824
               BLKmode access to the object, in particular block copy, as
825
               this will for example disable the NRV optimization for it.
826
               No point in jumping through all the hoops needed in order
827
               to support BIGGEST_ALIGNMENT if we don't really have to.
828
               So we cap to the smallest alignment that corresponds to
829
               a known efficient memory access pattern of the target.  */
830
            if (Is_Atomic (gnat_entity))
831
              {
832
                size_cap = UINT_MAX;
833
                align_cap = BIGGEST_ALIGNMENT;
834
              }
835
            else
836
              {
837
                size_cap = MAX_FIXED_MODE_SIZE;
838
                align_cap = get_mode_alignment (ptr_mode);
839
              }
840
 
841
            if (!host_integerp (TYPE_SIZE (gnu_type), 1)
842
                || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
843
              align = 0;
844
            else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
845
              align = align_cap;
846
            else
847
              align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
848
 
849
            /* But make sure not to under-align the object.  */
850
            if (align <= TYPE_ALIGN (gnu_type))
851
              align = 0;
852
 
853
            /* And honor the minimum valid atomic alignment, if any.  */
854
#ifdef MINIMUM_ATOMIC_ALIGNMENT
855
            else if (align < MINIMUM_ATOMIC_ALIGNMENT)
856
              align = MINIMUM_ATOMIC_ALIGNMENT;
857
#endif
858
          }
859
 
860
        /* If the object is set to have atomic components, find the component
861
           type and validate it.
862
 
863
           ??? Note that we ignore Has_Volatile_Components on objects; it's
864
           not at all clear what to do in that case.  */
865
        if (Has_Atomic_Components (gnat_entity))
866
          {
867
            tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
868
                              ? TREE_TYPE (gnu_type) : gnu_type);
869
 
870
            while (TREE_CODE (gnu_inner) == ARRAY_TYPE
871
                   && TYPE_MULTI_ARRAY_P (gnu_inner))
872
              gnu_inner = TREE_TYPE (gnu_inner);
873
 
874
            check_ok_for_atomic (gnu_inner, gnat_entity, true);
875
          }
876
 
877
        /* Now check if the type of the object allows atomic access.  Note
878
           that we must test the type, even if this object has size and
879
           alignment to allow such access, because we will be going inside
880
           the padded record to assign to the object.  We could fix this by
881
           always copying via an intermediate value, but it's not clear it's
882
           worth the effort.  */
883
        if (Is_Atomic (gnat_entity))
884
          check_ok_for_atomic (gnu_type, gnat_entity, false);
885
 
886
        /* If this is an aliased object with an unconstrained nominal subtype,
887
           make a type that includes the template.  */
888
        if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
889
            && Is_Array_Type (Etype (gnat_entity))
890
            && !type_annotate_only)
891
          {
892
            tree gnu_array
893
              = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
894
            gnu_type
895
              = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
896
                                                gnu_type,
897
                                                concat_name (gnu_entity_name,
898
                                                             "UNC"),
899
                                                debug_info_p);
900
          }
901
 
902
#ifdef MINIMUM_ATOMIC_ALIGNMENT
903
        /* If the size is a constant and no alignment is specified, force
904
           the alignment to be the minimum valid atomic alignment.  The
905
           restriction on constant size avoids problems with variable-size
906
           temporaries; if the size is variable, there's no issue with
907
           atomic access.  Also don't do this for a constant, since it isn't
908
           necessary and can interfere with constant replacement.  Finally,
909
           do not do it for Out parameters since that creates an
910
           size inconsistency with In parameters.  */
911
        if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
912
            && !FLOAT_TYPE_P (gnu_type)
913
            && !const_flag && No (Renamed_Object (gnat_entity))
914
            && !imported_p && No (Address_Clause (gnat_entity))
915
            && kind != E_Out_Parameter
916
            && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
917
                : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
918
          align = MINIMUM_ATOMIC_ALIGNMENT;
919
#endif
920
 
921
        /* Make a new type with the desired size and alignment, if needed.
922
           But do not take into account alignment promotions to compute the
923
           size of the object.  */
924
        gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
925
        if (gnu_size || align > 0)
926
          {
927
            tree orig_type = gnu_type;
928
 
929
            gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
930
                                       false, false, definition,
931
                                       gnu_size ? true : false);
932
 
933
            /* If a padding record was made, declare it now since it will
934
               never be declared otherwise.  This is necessary to ensure
935
               that its subtrees are properly marked.  */
936
            if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
937
              create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
938
                                debug_info_p, gnat_entity);
939
          }
940
 
941
        /* If this is a renaming, avoid as much as possible to create a new
942
           object.  However, in several cases, creating it is required.
943
           This processing needs to be applied to the raw expression so
944
           as to make it more likely to rename the underlying object.  */
945
        if (Present (Renamed_Object (gnat_entity)))
946
          {
947
            bool create_normal_object = false;
948
 
949
            /* If the renamed object had padding, strip off the reference
950
               to the inner object and reset our type.  */
951
            if ((TREE_CODE (gnu_expr) == COMPONENT_REF
952
                 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
953
                /* Strip useless conversions around the object.  */
954
                || gnat_useless_type_conversion (gnu_expr))
955
              {
956
                gnu_expr = TREE_OPERAND (gnu_expr, 0);
957
                gnu_type = TREE_TYPE (gnu_expr);
958
              }
959
 
960
            /* Case 1: If this is a constant renaming stemming from a function
961
               call, treat it as a normal object whose initial value is what
962
               is being renamed.  RM 3.3 says that the result of evaluating a
963
               function call is a constant object.  As a consequence, it can
964
               be the inner object of a constant renaming.  In this case, the
965
               renaming must be fully instantiated, i.e. it cannot be a mere
966
               reference to (part of) an existing object.  */
967
            if (const_flag)
968
              {
969
                tree inner_object = gnu_expr;
970
                while (handled_component_p (inner_object))
971
                  inner_object = TREE_OPERAND (inner_object, 0);
972
                if (TREE_CODE (inner_object) == CALL_EXPR)
973
                  create_normal_object = true;
974
              }
975
 
976
            /* Otherwise, see if we can proceed with a stabilized version of
977
               the renamed entity or if we need to make a new object.  */
978
            if (!create_normal_object)
979
              {
980
                tree maybe_stable_expr = NULL_TREE;
981
                bool stable = false;
982
 
983
                /* Case 2: If the renaming entity need not be materialized and
984
                   the renamed expression is something we can stabilize, use
985
                   that for the renaming.  At the global level, we can only do
986
                   this if we know no SAVE_EXPRs need be made, because the
987
                   expression we return might be used in arbitrary conditional
988
                   branches so we must force the evaluation of the SAVE_EXPRs
989
                   immediately and this requires a proper function context.
990
                   Note that an external constant is at the global level.  */
991
                if (!Materialize_Entity (gnat_entity)
992
                    && (!((!definition && kind == E_Constant)
993
                          || global_bindings_p ())
994
                        || (staticp (gnu_expr)
995
                            && !TREE_SIDE_EFFECTS (gnu_expr))))
996
                  {
997
                    maybe_stable_expr
998
                      = gnat_stabilize_reference (gnu_expr, true, &stable);
999
 
1000
                    if (stable)
1001
                      {
1002
                        /* ??? No DECL_EXPR is created so we need to mark
1003
                           the expression manually lest it is shared.  */
1004
                        if ((!definition && kind == E_Constant)
1005
                            || global_bindings_p ())
1006
                          MARK_VISITED (maybe_stable_expr);
1007
                        gnu_decl = maybe_stable_expr;
1008
                        save_gnu_tree (gnat_entity, gnu_decl, true);
1009
                        saved = true;
1010
                        annotate_object (gnat_entity, gnu_type, NULL_TREE,
1011
                                         false, false);
1012
                        /* This assertion will fail if the renamed object
1013
                           isn't aligned enough as to make it possible to
1014
                           honor the alignment set on the renaming.  */
1015
                        if (align)
1016
                          {
1017
                            unsigned int renamed_align
1018
                              = DECL_P (gnu_decl)
1019
                                ? DECL_ALIGN (gnu_decl)
1020
                                : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1021
                            gcc_assert (renamed_align >= align);
1022
                          }
1023
                        break;
1024
                      }
1025
 
1026
                    /* The stabilization failed.  Keep maybe_stable_expr
1027
                       untouched here to let the pointer case below know
1028
                       about that failure.  */
1029
                  }
1030
 
1031
                /* Case 3: If this is a constant renaming and creating a
1032
                   new object is allowed and cheap, treat it as a normal
1033
                   object whose initial value is what is being renamed.  */
1034
                if (const_flag
1035
                    && !Is_Composite_Type
1036
                        (Underlying_Type (Etype (gnat_entity))))
1037
                  ;
1038
 
1039
                /* Case 4: Make this into a constant pointer to the object we
1040
                   are to rename and attach the object to the pointer if it is
1041
                   something we can stabilize.
1042
 
1043
                   From the proper scope, attached objects will be referenced
1044
                   directly instead of indirectly via the pointer to avoid
1045
                   subtle aliasing problems with non-addressable entities.
1046
                   They have to be stable because we must not evaluate the
1047
                   variables in the expression every time the renaming is used.
1048
                   The pointer is called a "renaming" pointer in this case.
1049
 
1050
                   In the rare cases where we cannot stabilize the renamed
1051
                   object, we just make a "bare" pointer, and the renamed
1052
                   entity is always accessed indirectly through it.  */
1053
                else
1054
                  {
1055
                    /* We need to preserve the volatileness of the renamed
1056
                       object through the indirection.  */
1057
                    if (TREE_THIS_VOLATILE (gnu_expr)
1058
                        && !TYPE_VOLATILE (gnu_type))
1059
                      gnu_type
1060
                        = build_qualified_type (gnu_type,
1061
                                                (TYPE_QUALS (gnu_type)
1062
                                                 | TYPE_QUAL_VOLATILE));
1063
                    gnu_type = build_reference_type (gnu_type);
1064
                    inner_const_flag = TREE_READONLY (gnu_expr);
1065
                    const_flag = true;
1066
 
1067
                    /* If the previous attempt at stabilizing failed, there
1068
                       is no point in trying again and we reuse the result
1069
                       without attaching it to the pointer.  In this case it
1070
                       will only be used as the initializing expression of
1071
                       the pointer and thus needs no special treatment with
1072
                       regard to multiple evaluations.  */
1073
                    if (maybe_stable_expr)
1074
                      ;
1075
 
1076
                    /* Otherwise, try to stabilize and attach the expression
1077
                       to the pointer if the stabilization succeeds.
1078
 
1079
                       Note that this might introduce SAVE_EXPRs and we don't
1080
                       check whether we're at the global level or not.  This
1081
                       is fine since we are building a pointer initializer and
1082
                       neither the pointer nor the initializing expression can
1083
                       be accessed before the pointer elaboration has taken
1084
                       place in a correct program.
1085
 
1086
                       These SAVE_EXPRs will be evaluated at the right place
1087
                       by either the evaluation of the initializer for the
1088
                       non-global case or the elaboration code for the global
1089
                       case, and will be attached to the elaboration procedure
1090
                       in the latter case.  */
1091
                    else
1092
                     {
1093
                        maybe_stable_expr
1094
                          = gnat_stabilize_reference (gnu_expr, true, &stable);
1095
 
1096
                        if (stable)
1097
                          renamed_obj = maybe_stable_expr;
1098
 
1099
                        /* Attaching is actually performed downstream, as soon
1100
                           as we have a VAR_DECL for the pointer we make.  */
1101
                      }
1102
 
1103
                    gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1104
                                               maybe_stable_expr);
1105
 
1106
                    gnu_size = NULL_TREE;
1107
                    used_by_ref = true;
1108
                  }
1109
              }
1110
          }
1111
 
1112
        /* Make a volatile version of this object's type if we are to make
1113
           the object volatile.  We also interpret 13.3(19) conservatively
1114
           and disallow any optimizations for such a non-constant object.  */
1115
        if ((Treat_As_Volatile (gnat_entity)
1116
             || (!const_flag
1117
                 && gnu_type != except_type_node
1118
                 && (Is_Exported (gnat_entity)
1119
                     || imported_p
1120
                     || Present (Address_Clause (gnat_entity)))))
1121
            && !TYPE_VOLATILE (gnu_type))
1122
          gnu_type = build_qualified_type (gnu_type,
1123
                                           (TYPE_QUALS (gnu_type)
1124
                                            | TYPE_QUAL_VOLATILE));
1125
 
1126
        /* If we are defining an aliased object whose nominal subtype is
1127
           unconstrained, the object is a record that contains both the
1128
           template and the object.  If there is an initializer, it will
1129
           have already been converted to the right type, but we need to
1130
           create the template if there is no initializer.  */
1131
        if (definition
1132
            && !gnu_expr
1133
            && TREE_CODE (gnu_type) == RECORD_TYPE
1134
            && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1135
                /* Beware that padding might have been introduced above.  */
1136
                || (TYPE_PADDING_P (gnu_type)
1137
                    && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1138
                       == RECORD_TYPE
1139
                    && TYPE_CONTAINS_TEMPLATE_P
1140
                       (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1141
          {
1142
            tree template_field
1143
              = TYPE_PADDING_P (gnu_type)
1144
                ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1145
                : TYPE_FIELDS (gnu_type);
1146
            VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
1147
            tree t = build_template (TREE_TYPE (template_field),
1148
                                     TREE_TYPE (DECL_CHAIN (template_field)),
1149
                                     NULL_TREE);
1150
            CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1151
            gnu_expr = gnat_build_constructor (gnu_type, v);
1152
          }
1153
 
1154
        /* Convert the expression to the type of the object except in the
1155
           case where the object's type is unconstrained or the object's type
1156
           is a padded record whose field is of self-referential size.  In
1157
           the former case, converting will generate unnecessary evaluations
1158
           of the CONSTRUCTOR to compute the size and in the latter case, we
1159
           want to only copy the actual data.  Also don't convert to a record
1160
           type with a variant part from a record type without one, to keep
1161
           the object simpler.  */
1162
        if (gnu_expr
1163
            && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1164
            && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1165
            && !(TYPE_IS_PADDING_P (gnu_type)
1166
                 && CONTAINS_PLACEHOLDER_P
1167
                    (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1168
            && !(TREE_CODE (gnu_type) == RECORD_TYPE
1169
                 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1170
                 && get_variant_part (gnu_type) != NULL_TREE
1171
                 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1172
          gnu_expr = convert (gnu_type, gnu_expr);
1173
 
1174
        /* If this is a pointer that doesn't have an initializing expression,
1175
           initialize it to NULL, unless the object is imported.  */
1176
        if (definition
1177
            && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1178
            && !gnu_expr
1179
            && !Is_Imported (gnat_entity))
1180
          gnu_expr = integer_zero_node;
1181
 
1182
        /* If we are defining the object and it has an Address clause, we must
1183
           either get the address expression from the saved GCC tree for the
1184
           object if it has a Freeze node, or elaborate the address expression
1185
           here since the front-end has guaranteed that the elaboration has no
1186
           effects in this case.  */
1187
        if (definition && Present (Address_Clause (gnat_entity)))
1188
          {
1189
            Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1190
            tree gnu_address
1191
              = present_gnu_tree (gnat_entity)
1192
                ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1193
 
1194
            save_gnu_tree (gnat_entity, NULL_TREE, false);
1195
 
1196
            /* Ignore the size.  It's either meaningless or was handled
1197
               above.  */
1198
            gnu_size = NULL_TREE;
1199
            /* Convert the type of the object to a reference type that can
1200
               alias everything as per 13.3(19).  */
1201
            gnu_type
1202
              = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1203
            gnu_address = convert (gnu_type, gnu_address);
1204
            used_by_ref = true;
1205
            const_flag
1206
              = !Is_Public (gnat_entity)
1207
                || compile_time_known_address_p (gnat_expr);
1208
 
1209
            /* If this is a deferred constant, the initializer is attached to
1210
               the full view.  */
1211
            if (kind == E_Constant && Present (Full_View (gnat_entity)))
1212
              gnu_expr
1213
                = gnat_to_gnu
1214
                    (Expression (Declaration_Node (Full_View (gnat_entity))));
1215
 
1216
            /* If we don't have an initializing expression for the underlying
1217
               variable, the initializing expression for the pointer is the
1218
               specified address.  Otherwise, we have to make a COMPOUND_EXPR
1219
               to assign both the address and the initial value.  */
1220
            if (!gnu_expr)
1221
              gnu_expr = gnu_address;
1222
            else
1223
              gnu_expr
1224
                = build2 (COMPOUND_EXPR, gnu_type,
1225
                          build_binary_op
1226
                          (MODIFY_EXPR, NULL_TREE,
1227
                           build_unary_op (INDIRECT_REF, NULL_TREE,
1228
                                           gnu_address),
1229
                           gnu_expr),
1230
                          gnu_address);
1231
          }
1232
 
1233
        /* If it has an address clause and we are not defining it, mark it
1234
           as an indirect object.  Likewise for Stdcall objects that are
1235
           imported.  */
1236
        if ((!definition && Present (Address_Clause (gnat_entity)))
1237
            || (Is_Imported (gnat_entity)
1238
                && Has_Stdcall_Convention (gnat_entity)))
1239
          {
1240
            /* Convert the type of the object to a reference type that can
1241
               alias everything as per 13.3(19).  */
1242
            gnu_type
1243
              = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1244
            gnu_size = NULL_TREE;
1245
 
1246
            /* No point in taking the address of an initializing expression
1247
               that isn't going to be used.  */
1248
            gnu_expr = NULL_TREE;
1249
 
1250
            /* If it has an address clause whose value is known at compile
1251
               time, make the object a CONST_DECL.  This will avoid a
1252
               useless dereference.  */
1253
            if (Present (Address_Clause (gnat_entity)))
1254
              {
1255
                Node_Id gnat_address
1256
                  = Expression (Address_Clause (gnat_entity));
1257
 
1258
                if (compile_time_known_address_p (gnat_address))
1259
                  {
1260
                    gnu_expr = gnat_to_gnu (gnat_address);
1261
                    const_flag = true;
1262
                  }
1263
              }
1264
 
1265
            used_by_ref = true;
1266
          }
1267
 
1268
        /* If we are at top level and this object is of variable size,
1269
           make the actual type a hidden pointer to the real type and
1270
           make the initializer be a memory allocation and initialization.
1271
           Likewise for objects we aren't defining (presumed to be
1272
           external references from other packages), but there we do
1273
           not set up an initialization.
1274
 
1275
           If the object's size overflows, make an allocator too, so that
1276
           Storage_Error gets raised.  Note that we will never free
1277
           such memory, so we presume it never will get allocated.  */
1278
        if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1279
                                 global_bindings_p ()
1280
                                 || !definition
1281
                                 || static_p)
1282
            || (gnu_size && !allocatable_size_p (gnu_size,
1283
                                                 global_bindings_p ()
1284
                                                 || !definition
1285
                                                 || static_p)))
1286
          {
1287
            gnu_type = build_reference_type (gnu_type);
1288
            gnu_size = NULL_TREE;
1289
            used_by_ref = true;
1290
 
1291
            /* In case this was a aliased object whose nominal subtype is
1292
               unconstrained, the pointer above will be a thin pointer and
1293
               build_allocator will automatically make the template.
1294
 
1295
               If we have a template initializer only (that we made above),
1296
               pretend there is none and rely on what build_allocator creates
1297
               again anyway.  Otherwise (if we have a full initializer), get
1298
               the data part and feed that to build_allocator.
1299
 
1300
               If we are elaborating a mutable object, tell build_allocator to
1301
               ignore a possibly simpler size from the initializer, if any, as
1302
               we must allocate the maximum possible size in this case.  */
1303
            if (definition && !imported_p)
1304
              {
1305
                tree gnu_alloc_type = TREE_TYPE (gnu_type);
1306
 
1307
                if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1308
                    && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1309
                  {
1310
                    gnu_alloc_type
1311
                      = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1312
 
1313
                    if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1314
                        && 1 == VEC_length (constructor_elt,
1315
                                            CONSTRUCTOR_ELTS (gnu_expr)))
1316
                      gnu_expr = 0;
1317
                    else
1318
                      gnu_expr
1319
                        = build_component_ref
1320
                            (gnu_expr, NULL_TREE,
1321
                             DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1322
                             false);
1323
                  }
1324
 
1325
                if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1326
                    && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type)))
1327
                  post_error ("?`Storage_Error` will be raised at run time!",
1328
                              gnat_entity);
1329
 
1330
                gnu_expr
1331
                  = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1332
                                     Empty, Empty, gnat_entity, mutable_p);
1333
                const_flag = true;
1334
              }
1335
            else
1336
              {
1337
                gnu_expr = NULL_TREE;
1338
                const_flag = false;
1339
              }
1340
          }
1341
 
1342
        /* If this object would go into the stack and has an alignment larger
1343
           than the largest stack alignment the back-end can honor, resort to
1344
           a variable of "aligning type".  */
1345
        if (!global_bindings_p () && !static_p && definition
1346
            && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1347
          {
1348
            /* Create the new variable.  No need for extra room before the
1349
               aligned field as this is in automatic storage.  */
1350
            tree gnu_new_type
1351
              = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1352
                                    TYPE_SIZE_UNIT (gnu_type),
1353
                                    BIGGEST_ALIGNMENT, 0);
1354
            tree gnu_new_var
1355
              = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1356
                                 NULL_TREE, gnu_new_type, NULL_TREE, false,
1357
                                 false, false, false, NULL, gnat_entity);
1358
 
1359
            /* Initialize the aligned field if we have an initializer.  */
1360
            if (gnu_expr)
1361
              add_stmt_with_node
1362
                (build_binary_op (MODIFY_EXPR, NULL_TREE,
1363
                                  build_component_ref
1364
                                  (gnu_new_var, NULL_TREE,
1365
                                   TYPE_FIELDS (gnu_new_type), false),
1366
                                  gnu_expr),
1367
                 gnat_entity);
1368
 
1369
            /* And setup this entity as a reference to the aligned field.  */
1370
            gnu_type = build_reference_type (gnu_type);
1371
            gnu_expr
1372
              = build_unary_op
1373
                (ADDR_EXPR, gnu_type,
1374
                 build_component_ref (gnu_new_var, NULL_TREE,
1375
                                      TYPE_FIELDS (gnu_new_type), false));
1376
 
1377
            gnu_size = NULL_TREE;
1378
            used_by_ref = true;
1379
            const_flag = true;
1380
          }
1381
 
1382
        /* If this is an aliased object with an unconstrained nominal subtype,
1383
           we make its type a thin reference, i.e. the reference counterpart
1384
           of a thin pointer, so that it points to the array part.  This is
1385
           aimed at making it easier for the debugger to decode the object.
1386
           Note that we have to do that this late because of the couple of
1387
           allocation adjustments that might be made just above.  */
1388
        if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1389
            && Is_Array_Type (Etype (gnat_entity))
1390
            && !type_annotate_only)
1391
          {
1392
            tree gnu_array
1393
              = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1394
 
1395
            /* In case the object with the template has already been allocated
1396
               just above, we have nothing to do here.  */
1397
            if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1398
              {
1399
                gnu_size = NULL_TREE;
1400
                used_by_ref = true;
1401
 
1402
                if (definition && !imported_p)
1403
                  {
1404
                    tree gnu_unc_var
1405
                      = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1406
                                         NULL_TREE, gnu_type, gnu_expr,
1407
                                         const_flag, Is_Public (gnat_entity),
1408
                                         false, static_p, NULL, gnat_entity);
1409
                    gnu_expr
1410
                      = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1411
                    TREE_CONSTANT (gnu_expr) = 1;
1412
                    const_flag = true;
1413
                  }
1414
                else
1415
                  {
1416
                    gnu_expr = NULL_TREE;
1417
                    const_flag = false;
1418
                  }
1419
              }
1420
 
1421
            gnu_type
1422
              = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1423
          }
1424
 
1425
        if (const_flag)
1426
          gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1427
                                                      | TYPE_QUAL_CONST));
1428
 
1429
        /* Convert the expression to the type of the object except in the
1430
           case where the object's type is unconstrained or the object's type
1431
           is a padded record whose field is of self-referential size.  In
1432
           the former case, converting will generate unnecessary evaluations
1433
           of the CONSTRUCTOR to compute the size and in the latter case, we
1434
           want to only copy the actual data.  Also don't convert to a record
1435
           type with a variant part from a record type without one, to keep
1436
           the object simpler.  */
1437
        if (gnu_expr
1438
            && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1439
            && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1440
            && !(TYPE_IS_PADDING_P (gnu_type)
1441
                 && CONTAINS_PLACEHOLDER_P
1442
                    (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1443
            && !(TREE_CODE (gnu_type) == RECORD_TYPE
1444
                 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1445
                 && get_variant_part (gnu_type) != NULL_TREE
1446
                 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1447
          gnu_expr = convert (gnu_type, gnu_expr);
1448
 
1449
        /* If this name is external or there was a name specified, use it,
1450
           unless this is a VMS exception object since this would conflict
1451
           with the symbol we need to export in addition.  Don't use the
1452
           Interface_Name if there is an address clause (see CD30005).  */
1453
        if (!Is_VMS_Exception (gnat_entity)
1454
            && ((Present (Interface_Name (gnat_entity))
1455
                 && No (Address_Clause (gnat_entity)))
1456
                || (Is_Public (gnat_entity)
1457
                    && (!Is_Imported (gnat_entity)
1458
                        || Is_Exported (gnat_entity)))))
1459
          gnu_ext_name = create_concat_name (gnat_entity, NULL);
1460
 
1461
        /* If this is an aggregate constant initialized to a constant, force it
1462
           to be statically allocated.  This saves an initialization copy.  */
1463
        if (!static_p
1464
            && const_flag
1465
            && gnu_expr && TREE_CONSTANT (gnu_expr)
1466
            && AGGREGATE_TYPE_P (gnu_type)
1467
            && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1468
            && !(TYPE_IS_PADDING_P (gnu_type)
1469
                 && !host_integerp (TYPE_SIZE_UNIT
1470
                                    (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1471
          static_p = true;
1472
 
1473
        /* Now create the variable or the constant and set various flags.  */
1474
        gnu_decl
1475
          = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1476
                             gnu_expr, const_flag, Is_Public (gnat_entity),
1477
                             imported_p || !definition, static_p, attr_list,
1478
                             gnat_entity);
1479
        DECL_BY_REF_P (gnu_decl) = used_by_ref;
1480
        DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1481
        DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1482
 
1483
        /* If we are defining an Out parameter and optimization isn't enabled,
1484
           create a fake PARM_DECL for debugging purposes and make it point to
1485
           the VAR_DECL.  Suppress debug info for the latter but make sure it
1486
           will live on the stack so that it can be accessed from within the
1487
           debugger through the PARM_DECL.  */
1488
        if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1489
          {
1490
            tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1491
            gnat_pushdecl (param, gnat_entity);
1492
            SET_DECL_VALUE_EXPR (param, gnu_decl);
1493
            DECL_HAS_VALUE_EXPR_P (param) = 1;
1494
            DECL_IGNORED_P (gnu_decl) = 1;
1495
            TREE_ADDRESSABLE (gnu_decl) = 1;
1496
          }
1497
 
1498
        /* If this is a loop parameter, set the corresponding flag.  */
1499
        else if (kind == E_Loop_Parameter)
1500
          DECL_LOOP_PARM_P (gnu_decl) = 1;
1501
 
1502
        /* If this is a renaming pointer, attach the renamed object to it and
1503
           register it if we are at the global level.  Note that an external
1504
           constant is at the global level.  */
1505
        else if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1506
          {
1507
            SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1508
            if ((!definition && kind == E_Constant) || global_bindings_p ())
1509
              {
1510
                DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1511
                record_global_renaming_pointer (gnu_decl);
1512
              }
1513
          }
1514
 
1515
        /* If this is a constant and we are defining it or it generates a real
1516
           symbol at the object level and we are referencing it, we may want
1517
           or need to have a true variable to represent it:
1518
             - if optimization isn't enabled, for debugging purposes,
1519
             - if the constant is public and not overlaid on something else,
1520
             - if its address is taken,
1521
             - if either itself or its type is aliased.  */
1522
        if (TREE_CODE (gnu_decl) == CONST_DECL
1523
            && (definition || Sloc (gnat_entity) > Standard_Location)
1524
            && ((!optimize && debug_info_p)
1525
                || (Is_Public (gnat_entity)
1526
                    && No (Address_Clause (gnat_entity)))
1527
                || Address_Taken (gnat_entity)
1528
                || Is_Aliased (gnat_entity)
1529
                || Is_Aliased (Etype (gnat_entity))))
1530
          {
1531
            tree gnu_corr_var
1532
              = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1533
                                      gnu_expr, true, Is_Public (gnat_entity),
1534
                                      !definition, static_p, attr_list,
1535
                                      gnat_entity);
1536
 
1537
            SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1538
 
1539
            /* As debugging information will be generated for the variable,
1540
               do not generate debugging information for the constant.  */
1541
            if (debug_info_p)
1542
              DECL_IGNORED_P (gnu_decl) = 1;
1543
            else
1544
              DECL_IGNORED_P (gnu_corr_var) = 1;
1545
          }
1546
 
1547
        /* If this is a constant, even if we don't need a true variable, we
1548
           may need to avoid returning the initializer in every case.  That
1549
           can happen for the address of a (constant) constructor because,
1550
           upon dereferencing it, the constructor will be reinjected in the
1551
           tree, which may not be valid in every case; see lvalue_required_p
1552
           for more details.  */
1553
        if (TREE_CODE (gnu_decl) == CONST_DECL)
1554
          DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1555
 
1556
        /* If this object is declared in a block that contains a block with an
1557
           exception handler, and we aren't using the GCC exception mechanism,
1558
           we must force this variable in memory in order to avoid an invalid
1559
           optimization.  */
1560
        if (Exception_Mechanism != Back_End_Exceptions
1561
            && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1562
          TREE_ADDRESSABLE (gnu_decl) = 1;
1563
 
1564
        /* If we are defining an object with variable size or an object with
1565
           fixed size that will be dynamically allocated, and we are using the
1566
           setjmp/longjmp exception mechanism, update the setjmp buffer.  */
1567
        if (definition
1568
            && Exception_Mechanism == Setjmp_Longjmp
1569
            && get_block_jmpbuf_decl ()
1570
            && DECL_SIZE_UNIT (gnu_decl)
1571
            && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1572
                || (flag_stack_check == GENERIC_STACK_CHECK
1573
                    && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1574
                                         STACK_CHECK_MAX_VAR_SIZE) > 0)))
1575
          add_stmt_with_node (build_call_n_expr
1576
                              (update_setjmp_buf_decl, 1,
1577
                               build_unary_op (ADDR_EXPR, NULL_TREE,
1578
                                               get_block_jmpbuf_decl ())),
1579
                              gnat_entity);
1580
 
1581
        /* Back-annotate Esize and Alignment of the object if not already
1582
           known.  Note that we pick the values of the type, not those of
1583
           the object, to shield ourselves from low-level platform-dependent
1584
           adjustments like alignment promotion.  This is both consistent with
1585
           all the treatment above, where alignment and size are set on the
1586
           type of the object and not on the object directly, and makes it
1587
           possible to support all confirming representation clauses.  */
1588
        annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1589
                         used_by_ref, false);
1590
      }
1591
      break;
1592
 
1593
    case E_Void:
1594
      /* Return a TYPE_DECL for "void" that we previously made.  */
1595
      gnu_decl = TYPE_NAME (void_type_node);
1596
      break;
1597
 
1598
    case E_Enumeration_Type:
1599
      /* A special case: for the types Character and Wide_Character in
1600
         Standard, we do not list all the literals.  So if the literals
1601
         are not specified, make this an unsigned type.  */
1602
      if (No (First_Literal (gnat_entity)))
1603
        {
1604
          gnu_type = make_unsigned_type (esize);
1605
          TYPE_NAME (gnu_type) = gnu_entity_name;
1606
 
1607
          /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1608
             This is needed by the DWARF-2 back-end to distinguish between
1609
             unsigned integer types and character types.  */
1610
          TYPE_STRING_FLAG (gnu_type) = 1;
1611
          break;
1612
        }
1613
 
1614
      {
1615
        /* We have a list of enumeral constants in First_Literal.  We make a
1616
           CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1617
           be placed into TYPE_FIELDS.  Each node in the list is a TREE_LIST
1618
           whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1619
           value of the literal.  But when we have a regular boolean type, we
1620
           simplify this a little by using a BOOLEAN_TYPE.  */
1621
        bool is_boolean = Is_Boolean_Type (gnat_entity)
1622
                          && !Has_Non_Standard_Rep (gnat_entity);
1623
        tree gnu_literal_list = NULL_TREE;
1624
        Entity_Id gnat_literal;
1625
 
1626
        if (Is_Unsigned_Type (gnat_entity))
1627
          gnu_type = make_unsigned_type (esize);
1628
        else
1629
          gnu_type = make_signed_type (esize);
1630
 
1631
        TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1632
 
1633
        for (gnat_literal = First_Literal (gnat_entity);
1634
             Present (gnat_literal);
1635
             gnat_literal = Next_Literal (gnat_literal))
1636
          {
1637
            tree gnu_value
1638
              = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1639
            tree gnu_literal
1640
              = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1641
                                 gnu_type, gnu_value, true, false, false,
1642
                                 false, NULL, gnat_literal);
1643
            /* Do not generate debug info for individual enumerators.  */
1644
            DECL_IGNORED_P (gnu_literal) = 1;
1645
            save_gnu_tree (gnat_literal, gnu_literal, false);
1646
            gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1647
                                          gnu_value, gnu_literal_list);
1648
          }
1649
 
1650
        if (!is_boolean)
1651
          TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1652
 
1653
        /* Note that the bounds are updated at the end of this function
1654
           to avoid an infinite recursion since they refer to the type.  */
1655
      }
1656
      goto discrete_type;
1657
 
1658
    case E_Signed_Integer_Type:
1659
    case E_Ordinary_Fixed_Point_Type:
1660
    case E_Decimal_Fixed_Point_Type:
1661
      /* For integer types, just make a signed type the appropriate number
1662
         of bits.  */
1663
      gnu_type = make_signed_type (esize);
1664
      goto discrete_type;
1665
 
1666
    case E_Modular_Integer_Type:
1667
      {
1668
        /* For modular types, make the unsigned type of the proper number
1669
           of bits and then set up the modulus, if required.  */
1670
        tree gnu_modulus, gnu_high = NULL_TREE;
1671
 
1672
        /* Packed array types are supposed to be subtypes only.  */
1673
        gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1674
 
1675
        gnu_type = make_unsigned_type (esize);
1676
 
1677
        /* Get the modulus in this type.  If it overflows, assume it is because
1678
           it is equal to 2**Esize.  Note that there is no overflow checking
1679
           done on unsigned type, so we detect the overflow by looking for
1680
           a modulus of zero, which is otherwise invalid.  */
1681
        gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1682
 
1683
        if (!integer_zerop (gnu_modulus))
1684
          {
1685
            TYPE_MODULAR_P (gnu_type) = 1;
1686
            SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1687
            gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1688
                                    convert (gnu_type, integer_one_node));
1689
          }
1690
 
1691
        /* If the upper bound is not maximal, make an extra subtype.  */
1692
        if (gnu_high
1693
            && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1694
          {
1695
            tree gnu_subtype = make_unsigned_type (esize);
1696
            SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1697
            TREE_TYPE (gnu_subtype) = gnu_type;
1698
            TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1699
            TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1700
            gnu_type = gnu_subtype;
1701
          }
1702
      }
1703
      goto discrete_type;
1704
 
1705
    case E_Signed_Integer_Subtype:
1706
    case E_Enumeration_Subtype:
1707
    case E_Modular_Integer_Subtype:
1708
    case E_Ordinary_Fixed_Point_Subtype:
1709
    case E_Decimal_Fixed_Point_Subtype:
1710
 
1711
      /* For integral subtypes, we make a new INTEGER_TYPE.  Note that we do
1712
         not want to call create_range_type since we would like each subtype
1713
         node to be distinct.  ??? Historically this was in preparation for
1714
         when memory aliasing is implemented, but that's obsolete now given
1715
         the call to relate_alias_sets below.
1716
 
1717
         The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1718
         this fact is used by the arithmetic conversion functions.
1719
 
1720
         We elaborate the Ancestor_Subtype if it is not in the current unit
1721
         and one of our bounds is non-static.  We do this to ensure consistent
1722
         naming in the case where several subtypes share the same bounds, by
1723
         elaborating the first such subtype first, thus using its name.  */
1724
 
1725
      if (!definition
1726
          && Present (Ancestor_Subtype (gnat_entity))
1727
          && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1728
          && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1729
              || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1730
        gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1731
 
1732
      /* Set the precision to the Esize except for bit-packed arrays.  */
1733
      if (Is_Packed_Array_Type (gnat_entity)
1734
          && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1735
        esize = UI_To_Int (RM_Size (gnat_entity));
1736
 
1737
      /* This should be an unsigned type if the base type is unsigned or
1738
         if the lower bound is constant and non-negative or if the type
1739
         is biased.  */
1740
      if (Is_Unsigned_Type (Etype (gnat_entity))
1741
          || Is_Unsigned_Type (gnat_entity)
1742
          || Has_Biased_Representation (gnat_entity))
1743
        gnu_type = make_unsigned_type (esize);
1744
      else
1745
        gnu_type = make_signed_type (esize);
1746
      TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1747
 
1748
      SET_TYPE_RM_MIN_VALUE
1749
        (gnu_type,
1750
         convert (TREE_TYPE (gnu_type),
1751
                  elaborate_expression (Type_Low_Bound (gnat_entity),
1752
                                        gnat_entity, get_identifier ("L"),
1753
                                        definition, true,
1754
                                        Needs_Debug_Info (gnat_entity))));
1755
 
1756
      SET_TYPE_RM_MAX_VALUE
1757
        (gnu_type,
1758
         convert (TREE_TYPE (gnu_type),
1759
                  elaborate_expression (Type_High_Bound (gnat_entity),
1760
                                        gnat_entity, get_identifier ("U"),
1761
                                        definition, true,
1762
                                        Needs_Debug_Info (gnat_entity))));
1763
 
1764
      /* One of the above calls might have caused us to be elaborated,
1765
         so don't blow up if so.  */
1766
      if (present_gnu_tree (gnat_entity))
1767
        {
1768
          maybe_present = true;
1769
          break;
1770
        }
1771
 
1772
      TYPE_BIASED_REPRESENTATION_P (gnu_type)
1773
        = Has_Biased_Representation (gnat_entity);
1774
 
1775
      /* Attach the TYPE_STUB_DECL in case we have a parallel type.  */
1776
      TYPE_STUB_DECL (gnu_type)
1777
        = create_type_stub_decl (gnu_entity_name, gnu_type);
1778
 
1779
      /* Inherit our alias set from what we're a subtype of.  Subtypes
1780
         are not different types and a pointer can designate any instance
1781
         within a subtype hierarchy.  */
1782
      relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1783
 
1784
      /* For a packed array, make the original array type a parallel type.  */
1785
      if (debug_info_p
1786
          && Is_Packed_Array_Type (gnat_entity)
1787
          && present_gnu_tree (Original_Array_Type (gnat_entity)))
1788
        add_parallel_type (TYPE_STUB_DECL (gnu_type),
1789
                           gnat_to_gnu_type
1790
                           (Original_Array_Type (gnat_entity)));
1791
 
1792
    discrete_type:
1793
 
1794
      /* We have to handle clauses that under-align the type specially.  */
1795
      if ((Present (Alignment_Clause (gnat_entity))
1796
           || (Is_Packed_Array_Type (gnat_entity)
1797
               && Present
1798
                  (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1799
          && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1800
        {
1801
          align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1802
          if (align >= TYPE_ALIGN (gnu_type))
1803
            align = 0;
1804
        }
1805
 
1806
      /* If the type we are dealing with represents a bit-packed array,
1807
         we need to have the bits left justified on big-endian targets
1808
         and right justified on little-endian targets.  We also need to
1809
         ensure that when the value is read (e.g. for comparison of two
1810
         such values), we only get the good bits, since the unused bits
1811
         are uninitialized.  Both goals are accomplished by wrapping up
1812
         the modular type in an enclosing record type.  */
1813
      if (Is_Packed_Array_Type (gnat_entity)
1814
          && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1815
        {
1816
          tree gnu_field_type, gnu_field;
1817
 
1818
          /* Set the RM size before wrapping up the original type.  */
1819
          SET_TYPE_RM_SIZE (gnu_type,
1820
                            UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1821
          TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1822
 
1823
          /* Create a stripped-down declaration, mainly for debugging.  */
1824
          create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1825
                            debug_info_p, gnat_entity);
1826
 
1827
          /* Now save it and build the enclosing record type.  */
1828
          gnu_field_type = gnu_type;
1829
 
1830
          gnu_type = make_node (RECORD_TYPE);
1831
          TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1832
          TYPE_PACKED (gnu_type) = 1;
1833
          TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1834
          TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1835
          SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1836
 
1837
          /* Propagate the alignment of the modular type to the record type,
1838
             unless there is an alignment clause that under-aligns the type.
1839
             This means that bit-packed arrays are given "ceil" alignment for
1840
             their size by default, which may seem counter-intuitive but makes
1841
             it possible to overlay them on modular types easily.  */
1842
          TYPE_ALIGN (gnu_type)
1843
            = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1844
 
1845
          relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1846
 
1847
          /* Don't declare the field as addressable since we won't be taking
1848
             its address and this would prevent create_field_decl from making
1849
             a bitfield.  */
1850
          gnu_field
1851
            = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1852
                                 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1853
 
1854
          /* Do not emit debug info until after the parallel type is added.  */
1855
          finish_record_type (gnu_type, gnu_field, 2, false);
1856
          compute_record_mode (gnu_type);
1857
          TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1858
 
1859
          if (debug_info_p)
1860
            {
1861
              /* Make the original array type a parallel type.  */
1862
              if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1863
                add_parallel_type (TYPE_STUB_DECL (gnu_type),
1864
                                   gnat_to_gnu_type
1865
                                   (Original_Array_Type (gnat_entity)));
1866
 
1867
              rest_of_record_type_compilation (gnu_type);
1868
            }
1869
        }
1870
 
1871
      /* If the type we are dealing with has got a smaller alignment than the
1872
         natural one, we need to wrap it up in a record type and under-align
1873
         the latter.  We reuse the padding machinery for this purpose.  */
1874
      else if (align > 0)
1875
        {
1876
          tree gnu_field_type, gnu_field;
1877
 
1878
          /* Set the RM size before wrapping up the type.  */
1879
          SET_TYPE_RM_SIZE (gnu_type,
1880
                            UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1881
 
1882
          /* Create a stripped-down declaration, mainly for debugging.  */
1883
          create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1884
                            debug_info_p, gnat_entity);
1885
 
1886
          /* Now save it and build the enclosing record type.  */
1887
          gnu_field_type = gnu_type;
1888
 
1889
          gnu_type = make_node (RECORD_TYPE);
1890
          TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1891
          TYPE_PACKED (gnu_type) = 1;
1892
          TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1893
          TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1894
          SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1895
          TYPE_ALIGN (gnu_type) = align;
1896
          relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1897
 
1898
          /* Don't declare the field as addressable since we won't be taking
1899
             its address and this would prevent create_field_decl from making
1900
             a bitfield.  */
1901
          gnu_field
1902
            = create_field_decl (get_identifier ("F"), gnu_field_type,
1903
                                 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1904
 
1905
          finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1906
          compute_record_mode (gnu_type);
1907
          TYPE_PADDING_P (gnu_type) = 1;
1908
        }
1909
 
1910
      break;
1911
 
1912
    case E_Floating_Point_Type:
1913
      /* If this is a VAX floating-point type, use an integer of the proper
1914
         size.  All the operations will be handled with ASM statements.  */
1915
      if (Vax_Float (gnat_entity))
1916
        {
1917
          gnu_type = make_signed_type (esize);
1918
          TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1919
          SET_TYPE_DIGITS_VALUE (gnu_type,
1920
                                 UI_To_gnu (Digits_Value (gnat_entity),
1921
                                            sizetype));
1922
          break;
1923
        }
1924
 
1925
      /* The type of the Low and High bounds can be our type if this is
1926
         a type from Standard, so set them at the end of the function.  */
1927
      gnu_type = make_node (REAL_TYPE);
1928
      TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1929
      layout_type (gnu_type);
1930
      break;
1931
 
1932
    case E_Floating_Point_Subtype:
1933
      if (Vax_Float (gnat_entity))
1934
        {
1935
          gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1936
          break;
1937
        }
1938
 
1939
      {
1940
        if (!definition
1941
            && Present (Ancestor_Subtype (gnat_entity))
1942
            && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1943
            && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1944
                || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1945
          gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1946
                              gnu_expr, 0);
1947
 
1948
        gnu_type = make_node (REAL_TYPE);
1949
        TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1950
        TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1951
        TYPE_GCC_MIN_VALUE (gnu_type)
1952
          = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1953
        TYPE_GCC_MAX_VALUE (gnu_type)
1954
          = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1955
        layout_type (gnu_type);
1956
 
1957
        SET_TYPE_RM_MIN_VALUE
1958
          (gnu_type,
1959
           convert (TREE_TYPE (gnu_type),
1960
                    elaborate_expression (Type_Low_Bound (gnat_entity),
1961
                                          gnat_entity, get_identifier ("L"),
1962
                                          definition, true,
1963
                                          Needs_Debug_Info (gnat_entity))));
1964
 
1965
        SET_TYPE_RM_MAX_VALUE
1966
          (gnu_type,
1967
           convert (TREE_TYPE (gnu_type),
1968
                    elaborate_expression (Type_High_Bound (gnat_entity),
1969
                                          gnat_entity, get_identifier ("U"),
1970
                                          definition, true,
1971
                                          Needs_Debug_Info (gnat_entity))));
1972
 
1973
        /* One of the above calls might have caused us to be elaborated,
1974
           so don't blow up if so.  */
1975
        if (present_gnu_tree (gnat_entity))
1976
          {
1977
            maybe_present = true;
1978
            break;
1979
          }
1980
 
1981
        /* Inherit our alias set from what we're a subtype of, as for
1982
           integer subtypes.  */
1983
        relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1984
      }
1985
    break;
1986
 
1987
      /* Array and String Types and Subtypes
1988
 
1989
         Unconstrained array types are represented by E_Array_Type and
1990
         constrained array types are represented by E_Array_Subtype.  There
1991
         are no actual objects of an unconstrained array type; all we have
1992
         are pointers to that type.
1993
 
1994
         The following fields are defined on array types and subtypes:
1995
 
1996
                Component_Type     Component type of the array.
1997
                Number_Dimensions  Number of dimensions (an int).
1998
                First_Index        Type of first index.  */
1999
 
2000
    case E_String_Type:
2001
    case E_Array_Type:
2002
      {
2003
        const bool convention_fortran_p
2004
          = (Convention (gnat_entity) == Convention_Fortran);
2005
        const int ndim = Number_Dimensions (gnat_entity);
2006
        tree gnu_template_type;
2007
        tree gnu_ptr_template;
2008
        tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2009
        tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2010
        tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2011
        tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2012
        Entity_Id gnat_index, gnat_name;
2013
        int index;
2014
        tree comp_type;
2015
 
2016
        /* Create the type for the component now, as it simplifies breaking
2017
           type reference loops.  */
2018
        comp_type
2019
          = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2020
        if (present_gnu_tree (gnat_entity))
2021
          {
2022
            /* As a side effect, the type may have been translated.  */
2023
            maybe_present = true;
2024
            break;
2025
          }
2026
 
2027
        /* We complete an existing dummy fat pointer type in place.  This both
2028
           avoids further complex adjustments in update_pointer_to and yields
2029
           better debugging information in DWARF by leveraging the support for
2030
           incomplete declarations of "tagged" types in the DWARF back-end.  */
2031
        gnu_type = get_dummy_type (gnat_entity);
2032
        if (gnu_type && TYPE_POINTER_TO (gnu_type))
2033
          {
2034
            gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2035
            TYPE_NAME (gnu_fat_type) = NULL_TREE;
2036
            /* Save the contents of the dummy type for update_pointer_to.  */
2037
            TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2038
            gnu_ptr_template =
2039
              TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2040
            gnu_template_type = TREE_TYPE (gnu_ptr_template);
2041
          }
2042
        else
2043
          {
2044
            gnu_fat_type = make_node (RECORD_TYPE);
2045
            gnu_template_type = make_node (RECORD_TYPE);
2046
            gnu_ptr_template = build_pointer_type (gnu_template_type);
2047
          }
2048
 
2049
        /* Make a node for the array.  If we are not defining the array
2050
           suppress expanding incomplete types.  */
2051
        gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2052
 
2053
        if (!definition)
2054
          {
2055
            defer_incomplete_level++;
2056
            this_deferred = true;
2057
          }
2058
 
2059
        /* Build the fat pointer type.  Use a "void *" object instead of
2060
           a pointer to the array type since we don't have the array type
2061
           yet (it will reference the fat pointer via the bounds).  */
2062
        tem
2063
          = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2064
                               gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2065
        DECL_CHAIN (tem)
2066
          = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2067
                               gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2068
 
2069
        if (COMPLETE_TYPE_P (gnu_fat_type))
2070
          {
2071
            /* We are going to lay it out again so reset the alias set.  */
2072
            alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2073
            TYPE_ALIAS_SET (gnu_fat_type) = -1;
2074
            finish_fat_pointer_type (gnu_fat_type, tem);
2075
            TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2076
            for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2077
              {
2078
                TYPE_FIELDS (t) = tem;
2079
                SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2080
              }
2081
          }
2082
        else
2083
          {
2084
            finish_fat_pointer_type (gnu_fat_type, tem);
2085
            SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2086
          }
2087
 
2088
        /* Build a reference to the template from a PLACEHOLDER_EXPR that
2089
           is the fat pointer.  This will be used to access the individual
2090
           fields once we build them.  */
2091
        tem = build3 (COMPONENT_REF, gnu_ptr_template,
2092
                      build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2093
                      DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2094
        gnu_template_reference
2095
          = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2096
        TREE_READONLY (gnu_template_reference) = 1;
2097
        TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2098
 
2099
        /* Now create the GCC type for each index and add the fields for that
2100
           index to the template.  */
2101
        for (index = (convention_fortran_p ? ndim - 1 : 0),
2102
             gnat_index = First_Index (gnat_entity);
2103
 
2104
             index += (convention_fortran_p ? - 1 : 1),
2105
             gnat_index = Next_Index (gnat_index))
2106
          {
2107
            char field_name[16];
2108
            tree gnu_index_base_type
2109
              = get_unpadded_type (Base_Type (Etype (gnat_index)));
2110
            tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2111
            tree gnu_min, gnu_max, gnu_high;
2112
 
2113
            /* Make the FIELD_DECLs for the low and high bounds of this
2114
               type and then make extractions of these fields from the
2115
               template.  */
2116
            sprintf (field_name, "LB%d", index);
2117
            gnu_lb_field = create_field_decl (get_identifier (field_name),
2118
                                              gnu_index_base_type,
2119
                                              gnu_template_type, NULL_TREE,
2120
                                              NULL_TREE, 0, 0);
2121
            Sloc_to_locus (Sloc (gnat_entity),
2122
                           &DECL_SOURCE_LOCATION (gnu_lb_field));
2123
 
2124
            field_name[0] = 'U';
2125
            gnu_hb_field = create_field_decl (get_identifier (field_name),
2126
                                              gnu_index_base_type,
2127
                                              gnu_template_type, NULL_TREE,
2128
                                              NULL_TREE, 0, 0);
2129
            Sloc_to_locus (Sloc (gnat_entity),
2130
                           &DECL_SOURCE_LOCATION (gnu_hb_field));
2131
 
2132
            gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2133
 
2134
            /* We can't use build_component_ref here since the template type
2135
               isn't complete yet.  */
2136
            gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2137
                                   gnu_template_reference, gnu_lb_field,
2138
                                   NULL_TREE);
2139
            gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2140
                                   gnu_template_reference, gnu_hb_field,
2141
                                   NULL_TREE);
2142
            TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2143
 
2144
            gnu_min = convert (sizetype, gnu_orig_min);
2145
            gnu_max = convert (sizetype, gnu_orig_max);
2146
 
2147
            /* Compute the size of this dimension.  See the E_Array_Subtype
2148
               case below for the rationale.  */
2149
            gnu_high
2150
              = build3 (COND_EXPR, sizetype,
2151
                        build2 (GE_EXPR, boolean_type_node,
2152
                                gnu_orig_max, gnu_orig_min),
2153
                        gnu_max,
2154
                        size_binop (MINUS_EXPR, gnu_min, size_one_node));
2155
 
2156
            /* Make a range type with the new range in the Ada base type.
2157
               Then make an index type with the size range in sizetype.  */
2158
            gnu_index_types[index]
2159
              = create_index_type (gnu_min, gnu_high,
2160
                                   create_range_type (gnu_index_base_type,
2161
                                                      gnu_orig_min,
2162
                                                      gnu_orig_max),
2163
                                   gnat_entity);
2164
 
2165
            /* Update the maximum size of the array in elements.  */
2166
            if (gnu_max_size)
2167
              {
2168
                tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2169
                tree gnu_min
2170
                  = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2171
                tree gnu_max
2172
                  = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2173
                tree gnu_this_max
2174
                  = size_binop (MAX_EXPR,
2175
                                size_binop (PLUS_EXPR, size_one_node,
2176
                                            size_binop (MINUS_EXPR,
2177
                                                        gnu_max, gnu_min)),
2178
                                size_zero_node);
2179
 
2180
                if (TREE_CODE (gnu_this_max) == INTEGER_CST
2181
                    && TREE_OVERFLOW (gnu_this_max))
2182
                  gnu_max_size = NULL_TREE;
2183
                else
2184
                  gnu_max_size
2185
                    = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2186
              }
2187
 
2188
            TYPE_NAME (gnu_index_types[index])
2189
              = create_concat_name (gnat_entity, field_name);
2190
          }
2191
 
2192
        /* Install all the fields into the template.  */
2193
        TYPE_NAME (gnu_template_type)
2194
          = create_concat_name (gnat_entity, "XUB");
2195
        gnu_template_fields = NULL_TREE;
2196
        for (index = 0; index < ndim; index++)
2197
          gnu_template_fields
2198
            = chainon (gnu_template_fields, gnu_temp_fields[index]);
2199
        finish_record_type (gnu_template_type, gnu_template_fields, 0,
2200
                            debug_info_p);
2201
        TYPE_READONLY (gnu_template_type) = 1;
2202
 
2203
        /* Now build the array type.  */
2204
 
2205
        /* If Component_Size is not already specified, annotate it with the
2206
           size of the component.  */
2207
        if (Unknown_Component_Size (gnat_entity))
2208
          Set_Component_Size (gnat_entity,
2209
                              annotate_value (TYPE_SIZE (comp_type)));
2210
 
2211
        /* Compute the maximum size of the array in units and bits.  */
2212
        if (gnu_max_size)
2213
          {
2214
            gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2215
                                            TYPE_SIZE_UNIT (comp_type));
2216
            gnu_max_size = size_binop (MULT_EXPR,
2217
                                       convert (bitsizetype, gnu_max_size),
2218
                                       TYPE_SIZE (comp_type));
2219
          }
2220
        else
2221
          gnu_max_size_unit = NULL_TREE;
2222
 
2223
        /* Now build the array type.  */
2224
        tem = comp_type;
2225
        for (index = ndim - 1; index >= 0; index--)
2226
          {
2227
            tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2228
            TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2229
            if (array_type_has_nonaliased_component (tem, gnat_entity))
2230
              TYPE_NONALIASED_COMPONENT (tem) = 1;
2231
          }
2232
 
2233
        /* If an alignment is specified, use it if valid.  But ignore it
2234
           for the original type of packed array types.  If the alignment
2235
           was requested with an explicit alignment clause, state so.  */
2236
        if (No (Packed_Array_Type (gnat_entity))
2237
            && Known_Alignment (gnat_entity))
2238
          {
2239
            TYPE_ALIGN (tem)
2240
              = validate_alignment (Alignment (gnat_entity), gnat_entity,
2241
                                    TYPE_ALIGN (tem));
2242
            if (Present (Alignment_Clause (gnat_entity)))
2243
              TYPE_USER_ALIGN (tem) = 1;
2244
          }
2245
 
2246
        TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2247
 
2248
        /* Adjust the type of the pointer-to-array field of the fat pointer
2249
           and record the aliasing relationships if necessary.  */
2250
        TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2251
        if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2252
          record_component_aliases (gnu_fat_type);
2253
 
2254
        /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2255
           corresponding fat pointer.  */
2256
        TREE_TYPE (gnu_type) = gnu_fat_type;
2257
        TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2258
        TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2259
        SET_TYPE_MODE (gnu_type, BLKmode);
2260
        TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2261
 
2262
        /* If the maximum size doesn't overflow, use it.  */
2263
        if (gnu_max_size
2264
            && TREE_CODE (gnu_max_size) == INTEGER_CST
2265
            && !TREE_OVERFLOW (gnu_max_size)
2266
            && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2267
            && !TREE_OVERFLOW (gnu_max_size_unit))
2268
          {
2269
            TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2270
                                          TYPE_SIZE (tem));
2271
            TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2272
                                               TYPE_SIZE_UNIT (tem));
2273
          }
2274
 
2275
        create_type_decl (create_concat_name (gnat_entity, "XUA"),
2276
                          tem, NULL, !Comes_From_Source (gnat_entity),
2277
                          debug_info_p, gnat_entity);
2278
 
2279
        /* Give the fat pointer type a name.  If this is a packed type, tell
2280
           the debugger how to interpret the underlying bits.  */
2281
        if (Present (Packed_Array_Type (gnat_entity)))
2282
          gnat_name = Packed_Array_Type (gnat_entity);
2283
        else
2284
          gnat_name = gnat_entity;
2285
        create_type_decl (create_concat_name (gnat_name, "XUP"),
2286
                          gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
2287
                          debug_info_p, gnat_entity);
2288
 
2289
        /* Create the type to be used as what a thin pointer designates:
2290
           a record type for the object and its template with the fields
2291
           shifted to have the template at a negative offset.  */
2292
        tem = build_unc_object_type (gnu_template_type, tem,
2293
                                     create_concat_name (gnat_name, "XUT"),
2294
                                     debug_info_p);
2295
        shift_unc_components_for_thin_pointers (tem);
2296
 
2297
        SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2298
        TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2299
      }
2300
      break;
2301
 
2302
    case E_String_Subtype:
2303
    case E_Array_Subtype:
2304
 
2305
      /* This is the actual data type for array variables.  Multidimensional
2306
         arrays are implemented as arrays of arrays.  Note that arrays which
2307
         have sparse enumeration subtypes as index components create sparse
2308
         arrays, which is obviously space inefficient but so much easier to
2309
         code for now.
2310
 
2311
         Also note that the subtype never refers to the unconstrained array
2312
         type, which is somewhat at variance with Ada semantics.
2313
 
2314
         First check to see if this is simply a renaming of the array type.
2315
         If so, the result is the array type.  */
2316
 
2317
      gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2318
      if (!Is_Constrained (gnat_entity))
2319
        ;
2320
      else
2321
        {
2322
          Entity_Id gnat_index, gnat_base_index;
2323
          const bool convention_fortran_p
2324
            = (Convention (gnat_entity) == Convention_Fortran);
2325
          const int ndim = Number_Dimensions (gnat_entity);
2326
          tree gnu_base_type = gnu_type;
2327
          tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2328
          tree gnu_max_size = size_one_node, gnu_max_size_unit;
2329
          bool need_index_type_struct = false;
2330
          int index;
2331
 
2332
          /* First create the GCC type for each index and find out whether
2333
             special types are needed for debugging information.  */
2334
          for (index = (convention_fortran_p ? ndim - 1 : 0),
2335
               gnat_index = First_Index (gnat_entity),
2336
               gnat_base_index
2337
                 = First_Index (Implementation_Base_Type (gnat_entity));
2338
 
2339
               index += (convention_fortran_p ? - 1 : 1),
2340
               gnat_index = Next_Index (gnat_index),
2341
               gnat_base_index = Next_Index (gnat_base_index))
2342
            {
2343
              tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2344
              tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2345
              tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2346
              tree gnu_min = convert (sizetype, gnu_orig_min);
2347
              tree gnu_max = convert (sizetype, gnu_orig_max);
2348
              tree gnu_base_index_type
2349
                = get_unpadded_type (Etype (gnat_base_index));
2350
              tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2351
              tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2352
              tree gnu_high;
2353
 
2354
              /* See if the base array type is already flat.  If it is, we
2355
                 are probably compiling an ACATS test but it will cause the
2356
                 code below to malfunction if we don't handle it specially.  */
2357
              if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2358
                  && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2359
                  && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2360
                {
2361
                  gnu_min = size_one_node;
2362
                  gnu_max = size_zero_node;
2363
                  gnu_high = gnu_max;
2364
                }
2365
 
2366
              /* Similarly, if one of the values overflows in sizetype and the
2367
                 range is null, use 1..0 for the sizetype bounds.  */
2368
              else if (TREE_CODE (gnu_min) == INTEGER_CST
2369
                       && TREE_CODE (gnu_max) == INTEGER_CST
2370
                       && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2371
                       && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2372
                {
2373
                  gnu_min = size_one_node;
2374
                  gnu_max = size_zero_node;
2375
                  gnu_high = gnu_max;
2376
                }
2377
 
2378
              /* If the minimum and maximum values both overflow in sizetype,
2379
                 but the difference in the original type does not overflow in
2380
                 sizetype, ignore the overflow indication.  */
2381
              else if (TREE_CODE (gnu_min) == INTEGER_CST
2382
                       && TREE_CODE (gnu_max) == INTEGER_CST
2383
                       && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2384
                       && !TREE_OVERFLOW
2385
                           (convert (sizetype,
2386
                                     fold_build2 (MINUS_EXPR, gnu_index_type,
2387
                                                  gnu_orig_max,
2388
                                                  gnu_orig_min))))
2389
                {
2390
                  TREE_OVERFLOW (gnu_min) = 0;
2391
                  TREE_OVERFLOW (gnu_max) = 0;
2392
                  gnu_high = gnu_max;
2393
                }
2394
 
2395
              /* Compute the size of this dimension in the general case.  We
2396
                 need to provide GCC with an upper bound to use but have to
2397
                 deal with the "superflat" case.  There are three ways to do
2398
                 this.  If we can prove that the array can never be superflat,
2399
                 we can just use the high bound of the index type.  */
2400
              else if ((Nkind (gnat_index) == N_Range
2401
                        && cannot_be_superflat_p (gnat_index))
2402
                       /* Packed Array Types are never superflat.  */
2403
                       || Is_Packed_Array_Type (gnat_entity))
2404
                gnu_high = gnu_max;
2405
 
2406
              /* Otherwise, if the high bound is constant but the low bound is
2407
                 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2408
                 lower bound.  Note that the comparison must be done in the
2409
                 original type to avoid any overflow during the conversion.  */
2410
              else if (TREE_CODE (gnu_max) == INTEGER_CST
2411
                       && TREE_CODE (gnu_min) != INTEGER_CST)
2412
                {
2413
                  gnu_high = gnu_max;
2414
                  gnu_min
2415
                    = build_cond_expr (sizetype,
2416
                                       build_binary_op (GE_EXPR,
2417
                                                        boolean_type_node,
2418
                                                        gnu_orig_max,
2419
                                                        gnu_orig_min),
2420
                                       gnu_min,
2421
                                       size_binop (PLUS_EXPR, gnu_max,
2422
                                                   size_one_node));
2423
                }
2424
 
2425
              /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2426
                 in all the other cases.  Note that, here as well as above,
2427
                 the condition used in the comparison must be equivalent to
2428
                 the condition (length != 0).  This is relied upon in order
2429
                 to optimize array comparisons in compare_arrays.  */
2430
              else
2431
                gnu_high
2432
                  = build_cond_expr (sizetype,
2433
                                     build_binary_op (GE_EXPR,
2434
                                                      boolean_type_node,
2435
                                                      gnu_orig_max,
2436
                                                      gnu_orig_min),
2437
                                     gnu_max,
2438
                                     size_binop (MINUS_EXPR, gnu_min,
2439
                                                 size_one_node));
2440
 
2441
              /* Reuse the index type for the range type.  Then make an index
2442
                 type with the size range in sizetype.  */
2443
              gnu_index_types[index]
2444
                = create_index_type (gnu_min, gnu_high, gnu_index_type,
2445
                                     gnat_entity);
2446
 
2447
              /* Update the maximum size of the array in elements.  Here we
2448
                 see if any constraint on the index type of the base type
2449
                 can be used in the case of self-referential bound on the
2450
                 index type of the subtype.  We look for a non-"infinite"
2451
                 and non-self-referential bound from any type involved and
2452
                 handle each bound separately.  */
2453
              if (gnu_max_size)
2454
                {
2455
                  tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2456
                  tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2457
                  tree gnu_base_index_base_type
2458
                    = get_base_type (gnu_base_index_type);
2459
                  tree gnu_base_base_min
2460
                    = convert (sizetype,
2461
                               TYPE_MIN_VALUE (gnu_base_index_base_type));
2462
                  tree gnu_base_base_max
2463
                    = convert (sizetype,
2464
                               TYPE_MAX_VALUE (gnu_base_index_base_type));
2465
 
2466
                  if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2467
                      || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2468
                           && !TREE_OVERFLOW (gnu_base_min)))
2469
                    gnu_base_min = gnu_min;
2470
 
2471
                  if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2472
                      || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2473
                           && !TREE_OVERFLOW (gnu_base_max)))
2474
                    gnu_base_max = gnu_max;
2475
 
2476
                  if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2477
                       && TREE_OVERFLOW (gnu_base_min))
2478
                      || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2479
                      || (TREE_CODE (gnu_base_max) == INTEGER_CST
2480
                          && TREE_OVERFLOW (gnu_base_max))
2481
                      || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2482
                    gnu_max_size = NULL_TREE;
2483
                  else
2484
                    {
2485
                      tree gnu_this_max
2486
                        = size_binop (MAX_EXPR,
2487
                                      size_binop (PLUS_EXPR, size_one_node,
2488
                                                  size_binop (MINUS_EXPR,
2489
                                                              gnu_base_max,
2490
                                                              gnu_base_min)),
2491
                                      size_zero_node);
2492
 
2493
                      if (TREE_CODE (gnu_this_max) == INTEGER_CST
2494
                          && TREE_OVERFLOW (gnu_this_max))
2495
                        gnu_max_size = NULL_TREE;
2496
                      else
2497
                        gnu_max_size
2498
                          = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2499
                    }
2500
                }
2501
 
2502
              /* We need special types for debugging information to point to
2503
                 the index types if they have variable bounds, are not integer
2504
                 types, are biased or are wider than sizetype.  */
2505
              if (!integer_onep (gnu_orig_min)
2506
                  || TREE_CODE (gnu_orig_max) != INTEGER_CST
2507
                  || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2508
                  || (TREE_TYPE (gnu_index_type)
2509
                      && TREE_CODE (TREE_TYPE (gnu_index_type))
2510
                         != INTEGER_TYPE)
2511
                  || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2512
                  || compare_tree_int (rm_size (gnu_index_type),
2513
                                       TYPE_PRECISION (sizetype)) > 0)
2514
                need_index_type_struct = true;
2515
            }
2516
 
2517
          /* Then flatten: create the array of arrays.  For an array type
2518
             used to implement a packed array, get the component type from
2519
             the original array type since the representation clauses that
2520
             can affect it are on the latter.  */
2521
          if (Is_Packed_Array_Type (gnat_entity)
2522
              && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2523
            {
2524
              gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2525
              for (index = ndim - 1; index >= 0; index--)
2526
                gnu_type = TREE_TYPE (gnu_type);
2527
 
2528
              /* One of the above calls might have caused us to be elaborated,
2529
                 so don't blow up if so.  */
2530
              if (present_gnu_tree (gnat_entity))
2531
                {
2532
                  maybe_present = true;
2533
                  break;
2534
                }
2535
            }
2536
          else
2537
            {
2538
              gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2539
                                                     debug_info_p);
2540
 
2541
              /* One of the above calls might have caused us to be elaborated,
2542
                 so don't blow up if so.  */
2543
              if (present_gnu_tree (gnat_entity))
2544
                {
2545
                  maybe_present = true;
2546
                  break;
2547
                }
2548
            }
2549
 
2550
          /* Compute the maximum size of the array in units and bits.  */
2551
          if (gnu_max_size)
2552
            {
2553
              gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2554
                                              TYPE_SIZE_UNIT (gnu_type));
2555
              gnu_max_size = size_binop (MULT_EXPR,
2556
                                         convert (bitsizetype, gnu_max_size),
2557
                                         TYPE_SIZE (gnu_type));
2558
            }
2559
          else
2560
            gnu_max_size_unit = NULL_TREE;
2561
 
2562
          /* Now build the array type.  */
2563
          for (index = ndim - 1; index >= 0; index --)
2564
            {
2565
              gnu_type = build_nonshared_array_type (gnu_type,
2566
                                                     gnu_index_types[index]);
2567
              TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2568
              if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2569
                TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2570
            }
2571
 
2572
          /* Attach the TYPE_STUB_DECL in case we have a parallel type.  */
2573
          TYPE_STUB_DECL (gnu_type)
2574
            = create_type_stub_decl (gnu_entity_name, gnu_type);
2575
 
2576
          /* If we are at file level and this is a multi-dimensional array,
2577
             we need to make a variable corresponding to the stride of the
2578
             inner dimensions.   */
2579
          if (global_bindings_p () && ndim > 1)
2580
            {
2581
              tree gnu_st_name = get_identifier ("ST");
2582
              tree gnu_arr_type;
2583
 
2584
              for (gnu_arr_type = TREE_TYPE (gnu_type);
2585
                   TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2586
                   gnu_arr_type = TREE_TYPE (gnu_arr_type),
2587
                   gnu_st_name = concat_name (gnu_st_name, "ST"))
2588
                {
2589
                  tree eltype = TREE_TYPE (gnu_arr_type);
2590
 
2591
                  TYPE_SIZE (gnu_arr_type)
2592
                    = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2593
                                              gnat_entity, gnu_st_name,
2594
                                              definition, false);
2595
 
2596
                  /* ??? For now, store the size as a multiple of the
2597
                     alignment of the element type in bytes so that we
2598
                     can see the alignment from the tree.  */
2599
                  TYPE_SIZE_UNIT (gnu_arr_type)
2600
                    = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2601
                                              gnat_entity,
2602
                                              concat_name (gnu_st_name, "A_U"),
2603
                                              definition, false,
2604
                                              TYPE_ALIGN (eltype));
2605
 
2606
                  /* ??? create_type_decl is not invoked on the inner types so
2607
                     the MULT_EXPR node built above will never be marked.  */
2608
                  MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2609
                }
2610
            }
2611
 
2612
          /* If we need to write out a record type giving the names of the
2613
             bounds for debugging purposes, do it now and make the record
2614
             type a parallel type.  This is not needed for a packed array
2615
             since the bounds are conveyed by the original array type.  */
2616
          if (need_index_type_struct
2617
              && debug_info_p
2618
              && !Is_Packed_Array_Type (gnat_entity))
2619
            {
2620
              tree gnu_bound_rec = make_node (RECORD_TYPE);
2621
              tree gnu_field_list = NULL_TREE;
2622
              tree gnu_field;
2623
 
2624
              TYPE_NAME (gnu_bound_rec)
2625
                = create_concat_name (gnat_entity, "XA");
2626
 
2627
              for (index = ndim - 1; index >= 0; index--)
2628
                {
2629
                  tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2630
                  tree gnu_index_name = TYPE_NAME (gnu_index);
2631
 
2632
                  if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2633
                    gnu_index_name = DECL_NAME (gnu_index_name);
2634
 
2635
                  /* Make sure to reference the types themselves, and not just
2636
                     their names, as the debugger may fall back on them.  */
2637
                  gnu_field = create_field_decl (gnu_index_name, gnu_index,
2638
                                                 gnu_bound_rec, NULL_TREE,
2639
                                                 NULL_TREE, 0, 0);
2640
                  DECL_CHAIN (gnu_field) = gnu_field_list;
2641
                  gnu_field_list = gnu_field;
2642
                }
2643
 
2644
              finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2645
              add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2646
            }
2647
 
2648
          /* If this is a packed array type, make the original array type a
2649
             parallel type.  Otherwise, do it for the base array type if it
2650
             isn't artificial to make sure it is kept in the debug info.  */
2651
          if (debug_info_p)
2652
            {
2653
              if (Is_Packed_Array_Type (gnat_entity)
2654
                  && present_gnu_tree (Original_Array_Type (gnat_entity)))
2655
                add_parallel_type (TYPE_STUB_DECL (gnu_type),
2656
                                   gnat_to_gnu_type
2657
                                   (Original_Array_Type (gnat_entity)));
2658
              else
2659
                {
2660
                  tree gnu_base_decl
2661
                    = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2662
                  if (!DECL_ARTIFICIAL (gnu_base_decl))
2663
                    add_parallel_type (TYPE_STUB_DECL (gnu_type),
2664
                                       TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2665
                }
2666
            }
2667
 
2668
          TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2669
          TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2670
            = (Is_Packed_Array_Type (gnat_entity)
2671
               && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2672
 
2673
          /* If the size is self-referential and the maximum size doesn't
2674
             overflow, use it.  */
2675
          if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2676
              && gnu_max_size
2677
              && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2678
                   && TREE_OVERFLOW (gnu_max_size))
2679
              && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2680
                   && TREE_OVERFLOW (gnu_max_size_unit)))
2681
            {
2682
              TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2683
                                                 TYPE_SIZE (gnu_type));
2684
              TYPE_SIZE_UNIT (gnu_type)
2685
                = size_binop (MIN_EXPR, gnu_max_size_unit,
2686
                              TYPE_SIZE_UNIT (gnu_type));
2687
            }
2688
 
2689
          /* Set our alias set to that of our base type.  This gives all
2690
             array subtypes the same alias set.  */
2691
          relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2692
 
2693
          /* If this is a packed type, make this type the same as the packed
2694
             array type, but do some adjusting in the type first.  */
2695
          if (Present (Packed_Array_Type (gnat_entity)))
2696
            {
2697
              Entity_Id gnat_index;
2698
              tree gnu_inner;
2699
 
2700
              /* First finish the type we had been making so that we output
2701
                 debugging information for it.  */
2702
              if (Treat_As_Volatile (gnat_entity))
2703
                gnu_type
2704
                  = build_qualified_type (gnu_type,
2705
                                          TYPE_QUALS (gnu_type)
2706
                                          | TYPE_QUAL_VOLATILE);
2707
 
2708
              /* Make it artificial only if the base type was artificial too.
2709
                 That's sort of "morally" true and will make it possible for
2710
                 the debugger to look it up by name in DWARF, which is needed
2711
                 in order to decode the packed array type.  */
2712
              gnu_decl
2713
                = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2714
                                    !Comes_From_Source (Etype (gnat_entity))
2715
                                    && !Comes_From_Source (gnat_entity),
2716
                                    debug_info_p, gnat_entity);
2717
 
2718
              /* Save it as our equivalent in case the call below elaborates
2719
                 this type again.  */
2720
              save_gnu_tree (gnat_entity, gnu_decl, false);
2721
 
2722
              gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2723
                                             NULL_TREE, 0);
2724
              this_made_decl = true;
2725
              gnu_type = TREE_TYPE (gnu_decl);
2726
              save_gnu_tree (gnat_entity, NULL_TREE, false);
2727
 
2728
              gnu_inner = gnu_type;
2729
              while (TREE_CODE (gnu_inner) == RECORD_TYPE
2730
                     && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2731
                         || TYPE_PADDING_P (gnu_inner)))
2732
                gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2733
 
2734
              /* We need to attach the index type to the type we just made so
2735
                 that the actual bounds can later be put into a template.  */
2736
              if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2737
                   && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2738
                  || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2739
                      && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2740
                {
2741
                  if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2742
                    {
2743
                      /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2744
                         TYPE_MODULUS for modular types so we make an extra
2745
                         subtype if necessary.  */
2746
                      if (TYPE_MODULAR_P (gnu_inner))
2747
                        {
2748
                          tree gnu_subtype
2749
                            = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2750
                          TREE_TYPE (gnu_subtype) = gnu_inner;
2751
                          TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2752
                          SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2753
                                                 TYPE_MIN_VALUE (gnu_inner));
2754
                          SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2755
                                                 TYPE_MAX_VALUE (gnu_inner));
2756
                          gnu_inner = gnu_subtype;
2757
                        }
2758
 
2759
                      TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2760
 
2761
#ifdef ENABLE_CHECKING
2762
                      /* Check for other cases of overloading.  */
2763
                      gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2764
#endif
2765
                    }
2766
 
2767
                  for (gnat_index = First_Index (gnat_entity);
2768
                       Present (gnat_index);
2769
                       gnat_index = Next_Index (gnat_index))
2770
                    SET_TYPE_ACTUAL_BOUNDS
2771
                      (gnu_inner,
2772
                       tree_cons (NULL_TREE,
2773
                                  get_unpadded_type (Etype (gnat_index)),
2774
                                  TYPE_ACTUAL_BOUNDS (gnu_inner)));
2775
 
2776
                  if (Convention (gnat_entity) != Convention_Fortran)
2777
                    SET_TYPE_ACTUAL_BOUNDS
2778
                      (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2779
 
2780
                  if (TREE_CODE (gnu_type) == RECORD_TYPE
2781
                      && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2782
                    TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2783
                }
2784
            }
2785
 
2786
          else
2787
            /* Abort if packed array with no Packed_Array_Type field set.  */
2788
            gcc_assert (!Is_Packed (gnat_entity));
2789
        }
2790
      break;
2791
 
2792
    case E_String_Literal_Subtype:
2793
      /* Create the type for a string literal.  */
2794
      {
2795
        Entity_Id gnat_full_type
2796
          = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2797
             && Present (Full_View (Etype (gnat_entity)))
2798
             ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2799
        tree gnu_string_type = get_unpadded_type (gnat_full_type);
2800
        tree gnu_string_array_type
2801
          = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2802
        tree gnu_string_index_type
2803
          = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2804
                                      (TYPE_DOMAIN (gnu_string_array_type))));
2805
        tree gnu_lower_bound
2806
          = convert (gnu_string_index_type,
2807
                     gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2808
        int length = UI_To_Int (String_Literal_Length (gnat_entity));
2809
        tree gnu_length = ssize_int (length - 1);
2810
        tree gnu_upper_bound
2811
          = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2812
                             gnu_lower_bound,
2813
                             convert (gnu_string_index_type, gnu_length));
2814
        tree gnu_index_type
2815
          = create_index_type (convert (sizetype, gnu_lower_bound),
2816
                               convert (sizetype, gnu_upper_bound),
2817
                               create_range_type (gnu_string_index_type,
2818
                                                  gnu_lower_bound,
2819
                                                  gnu_upper_bound),
2820
                               gnat_entity);
2821
 
2822
        gnu_type
2823
          = build_nonshared_array_type (gnat_to_gnu_type
2824
                                        (Component_Type (gnat_entity)),
2825
                                        gnu_index_type);
2826
        if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2827
          TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2828
        relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2829
      }
2830
      break;
2831
 
2832
    /* Record Types and Subtypes
2833
 
2834
       The following fields are defined on record types:
2835
 
2836
                Has_Discriminants       True if the record has discriminants
2837
                First_Discriminant      Points to head of list of discriminants
2838
                First_Entity            Points to head of list of fields
2839
                Is_Tagged_Type          True if the record is tagged
2840
 
2841
       Implementation of Ada records and discriminated records:
2842
 
2843
       A record type definition is transformed into the equivalent of a C
2844
       struct definition.  The fields that are the discriminants which are
2845
       found in the Full_Type_Declaration node and the elements of the
2846
       Component_List found in the Record_Type_Definition node.  The
2847
       Component_List can be a recursive structure since each Variant of
2848
       the Variant_Part of the Component_List has a Component_List.
2849
 
2850
       Processing of a record type definition comprises starting the list of
2851
       field declarations here from the discriminants and the calling the
2852
       function components_to_record to add the rest of the fields from the
2853
       component list and return the gnu type node.  The function
2854
       components_to_record will call itself recursively as it traverses
2855
       the tree.  */
2856
 
2857
    case E_Record_Type:
2858
      if (Has_Complex_Representation (gnat_entity))
2859
        {
2860
          gnu_type
2861
            = build_complex_type
2862
              (get_unpadded_type
2863
               (Etype (Defining_Entity
2864
                       (First (Component_Items
2865
                               (Component_List
2866
                                (Type_Definition
2867
                                 (Declaration_Node (gnat_entity)))))))));
2868
 
2869
          break;
2870
        }
2871
 
2872
      {
2873
        Node_Id full_definition = Declaration_Node (gnat_entity);
2874
        Node_Id record_definition = Type_Definition (full_definition);
2875
        Entity_Id gnat_field;
2876
        tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2877
        /* Set PACKED in keeping with gnat_to_gnu_field.  */
2878
        int packed
2879
          = Is_Packed (gnat_entity)
2880
            ? 1
2881
            : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2882
              ? -1
2883
              : (Known_Alignment (gnat_entity)
2884
                 || (Strict_Alignment (gnat_entity)
2885
                     && Known_RM_Size (gnat_entity)))
2886
                ? -2
2887
                : 0;
2888
        bool has_discr = Has_Discriminants (gnat_entity);
2889
        bool has_rep = Has_Specified_Layout (gnat_entity);
2890
        bool all_rep = has_rep;
2891
        bool is_extension
2892
          = (Is_Tagged_Type (gnat_entity)
2893
             && Nkind (record_definition) == N_Derived_Type_Definition);
2894
        bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2895
 
2896
        /* See if all fields have a rep clause.  Stop when we find one
2897
           that doesn't.  */
2898
        if (all_rep)
2899
          for (gnat_field = First_Entity (gnat_entity);
2900
               Present (gnat_field);
2901
               gnat_field = Next_Entity (gnat_field))
2902
            if ((Ekind (gnat_field) == E_Component
2903
                 || Ekind (gnat_field) == E_Discriminant)
2904
                && No (Component_Clause (gnat_field)))
2905
              {
2906
                all_rep = false;
2907
                break;
2908
              }
2909
 
2910
        /* If this is a record extension, go a level further to find the
2911
           record definition.  Also, verify we have a Parent_Subtype.  */
2912
        if (is_extension)
2913
          {
2914
            if (!type_annotate_only
2915
                || Present (Record_Extension_Part (record_definition)))
2916
              record_definition = Record_Extension_Part (record_definition);
2917
 
2918
            gcc_assert (type_annotate_only
2919
                        || Present (Parent_Subtype (gnat_entity)));
2920
          }
2921
 
2922
        /* Make a node for the record.  If we are not defining the record,
2923
           suppress expanding incomplete types.  */
2924
        gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2925
        TYPE_NAME (gnu_type) = gnu_entity_name;
2926
        TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2927
 
2928
        if (!definition)
2929
          {
2930
            defer_incomplete_level++;
2931
            this_deferred = true;
2932
          }
2933
 
2934
        /* If both a size and rep clause was specified, put the size in
2935
           the record type now so that it can get the proper mode.  */
2936
        if (has_rep && Known_RM_Size (gnat_entity))
2937
          TYPE_SIZE (gnu_type)
2938
            = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2939
 
2940
        /* Always set the alignment here so that it can be used to
2941
           set the mode, if it is making the alignment stricter.  If
2942
           it is invalid, it will be checked again below.  If this is to
2943
           be Atomic, choose a default alignment of a word unless we know
2944
           the size and it's smaller.  */
2945
        if (Known_Alignment (gnat_entity))
2946
          TYPE_ALIGN (gnu_type)
2947
            = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2948
        else if (Is_Atomic (gnat_entity))
2949
          TYPE_ALIGN (gnu_type)
2950
            = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2951
        /* If a type needs strict alignment, the minimum size will be the
2952
           type size instead of the RM size (see validate_size).  Cap the
2953
           alignment, lest it causes this type size to become too large.  */
2954
        else if (Strict_Alignment (gnat_entity)
2955
                 && Known_RM_Size (gnat_entity))
2956
          {
2957
            unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2958
            unsigned int raw_align = raw_size & -raw_size;
2959
            if (raw_align < BIGGEST_ALIGNMENT)
2960
              TYPE_ALIGN (gnu_type) = raw_align;
2961
          }
2962
        else
2963
          TYPE_ALIGN (gnu_type) = 0;
2964
 
2965
        /* If we have a Parent_Subtype, make a field for the parent.  If
2966
           this record has rep clauses, force the position to zero.  */
2967
        if (Present (Parent_Subtype (gnat_entity)))
2968
          {
2969
            Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2970
            tree gnu_parent;
2971
 
2972
            /* A major complexity here is that the parent subtype will
2973
               reference our discriminants in its Discriminant_Constraint
2974
               list.  But those must reference the parent component of this
2975
               record which is of the parent subtype we have not built yet!
2976
               To break the circle we first build a dummy COMPONENT_REF which
2977
               represents the "get to the parent" operation and initialize
2978
               each of those discriminants to a COMPONENT_REF of the above
2979
               dummy parent referencing the corresponding discriminant of the
2980
               base type of the parent subtype.  */
2981
            gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2982
                                     build0 (PLACEHOLDER_EXPR, gnu_type),
2983
                                     build_decl (input_location,
2984
                                                 FIELD_DECL, NULL_TREE,
2985
                                                 void_type_node),
2986
                                     NULL_TREE);
2987
 
2988
            if (has_discr)
2989
              for (gnat_field = First_Stored_Discriminant (gnat_entity);
2990
                   Present (gnat_field);
2991
                   gnat_field = Next_Stored_Discriminant (gnat_field))
2992
                if (Present (Corresponding_Discriminant (gnat_field)))
2993
                  {
2994
                    tree gnu_field
2995
                      = gnat_to_gnu_field_decl (Corresponding_Discriminant
2996
                                                (gnat_field));
2997
                    save_gnu_tree
2998
                      (gnat_field,
2999
                       build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3000
                               gnu_get_parent, gnu_field, NULL_TREE),
3001
                       true);
3002
                  }
3003
 
3004
            /* Then we build the parent subtype.  If it has discriminants but
3005
               the type itself has unknown discriminants, this means that it
3006
               doesn't contain information about how the discriminants are
3007
               derived from those of the ancestor type, so it cannot be used
3008
               directly.  Instead it is built by cloning the parent subtype
3009
               of the underlying record view of the type, for which the above
3010
               derivation of discriminants has been made explicit.  */
3011
            if (Has_Discriminants (gnat_parent)
3012
                && Has_Unknown_Discriminants (gnat_entity))
3013
              {
3014
                Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3015
 
3016
                /* If we are defining the type, the underlying record
3017
                   view must already have been elaborated at this point.
3018
                   Otherwise do it now as its parent subtype cannot be
3019
                   technically elaborated on its own.  */
3020
                if (definition)
3021
                  gcc_assert (present_gnu_tree (gnat_uview));
3022
                else
3023
                  gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3024
 
3025
                gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3026
 
3027
                /* Substitute the "get to the parent" of the type for that
3028
                   of its underlying record view in the cloned type.  */
3029
                for (gnat_field = First_Stored_Discriminant (gnat_uview);
3030
                     Present (gnat_field);
3031
                     gnat_field = Next_Stored_Discriminant (gnat_field))
3032
                  if (Present (Corresponding_Discriminant (gnat_field)))
3033
                    {
3034
                      tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3035
                      tree gnu_ref
3036
                        = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3037
                                  gnu_get_parent, gnu_field, NULL_TREE);
3038
                      gnu_parent
3039
                        = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3040
                    }
3041
              }
3042
            else
3043
              gnu_parent = gnat_to_gnu_type (gnat_parent);
3044
 
3045
            /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3046
               initially built.  The discriminants must reference the fields
3047
               of the parent subtype and not those of its base type for the
3048
               placeholder machinery to properly work.  */
3049
            if (has_discr)
3050
              {
3051
                /* The actual parent subtype is the full view.  */
3052
                if (IN (Ekind (gnat_parent), Private_Kind))
3053
                  {
3054
                    if (Present (Full_View (gnat_parent)))
3055
                      gnat_parent = Full_View (gnat_parent);
3056
                    else
3057
                      gnat_parent = Underlying_Full_View (gnat_parent);
3058
                  }
3059
 
3060
                for (gnat_field = First_Stored_Discriminant (gnat_entity);
3061
                     Present (gnat_field);
3062
                     gnat_field = Next_Stored_Discriminant (gnat_field))
3063
                  if (Present (Corresponding_Discriminant (gnat_field)))
3064
                    {
3065
                      Entity_Id field = Empty;
3066
                      for (field = First_Stored_Discriminant (gnat_parent);
3067
                           Present (field);
3068
                           field = Next_Stored_Discriminant (field))
3069
                        if (same_discriminant_p (gnat_field, field))
3070
                          break;
3071
                      gcc_assert (Present (field));
3072
                      TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3073
                        = gnat_to_gnu_field_decl (field);
3074
                    }
3075
              }
3076
 
3077
            /* The "get to the parent" COMPONENT_REF must be given its
3078
               proper type...  */
3079
            TREE_TYPE (gnu_get_parent) = gnu_parent;
3080
 
3081
            /* ...and reference the _Parent field of this record.  */
3082
            gnu_field
3083
              = create_field_decl (parent_name_id,
3084
                                   gnu_parent, gnu_type,
3085
                                   has_rep
3086
                                   ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3087
                                   has_rep
3088
                                   ? bitsize_zero_node : NULL_TREE,
3089
                                   0, 1);
3090
            DECL_INTERNAL_P (gnu_field) = 1;
3091
            TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3092
            TYPE_FIELDS (gnu_type) = gnu_field;
3093
          }
3094
 
3095
        /* Make the fields for the discriminants and put them into the record
3096
           unless it's an Unchecked_Union.  */
3097
        if (has_discr)
3098
          for (gnat_field = First_Stored_Discriminant (gnat_entity);
3099
               Present (gnat_field);
3100
               gnat_field = Next_Stored_Discriminant (gnat_field))
3101
            {
3102
              /* If this is a record extension and this discriminant is the
3103
                 renaming of another discriminant, we've handled it above.  */
3104
              if (Present (Parent_Subtype (gnat_entity))
3105
                  && Present (Corresponding_Discriminant (gnat_field)))
3106
                continue;
3107
 
3108
              gnu_field
3109
                = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3110
                                     debug_info_p);
3111
 
3112
              /* Make an expression using a PLACEHOLDER_EXPR from the
3113
                 FIELD_DECL node just created and link that with the
3114
                 corresponding GNAT defining identifier.  */
3115
              save_gnu_tree (gnat_field,
3116
                             build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3117
                                     build0 (PLACEHOLDER_EXPR, gnu_type),
3118
                                     gnu_field, NULL_TREE),
3119
                             true);
3120
 
3121
              if (!is_unchecked_union)
3122
                {
3123
                  DECL_CHAIN (gnu_field) = gnu_field_list;
3124
                  gnu_field_list = gnu_field;
3125
                }
3126
            }
3127
 
3128
        /* Add the fields into the record type and finish it up.  */
3129
        components_to_record (gnu_type, Component_List (record_definition),
3130
                              gnu_field_list, packed, definition, false,
3131
                              all_rep, is_unchecked_union,
3132
                              !Comes_From_Source (gnat_entity), debug_info_p,
3133
                              false, OK_To_Reorder_Components (gnat_entity),
3134
                              all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3135
 
3136
        /* If it is passed by reference, force BLKmode to ensure that objects
3137
           of this type will always be put in memory.  */
3138
        if (Is_By_Reference_Type (gnat_entity))
3139
          SET_TYPE_MODE (gnu_type, BLKmode);
3140
 
3141
        /* We used to remove the associations of the discriminants and _Parent
3142
           for validity checking but we may need them if there's a Freeze_Node
3143
           for a subtype used in this record.  */
3144
        TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3145
 
3146
        /* Fill in locations of fields.  */
3147
        annotate_rep (gnat_entity, gnu_type);
3148
 
3149
        /* If there are any entities in the chain corresponding to components
3150
           that we did not elaborate, ensure we elaborate their types if they
3151
           are Itypes.  */
3152
        for (gnat_temp = First_Entity (gnat_entity);
3153
             Present (gnat_temp);
3154
             gnat_temp = Next_Entity (gnat_temp))
3155
          if ((Ekind (gnat_temp) == E_Component
3156
               || Ekind (gnat_temp) == E_Discriminant)
3157
              && Is_Itype (Etype (gnat_temp))
3158
              && !present_gnu_tree (gnat_temp))
3159
            gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3160
 
3161
        /* If this is a record type associated with an exception definition,
3162
           equate its fields to those of the standard exception type.  This
3163
           will make it possible to convert between them.  */
3164
        if (gnu_entity_name == exception_data_name_id)
3165
          {
3166
            tree gnu_std_field;
3167
            for (gnu_field = TYPE_FIELDS (gnu_type),
3168
                 gnu_std_field = TYPE_FIELDS (except_type_node);
3169
                 gnu_field;
3170
                 gnu_field = DECL_CHAIN (gnu_field),
3171
                 gnu_std_field = DECL_CHAIN (gnu_std_field))
3172
              SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3173
            gcc_assert (!gnu_std_field);
3174
          }
3175
      }
3176
      break;
3177
 
3178
    case E_Class_Wide_Subtype:
3179
      /* If an equivalent type is present, that is what we should use.
3180
         Otherwise, fall through to handle this like a record subtype
3181
         since it may have constraints.  */
3182
      if (gnat_equiv_type != gnat_entity)
3183
        {
3184
          gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3185
          maybe_present = true;
3186
          break;
3187
        }
3188
 
3189
      /* ... fall through ... */
3190
 
3191
    case E_Record_Subtype:
3192
      /* If Cloned_Subtype is Present it means this record subtype has
3193
         identical layout to that type or subtype and we should use
3194
         that GCC type for this one.  The front end guarantees that
3195
         the component list is shared.  */
3196
      if (Present (Cloned_Subtype (gnat_entity)))
3197
        {
3198
          gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3199
                                         NULL_TREE, 0);
3200
          maybe_present = true;
3201
          break;
3202
        }
3203
 
3204
      /* Otherwise, first ensure the base type is elaborated.  Then, if we are
3205
         changing the type, make a new type with each field having the type of
3206
         the field in the new subtype but the position computed by transforming
3207
         every discriminant reference according to the constraints.  We don't
3208
         see any difference between private and non-private type here since
3209
         derivations from types should have been deferred until the completion
3210
         of the private type.  */
3211
      else
3212
        {
3213
          Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3214
          tree gnu_base_type;
3215
 
3216
          if (!definition)
3217
            {
3218
              defer_incomplete_level++;
3219
              this_deferred = true;
3220
            }
3221
 
3222
          gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3223
 
3224
          if (present_gnu_tree (gnat_entity))
3225
            {
3226
              maybe_present = true;
3227
              break;
3228
            }
3229
 
3230
          /* If this is a record subtype associated with a dispatch table,
3231
             strip the suffix.  This is necessary to make sure 2 different
3232
             subtypes associated with the imported and exported views of a
3233
             dispatch table are properly merged in LTO mode.  */
3234
          if (Is_Dispatch_Table_Entity (gnat_entity))
3235
            {
3236
              char *p;
3237
              Get_Encoded_Name (gnat_entity);
3238
              p = strchr (Name_Buffer, '_');
3239
              gcc_assert (p);
3240
              strcpy (p+2, "dtS");
3241
              gnu_entity_name = get_identifier (Name_Buffer);
3242
            }
3243
 
3244
          /* When the subtype has discriminants and these discriminants affect
3245
             the initial shape it has inherited, factor them in.  But for an
3246
             Unchecked_Union (it must be an Itype), just return the type.
3247
             We can't just test Is_Constrained because private subtypes without
3248
             discriminants of types with discriminants with default expressions
3249
             are Is_Constrained but aren't constrained!  */
3250
          if (IN (Ekind (gnat_base_type), Record_Kind)
3251
              && !Is_Unchecked_Union (gnat_base_type)
3252
              && !Is_For_Access_Subtype (gnat_entity)
3253
              && Is_Constrained (gnat_entity)
3254
              && Has_Discriminants (gnat_entity)
3255
              && Present (Discriminant_Constraint (gnat_entity))
3256
              && Stored_Constraint (gnat_entity) != No_Elist)
3257
            {
3258
              VEC(subst_pair,heap) *gnu_subst_list
3259
                = build_subst_list (gnat_entity, gnat_base_type, definition);
3260
              tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3261
              tree gnu_pos_list, gnu_field_list = NULL_TREE;
3262
              bool selected_variant = false;
3263
              Entity_Id gnat_field;
3264
              VEC(variant_desc,heap) *gnu_variant_list;
3265
 
3266
              gnu_type = make_node (RECORD_TYPE);
3267
              TYPE_NAME (gnu_type) = gnu_entity_name;
3268
 
3269
              /* Set the size, alignment and alias set of the new type to
3270
                 match that of the old one, doing required substitutions.  */
3271
              copy_and_substitute_in_size (gnu_type, gnu_base_type,
3272
                                           gnu_subst_list);
3273
 
3274
              if (TYPE_IS_PADDING_P (gnu_base_type))
3275
                gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3276
              else
3277
                gnu_unpad_base_type = gnu_base_type;
3278
 
3279
              /* Look for a REP part in the base type.  */
3280
              gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3281
 
3282
              /* Look for a variant part in the base type.  */
3283
              gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3284
 
3285
              /* If there is a variant part, we must compute whether the
3286
                 constraints statically select a particular variant.  If
3287
                 so, we simply drop the qualified union and flatten the
3288
                 list of fields.  Otherwise we'll build a new qualified
3289
                 union for the variants that are still relevant.  */
3290
              if (gnu_variant_part)
3291
                {
3292
                  variant_desc *v;
3293
                  unsigned ix;
3294
 
3295
                  gnu_variant_list
3296
                    = build_variant_list (TREE_TYPE (gnu_variant_part),
3297
                                          gnu_subst_list, NULL);
3298
 
3299
                  /* If all the qualifiers are unconditionally true, the
3300
                     innermost variant is statically selected.  */
3301
                  selected_variant = true;
3302
                  FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3303
                                            ix, v)
3304
                    if (!integer_onep (v->qual))
3305
                      {
3306
                        selected_variant = false;
3307
                        break;
3308
                      }
3309
 
3310
                  /* Otherwise, create the new variants.  */
3311
                  if (!selected_variant)
3312
                    FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3313
                                              ix, v)
3314
                      {
3315
                        tree old_variant = v->type;
3316
                        tree new_variant = make_node (RECORD_TYPE);
3317
                        TYPE_NAME (new_variant)
3318
                          = DECL_NAME (TYPE_NAME (old_variant));
3319
                        copy_and_substitute_in_size (new_variant, old_variant,
3320
                                                     gnu_subst_list);
3321
                        v->record = new_variant;
3322
                      }
3323
                }
3324
              else
3325
                {
3326
                  gnu_variant_list = NULL;
3327
                  selected_variant = false;
3328
                }
3329
 
3330
              gnu_pos_list
3331
                = build_position_list (gnu_unpad_base_type,
3332
                                       gnu_variant_list && !selected_variant,
3333
                                       size_zero_node, bitsize_zero_node,
3334
                                       BIGGEST_ALIGNMENT, NULL_TREE);
3335
 
3336
              for (gnat_field = First_Entity (gnat_entity);
3337
                   Present (gnat_field);
3338
                   gnat_field = Next_Entity (gnat_field))
3339
                if ((Ekind (gnat_field) == E_Component
3340
                     || Ekind (gnat_field) == E_Discriminant)
3341
                    && !(Present (Corresponding_Discriminant (gnat_field))
3342
                         && Is_Tagged_Type (gnat_base_type))
3343
                    && Underlying_Type (Scope (Original_Record_Component
3344
                                               (gnat_field)))
3345
                       == gnat_base_type)
3346
                  {
3347
                    Name_Id gnat_name = Chars (gnat_field);
3348
                    Entity_Id gnat_old_field
3349
                      = Original_Record_Component (gnat_field);
3350
                    tree gnu_old_field
3351
                      = gnat_to_gnu_field_decl (gnat_old_field);
3352
                    tree gnu_context = DECL_CONTEXT (gnu_old_field);
3353
                    tree gnu_field, gnu_field_type, gnu_size;
3354
                    tree gnu_cont_type, gnu_last = NULL_TREE;
3355
 
3356
                    /* If the type is the same, retrieve the GCC type from the
3357
                       old field to take into account possible adjustments.  */
3358
                    if (Etype (gnat_field) == Etype (gnat_old_field))
3359
                      gnu_field_type = TREE_TYPE (gnu_old_field);
3360
                    else
3361
                      gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3362
 
3363
                    /* If there was a component clause, the field types must be
3364
                       the same for the type and subtype, so copy the data from
3365
                       the old field to avoid recomputation here.  Also if the
3366
                       field is justified modular and the optimization in
3367
                       gnat_to_gnu_field was applied.  */
3368
                    if (Present (Component_Clause (gnat_old_field))
3369
                        || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3370
                            && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3371
                            && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3372
                               == TREE_TYPE (gnu_old_field)))
3373
                      {
3374
                        gnu_size = DECL_SIZE (gnu_old_field);
3375
                        gnu_field_type = TREE_TYPE (gnu_old_field);
3376
                      }
3377
 
3378
                    /* If the old field was packed and of constant size, we
3379
                       have to get the old size here, as it might differ from
3380
                       what the Etype conveys and the latter might overlap
3381
                       onto the following field.  Try to arrange the type for
3382
                       possible better packing along the way.  */
3383
                    else if (DECL_PACKED (gnu_old_field)
3384
                             && TREE_CODE (DECL_SIZE (gnu_old_field))
3385
                                == INTEGER_CST)
3386
                      {
3387
                        gnu_size = DECL_SIZE (gnu_old_field);
3388
                        if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3389
                            && !TYPE_FAT_POINTER_P (gnu_field_type)
3390
                            && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3391
                          gnu_field_type
3392
                            = make_packable_type (gnu_field_type, true);
3393
                      }
3394
 
3395
                    else
3396
                      gnu_size = TYPE_SIZE (gnu_field_type);
3397
 
3398
                    /* If the context of the old field is the base type or its
3399
                       REP part (if any), put the field directly in the new
3400
                       type; otherwise look up the context in the variant list
3401
                       and put the field either in the new type if there is a
3402
                       selected variant or in one of the new variants.  */
3403
                    if (gnu_context == gnu_unpad_base_type
3404
                        || (gnu_rep_part
3405
                            && gnu_context == TREE_TYPE (gnu_rep_part)))
3406
                      gnu_cont_type = gnu_type;
3407
                    else
3408
                      {
3409
                        variant_desc *v;
3410
                        unsigned ix;
3411
 
3412
                        t = NULL_TREE;
3413
                        FOR_EACH_VEC_ELT_REVERSE (variant_desc,
3414
                                                  gnu_variant_list, ix, v)
3415
                          if (v->type == gnu_context)
3416
                            {
3417
                              t = v->type;
3418
                              break;
3419
                            }
3420
                        if (t)
3421
                          {
3422
                            if (selected_variant)
3423
                              gnu_cont_type = gnu_type;
3424
                            else
3425
                              gnu_cont_type = v->record;
3426
                          }
3427
                        else
3428
                          /* The front-end may pass us "ghost" components if
3429
                             it fails to recognize that a constrained subtype
3430
                             is statically constrained.  Discard them.  */
3431
                          continue;
3432
                      }
3433
 
3434
                    /* Now create the new field modeled on the old one.  */
3435
                    gnu_field
3436
                      = create_field_decl_from (gnu_old_field, gnu_field_type,
3437
                                                gnu_cont_type, gnu_size,
3438
                                                gnu_pos_list, gnu_subst_list);
3439
 
3440
                    /* Put it in one of the new variants directly.  */
3441
                    if (gnu_cont_type != gnu_type)
3442
                      {
3443
                        DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3444
                        TYPE_FIELDS (gnu_cont_type) = gnu_field;
3445
                      }
3446
 
3447
                    /* To match the layout crafted in components_to_record,
3448
                       if this is the _Tag or _Parent field, put it before
3449
                       any other fields.  */
3450
                    else if (gnat_name == Name_uTag
3451
                             || gnat_name == Name_uParent)
3452
                      gnu_field_list = chainon (gnu_field_list, gnu_field);
3453
 
3454
                    /* Similarly, if this is the _Controller field, put
3455
                       it before the other fields except for the _Tag or
3456
                       _Parent field.  */
3457
                    else if (gnat_name == Name_uController && gnu_last)
3458
                      {
3459
                        DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3460
                        DECL_CHAIN (gnu_last) = gnu_field;
3461
                      }
3462
 
3463
                    /* Otherwise, if this is a regular field, put it after
3464
                       the other fields.  */
3465
                    else
3466
                      {
3467
                        DECL_CHAIN (gnu_field) = gnu_field_list;
3468
                        gnu_field_list = gnu_field;
3469
                        if (!gnu_last)
3470
                          gnu_last = gnu_field;
3471
                      }
3472
 
3473
                    save_gnu_tree (gnat_field, gnu_field, false);
3474
                  }
3475
 
3476
              /* If there is a variant list and no selected variant, we need
3477
                 to create the nest of variant parts from the old nest.  */
3478
              if (gnu_variant_list && !selected_variant)
3479
                {
3480
                  tree new_variant_part
3481
                    = create_variant_part_from (gnu_variant_part,
3482
                                                gnu_variant_list, gnu_type,
3483
                                                gnu_pos_list, gnu_subst_list);
3484
                  DECL_CHAIN (new_variant_part) = gnu_field_list;
3485
                  gnu_field_list = new_variant_part;
3486
                }
3487
 
3488
              /* Now go through the entities again looking for Itypes that
3489
                 we have not elaborated but should (e.g., Etypes of fields
3490
                 that have Original_Components).  */
3491
              for (gnat_field = First_Entity (gnat_entity);
3492
                   Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3493
                if ((Ekind (gnat_field) == E_Discriminant
3494
                     || Ekind (gnat_field) == E_Component)
3495
                    && !present_gnu_tree (Etype (gnat_field)))
3496
                  gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3497
 
3498
              /* Do not emit debug info for the type yet since we're going to
3499
                 modify it below.  */
3500
              gnu_field_list = nreverse (gnu_field_list);
3501
              finish_record_type (gnu_type, gnu_field_list, 2, false);
3502
 
3503
              /* See the E_Record_Type case for the rationale.  */
3504
              if (Is_By_Reference_Type (gnat_entity))
3505
                SET_TYPE_MODE (gnu_type, BLKmode);
3506
              else
3507
                compute_record_mode (gnu_type);
3508
 
3509
              TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3510
 
3511
              /* Fill in locations of fields.  */
3512
              annotate_rep (gnat_entity, gnu_type);
3513
 
3514
              /* If debugging information is being written for the type, write
3515
                 a record that shows what we are a subtype of and also make a
3516
                 variable that indicates our size, if still variable.  */
3517
              if (debug_info_p)
3518
                {
3519
                  tree gnu_subtype_marker = make_node (RECORD_TYPE);
3520
                  tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3521
                  tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3522
 
3523
                  if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3524
                    gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3525
 
3526
                  TYPE_NAME (gnu_subtype_marker)
3527
                    = create_concat_name (gnat_entity, "XVS");
3528
                  finish_record_type (gnu_subtype_marker,
3529
                                      create_field_decl (gnu_unpad_base_name,
3530
                                                         build_reference_type
3531
                                                         (gnu_unpad_base_type),
3532
                                                         gnu_subtype_marker,
3533
                                                         NULL_TREE, NULL_TREE,
3534
                                                         0, 0),
3535
                                      0, true);
3536
 
3537
                  add_parallel_type (TYPE_STUB_DECL (gnu_type),
3538
                                     gnu_subtype_marker);
3539
 
3540
                  if (definition
3541
                      && TREE_CODE (gnu_size_unit) != INTEGER_CST
3542
                      && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3543
                    TYPE_SIZE_UNIT (gnu_subtype_marker)
3544
                      = create_var_decl (create_concat_name (gnat_entity,
3545
                                                             "XVZ"),
3546
                                         NULL_TREE, sizetype, gnu_size_unit,
3547
                                         false, false, false, false, NULL,
3548
                                         gnat_entity);
3549
                }
3550
 
3551
              VEC_free (variant_desc, heap, gnu_variant_list);
3552
              VEC_free (subst_pair, heap, gnu_subst_list);
3553
 
3554
              /* Now we can finalize it.  */
3555
              rest_of_record_type_compilation (gnu_type);
3556
            }
3557
 
3558
          /* Otherwise, go down all the components in the new type and make
3559
             them equivalent to those in the base type.  */
3560
          else
3561
            {
3562
              gnu_type = gnu_base_type;
3563
 
3564
              for (gnat_temp = First_Entity (gnat_entity);
3565
                   Present (gnat_temp);
3566
                   gnat_temp = Next_Entity (gnat_temp))
3567
                if ((Ekind (gnat_temp) == E_Discriminant
3568
                     && !Is_Unchecked_Union (gnat_base_type))
3569
                    || Ekind (gnat_temp) == E_Component)
3570
                  save_gnu_tree (gnat_temp,
3571
                                 gnat_to_gnu_field_decl
3572
                                 (Original_Record_Component (gnat_temp)),
3573
                                 false);
3574
            }
3575
        }
3576
      break;
3577
 
3578
    case E_Access_Subprogram_Type:
3579
      /* Use the special descriptor type for dispatch tables if needed,
3580
         that is to say for the Prim_Ptr of a-tags.ads and its clones.
3581
         Note that we are only required to do so for static tables in
3582
         order to be compatible with the C++ ABI, but Ada 2005 allows
3583
         to extend library level tagged types at the local level so
3584
         we do it in the non-static case as well.  */
3585
      if (TARGET_VTABLE_USES_DESCRIPTORS
3586
          && Is_Dispatch_Table_Entity (gnat_entity))
3587
        {
3588
            gnu_type = fdesc_type_node;
3589
            gnu_size = TYPE_SIZE (gnu_type);
3590
            break;
3591
        }
3592
 
3593
      /* ... fall through ... */
3594
 
3595
    case E_Anonymous_Access_Subprogram_Type:
3596
      /* If we are not defining this entity, and we have incomplete
3597
         entities being processed above us, make a dummy type and
3598
         fill it in later.  */
3599
      if (!definition && defer_incomplete_level != 0)
3600
        {
3601
          struct incomplete *p = XNEW (struct incomplete);
3602
 
3603
          gnu_type
3604
            = build_pointer_type
3605
              (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3606
          gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3607
                                       !Comes_From_Source (gnat_entity),
3608
                                       debug_info_p, gnat_entity);
3609
          this_made_decl = true;
3610
          gnu_type = TREE_TYPE (gnu_decl);
3611
          save_gnu_tree (gnat_entity, gnu_decl, false);
3612
          saved = true;
3613
 
3614
          p->old_type = TREE_TYPE (gnu_type);
3615
          p->full_type = Directly_Designated_Type (gnat_entity);
3616
          p->next = defer_incomplete_list;
3617
          defer_incomplete_list = p;
3618
          break;
3619
        }
3620
 
3621
      /* ... fall through ... */
3622
 
3623
    case E_Allocator_Type:
3624
    case E_Access_Type:
3625
    case E_Access_Attribute_Type:
3626
    case E_Anonymous_Access_Type:
3627
    case E_General_Access_Type:
3628
      {
3629
        /* The designated type and its equivalent type for gigi.  */
3630
        Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3631
        Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3632
        /* Whether it comes from a limited with.  */
3633
        bool is_from_limited_with
3634
          = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3635
             && From_With_Type (gnat_desig_equiv));
3636
        /* The "full view" of the designated type.  If this is an incomplete
3637
           entity from a limited with, treat its non-limited view as the full
3638
           view.  Otherwise, if this is an incomplete or private type, use the
3639
           full view.  In the former case, we might point to a private type,
3640
           in which case, we need its full view.  Also, we want to look at the
3641
           actual type used for the representation, so this takes a total of
3642
           three steps.  */
3643
        Entity_Id gnat_desig_full_direct_first
3644
          = (is_from_limited_with
3645
             ? Non_Limited_View (gnat_desig_equiv)
3646
             : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3647
                ? Full_View (gnat_desig_equiv) : Empty));
3648
        Entity_Id gnat_desig_full_direct
3649
          = ((is_from_limited_with
3650
              && Present (gnat_desig_full_direct_first)
3651
              && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3652
             ? Full_View (gnat_desig_full_direct_first)
3653
             : gnat_desig_full_direct_first);
3654
        Entity_Id gnat_desig_full
3655
          = Gigi_Equivalent_Type (gnat_desig_full_direct);
3656
        /* The type actually used to represent the designated type, either
3657
           gnat_desig_full or gnat_desig_equiv.  */
3658
        Entity_Id gnat_desig_rep;
3659
        /* True if this is a pointer to an unconstrained array.  */
3660
        bool is_unconstrained_array;
3661
        /* We want to know if we'll be seeing the freeze node for any
3662
           incomplete type we may be pointing to.  */
3663
        bool in_main_unit
3664
          = (Present (gnat_desig_full)
3665
             ? In_Extended_Main_Code_Unit (gnat_desig_full)
3666
             : In_Extended_Main_Code_Unit (gnat_desig_type));
3667
        /* True if we make a dummy type here.  */
3668
        bool made_dummy = false;
3669
        /* The mode to be used for the pointer type.  */
3670
        enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3671
        /* The GCC type used for the designated type.  */
3672
        tree gnu_desig_type = NULL_TREE;
3673
 
3674
        if (!targetm.valid_pointer_mode (p_mode))
3675
          p_mode = ptr_mode;
3676
 
3677
        /* If either the designated type or its full view is an unconstrained
3678
           array subtype, replace it with the type it's a subtype of.  This
3679
           avoids problems with multiple copies of unconstrained array types.
3680
           Likewise, if the designated type is a subtype of an incomplete
3681
           record type, use the parent type to avoid order of elaboration
3682
           issues.  This can lose some code efficiency, but there is no
3683
           alternative.  */
3684
        if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3685
            && !Is_Constrained (gnat_desig_equiv))
3686
          gnat_desig_equiv = Etype (gnat_desig_equiv);
3687
        if (Present (gnat_desig_full)
3688
            && ((Ekind (gnat_desig_full) == E_Array_Subtype
3689
                 && !Is_Constrained (gnat_desig_full))
3690
                || (Ekind (gnat_desig_full) == E_Record_Subtype
3691
                    && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3692
          gnat_desig_full = Etype (gnat_desig_full);
3693
 
3694
        /* Set the type that's actually the representation of the designated
3695
           type and also flag whether we have a unconstrained array.  */
3696
        gnat_desig_rep
3697
          = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3698
        is_unconstrained_array
3699
          = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3700
 
3701
        /* If we are pointing to an incomplete type whose completion is an
3702
           unconstrained array, make dummy fat and thin pointer types to it.
3703
           Likewise if the type itself is dummy or an unconstrained array.  */
3704
        if (is_unconstrained_array
3705
            && (Present (gnat_desig_full)
3706
                || (present_gnu_tree (gnat_desig_equiv)
3707
                    && TYPE_IS_DUMMY_P
3708
                       (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3709
                || (!in_main_unit
3710
                    && defer_incomplete_level != 0
3711
                    && !present_gnu_tree (gnat_desig_equiv))
3712
                || (in_main_unit
3713
                    && is_from_limited_with
3714
                    && Present (Freeze_Node (gnat_desig_equiv)))))
3715
          {
3716
            if (present_gnu_tree (gnat_desig_rep))
3717
              gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3718
            else
3719
              {
3720
                gnu_desig_type = make_dummy_type (gnat_desig_rep);
3721
                made_dummy = true;
3722
              }
3723
 
3724
            /* If the call above got something that has a pointer, the pointer
3725
               is our type.  This could have happened either because the type
3726
               was elaborated or because somebody else executed the code.  */
3727
            if (!TYPE_POINTER_TO (gnu_desig_type))
3728
              build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3729
            gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3730
          }
3731
 
3732
        /* If we already know what the full type is, use it.  */
3733
        else if (Present (gnat_desig_full)
3734
                 && present_gnu_tree (gnat_desig_full))
3735
          gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3736
 
3737
        /* Get the type of the thing we are to point to and build a pointer to
3738
           it.  If it is a reference to an incomplete or private type with a
3739
           full view that is a record, make a dummy type node and get the
3740
           actual type later when we have verified it is safe.  */
3741
        else if ((!in_main_unit
3742
                  && !present_gnu_tree (gnat_desig_equiv)
3743
                  && Present (gnat_desig_full)
3744
                  && !present_gnu_tree (gnat_desig_full)
3745
                  && Is_Record_Type (gnat_desig_full))
3746
                 /* Likewise if we are pointing to a record or array and we are
3747
                    to defer elaborating incomplete types.  We do this as this
3748
                    access type may be the full view of a private type.  Note
3749
                    that the unconstrained array case is handled above.  */
3750
                 || ((!in_main_unit || imported_p)
3751
                     && defer_incomplete_level != 0
3752
                     && !present_gnu_tree (gnat_desig_equiv)
3753
                     && (Is_Record_Type (gnat_desig_rep)
3754
                         || Is_Array_Type (gnat_desig_rep)))
3755
                 /* If this is a reference from a limited_with type back to our
3756
                    main unit and there's a freeze node for it, either we have
3757
                    already processed the declaration and made the dummy type,
3758
                    in which case we just reuse the latter, or we have not yet,
3759
                    in which case we make the dummy type and it will be reused
3760
                    when the declaration is finally processed.  In both cases,
3761
                    the pointer eventually created below will be automatically
3762
                    adjusted when the freeze node is processed.  Note that the
3763
                    unconstrained array case is handled above.  */
3764
                 ||  (in_main_unit
3765
                      && is_from_limited_with
3766
                      && Present (Freeze_Node (gnat_desig_rep))))
3767
          {
3768
            gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3769
            made_dummy = true;
3770
          }
3771
 
3772
        /* Otherwise handle the case of a pointer to itself.  */
3773
        else if (gnat_desig_equiv == gnat_entity)
3774
          {
3775
            gnu_type
3776
              = build_pointer_type_for_mode (void_type_node, p_mode,
3777
                                             No_Strict_Aliasing (gnat_entity));
3778
            TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3779
          }
3780
 
3781
        /* If expansion is disabled, the equivalent type of a concurrent type
3782
           is absent, so build a dummy pointer type.  */
3783
        else if (type_annotate_only && No (gnat_desig_equiv))
3784
          gnu_type = ptr_void_type_node;
3785
 
3786
        /* Finally, handle the default case where we can just elaborate our
3787
           designated type.  */
3788
        else
3789
          gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3790
 
3791
        /* It is possible that a call to gnat_to_gnu_type above resolved our
3792
           type.  If so, just return it.  */
3793
        if (present_gnu_tree (gnat_entity))
3794
          {
3795
            maybe_present = true;
3796
            break;
3797
          }
3798
 
3799
        /* If we haven't done it yet, build the pointer type the usual way.  */
3800
        if (!gnu_type)
3801
          {
3802
            /* Modify the designated type if we are pointing only to constant
3803
               objects, but don't do it for unconstrained arrays.  */
3804
            if (Is_Access_Constant (gnat_entity)
3805
                && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3806
              {
3807
                gnu_desig_type
3808
                  = build_qualified_type
3809
                    (gnu_desig_type,
3810
                     TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3811
 
3812
                /* Some extra processing is required if we are building a
3813
                   pointer to an incomplete type (in the GCC sense).  We might
3814
                   have such a type if we just made a dummy, or directly out
3815
                   of the call to gnat_to_gnu_type above if we are processing
3816
                   an access type for a record component designating the
3817
                   record type itself.  */
3818
                if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3819
                  {
3820
                    /* We must ensure that the pointer to variant we make will
3821
                       be processed by update_pointer_to when the initial type
3822
                       is completed.  Pretend we made a dummy and let further
3823
                       processing act as usual.  */
3824
                    made_dummy = true;
3825
 
3826
                    /* We must ensure that update_pointer_to will not retrieve
3827
                       the dummy variant when building a properly qualified
3828
                       version of the complete type.  We take advantage of the
3829
                       fact that get_qualified_type is requiring TYPE_NAMEs to
3830
                       match to influence build_qualified_type and then also
3831
                       update_pointer_to here.  */
3832
                    TYPE_NAME (gnu_desig_type)
3833
                      = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3834
                  }
3835
              }
3836
 
3837
            gnu_type
3838
              = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3839
                                             No_Strict_Aliasing (gnat_entity));
3840
          }
3841
 
3842
        /* If we are not defining this object and we have made a dummy pointer,
3843
           save our current definition, evaluate the actual type, and replace
3844
           the tentative type we made with the actual one.  If we are to defer
3845
           actually looking up the actual type, make an entry in the deferred
3846
           list.  If this is from a limited with, we may have to defer to the
3847
           end of the current unit.  */
3848
        if ((!in_main_unit || is_from_limited_with) && made_dummy)
3849
          {
3850
            tree gnu_old_desig_type;
3851
 
3852
            if (TYPE_IS_FAT_POINTER_P (gnu_type))
3853
              {
3854
                gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3855
                if (esize == POINTER_SIZE)
3856
                  gnu_type = build_pointer_type
3857
                             (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3858
              }
3859
            else
3860
              gnu_old_desig_type = TREE_TYPE (gnu_type);
3861
 
3862
            gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3863
                                         !Comes_From_Source (gnat_entity),
3864
                                         debug_info_p, gnat_entity);
3865
            this_made_decl = true;
3866
            gnu_type = TREE_TYPE (gnu_decl);
3867
            save_gnu_tree (gnat_entity, gnu_decl, false);
3868
            saved = true;
3869
 
3870
            /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3871
               update gnu_old_desig_type directly, in which case it will not be
3872
               a dummy type any more when we get into update_pointer_to.
3873
 
3874
               This can happen e.g. when the designated type is a record type,
3875
               because their elaboration starts with an initial node from
3876
               make_dummy_type, which may be the same node as the one we got.
3877
 
3878
               Besides, variants of this non-dummy type might have been created
3879
               along the way.  update_pointer_to is expected to properly take
3880
               care of those situations.  */
3881
            if (defer_incomplete_level == 0 && !is_from_limited_with)
3882
              {
3883
                defer_finalize_level++;
3884
                update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3885
                                   gnat_to_gnu_type (gnat_desig_equiv));
3886
                defer_finalize_level--;
3887
              }
3888
            else
3889
              {
3890
                struct incomplete *p = XNEW (struct incomplete);
3891
                struct incomplete **head
3892
                  = (is_from_limited_with
3893
                     ? &defer_limited_with : &defer_incomplete_list);
3894
                p->old_type = gnu_old_desig_type;
3895
                p->full_type = gnat_desig_equiv;
3896
                p->next = *head;
3897
                *head = p;
3898
              }
3899
          }
3900
      }
3901
      break;
3902
 
3903
    case E_Access_Protected_Subprogram_Type:
3904
    case E_Anonymous_Access_Protected_Subprogram_Type:
3905
      if (type_annotate_only && No (gnat_equiv_type))
3906
        gnu_type = ptr_void_type_node;
3907
      else
3908
        {
3909
          /* The run-time representation is the equivalent type.  */
3910
          gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3911
          maybe_present = true;
3912
        }
3913
 
3914
      if (Is_Itype (Directly_Designated_Type (gnat_entity))
3915
          && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3916
          && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3917
          && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3918
        gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3919
                            NULL_TREE, 0);
3920
 
3921
      break;
3922
 
3923
    case E_Access_Subtype:
3924
 
3925
      /* We treat this as identical to its base type; any constraint is
3926
         meaningful only to the front-end.
3927
 
3928
         The designated type must be elaborated as well, if it does
3929
         not have its own freeze node.  Designated (sub)types created
3930
         for constrained components of records with discriminants are
3931
         not frozen by the front-end and thus not elaborated by gigi,
3932
         because their use may appear before the base type is frozen,
3933
         and because it is not clear that they are needed anywhere in
3934
         gigi.  With the current model, there is no correct place where
3935
         they could be elaborated.  */
3936
 
3937
      gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3938
      if (Is_Itype (Directly_Designated_Type (gnat_entity))
3939
          && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3940
          && Is_Frozen (Directly_Designated_Type (gnat_entity))
3941
          && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3942
        {
3943
          /* If we are not defining this entity, and we have incomplete
3944
             entities being processed above us, make a dummy type and
3945
             elaborate it later.  */
3946
          if (!definition && defer_incomplete_level != 0)
3947
            {
3948
              struct incomplete *p = XNEW (struct incomplete);
3949
 
3950
              p->old_type
3951
                = make_dummy_type (Directly_Designated_Type (gnat_entity));
3952
              p->full_type = Directly_Designated_Type (gnat_entity);
3953
              p->next = defer_incomplete_list;
3954
              defer_incomplete_list = p;
3955
            }
3956
          else if (!IN (Ekind (Base_Type
3957
                               (Directly_Designated_Type (gnat_entity))),
3958
                        Incomplete_Or_Private_Kind))
3959
            gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3960
                                NULL_TREE, 0);
3961
        }
3962
 
3963
      maybe_present = true;
3964
      break;
3965
 
3966
    /* Subprogram Entities
3967
 
3968
       The following access functions are defined for subprograms:
3969
 
3970
                Etype           Return type or Standard_Void_Type.
3971
                First_Formal    The first formal parameter.
3972
                Is_Imported     Indicates that the subprogram has appeared in
3973
                                an INTERFACE or IMPORT pragma.  For now we
3974
                                assume that the external language is C.
3975
                Is_Exported     Likewise but for an EXPORT pragma.
3976
                Is_Inlined      True if the subprogram is to be inlined.
3977
 
3978
       Each parameter is first checked by calling must_pass_by_ref on its
3979
       type to determine if it is passed by reference.  For parameters which
3980
       are copied in, if they are Ada In Out or Out parameters, their return
3981
       value becomes part of a record which becomes the return type of the
3982
       function (C function - note that this applies only to Ada procedures
3983
       so there is no Ada return type).  Additional code to store back the
3984
       parameters will be generated on the caller side.  This transformation
3985
       is done here, not in the front-end.
3986
 
3987
       The intended result of the transformation can be seen from the
3988
       equivalent source rewritings that follow:
3989
 
3990
                                                struct temp {int a,b};
3991
       procedure P (A,B: In Out ...) is         temp P (int A,B)
3992
       begin                                    {
3993
         ..                                       ..
3994
       end P;                                     return {A,B};
3995
                                                }
3996
 
3997
                                                temp t;
3998
       P(X,Y);                                  t = P(X,Y);
3999
                                                X = t.a , Y = t.b;
4000
 
4001
       For subprogram types we need to perform mainly the same conversions to
4002
       GCC form that are needed for procedures and function declarations.  The
4003
       only difference is that at the end, we make a type declaration instead
4004
       of a function declaration.  */
4005
 
4006
    case E_Subprogram_Type:
4007
    case E_Function:
4008
    case E_Procedure:
4009
      {
4010
        /* The type returned by a function or else Standard_Void_Type for a
4011
           procedure.  */
4012
        Entity_Id gnat_return_type = Etype (gnat_entity);
4013
        tree gnu_return_type;
4014
        /* The first GCC parameter declaration (a PARM_DECL node).  The
4015
           PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4016
           actually is the head of this parameter list.  */
4017
        tree gnu_param_list = NULL_TREE;
4018
        /* Likewise for the stub associated with an exported procedure.  */
4019
        tree gnu_stub_param_list = NULL_TREE;
4020
        /* Non-null for subprograms containing parameters passed by copy-in
4021
           copy-out (Ada In Out or Out parameters not passed by reference),
4022
           in which case it is the list of nodes used to specify the values
4023
           of the In Out/Out parameters that are returned as a record upon
4024
           procedure return.  The TREE_PURPOSE of an element of this list is
4025
           a field of the record and the TREE_VALUE is the PARM_DECL
4026
           corresponding to that field.  This list will be saved in the
4027
           TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create.  */
4028
        tree gnu_cico_list = NULL_TREE;
4029
        /* List of fields in return type of procedure with copy-in copy-out
4030
           parameters.  */
4031
        tree gnu_field_list = NULL_TREE;
4032
        /* If an import pragma asks to map this subprogram to a GCC builtin,
4033
           this is the builtin DECL node.  */
4034
        tree gnu_builtin_decl = NULL_TREE;
4035
        /* For the stub associated with an exported procedure.  */
4036
        tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
4037
        tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4038
        Entity_Id gnat_param;
4039
        bool inline_flag = Is_Inlined (gnat_entity);
4040
        bool public_flag = Is_Public (gnat_entity) || imported_p;
4041
        bool extern_flag
4042
          = (Is_Public (gnat_entity) && !definition) || imported_p;
4043
        bool artificial_flag = !Comes_From_Source (gnat_entity);
4044
       /* The semantics of "pure" in Ada essentially matches that of "const"
4045
          in the back-end.  In particular, both properties are orthogonal to
4046
          the "nothrow" property if the EH circuitry is explicit in the
4047
          internal representation of the back-end.  If we are to completely
4048
          hide the EH circuitry from it, we need to declare that calls to pure
4049
          Ada subprograms that can throw have side effects since they can
4050
          trigger an "abnormal" transfer of control flow; thus they can be
4051
          neither "const" nor "pure" in the back-end sense.  */
4052
        bool const_flag
4053
          = (Exception_Mechanism == Back_End_Exceptions
4054
             && Is_Pure (gnat_entity));
4055
        bool volatile_flag = No_Return (gnat_entity);
4056
        bool return_by_direct_ref_p = false;
4057
        bool return_by_invisi_ref_p = false;
4058
        bool return_unconstrained_p = false;
4059
        bool has_stub = false;
4060
        int parmnum;
4061
 
4062
        /* A parameter may refer to this type, so defer completion of any
4063
           incomplete types.  */
4064
        if (kind == E_Subprogram_Type && !definition)
4065
          {
4066
            defer_incomplete_level++;
4067
            this_deferred = true;
4068
          }
4069
 
4070
        /* If the subprogram has an alias, it is probably inherited, so
4071
           we can use the original one.  If the original "subprogram"
4072
           is actually an enumeration literal, it may be the first use
4073
           of its type, so we must elaborate that type now.  */
4074
        if (Present (Alias (gnat_entity)))
4075
          {
4076
            if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4077
              gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4078
 
4079
            gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4080
 
4081
            /* Elaborate any Itypes in the parameters of this entity.  */
4082
            for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4083
                 Present (gnat_temp);
4084
                 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4085
              if (Is_Itype (Etype (gnat_temp)))
4086
                gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4087
 
4088
            break;
4089
          }
4090
 
4091
        /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4092
           corresponding DECL node.  Proper generation of calls later on need
4093
           proper parameter associations so we don't "break;" here.  */
4094
        if (Convention (gnat_entity) == Convention_Intrinsic
4095
            && Present (Interface_Name (gnat_entity)))
4096
          {
4097
            gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4098
 
4099
            /* Inability to find the builtin decl most often indicates a
4100
               genuine mistake, but imports of unregistered intrinsics are
4101
               sometimes issued on purpose to allow hooking in alternate
4102
               bodies.  We post a warning conditioned on Wshadow in this case,
4103
               to let developers be notified on demand without risking false
4104
               positives with common default sets of options.  */
4105
 
4106
            if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4107
              post_error ("?gcc intrinsic not found for&!", gnat_entity);
4108
          }
4109
 
4110
        /* ??? What if we don't find the builtin node above ? warn ? err ?
4111
           In the current state we neither warn nor err, and calls will just
4112
           be handled as for regular subprograms.  */
4113
 
4114
        /* Look into the return type and get its associated GCC tree.  If it
4115
           is not void, compute various flags for the subprogram type.  */
4116
        if (Ekind (gnat_return_type) == E_Void)
4117
          gnu_return_type = void_type_node;
4118
        else
4119
          {
4120
            gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4121
 
4122
            /* If this function returns by reference, make the actual return
4123
               type the pointer type and make a note of that.  */
4124
            if (Returns_By_Ref (gnat_entity))
4125
              {
4126
                gnu_return_type = build_pointer_type (gnu_return_type);
4127
                return_by_direct_ref_p = true;
4128
              }
4129
 
4130
            /* If we are supposed to return an unconstrained array type, make
4131
               the actual return type the fat pointer type.  */
4132
            else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4133
              {
4134
                gnu_return_type = TREE_TYPE (gnu_return_type);
4135
                return_unconstrained_p = true;
4136
              }
4137
 
4138
            /* Likewise, if the return type requires a transient scope, the
4139
               return value will be allocated on the secondary stack so the
4140
               actual return type is the pointer type.  */
4141
            else if (Requires_Transient_Scope (gnat_return_type))
4142
              {
4143
                gnu_return_type = build_pointer_type (gnu_return_type);
4144
                return_unconstrained_p = true;
4145
              }
4146
 
4147
            /* If the Mechanism is By_Reference, ensure this function uses the
4148
               target's by-invisible-reference mechanism, which may not be the
4149
               same as above (e.g. it might be passing an extra parameter).  */
4150
            else if (kind == E_Function
4151
                     && Mechanism (gnat_entity) == By_Reference)
4152
              return_by_invisi_ref_p = true;
4153
 
4154
            /* Likewise, if the return type is itself By_Reference.  */
4155
            else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4156
              return_by_invisi_ref_p = true;
4157
 
4158
            /* If the type is a padded type and the underlying type would not
4159
               be passed by reference or the function has a foreign convention,
4160
               return the underlying type.  */
4161
            else if (TYPE_IS_PADDING_P (gnu_return_type)
4162
                     && (!default_pass_by_ref
4163
                          (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4164
                         || Has_Foreign_Convention (gnat_entity)))
4165
              gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4166
 
4167
            /* If the return type is unconstrained, that means it must have a
4168
               maximum size.  Use the padded type as the effective return type.
4169
               And ensure the function uses the target's by-invisible-reference
4170
               mechanism to avoid copying too much data when it returns.  */
4171
            if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4172
              {
4173
                gnu_return_type
4174
                  = maybe_pad_type (gnu_return_type,
4175
                                    max_size (TYPE_SIZE (gnu_return_type),
4176
                                              true),
4177
                                    0, gnat_entity, false, false, false, true);
4178
 
4179
                /* Declare it now since it will never be declared otherwise.
4180
                   This is necessary to ensure that its subtrees are properly
4181
                   marked.  */
4182
                create_type_decl (TYPE_NAME (gnu_return_type), gnu_return_type,
4183
                                  NULL, true, debug_info_p, gnat_entity);
4184
 
4185
                return_by_invisi_ref_p = true;
4186
              }
4187
 
4188
            /* If the return type has a size that overflows, we cannot have
4189
               a function that returns that type.  This usage doesn't make
4190
               sense anyway, so give an error here.  */
4191
            if (TYPE_SIZE_UNIT (gnu_return_type)
4192
                && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
4193
                && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
4194
              {
4195
                post_error ("cannot return type whose size overflows",
4196
                            gnat_entity);
4197
                gnu_return_type = copy_node (gnu_return_type);
4198
                TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4199
                TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4200
                TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4201
                TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4202
              }
4203
          }
4204
 
4205
        /* Loop over the parameters and get their associated GCC tree.  While
4206
           doing this, build a copy-in copy-out structure if we need one.  */
4207
        for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4208
             Present (gnat_param);
4209
             gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4210
          {
4211
            tree gnu_param_name = get_entity_name (gnat_param);
4212
            tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4213
            tree gnu_param, gnu_field;
4214
            bool copy_in_copy_out = false;
4215
            Mechanism_Type mech = Mechanism (gnat_param);
4216
 
4217
            /* Builtins are expanded inline and there is no real call sequence
4218
               involved.  So the type expected by the underlying expander is
4219
               always the type of each argument "as is".  */
4220
            if (gnu_builtin_decl)
4221
              mech = By_Copy;
4222
            /* Handle the first parameter of a valued procedure specially.  */
4223
            else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4224
              mech = By_Copy_Return;
4225
            /* Otherwise, see if a Mechanism was supplied that forced this
4226
               parameter to be passed one way or another.  */
4227
            else if (mech == Default
4228
                     || mech == By_Copy || mech == By_Reference)
4229
              ;
4230
            else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4231
              mech = By_Descriptor;
4232
 
4233
            else if (By_Short_Descriptor_Last <= mech &&
4234
                     mech <= By_Short_Descriptor)
4235
              mech = By_Short_Descriptor;
4236
 
4237
            else if (mech > 0)
4238
              {
4239
                if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4240
                    || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4241
                    || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4242
                                             mech))
4243
                  mech = By_Reference;
4244
                else
4245
                  mech = By_Copy;
4246
              }
4247
            else
4248
              {
4249
                post_error ("unsupported mechanism for&", gnat_param);
4250
                mech = Default;
4251
              }
4252
 
4253
            gnu_param
4254
              = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4255
                                   Has_Foreign_Convention (gnat_entity),
4256
                                   &copy_in_copy_out);
4257
 
4258
            /* We are returned either a PARM_DECL or a type if no parameter
4259
               needs to be passed; in either case, adjust the type.  */
4260
            if (DECL_P (gnu_param))
4261
              gnu_param_type = TREE_TYPE (gnu_param);
4262
            else
4263
              {
4264
                gnu_param_type = gnu_param;
4265
                gnu_param = NULL_TREE;
4266
              }
4267
 
4268
            /* The failure of this assertion will very likely come from an
4269
               order of elaboration issue for the type of the parameter.  */
4270
            gcc_assert (kind == E_Subprogram_Type
4271
                        || !TYPE_IS_DUMMY_P (gnu_param_type)
4272
                        || type_annotate_only);
4273
 
4274
            if (gnu_param)
4275
              {
4276
                /* If it's an exported subprogram, we build a parameter list
4277
                   in parallel, in case we need to emit a stub for it.  */
4278
                if (Is_Exported (gnat_entity))
4279
                  {
4280
                    gnu_stub_param_list
4281
                      = chainon (gnu_param, gnu_stub_param_list);
4282
                    /* Change By_Descriptor parameter to By_Reference for
4283
                       the internal version of an exported subprogram.  */
4284
                    if (mech == By_Descriptor || mech == By_Short_Descriptor)
4285
                      {
4286
                        gnu_param
4287
                          = gnat_to_gnu_param (gnat_param, By_Reference,
4288
                                               gnat_entity, false,
4289
                                               &copy_in_copy_out);
4290
                        has_stub = true;
4291
                      }
4292
                    else
4293
                      gnu_param = copy_node (gnu_param);
4294
                  }
4295
 
4296
                gnu_param_list = chainon (gnu_param, gnu_param_list);
4297
                Sloc_to_locus (Sloc (gnat_param),
4298
                               &DECL_SOURCE_LOCATION (gnu_param));
4299
                save_gnu_tree (gnat_param, gnu_param, false);
4300
 
4301
                /* If a parameter is a pointer, this function may modify
4302
                   memory through it and thus shouldn't be considered
4303
                   a const function.  Also, the memory may be modified
4304
                   between two calls, so they can't be CSE'ed.  The latter
4305
                   case also handles by-ref parameters.  */
4306
                if (POINTER_TYPE_P (gnu_param_type)
4307
                    || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4308
                  const_flag = false;
4309
              }
4310
 
4311
            if (copy_in_copy_out)
4312
              {
4313
                if (!gnu_cico_list)
4314
                  {
4315
                    tree gnu_new_ret_type = make_node (RECORD_TYPE);
4316
 
4317
                    /* If this is a function, we also need a field for the
4318
                       return value to be placed.  */
4319
                    if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4320
                      {
4321
                        gnu_field
4322
                          = create_field_decl (get_identifier ("RETVAL"),
4323
                                               gnu_return_type,
4324
                                               gnu_new_ret_type, NULL_TREE,
4325
                                               NULL_TREE, 0, 0);
4326
                        Sloc_to_locus (Sloc (gnat_entity),
4327
                                       &DECL_SOURCE_LOCATION (gnu_field));
4328
                        gnu_field_list = gnu_field;
4329
                        gnu_cico_list
4330
                          = tree_cons (gnu_field, void_type_node, NULL_TREE);
4331
                      }
4332
 
4333
                    gnu_return_type = gnu_new_ret_type;
4334
                    TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4335
                    /* Set a default alignment to speed up accesses.  But we
4336
                       shouldn't increase the size of the structure too much,
4337
                       lest it doesn't fit in return registers anymore.  */
4338
                    TYPE_ALIGN (gnu_return_type)
4339
                      = get_mode_alignment (ptr_mode);
4340
                  }
4341
 
4342
                gnu_field
4343
                  = create_field_decl (gnu_param_name, gnu_param_type,
4344
                                       gnu_return_type, NULL_TREE, NULL_TREE,
4345
                                       0, 0);
4346
                Sloc_to_locus (Sloc (gnat_param),
4347
                               &DECL_SOURCE_LOCATION (gnu_field));
4348
                DECL_CHAIN (gnu_field) = gnu_field_list;
4349
                gnu_field_list = gnu_field;
4350
                gnu_cico_list
4351
                  = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4352
              }
4353
          }
4354
 
4355
        if (gnu_cico_list)
4356
          {
4357
            /* If we have a CICO list but it has only one entry, we convert
4358
               this function into a function that returns this object.  */
4359
            if (list_length (gnu_cico_list) == 1)
4360
              gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4361
 
4362
            /* Do not finalize the return type if the subprogram is stubbed
4363
               since structures are incomplete for the back-end.  */
4364
            else if (Convention (gnat_entity) != Convention_Stubbed)
4365
              {
4366
                finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4367
                                    0, false);
4368
 
4369
                /* Try to promote the mode of the return type if it is passed
4370
                   in registers, again to speed up accesses.  */
4371
                if (TYPE_MODE (gnu_return_type) == BLKmode
4372
                    && !targetm.calls.return_in_memory (gnu_return_type,
4373
                                                        NULL_TREE))
4374
                  {
4375
                    unsigned int size
4376
                      = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4377
                    unsigned int i = BITS_PER_UNIT;
4378
                    enum machine_mode mode;
4379
 
4380
                    while (i < size)
4381
                      i <<= 1;
4382
                    mode = mode_for_size (i, MODE_INT, 0);
4383
                    if (mode != BLKmode)
4384
                      {
4385
                        SET_TYPE_MODE (gnu_return_type, mode);
4386
                        TYPE_ALIGN (gnu_return_type)
4387
                          = GET_MODE_ALIGNMENT (mode);
4388
                        TYPE_SIZE (gnu_return_type)
4389
                          = bitsize_int (GET_MODE_BITSIZE (mode));
4390
                        TYPE_SIZE_UNIT (gnu_return_type)
4391
                          = size_int (GET_MODE_SIZE (mode));
4392
                      }
4393
                  }
4394
 
4395
                if (debug_info_p)
4396
                  rest_of_record_type_compilation (gnu_return_type);
4397
              }
4398
          }
4399
 
4400
        if (Has_Stdcall_Convention (gnat_entity))
4401
          prepend_one_attribute_to
4402
            (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4403
             get_identifier ("stdcall"), NULL_TREE,
4404
             gnat_entity);
4405
 
4406
        /* If we should request stack realignment for a foreign convention
4407
           subprogram, do so.  Note that this applies to task entry points in
4408
           particular.  */
4409
        if (FOREIGN_FORCE_REALIGN_STACK
4410
            && Has_Foreign_Convention (gnat_entity))
4411
          prepend_one_attribute_to
4412
            (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4413
             get_identifier ("force_align_arg_pointer"), NULL_TREE,
4414
             gnat_entity);
4415
 
4416
        /* The lists have been built in reverse.  */
4417
        gnu_param_list = nreverse (gnu_param_list);
4418
        if (has_stub)
4419
          gnu_stub_param_list = nreverse (gnu_stub_param_list);
4420
        gnu_cico_list = nreverse (gnu_cico_list);
4421
 
4422
        if (kind == E_Function)
4423
          Set_Mechanism (gnat_entity, return_unconstrained_p
4424
                                      || return_by_direct_ref_p
4425
                                      || return_by_invisi_ref_p
4426
                                      ? By_Reference : By_Copy);
4427
        gnu_type
4428
          = create_subprog_type (gnu_return_type, gnu_param_list,
4429
                                 gnu_cico_list, return_unconstrained_p,
4430
                                 return_by_direct_ref_p,
4431
                                 return_by_invisi_ref_p);
4432
 
4433
        if (has_stub)
4434
          gnu_stub_type
4435
            = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4436
                                   gnu_cico_list, return_unconstrained_p,
4437
                                   return_by_direct_ref_p,
4438
                                   return_by_invisi_ref_p);
4439
 
4440
        /* A subprogram (something that doesn't return anything) shouldn't
4441
           be considered const since there would be no reason for such a
4442
           subprogram.  Note that procedures with Out (or In Out) parameters
4443
           have already been converted into a function with a return type.  */
4444
        if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4445
          const_flag = false;
4446
 
4447
        gnu_type
4448
          = build_qualified_type (gnu_type,
4449
                                  TYPE_QUALS (gnu_type)
4450
                                  | (TYPE_QUAL_CONST * const_flag)
4451
                                  | (TYPE_QUAL_VOLATILE * volatile_flag));
4452
 
4453
        if (has_stub)
4454
          gnu_stub_type
4455
            = build_qualified_type (gnu_stub_type,
4456
                                    TYPE_QUALS (gnu_stub_type)
4457
                                    | (TYPE_QUAL_CONST * const_flag)
4458
                                    | (TYPE_QUAL_VOLATILE * volatile_flag));
4459
 
4460
        /* If we have a builtin decl for that function, use it.  Check if the
4461
           profiles are compatible and warn if they are not.  The checker is
4462
           expected to post extra diagnostics in this case.  */
4463
        if (gnu_builtin_decl)
4464
          {
4465
            intrin_binding_t inb;
4466
 
4467
            inb.gnat_entity = gnat_entity;
4468
            inb.ada_fntype = gnu_type;
4469
            inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4470
 
4471
            if (!intrin_profiles_compatible_p (&inb))
4472
              post_error
4473
                ("?profile of& doesn''t match the builtin it binds!",
4474
                 gnat_entity);
4475
 
4476
            gnu_decl = gnu_builtin_decl;
4477
            gnu_type = TREE_TYPE (gnu_builtin_decl);
4478
            break;
4479
          }
4480
 
4481
        /* If there was no specified Interface_Name and the external and
4482
           internal names of the subprogram are the same, only use the
4483
           internal name to allow disambiguation of nested subprograms.  */
4484
        if (No (Interface_Name (gnat_entity))
4485
            && gnu_ext_name == gnu_entity_name)
4486
          gnu_ext_name = NULL_TREE;
4487
 
4488
        /* If we are defining the subprogram and it has an Address clause
4489
           we must get the address expression from the saved GCC tree for the
4490
           subprogram if it has a Freeze_Node.  Otherwise, we elaborate
4491
           the address expression here since the front-end has guaranteed
4492
           in that case that the elaboration has no effects.  If there is
4493
           an Address clause and we are not defining the object, just
4494
           make it a constant.  */
4495
        if (Present (Address_Clause (gnat_entity)))
4496
          {
4497
            tree gnu_address = NULL_TREE;
4498
 
4499
            if (definition)
4500
              gnu_address
4501
                = (present_gnu_tree (gnat_entity)
4502
                   ? get_gnu_tree (gnat_entity)
4503
                   : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4504
 
4505
            save_gnu_tree (gnat_entity, NULL_TREE, false);
4506
 
4507
            /* Convert the type of the object to a reference type that can
4508
               alias everything as per 13.3(19).  */
4509
            gnu_type
4510
              = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4511
            if (gnu_address)
4512
              gnu_address = convert (gnu_type, gnu_address);
4513
 
4514
            gnu_decl
4515
              = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4516
                                 gnu_address, false, Is_Public (gnat_entity),
4517
                                 extern_flag, false, NULL, gnat_entity);
4518
            DECL_BY_REF_P (gnu_decl) = 1;
4519
          }
4520
 
4521
        else if (kind == E_Subprogram_Type)
4522
          gnu_decl
4523
            = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4524
                                artificial_flag, debug_info_p, gnat_entity);
4525
        else
4526
          {
4527
            if (has_stub)
4528
              {
4529
                gnu_stub_name = gnu_ext_name;
4530
                gnu_ext_name = create_concat_name (gnat_entity, "internal");
4531
                public_flag = false;
4532
                artificial_flag = true;
4533
              }
4534
 
4535
            gnu_decl
4536
              = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4537
                                     gnu_param_list, inline_flag, public_flag,
4538
                                     extern_flag, artificial_flag, attr_list,
4539
                                     gnat_entity);
4540
            if (has_stub)
4541
              {
4542
                tree gnu_stub_decl
4543
                  = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4544
                                         gnu_stub_type, gnu_stub_param_list,
4545
                                         inline_flag, true, extern_flag,
4546
                                         false, attr_list, gnat_entity);
4547
                SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4548
              }
4549
 
4550
            /* This is unrelated to the stub built right above.  */
4551
            DECL_STUBBED_P (gnu_decl)
4552
              = Convention (gnat_entity) == Convention_Stubbed;
4553
          }
4554
      }
4555
      break;
4556
 
4557
    case E_Incomplete_Type:
4558
    case E_Incomplete_Subtype:
4559
    case E_Private_Type:
4560
    case E_Private_Subtype:
4561
    case E_Limited_Private_Type:
4562
    case E_Limited_Private_Subtype:
4563
    case E_Record_Type_With_Private:
4564
    case E_Record_Subtype_With_Private:
4565
      {
4566
        /* Get the "full view" of this entity.  If this is an incomplete
4567
           entity from a limited with, treat its non-limited view as the
4568
           full view.  Otherwise, use either the full view or the underlying
4569
           full view, whichever is present.  This is used in all the tests
4570
           below.  */
4571
        Entity_Id full_view
4572
          = (IN (kind, Incomplete_Kind) && From_With_Type (gnat_entity))
4573
            ? Non_Limited_View (gnat_entity)
4574
            : Present (Full_View (gnat_entity))
4575
              ? Full_View (gnat_entity)
4576
              : Underlying_Full_View (gnat_entity);
4577
 
4578
        /* If this is an incomplete type with no full view, it must be a Taft
4579
           Amendment type, in which case we return a dummy type.  Otherwise,
4580
           just get the type from its Etype.  */
4581
        if (No (full_view))
4582
          {
4583
            if (kind == E_Incomplete_Type)
4584
              {
4585
                gnu_type = make_dummy_type (gnat_entity);
4586
                gnu_decl = TYPE_STUB_DECL (gnu_type);
4587
              }
4588
            else
4589
              {
4590
                gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4591
                                               NULL_TREE, 0);
4592
                maybe_present = true;
4593
              }
4594
            break;
4595
          }
4596
 
4597
        /* If we already made a type for the full view, reuse it.  */
4598
        else if (present_gnu_tree (full_view))
4599
          {
4600
            gnu_decl = get_gnu_tree (full_view);
4601
            break;
4602
          }
4603
 
4604
        /* Otherwise, if we are not defining the type now, get the type
4605
           from the full view.  But always get the type from the full view
4606
           for define on use types, since otherwise we won't see them!  */
4607
        else if (!definition
4608
                 || (Is_Itype (full_view)
4609
                   && No (Freeze_Node (gnat_entity)))
4610
                 || (Is_Itype (gnat_entity)
4611
                   && No (Freeze_Node (full_view))))
4612
          {
4613
            gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4614
            maybe_present = true;
4615
            break;
4616
          }
4617
 
4618
        /* For incomplete types, make a dummy type entry which will be
4619
           replaced later.  Save it as the full declaration's type so
4620
           we can do any needed updates when we see it.  */
4621
        gnu_type = make_dummy_type (gnat_entity);
4622
        gnu_decl = TYPE_STUB_DECL (gnu_type);
4623
        if (Has_Completion_In_Body (gnat_entity))
4624
          DECL_TAFT_TYPE_P (gnu_decl) = 1;
4625
        save_gnu_tree (full_view, gnu_decl, 0);
4626
        break;
4627
      }
4628
 
4629
    case E_Class_Wide_Type:
4630
      /* Class-wide types are always transformed into their root type.  */
4631
      gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4632
      maybe_present = true;
4633
      break;
4634
 
4635
    case E_Task_Type:
4636
    case E_Task_Subtype:
4637
    case E_Protected_Type:
4638
    case E_Protected_Subtype:
4639
      /* Concurrent types are always transformed into their record type.  */
4640
      if (type_annotate_only && No (gnat_equiv_type))
4641
        gnu_type = void_type_node;
4642
      else
4643
        gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4644
      maybe_present = true;
4645
      break;
4646
 
4647
    case E_Label:
4648
      gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4649
      break;
4650
 
4651
    case E_Block:
4652
    case E_Loop:
4653
      /* Nothing at all to do here, so just return an ERROR_MARK and claim
4654
         we've already saved it, so we don't try to.  */
4655
      gnu_decl = error_mark_node;
4656
      saved = true;
4657
      break;
4658
 
4659
    default:
4660
      gcc_unreachable ();
4661
    }
4662
 
4663
  /* If we had a case where we evaluated another type and it might have
4664
     defined this one, handle it here.  */
4665
  if (maybe_present && present_gnu_tree (gnat_entity))
4666
    {
4667
      gnu_decl = get_gnu_tree (gnat_entity);
4668
      saved = true;
4669
    }
4670
 
4671
  /* If we are processing a type and there is either no decl for it or
4672
     we just made one, do some common processing for the type, such as
4673
     handling alignment and possible padding.  */
4674
  if (is_type && (!gnu_decl || this_made_decl))
4675
    {
4676
      /* Tell the middle-end that objects of tagged types are guaranteed to
4677
         be properly aligned.  This is necessary because conversions to the
4678
         class-wide type are translated into conversions to the root type,
4679
         which can be less aligned than some of its derived types.  */
4680
      if (Is_Tagged_Type (gnat_entity)
4681
          || Is_Class_Wide_Equivalent_Type (gnat_entity))
4682
        TYPE_ALIGN_OK (gnu_type) = 1;
4683
 
4684
      /* Record whether the type is passed by reference.  */
4685
      if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4686
        TYPE_BY_REFERENCE_P (gnu_type) = 1;
4687
 
4688
      /* ??? Don't set the size for a String_Literal since it is either
4689
         confirming or we don't handle it properly (if the low bound is
4690
         non-constant).  */
4691
      if (!gnu_size && kind != E_String_Literal_Subtype)
4692
        {
4693
          Uint gnat_size = Known_Esize (gnat_entity)
4694
                           ? Esize (gnat_entity) : RM_Size (gnat_entity);
4695
          gnu_size
4696
            = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4697
                             false, Has_Size_Clause (gnat_entity));
4698
        }
4699
 
4700
      /* If a size was specified, see if we can make a new type of that size
4701
         by rearranging the type, for example from a fat to a thin pointer.  */
4702
      if (gnu_size)
4703
        {
4704
          gnu_type
4705
            = make_type_from_size (gnu_type, gnu_size,
4706
                                   Has_Biased_Representation (gnat_entity));
4707
 
4708
          if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4709
              && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4710
            gnu_size = 0;
4711
        }
4712
 
4713
      /* If the alignment hasn't already been processed and this is
4714
         not an unconstrained array, see if an alignment is specified.
4715
         If not, we pick a default alignment for atomic objects.  */
4716
      if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4717
        ;
4718
      else if (Known_Alignment (gnat_entity))
4719
        {
4720
          align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4721
                                      TYPE_ALIGN (gnu_type));
4722
 
4723
          /* Warn on suspiciously large alignments.  This should catch
4724
             errors about the (alignment,byte)/(size,bit) discrepancy.  */
4725
          if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4726
            {
4727
              tree size;
4728
 
4729
              /* If a size was specified, take it into account.  Otherwise
4730
                 use the RM size for records or unions as the type size has
4731
                 already been adjusted to the alignment.  */
4732
              if (gnu_size)
4733
                size = gnu_size;
4734
              else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4735
                       && !TYPE_FAT_POINTER_P (gnu_type))
4736
                size = rm_size (gnu_type);
4737
              else
4738
                size = TYPE_SIZE (gnu_type);
4739
 
4740
              /* Consider an alignment as suspicious if the alignment/size
4741
                 ratio is greater or equal to the byte/bit ratio.  */
4742
              if (host_integerp (size, 1)
4743
                  && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4744
                post_error_ne ("?suspiciously large alignment specified for&",
4745
                               Expression (Alignment_Clause (gnat_entity)),
4746
                               gnat_entity);
4747
            }
4748
        }
4749
      else if (Is_Atomic (gnat_entity) && !gnu_size
4750
               && host_integerp (TYPE_SIZE (gnu_type), 1)
4751
               && integer_pow2p (TYPE_SIZE (gnu_type)))
4752
        align = MIN (BIGGEST_ALIGNMENT,
4753
                     tree_low_cst (TYPE_SIZE (gnu_type), 1));
4754
      else if (Is_Atomic (gnat_entity) && gnu_size
4755
               && host_integerp (gnu_size, 1)
4756
               && integer_pow2p (gnu_size))
4757
        align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4758
 
4759
      /* See if we need to pad the type.  If we did, and made a record,
4760
         the name of the new type may be changed.  So get it back for
4761
         us when we make the new TYPE_DECL below.  */
4762
      if (gnu_size || align > 0)
4763
        gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4764
                                   false, !gnu_decl, definition, false);
4765
 
4766
      if (TYPE_IS_PADDING_P (gnu_type))
4767
        {
4768
          gnu_entity_name = TYPE_NAME (gnu_type);
4769
          if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4770
            gnu_entity_name = DECL_NAME (gnu_entity_name);
4771
        }
4772
 
4773
      set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4774
 
4775
      /* If we are at global level, GCC will have applied variable_size to
4776
         the type, but that won't have done anything.  So, if it's not
4777
         a constant or self-referential, call elaborate_expression_1 to
4778
         make a variable for the size rather than calculating it each time.
4779
         Handle both the RM size and the actual size.  */
4780
      if (global_bindings_p ()
4781
          && TYPE_SIZE (gnu_type)
4782
          && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4783
          && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4784
        {
4785
          tree size = TYPE_SIZE (gnu_type);
4786
 
4787
          TYPE_SIZE (gnu_type)
4788
            = elaborate_expression_1 (size, gnat_entity,
4789
                                      get_identifier ("SIZE"),
4790
                                      definition, false);
4791
 
4792
          /* ??? For now, store the size as a multiple of the alignment in
4793
             bytes so that we can see the alignment from the tree.  */
4794
          TYPE_SIZE_UNIT (gnu_type)
4795
            = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4796
                                      get_identifier ("SIZE_A_UNIT"),
4797
                                      definition, false,
4798
                                      TYPE_ALIGN (gnu_type));
4799
 
4800
          /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4801
             may not be marked by the call to create_type_decl below.  */
4802
          MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4803
 
4804
          if (TREE_CODE (gnu_type) == RECORD_TYPE)
4805
            {
4806
              tree variant_part = get_variant_part (gnu_type);
4807
              tree ada_size = TYPE_ADA_SIZE (gnu_type);
4808
 
4809
              if (variant_part)
4810
                {
4811
                  tree union_type = TREE_TYPE (variant_part);
4812
                  tree offset = DECL_FIELD_OFFSET (variant_part);
4813
 
4814
                  /* If the position of the variant part is constant, subtract
4815
                     it from the size of the type of the parent to get the new
4816
                     size.  This manual CSE reduces the data size.  */
4817
                  if (TREE_CODE (offset) == INTEGER_CST)
4818
                    {
4819
                      tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4820
                      TYPE_SIZE (union_type)
4821
                        = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4822
                                      bit_from_pos (offset, bitpos));
4823
                      TYPE_SIZE_UNIT (union_type)
4824
                        = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4825
                                      byte_from_pos (offset, bitpos));
4826
                    }
4827
                  else
4828
                    {
4829
                      TYPE_SIZE (union_type)
4830
                        = elaborate_expression_1 (TYPE_SIZE (union_type),
4831
                                                  gnat_entity,
4832
                                                  get_identifier ("VSIZE"),
4833
                                                  definition, false);
4834
 
4835
                      /* ??? For now, store the size as a multiple of the
4836
                         alignment in bytes so that we can see the alignment
4837
                         from the tree.  */
4838
                      TYPE_SIZE_UNIT (union_type)
4839
                        = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4840
                                                  gnat_entity,
4841
                                                  get_identifier
4842
                                                  ("VSIZE_A_UNIT"),
4843
                                                  definition, false,
4844
                                                  TYPE_ALIGN (union_type));
4845
 
4846
                      /* ??? For now, store the offset as a multiple of the
4847
                         alignment in bytes so that we can see the alignment
4848
                         from the tree.  */
4849
                      DECL_FIELD_OFFSET (variant_part)
4850
                        = elaborate_expression_2 (offset,
4851
                                                  gnat_entity,
4852
                                                  get_identifier ("VOFFSET"),
4853
                                                  definition, false,
4854
                                                  DECL_OFFSET_ALIGN
4855
                                                  (variant_part));
4856
                    }
4857
 
4858
                  DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4859
                  DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4860
                }
4861
 
4862
              if (operand_equal_p (ada_size, size, 0))
4863
                ada_size = TYPE_SIZE (gnu_type);
4864
              else
4865
                ada_size
4866
                  = elaborate_expression_1 (ada_size, gnat_entity,
4867
                                            get_identifier ("RM_SIZE"),
4868
                                            definition, false);
4869
              SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4870
            }
4871
        }
4872
 
4873
      /* If this is a record type or subtype, call elaborate_expression_1 on
4874
         any field position.  Do this for both global and local types.
4875
         Skip any fields that we haven't made trees for to avoid problems with
4876
         class wide types.  */
4877
      if (IN (kind, Record_Kind))
4878
        for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4879
             gnat_temp = Next_Entity (gnat_temp))
4880
          if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4881
            {
4882
              tree gnu_field = get_gnu_tree (gnat_temp);
4883
 
4884
              /* ??? For now, store the offset as a multiple of the alignment
4885
                 in bytes so that we can see the alignment from the tree.  */
4886
              if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4887
                {
4888
                  DECL_FIELD_OFFSET (gnu_field)
4889
                    = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4890
                                              gnat_temp,
4891
                                              get_identifier ("OFFSET"),
4892
                                              definition, false,
4893
                                              DECL_OFFSET_ALIGN (gnu_field));
4894
 
4895
                  /* ??? The context of gnu_field is not necessarily gnu_type
4896
                     so the MULT_EXPR node built above may not be marked by
4897
                     the call to create_type_decl below.  */
4898
                  if (global_bindings_p ())
4899
                    MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4900
                }
4901
            }
4902
 
4903
      if (Treat_As_Volatile (gnat_entity))
4904
        gnu_type
4905
          = build_qualified_type (gnu_type,
4906
                                  TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4907
 
4908
      if (Is_Atomic (gnat_entity))
4909
        check_ok_for_atomic (gnu_type, gnat_entity, false);
4910
 
4911
      if (Present (Alignment_Clause (gnat_entity)))
4912
        TYPE_USER_ALIGN (gnu_type) = 1;
4913
 
4914
      if (Universal_Aliasing (gnat_entity))
4915
        TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4916
 
4917
      if (!gnu_decl)
4918
        gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4919
                                     !Comes_From_Source (gnat_entity),
4920
                                     debug_info_p, gnat_entity);
4921
      else
4922
        {
4923
          TREE_TYPE (gnu_decl) = gnu_type;
4924
          TYPE_STUB_DECL (gnu_type) = gnu_decl;
4925
        }
4926
    }
4927
 
4928
  if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4929
    {
4930
      gnu_type = TREE_TYPE (gnu_decl);
4931
 
4932
      /* If this is a derived type, relate its alias set to that of its parent
4933
         to avoid troubles when a call to an inherited primitive is inlined in
4934
         a context where a derived object is accessed.  The inlined code works
4935
         on the parent view so the resulting code may access the same object
4936
         using both the parent and the derived alias sets, which thus have to
4937
         conflict.  As the same issue arises with component references, the
4938
         parent alias set also has to conflict with composite types enclosing
4939
         derived components.  For instance, if we have:
4940
 
4941
            type D is new T;
4942
            type R is record
4943
               Component : D;
4944
            end record;
4945
 
4946
         we want T to conflict with both D and R, in addition to R being a
4947
         superset of D by record/component construction.
4948
 
4949
         One way to achieve this is to perform an alias set copy from the
4950
         parent to the derived type.  This is not quite appropriate, though,
4951
         as we don't want separate derived types to conflict with each other:
4952
 
4953
            type I1 is new Integer;
4954
            type I2 is new Integer;
4955
 
4956
         We want I1 and I2 to both conflict with Integer but we do not want
4957
         I1 to conflict with I2, and an alias set copy on derivation would
4958
         have that effect.
4959
 
4960
         The option chosen is to make the alias set of the derived type a
4961
         superset of that of its parent type.  It trivially fulfills the
4962
         simple requirement for the Integer derivation example above, and
4963
         the component case as well by superset transitivity:
4964
 
4965
                   superset      superset
4966
                R ----------> D ----------> T
4967
 
4968
         However, for composite types, conversions between derived types are
4969
         translated into VIEW_CONVERT_EXPRs so a sequence like:
4970
 
4971
            type Comp1 is new Comp;
4972
            type Comp2 is new Comp;
4973
            procedure Proc (C : Comp1);
4974
 
4975
            C : Comp2;
4976
            Proc (Comp1 (C));
4977
 
4978
         is translated into:
4979
 
4980
            C : Comp2;
4981
            Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4982
 
4983
         and gimplified into:
4984
 
4985
            C : Comp2;
4986
            Comp1 *C.0;
4987
            C.0 = (Comp1 *) &C;
4988
            Proc (C.0);
4989
 
4990
         i.e. generates code involving type punning.  Therefore, Comp1 needs
4991
         to conflict with Comp2 and an alias set copy is required.
4992
 
4993
         The language rules ensure the parent type is already frozen here.  */
4994
      if (Is_Derived_Type (gnat_entity))
4995
        {
4996
          tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4997
          relate_alias_sets (gnu_type, gnu_parent_type,
4998
                             Is_Composite_Type (gnat_entity)
4999
                             ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5000
        }
5001
 
5002
      /* Back-annotate the Alignment of the type if not already in the
5003
         tree.  Likewise for sizes.  */
5004
      if (Unknown_Alignment (gnat_entity))
5005
        {
5006
          unsigned int double_align, align;
5007
          bool is_capped_double, align_clause;
5008
 
5009
          /* If the default alignment of "double" or larger scalar types is
5010
             specifically capped and this is not an array with an alignment
5011
             clause on the component type, return the cap.  */
5012
          if ((double_align = double_float_alignment) > 0)
5013
            is_capped_double
5014
              = is_double_float_or_array (gnat_entity, &align_clause);
5015
          else if ((double_align = double_scalar_alignment) > 0)
5016
            is_capped_double
5017
              = is_double_scalar_or_array (gnat_entity, &align_clause);
5018
          else
5019
            is_capped_double = align_clause = false;
5020
 
5021
          if (is_capped_double && !align_clause)
5022
            align = double_align;
5023
          else
5024
            align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5025
 
5026
          Set_Alignment (gnat_entity, UI_From_Int (align));
5027
        }
5028
 
5029
      if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5030
        {
5031
          tree gnu_size = TYPE_SIZE (gnu_type);
5032
 
5033
          /* If the size is self-referential, annotate the maximum value.  */
5034
          if (CONTAINS_PLACEHOLDER_P (gnu_size))
5035
            gnu_size = max_size (gnu_size, true);
5036
 
5037
          if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5038
            {
5039
              /* In this mode, the tag and the parent components are not
5040
                 generated by the front-end so the sizes must be adjusted.  */
5041
              tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5042
              Uint uint_size;
5043
 
5044
              if (Is_Derived_Type (gnat_entity))
5045
                {
5046
                  offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5047
                                      bitsizetype);
5048
                  Set_Alignment (gnat_entity,
5049
                                 Alignment (Etype (Base_Type (gnat_entity))));
5050
                }
5051
              else
5052
                offset = pointer_size;
5053
 
5054
              gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5055
              gnu_size = size_binop (MULT_EXPR, pointer_size,
5056
                                                size_binop (CEIL_DIV_EXPR,
5057
                                                            gnu_size,
5058
                                                            pointer_size));
5059
              uint_size = annotate_value (gnu_size);
5060
              Set_Esize (gnat_entity, uint_size);
5061
              Set_RM_Size (gnat_entity, uint_size);
5062
            }
5063
          else
5064
            Set_Esize (gnat_entity, annotate_value (gnu_size));
5065
        }
5066
 
5067
      if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5068
        Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5069
    }
5070
 
5071
  /* If we really have a ..._DECL node, set a couple of flags on it.  But we
5072
     cannot do so if we are reusing the ..._DECL node made for an alias or a
5073
     renamed object as the predicates don't apply to it but to GNAT_ENTITY.  */
5074
  if (DECL_P (gnu_decl)
5075
      && !Present (Alias (gnat_entity))
5076
      && !(Present (Renamed_Object (gnat_entity)) && saved))
5077
    {
5078
      if (!Comes_From_Source (gnat_entity))
5079
        DECL_ARTIFICIAL (gnu_decl) = 1;
5080
 
5081
      if (!debug_info_p)
5082
        DECL_IGNORED_P (gnu_decl) = 1;
5083
    }
5084
 
5085
  /* If we haven't already, associate the ..._DECL node that we just made with
5086
     the input GNAT entity node.  */
5087
  if (!saved)
5088
    save_gnu_tree (gnat_entity, gnu_decl, false);
5089
 
5090
  /* If this is an enumeration or floating-point type, we were not able to set
5091
     the bounds since they refer to the type.  These are always static.  */
5092
  if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5093
      || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
5094
    {
5095
      tree gnu_scalar_type = gnu_type;
5096
      tree gnu_low_bound, gnu_high_bound;
5097
 
5098
      /* If this is a padded type, we need to use the underlying type.  */
5099
      if (TYPE_IS_PADDING_P (gnu_scalar_type))
5100
        gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5101
 
5102
      /* If this is a floating point type and we haven't set a floating
5103
         point type yet, use this in the evaluation of the bounds.  */
5104
      if (!longest_float_type_node && kind == E_Floating_Point_Type)
5105
        longest_float_type_node = gnu_scalar_type;
5106
 
5107
      gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5108
      gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5109
 
5110
      if (kind == E_Enumeration_Type)
5111
        {
5112
          /* Enumeration types have specific RM bounds.  */
5113
          SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5114
          SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5115
 
5116
          /* Write full debugging information.  */
5117
          rest_of_type_decl_compilation (gnu_decl);
5118
        }
5119
 
5120
      else
5121
        {
5122
          /* Floating-point types don't have specific RM bounds.  */
5123
          TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5124
          TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5125
        }
5126
    }
5127
 
5128
  /* If we deferred processing of incomplete types, re-enable it.  If there
5129
     were no other disables and we have deferred types to process, do so.  */
5130
  if (this_deferred
5131
      && --defer_incomplete_level == 0
5132
      && defer_incomplete_list)
5133
    {
5134
      struct incomplete *p, *next;
5135
 
5136
      /* We are back to level 0 for the deferring of incomplete types.
5137
         But processing these incomplete types below may itself require
5138
         deferring, so preserve what we have and restart from scratch.  */
5139
      p = defer_incomplete_list;
5140
      defer_incomplete_list = NULL;
5141
 
5142
      /* For finalization, however, all types must be complete so we
5143
         cannot do the same because deferred incomplete types may end up
5144
         referencing each other.  Process them all recursively first.  */
5145
      defer_finalize_level++;
5146
 
5147
      for (; p; p = next)
5148
        {
5149
          next = p->next;
5150
 
5151
          if (p->old_type)
5152
            update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5153
                               gnat_to_gnu_type (p->full_type));
5154
          free (p);
5155
        }
5156
 
5157
      defer_finalize_level--;
5158
    }
5159
 
5160
  /* If all the deferred incomplete types have been processed, we can proceed
5161
     with the finalization of the deferred types.  */
5162
  if (defer_incomplete_level == 0
5163
      && defer_finalize_level == 0
5164
      && defer_finalize_list)
5165
    {
5166
      unsigned int i;
5167
      tree t;
5168
 
5169
      FOR_EACH_VEC_ELT (tree, defer_finalize_list, i, t)
5170
        rest_of_type_decl_compilation_no_defer (t);
5171
 
5172
      VEC_free (tree, heap, defer_finalize_list);
5173
    }
5174
 
5175
  /* If we are not defining this type, see if it's on one of the lists of
5176
     incomplete types.  If so, handle the list entry now.  */
5177
  if (is_type && !definition)
5178
    {
5179
      struct incomplete *p;
5180
 
5181
      for (p = defer_incomplete_list; p; p = p->next)
5182
        if (p->old_type && p->full_type == gnat_entity)
5183
          {
5184
            update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5185
                               TREE_TYPE (gnu_decl));
5186
            p->old_type = NULL_TREE;
5187
          }
5188
 
5189
      for (p = defer_limited_with; p; p = p->next)
5190
        if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5191
          {
5192
            update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5193
                               TREE_TYPE (gnu_decl));
5194
            p->old_type = NULL_TREE;
5195
          }
5196
    }
5197
 
5198
  if (this_global)
5199
    force_global--;
5200
 
5201
  /* If this is a packed array type whose original array type is itself
5202
     an Itype without freeze node, make sure the latter is processed.  */
5203
  if (Is_Packed_Array_Type (gnat_entity)
5204
      && Is_Itype (Original_Array_Type (gnat_entity))
5205
      && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5206
      && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5207
    gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5208
 
5209
  return gnu_decl;
5210
}
5211
 
5212
/* Similar, but if the returned value is a COMPONENT_REF, return the
5213
   FIELD_DECL.  */
5214
 
5215
tree
5216
gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5217
{
5218
  tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5219
 
5220
  if (TREE_CODE (gnu_field) == COMPONENT_REF)
5221
    gnu_field = TREE_OPERAND (gnu_field, 1);
5222
 
5223
  return gnu_field;
5224
}
5225
 
5226
/* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type.  Return
5227
   the GCC type corresponding to that entity.  */
5228
 
5229
tree
5230
gnat_to_gnu_type (Entity_Id gnat_entity)
5231
{
5232
  tree gnu_decl;
5233
 
5234
  /* The back end never attempts to annotate generic types.  */
5235
  if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5236
     return void_type_node;
5237
 
5238
  gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5239
  gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5240
 
5241
  return TREE_TYPE (gnu_decl);
5242
}
5243
 
5244
/* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type.  Return
5245
   the unpadded version of the GCC type corresponding to that entity.  */
5246
 
5247
tree
5248
get_unpadded_type (Entity_Id gnat_entity)
5249
{
5250
  tree type = gnat_to_gnu_type (gnat_entity);
5251
 
5252
  if (TYPE_IS_PADDING_P (type))
5253
    type = TREE_TYPE (TYPE_FIELDS (type));
5254
 
5255
  return type;
5256
}
5257
 
5258
/* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5259
   type has been changed to that of the parameterless procedure, except if an
5260
   alias is already present, in which case it is returned instead.  */
5261
 
5262
tree
5263
get_minimal_subprog_decl (Entity_Id gnat_entity)
5264
{
5265
  tree gnu_entity_name, gnu_ext_name;
5266
  struct attrib *attr_list = NULL;
5267
 
5268
  /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5269
     of the handling applied here.  */
5270
 
5271
  while (Present (Alias (gnat_entity)))
5272
    {
5273
      gnat_entity = Alias (gnat_entity);
5274
      if (present_gnu_tree (gnat_entity))
5275
        return get_gnu_tree (gnat_entity);
5276
    }
5277
 
5278
  gnu_entity_name = get_entity_name (gnat_entity);
5279
  gnu_ext_name = create_concat_name (gnat_entity, NULL);
5280
 
5281
  if (Has_Stdcall_Convention (gnat_entity))
5282
    prepend_one_attribute_to (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5283
                              get_identifier ("stdcall"), NULL_TREE,
5284
                              gnat_entity);
5285
 
5286
  if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5287
    gnu_ext_name = NULL_TREE;
5288
 
5289
  return
5290
    create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5291
                         false, true, true, true, attr_list, gnat_entity);
5292
}
5293
 
5294
/* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
5295
   Every TYPE_DECL generated for a type definition must be passed
5296
   to this function once everything else has been done for it.  */
5297
 
5298
void
5299
rest_of_type_decl_compilation (tree decl)
5300
{
5301
  /* We need to defer finalizing the type if incomplete types
5302
     are being deferred or if they are being processed.  */
5303
  if (defer_incomplete_level != 0 || defer_finalize_level != 0)
5304
    VEC_safe_push (tree, heap, defer_finalize_list, decl);
5305
  else
5306
    rest_of_type_decl_compilation_no_defer (decl);
5307
}
5308
 
5309
/* Same as above but without deferring the compilation.  This
5310
   function should not be invoked directly on a TYPE_DECL.  */
5311
 
5312
static void
5313
rest_of_type_decl_compilation_no_defer (tree decl)
5314
{
5315
  const int toplev = global_bindings_p ();
5316
  tree t = TREE_TYPE (decl);
5317
 
5318
  rest_of_decl_compilation (decl, toplev, 0);
5319
 
5320
  /* Now process all the variants.  This is needed for STABS.  */
5321
  for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5322
    {
5323
      if (t == TREE_TYPE (decl))
5324
        continue;
5325
 
5326
      if (!TYPE_STUB_DECL (t))
5327
        TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5328
 
5329
      rest_of_type_compilation (t, toplev);
5330
    }
5331
}
5332
 
5333
/* Finalize the processing of From_With_Type incomplete types.  */
5334
 
5335
void
5336
finalize_from_with_types (void)
5337
{
5338
  struct incomplete *p, *next;
5339
 
5340
  p = defer_limited_with;
5341
  defer_limited_with = NULL;
5342
 
5343
  for (; p; p = next)
5344
    {
5345
      next = p->next;
5346
 
5347
      if (p->old_type)
5348
        update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5349
                           gnat_to_gnu_type (p->full_type));
5350
      free (p);
5351
    }
5352
}
5353
 
5354
/* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5355
   kind of type (such E_Task_Type) that has a different type which Gigi
5356
   uses for its representation.  If the type does not have a special type
5357
   for its representation, return GNAT_ENTITY.  If a type is supposed to
5358
   exist, but does not, abort unless annotating types, in which case
5359
   return Empty.  If GNAT_ENTITY is Empty, return Empty.  */
5360
 
5361
Entity_Id
5362
Gigi_Equivalent_Type (Entity_Id gnat_entity)
5363
{
5364
  Entity_Id gnat_equiv = gnat_entity;
5365
 
5366
  if (No (gnat_entity))
5367
    return gnat_entity;
5368
 
5369
  switch (Ekind (gnat_entity))
5370
    {
5371
    case E_Class_Wide_Subtype:
5372
      if (Present (Equivalent_Type (gnat_entity)))
5373
        gnat_equiv = Equivalent_Type (gnat_entity);
5374
      break;
5375
 
5376
    case E_Access_Protected_Subprogram_Type:
5377
    case E_Anonymous_Access_Protected_Subprogram_Type:
5378
      gnat_equiv = Equivalent_Type (gnat_entity);
5379
      break;
5380
 
5381
    case E_Class_Wide_Type:
5382
      gnat_equiv = Root_Type (gnat_entity);
5383
      break;
5384
 
5385
    case E_Task_Type:
5386
    case E_Task_Subtype:
5387
    case E_Protected_Type:
5388
    case E_Protected_Subtype:
5389
      gnat_equiv = Corresponding_Record_Type (gnat_entity);
5390
      break;
5391
 
5392
    default:
5393
      break;
5394
    }
5395
 
5396
  gcc_assert (Present (gnat_equiv) || type_annotate_only);
5397
 
5398
  return gnat_equiv;
5399
}
5400
 
5401
/* Return a GCC tree for a type corresponding to the component type of the
5402
   array type or subtype GNAT_ARRAY.  DEFINITION is true if this component
5403
   is for an array being defined.  DEBUG_INFO_P is true if we need to write
5404
   debug information for other types that we may create in the process.  */
5405
 
5406
static tree
5407
gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5408
                            bool debug_info_p)
5409
{
5410
  const Entity_Id gnat_type = Component_Type (gnat_array);
5411
  tree gnu_type = gnat_to_gnu_type (gnat_type);
5412
  tree gnu_comp_size;
5413
 
5414
  /* Try to get a smaller form of the component if needed.  */
5415
  if ((Is_Packed (gnat_array)
5416
       || Has_Component_Size_Clause (gnat_array))
5417
      && !Is_Bit_Packed_Array (gnat_array)
5418
      && !Has_Aliased_Components (gnat_array)
5419
      && !Strict_Alignment (gnat_type)
5420
      && RECORD_OR_UNION_TYPE_P (gnu_type)
5421
      && !TYPE_FAT_POINTER_P (gnu_type)
5422
      && host_integerp (TYPE_SIZE (gnu_type), 1))
5423
    gnu_type = make_packable_type (gnu_type, false);
5424
 
5425
  if (Has_Atomic_Components (gnat_array))
5426
    check_ok_for_atomic (gnu_type, gnat_array, true);
5427
 
5428
  /* Get and validate any specified Component_Size.  */
5429
  gnu_comp_size
5430
    = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5431
                     Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5432
                     true, Has_Component_Size_Clause (gnat_array));
5433
 
5434
  /* If the array has aliased components and the component size can be zero,
5435
     force at least unit size to ensure that the components have distinct
5436
     addresses.  */
5437
  if (!gnu_comp_size
5438
      && Has_Aliased_Components (gnat_array)
5439
      && (integer_zerop (TYPE_SIZE (gnu_type))
5440
          || (TREE_CODE (gnu_type) == ARRAY_TYPE
5441
              && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5442
    gnu_comp_size
5443
      = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5444
 
5445
  /* If the component type is a RECORD_TYPE that has a self-referential size,
5446
     then use the maximum size for the component size.  */
5447
  if (!gnu_comp_size
5448
      && TREE_CODE (gnu_type) == RECORD_TYPE
5449
      && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5450
    gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5451
 
5452
  /* Honor the component size.  This is not needed for bit-packed arrays.  */
5453
  if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5454
    {
5455
      tree orig_type = gnu_type;
5456
      unsigned int max_align;
5457
 
5458
      /* If an alignment is specified, use it as a cap on the component type
5459
         so that it can be honored for the whole type.  But ignore it for the
5460
         original type of packed array types.  */
5461
      if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5462
        max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5463
      else
5464
        max_align = 0;
5465
 
5466
      gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5467
      if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5468
        gnu_type = orig_type;
5469
      else
5470
        orig_type = gnu_type;
5471
 
5472
      gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5473
                                 true, false, definition, true);
5474
 
5475
      /* If a padding record was made, declare it now since it will never be
5476
         declared otherwise.  This is necessary to ensure that its subtrees
5477
         are properly marked.  */
5478
      if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5479
        create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5480
                          debug_info_p, gnat_array);
5481
    }
5482
 
5483
  if (Has_Volatile_Components (gnat_array))
5484
    gnu_type
5485
      = build_qualified_type (gnu_type,
5486
                              TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5487
 
5488
  return gnu_type;
5489
}
5490
 
5491
/* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5492
   using MECH as its passing mechanism, to be placed in the parameter
5493
   list built for GNAT_SUBPROG.  Assume a foreign convention for the
5494
   latter if FOREIGN is true.  Also set CICO to true if the parameter
5495
   must use the copy-in copy-out implementation mechanism.
5496
 
5497
   The returned tree is a PARM_DECL, except for those cases where no
5498
   parameter needs to be actually passed to the subprogram; the type
5499
   of this "shadow" parameter is then returned instead.  */
5500
 
5501
static tree
5502
gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5503
                   Entity_Id gnat_subprog, bool foreign, bool *cico)
5504
{
5505
  tree gnu_param_name = get_entity_name (gnat_param);
5506
  tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5507
  tree gnu_param_type_alt = NULL_TREE;
5508
  bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5509
  /* The parameter can be indirectly modified if its address is taken.  */
5510
  bool ro_param = in_param && !Address_Taken (gnat_param);
5511
  bool by_return = false, by_component_ptr = false;
5512
  bool by_ref = false, by_double_ref = false;
5513
  tree gnu_param;
5514
 
5515
  /* Copy-return is used only for the first parameter of a valued procedure.
5516
     It's a copy mechanism for which a parameter is never allocated.  */
5517
  if (mech == By_Copy_Return)
5518
    {
5519
      gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5520
      mech = By_Copy;
5521
      by_return = true;
5522
    }
5523
 
5524
  /* If this is either a foreign function or if the underlying type won't
5525
     be passed by reference, strip off possible padding type.  */
5526
  if (TYPE_IS_PADDING_P (gnu_param_type))
5527
    {
5528
      tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5529
 
5530
      if (mech == By_Reference
5531
          || foreign
5532
          || (!must_pass_by_ref (unpadded_type)
5533
              && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5534
        gnu_param_type = unpadded_type;
5535
    }
5536
 
5537
  /* If this is a read-only parameter, make a variant of the type that is
5538
     read-only.  ??? However, if this is an unconstrained array, that type
5539
     can be very complex, so skip it for now.  Likewise for any other
5540
     self-referential type.  */
5541
  if (ro_param
5542
      && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5543
      && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5544
    gnu_param_type = build_qualified_type (gnu_param_type,
5545
                                           (TYPE_QUALS (gnu_param_type)
5546
                                            | TYPE_QUAL_CONST));
5547
 
5548
  /* For foreign conventions, pass arrays as pointers to the element type.
5549
     First check for unconstrained array and get the underlying array.  */
5550
  if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5551
    gnu_param_type
5552
      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5553
 
5554
  /* For GCC builtins, pass Address integer types as (void *)  */
5555
  if (Convention (gnat_subprog) == Convention_Intrinsic
5556
      && Present (Interface_Name (gnat_subprog))
5557
      && Is_Descendent_Of_Address (Etype (gnat_param)))
5558
    gnu_param_type = ptr_void_type_node;
5559
 
5560
  /* VMS descriptors are themselves passed by reference.  */
5561
  if (mech == By_Short_Descriptor ||
5562
      (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5563
    gnu_param_type
5564
      = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5565
                                                    Mechanism (gnat_param),
5566
                                                    gnat_subprog));
5567
  else if (mech == By_Descriptor)
5568
    {
5569
      /* Build both a 32-bit and 64-bit descriptor, one of which will be
5570
         chosen in fill_vms_descriptor.  */
5571
      gnu_param_type_alt
5572
        = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5573
                                                      Mechanism (gnat_param),
5574
                                                      gnat_subprog));
5575
      gnu_param_type
5576
        = build_pointer_type (build_vms_descriptor (gnu_param_type,
5577
                                                    Mechanism (gnat_param),
5578
                                                    gnat_subprog));
5579
    }
5580
 
5581
  /* Arrays are passed as pointers to element type for foreign conventions.  */
5582
  else if (foreign
5583
           && mech != By_Copy
5584
           && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5585
    {
5586
      /* Strip off any multi-dimensional entries, then strip
5587
         off the last array to get the component type.  */
5588
      while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5589
             && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5590
        gnu_param_type = TREE_TYPE (gnu_param_type);
5591
 
5592
      by_component_ptr = true;
5593
      gnu_param_type = TREE_TYPE (gnu_param_type);
5594
 
5595
      if (ro_param)
5596
        gnu_param_type = build_qualified_type (gnu_param_type,
5597
                                               (TYPE_QUALS (gnu_param_type)
5598
                                                | TYPE_QUAL_CONST));
5599
 
5600
      gnu_param_type = build_pointer_type (gnu_param_type);
5601
    }
5602
 
5603
  /* Fat pointers are passed as thin pointers for foreign conventions.  */
5604
  else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5605
    gnu_param_type
5606
      = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5607
 
5608
  /* If we must pass or were requested to pass by reference, do so.
5609
     If we were requested to pass by copy, do so.
5610
     Otherwise, for foreign conventions, pass In Out or Out parameters
5611
     or aggregates by reference.  For COBOL and Fortran, pass all
5612
     integer and FP types that way too.  For Convention Ada, use
5613
     the standard Ada default.  */
5614
  else if (must_pass_by_ref (gnu_param_type)
5615
           || mech == By_Reference
5616
           || (mech != By_Copy
5617
               && ((foreign
5618
                    && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5619
                   || (foreign
5620
                       && (Convention (gnat_subprog) == Convention_Fortran
5621
                           || Convention (gnat_subprog) == Convention_COBOL)
5622
                       && (INTEGRAL_TYPE_P (gnu_param_type)
5623
                           || FLOAT_TYPE_P (gnu_param_type)))
5624
                   || (!foreign
5625
                       && default_pass_by_ref (gnu_param_type)))))
5626
    {
5627
      /* We take advantage of 6.2(12) by considering that references built for
5628
         parameters whose type isn't by-ref and for which the mechanism hasn't
5629
         been forced to by-ref are restrict-qualified in the C sense.  */
5630
      bool restrict_p
5631
        = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5632
      gnu_param_type = build_reference_type (gnu_param_type);
5633
      if (restrict_p)
5634
        gnu_param_type
5635
          = build_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5636
      by_ref = true;
5637
 
5638
      /* In some ABIs, e.g. SPARC 32-bit, fat pointer types are themselves
5639
         passed by reference.  Pass them by explicit reference, this will
5640
         generate more debuggable code at -O0.  */
5641
      if (TYPE_IS_FAT_POINTER_P (gnu_param_type)
5642
          && targetm.calls.pass_by_reference (pack_cumulative_args (NULL),
5643
                                              TYPE_MODE (gnu_param_type),
5644
                                              gnu_param_type,
5645
                                              true))
5646
        {
5647
           gnu_param_type = build_reference_type (gnu_param_type);
5648
           by_double_ref = true;
5649
        }
5650
    }
5651
 
5652
  /* Pass In Out or Out parameters using copy-in copy-out mechanism.  */
5653
  else if (!in_param)
5654
    *cico = true;
5655
 
5656
  if (mech == By_Copy && (by_ref || by_component_ptr))
5657
    post_error ("?cannot pass & by copy", gnat_param);
5658
 
5659
  /* If this is an Out parameter that isn't passed by reference and isn't
5660
     a pointer or aggregate, we don't make a PARM_DECL for it.  Instead,
5661
     it will be a VAR_DECL created when we process the procedure, so just
5662
     return its type.  For the special parameter of a valued procedure,
5663
     never pass it in.
5664
 
5665
     An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5666
     Out parameters with discriminants or implicit initial values to be
5667
     handled like In Out parameters.  These type are normally built as
5668
     aggregates, hence passed by reference, except for some packed arrays
5669
     which end up encoded in special integer types.
5670
 
5671
     The exception we need to make is then for packed arrays of records
5672
     with discriminants or implicit initial values.  We have no light/easy
5673
     way to check for the latter case, so we merely check for packed arrays
5674
     of records.  This may lead to useless copy-in operations, but in very
5675
     rare cases only, as these would be exceptions in a set of already
5676
     exceptional situations.  */
5677
  if (Ekind (gnat_param) == E_Out_Parameter
5678
      && !by_ref
5679
      && (by_return
5680
          || (mech != By_Descriptor
5681
              && mech != By_Short_Descriptor
5682
              && !POINTER_TYPE_P (gnu_param_type)
5683
              && !AGGREGATE_TYPE_P (gnu_param_type)))
5684
      && !(Is_Array_Type (Etype (gnat_param))
5685
           && Is_Packed (Etype (gnat_param))
5686
           && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5687
    return gnu_param_type;
5688
 
5689
  gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5690
                                 ro_param || by_ref || by_component_ptr);
5691
  DECL_BY_REF_P (gnu_param) = by_ref;
5692
  DECL_BY_DOUBLE_REF_P (gnu_param) = by_double_ref;
5693
  DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5694
  DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5695
                                      mech == By_Short_Descriptor);
5696
  /* Note that, in case of a parameter passed by double reference, the
5697
     DECL_POINTS_TO_READONLY_P flag is meant for the second reference.
5698
     The first reference always points to read-only, as it points to
5699
     the second reference, i.e. the reference to the actual parameter.  */
5700
  DECL_POINTS_TO_READONLY_P (gnu_param)
5701
    = (ro_param && (by_ref || by_component_ptr));
5702
  DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5703
 
5704
  /* Save the alternate descriptor type, if any.  */
5705
  if (gnu_param_type_alt)
5706
    SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5707
 
5708
  /* If no Mechanism was specified, indicate what we're using, then
5709
     back-annotate it.  */
5710
  if (mech == Default)
5711
    mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5712
 
5713
  Set_Mechanism (gnat_param, mech);
5714
  return gnu_param;
5715
}
5716
 
5717
/* Return true if DISCR1 and DISCR2 represent the same discriminant.  */
5718
 
5719
static bool
5720
same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5721
{
5722
  while (Present (Corresponding_Discriminant (discr1)))
5723
    discr1 = Corresponding_Discriminant (discr1);
5724
 
5725
  while (Present (Corresponding_Discriminant (discr2)))
5726
    discr2 = Corresponding_Discriminant (discr2);
5727
 
5728
  return
5729
    Original_Record_Component (discr1) == Original_Record_Component (discr2);
5730
}
5731
 
5732
/* Return true if the array type GNU_TYPE, which represents a dimension of
5733
   GNAT_TYPE, has a non-aliased component in the back-end sense.  */
5734
 
5735
static bool
5736
array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5737
{
5738
  /* If the array type is not the innermost dimension of the GNAT type,
5739
     then it has a non-aliased component.  */
5740
  if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5741
      && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5742
    return true;
5743
 
5744
  /* If the array type has an aliased component in the front-end sense,
5745
     then it also has an aliased component in the back-end sense.  */
5746
  if (Has_Aliased_Components (gnat_type))
5747
    return false;
5748
 
5749
  /* If this is a derived type, then it has a non-aliased component if
5750
     and only if its parent type also has one.  */
5751
  if (Is_Derived_Type (gnat_type))
5752
    {
5753
      tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5754
      int index;
5755
      if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5756
        gnu_parent_type
5757
          = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5758
      for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5759
        gnu_parent_type = TREE_TYPE (gnu_parent_type);
5760
      return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5761
    }
5762
 
5763
  /* Otherwise, rely exclusively on properties of the element type.  */
5764
  return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5765
}
5766
 
5767
/* Return true if GNAT_ADDRESS is a value known at compile-time.  */
5768
 
5769
static bool
5770
compile_time_known_address_p (Node_Id gnat_address)
5771
{
5772
  /* Catch System'To_Address.  */
5773
  if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5774
    gnat_address = Expression (gnat_address);
5775
 
5776
  return Compile_Time_Known_Value (gnat_address);
5777
}
5778
 
5779
/* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5780
   inequality HB >= LB-1 is true.  LB and HB are the low and high bounds.  */
5781
 
5782
static bool
5783
cannot_be_superflat_p (Node_Id gnat_range)
5784
{
5785
  Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5786
  Node_Id scalar_range;
5787
  tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5788
 
5789
  /* If the low bound is not constant, try to find an upper bound.  */
5790
  while (Nkind (gnat_lb) != N_Integer_Literal
5791
         && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5792
             || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5793
         && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5794
         && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5795
             || Nkind (scalar_range) == N_Range))
5796
    gnat_lb = High_Bound (scalar_range);
5797
 
5798
  /* If the high bound is not constant, try to find a lower bound.  */
5799
  while (Nkind (gnat_hb) != N_Integer_Literal
5800
         && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5801
             || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5802
         && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5803
         && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5804
             || Nkind (scalar_range) == N_Range))
5805
    gnat_hb = Low_Bound (scalar_range);
5806
 
5807
  /* If we have failed to find constant bounds, punt.  */
5808
  if (Nkind (gnat_lb) != N_Integer_Literal
5809
      || Nkind (gnat_hb) != N_Integer_Literal)
5810
    return false;
5811
 
5812
  /* We need at least a signed 64-bit type to catch most cases.  */
5813
  gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5814
  gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5815
  if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5816
    return false;
5817
 
5818
  /* If the low bound is the smallest integer, nothing can be smaller.  */
5819
  gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5820
  if (TREE_OVERFLOW (gnu_lb_minus_one))
5821
    return true;
5822
 
5823
  return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5824
}
5825
 
5826
/* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR.  */
5827
 
5828
static bool
5829
constructor_address_p (tree gnu_expr)
5830
{
5831
  while (TREE_CODE (gnu_expr) == NOP_EXPR
5832
         || TREE_CODE (gnu_expr) == CONVERT_EXPR
5833
         || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5834
    gnu_expr = TREE_OPERAND (gnu_expr, 0);
5835
 
5836
  return (TREE_CODE (gnu_expr) == ADDR_EXPR
5837
          && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5838
}
5839
 
5840
/* Given GNAT_ENTITY, elaborate all expressions that are required to
5841
   be elaborated at the point of its definition, but do nothing else.  */
5842
 
5843
void
5844
elaborate_entity (Entity_Id gnat_entity)
5845
{
5846
  switch (Ekind (gnat_entity))
5847
    {
5848
    case E_Signed_Integer_Subtype:
5849
    case E_Modular_Integer_Subtype:
5850
    case E_Enumeration_Subtype:
5851
    case E_Ordinary_Fixed_Point_Subtype:
5852
    case E_Decimal_Fixed_Point_Subtype:
5853
    case E_Floating_Point_Subtype:
5854
      {
5855
        Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5856
        Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5857
 
5858
        /* ??? Tests to avoid Constraint_Error in static expressions
5859
           are needed until after the front stops generating bogus
5860
           conversions on bounds of real types.  */
5861
        if (!Raises_Constraint_Error (gnat_lb))
5862
          elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5863
                                true, false, Needs_Debug_Info (gnat_entity));
5864
        if (!Raises_Constraint_Error (gnat_hb))
5865
          elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5866
                                true, false, Needs_Debug_Info (gnat_entity));
5867
      break;
5868
      }
5869
 
5870
    case E_Record_Type:
5871
      {
5872
        Node_Id full_definition = Declaration_Node (gnat_entity);
5873
        Node_Id record_definition = Type_Definition (full_definition);
5874
 
5875
        /* If this is a record extension, go a level further to find the
5876
           record definition.  */
5877
        if (Nkind (record_definition) == N_Derived_Type_Definition)
5878
          record_definition = Record_Extension_Part (record_definition);
5879
      }
5880
      break;
5881
 
5882
    case E_Record_Subtype:
5883
    case E_Private_Subtype:
5884
    case E_Limited_Private_Subtype:
5885
    case E_Record_Subtype_With_Private:
5886
      if (Is_Constrained (gnat_entity)
5887
          && Has_Discriminants (gnat_entity)
5888
          && Present (Discriminant_Constraint (gnat_entity)))
5889
        {
5890
          Node_Id gnat_discriminant_expr;
5891
          Entity_Id gnat_field;
5892
 
5893
          for (gnat_field
5894
               = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5895
               gnat_discriminant_expr
5896
               = First_Elmt (Discriminant_Constraint (gnat_entity));
5897
               Present (gnat_field);
5898
               gnat_field = Next_Discriminant (gnat_field),
5899
               gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5900
            /* ??? For now, ignore access discriminants.  */
5901
            if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5902
              elaborate_expression (Node (gnat_discriminant_expr),
5903
                                    gnat_entity, get_entity_name (gnat_field),
5904
                                    true, false, false);
5905
        }
5906
      break;
5907
 
5908
    }
5909
}
5910
 
5911
/* Mark GNAT_ENTITY as going out of scope at this point.  Recursively mark
5912
   any entities on its entity chain similarly.  */
5913
 
5914
void
5915
mark_out_of_scope (Entity_Id gnat_entity)
5916
{
5917
  Entity_Id gnat_sub_entity;
5918
  unsigned int kind = Ekind (gnat_entity);
5919
 
5920
  /* If this has an entity list, process all in the list.  */
5921
  if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5922
      || IN (kind, Private_Kind)
5923
      || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5924
      || kind == E_Function || kind == E_Generic_Function
5925
      || kind == E_Generic_Package || kind == E_Generic_Procedure
5926
      || kind == E_Loop || kind == E_Operator || kind == E_Package
5927
      || kind == E_Package_Body || kind == E_Procedure
5928
      || kind == E_Record_Type || kind == E_Record_Subtype
5929
      || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5930
    for (gnat_sub_entity = First_Entity (gnat_entity);
5931
         Present (gnat_sub_entity);
5932
         gnat_sub_entity = Next_Entity (gnat_sub_entity))
5933
      if (Scope (gnat_sub_entity) == gnat_entity
5934
          && gnat_sub_entity != gnat_entity)
5935
        mark_out_of_scope (gnat_sub_entity);
5936
 
5937
  /* Now clear this if it has been defined, but only do so if it isn't
5938
     a subprogram or parameter.  We could refine this, but it isn't
5939
     worth it.  If this is statically allocated, it is supposed to
5940
     hang around out of cope.  */
5941
  if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5942
      && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5943
    {
5944
      save_gnu_tree (gnat_entity, NULL_TREE, true);
5945
      save_gnu_tree (gnat_entity, error_mark_node, true);
5946
    }
5947
}
5948
 
5949
/* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5950
   If this is a multi-dimensional array type, do this recursively.
5951
 
5952
   OP may be
5953
   - ALIAS_SET_COPY:     the new set is made a copy of the old one.
5954
   - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5955
   - ALIAS_SET_SUBSET:   the new set is made a subset of the old one.  */
5956
 
5957
static void
5958
relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5959
{
5960
  /* Remove any padding from GNU_OLD_TYPE.  It doesn't matter in the case
5961
     of a one-dimensional array, since the padding has the same alias set
5962
     as the field type, but if it's a multi-dimensional array, we need to
5963
     see the inner types.  */
5964
  while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5965
         && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5966
             || TYPE_PADDING_P (gnu_old_type)))
5967
    gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5968
 
5969
  /* Unconstrained array types are deemed incomplete and would thus be given
5970
     alias set 0.  Retrieve the underlying array type.  */
5971
  if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5972
    gnu_old_type
5973
      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5974
  if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5975
    gnu_new_type
5976
      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5977
 
5978
  if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5979
      && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5980
      && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5981
    relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5982
 
5983
  switch (op)
5984
    {
5985
    case ALIAS_SET_COPY:
5986
      /* The alias set shouldn't be copied between array types with different
5987
         aliasing settings because this can break the aliasing relationship
5988
         between the array type and its element type.  */
5989
#ifndef ENABLE_CHECKING
5990
      if (flag_strict_aliasing)
5991
#endif
5992
        gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5993
                      && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5994
                      && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5995
                         != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5996
 
5997
      TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5998
      break;
5999
 
6000
    case ALIAS_SET_SUBSET:
6001
    case ALIAS_SET_SUPERSET:
6002
      {
6003
        alias_set_type old_set = get_alias_set (gnu_old_type);
6004
        alias_set_type new_set = get_alias_set (gnu_new_type);
6005
 
6006
        /* Do nothing if the alias sets conflict.  This ensures that we
6007
           never call record_alias_subset several times for the same pair
6008
           or at all for alias set 0.  */
6009
        if (!alias_sets_conflict_p (old_set, new_set))
6010
          {
6011
            if (op == ALIAS_SET_SUBSET)
6012
              record_alias_subset (old_set, new_set);
6013
            else
6014
              record_alias_subset (new_set, old_set);
6015
          }
6016
      }
6017
      break;
6018
 
6019
    default:
6020
      gcc_unreachable ();
6021
    }
6022
 
6023
  record_component_aliases (gnu_new_type);
6024
}
6025
 
6026
/* Return true if the size represented by GNU_SIZE can be handled by an
6027
   allocation.  If STATIC_P is true, consider only what can be done with a
6028
   static allocation.  */
6029
 
6030
static bool
6031
allocatable_size_p (tree gnu_size, bool static_p)
6032
{
6033
  HOST_WIDE_INT our_size;
6034
 
6035
  /* If this is not a static allocation, the only case we want to forbid
6036
     is an overflowing size.  That will be converted into a raise a
6037
     Storage_Error.  */
6038
  if (!static_p)
6039
    return !(TREE_CODE (gnu_size) == INTEGER_CST
6040
             && TREE_OVERFLOW (gnu_size));
6041
 
6042
  /* Otherwise, we need to deal with both variable sizes and constant
6043
     sizes that won't fit in a host int.  We use int instead of HOST_WIDE_INT
6044
     since assemblers may not like very large sizes.  */
6045
  if (!host_integerp (gnu_size, 1))
6046
    return false;
6047
 
6048
  our_size = tree_low_cst (gnu_size, 1);
6049
  return (int) our_size == our_size;
6050
}
6051
 
6052
/* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6053
   NAME, ARGS and ERROR_POINT.  */
6054
 
6055
static void
6056
prepend_one_attribute_to (struct attrib ** attr_list,
6057
                          enum attr_type attr_type,
6058
                          tree attr_name,
6059
                          tree attr_args,
6060
                          Node_Id attr_error_point)
6061
{
6062
  struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6063
 
6064
  attr->type = attr_type;
6065
  attr->name = attr_name;
6066
  attr->args = attr_args;
6067
  attr->error_point = attr_error_point;
6068
 
6069
  attr->next = *attr_list;
6070
  *attr_list = attr;
6071
}
6072
 
6073
/* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any.  */
6074
 
6075
static void
6076
prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
6077
{
6078
  Node_Id gnat_temp;
6079
 
6080
  /* Attributes are stored as Representation Item pragmas.  */
6081
 
6082
  for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
6083
       gnat_temp = Next_Rep_Item (gnat_temp))
6084
    if (Nkind (gnat_temp) == N_Pragma)
6085
      {
6086
        tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6087
        Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
6088
        enum attr_type etype;
6089
 
6090
        /* Map the kind of pragma at hand.  Skip if this is not one
6091
           we know how to handle.  */
6092
 
6093
        switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
6094
          {
6095
          case Pragma_Machine_Attribute:
6096
            etype = ATTR_MACHINE_ATTRIBUTE;
6097
            break;
6098
 
6099
          case Pragma_Linker_Alias:
6100
            etype = ATTR_LINK_ALIAS;
6101
            break;
6102
 
6103
          case Pragma_Linker_Section:
6104
            etype = ATTR_LINK_SECTION;
6105
            break;
6106
 
6107
          case Pragma_Linker_Constructor:
6108
            etype = ATTR_LINK_CONSTRUCTOR;
6109
            break;
6110
 
6111
          case Pragma_Linker_Destructor:
6112
            etype = ATTR_LINK_DESTRUCTOR;
6113
            break;
6114
 
6115
          case Pragma_Weak_External:
6116
            etype = ATTR_WEAK_EXTERNAL;
6117
            break;
6118
 
6119
          case Pragma_Thread_Local_Storage:
6120
            etype = ATTR_THREAD_LOCAL_STORAGE;
6121
            break;
6122
 
6123
          default:
6124
            continue;
6125
          }
6126
 
6127
        /* See what arguments we have and turn them into GCC trees for
6128
           attribute handlers.  These expect identifier for strings.  We
6129
           handle at most two arguments, static expressions only.  */
6130
 
6131
        if (Present (gnat_assoc) && Present (First (gnat_assoc)))
6132
          {
6133
            Node_Id gnat_arg0 = Next (First (gnat_assoc));
6134
            Node_Id gnat_arg1 = Empty;
6135
 
6136
            if (Present (gnat_arg0)
6137
                && Is_Static_Expression (Expression (gnat_arg0)))
6138
              {
6139
                gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6140
 
6141
                if (TREE_CODE (gnu_arg0) == STRING_CST)
6142
                  gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6143
 
6144
                gnat_arg1 = Next (gnat_arg0);
6145
              }
6146
 
6147
            if (Present (gnat_arg1)
6148
                && Is_Static_Expression (Expression (gnat_arg1)))
6149
              {
6150
                gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6151
 
6152
                if (TREE_CODE (gnu_arg1) == STRING_CST)
6153
                  gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6154
              }
6155
          }
6156
 
6157
        /* Prepend to the list now.  Make a list of the argument we might
6158
           have, as GCC expects it.  */
6159
        prepend_one_attribute_to
6160
          (attr_list,
6161
           etype, gnu_arg0,
6162
           (gnu_arg1 != NULL_TREE)
6163
           ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6164
           Present (Next (First (gnat_assoc)))
6165
           ? Expression (Next (First (gnat_assoc))) : gnat_temp);
6166
      }
6167
}
6168
 
6169
/* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6170
   type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6171
   return the GCC tree to use for that expression.  GNU_NAME is the suffix
6172
   to use if a variable needs to be created and DEFINITION is true if this
6173
   is a definition of GNAT_ENTITY.  If NEED_VALUE is true, we need a result;
6174
   otherwise, we are just elaborating the expression for side-effects.  If
6175
   NEED_DEBUG is true, we need a variable for debugging purposes even if it
6176
   isn't needed for code generation.  */
6177
 
6178
static tree
6179
elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6180
                      bool definition, bool need_value, bool need_debug)
6181
{
6182
  tree gnu_expr;
6183
 
6184
  /* If we already elaborated this expression (e.g. it was involved
6185
     in the definition of a private type), use the old value.  */
6186
  if (present_gnu_tree (gnat_expr))
6187
    return get_gnu_tree (gnat_expr);
6188
 
6189
  /* If we don't need a value and this is static or a discriminant,
6190
     we don't need to do anything.  */
6191
  if (!need_value
6192
      && (Is_OK_Static_Expression (gnat_expr)
6193
          || (Nkind (gnat_expr) == N_Identifier
6194
              && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6195
    return NULL_TREE;
6196
 
6197
  /* If it's a static expression, we don't need a variable for debugging.  */
6198
  if (need_debug && Is_OK_Static_Expression (gnat_expr))
6199
    need_debug = false;
6200
 
6201
  /* Otherwise, convert this tree to its GCC equivalent and elaborate it.  */
6202
  gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6203
                                     gnu_name, definition, need_debug);
6204
 
6205
  /* Save the expression in case we try to elaborate this entity again.  Since
6206
     it's not a DECL, don't check it.  Don't save if it's a discriminant.  */
6207
  if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6208
    save_gnu_tree (gnat_expr, gnu_expr, true);
6209
 
6210
  return need_value ? gnu_expr : error_mark_node;
6211
}
6212
 
6213
/* Similar, but take a GNU expression and always return a result.  */
6214
 
6215
static tree
6216
elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6217
                        bool definition, bool need_debug)
6218
{
6219
  const bool expr_public_p = Is_Public (gnat_entity);
6220
  const bool expr_global_p = expr_public_p || global_bindings_p ();
6221
  bool expr_variable_p, use_variable;
6222
 
6223
  /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6224
     reference will have been replaced with a COMPONENT_REF when the type
6225
     is being elaborated.  However, there are some cases involving child
6226
     types where we will.  So convert it to a COMPONENT_REF.  We hope it
6227
     will be at the highest level of the expression in these cases.  */
6228
  if (TREE_CODE (gnu_expr) == FIELD_DECL)
6229
    gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6230
                       build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6231
                       gnu_expr, NULL_TREE);
6232
 
6233
  /* If GNU_EXPR contains a placeholder, just return it.  We rely on the fact
6234
     that an expression cannot contain both a discriminant and a variable.  */
6235
  if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6236
    return gnu_expr;
6237
 
6238
  /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6239
     a variable that is initialized to contain the expression when the package
6240
     containing the definition is elaborated.  If this entity is defined at top
6241
     level, replace the expression by the variable; otherwise use a SAVE_EXPR
6242
     if this is necessary.  */
6243
  if (CONSTANT_CLASS_P (gnu_expr))
6244
    expr_variable_p = false;
6245
  else
6246
    {
6247
      /* Skip any conversions and simple arithmetics to see if the expression
6248
         is based on a read-only variable.
6249
         ??? This really should remain read-only, but we have to think about
6250
         the typing of the tree here.  */
6251
      tree inner
6252
        = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
6253
 
6254
      if (handled_component_p (inner))
6255
        {
6256
          HOST_WIDE_INT bitsize, bitpos;
6257
          tree offset;
6258
          enum machine_mode mode;
6259
          int unsignedp, volatilep;
6260
 
6261
          inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6262
                                       &mode, &unsignedp, &volatilep, false);
6263
          /* If the offset is variable, err on the side of caution.  */
6264
          if (offset)
6265
            inner = NULL_TREE;
6266
        }
6267
 
6268
      expr_variable_p
6269
        = !(inner
6270
            && TREE_CODE (inner) == VAR_DECL
6271
            && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6272
    }
6273
 
6274
  /* We only need to use the variable if we are in a global context since GCC
6275
     can do the right thing in the local case.  However, when not optimizing,
6276
     use it for bounds of loop iteration scheme to avoid code duplication.  */
6277
  use_variable = expr_variable_p
6278
                 && (expr_global_p
6279
                     || (!optimize
6280
                         && Is_Itype (gnat_entity)
6281
                         && Nkind (Associated_Node_For_Itype (gnat_entity))
6282
                            == N_Loop_Parameter_Specification));
6283
 
6284
  /* Now create it, possibly only for debugging purposes.  */
6285
  if (use_variable || need_debug)
6286
    {
6287
      tree gnu_decl
6288
        = create_var_decl_1
6289
          (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6290
           NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6291
           !definition, expr_global_p, !need_debug, NULL, gnat_entity);
6292
 
6293
      if (use_variable)
6294
        return gnu_decl;
6295
    }
6296
 
6297
  return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6298
}
6299
 
6300
/* Similar, but take an alignment factor and make it explicit in the tree.  */
6301
 
6302
static tree
6303
elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6304
                        bool definition, bool need_debug, unsigned int align)
6305
{
6306
  tree unit_align = size_int (align / BITS_PER_UNIT);
6307
  return
6308
    size_binop (MULT_EXPR,
6309
                elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6310
                                                    gnu_expr,
6311
                                                    unit_align),
6312
                                        gnat_entity, gnu_name, definition,
6313
                                        need_debug),
6314
                unit_align);
6315
}
6316
 
6317
/* Create a record type that contains a SIZE bytes long field of TYPE with a
6318
   starting bit position so that it is aligned to ALIGN bits, and leaving at
6319
   least ROOM bytes free before the field.  BASE_ALIGN is the alignment the
6320
   record is guaranteed to get.  */
6321
 
6322
tree
6323
make_aligning_type (tree type, unsigned int align, tree size,
6324
                    unsigned int base_align, int room)
6325
{
6326
  /* We will be crafting a record type with one field at a position set to be
6327
     the next multiple of ALIGN past record'address + room bytes.  We use a
6328
     record placeholder to express record'address.  */
6329
  tree record_type = make_node (RECORD_TYPE);
6330
  tree record = build0 (PLACEHOLDER_EXPR, record_type);
6331
 
6332
  tree record_addr_st
6333
    = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
6334
 
6335
  /* The diagram below summarizes the shape of what we manipulate:
6336
 
6337
                    <--------- pos ---------->
6338
                {  +------------+-------------+-----------------+
6339
      record  =>{  |############|     ...     | field (type)    |
6340
                {  +------------+-------------+-----------------+
6341
                   |<-- room -->|<- voffset ->|<---- size ----->|
6342
                   o            o
6343
                   |            |
6344
                   record_addr  vblock_addr
6345
 
6346
     Every length is in sizetype bytes there, except "pos" which has to be
6347
     set as a bit position in the GCC tree for the record.  */
6348
  tree room_st = size_int (room);
6349
  tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
6350
  tree voffset_st, pos, field;
6351
 
6352
  tree name = TYPE_NAME (type);
6353
 
6354
  if (TREE_CODE (name) == TYPE_DECL)
6355
    name = DECL_NAME (name);
6356
  name = concat_name (name, "ALIGN");
6357
  TYPE_NAME (record_type) = name;
6358
 
6359
  /* Compute VOFFSET and then POS.  The next byte position multiple of some
6360
     alignment after some address is obtained by "and"ing the alignment minus
6361
     1 with the two's complement of the address.   */
6362
  voffset_st = size_binop (BIT_AND_EXPR,
6363
                           fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
6364
                           size_int ((align / BITS_PER_UNIT) - 1));
6365
 
6366
  /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype.  */
6367
  pos = size_binop (MULT_EXPR,
6368
                    convert (bitsizetype,
6369
                             size_binop (PLUS_EXPR, room_st, voffset_st)),
6370
                    bitsize_unit_node);
6371
 
6372
  /* Craft the GCC record representation.  We exceptionally do everything
6373
     manually here because 1) our generic circuitry is not quite ready to
6374
     handle the complex position/size expressions we are setting up, 2) we
6375
     have a strong simplifying factor at hand: we know the maximum possible
6376
     value of voffset, and 3) we have to set/reset at least the sizes in
6377
     accordance with this maximum value anyway, as we need them to convey
6378
     what should be "alloc"ated for this type.
6379
 
6380
     Use -1 as the 'addressable' indication for the field to prevent the
6381
     creation of a bitfield.  We don't need one, it would have damaging
6382
     consequences on the alignment computation, and create_field_decl would
6383
     make one without this special argument, for instance because of the
6384
     complex position expression.  */
6385
  field = create_field_decl (get_identifier ("F"), type, record_type, size,
6386
                             pos, 1, -1);
6387
  TYPE_FIELDS (record_type) = field;
6388
 
6389
  TYPE_ALIGN (record_type) = base_align;
6390
  TYPE_USER_ALIGN (record_type) = 1;
6391
 
6392
  TYPE_SIZE (record_type)
6393
    = size_binop (PLUS_EXPR,
6394
                  size_binop (MULT_EXPR, convert (bitsizetype, size),
6395
                              bitsize_unit_node),
6396
                  bitsize_int (align + room * BITS_PER_UNIT));
6397
  TYPE_SIZE_UNIT (record_type)
6398
    = size_binop (PLUS_EXPR, size,
6399
                  size_int (room + align / BITS_PER_UNIT));
6400
 
6401
  SET_TYPE_MODE (record_type, BLKmode);
6402
  relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6403
 
6404
  /* Declare it now since it will never be declared otherwise.  This is
6405
     necessary to ensure that its subtrees are properly marked.  */
6406
  create_type_decl (name, record_type, NULL, true, false, Empty);
6407
 
6408
  return record_type;
6409
}
6410
 
6411
/* Return the result of rounding T up to ALIGN.  */
6412
 
6413
static inline unsigned HOST_WIDE_INT
6414
round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6415
{
6416
  t += align - 1;
6417
  t /= align;
6418
  t *= align;
6419
  return t;
6420
}
6421
 
6422
/* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6423
   as the field type of a packed record if IN_RECORD is true, or as the
6424
   component type of a packed array if IN_RECORD is false.  See if we can
6425
   rewrite it either as a type that has a non-BLKmode, which we can pack
6426
   tighter in the packed record case, or as a smaller type.  If so, return
6427
   the new type.  If not, return the original type.  */
6428
 
6429
static tree
6430
make_packable_type (tree type, bool in_record)
6431
{
6432
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6433
  unsigned HOST_WIDE_INT new_size;
6434
  tree new_type, old_field, field_list = NULL_TREE;
6435
 
6436
  /* No point in doing anything if the size is zero.  */
6437
  if (size == 0)
6438
    return type;
6439
 
6440
  new_type = make_node (TREE_CODE (type));
6441
 
6442
  /* Copy the name and flags from the old type to that of the new.
6443
     Note that we rely on the pointer equality created here for
6444
     TYPE_NAME to look through conversions in various places.  */
6445
  TYPE_NAME (new_type) = TYPE_NAME (type);
6446
  TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6447
  TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6448
  if (TREE_CODE (type) == RECORD_TYPE)
6449
    TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6450
 
6451
  /* If we are in a record and have a small size, set the alignment to
6452
     try for an integral mode.  Otherwise set it to try for a smaller
6453
     type with BLKmode.  */
6454
  if (in_record && size <= MAX_FIXED_MODE_SIZE)
6455
    {
6456
      TYPE_ALIGN (new_type) = ceil_alignment (size);
6457
      new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6458
    }
6459
  else
6460
    {
6461
      unsigned HOST_WIDE_INT align;
6462
 
6463
      /* Do not try to shrink the size if the RM size is not constant.  */
6464
      if (TYPE_CONTAINS_TEMPLATE_P (type)
6465
          || !host_integerp (TYPE_ADA_SIZE (type), 1))
6466
        return type;
6467
 
6468
      /* Round the RM size up to a unit boundary to get the minimal size
6469
         for a BLKmode record.  Give up if it's already the size.  */
6470
      new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6471
      new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6472
      if (new_size == size)
6473
        return type;
6474
 
6475
      align = new_size & -new_size;
6476
      TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6477
    }
6478
 
6479
  TYPE_USER_ALIGN (new_type) = 1;
6480
 
6481
  /* Now copy the fields, keeping the position and size as we don't want
6482
     to change the layout by propagating the packedness downwards.  */
6483
  for (old_field = TYPE_FIELDS (type); old_field;
6484
       old_field = DECL_CHAIN (old_field))
6485
    {
6486
      tree new_field_type = TREE_TYPE (old_field);
6487
      tree new_field, new_size;
6488
 
6489
      if (RECORD_OR_UNION_TYPE_P (new_field_type)
6490
          && !TYPE_FAT_POINTER_P (new_field_type)
6491
          && host_integerp (TYPE_SIZE (new_field_type), 1))
6492
        new_field_type = make_packable_type (new_field_type, true);
6493
 
6494
      /* However, for the last field in a not already packed record type
6495
         that is of an aggregate type, we need to use the RM size in the
6496
         packable version of the record type, see finish_record_type.  */
6497
      if (!DECL_CHAIN (old_field)
6498
          && !TYPE_PACKED (type)
6499
          && RECORD_OR_UNION_TYPE_P (new_field_type)
6500
          && !TYPE_FAT_POINTER_P (new_field_type)
6501
          && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6502
          && TYPE_ADA_SIZE (new_field_type))
6503
        new_size = TYPE_ADA_SIZE (new_field_type);
6504
      else
6505
        new_size = DECL_SIZE (old_field);
6506
 
6507
      new_field
6508
        = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6509
                             new_size, bit_position (old_field),
6510
                             TYPE_PACKED (type),
6511
                             !DECL_NONADDRESSABLE_P (old_field));
6512
 
6513
      DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6514
      SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6515
      if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6516
        DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6517
 
6518
      DECL_CHAIN (new_field) = field_list;
6519
      field_list = new_field;
6520
    }
6521
 
6522
  finish_record_type (new_type, nreverse (field_list), 2, false);
6523
  relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6524
  SET_DECL_PARALLEL_TYPE (TYPE_STUB_DECL (new_type),
6525
                          DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type)));
6526
 
6527
  /* If this is a padding record, we never want to make the size smaller
6528
     than what was specified.  For QUAL_UNION_TYPE, also copy the size.  */
6529
  if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6530
    {
6531
      TYPE_SIZE (new_type) = TYPE_SIZE (type);
6532
      TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6533
      new_size = size;
6534
    }
6535
  else
6536
    {
6537
      TYPE_SIZE (new_type) = bitsize_int (new_size);
6538
      TYPE_SIZE_UNIT (new_type)
6539
        = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6540
    }
6541
 
6542
  if (!TYPE_CONTAINS_TEMPLATE_P (type))
6543
    SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6544
 
6545
  compute_record_mode (new_type);
6546
 
6547
  /* Try harder to get a packable type if necessary, for example
6548
     in case the record itself contains a BLKmode field.  */
6549
  if (in_record && TYPE_MODE (new_type) == BLKmode)
6550
    SET_TYPE_MODE (new_type,
6551
                   mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6552
 
6553
  /* If neither the mode nor the size has shrunk, return the old type.  */
6554
  if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6555
    return type;
6556
 
6557
  return new_type;
6558
}
6559
 
6560
/* Ensure that TYPE has SIZE and ALIGN.  Make and return a new padded type
6561
   if needed.  We have already verified that SIZE and TYPE are large enough.
6562
   GNAT_ENTITY is used to name the resulting record and to issue a warning.
6563
   IS_COMPONENT_TYPE is true if this is being done for the component type
6564
   of an array.  IS_USER_TYPE is true if we must complete the original type.
6565
   DEFINITION is true if this type is being defined.  SAME_RM_SIZE is true
6566
   if the RM size of the resulting type is to be set to SIZE too; otherwise,
6567
   it's set to the RM size of the original type.  */
6568
 
6569
tree
6570
maybe_pad_type (tree type, tree size, unsigned int align,
6571
                Entity_Id gnat_entity, bool is_component_type,
6572
                bool is_user_type, bool definition, bool same_rm_size)
6573
{
6574
  tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6575
  tree orig_size = TYPE_SIZE (type);
6576
  tree record, field;
6577
 
6578
  /* If TYPE is a padded type, see if it agrees with any size and alignment
6579
     we were given.  If so, return the original type.  Otherwise, strip
6580
     off the padding, since we will either be returning the inner type
6581
     or repadding it.  If no size or alignment is specified, use that of
6582
     the original padded type.  */
6583
  if (TYPE_IS_PADDING_P (type))
6584
    {
6585
      if ((!size
6586
           || operand_equal_p (round_up (size,
6587
                                         MAX (align, TYPE_ALIGN (type))),
6588
                               round_up (TYPE_SIZE (type),
6589
                                         MAX (align, TYPE_ALIGN (type))),
6590
                               0))
6591
          && (align == 0 || align == TYPE_ALIGN (type)))
6592
        return type;
6593
 
6594
      if (!size)
6595
        size = TYPE_SIZE (type);
6596
      if (align == 0)
6597
        align = TYPE_ALIGN (type);
6598
 
6599
      type = TREE_TYPE (TYPE_FIELDS (type));
6600
      orig_size = TYPE_SIZE (type);
6601
    }
6602
 
6603
  /* If the size is either not being changed or is being made smaller (which
6604
     is not done here and is only valid for bitfields anyway), show the size
6605
     isn't changing.  Likewise, clear the alignment if it isn't being
6606
     changed.  Then return if we aren't doing anything.  */
6607
  if (size
6608
      && (operand_equal_p (size, orig_size, 0)
6609
          || (TREE_CODE (orig_size) == INTEGER_CST
6610
              && tree_int_cst_lt (size, orig_size))))
6611
    size = NULL_TREE;
6612
 
6613
  if (align == TYPE_ALIGN (type))
6614
    align = 0;
6615
 
6616
  if (align == 0 && !size)
6617
    return type;
6618
 
6619
  /* If requested, complete the original type and give it a name.  */
6620
  if (is_user_type)
6621
    create_type_decl (get_entity_name (gnat_entity), type,
6622
                      NULL, !Comes_From_Source (gnat_entity),
6623
                      !(TYPE_NAME (type)
6624
                        && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6625
                        && DECL_IGNORED_P (TYPE_NAME (type))),
6626
                      gnat_entity);
6627
 
6628
  /* We used to modify the record in place in some cases, but that could
6629
     generate incorrect debugging information.  So make a new record
6630
     type and name.  */
6631
  record = make_node (RECORD_TYPE);
6632
  TYPE_PADDING_P (record) = 1;
6633
 
6634
  if (Present (gnat_entity))
6635
    TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6636
 
6637
  TYPE_VOLATILE (record)
6638
    = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6639
 
6640
  TYPE_ALIGN (record) = align;
6641
  TYPE_SIZE (record) = size ? size : orig_size;
6642
  TYPE_SIZE_UNIT (record)
6643
    = convert (sizetype,
6644
               size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6645
                           bitsize_unit_node));
6646
 
6647
  /* If we are changing the alignment and the input type is a record with
6648
     BLKmode and a small constant size, try to make a form that has an
6649
     integral mode.  This might allow the padding record to also have an
6650
     integral mode, which will be much more efficient.  There is no point
6651
     in doing so if a size is specified unless it is also a small constant
6652
     size and it is incorrect to do so if we cannot guarantee that the mode
6653
     will be naturally aligned since the field must always be addressable.
6654
 
6655
     ??? This might not always be a win when done for a stand-alone object:
6656
     since the nominal and the effective type of the object will now have
6657
     different modes, a VIEW_CONVERT_EXPR will be required for converting
6658
     between them and it might be hard to overcome afterwards, including
6659
     at the RTL level when the stand-alone object is accessed as a whole.  */
6660
  if (align != 0
6661
      && RECORD_OR_UNION_TYPE_P (type)
6662
      && TYPE_MODE (type) == BLKmode
6663
      && !TYPE_BY_REFERENCE_P (type)
6664
      && TREE_CODE (orig_size) == INTEGER_CST
6665
      && !TREE_OVERFLOW (orig_size)
6666
      && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6667
      && (!size
6668
          || (TREE_CODE (size) == INTEGER_CST
6669
              && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6670
    {
6671
      tree packable_type = make_packable_type (type, true);
6672
      if (TYPE_MODE (packable_type) != BLKmode
6673
          && align >= TYPE_ALIGN (packable_type))
6674
        type = packable_type;
6675
    }
6676
 
6677
  /* Now create the field with the original size.  */
6678
  field  = create_field_decl (get_identifier ("F"), type, record, orig_size,
6679
                              bitsize_zero_node, 0, 1);
6680
  DECL_INTERNAL_P (field) = 1;
6681
 
6682
  /* Do not emit debug info until after the auxiliary record is built.  */
6683
  finish_record_type (record, field, 1, false);
6684
 
6685
  /* Set the same size for its RM size if requested; otherwise reuse
6686
     the RM size of the original type.  */
6687
  SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6688
 
6689
  /* Unless debugging information isn't being written for the input type,
6690
     write a record that shows what we are a subtype of and also make a
6691
     variable that indicates our size, if still variable.  */
6692
  if (TREE_CODE (orig_size) != INTEGER_CST
6693
      && TYPE_NAME (record)
6694
      && TYPE_NAME (type)
6695
      && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6696
           && DECL_IGNORED_P (TYPE_NAME (type))))
6697
    {
6698
      tree marker = make_node (RECORD_TYPE);
6699
      tree name = TYPE_NAME (record);
6700
      tree orig_name = TYPE_NAME (type);
6701
 
6702
      if (TREE_CODE (name) == TYPE_DECL)
6703
        name = DECL_NAME (name);
6704
 
6705
      if (TREE_CODE (orig_name) == TYPE_DECL)
6706
        orig_name = DECL_NAME (orig_name);
6707
 
6708
      TYPE_NAME (marker) = concat_name (name, "XVS");
6709
      finish_record_type (marker,
6710
                          create_field_decl (orig_name,
6711
                                             build_reference_type (type),
6712
                                             marker, NULL_TREE, NULL_TREE,
6713
                                             0, 0),
6714
                          0, true);
6715
 
6716
      add_parallel_type (TYPE_STUB_DECL (record), marker);
6717
 
6718
      if (definition && size && TREE_CODE (size) != INTEGER_CST)
6719
        TYPE_SIZE_UNIT (marker)
6720
          = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6721
                             TYPE_SIZE_UNIT (record), false, false, false,
6722
                             false, NULL, gnat_entity);
6723
    }
6724
 
6725
  rest_of_record_type_compilation (record);
6726
 
6727
  /* If the size was widened explicitly, maybe give a warning.  Take the
6728
     original size as the maximum size of the input if there was an
6729
     unconstrained record involved and round it up to the specified alignment,
6730
     if one was specified.  */
6731
  if (CONTAINS_PLACEHOLDER_P (orig_size))
6732
    orig_size = max_size (orig_size, true);
6733
 
6734
  if (align)
6735
    orig_size = round_up (orig_size, align);
6736
 
6737
  if (Present (gnat_entity)
6738
      && size
6739
      && TREE_CODE (size) != MAX_EXPR
6740
      && TREE_CODE (size) != COND_EXPR
6741
      && !operand_equal_p (size, orig_size, 0)
6742
      && !(TREE_CODE (size) == INTEGER_CST
6743
           && TREE_CODE (orig_size) == INTEGER_CST
6744
           && (TREE_OVERFLOW (size)
6745
               || TREE_OVERFLOW (orig_size)
6746
               || tree_int_cst_lt (size, orig_size))))
6747
    {
6748
      Node_Id gnat_error_node = Empty;
6749
 
6750
      if (Is_Packed_Array_Type (gnat_entity))
6751
        gnat_entity = Original_Array_Type (gnat_entity);
6752
 
6753
      if ((Ekind (gnat_entity) == E_Component
6754
           || Ekind (gnat_entity) == E_Discriminant)
6755
          && Present (Component_Clause (gnat_entity)))
6756
        gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6757
      else if (Present (Size_Clause (gnat_entity)))
6758
        gnat_error_node = Expression (Size_Clause (gnat_entity));
6759
 
6760
      /* Generate message only for entities that come from source, since
6761
         if we have an entity created by expansion, the message will be
6762
         generated for some other corresponding source entity.  */
6763
      if (Comes_From_Source (gnat_entity))
6764
        {
6765
          if (Present (gnat_error_node))
6766
            post_error_ne_tree ("{^ }bits of & unused?",
6767
                                gnat_error_node, gnat_entity,
6768
                                size_diffop (size, orig_size));
6769
          else if (is_component_type)
6770
            post_error_ne_tree ("component of& padded{ by ^ bits}?",
6771
                                gnat_entity, gnat_entity,
6772
                                size_diffop (size, orig_size));
6773
        }
6774
    }
6775
 
6776
  return record;
6777
}
6778
 
6779
/* Given a GNU tree and a GNAT list of choices, generate an expression to test
6780
   the value passed against the list of choices.  */
6781
 
6782
tree
6783
choices_to_gnu (tree operand, Node_Id choices)
6784
{
6785
  Node_Id choice;
6786
  Node_Id gnat_temp;
6787
  tree result = boolean_false_node;
6788
  tree this_test, low = 0, high = 0, single = 0;
6789
 
6790
  for (choice = First (choices); Present (choice); choice = Next (choice))
6791
    {
6792
      switch (Nkind (choice))
6793
        {
6794
        case N_Range:
6795
          low = gnat_to_gnu (Low_Bound (choice));
6796
          high = gnat_to_gnu (High_Bound (choice));
6797
 
6798
          this_test
6799
            = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6800
                               build_binary_op (GE_EXPR, boolean_type_node,
6801
                                                operand, low),
6802
                               build_binary_op (LE_EXPR, boolean_type_node,
6803
                                                operand, high));
6804
 
6805
          break;
6806
 
6807
        case N_Subtype_Indication:
6808
          gnat_temp = Range_Expression (Constraint (choice));
6809
          low = gnat_to_gnu (Low_Bound (gnat_temp));
6810
          high = gnat_to_gnu (High_Bound (gnat_temp));
6811
 
6812
          this_test
6813
            = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6814
                               build_binary_op (GE_EXPR, boolean_type_node,
6815
                                                operand, low),
6816
                               build_binary_op (LE_EXPR, boolean_type_node,
6817
                                                operand, high));
6818
          break;
6819
 
6820
        case N_Identifier:
6821
        case N_Expanded_Name:
6822
          /* This represents either a subtype range, an enumeration
6823
             literal, or a constant  Ekind says which.  If an enumeration
6824
             literal or constant, fall through to the next case.  */
6825
          if (Ekind (Entity (choice)) != E_Enumeration_Literal
6826
              && Ekind (Entity (choice)) != E_Constant)
6827
            {
6828
              tree type = gnat_to_gnu_type (Entity (choice));
6829
 
6830
              low = TYPE_MIN_VALUE (type);
6831
              high = TYPE_MAX_VALUE (type);
6832
 
6833
              this_test
6834
                = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6835
                                   build_binary_op (GE_EXPR, boolean_type_node,
6836
                                                    operand, low),
6837
                                   build_binary_op (LE_EXPR, boolean_type_node,
6838
                                                    operand, high));
6839
              break;
6840
            }
6841
 
6842
          /* ... fall through ... */
6843
 
6844
        case N_Character_Literal:
6845
        case N_Integer_Literal:
6846
          single = gnat_to_gnu (choice);
6847
          this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6848
                                       single);
6849
          break;
6850
 
6851
        case N_Others_Choice:
6852
          this_test = boolean_true_node;
6853
          break;
6854
 
6855
        default:
6856
          gcc_unreachable ();
6857
        }
6858
 
6859
      result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6860
                                this_test);
6861
    }
6862
 
6863
  return result;
6864
}
6865
 
6866
/* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6867
   type FIELD_TYPE to be placed in RECORD_TYPE.  Return the result.  */
6868
 
6869
static int
6870
adjust_packed (tree field_type, tree record_type, int packed)
6871
{
6872
  /* If the field contains an item of variable size, we cannot pack it
6873
     because we cannot create temporaries of non-fixed size in case
6874
     we need to take the address of the field.  See addressable_p and
6875
     the notes on the addressability issues for further details.  */
6876
  if (type_has_variable_size (field_type))
6877
    return 0;
6878
 
6879
  /* If the alignment of the record is specified and the field type
6880
     is over-aligned, request Storage_Unit alignment for the field.  */
6881
  if (packed == -2)
6882
    {
6883
      if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6884
        return -1;
6885
      else
6886
        return 0;
6887
    }
6888
 
6889
  return packed;
6890
}
6891
 
6892
/* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6893
   placed in GNU_RECORD_TYPE.
6894
 
6895
   PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6896
   record has Component_Alignment of Storage_Unit, -2 if the enclosing
6897
   record has a specified alignment.
6898
 
6899
   DEFINITION is true if this field is for a record being defined.
6900
 
6901
   DEBUG_INFO_P is true if we need to write debug information for types
6902
   that we may create in the process.  */
6903
 
6904
static tree
6905
gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6906
                   bool definition, bool debug_info_p)
6907
{
6908
  const Entity_Id gnat_field_type = Etype (gnat_field);
6909
  tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6910
  tree gnu_field_id = get_entity_name (gnat_field);
6911
  tree gnu_field, gnu_size, gnu_pos;
6912
  bool is_volatile
6913
    = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6914
  bool needs_strict_alignment
6915
    = (is_volatile
6916
       || Is_Aliased (gnat_field)
6917
       || Strict_Alignment (gnat_field_type));
6918
 
6919
  /* If this field requires strict alignment, we cannot pack it because
6920
     it would very likely be under-aligned in the record.  */
6921
  if (needs_strict_alignment)
6922
    packed = 0;
6923
  else
6924
    packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6925
 
6926
  /* If a size is specified, use it.  Otherwise, if the record type is packed,
6927
     use the official RM size.  See "Handling of Type'Size Values" in Einfo
6928
     for further details.  */
6929
  if (Known_Esize (gnat_field))
6930
    gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6931
                              gnat_field, FIELD_DECL, false, true);
6932
  else if (packed == 1)
6933
    gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6934
                              gnat_field, FIELD_DECL, false, true);
6935
  else
6936
    gnu_size = NULL_TREE;
6937
 
6938
  /* If we have a specified size that is smaller than that of the field's type,
6939
     or a position is specified, and the field's type is a record that doesn't
6940
     require strict alignment, see if we can get either an integral mode form
6941
     of the type or a smaller form.  If we can, show a size was specified for
6942
     the field if there wasn't one already, so we know to make this a bitfield
6943
     and avoid making things wider.
6944
 
6945
     Changing to an integral mode form is useful when the record is packed as
6946
     we can then place the field at a non-byte-aligned position and so achieve
6947
     tighter packing.  This is in addition required if the field shares a byte
6948
     with another field and the front-end lets the back-end handle the access
6949
     to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6950
 
6951
     Changing to a smaller form is required if the specified size is smaller
6952
     than that of the field's type and the type contains sub-fields that are
6953
     padded, in order to avoid generating accesses to these sub-fields that
6954
     are wider than the field.
6955
 
6956
     We avoid the transformation if it is not required or potentially useful,
6957
     as it might entail an increase of the field's alignment and have ripple
6958
     effects on the outer record type.  A typical case is a field known to be
6959
     byte-aligned and not to share a byte with another field.  */
6960
  if (!needs_strict_alignment
6961
      && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6962
      && !TYPE_FAT_POINTER_P (gnu_field_type)
6963
      && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6964
      && (packed == 1
6965
          || (gnu_size
6966
              && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6967
                  || (Present (Component_Clause (gnat_field))
6968
                      && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6969
                           % BITS_PER_UNIT == 0
6970
                           && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6971
    {
6972
      tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6973
      if (gnu_packable_type != gnu_field_type)
6974
        {
6975
          gnu_field_type = gnu_packable_type;
6976
          if (!gnu_size)
6977
            gnu_size = rm_size (gnu_field_type);
6978
        }
6979
    }
6980
 
6981
  if (Is_Atomic (gnat_field))
6982
    check_ok_for_atomic (gnu_field_type, gnat_field, false);
6983
 
6984
  if (Present (Component_Clause (gnat_field)))
6985
    {
6986
      Entity_Id gnat_parent
6987
        = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6988
 
6989
      gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6990
      gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6991
                                gnat_field, FIELD_DECL, false, true);
6992
 
6993
      /* Ensure the position does not overlap with the parent subtype, if there
6994
         is one.  This test is omitted if the parent of the tagged type has a
6995
         full rep clause since, in this case, component clauses are allowed to
6996
         overlay the space allocated for the parent type and the front-end has
6997
         checked that there are no overlapping components.  */
6998
      if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6999
        {
7000
          tree gnu_parent = gnat_to_gnu_type (gnat_parent);
7001
 
7002
          if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
7003
              && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
7004
            {
7005
              post_error_ne_tree
7006
                ("offset of& must be beyond parent{, minimum allowed is ^}",
7007
                 First_Bit (Component_Clause (gnat_field)), gnat_field,
7008
                 TYPE_SIZE_UNIT (gnu_parent));
7009
            }
7010
        }
7011
 
7012
      /* If this field needs strict alignment, ensure the record is
7013
         sufficiently aligned and that that position and size are
7014
         consistent with the alignment.  */
7015
      if (needs_strict_alignment)
7016
        {
7017
          TYPE_ALIGN (gnu_record_type)
7018
            = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
7019
 
7020
          if (gnu_size
7021
              && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
7022
            {
7023
              if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
7024
                post_error_ne_tree
7025
                  ("atomic field& must be natural size of type{ (^)}",
7026
                   Last_Bit (Component_Clause (gnat_field)), gnat_field,
7027
                   TYPE_SIZE (gnu_field_type));
7028
 
7029
              else if (Is_Aliased (gnat_field))
7030
                post_error_ne_tree
7031
                  ("size of aliased field& must be ^ bits",
7032
                   Last_Bit (Component_Clause (gnat_field)), gnat_field,
7033
                   TYPE_SIZE (gnu_field_type));
7034
 
7035
              else if (Strict_Alignment (gnat_field_type))
7036
                post_error_ne_tree
7037
                  ("size of & with aliased or tagged components not ^ bits",
7038
                   Last_Bit (Component_Clause (gnat_field)), gnat_field,
7039
                   TYPE_SIZE (gnu_field_type));
7040
 
7041
              gnu_size = NULL_TREE;
7042
            }
7043
 
7044
          if (!integer_zerop (size_binop
7045
                              (TRUNC_MOD_EXPR, gnu_pos,
7046
                               bitsize_int (TYPE_ALIGN (gnu_field_type)))))
7047
            {
7048
              if (is_volatile)
7049
                post_error_ne_num
7050
                  ("position of volatile field& must be multiple of ^ bits",
7051
                   First_Bit (Component_Clause (gnat_field)), gnat_field,
7052
                   TYPE_ALIGN (gnu_field_type));
7053
 
7054
              else if (Is_Aliased (gnat_field))
7055
                post_error_ne_num
7056
                  ("position of aliased field& must be multiple of ^ bits",
7057
                   First_Bit (Component_Clause (gnat_field)), gnat_field,
7058
                   TYPE_ALIGN (gnu_field_type));
7059
 
7060
              else if (Strict_Alignment (gnat_field_type))
7061
                post_error_ne
7062
                  ("position of & is not compatible with alignment required "
7063
                   "by its components",
7064
                    First_Bit (Component_Clause (gnat_field)), gnat_field);
7065
 
7066
              else
7067
                gcc_unreachable ();
7068
 
7069
              gnu_pos = NULL_TREE;
7070
            }
7071
        }
7072
    }
7073
 
7074
  /* If the record has rep clauses and this is the tag field, make a rep
7075
     clause for it as well.  */
7076
  else if (Has_Specified_Layout (Scope (gnat_field))
7077
           && Chars (gnat_field) == Name_uTag)
7078
    {
7079
      gnu_pos = bitsize_zero_node;
7080
      gnu_size = TYPE_SIZE (gnu_field_type);
7081
    }
7082
 
7083
  else
7084
    {
7085
      gnu_pos = NULL_TREE;
7086
 
7087
      /* If we are packing the record and the field is BLKmode, round the
7088
         size up to a byte boundary.  */
7089
      if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
7090
        gnu_size = round_up (gnu_size, BITS_PER_UNIT);
7091
    }
7092
 
7093
  /* We need to make the size the maximum for the type if it is
7094
     self-referential and an unconstrained type.  In that case, we can't
7095
     pack the field since we can't make a copy to align it.  */
7096
  if (TREE_CODE (gnu_field_type) == RECORD_TYPE
7097
      && !gnu_size
7098
      && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
7099
      && !Is_Constrained (Underlying_Type (gnat_field_type)))
7100
    {
7101
      gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
7102
      packed = 0;
7103
    }
7104
 
7105
  /* If a size is specified, adjust the field's type to it.  */
7106
  if (gnu_size)
7107
    {
7108
      tree orig_field_type;
7109
 
7110
      /* If the field's type is justified modular, we would need to remove
7111
         the wrapper to (better) meet the layout requirements.  However we
7112
         can do so only if the field is not aliased to preserve the unique
7113
         layout and if the prescribed size is not greater than that of the
7114
         packed array to preserve the justification.  */
7115
      if (!needs_strict_alignment
7116
          && TREE_CODE (gnu_field_type) == RECORD_TYPE
7117
          && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
7118
          && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
7119
               <= 0)
7120
        gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
7121
 
7122
      gnu_field_type
7123
        = make_type_from_size (gnu_field_type, gnu_size,
7124
                               Has_Biased_Representation (gnat_field));
7125
 
7126
      orig_field_type = gnu_field_type;
7127
      gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
7128
                                       false, false, definition, true);
7129
 
7130
      /* If a padding record was made, declare it now since it will never be
7131
         declared otherwise.  This is necessary to ensure that its subtrees
7132
         are properly marked.  */
7133
      if (gnu_field_type != orig_field_type
7134
          && !DECL_P (TYPE_NAME (gnu_field_type)))
7135
        create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
7136
                          true, debug_info_p, gnat_field);
7137
    }
7138
 
7139
  /* Otherwise (or if there was an error), don't specify a position.  */
7140
  else
7141
    gnu_pos = NULL_TREE;
7142
 
7143
  gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
7144
              || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
7145
 
7146
  /* Now create the decl for the field.  */
7147
  gnu_field
7148
    = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
7149
                         gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
7150
  Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
7151
  DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
7152
  TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
7153
 
7154
  if (Ekind (gnat_field) == E_Discriminant)
7155
    DECL_DISCRIMINANT_NUMBER (gnu_field)
7156
      = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
7157
 
7158
  return gnu_field;
7159
}
7160
 
7161
/* Return true if TYPE is a type with variable size or a padding type with a
7162
   field of variable size or a record that has a field with such a type.  */
7163
 
7164
static bool
7165
type_has_variable_size (tree type)
7166
{
7167
  tree field;
7168
 
7169
  if (!TREE_CONSTANT (TYPE_SIZE (type)))
7170
    return true;
7171
 
7172
  if (TYPE_IS_PADDING_P (type)
7173
      && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7174
    return true;
7175
 
7176
  if (!RECORD_OR_UNION_TYPE_P (type))
7177
    return false;
7178
 
7179
  for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7180
    if (type_has_variable_size (TREE_TYPE (field)))
7181
      return true;
7182
 
7183
  return false;
7184
}
7185
 
7186
/* Return true if FIELD is an artificial field.  */
7187
 
7188
static bool
7189
field_is_artificial (tree field)
7190
{
7191
  /* These fields are generated by the front-end proper.  */
7192
  if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7193
    return true;
7194
 
7195
  /* These fields are generated by gigi.  */
7196
  if (DECL_INTERNAL_P (field))
7197
    return true;
7198
 
7199
  return false;
7200
}
7201
 
7202
/* Return true if FIELD is a non-artificial aliased field.  */
7203
 
7204
static bool
7205
field_is_aliased (tree field)
7206
{
7207
  if (field_is_artificial (field))
7208
    return false;
7209
 
7210
  return DECL_ALIASED_P (field);
7211
}
7212
 
7213
/* Return true if FIELD is a non-artificial field with self-referential
7214
   size.  */
7215
 
7216
static bool
7217
field_has_self_size (tree field)
7218
{
7219
  if (field_is_artificial (field))
7220
    return false;
7221
 
7222
  if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7223
    return false;
7224
 
7225
  return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7226
}
7227
 
7228
/* Return true if FIELD is a non-artificial field with variable size.  */
7229
 
7230
static bool
7231
field_has_variable_size (tree field)
7232
{
7233
  if (field_is_artificial (field))
7234
    return false;
7235
 
7236
  if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7237
    return false;
7238
 
7239
  return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7240
}
7241
 
7242
/* qsort comparer for the bit positions of two record components.  */
7243
 
7244
static int
7245
compare_field_bitpos (const PTR rt1, const PTR rt2)
7246
{
7247
  const_tree const field1 = * (const_tree const *) rt1;
7248
  const_tree const field2 = * (const_tree const *) rt2;
7249
  const int ret
7250
    = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7251
 
7252
  return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7253
}
7254
 
7255
/* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
7256
   the result as the field list of GNU_RECORD_TYPE and finish it up.  When
7257
   called from gnat_to_gnu_entity during the processing of a record type
7258
   definition, the GCC node for the parent, if any, will be the single field
7259
   of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7260
   GNU_FIELD_LIST.  The other calls to this function are recursive calls for
7261
   the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7262
 
7263
   PACKED is 1 if this is for a packed record, -1 if this is for a record
7264
   with Component_Alignment of Storage_Unit, -2 if this is for a record
7265
   with a specified alignment.
7266
 
7267
   DEFINITION is true if we are defining this record type.
7268
 
7269
   CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7270
   out the record.  This means the alignment only serves to force fields to
7271
   be bitfields, but not to require the record to be that aligned.  This is
7272
   used for variants.
7273
 
7274
   ALL_REP is true if a rep clause is present for all the fields.
7275
 
7276
   UNCHECKED_UNION is true if we are building this type for a record with a
7277
   Pragma Unchecked_Union.
7278
 
7279
   ARTIFICIAL is true if this is a type that was generated by the compiler.
7280
 
7281
   DEBUG_INFO is true if we need to write debug information about the type.
7282
 
7283
   MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7284
   mean that its contents may be unused as well, only the container itself.
7285
 
7286
   REORDER is true if we are permitted to reorder components of this type.
7287
 
7288
   FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7289
   the outer record type down to this variant level.  It is nonzero only if
7290
   all the fields down to this level have a rep clause and ALL_REP is false.
7291
 
7292
   P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7293
   with a rep clause is to be added; in this case, that is all that should
7294
   be done with such fields.  */
7295
 
7296
static void
7297
components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7298
                      tree gnu_field_list, int packed, bool definition,
7299
                      bool cancel_alignment, bool all_rep,
7300
                      bool unchecked_union, bool artificial,
7301
                      bool debug_info, bool maybe_unused, bool reorder,
7302
                      tree first_free_pos, tree *p_gnu_rep_list)
7303
{
7304
  bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7305
  bool layout_with_rep = false;
7306
  bool has_self_field = false;
7307
  bool has_aliased_after_self_field = false;
7308
  Node_Id component_decl, variant_part;
7309
  tree gnu_field, gnu_next, gnu_last;
7310
  tree gnu_rep_part = NULL_TREE;
7311
  tree gnu_variant_part = NULL_TREE;
7312
  tree gnu_rep_list = NULL_TREE;
7313
  tree gnu_var_list = NULL_TREE;
7314
  tree gnu_self_list = NULL_TREE;
7315
 
7316
  /* For each component referenced in a component declaration create a GCC
7317
     field and add it to the list, skipping pragmas in the GNAT list.  */
7318
  gnu_last = tree_last (gnu_field_list);
7319
  if (Present (Component_Items (gnat_component_list)))
7320
    for (component_decl
7321
           = First_Non_Pragma (Component_Items (gnat_component_list));
7322
         Present (component_decl);
7323
         component_decl = Next_Non_Pragma (component_decl))
7324
      {
7325
        Entity_Id gnat_field = Defining_Entity (component_decl);
7326
        Name_Id gnat_name = Chars (gnat_field);
7327
 
7328
        /* If present, the _Parent field must have been created as the single
7329
           field of the record type.  Put it before any other fields.  */
7330
        if (gnat_name == Name_uParent)
7331
          {
7332
            gnu_field = TYPE_FIELDS (gnu_record_type);
7333
            gnu_field_list = chainon (gnu_field_list, gnu_field);
7334
          }
7335
        else
7336
          {
7337
            gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7338
                                           definition, debug_info);
7339
 
7340
            /* If this is the _Tag field, put it before any other fields.  */
7341
            if (gnat_name == Name_uTag)
7342
              gnu_field_list = chainon (gnu_field_list, gnu_field);
7343
 
7344
            /* If this is the _Controller field, put it before the other
7345
               fields except for the _Tag or _Parent field.  */
7346
            else if (gnat_name == Name_uController && gnu_last)
7347
              {
7348
                DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7349
                DECL_CHAIN (gnu_last) = gnu_field;
7350
              }
7351
 
7352
            /* If this is a regular field, put it after the other fields.  */
7353
            else
7354
              {
7355
                DECL_CHAIN (gnu_field) = gnu_field_list;
7356
                gnu_field_list = gnu_field;
7357
                if (!gnu_last)
7358
                  gnu_last = gnu_field;
7359
 
7360
                /* And record information for the final layout.  */
7361
                if (field_has_self_size (gnu_field))
7362
                  has_self_field = true;
7363
                else if (has_self_field && field_is_aliased (gnu_field))
7364
                  has_aliased_after_self_field = true;
7365
              }
7366
          }
7367
 
7368
        save_gnu_tree (gnat_field, gnu_field, false);
7369
      }
7370
 
7371
  /* At the end of the component list there may be a variant part.  */
7372
  variant_part = Variant_Part (gnat_component_list);
7373
 
7374
  /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7375
     mutually exclusive and should go in the same memory.  To do this we need
7376
     to treat each variant as a record whose elements are created from the
7377
     component list for the variant.  So here we create the records from the
7378
     lists for the variants and put them all into the QUAL_UNION_TYPE.
7379
     If this is an Unchecked_Union, we make a UNION_TYPE instead or
7380
     use GNU_RECORD_TYPE if there are no fields so far.  */
7381
  if (Present (variant_part))
7382
    {
7383
      Node_Id gnat_discr = Name (variant_part), variant;
7384
      tree gnu_discr = gnat_to_gnu (gnat_discr);
7385
      tree gnu_name = TYPE_NAME (gnu_record_type);
7386
      tree gnu_var_name
7387
        = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7388
                       "XVN");
7389
      tree gnu_union_type, gnu_union_name;
7390
      tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7391
 
7392
      if (TREE_CODE (gnu_name) == TYPE_DECL)
7393
        gnu_name = DECL_NAME (gnu_name);
7394
 
7395
      gnu_union_name
7396
        = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7397
 
7398
      /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7399
         are all in the variant part, to match the layout of C unions.  There
7400
         is an associated check below.  */
7401
      if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7402
        gnu_union_type = gnu_record_type;
7403
      else
7404
        {
7405
          gnu_union_type
7406
            = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7407
 
7408
          TYPE_NAME (gnu_union_type) = gnu_union_name;
7409
          TYPE_ALIGN (gnu_union_type) = 0;
7410
          TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7411
        }
7412
 
7413
      /* If all the fields down to this level have a rep clause, find out
7414
         whether all the fields at this level also have one.  If so, then
7415
         compute the new first free position to be passed downward.  */
7416
      this_first_free_pos = first_free_pos;
7417
      if (this_first_free_pos)
7418
        {
7419
          for (gnu_field = gnu_field_list;
7420
               gnu_field;
7421
               gnu_field = DECL_CHAIN (gnu_field))
7422
            if (DECL_FIELD_OFFSET (gnu_field))
7423
              {
7424
                tree pos = bit_position (gnu_field);
7425
                if (!tree_int_cst_lt (pos, this_first_free_pos))
7426
                  this_first_free_pos
7427
                    = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7428
              }
7429
            else
7430
              {
7431
                this_first_free_pos = NULL_TREE;
7432
                break;
7433
              }
7434
        }
7435
 
7436
      for (variant = First_Non_Pragma (Variants (variant_part));
7437
           Present (variant);
7438
           variant = Next_Non_Pragma (variant))
7439
        {
7440
          tree gnu_variant_type = make_node (RECORD_TYPE);
7441
          tree gnu_inner_name;
7442
          tree gnu_qual;
7443
 
7444
          Get_Variant_Encoding (variant);
7445
          gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7446
          TYPE_NAME (gnu_variant_type)
7447
            = concat_name (gnu_union_name,
7448
                           IDENTIFIER_POINTER (gnu_inner_name));
7449
 
7450
          /* Set the alignment of the inner type in case we need to make
7451
             inner objects into bitfields, but then clear it out so the
7452
             record actually gets only the alignment required.  */
7453
          TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7454
          TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7455
 
7456
          /* Similarly, if the outer record has a size specified and all
7457
             the fields have a rep clause, we can propagate the size.  */
7458
          if (all_rep_and_size)
7459
            {
7460
              TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7461
              TYPE_SIZE_UNIT (gnu_variant_type)
7462
                = TYPE_SIZE_UNIT (gnu_record_type);
7463
            }
7464
 
7465
          /* Add the fields into the record type for the variant.  Note that
7466
             we aren't sure to really use it at this point, see below.  */
7467
          components_to_record (gnu_variant_type, Component_List (variant),
7468
                                NULL_TREE, packed, definition,
7469
                                !all_rep_and_size, all_rep, unchecked_union,
7470
                                true, debug_info, true, reorder,
7471
                                this_first_free_pos,
7472
                                all_rep || this_first_free_pos
7473
                                ? NULL : &gnu_rep_list);
7474
 
7475
          gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7476
          Set_Present_Expr (variant, annotate_value (gnu_qual));
7477
 
7478
          /* If this is an Unchecked_Union whose fields are all in the variant
7479
             part and we have a single field with no representation clause or
7480
             placed at offset zero, use the field directly to match the layout
7481
             of C unions.  */
7482
          if (TREE_CODE (gnu_record_type) == UNION_TYPE
7483
              && (gnu_field = TYPE_FIELDS (gnu_variant_type)) != NULL_TREE
7484
              && !DECL_CHAIN (gnu_field)
7485
              && (!DECL_FIELD_OFFSET (gnu_field)
7486
                  || integer_zerop (bit_position (gnu_field))))
7487
            DECL_CONTEXT (gnu_field) = gnu_union_type;
7488
          else
7489
            {
7490
              /* Deal with packedness like in gnat_to_gnu_field.  */
7491
              int field_packed
7492
                = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7493
 
7494
              /* Finalize the record type now.  We used to throw away
7495
                 empty records but we no longer do that because we need
7496
                 them to generate complete debug info for the variant;
7497
                 otherwise, the union type definition will be lacking
7498
                 the fields associated with these empty variants.  */
7499
              rest_of_record_type_compilation (gnu_variant_type);
7500
              create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7501
                                NULL, true, debug_info, gnat_component_list);
7502
 
7503
              gnu_field
7504
                = create_field_decl (gnu_inner_name, gnu_variant_type,
7505
                                     gnu_union_type,
7506
                                     all_rep_and_size
7507
                                     ? TYPE_SIZE (gnu_variant_type) : 0,
7508
                                     all_rep_and_size
7509
                                     ? bitsize_zero_node : 0,
7510
                                     field_packed, 0);
7511
 
7512
              DECL_INTERNAL_P (gnu_field) = 1;
7513
 
7514
              if (!unchecked_union)
7515
                DECL_QUALIFIER (gnu_field) = gnu_qual;
7516
            }
7517
 
7518
          DECL_CHAIN (gnu_field) = gnu_variant_list;
7519
          gnu_variant_list = gnu_field;
7520
        }
7521
 
7522
      /* Only make the QUAL_UNION_TYPE if there are non-empty variants.  */
7523
      if (gnu_variant_list)
7524
        {
7525
          int union_field_packed;
7526
 
7527
          if (all_rep_and_size)
7528
            {
7529
              TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7530
              TYPE_SIZE_UNIT (gnu_union_type)
7531
                = TYPE_SIZE_UNIT (gnu_record_type);
7532
            }
7533
 
7534
          finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7535
                              all_rep_and_size ? 1 : 0, debug_info);
7536
 
7537
          /* If GNU_UNION_TYPE is our record type, it means we must have an
7538
             Unchecked_Union with no fields.  Verify that and, if so, just
7539
             return.  */
7540
          if (gnu_union_type == gnu_record_type)
7541
            {
7542
              gcc_assert (unchecked_union
7543
                          && !gnu_field_list
7544
                          && !gnu_rep_list);
7545
              return;
7546
            }
7547
 
7548
          create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7549
                            NULL, true, debug_info, gnat_component_list);
7550
 
7551
          /* Deal with packedness like in gnat_to_gnu_field.  */
7552
          union_field_packed
7553
            = adjust_packed (gnu_union_type, gnu_record_type, packed);
7554
 
7555
          gnu_variant_part
7556
            = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7557
                                 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7558
                                 all_rep || this_first_free_pos
7559
                                 ? bitsize_zero_node : 0,
7560
                                 union_field_packed, 0);
7561
 
7562
          DECL_INTERNAL_P (gnu_variant_part) = 1;
7563
        }
7564
    }
7565
 
7566
  /* From now on, a zero FIRST_FREE_POS is totally useless.  */
7567
  if (first_free_pos && integer_zerop (first_free_pos))
7568
    first_free_pos = NULL_TREE;
7569
 
7570
  /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7571
     permitted to reorder components, self-referential sizes or variable sizes.
7572
     If they do, pull them out and put them onto the appropriate list.  We have
7573
     to do this in a separate pass since we want to handle the discriminants
7574
     but can't play with them until we've used them in debugging data above.
7575
 
7576
     ??? If we reorder them, debugging information will be wrong but there is
7577
     nothing that can be done about this at the moment.  */
7578
  gnu_last = NULL_TREE;
7579
 
7580
#define MOVE_FROM_FIELD_LIST_TO(LIST)   \
7581
  do {                                  \
7582
    if (gnu_last)                       \
7583
      DECL_CHAIN (gnu_last) = gnu_next; \
7584
    else                                \
7585
      gnu_field_list = gnu_next;        \
7586
                                        \
7587
    DECL_CHAIN (gnu_field) = (LIST);    \
7588
    (LIST) = gnu_field;                 \
7589
  } while (0)
7590
 
7591
  for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7592
    {
7593
      gnu_next = DECL_CHAIN (gnu_field);
7594
 
7595
      if (DECL_FIELD_OFFSET (gnu_field))
7596
        {
7597
          MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7598
          continue;
7599
        }
7600
 
7601
      if ((reorder || has_aliased_after_self_field)
7602
          && field_has_self_size (gnu_field))
7603
        {
7604
          MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7605
          continue;
7606
        }
7607
 
7608
      if (reorder && field_has_variable_size (gnu_field))
7609
        {
7610
          MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7611
          continue;
7612
        }
7613
 
7614
      gnu_last = gnu_field;
7615
    }
7616
 
7617
#undef MOVE_FROM_FIELD_LIST_TO
7618
 
7619
  /* If permitted, we reorder the fields as follows:
7620
 
7621
       1) all fixed length fields,
7622
       2) all fields whose length doesn't depend on discriminants,
7623
       3) all fields whose length depends on discriminants,
7624
       4) the variant part,
7625
 
7626
     within the record and within each variant recursively.  */
7627
  if (reorder)
7628
    gnu_field_list
7629
      = chainon (nreverse (gnu_self_list),
7630
                 chainon (nreverse (gnu_var_list), gnu_field_list));
7631
 
7632
  /* Otherwise, if there is an aliased field placed after a field whose length
7633
     depends on discriminants, we put all the fields of the latter sort, last.
7634
     We need to do this in case an object of this record type is mutable.  */
7635
  else if (has_aliased_after_self_field)
7636
    gnu_field_list = chainon (nreverse (gnu_self_list), gnu_field_list);
7637
 
7638
  /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7639
     in our REP list to the previous level because this level needs them in
7640
     order to do a correct layout, i.e. avoid having overlapping fields.  */
7641
  if (p_gnu_rep_list && gnu_rep_list)
7642
    *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7643
 
7644
  /* Otherwise, sort the fields by bit position and put them into their own
7645
     record, before the others, if we also have fields without rep clause.  */
7646
  else if (gnu_rep_list)
7647
    {
7648
      tree gnu_rep_type
7649
        = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7650
      int i, len = list_length (gnu_rep_list);
7651
      tree *gnu_arr = XALLOCAVEC (tree, len);
7652
 
7653
      for (gnu_field = gnu_rep_list, i = 0;
7654
           gnu_field;
7655
           gnu_field = DECL_CHAIN (gnu_field), i++)
7656
        gnu_arr[i] = gnu_field;
7657
 
7658
      qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7659
 
7660
      /* Put the fields in the list in order of increasing position, which
7661
         means we start from the end.  */
7662
      gnu_rep_list = NULL_TREE;
7663
      for (i = len - 1; i >= 0; i--)
7664
        {
7665
          DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7666
          gnu_rep_list = gnu_arr[i];
7667
          DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7668
        }
7669
 
7670
      if (gnu_field_list)
7671
        {
7672
          finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7673
 
7674
          /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7675
             without rep clause are laid out starting from this position.
7676
             Therefore, we force it as a minimal size on the REP part.  */
7677
          gnu_rep_part
7678
            = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7679
        }
7680
      else
7681
        {
7682
          layout_with_rep = true;
7683
          gnu_field_list = nreverse (gnu_rep_list);
7684
        }
7685
    }
7686
 
7687
  /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields without
7688
     rep clause are laid out starting from this position.  Therefore, if we
7689
     have not already done so, we create a fake REP part with this size.  */
7690
  if (first_free_pos && !layout_with_rep && !gnu_rep_part)
7691
    {
7692
      tree gnu_rep_type = make_node (RECORD_TYPE);
7693
      finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7694
      gnu_rep_part
7695
        = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7696
    }
7697
 
7698
  /* Now chain the REP part at the end of the reversed field list.  */
7699
  if (gnu_rep_part)
7700
    gnu_field_list = chainon (gnu_field_list, gnu_rep_part);
7701
 
7702
  /* And the variant part at the beginning.  */
7703
  if (gnu_variant_part)
7704
    {
7705
      DECL_CHAIN (gnu_variant_part) = gnu_field_list;
7706
      gnu_field_list = gnu_variant_part;
7707
    }
7708
 
7709
  if (cancel_alignment)
7710
    TYPE_ALIGN (gnu_record_type) = 0;
7711
 
7712
  finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7713
                      layout_with_rep ? 1 : 0, false);
7714
  TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7715
  if (debug_info && !maybe_unused)
7716
    rest_of_record_type_compilation (gnu_record_type);
7717
}
7718
 
7719
/* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7720
   placed into an Esize, Component_Bit_Offset, or Component_Size value
7721
   in the GNAT tree.  */
7722
 
7723
static Uint
7724
annotate_value (tree gnu_size)
7725
{
7726
  TCode tcode;
7727
  Node_Ref_Or_Val ops[3], ret;
7728
  struct tree_int_map in;
7729
  int i;
7730
 
7731
  /* See if we've already saved the value for this node.  */
7732
  if (EXPR_P (gnu_size))
7733
    {
7734
      struct tree_int_map *e;
7735
 
7736
      if (!annotate_value_cache)
7737
        annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7738
                                                tree_int_map_eq, 0);
7739
      in.base.from = gnu_size;
7740
      e = (struct tree_int_map *)
7741
            htab_find (annotate_value_cache, &in);
7742
 
7743
      if (e)
7744
        return (Node_Ref_Or_Val) e->to;
7745
    }
7746
  else
7747
    in.base.from = NULL_TREE;
7748
 
7749
  /* If we do not return inside this switch, TCODE will be set to the
7750
     code to use for a Create_Node operand and LEN (set above) will be
7751
     the number of recursive calls for us to make.  */
7752
 
7753
  switch (TREE_CODE (gnu_size))
7754
    {
7755
    case INTEGER_CST:
7756
      if (TREE_OVERFLOW (gnu_size))
7757
        return No_Uint;
7758
 
7759
      /* This may come from a conversion from some smaller type, so ensure
7760
         this is in bitsizetype.  */
7761
      gnu_size = convert (bitsizetype, gnu_size);
7762
 
7763
      /* For a negative value, build NEGATE_EXPR of the opposite.  Such values
7764
         appear in expressions containing aligning patterns.  Note that, since
7765
         sizetype is sign-extended but nonetheless unsigned, we don't directly
7766
         use tree_int_cst_sgn.  */
7767
      if (TREE_INT_CST_HIGH (gnu_size) < 0)
7768
        {
7769
          tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7770
          return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7771
        }
7772
 
7773
      return UI_From_gnu (gnu_size);
7774
 
7775
    case COMPONENT_REF:
7776
      /* The only case we handle here is a simple discriminant reference.  */
7777
      if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7778
          && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7779
          && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7780
        return Create_Node (Discrim_Val,
7781
                            annotate_value (DECL_DISCRIMINANT_NUMBER
7782
                                            (TREE_OPERAND (gnu_size, 1))),
7783
                            No_Uint, No_Uint);
7784
      else
7785
        return No_Uint;
7786
 
7787
    CASE_CONVERT:   case NON_LVALUE_EXPR:
7788
      return annotate_value (TREE_OPERAND (gnu_size, 0));
7789
 
7790
      /* Now just list the operations we handle.  */
7791
    case COND_EXPR:             tcode = Cond_Expr; break;
7792
    case PLUS_EXPR:             tcode = Plus_Expr; break;
7793
    case MINUS_EXPR:            tcode = Minus_Expr; break;
7794
    case MULT_EXPR:             tcode = Mult_Expr; break;
7795
    case TRUNC_DIV_EXPR:        tcode = Trunc_Div_Expr; break;
7796
    case CEIL_DIV_EXPR:         tcode = Ceil_Div_Expr; break;
7797
    case FLOOR_DIV_EXPR:        tcode = Floor_Div_Expr; break;
7798
    case TRUNC_MOD_EXPR:        tcode = Trunc_Mod_Expr; break;
7799
    case CEIL_MOD_EXPR:         tcode = Ceil_Mod_Expr; break;
7800
    case FLOOR_MOD_EXPR:        tcode = Floor_Mod_Expr; break;
7801
    case EXACT_DIV_EXPR:        tcode = Exact_Div_Expr; break;
7802
    case NEGATE_EXPR:           tcode = Negate_Expr; break;
7803
    case MIN_EXPR:              tcode = Min_Expr; break;
7804
    case MAX_EXPR:              tcode = Max_Expr; break;
7805
    case ABS_EXPR:              tcode = Abs_Expr; break;
7806
    case TRUTH_ANDIF_EXPR:      tcode = Truth_Andif_Expr; break;
7807
    case TRUTH_ORIF_EXPR:       tcode = Truth_Orif_Expr; break;
7808
    case TRUTH_AND_EXPR:        tcode = Truth_And_Expr; break;
7809
    case TRUTH_OR_EXPR:         tcode = Truth_Or_Expr; break;
7810
    case TRUTH_XOR_EXPR:        tcode = Truth_Xor_Expr; break;
7811
    case TRUTH_NOT_EXPR:        tcode = Truth_Not_Expr; break;
7812
    case BIT_AND_EXPR:          tcode = Bit_And_Expr; break;
7813
    case LT_EXPR:               tcode = Lt_Expr; break;
7814
    case LE_EXPR:               tcode = Le_Expr; break;
7815
    case GT_EXPR:               tcode = Gt_Expr; break;
7816
    case GE_EXPR:               tcode = Ge_Expr; break;
7817
    case EQ_EXPR:               tcode = Eq_Expr; break;
7818
    case NE_EXPR:               tcode = Ne_Expr; break;
7819
 
7820
    case CALL_EXPR:
7821
      {
7822
        tree t = maybe_inline_call_in_expr (gnu_size);
7823
        if (t)
7824
          return annotate_value (t);
7825
      }
7826
 
7827
      /* Fall through... */
7828
 
7829
    default:
7830
      return No_Uint;
7831
    }
7832
 
7833
  /* Now get each of the operands that's relevant for this code.  If any
7834
     cannot be expressed as a repinfo node, say we can't.  */
7835
  for (i = 0; i < 3; i++)
7836
    ops[i] = No_Uint;
7837
 
7838
  for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7839
    {
7840
      ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7841
      if (ops[i] == No_Uint)
7842
        return No_Uint;
7843
    }
7844
 
7845
  ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7846
 
7847
  /* Save the result in the cache.  */
7848
  if (in.base.from)
7849
    {
7850
      struct tree_int_map **h;
7851
      /* We can't assume the hash table data hasn't moved since the
7852
         initial look up, so we have to search again.  Allocating and
7853
         inserting an entry at that point would be an alternative, but
7854
         then we'd better discard the entry if we decided not to cache
7855
         it.  */
7856
      h = (struct tree_int_map **)
7857
            htab_find_slot (annotate_value_cache, &in, INSERT);
7858
      gcc_assert (!*h);
7859
      *h = ggc_alloc_tree_int_map ();
7860
      (*h)->base.from = gnu_size;
7861
      (*h)->to = ret;
7862
    }
7863
 
7864
  return ret;
7865
}
7866
 
7867
/* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7868
   and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7869
   size and alignment used by Gigi.  Prefer SIZE over TYPE_SIZE if non-null.
7870
   BY_REF is true if the object is used by reference and BY_DOUBLE_REF is
7871
   true if the object is used by double reference.  */
7872
 
7873
void
7874
annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref,
7875
                 bool by_double_ref)
7876
{
7877
  if (by_ref)
7878
    {
7879
      if (by_double_ref)
7880
        gnu_type = TREE_TYPE (gnu_type);
7881
 
7882
      if (TYPE_IS_FAT_POINTER_P (gnu_type))
7883
        gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7884
      else
7885
        gnu_type = TREE_TYPE (gnu_type);
7886
    }
7887
 
7888
  if (Unknown_Esize (gnat_entity))
7889
    {
7890
      if (TREE_CODE (gnu_type) == RECORD_TYPE
7891
          && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7892
        size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7893
      else if (!size)
7894
        size = TYPE_SIZE (gnu_type);
7895
 
7896
      if (size)
7897
        Set_Esize (gnat_entity, annotate_value (size));
7898
    }
7899
 
7900
  if (Unknown_Alignment (gnat_entity))
7901
    Set_Alignment (gnat_entity,
7902
                   UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7903
}
7904
 
7905
/* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7906
   Return NULL_TREE if there is no such element in the list.  */
7907
 
7908
static tree
7909
purpose_member_field (const_tree elem, tree list)
7910
{
7911
  while (list)
7912
    {
7913
      tree field = TREE_PURPOSE (list);
7914
      if (SAME_FIELD_P (field, elem))
7915
        return list;
7916
      list = TREE_CHAIN (list);
7917
    }
7918
  return NULL_TREE;
7919
}
7920
 
7921
/* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7922
   set Component_Bit_Offset and Esize of the components to the position and
7923
   size used by Gigi.  */
7924
 
7925
static void
7926
annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7927
{
7928
  Entity_Id gnat_field;
7929
  tree gnu_list;
7930
 
7931
  /* We operate by first making a list of all fields and their position (we
7932
     can get the size easily) and then update all the sizes in the tree.  */
7933
  gnu_list
7934
    = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7935
                           BIGGEST_ALIGNMENT, NULL_TREE);
7936
 
7937
  for (gnat_field = First_Entity (gnat_entity);
7938
       Present (gnat_field);
7939
       gnat_field = Next_Entity (gnat_field))
7940
    if (Ekind (gnat_field) == E_Component
7941
        || (Ekind (gnat_field) == E_Discriminant
7942
            && !Is_Unchecked_Union (Scope (gnat_field))))
7943
      {
7944
        tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7945
                                       gnu_list);
7946
        if (t)
7947
          {
7948
            tree parent_offset;
7949
 
7950
            if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7951
              {
7952
                /* In this mode the tag and parent components are not
7953
                   generated, so we add the appropriate offset to each
7954
                   component.  For a component appearing in the current
7955
                   extension, the offset is the size of the parent.  */
7956
                if (Is_Derived_Type (gnat_entity)
7957
                    && Original_Record_Component (gnat_field) == gnat_field)
7958
                  parent_offset
7959
                    = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7960
                                 bitsizetype);
7961
                else
7962
                  parent_offset = bitsize_int (POINTER_SIZE);
7963
              }
7964
            else
7965
              parent_offset = bitsize_zero_node;
7966
 
7967
            Set_Component_Bit_Offset
7968
              (gnat_field,
7969
               annotate_value
7970
                 (size_binop (PLUS_EXPR,
7971
                              bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7972
                                            TREE_VEC_ELT (TREE_VALUE (t), 2)),
7973
                              parent_offset)));
7974
 
7975
            Set_Esize (gnat_field,
7976
                       annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7977
          }
7978
        else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7979
          {
7980
            /* If there is no entry, this is an inherited component whose
7981
               position is the same as in the parent type.  */
7982
            Set_Component_Bit_Offset
7983
              (gnat_field,
7984
               Component_Bit_Offset (Original_Record_Component (gnat_field)));
7985
 
7986
            Set_Esize (gnat_field,
7987
                       Esize (Original_Record_Component (gnat_field)));
7988
          }
7989
      }
7990
}
7991
 
7992
/* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7993
   the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7994
   value to be placed into DECL_OFFSET_ALIGN and the bit position.  The list
7995
   of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7996
   is set to true.  GNU_POS is to be added to the position, GNU_BITPOS to the
7997
   bit position, OFFSET_ALIGN is the present offset alignment.  GNU_LIST is a
7998
   pre-existing list to be chained to the newly created entries.  */
7999
 
8000
static tree
8001
build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8002
                     tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8003
{
8004
  tree gnu_field;
8005
 
8006
  for (gnu_field = TYPE_FIELDS (gnu_type);
8007
       gnu_field;
8008
       gnu_field = DECL_CHAIN (gnu_field))
8009
    {
8010
      tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8011
                                        DECL_FIELD_BIT_OFFSET (gnu_field));
8012
      tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8013
                                        DECL_FIELD_OFFSET (gnu_field));
8014
      unsigned int our_offset_align
8015
        = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8016
      tree v = make_tree_vec (3);
8017
 
8018
      TREE_VEC_ELT (v, 0) = gnu_our_offset;
8019
      TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8020
      TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8021
      gnu_list = tree_cons (gnu_field, v, gnu_list);
8022
 
8023
      /* Recurse on internal fields, flattening the nested fields except for
8024
         those in the variant part, if requested.  */
8025
      if (DECL_INTERNAL_P (gnu_field))
8026
        {
8027
          tree gnu_field_type = TREE_TYPE (gnu_field);
8028
          if (do_not_flatten_variant
8029
              && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8030
            gnu_list
8031
              = build_position_list (gnu_field_type, do_not_flatten_variant,
8032
                                     size_zero_node, bitsize_zero_node,
8033
                                     BIGGEST_ALIGNMENT, gnu_list);
8034
          else
8035
            gnu_list
8036
              = build_position_list (gnu_field_type, do_not_flatten_variant,
8037
                                     gnu_our_offset, gnu_our_bitpos,
8038
                                     our_offset_align, gnu_list);
8039
        }
8040
    }
8041
 
8042
  return gnu_list;
8043
}
8044
 
8045
/* Return a VEC describing the substitutions needed to reflect the
8046
   discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE.  They can
8047
   be in any order.  The values in an element of the VEC are in the form
8048
   of operands to SUBSTITUTE_IN_EXPR.  DEFINITION is true if this is for
8049
   a definition of GNAT_SUBTYPE.  */
8050
 
8051
static VEC(subst_pair,heap) *
8052
build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8053
{
8054
  VEC(subst_pair,heap) *gnu_vec = NULL;
8055
  Entity_Id gnat_discrim;
8056
  Node_Id gnat_value;
8057
 
8058
  for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8059
       gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
8060
       Present (gnat_discrim);
8061
       gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8062
       gnat_value = Next_Elmt (gnat_value))
8063
    /* Ignore access discriminants.  */
8064
    if (!Is_Access_Type (Etype (Node (gnat_value))))
8065
      {
8066
        tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8067
        tree replacement = convert (TREE_TYPE (gnu_field),
8068
                                    elaborate_expression
8069
                                    (Node (gnat_value), gnat_subtype,
8070
                                     get_entity_name (gnat_discrim),
8071
                                     definition, true, false));
8072
        subst_pair *s = VEC_safe_push (subst_pair, heap, gnu_vec, NULL);
8073
        s->discriminant = gnu_field;
8074
        s->replacement = replacement;
8075
      }
8076
 
8077
  return gnu_vec;
8078
}
8079
 
8080
/* Scan all fields in QUAL_UNION_TYPE and return a VEC describing the
8081
   variants of QUAL_UNION_TYPE that are still relevant after applying
8082
   the substitutions described in SUBST_LIST.  VARIANT_LIST is a
8083
   pre-existing VEC onto which newly created entries should be
8084
   pushed.  */
8085
 
8086
static VEC(variant_desc,heap) *
8087
build_variant_list (tree qual_union_type, VEC(subst_pair,heap) *subst_list,
8088
                    VEC(variant_desc,heap) *variant_list)
8089
{
8090
  tree gnu_field;
8091
 
8092
  for (gnu_field = TYPE_FIELDS (qual_union_type);
8093
       gnu_field;
8094
       gnu_field = DECL_CHAIN (gnu_field))
8095
    {
8096
      tree qual = DECL_QUALIFIER (gnu_field);
8097
      unsigned ix;
8098
      subst_pair *s;
8099
 
8100
      FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8101
        qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8102
 
8103
      /* If the new qualifier is not unconditionally false, its variant may
8104
         still be accessed.  */
8105
      if (!integer_zerop (qual))
8106
        {
8107
          variant_desc *v;
8108
          tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8109
 
8110
          v = VEC_safe_push (variant_desc, heap, variant_list, NULL);
8111
          v->type = variant_type;
8112
          v->field = gnu_field;
8113
          v->qual = qual;
8114
          v->record = NULL_TREE;
8115
 
8116
          /* Recurse on the variant subpart of the variant, if any.  */
8117
          variant_subpart = get_variant_part (variant_type);
8118
          if (variant_subpart)
8119
            variant_list = build_variant_list (TREE_TYPE (variant_subpart),
8120
                                               subst_list, variant_list);
8121
 
8122
          /* If the new qualifier is unconditionally true, the subsequent
8123
             variants cannot be accessed.  */
8124
          if (integer_onep (qual))
8125
            break;
8126
        }
8127
    }
8128
 
8129
  return variant_list;
8130
}
8131
 
8132
/* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8133
   corresponding to GNAT_OBJECT.  If the size is valid, return an INTEGER_CST
8134
   corresponding to its value.  Otherwise, return NULL_TREE.  KIND is set to
8135
   VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8136
   size of a type, and FIELD_DECL for the size of a field.  COMPONENT_P is
8137
   true if we are being called to process the Component_Size of GNAT_OBJECT;
8138
   this is used only for error messages.  ZERO_OK is true if a size of zero
8139
   is permitted; if ZERO_OK is false, it means that a size of zero should be
8140
   treated as an unspecified size.  */
8141
 
8142
static tree
8143
validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8144
               enum tree_code kind, bool component_p, bool zero_ok)
8145
{
8146
  Node_Id gnat_error_node;
8147
  tree type_size, size;
8148
 
8149
  /* Return 0 if no size was specified.  */
8150
  if (uint_size == No_Uint)
8151
    return NULL_TREE;
8152
 
8153
  /* Ignore a negative size since that corresponds to our back-annotation.  */
8154
  if (UI_Lt (uint_size, Uint_0))
8155
    return NULL_TREE;
8156
 
8157
  /* Find the node to use for error messages.  */
8158
  if ((Ekind (gnat_object) == E_Component
8159
       || Ekind (gnat_object) == E_Discriminant)
8160
      && Present (Component_Clause (gnat_object)))
8161
    gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8162
  else if (Present (Size_Clause (gnat_object)))
8163
    gnat_error_node = Expression (Size_Clause (gnat_object));
8164
  else
8165
    gnat_error_node = gnat_object;
8166
 
8167
  /* Get the size as an INTEGER_CST.  Issue an error if a size was specified
8168
     but cannot be represented in bitsizetype.  */
8169
  size = UI_To_gnu (uint_size, bitsizetype);
8170
  if (TREE_OVERFLOW (size))
8171
    {
8172
      if (component_p)
8173
        post_error_ne ("component size for& is too large", gnat_error_node,
8174
                       gnat_object);
8175
      else
8176
        post_error_ne ("size for& is too large", gnat_error_node,
8177
                       gnat_object);
8178
      return NULL_TREE;
8179
    }
8180
 
8181
  /* Ignore a zero size if it is not permitted.  */
8182
  if (!zero_ok && integer_zerop (size))
8183
    return NULL_TREE;
8184
 
8185
  /* The size of objects is always a multiple of a byte.  */
8186
  if (kind == VAR_DECL
8187
      && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8188
    {
8189
      if (component_p)
8190
        post_error_ne ("component size for& is not a multiple of Storage_Unit",
8191
                       gnat_error_node, gnat_object);
8192
      else
8193
        post_error_ne ("size for& is not a multiple of Storage_Unit",
8194
                       gnat_error_node, gnat_object);
8195
      return NULL_TREE;
8196
    }
8197
 
8198
  /* If this is an integral type or a packed array type, the front-end has
8199
     already verified the size, so we need not do it here (which would mean
8200
     checking against the bounds).  However, if this is an aliased object,
8201
     it may not be smaller than the type of the object.  */
8202
  if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8203
      && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8204
    return size;
8205
 
8206
  /* If the object is a record that contains a template, add the size of the
8207
     template to the specified size.  */
8208
  if (TREE_CODE (gnu_type) == RECORD_TYPE
8209
      && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8210
    size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8211
 
8212
  if (kind == VAR_DECL
8213
      /* If a type needs strict alignment, a component of this type in
8214
         a packed record cannot be packed and thus uses the type size.  */
8215
      || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8216
    type_size = TYPE_SIZE (gnu_type);
8217
  else
8218
    type_size = rm_size (gnu_type);
8219
 
8220
  /* Modify the size of a discriminated type to be the maximum size.  */
8221
  if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8222
    type_size = max_size (type_size, true);
8223
 
8224
  /* If this is an access type or a fat pointer, the minimum size is that given
8225
     by the smallest integral mode that's valid for pointers.  */
8226
  if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8227
    {
8228
      enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8229
      while (!targetm.valid_pointer_mode (p_mode))
8230
        p_mode = GET_MODE_WIDER_MODE (p_mode);
8231
      type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8232
    }
8233
 
8234
  /* Issue an error either if the default size of the object isn't a constant
8235
     or if the new size is smaller than it.  */
8236
  if (TREE_CODE (type_size) != INTEGER_CST
8237
      || TREE_OVERFLOW (type_size)
8238
      || tree_int_cst_lt (size, type_size))
8239
    {
8240
      if (component_p)
8241
        post_error_ne_tree
8242
          ("component size for& too small{, minimum allowed is ^}",
8243
           gnat_error_node, gnat_object, type_size);
8244
      else
8245
        post_error_ne_tree
8246
          ("size for& too small{, minimum allowed is ^}",
8247
           gnat_error_node, gnat_object, type_size);
8248
      return NULL_TREE;
8249
    }
8250
 
8251
  return size;
8252
}
8253
 
8254
/* Similarly, but both validate and process a value of RM size.  This routine
8255
   is only called for types.  */
8256
 
8257
static void
8258
set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8259
{
8260
  Node_Id gnat_attr_node;
8261
  tree old_size, size;
8262
 
8263
  /* Do nothing if no size was specified.  */
8264
  if (uint_size == No_Uint)
8265
    return;
8266
 
8267
  /* Ignore a negative size since that corresponds to our back-annotation.  */
8268
  if (UI_Lt (uint_size, Uint_0))
8269
    return;
8270
 
8271
  /* Only issue an error if a Value_Size clause was explicitly given.
8272
     Otherwise, we'd be duplicating an error on the Size clause.  */
8273
  gnat_attr_node
8274
    = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8275
 
8276
  /* Get the size as an INTEGER_CST.  Issue an error if a size was specified
8277
     but cannot be represented in bitsizetype.  */
8278
  size = UI_To_gnu (uint_size, bitsizetype);
8279
  if (TREE_OVERFLOW (size))
8280
    {
8281
      if (Present (gnat_attr_node))
8282
        post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8283
                       gnat_entity);
8284
      return;
8285
    }
8286
 
8287
  /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8288
     exists, or this is an integer type, in which case the front-end will
8289
     have always set it.  */
8290
  if (No (gnat_attr_node)
8291
      && integer_zerop (size)
8292
      && !Has_Size_Clause (gnat_entity)
8293
      && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8294
    return;
8295
 
8296
  old_size = rm_size (gnu_type);
8297
 
8298
  /* If the old size is self-referential, get the maximum size.  */
8299
  if (CONTAINS_PLACEHOLDER_P (old_size))
8300
    old_size = max_size (old_size, true);
8301
 
8302
  /* Issue an error either if the old size of the object isn't a constant or
8303
     if the new size is smaller than it.  The front-end has already verified
8304
     this for scalar and packed array types.  */
8305
  if (TREE_CODE (old_size) != INTEGER_CST
8306
      || TREE_OVERFLOW (old_size)
8307
      || (AGGREGATE_TYPE_P (gnu_type)
8308
          && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8309
               && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8310
          && !(TYPE_IS_PADDING_P (gnu_type)
8311
               && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8312
               && TYPE_PACKED_ARRAY_TYPE_P
8313
                  (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8314
          && tree_int_cst_lt (size, old_size)))
8315
    {
8316
      if (Present (gnat_attr_node))
8317
        post_error_ne_tree
8318
          ("Value_Size for& too small{, minimum allowed is ^}",
8319
           gnat_attr_node, gnat_entity, old_size);
8320
      return;
8321
    }
8322
 
8323
  /* Otherwise, set the RM size proper for integral types...  */
8324
  if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8325
       && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8326
      || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8327
          || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8328
    SET_TYPE_RM_SIZE (gnu_type, size);
8329
 
8330
  /* ...or the Ada size for record and union types.  */
8331
  else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8332
           && !TYPE_FAT_POINTER_P (gnu_type))
8333
    SET_TYPE_ADA_SIZE (gnu_type, size);
8334
}
8335
 
8336
/* Given a type TYPE, return a new type whose size is appropriate for SIZE.
8337
   If TYPE is the best type, return it.  Otherwise, make a new type.  We
8338
   only support new integral and pointer types.  FOR_BIASED is true if
8339
   we are making a biased type.  */
8340
 
8341
static tree
8342
make_type_from_size (tree type, tree size_tree, bool for_biased)
8343
{
8344
  unsigned HOST_WIDE_INT size;
8345
  bool biased_p;
8346
  tree new_type;
8347
 
8348
  /* If size indicates an error, just return TYPE to avoid propagating
8349
     the error.  Likewise if it's too large to represent.  */
8350
  if (!size_tree || !host_integerp (size_tree, 1))
8351
    return type;
8352
 
8353
  size = tree_low_cst (size_tree, 1);
8354
 
8355
  switch (TREE_CODE (type))
8356
    {
8357
    case INTEGER_TYPE:
8358
    case ENUMERAL_TYPE:
8359
    case BOOLEAN_TYPE:
8360
      biased_p = (TREE_CODE (type) == INTEGER_TYPE
8361
                  && TYPE_BIASED_REPRESENTATION_P (type));
8362
 
8363
      /* Integer types with precision 0 are forbidden.  */
8364
      if (size == 0)
8365
        size = 1;
8366
 
8367
      /* Only do something if the type is not a packed array type and
8368
         doesn't already have the proper size.  */
8369
      if (TYPE_IS_PACKED_ARRAY_TYPE_P (type)
8370
          || (TYPE_PRECISION (type) == size && biased_p == for_biased))
8371
        break;
8372
 
8373
      biased_p |= for_biased;
8374
      if (size > LONG_LONG_TYPE_SIZE)
8375
        size = LONG_LONG_TYPE_SIZE;
8376
 
8377
      if (TYPE_UNSIGNED (type) || biased_p)
8378
        new_type = make_unsigned_type (size);
8379
      else
8380
        new_type = make_signed_type (size);
8381
      TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
8382
      SET_TYPE_RM_MIN_VALUE (new_type,
8383
                             convert (TREE_TYPE (new_type),
8384
                                      TYPE_MIN_VALUE (type)));
8385
      SET_TYPE_RM_MAX_VALUE (new_type,
8386
                             convert (TREE_TYPE (new_type),
8387
                                      TYPE_MAX_VALUE (type)));
8388
      /* Copy the name to show that it's essentially the same type and
8389
         not a subrange type.  */
8390
      TYPE_NAME (new_type) = TYPE_NAME (type);
8391
      TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
8392
      SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
8393
      return new_type;
8394
 
8395
    case RECORD_TYPE:
8396
      /* Do something if this is a fat pointer, in which case we
8397
         may need to return the thin pointer.  */
8398
      if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
8399
        {
8400
          enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
8401
          if (!targetm.valid_pointer_mode (p_mode))
8402
            p_mode = ptr_mode;
8403
          return
8404
            build_pointer_type_for_mode
8405
              (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
8406
               p_mode, 0);
8407
        }
8408
      break;
8409
 
8410
    case POINTER_TYPE:
8411
      /* Only do something if this is a thin pointer, in which case we
8412
         may need to return the fat pointer.  */
8413
      if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
8414
        return
8415
          build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
8416
      break;
8417
 
8418
    default:
8419
      break;
8420
    }
8421
 
8422
  return type;
8423
}
8424
 
8425
/* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8426
   a type or object whose present alignment is ALIGN.  If this alignment is
8427
   valid, return it.  Otherwise, give an error and return ALIGN.  */
8428
 
8429
static unsigned int
8430
validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8431
{
8432
  unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8433
  unsigned int new_align;
8434
  Node_Id gnat_error_node;
8435
 
8436
  /* Don't worry about checking alignment if alignment was not specified
8437
     by the source program and we already posted an error for this entity.  */
8438
  if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8439
    return align;
8440
 
8441
  /* Post the error on the alignment clause if any.  Note, for the implicit
8442
     base type of an array type, the alignment clause is on the first
8443
     subtype.  */
8444
  if (Present (Alignment_Clause (gnat_entity)))
8445
    gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8446
 
8447
  else if (Is_Itype (gnat_entity)
8448
           && Is_Array_Type (gnat_entity)
8449
           && Etype (gnat_entity) == gnat_entity
8450
           && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8451
    gnat_error_node =
8452
      Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8453
 
8454
  else
8455
    gnat_error_node = gnat_entity;
8456
 
8457
  /* Within GCC, an alignment is an integer, so we must make sure a value is
8458
     specified that fits in that range.  Also, there is an upper bound to
8459
     alignments we can support/allow.  */
8460
  if (!UI_Is_In_Int_Range (alignment)
8461
      || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8462
    post_error_ne_num ("largest supported alignment for& is ^",
8463
                       gnat_error_node, gnat_entity, max_allowed_alignment);
8464
  else if (!(Present (Alignment_Clause (gnat_entity))
8465
             && From_At_Mod (Alignment_Clause (gnat_entity)))
8466
           && new_align * BITS_PER_UNIT < align)
8467
    {
8468
      unsigned int double_align;
8469
      bool is_capped_double, align_clause;
8470
 
8471
      /* If the default alignment of "double" or larger scalar types is
8472
         specifically capped and the new alignment is above the cap, do
8473
         not post an error and change the alignment only if there is an
8474
         alignment clause; this makes it possible to have the associated
8475
         GCC type overaligned by default for performance reasons.  */
8476
      if ((double_align = double_float_alignment) > 0)
8477
        {
8478
          Entity_Id gnat_type
8479
            = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8480
          is_capped_double
8481
            = is_double_float_or_array (gnat_type, &align_clause);
8482
        }
8483
      else if ((double_align = double_scalar_alignment) > 0)
8484
        {
8485
          Entity_Id gnat_type
8486
            = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8487
          is_capped_double
8488
            = is_double_scalar_or_array (gnat_type, &align_clause);
8489
        }
8490
      else
8491
        is_capped_double = align_clause = false;
8492
 
8493
      if (is_capped_double && new_align >= double_align)
8494
        {
8495
          if (align_clause)
8496
            align = new_align * BITS_PER_UNIT;
8497
        }
8498
      else
8499
        {
8500
          if (is_capped_double)
8501
            align = double_align * BITS_PER_UNIT;
8502
 
8503
          post_error_ne_num ("alignment for& must be at least ^",
8504
                             gnat_error_node, gnat_entity,
8505
                             align / BITS_PER_UNIT);
8506
        }
8507
    }
8508
  else
8509
    {
8510
      new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8511
      if (new_align > align)
8512
        align = new_align;
8513
    }
8514
 
8515
  return align;
8516
}
8517
 
8518
/* Return the smallest alignment not less than SIZE.  */
8519
 
8520
static unsigned int
8521
ceil_alignment (unsigned HOST_WIDE_INT size)
8522
{
8523
  return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
8524
}
8525
 
8526
/* Verify that OBJECT, a type or decl, is something we can implement
8527
   atomically.  If not, give an error for GNAT_ENTITY.  COMP_P is true
8528
   if we require atomic components.  */
8529
 
8530
static void
8531
check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8532
{
8533
  Node_Id gnat_error_point = gnat_entity;
8534
  Node_Id gnat_node;
8535
  enum machine_mode mode;
8536
  unsigned int align;
8537
  tree size;
8538
 
8539
  /* There are three case of what OBJECT can be.  It can be a type, in which
8540
     case we take the size, alignment and mode from the type.  It can be a
8541
     declaration that was indirect, in which case the relevant values are
8542
     that of the type being pointed to, or it can be a normal declaration,
8543
     in which case the values are of the decl.  The code below assumes that
8544
     OBJECT is either a type or a decl.  */
8545
  if (TYPE_P (object))
8546
    {
8547
      /* If this is an anonymous base type, nothing to check.  Error will be
8548
         reported on the source type.  */
8549
      if (!Comes_From_Source (gnat_entity))
8550
        return;
8551
 
8552
      mode = TYPE_MODE (object);
8553
      align = TYPE_ALIGN (object);
8554
      size = TYPE_SIZE (object);
8555
    }
8556
  else if (DECL_BY_REF_P (object))
8557
    {
8558
      mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8559
      align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8560
      size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8561
    }
8562
  else
8563
    {
8564
      mode = DECL_MODE (object);
8565
      align = DECL_ALIGN (object);
8566
      size = DECL_SIZE (object);
8567
    }
8568
 
8569
  /* Consider all floating-point types atomic and any types that that are
8570
     represented by integers no wider than a machine word.  */
8571
  if (GET_MODE_CLASS (mode) == MODE_FLOAT
8572
      || ((GET_MODE_CLASS (mode) == MODE_INT
8573
           || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8574
          && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8575
    return;
8576
 
8577
  /* For the moment, also allow anything that has an alignment equal
8578
     to its size and which is smaller than a word.  */
8579
  if (size && TREE_CODE (size) == INTEGER_CST
8580
      && compare_tree_int (size, align) == 0
8581
      && align <= BITS_PER_WORD)
8582
    return;
8583
 
8584
  for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8585
       gnat_node = Next_Rep_Item (gnat_node))
8586
    {
8587
      if (!comp_p && Nkind (gnat_node) == N_Pragma
8588
          && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8589
              == Pragma_Atomic))
8590
        gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8591
      else if (comp_p && Nkind (gnat_node) == N_Pragma
8592
               && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8593
                   == Pragma_Atomic_Components))
8594
        gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8595
    }
8596
 
8597
  if (comp_p)
8598
    post_error_ne ("atomic access to component of & cannot be guaranteed",
8599
                   gnat_error_point, gnat_entity);
8600
  else
8601
    post_error_ne ("atomic access to & cannot be guaranteed",
8602
                   gnat_error_point, gnat_entity);
8603
}
8604
 
8605
 
8606
/* Helper for the intrin compatibility checks family.  Evaluate whether
8607
   two types are definitely incompatible.  */
8608
 
8609
static bool
8610
intrin_types_incompatible_p (tree t1, tree t2)
8611
{
8612
  enum tree_code code;
8613
 
8614
  if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8615
    return false;
8616
 
8617
  if (TYPE_MODE (t1) != TYPE_MODE (t2))
8618
    return true;
8619
 
8620
  if (TREE_CODE (t1) != TREE_CODE (t2))
8621
    return true;
8622
 
8623
  code = TREE_CODE (t1);
8624
 
8625
  switch (code)
8626
    {
8627
    case INTEGER_TYPE:
8628
    case REAL_TYPE:
8629
      return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8630
 
8631
    case POINTER_TYPE:
8632
    case REFERENCE_TYPE:
8633
      /* Assume designated types are ok.  We'd need to account for char * and
8634
         void * variants to do better, which could rapidly get messy and isn't
8635
         clearly worth the effort.  */
8636
      return false;
8637
 
8638
    default:
8639
      break;
8640
    }
8641
 
8642
  return false;
8643
}
8644
 
8645
/* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8646
   on the Ada/builtin argument lists for the INB binding.  */
8647
 
8648
static bool
8649
intrin_arglists_compatible_p (intrin_binding_t * inb)
8650
{
8651
  function_args_iterator ada_iter, btin_iter;
8652
 
8653
  function_args_iter_init (&ada_iter, inb->ada_fntype);
8654
  function_args_iter_init (&btin_iter, inb->btin_fntype);
8655
 
8656
  /* Sequence position of the last argument we checked.  */
8657
  int argpos = 0;
8658
 
8659
  while (1)
8660
    {
8661
      tree ada_type = function_args_iter_cond (&ada_iter);
8662
      tree btin_type = function_args_iter_cond (&btin_iter);
8663
 
8664
      /* If we've exhausted both lists simultaneously, we're done.  */
8665
      if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8666
        break;
8667
 
8668
      /* If one list is shorter than the other, they fail to match.  */
8669
      if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8670
        return false;
8671
 
8672
      /* If we're done with the Ada args and not with the internal builtin
8673
         args, or the other way around, complain.  */
8674
      if (ada_type == void_type_node
8675
          && btin_type != void_type_node)
8676
        {
8677
          post_error ("?Ada arguments list too short!", inb->gnat_entity);
8678
          return false;
8679
        }
8680
 
8681
      if (btin_type == void_type_node
8682
          && ada_type != void_type_node)
8683
        {
8684
          post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8685
                             inb->gnat_entity, inb->gnat_entity, argpos);
8686
          return false;
8687
        }
8688
 
8689
      /* Otherwise, check that types match for the current argument.  */
8690
      argpos ++;
8691
      if (intrin_types_incompatible_p (ada_type, btin_type))
8692
        {
8693
          post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8694
                             inb->gnat_entity, inb->gnat_entity, argpos);
8695
          return false;
8696
        }
8697
 
8698
 
8699
      function_args_iter_next (&ada_iter);
8700
      function_args_iter_next (&btin_iter);
8701
    }
8702
 
8703
  return true;
8704
}
8705
 
8706
/* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8707
   on the Ada/builtin return values for the INB binding.  */
8708
 
8709
static bool
8710
intrin_return_compatible_p (intrin_binding_t * inb)
8711
{
8712
  tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8713
  tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8714
 
8715
  /* Accept function imported as procedure, common and convenient.  */
8716
  if (VOID_TYPE_P (ada_return_type)
8717
      && !VOID_TYPE_P (btin_return_type))
8718
    return true;
8719
 
8720
  /* Check return types compatibility otherwise.  Note that this
8721
     handles void/void as well.  */
8722
  if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8723
    {
8724
      post_error ("?intrinsic binding type mismatch on return value!",
8725
                  inb->gnat_entity);
8726
      return false;
8727
    }
8728
 
8729
  return true;
8730
}
8731
 
8732
/* Check and return whether the Ada and gcc builtin profiles bound by INB are
8733
   compatible.  Issue relevant warnings when they are not.
8734
 
8735
   This is intended as a light check to diagnose the most obvious cases, not
8736
   as a full fledged type compatibility predicate.  It is the programmer's
8737
   responsibility to ensure correctness of the Ada declarations in Imports,
8738
   especially when binding straight to a compiler internal.  */
8739
 
8740
static bool
8741
intrin_profiles_compatible_p (intrin_binding_t * inb)
8742
{
8743
  /* Check compatibility on return values and argument lists, each responsible
8744
     for posting warnings as appropriate.  Ensure use of the proper sloc for
8745
     this purpose.  */
8746
 
8747
  bool arglists_compatible_p, return_compatible_p;
8748
  location_t saved_location = input_location;
8749
 
8750
  Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8751
 
8752
  return_compatible_p = intrin_return_compatible_p (inb);
8753
  arglists_compatible_p = intrin_arglists_compatible_p (inb);
8754
 
8755
  input_location = saved_location;
8756
 
8757
  return return_compatible_p && arglists_compatible_p;
8758
}
8759
 
8760
/* Return a FIELD_DECL node modeled on OLD_FIELD.  FIELD_TYPE is its type
8761
   and RECORD_TYPE is the type of the parent.  If SIZE is nonzero, it is the
8762
   specified size for this field.  POS_LIST is a position list describing
8763
   the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8764
   to this layout.  */
8765
 
8766
static tree
8767
create_field_decl_from (tree old_field, tree field_type, tree record_type,
8768
                        tree size, tree pos_list,
8769
                        VEC(subst_pair,heap) *subst_list)
8770
{
8771
  tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8772
  tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8773
  unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8774
  tree new_pos, new_field;
8775
  unsigned ix;
8776
  subst_pair *s;
8777
 
8778
  if (CONTAINS_PLACEHOLDER_P (pos))
8779
    FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8780
      pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8781
 
8782
  /* If the position is now a constant, we can set it as the position of the
8783
     field when we make it.  Otherwise, we need to deal with it specially.  */
8784
  if (TREE_CONSTANT (pos))
8785
    new_pos = bit_from_pos (pos, bitpos);
8786
  else
8787
    new_pos = NULL_TREE;
8788
 
8789
  new_field
8790
    = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8791
                         size, new_pos, DECL_PACKED (old_field),
8792
                         !DECL_NONADDRESSABLE_P (old_field));
8793
 
8794
  if (!new_pos)
8795
    {
8796
      normalize_offset (&pos, &bitpos, offset_align);
8797
      DECL_FIELD_OFFSET (new_field) = pos;
8798
      DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8799
      SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8800
      DECL_SIZE (new_field) = size;
8801
      DECL_SIZE_UNIT (new_field)
8802
        = convert (sizetype,
8803
                   size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8804
      layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8805
    }
8806
 
8807
  DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8808
  SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8809
  DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8810
  TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8811
 
8812
  return new_field;
8813
}
8814
 
8815
/* Create the REP part of RECORD_TYPE with REP_TYPE.  If MIN_SIZE is nonzero,
8816
   it is the minimal size the REP_PART must have.  */
8817
 
8818
static tree
8819
create_rep_part (tree rep_type, tree record_type, tree min_size)
8820
{
8821
  tree field;
8822
 
8823
  if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8824
    min_size = NULL_TREE;
8825
 
8826
  field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8827
                             min_size, bitsize_zero_node, 0, 1);
8828
  DECL_INTERNAL_P (field) = 1;
8829
 
8830
  return field;
8831
}
8832
 
8833
/* Return the REP part of RECORD_TYPE, if any.  Otherwise return NULL.  */
8834
 
8835
static tree
8836
get_rep_part (tree record_type)
8837
{
8838
  tree field = TYPE_FIELDS (record_type);
8839
 
8840
  /* The REP part is the first field, internal, another record, and its name
8841
     starts with an 'R'.  */
8842
  if (DECL_INTERNAL_P (field)
8843
      && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8844
      && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8845
    return field;
8846
 
8847
  return NULL_TREE;
8848
}
8849
 
8850
/* Return the variant part of RECORD_TYPE, if any.  Otherwise return NULL.  */
8851
 
8852
tree
8853
get_variant_part (tree record_type)
8854
{
8855
  tree field;
8856
 
8857
  /* The variant part is the only internal field that is a qualified union.  */
8858
  for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8859
    if (DECL_INTERNAL_P (field)
8860
        && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8861
      return field;
8862
 
8863
  return NULL_TREE;
8864
}
8865
 
8866
/* Return a new variant part modeled on OLD_VARIANT_PART.  VARIANT_LIST is
8867
   the list of variants to be used and RECORD_TYPE is the type of the parent.
8868
   POS_LIST is a position list describing the layout of fields present in
8869
   OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8870
   layout.  */
8871
 
8872
static tree
8873
create_variant_part_from (tree old_variant_part,
8874
                          VEC(variant_desc,heap) *variant_list,
8875
                          tree record_type, tree pos_list,
8876
                          VEC(subst_pair,heap) *subst_list)
8877
{
8878
  tree offset = DECL_FIELD_OFFSET (old_variant_part);
8879
  tree old_union_type = TREE_TYPE (old_variant_part);
8880
  tree new_union_type, new_variant_part;
8881
  tree union_field_list = NULL_TREE;
8882
  variant_desc *v;
8883
  unsigned ix;
8884
 
8885
  /* First create the type of the variant part from that of the old one.  */
8886
  new_union_type = make_node (QUAL_UNION_TYPE);
8887
  TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8888
 
8889
  /* If the position of the variant part is constant, subtract it from the
8890
     size of the type of the parent to get the new size.  This manual CSE
8891
     reduces the code size when not optimizing.  */
8892
  if (TREE_CODE (offset) == INTEGER_CST)
8893
    {
8894
      tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8895
      tree first_bit = bit_from_pos (offset, bitpos);
8896
      TYPE_SIZE (new_union_type)
8897
        = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8898
      TYPE_SIZE_UNIT (new_union_type)
8899
        = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8900
                      byte_from_pos (offset, bitpos));
8901
      SET_TYPE_ADA_SIZE (new_union_type,
8902
                         size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8903
                                     first_bit));
8904
      TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8905
      relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8906
    }
8907
  else
8908
    copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8909
 
8910
  /* Now finish up the new variants and populate the union type.  */
8911
  FOR_EACH_VEC_ELT_REVERSE (variant_desc, variant_list, ix, v)
8912
    {
8913
      tree old_field = v->field, new_field;
8914
      tree old_variant, old_variant_subpart, new_variant, field_list;
8915
 
8916
      /* Skip variants that don't belong to this nesting level.  */
8917
      if (DECL_CONTEXT (old_field) != old_union_type)
8918
        continue;
8919
 
8920
      /* Retrieve the list of fields already added to the new variant.  */
8921
      new_variant = v->record;
8922
      field_list = TYPE_FIELDS (new_variant);
8923
 
8924
      /* If the old variant had a variant subpart, we need to create a new
8925
         variant subpart and add it to the field list.  */
8926
      old_variant = v->type;
8927
      old_variant_subpart = get_variant_part (old_variant);
8928
      if (old_variant_subpart)
8929
        {
8930
          tree new_variant_subpart
8931
            = create_variant_part_from (old_variant_subpart, variant_list,
8932
                                        new_variant, pos_list, subst_list);
8933
          DECL_CHAIN (new_variant_subpart) = field_list;
8934
          field_list = new_variant_subpart;
8935
        }
8936
 
8937
      /* Finish up the new variant and create the field.  No need for debug
8938
         info thanks to the XVS type.  */
8939
      finish_record_type (new_variant, nreverse (field_list), 2, false);
8940
      compute_record_mode (new_variant);
8941
      create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8942
                        true, false, Empty);
8943
 
8944
      new_field
8945
        = create_field_decl_from (old_field, new_variant, new_union_type,
8946
                                  TYPE_SIZE (new_variant),
8947
                                  pos_list, subst_list);
8948
      DECL_QUALIFIER (new_field) = v->qual;
8949
      DECL_INTERNAL_P (new_field) = 1;
8950
      DECL_CHAIN (new_field) = union_field_list;
8951
      union_field_list = new_field;
8952
    }
8953
 
8954
  /* Finish up the union type and create the variant part.  No need for debug
8955
     info thanks to the XVS type.  */
8956
  finish_record_type (new_union_type, union_field_list, 2, false);
8957
  compute_record_mode (new_union_type);
8958
  create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8959
                    true, false, Empty);
8960
 
8961
  new_variant_part
8962
    = create_field_decl_from (old_variant_part, new_union_type, record_type,
8963
                              TYPE_SIZE (new_union_type),
8964
                              pos_list, subst_list);
8965
  DECL_INTERNAL_P (new_variant_part) = 1;
8966
 
8967
  /* With multiple discriminants it is possible for an inner variant to be
8968
     statically selected while outer ones are not; in this case, the list
8969
     of fields of the inner variant is not flattened and we end up with a
8970
     qualified union with a single member.  Drop the useless container.  */
8971
  if (!DECL_CHAIN (union_field_list))
8972
    {
8973
      DECL_CONTEXT (union_field_list) = record_type;
8974
      DECL_FIELD_OFFSET (union_field_list)
8975
        = DECL_FIELD_OFFSET (new_variant_part);
8976
      DECL_FIELD_BIT_OFFSET (union_field_list)
8977
        = DECL_FIELD_BIT_OFFSET (new_variant_part);
8978
      SET_DECL_OFFSET_ALIGN (union_field_list,
8979
                             DECL_OFFSET_ALIGN (new_variant_part));
8980
      new_variant_part = union_field_list;
8981
    }
8982
 
8983
  return new_variant_part;
8984
}
8985
 
8986
/* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8987
   which are both RECORD_TYPE, after applying the substitutions described
8988
   in SUBST_LIST.  */
8989
 
8990
static void
8991
copy_and_substitute_in_size (tree new_type, tree old_type,
8992
                             VEC(subst_pair,heap) *subst_list)
8993
{
8994
  unsigned ix;
8995
  subst_pair *s;
8996
 
8997
  TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8998
  TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8999
  SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
9000
  TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
9001
  relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
9002
 
9003
  if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
9004
    FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
9005
      TYPE_SIZE (new_type)
9006
        = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
9007
                              s->discriminant, s->replacement);
9008
 
9009
  if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
9010
    FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
9011
      TYPE_SIZE_UNIT (new_type)
9012
        = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
9013
                              s->discriminant, s->replacement);
9014
 
9015
  if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
9016
    FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
9017
      SET_TYPE_ADA_SIZE
9018
        (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
9019
                                       s->discriminant, s->replacement));
9020
 
9021
  /* Finalize the size.  */
9022
  TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
9023
  TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
9024
}
9025
 
9026
/* Given a type T, a FIELD_DECL F, and a replacement value R, return a
9027
   type with all size expressions that contain F in a PLACEHOLDER_EXPR
9028
   updated by replacing F with R.
9029
 
9030
   The function doesn't update the layout of the type, i.e. it assumes
9031
   that the substitution is purely formal.  That's why the replacement
9032
   value R must itself contain a PLACEHOLDER_EXPR.  */
9033
 
9034
tree
9035
substitute_in_type (tree t, tree f, tree r)
9036
{
9037
  tree nt;
9038
 
9039
  gcc_assert (CONTAINS_PLACEHOLDER_P (r));
9040
 
9041
  switch (TREE_CODE (t))
9042
    {
9043
    case INTEGER_TYPE:
9044
    case ENUMERAL_TYPE:
9045
    case BOOLEAN_TYPE:
9046
    case REAL_TYPE:
9047
 
9048
      /* First the domain types of arrays.  */
9049
      if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
9050
          || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
9051
        {
9052
          tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
9053
          tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
9054
 
9055
          if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
9056
            return t;
9057
 
9058
          nt = copy_type (t);
9059
          TYPE_GCC_MIN_VALUE (nt) = low;
9060
          TYPE_GCC_MAX_VALUE (nt) = high;
9061
 
9062
          if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
9063
            SET_TYPE_INDEX_TYPE
9064
              (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9065
 
9066
          return nt;
9067
        }
9068
 
9069
      /* Then the subtypes.  */
9070
      if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9071
          || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9072
        {
9073
          tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9074
          tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9075
 
9076
          if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9077
            return t;
9078
 
9079
          nt = copy_type (t);
9080
          SET_TYPE_RM_MIN_VALUE (nt, low);
9081
          SET_TYPE_RM_MAX_VALUE (nt, high);
9082
 
9083
          return nt;
9084
        }
9085
 
9086
      return t;
9087
 
9088
    case COMPLEX_TYPE:
9089
      nt = substitute_in_type (TREE_TYPE (t), f, r);
9090
      if (nt == TREE_TYPE (t))
9091
        return t;
9092
 
9093
      return build_complex_type (nt);
9094
 
9095
    case FUNCTION_TYPE:
9096
      /* These should never show up here.  */
9097
      gcc_unreachable ();
9098
 
9099
    case ARRAY_TYPE:
9100
      {
9101
        tree component = substitute_in_type (TREE_TYPE (t), f, r);
9102
        tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9103
 
9104
        if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9105
          return t;
9106
 
9107
        nt = build_nonshared_array_type (component, domain);
9108
        TYPE_ALIGN (nt) = TYPE_ALIGN (t);
9109
        TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9110
        SET_TYPE_MODE (nt, TYPE_MODE (t));
9111
        TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9112
        TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9113
        TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
9114
        TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9115
        TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9116
        return nt;
9117
      }
9118
 
9119
    case RECORD_TYPE:
9120
    case UNION_TYPE:
9121
    case QUAL_UNION_TYPE:
9122
      {
9123
        bool changed_field = false;
9124
        tree field;
9125
 
9126
        /* Start out with no fields, make new fields, and chain them
9127
           in.  If we haven't actually changed the type of any field,
9128
           discard everything we've done and return the old type.  */
9129
        nt = copy_type (t);
9130
        TYPE_FIELDS (nt) = NULL_TREE;
9131
 
9132
        for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9133
          {
9134
            tree new_field = copy_node (field), new_n;
9135
 
9136
            new_n = substitute_in_type (TREE_TYPE (field), f, r);
9137
            if (new_n != TREE_TYPE (field))
9138
              {
9139
                TREE_TYPE (new_field) = new_n;
9140
                changed_field = true;
9141
              }
9142
 
9143
            new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9144
            if (new_n != DECL_FIELD_OFFSET (field))
9145
              {
9146
                DECL_FIELD_OFFSET (new_field) = new_n;
9147
                changed_field = true;
9148
              }
9149
 
9150
            /* Do the substitution inside the qualifier, if any.  */
9151
            if (TREE_CODE (t) == QUAL_UNION_TYPE)
9152
              {
9153
                new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9154
                if (new_n != DECL_QUALIFIER (field))
9155
                  {
9156
                    DECL_QUALIFIER (new_field) = new_n;
9157
                    changed_field = true;
9158
                  }
9159
              }
9160
 
9161
            DECL_CONTEXT (new_field) = nt;
9162
            SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9163
 
9164
            DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9165
            TYPE_FIELDS (nt) = new_field;
9166
          }
9167
 
9168
        if (!changed_field)
9169
          return t;
9170
 
9171
        TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9172
        TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9173
        TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9174
        SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9175
        return nt;
9176
      }
9177
 
9178
    default:
9179
      return t;
9180
    }
9181
}
9182
 
9183
/* Return the RM size of GNU_TYPE.  This is the actual number of bits
9184
   needed to represent the object.  */
9185
 
9186
tree
9187
rm_size (tree gnu_type)
9188
{
9189
  /* For integral types, we store the RM size explicitly.  */
9190
  if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9191
    return TYPE_RM_SIZE (gnu_type);
9192
 
9193
  /* Return the RM size of the actual data plus the size of the template.  */
9194
  if (TREE_CODE (gnu_type) == RECORD_TYPE
9195
      && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9196
    return
9197
      size_binop (PLUS_EXPR,
9198
                  rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9199
                  DECL_SIZE (TYPE_FIELDS (gnu_type)));
9200
 
9201
  /* For record or union types, we store the size explicitly.  */
9202
  if (RECORD_OR_UNION_TYPE_P (gnu_type)
9203
      && !TYPE_FAT_POINTER_P (gnu_type)
9204
      && TYPE_ADA_SIZE (gnu_type))
9205
    return TYPE_ADA_SIZE (gnu_type);
9206
 
9207
  /* For other types, this is just the size.  */
9208
  return TYPE_SIZE (gnu_type);
9209
}
9210
 
9211
/* Return the name to be used for GNAT_ENTITY.  If a type, create a
9212
   fully-qualified name, possibly with type information encoding.
9213
   Otherwise, return the name.  */
9214
 
9215
tree
9216
get_entity_name (Entity_Id gnat_entity)
9217
{
9218
  Get_Encoded_Name (gnat_entity);
9219
  return get_identifier_with_length (Name_Buffer, Name_Len);
9220
}
9221
 
9222
/* Return an identifier representing the external name to be used for
9223
   GNAT_ENTITY.  If SUFFIX is specified, the name is followed by "___"
9224
   and the specified suffix.  */
9225
 
9226
tree
9227
create_concat_name (Entity_Id gnat_entity, const char *suffix)
9228
{
9229
  Entity_Kind kind = Ekind (gnat_entity);
9230
 
9231
  if (suffix)
9232
    {
9233
      String_Template temp = {1, (int) strlen (suffix)};
9234
      Fat_Pointer fp = {suffix, &temp};
9235
      Get_External_Name_With_Suffix (gnat_entity, fp);
9236
    }
9237
  else
9238
    Get_External_Name (gnat_entity, 0);
9239
 
9240
  /* A variable using the Stdcall convention lives in a DLL.  We adjust
9241
     its name to use the jump table, the _imp__NAME contains the address
9242
     for the NAME variable.  */
9243
  if ((kind == E_Variable || kind == E_Constant)
9244
      && Has_Stdcall_Convention (gnat_entity))
9245
    {
9246
      const int len = 6 + Name_Len;
9247
      char *new_name = (char *) alloca (len + 1);
9248
      strcpy (new_name, "_imp__");
9249
      strcat (new_name, Name_Buffer);
9250
      return get_identifier_with_length (new_name, len);
9251
    }
9252
 
9253
  return get_identifier_with_length (Name_Buffer, Name_Len);
9254
}
9255
 
9256
/* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9257
   string, return a new IDENTIFIER_NODE that is the concatenation of
9258
   the name followed by "___" and the specified suffix.  */
9259
 
9260
tree
9261
concat_name (tree gnu_name, const char *suffix)
9262
{
9263
  const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9264
  char *new_name = (char *) alloca (len + 1);
9265
  strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9266
  strcat (new_name, "___");
9267
  strcat (new_name, suffix);
9268
  return get_identifier_with_length (new_name, len);
9269
}
9270
 
9271
#include "gt-ada-decl.h"

powered by: WebSVN 2.1.0

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