OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [gcc-interface/] [decl.c] - Blame information for rev 303

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

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