OSDN Git Service

* decl.c (gnat_to_gnu_entity, case E_General_Access_Type):
[pf3gnuchains/gcc-fork.git] / gcc / ada / decl.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                                 D E C L                                   *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *                            $Revision$
10  *                                                                          *
11  *          Copyright (C) 1992-2001, Free Software Foundation, Inc.         *
12  *                                                                          *
13  * GNAT is free software;  you can  redistribute it  and/or modify it under *
14  * terms of the  GNU General Public License as published  by the Free Soft- *
15  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
16  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
17  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
19  * for  more details.  You should have  received  a copy of the GNU General *
20  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
21  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
22  * MA 02111-1307, USA.                                                      *
23  *                                                                          *
24  * GNAT was originally developed  by the GNAT team at  New York University. *
25  * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
26  *                                                                          *
27  ****************************************************************************/
28
29 #include "config.h"
30 #include "system.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "convert.h"
35 #include "ggc.h"
36 #include "obstack.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 /* Setting this to 1 suppresses hashing of types.  */
55 extern int debug_no_type_hash;
56
57 /* Provide default values for the macros controlling stack checking.
58    This is copied from GCC's expr.h.  */
59
60 #ifndef STACK_CHECK_BUILTIN
61 #define STACK_CHECK_BUILTIN 0
62 #endif
63 #ifndef STACK_CHECK_PROBE_INTERVAL
64 #define STACK_CHECK_PROBE_INTERVAL 4096
65 #endif
66 #ifndef STACK_CHECK_MAX_FRAME_SIZE
67 #define STACK_CHECK_MAX_FRAME_SIZE \
68   (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
69 #endif
70 #ifndef STACK_CHECK_MAX_VAR_SIZE
71 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
72 #endif
73
74 /* These two variables are used to defer recursively expanding incomplete
75    types while we are processing a record or subprogram type.  */
76
77 static int defer_incomplete_level = 0;
78 static struct incomplete
79 {
80   struct incomplete *next;
81   tree old_type;
82   Entity_Id full_type;
83 } *defer_incomplete_list = 0;
84
85 static tree substitution_list           PARAMS ((Entity_Id, Entity_Id,
86                                                  tree, int));
87 static int allocatable_size_p           PARAMS ((tree, int));
88 static struct attrib *build_attr_list   PARAMS ((Entity_Id));
89 static tree elaborate_expression        PARAMS ((Node_Id, Entity_Id, tree,
90                                                  int, int, int));
91 static tree elaborate_expression_1      PARAMS ((Node_Id, Entity_Id, tree,
92                                                  tree, int, int));
93 static tree make_packable_type          PARAMS ((tree));
94 static tree maybe_pad_type              PARAMS ((tree, tree, unsigned int,
95                                                  Entity_Id, const char *, int,
96                                                  int, int));
97 static tree gnat_to_gnu_field           PARAMS ((Entity_Id, tree, int, int));
98 static void components_to_record        PARAMS ((tree, Node_Id, tree, int,
99                                                  int, tree *, int, int));
100 static int compare_field_bitpos         PARAMS ((const PTR, const PTR));
101 static Uint annotate_value              PARAMS ((tree));
102 static void annotate_rep                PARAMS ((Entity_Id, tree));
103 static tree compute_field_positions     PARAMS ((tree, tree, tree, tree));
104 static tree validate_size               PARAMS ((Uint, tree, Entity_Id,
105                                                  enum tree_code, int, int));
106 static void set_rm_size                 PARAMS ((Uint, tree, Entity_Id));
107 static tree make_type_from_size         PARAMS ((tree, tree, int));
108 static unsigned int validate_alignment  PARAMS ((Uint, Entity_Id,
109                                                  unsigned int));
110 static void check_ok_for_atomic         PARAMS ((tree, Entity_Id, int));
111 \f
112 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
113    GCC type corresponding to that entity.  GNAT_ENTITY is assumed to
114    refer to an Ada type.  */
115
116 tree
117 gnat_to_gnu_type (gnat_entity)
118      Entity_Id gnat_entity;
119 {
120   tree gnu_decl;
121
122   /* Convert the ada entity type into a GCC TYPE_DECL node.  */
123   gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
124   if (TREE_CODE (gnu_decl) != TYPE_DECL)
125     gigi_abort (101);
126
127   return TREE_TYPE (gnu_decl);
128 }
129 \f
130 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
131    entity, this routine returns the equivalent GCC tree for that entity
132    (an ..._DECL node) and associates the ..._DECL node with the input GNAT
133    defining identifier.
134
135    If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
136    initial value (in GCC tree form). This is optional for variables.
137    For renamed entities, GNU_EXPR gives the object being renamed.
138
139    DEFINITION is nonzero if this call is intended for a definition.  This is
140    used for separate compilation where it necessary to know whether an
141    external declaration or a definition should be created if the GCC equivalent
142    was not created previously.  The value of 1 is normally used for a non-zero
143    DEFINITION, but a value of 2 is used in special circumstances, defined in
144    the code.  */
145
146 tree
147 gnat_to_gnu_entity (gnat_entity, gnu_expr, definition)
148      Entity_Id gnat_entity;
149      tree gnu_expr;
150      int definition;
151 {
152   tree gnu_entity_id;
153   tree gnu_type = 0;
154   /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
155      GNAT tree. This node will be associated with the GNAT node by calling
156      the save_gnu_tree routine at the end of the `switch' statement.  */
157   tree gnu_decl = 0;
158   /* Nonzero if we have already saved gnu_decl as a gnat association.  */
159   int saved = 0;
160   /* Nonzero if we incremented defer_incomplete_level.  */
161   int this_deferred = 0;
162   /* Nonzero if we incremented force_global.  */
163   int this_global = 0;
164   /* Nonzero if we should check to see if elaborated during processing.  */
165   int maybe_present = 0;
166   /* Nonzero if we made GNU_DECL and its type here.  */
167   int this_made_decl = 0;
168   struct attrib *attr_list = 0;
169   int debug_info_p = (Needs_Debug_Info (gnat_entity)
170                       || debug_info_level == DINFO_LEVEL_VERBOSE);
171   Entity_Kind kind = Ekind (gnat_entity);
172   Entity_Id gnat_temp;
173   unsigned int esize
174     = ((Known_Esize (gnat_entity)
175         && UI_Is_In_Int_Range (Esize (gnat_entity)))
176        ? MIN (UI_To_Int (Esize (gnat_entity)),
177               IN (kind, Float_Kind)
178               ? LONG_DOUBLE_TYPE_SIZE
179               : IN (kind, Access_Kind) ? POINTER_SIZE * 2
180               : LONG_LONG_TYPE_SIZE)
181        : LONG_LONG_TYPE_SIZE);
182   tree gnu_size = 0;
183   int imported_p
184     = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
185        || From_With_Type (gnat_entity));
186   unsigned int align = 0;
187
188   /* Since a use of an Itype is a definition, process it as such if it
189      is not in a with'ed unit. */
190
191   if (! definition && Is_Itype (gnat_entity)
192       && ! present_gnu_tree (gnat_entity)
193       && In_Extended_Main_Code_Unit (gnat_entity))
194     {
195       /* Ensure that we are in a subprogram mentioned in the Scope
196          chain of this entity, our current scope is global,
197          or that we encountered a task or entry (where we can't currently
198          accurately check scoping).  */
199       if (current_function_decl == 0
200           || DECL_ELABORATION_PROC_P (current_function_decl))
201         {
202           process_type (gnat_entity);
203           return get_gnu_tree (gnat_entity);
204         }
205
206       for (gnat_temp = Scope (gnat_entity);
207            Present (gnat_temp); gnat_temp = Scope (gnat_temp))
208         {
209           if (Is_Type (gnat_temp))
210             gnat_temp = Underlying_Type (gnat_temp);
211
212           if (Ekind (gnat_temp) == E_Subprogram_Body)
213             gnat_temp
214               = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
215
216           if (IN (Ekind (gnat_temp), Subprogram_Kind)
217               && Present (Protected_Body_Subprogram (gnat_temp)))
218             gnat_temp = Protected_Body_Subprogram (gnat_temp);
219
220           if (Ekind (gnat_temp) == E_Entry
221               || Ekind (gnat_temp) == E_Entry_Family
222               || Ekind (gnat_temp) == E_Task_Type
223               || (IN (Ekind (gnat_temp), Subprogram_Kind)
224                   && present_gnu_tree (gnat_temp)
225                   && (current_function_decl
226                       == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
227             {
228               process_type (gnat_entity);
229               return get_gnu_tree (gnat_entity);
230             }
231         }
232
233       /* gigi abort 122 means that the entity "gnat_entity" has an incorrect
234          scope, i.e. that its scope does not correspond to the subprogram
235          in which it is declared */
236       gigi_abort (122);
237     }
238
239   /* If this is entity 0, something went badly wrong.  */
240   if (gnat_entity == 0)
241     gigi_abort (102);
242
243   /* If we've already processed this entity, return what we got last time.
244      If we are defining the node, we should not have already processed it.
245      In that case, we will abort below when we try to save a new GCC tree for
246      this object.   We also need to handle the case of getting a dummy type
247      when a Full_View exists.  */
248
249   if (present_gnu_tree (gnat_entity)
250       && (! definition
251           || (Is_Type (gnat_entity) && imported_p)))
252     {
253       gnu_decl = get_gnu_tree (gnat_entity);
254
255       if (TREE_CODE (gnu_decl) == TYPE_DECL
256           && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
257           && IN (kind, Incomplete_Or_Private_Kind)
258           && Present (Full_View (gnat_entity)))
259         {
260           gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
261                                          NULL_TREE, 0);
262
263           save_gnu_tree (gnat_entity, NULL_TREE, 0);
264           save_gnu_tree (gnat_entity, gnu_decl, 0);
265         }
266
267       return gnu_decl;
268     }
269
270   /* If this is a numeric or enumeral type, or an access type, a nonzero
271      Esize must be specified unless it was specified by the programmer.  */
272   if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
273        || (IN (kind, Access_Kind)
274            && kind != E_Access_Protected_Subprogram_Type
275            && kind != E_Access_Subtype))
276       && Unknown_Esize (gnat_entity)
277       && ! Has_Size_Clause (gnat_entity))
278     gigi_abort (109);
279
280   /* Likewise, RM_Size must be specified for all discrete and fixed-point
281      types.  */
282   if (IN (kind, Discrete_Or_Fixed_Point_Kind)
283       && Unknown_RM_Size (gnat_entity))
284     gigi_abort (123);
285
286   /* Get the name of the entity and set up the line number and filename of
287      the original definition for use in any decl we make.  */
288
289   gnu_entity_id = get_entity_name (gnat_entity);
290   set_lineno (gnat_entity, 0);
291
292   /* If we get here, it means we have not yet done anything with this
293      entity.  If we are not defining it here, it must be external,
294      otherwise we should have defined it already.  */
295   if (! definition && ! Is_Public (gnat_entity)
296       && ! type_annotate_only
297       && kind != E_Discriminant && kind != E_Component
298       && kind != E_Label
299       && ! (kind == E_Constant && Present (Full_View (gnat_entity)))
300 #if 1
301       && !IN (kind, Type_Kind)
302 #endif
303       )
304     gigi_abort (116);
305
306   /* For cases when we are not defining (i.e., we are referencing from
307      another compilation unit) Public entities, show we are at global level
308      for the purpose of computing sizes.  Don't do this for components or
309      discriminants since the relevant test is whether or not the record is
310      being defined.  */
311   if (! definition && Is_Public (gnat_entity)
312       && ! Is_Statically_Allocated (gnat_entity)
313       && kind != E_Discriminant && kind != E_Component)
314     force_global++, this_global = 1;
315
316   /* Handle any attributes.  */
317   if (Has_Gigi_Rep_Item (gnat_entity))
318     attr_list = build_attr_list (gnat_entity);
319
320   switch (kind)
321     {
322     case E_Constant:
323       /* If this is a use of a deferred constant, get its full
324          declaration.  */
325       if (! definition && Present (Full_View (gnat_entity)))
326         {
327           gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
328                                          gnu_expr, definition);
329           saved = 1;
330           break;
331         }
332
333       /* If we have an external constant that we are not defining,
334          get the expression that is was defined to represent.  We
335          may throw that expression away later if it is not a
336          constant.  */
337       if (! definition
338           && Present (Expression (Declaration_Node (gnat_entity)))
339           && ! No_Initialization (Declaration_Node (gnat_entity)))
340         gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
341
342       /* Ignore deferred constant definitions; they are processed fully in the
343          front-end.  For deferred constant references, get the full
344          definition.  On the other hand, constants that are renamings are
345          handled like variable renamings.  If No_Initialization is set, this is
346          not a deferred constant but a constant whose value is built
347          manually.  */
348
349       if (definition && gnu_expr == 0
350           && ! No_Initialization (Declaration_Node (gnat_entity))
351           && No (Renamed_Object (gnat_entity)))
352         {
353           gnu_decl = error_mark_node;
354           saved = 1;
355           break;
356         }
357       else if (! definition && IN (kind, Incomplete_Or_Private_Kind)
358                && Present (Full_View (gnat_entity)))
359         {
360           gnu_decl =  gnat_to_gnu_entity (Full_View (gnat_entity),
361                                           NULL_TREE, 0);
362           saved = 1;
363           break;
364         }
365
366       goto object;
367
368     case E_Exception:
369       /* If this is not a VMS exception, treat it as a normal object.
370          Otherwise, make an object at the specific address of character
371          type, point to it, and convert it to integer, and mask off
372          the lower 3 bits.  */
373       if (! Is_VMS_Exception (gnat_entity))
374         goto object;
375
376       /* Allocate the global object that we use to get the value of the
377          exception.  */
378       gnu_decl = create_var_decl (gnu_entity_id,
379                                   (Present (Interface_Name (gnat_entity))
380                                    ? create_concat_name (gnat_entity, 0)
381                                    : NULL_TREE),
382                                   char_type_node, NULL_TREE, 0, 0, 1, 1,
383                                   0);
384
385       /* Now return the expression giving the desired value.  */
386       gnu_decl
387         = build_binary_op (BIT_AND_EXPR, integer_type_node,
388                            convert (integer_type_node,
389                                     build_unary_op (ADDR_EXPR, NULL_TREE,
390                                                     gnu_decl)),
391                            build_unary_op (NEGATE_EXPR, integer_type_node,
392                                            build_int_2 (7, 0)));
393
394       save_gnu_tree (gnat_entity, gnu_decl, 1);
395       saved = 1;
396       break;
397
398     case E_Discriminant:
399     case E_Component:
400       {
401         /* The GNAT record where the component was defined. */
402         Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
403
404         /* If the variable is an inherited record component (in the case of
405            extended record types), just return the inherited entity, which
406            must be a FIELD_DECL.  Likewise for discriminants.
407            For discriminants of untagged records which have explicit
408            girder discriminants, return the entity for the corresponding
409            girder discriminant.  Also use Original_Record_Component
410            if the record has a private extension.  */
411
412         if ((Base_Type (gnat_record) == gnat_record
413              || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
414              || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
415             && Present (Original_Record_Component (gnat_entity))
416             && Original_Record_Component (gnat_entity) != gnat_entity)
417           {
418             gnu_decl
419               = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
420                                     gnu_expr, definition);
421             saved = 1;
422             break;
423           }
424
425         /* If the enclosing record has explicit girder discriminants,
426            then it is an untagged record.  If the Corresponding_Discriminant
427            is not empty then this must be a renamed discriminant and its
428            Original_Record_Component must point to the corresponding explicit
429            girder discriminant (i.e., we should have taken the previous
430            branch).  */
431
432         else if (Present (Corresponding_Discriminant (gnat_entity))
433                  && Is_Tagged_Type (gnat_record))
434           {
435             /* A tagged record has no explicit girder discriminants. */
436
437             if (First_Discriminant (gnat_record)
438                 != First_Girder_Discriminant (gnat_record))
439               gigi_abort (119);
440
441             gnu_decl
442               = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
443                                     gnu_expr, definition);
444             saved = 1;
445             break;
446           }
447
448         /* If the enclosing record has explicit girder discriminants,
449            then it is an untagged record. If the Corresponding_Discriminant
450            is not empty then this must be a renamed discriminant and its
451            Original_Record_Component must point to the corresponding explicit
452            girder discriminant (i.e., we should have taken the first
453            branch).  */
454
455         else if (Present (Corresponding_Discriminant (gnat_entity))
456                  && (First_Discriminant (gnat_record)
457                      != First_Girder_Discriminant (gnat_record)))
458           gigi_abort (120);
459
460         /* Otherwise, if we are not defining this and we have no GCC type
461            for the containing record, make one for it.  Then we should
462            have made our own equivalent.  */
463         else if (! definition && ! present_gnu_tree (gnat_record))
464           {
465             /* ??? If this is in a record whose scope is a protected
466                type and we have an Original_Record_Component, use it.
467                This is a workaround for major problems in protected type
468                handling.  */
469             if (Is_Protected_Type (Scope (Scope (gnat_entity)))
470                 && Present (Original_Record_Component (gnat_entity)))
471               { 
472                 gnu_decl
473                   = gnat_to_gnu_entity (Original_Record_Component
474                                         (gnat_entity),
475                                         gnu_expr, definition);
476                 saved = 1;
477                 break;
478               }
479
480             gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
481             gnu_decl = get_gnu_tree (gnat_entity);
482             saved = 1;
483             break;
484           }
485
486         /* Here we have no GCC type and this is a reference rather than a
487            definition. This should never happen. Most likely the cause is a
488            reference before declaration in the gnat tree for gnat_entity.  */
489         else
490           gigi_abort (103);
491       }
492
493     case E_Loop_Parameter:
494     case E_Out_Parameter:
495     case E_Variable:
496
497       /* Simple variables, loop variables, OUT parameters, and exceptions.  */
498     object:
499       {
500         int used_by_ref = 0;
501         int const_flag
502           = ((kind == E_Constant || kind == E_Variable)
503              && ! Is_Statically_Allocated (gnat_entity)
504              && Is_True_Constant (gnat_entity)
505              && (((Nkind (Declaration_Node (gnat_entity))
506                    == N_Object_Declaration)
507                   && Present (Expression (Declaration_Node (gnat_entity))))
508                  || Present (Renamed_Object (gnat_entity))));
509         int inner_const_flag = const_flag;
510         int static_p = Is_Statically_Allocated (gnat_entity);
511         tree gnu_ext_name = NULL_TREE;
512
513         if (Present (Renamed_Object (gnat_entity)) && ! definition)
514           {
515             if (kind == E_Exception)
516               gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
517                                              NULL_TREE, 0);
518             else
519               gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
520           }
521
522         /* Get the type after elaborating the renamed object.  */
523         gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
524
525         /* If this is a loop variable, its type should be the base type.
526            This is because the code for processing a loop determines whether
527            a normal loop end test can be done by comparing the bounds of the
528            loop against those of the base type, which is presumed to be the
529            size used for computation.  But this is not correct when the size
530            of the subtype is smaller than the type.  */
531         if (kind == E_Loop_Parameter)
532           gnu_type = get_base_type (gnu_type);
533
534         /* Reject non-renamed objects whose types are unconstrained arrays or
535            any object whose type is a dummy type or VOID_TYPE. */
536
537         if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
538              && No (Renamed_Object (gnat_entity)))
539             || TYPE_IS_DUMMY_P (gnu_type)
540             || TREE_CODE (gnu_type) == VOID_TYPE)
541           {
542             if (type_annotate_only)
543               return error_mark_node;
544             else
545               gigi_abort (104);
546           }
547
548         /* If we are defining the object, see if it has a Size value and
549            validate it if so.  Then get the new type, if any.  */
550         if (definition)
551           gnu_size = validate_size (Esize (gnat_entity), gnu_type,
552                                     gnat_entity, VAR_DECL, 0,
553                                     Has_Size_Clause (gnat_entity));
554
555         if (gnu_size != 0)
556           {
557             gnu_type
558               = make_type_from_size (gnu_type, gnu_size,
559                                      Has_Biased_Representation (gnat_entity));
560
561             if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
562               gnu_size = 0;
563           }
564
565         /* If this object has self-referential size, it must be a record with
566            a default value.  We are supposed to allocate an object of the
567            maximum size in this case unless it is a constant with an
568            initializing expression, in which case we can get the size from
569            that.  Note that the resulting size may still be a variable, so
570            this may end up with an indirect allocation.  */
571
572         if (No (Renamed_Object (gnat_entity))
573             && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
574             && contains_placeholder_p (TYPE_SIZE (gnu_type)))
575           {
576             if (gnu_expr != 0 && kind == E_Constant)
577               {
578                 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
579                 if (TREE_CODE (gnu_size) != INTEGER_CST
580                     && contains_placeholder_p (gnu_size))
581                   {
582                     tree gnu_temp = gnu_expr;
583
584                     /* Strip off any conversions in GNU_EXPR since
585                        they can't be changing the size to allocate.  */
586                     while (TREE_CODE (gnu_temp) == UNCHECKED_CONVERT_EXPR)
587                       gnu_temp = TREE_OPERAND (gnu_temp, 0);
588
589                     gnu_size = TYPE_SIZE (TREE_TYPE (gnu_temp));
590                     if (TREE_CODE (gnu_size) != INTEGER_CST
591                         && contains_placeholder_p (gnu_size))
592                       gnu_size = build (WITH_RECORD_EXPR, bitsizetype,
593                                         gnu_size, gnu_temp);
594                   }
595               }
596
597             /* We may have no GNU_EXPR because No_Initialization is
598                set even though there's an Expression.  */
599             else if (kind == E_Constant
600                      && (Nkind (Declaration_Node (gnat_entity))
601                          == N_Object_Declaration)
602                      && Present (Expression (Declaration_Node (gnat_entity))))
603               gnu_size
604                 = TYPE_SIZE (gnat_to_gnu_type
605                              (Etype
606                               (Expression (Declaration_Node (gnat_entity)))));
607             else
608               gnu_size = max_size (TYPE_SIZE (gnu_type), 1);
609           }
610
611         /* If the size is zero bytes, make it one byte since some linkers
612            have trouble with zero-sized objects. But if this will have a 
613            template, that will make it nonzero.  */
614         if (((gnu_size != 0 && integer_zerop (gnu_size))
615              || (TYPE_SIZE (gnu_type) != 0
616                  && integer_zerop (TYPE_SIZE (gnu_type))))
617             && (! Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
618                 || ! Is_Array_Type (Etype (gnat_entity))))
619           gnu_size = bitsize_unit_node;
620
621         /* If an alignment is specified, use it if valid.   Note that
622            exceptions are objects but don't have alignments.  */
623         if (kind != E_Exception && Known_Alignment (gnat_entity))
624           {
625             if (No (Alignment (gnat_entity)))
626               gigi_abort (125);
627
628             align
629               = validate_alignment (Alignment (gnat_entity), gnat_entity,
630                                     TYPE_ALIGN (gnu_type));
631           }
632
633         /* If this is an atomic object with no specified size and alignment,
634            but where the size of the type is a constant, set the alignment to
635            the lowest power of two greater than the size, or to the
636            biggest meaningful alignment, whichever is smaller.  */
637
638         if (Is_Atomic (gnat_entity) && gnu_size == 0 && align == 0
639             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
640           {
641             if (! host_integerp (TYPE_SIZE (gnu_type), 1)
642                 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
643                                           BIGGEST_ALIGNMENT))
644               align = BIGGEST_ALIGNMENT;
645             else
646               align = ((unsigned int) 1
647                        << (floor_log2 (tree_low_cst
648                                        (TYPE_SIZE (gnu_type), 1) - 1)
649                            + 1));
650           }
651
652 #ifdef MINIMUM_ATOMIC_ALIGNMENT
653         /* If the size is a constant and no alignment is specified, force
654            the alignment to be the minimum valid atomic alignment.  The
655            restriction on constant size avoids problems with variable-size
656            temporaries; if the size is variable, there's no issue with
657            atomic access.  Also don't do this for a constant, since it isn't
658            necessary and can interfere with constant replacement.  Finally,
659            do not do it for Out parameters since that creates an
660            size inconsistency with In parameters.  */
661         if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
662             && ! FLOAT_TYPE_P (gnu_type)
663             && ! const_flag && No (Renamed_Object (gnat_entity))
664             && ! imported_p && No (Address_Clause (gnat_entity))
665             && kind != E_Out_Parameter
666             && (gnu_size != 0 ? TREE_CODE (gnu_size) == INTEGER_CST
667                 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
668           align = MINIMUM_ATOMIC_ALIGNMENT;
669 #endif
670
671         /* If the object is set to have atomic components, find the component
672            type and validate it.
673
674            ??? Note that we ignore Has_Volatile_Components on objects; it's
675            not at all clear what to do in that case. */
676
677         if (Has_Atomic_Components (gnat_entity))
678           {
679             tree gnu_inner
680               = (TREE_CODE (gnu_type) == ARRAY_TYPE
681                  ? TREE_TYPE (gnu_type) : gnu_type);
682
683             while (TREE_CODE (gnu_inner) == ARRAY_TYPE
684                    && TYPE_MULTI_ARRAY_P (gnu_inner))
685               gnu_inner = TREE_TYPE (gnu_inner);
686
687             check_ok_for_atomic (gnu_inner, gnat_entity, 1);
688           }
689
690         /* Make a new type with the desired size and alignment, if needed. */
691         gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
692                                    gnat_entity, "PAD", 0, definition, 1);
693
694         /* Make a volatile version of this object's type if we are to
695            make the object volatile.  Note that 13.3(19) says that we
696            should treat other types of objects as volatile as well.  */
697         if ((Is_Volatile (gnat_entity)
698              || Is_Exported (gnat_entity)
699              || Is_Imported (gnat_entity)
700              || Present (Address_Clause (gnat_entity)))
701             && ! TYPE_VOLATILE (gnu_type))
702           gnu_type = build_qualified_type (gnu_type,
703                                            (TYPE_QUALS (gnu_type)
704                                             | TYPE_QUAL_VOLATILE));
705
706         /* If this is an aliased object with an unconstrained nominal subtype,
707            make a type that includes the template.  */
708         if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
709             && Is_Array_Type (Etype (gnat_entity))
710             && ! type_annotate_only)
711         {
712           tree gnu_fat
713             = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
714           tree gnu_temp_type
715             = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
716
717           gnu_type
718             = build_unc_object_type (gnu_temp_type, gnu_type,
719                                      concat_id_with_name (gnu_entity_id,
720                                                           "UNC"));
721         }
722
723
724         /* Convert the expression to the type of the object except in the
725            case where the object's type is unconstrained or the object's type
726            is a padded record whose field is of self-referential size.  In
727            the former case, converting will generate unnecessary evaluations
728            of the CONSTRUCTOR to compute the size and in the latter case, we
729            want to only copy the actual data.  */
730         if (gnu_expr != 0
731             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
732             && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
733                   && contains_placeholder_p (TYPE_SIZE (gnu_type)))
734             && ! (TREE_CODE (gnu_type) == RECORD_TYPE
735                   && TYPE_IS_PADDING_P (gnu_type)
736                   && (contains_placeholder_p
737                       (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
738           gnu_expr = convert (gnu_type, gnu_expr);
739
740         /* See if this is a renaming.  If this is a constant renaming,
741            treat it as a normal variable whose initial value is what
742            is being renamed.  We cannot do this if the type is
743            unconstrained or class-wide.
744
745            Otherwise, if what we are renaming is a reference, we can simply
746            return a stabilized version of that reference, after forcing
747            any SAVE_EXPRs to be evaluated.  But, if this is at global level,
748            we can only do this if we know no SAVE_EXPRs will be made.
749            Otherwise, make this into a constant pointer to the object we are
750            to rename.  */
751
752         if (Present (Renamed_Object (gnat_entity)))
753           {
754             /* If the renamed object had padding, strip off the reference
755                to the inner object and reset our type.  */
756             if (TREE_CODE (gnu_expr) == COMPONENT_REF
757                 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
758                     == RECORD_TYPE)
759                 && (TYPE_IS_PADDING_P
760                     (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
761               {
762                 gnu_expr = TREE_OPERAND (gnu_expr, 0);
763                 gnu_type = TREE_TYPE (gnu_expr);
764               }
765
766             if (const_flag
767                 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
768                 && TYPE_MODE (gnu_type) != BLKmode
769                 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
770                 && !Is_Array_Type (Etype (gnat_entity)))
771               ;
772
773             /* If this is a declaration or reference, we can just use that
774                declaration or reference as this entity.  */
775             else if ((DECL_P (gnu_expr)
776                       || TREE_CODE_CLASS (TREE_CODE (gnu_expr)) == 'r')
777                      && ! Materialize_Entity (gnat_entity)
778                      && (! global_bindings_p ()
779                          || (staticp (gnu_expr)
780                              && ! TREE_SIDE_EFFECTS (gnu_expr))))
781               {
782                 set_lineno (gnat_entity, ! global_bindings_p ());
783                 gnu_decl = gnat_stabilize_reference (gnu_expr, 1);
784                 save_gnu_tree (gnat_entity, gnu_decl, 1);
785                 saved = 1;
786
787                 if (! global_bindings_p ())
788                   expand_expr_stmt (build1 (CONVERT_EXPR, void_type_node,
789                                             gnu_decl));
790                 break;
791               }
792             else
793               {
794                 inner_const_flag = TREE_READONLY (gnu_expr);
795                 const_flag = 1;
796                 gnu_type = build_reference_type (gnu_type);
797                 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
798                 gnu_size = 0;
799                 used_by_ref = 1;
800               }
801           }
802
803         /* If this is an aliased object whose nominal subtype is unconstrained,
804            the object is a record that contains both the template and
805            the object.  If there is an initializer, it will have already
806            been converted to the right type, but we need to create the
807            template if there is no initializer.  */
808         else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
809                  && TYPE_CONTAINS_TEMPLATE_P (gnu_type)
810                  && gnu_expr == 0)
811           gnu_expr
812             = build_constructor
813               (gnu_type,
814                tree_cons
815                (TYPE_FIELDS (gnu_type),
816                 build_template
817                 (TREE_TYPE (TYPE_FIELDS (gnu_type)),
818                  TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))),
819                  NULL_TREE),
820                 NULL_TREE));
821
822         /* If this is a pointer and it does not have an initializing
823            expression, initialize it to NULL.  */
824         if (definition
825             && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
826             && gnu_expr == 0)
827           gnu_expr = integer_zero_node;
828
829         /* If we are defining the object and it has an Address clause we must
830            get the address expression from the saved GCC tree for the
831            object if the object has a Freeze_Node.  Otherwise, we elaborate
832            the address expression here since the front-end has guaranteed
833            in that case that the elaboration has no effects.  Note that
834            only the latter mechanism is currently in use.  */
835         if (definition && Present (Address_Clause (gnat_entity)))
836           {
837             tree gnu_address
838               = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
839                 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
840
841             save_gnu_tree (gnat_entity, NULL_TREE, 0);
842
843             /* Ignore the size.  It's either meaningless or was handled
844                above.  */
845             gnu_size = 0;
846             gnu_type = build_reference_type (gnu_type);
847             gnu_address = convert (gnu_type, gnu_address);
848             used_by_ref = 1;
849             const_flag = ! Is_Public (gnat_entity);
850
851             /* If we don't have an initializing expression for the underlying
852                variable, the initializing expression for the pointer is the
853                specified address.  Otherwise, we have to make a COMPOUND_EXPR
854                to assign both the address and the initial value.  */
855             if (gnu_expr == 0)
856               gnu_expr = gnu_address;
857             else
858               gnu_expr
859                 = build (COMPOUND_EXPR, gnu_type,
860                          build_binary_op
861                          (MODIFY_EXPR, NULL_TREE,
862                           build_unary_op (INDIRECT_REF, NULL_TREE,
863                                           gnu_address),
864                           gnu_expr),
865                          gnu_address);
866           }
867
868         /* If it has an address clause and we are not defining it, mark it
869            as an indirect object.  Likewise for Stdcall objects that are
870            imported.  */
871         if ((! definition && Present (Address_Clause (gnat_entity)))
872             || (Is_Imported (gnat_entity)
873                 && Convention (gnat_entity) == Convention_Stdcall))
874           {
875             gnu_type = build_reference_type (gnu_type);
876             gnu_size = 0;
877             used_by_ref = 1;
878           }
879
880         /* If we are at top level and this object is of variable size,
881            make the actual type a hidden pointer to the real type and
882            make the initializer be a memory allocation and initialization.
883            Likewise for objects we aren't defining (presumed to be
884            external references from other packages), but there we do
885            not set up an initialization.
886
887            If the object's size overflows, make an allocator too, so that
888            Storage_Error gets raised.  Note that we will never free
889            such memory, so we presume it never will get allocated.  */
890
891         if (! allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
892                                   global_bindings_p () || ! definition
893                                   || static_p)
894             || (gnu_size != 0
895                 && ! allocatable_size_p (gnu_size,
896                                          global_bindings_p () || ! definition
897                                          || static_p)))
898           {
899             gnu_type = build_reference_type (gnu_type);
900             gnu_size = 0;
901             used_by_ref = 1;
902             const_flag = 1;
903
904             /* Get the data part of GNU_EXPR in case this was a
905                aliased object whose nominal subtype is unconstrained.
906                In that case the pointer above will be a thin pointer and
907                build_allocator will automatically make the template and
908                constructor already made above.  */
909
910             if (definition)
911               {
912                 tree gnu_alloc_type = TREE_TYPE (gnu_type);
913
914                 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
915                     && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
916                   {
917                     gnu_alloc_type
918                       = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
919                     gnu_expr
920                       = build_component_ref
921                         (gnu_expr, NULL_TREE,
922                          TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))));
923                   }
924
925                 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
926                     && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
927                     && ! Is_Imported (gnat_entity))
928                   post_error ("Storage_Error will be raised at run-time?",
929                               gnat_entity);
930
931                 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
932                                             gnu_type, 0, 0);
933               }
934             else
935               {
936                 gnu_expr = 0;
937                 const_flag = 0;
938               }
939           }
940
941         /* If this object would go into the stack and has an alignment
942            larger than the default largest alignment, make a variable
943            to hold the "aligning type" with a modified initial value,
944            if any, then point to it and make that the value of this
945            variable, which is now indirect.  */
946
947         if (! global_bindings_p () && ! static_p && definition
948             && ! imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
949           {
950             tree gnu_new_type
951               = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
952                                     TYPE_SIZE_UNIT (gnu_type));
953             tree gnu_new_var;
954
955             if (gnu_expr != 0)
956               gnu_expr
957                 = build_constructor (gnu_new_type,
958                                      tree_cons (TYPE_FIELDS (gnu_new_type),
959                                                 gnu_expr, NULL_TREE));
960             set_lineno (gnat_entity, 1);
961             gnu_new_var
962               = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
963                                  NULL_TREE, gnu_new_type, gnu_expr,
964                                  0, 0, 0, 0, 0);
965
966             gnu_type = build_reference_type (gnu_type);
967             gnu_expr
968               = build_unary_op
969                 (ADDR_EXPR, gnu_type,
970                  build_component_ref (gnu_new_var, NULL_TREE,
971                                       TYPE_FIELDS (gnu_new_type)));
972
973             gnu_size = 0;
974             used_by_ref = 1;
975             const_flag = 1;
976           }
977
978         /* Convert the expression to the type of the object except in the
979            case where the object's type is unconstrained or the object's type
980            is a padded record whose field is of self-referential size.  In
981            the former case, converting will generate unnecessary evaluations
982            of the CONSTRUCTOR to compute the size and in the latter case, we
983            want to only copy the actual data.  */
984         if (gnu_expr != 0
985             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
986             && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
987                   && contains_placeholder_p (TYPE_SIZE (gnu_type)))
988             && ! (TREE_CODE (gnu_type) == RECORD_TYPE
989                   && TYPE_IS_PADDING_P (gnu_type)
990                   && (contains_placeholder_p
991                       (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
992           gnu_expr = convert (gnu_type, gnu_expr);
993
994         /* This name is external or there was a name specified, use it.
995            Don't use the Interface_Name if there is an address clause.
996            (see CD30005).  */
997         if ((Present (Interface_Name (gnat_entity))
998              && No (Address_Clause (gnat_entity)))
999             || (Is_Public (gnat_entity)
1000                 && (! Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1001           gnu_ext_name = create_concat_name (gnat_entity, 0);
1002
1003         if (const_flag)
1004           gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1005                                                       | TYPE_QUAL_CONST));
1006
1007         /* If this is constant initialized to a static constant and the
1008            object has an aggregrate type, force it to be statically
1009            allocated. */
1010         if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1011             && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1012             && (AGGREGATE_TYPE_P (gnu_type)
1013                 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1014                       && TYPE_IS_PADDING_P (gnu_type))))
1015           static_p = 1;
1016
1017         set_lineno (gnat_entity, ! global_bindings_p ());
1018         gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1019                                     gnu_expr, const_flag,
1020                                     Is_Public (gnat_entity),
1021                                     imported_p || !definition,
1022                                     static_p, attr_list);
1023
1024         DECL_BY_REF_P (gnu_decl) = used_by_ref;
1025         DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1026
1027         if (definition && DECL_SIZE (gnu_decl) != 0
1028             && gnu_block_stack != 0
1029             && TREE_VALUE (gnu_block_stack) != 0
1030             && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1031                 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1032                     && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1033                                              STACK_CHECK_MAX_VAR_SIZE))))
1034           update_setjmp_buf (TREE_VALUE (gnu_block_stack));
1035
1036         /* If this is a public constant or we're not optimizing and we're not
1037            making a VAR_DECL for it, make one just for export or debugger
1038            use.  Likewise if the address is taken or if the object or type is
1039            aliased.  */
1040         if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1041             && (Is_Public (gnat_entity)
1042                 || optimize == 0
1043                 || Address_Taken (gnat_entity)
1044                 || Is_Aliased (gnat_entity)
1045                 || Is_Aliased (Etype (gnat_entity))))
1046           DECL_CONST_CORRESPONDING_VAR (gnu_decl)
1047             = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1048                                gnu_expr, 0, Is_Public (gnat_entity), 0,
1049                                static_p, 0);
1050
1051         if (Is_Atomic (gnat_entity))
1052           check_ok_for_atomic (gnu_decl, gnat_entity, 0);
1053
1054         /* If this is declared in a block that contains an block with an
1055            exception handler, we must force this variable in memory to
1056            suppress an invalid optimization.  */
1057         if (Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1058           {
1059             mark_addressable (gnu_decl);
1060             flush_addressof (gnu_decl);
1061           }
1062
1063         /* Back-annotate the Alignment of the object if not already in the
1064            tree.  Likewise for Esize if the object is of a constant size.  */
1065         if (Unknown_Alignment (gnat_entity))
1066           Set_Alignment (gnat_entity,
1067                          UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1068
1069         if (Unknown_Esize (gnat_entity)
1070             && DECL_SIZE (gnu_decl) != 0)
1071           {
1072             tree gnu_back_size = DECL_SIZE (gnu_decl);
1073
1074             if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1075                 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1076               gnu_back_size
1077                 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1078                                         (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1079
1080             Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1081           }
1082       }
1083       break;
1084
1085     case E_Void:
1086       /* Return a TYPE_DECL for "void" that we previously made.  */
1087       gnu_decl = void_type_decl_node;
1088       break;
1089
1090     case E_Enumeration_Type:
1091       /* A special case, for the types Character and Wide_Character in
1092          Standard, we do not list all the literals. So if the literals
1093          are not specified, make this an unsigned type.  */
1094       if (No (First_Literal (gnat_entity)))
1095         {
1096           gnu_type = make_unsigned_type (esize);
1097           break;
1098         }
1099
1100       /* Normal case of non-character type, or non-Standard character type */
1101       {
1102         /* Here we have a list of enumeral constants in First_Literal.
1103            We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1104            the list to be places into TYPE_FIELDS.  Each node in the list
1105            is a TREE_LIST node whose TREE_VALUE is the literal name
1106            and whose TREE_PURPOSE is the value of the literal.
1107
1108            Esize contains the number of bits needed to represent the enumeral
1109            type, Type_Low_Bound also points to the first literal and
1110            Type_High_Bound points to the last literal.  */
1111
1112         Entity_Id gnat_literal;
1113         tree gnu_literal_list = NULL_TREE;
1114
1115         if (Is_Unsigned_Type (gnat_entity))
1116           gnu_type = make_unsigned_type (esize);
1117         else
1118           gnu_type = make_signed_type (esize);
1119
1120         TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1121
1122         for (gnat_literal = First_Literal (gnat_entity);
1123              Present (gnat_literal);
1124              gnat_literal = Next_Literal (gnat_literal))
1125           {
1126             tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1127                                         gnu_type);
1128             tree gnu_literal
1129               = create_var_decl (get_entity_name (gnat_literal),
1130                                  0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1131
1132             save_gnu_tree (gnat_literal, gnu_literal, 0);
1133             gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1134                                           gnu_value, gnu_literal_list);
1135           }
1136
1137         TYPE_FIELDS (gnu_type) = nreverse (gnu_literal_list);
1138
1139         /* Note that the bounds are updated at the end of this function
1140            because to avoid an infinite recursion when we get the bounds of
1141            this type, since those bounds are objects of this type.    */
1142       }
1143       break;
1144
1145     case E_Signed_Integer_Type:
1146     case E_Ordinary_Fixed_Point_Type:
1147     case E_Decimal_Fixed_Point_Type:
1148       /* For integer types, just make a signed type the appropriate number
1149          of bits.  */
1150       gnu_type = make_signed_type (esize);
1151       break;
1152
1153     case E_Modular_Integer_Type:
1154       /* For modular types, make the unsigned type of the proper number of
1155          bits and then set up the modulus, if required.  */
1156       {
1157         enum machine_mode mode;
1158         tree gnu_modulus;
1159         tree gnu_high = 0;
1160
1161         if (Is_Packed_Array_Type (gnat_entity))
1162           esize = UI_To_Int (RM_Size (gnat_entity));
1163
1164         /* Find the smallest mode at least ESIZE bits wide and make a class
1165            using that mode.  */
1166
1167         for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1168              GET_MODE_BITSIZE (mode) < esize;
1169              mode = GET_MODE_WIDER_MODE (mode))
1170           ;
1171
1172         gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1173         TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1174           = Is_Packed_Array_Type (gnat_entity);
1175
1176         /* Get the modulus in this type.  If it overflows, assume it is because
1177            it is equal to 2**Esize.  Note that there is no overflow checking
1178            done on unsigned type, so we detect the overflow by looking for
1179            a modulus of zero, which is otherwise invalid.  */
1180         gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1181
1182         if (! integer_zerop (gnu_modulus))
1183           {
1184             TYPE_MODULAR_P (gnu_type) = 1;
1185             TYPE_MODULUS (gnu_type) = gnu_modulus;
1186             gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1187                                     convert (gnu_type, integer_one_node)));
1188           }
1189
1190         /* If we have to set TYPE_PRECISION different from its natural value,
1191            make a subtype to do do.  Likewise if there is a modulus and
1192            it is not one greater than TYPE_MAX_VALUE.  */
1193         if (TYPE_PRECISION (gnu_type) != esize
1194             || (TYPE_MODULAR_P (gnu_type)
1195                 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1196           {
1197             tree gnu_subtype = make_node (INTEGER_TYPE);
1198
1199             TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1200             TREE_TYPE (gnu_subtype) = gnu_type;
1201             TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1202             TYPE_MAX_VALUE (gnu_subtype)
1203               = TYPE_MODULAR_P (gnu_type)
1204                 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1205             TYPE_PRECISION (gnu_subtype) = esize;
1206             TREE_UNSIGNED (gnu_subtype) = 1;
1207             TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1208             TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1209               = Is_Packed_Array_Type (gnat_entity);
1210             layout_type (gnu_subtype);
1211
1212             gnu_type = gnu_subtype;
1213           }
1214       }
1215       break;
1216
1217     case E_Signed_Integer_Subtype:
1218     case E_Enumeration_Subtype:
1219     case E_Modular_Integer_Subtype:
1220     case E_Ordinary_Fixed_Point_Subtype:
1221     case E_Decimal_Fixed_Point_Subtype:
1222
1223       /* For integral subtypes, we make a new INTEGER_TYPE.  Note
1224          that we do not want to call build_range_type since we would
1225          like each subtype node to be distinct.  This will be important
1226          when memory aliasing is implemented.
1227
1228          The TREE_TYPE field of the INTEGER_TYPE we make points to the
1229          parent type; this fact is used by the arithmetic conversion
1230          functions.
1231
1232          We elaborate the Ancestor_Subtype if it is not in the current
1233          unit and one of our bounds is non-static.  We do this to ensure
1234          consistent naming in the case where several subtypes share the same
1235          bounds by always elaborating the first such subtype first, thus
1236          using its name. */
1237
1238       if (definition == 0
1239           && Present (Ancestor_Subtype (gnat_entity))
1240           && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1241           && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1242               || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1243         gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1244                             gnu_expr, definition);
1245
1246       gnu_type = make_node (INTEGER_TYPE);
1247       if (Is_Packed_Array_Type (gnat_entity))
1248         {
1249
1250           esize = UI_To_Int (RM_Size (gnat_entity));
1251           TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1252         }
1253
1254       TYPE_PRECISION (gnu_type) = esize;
1255       TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1256
1257       TYPE_MIN_VALUE (gnu_type)
1258         = convert (TREE_TYPE (gnu_type),
1259                    elaborate_expression (Type_Low_Bound (gnat_entity),
1260                                          gnat_entity,
1261                                          get_identifier ("L"), definition, 1,
1262                                          Needs_Debug_Info (gnat_entity)));
1263
1264       TYPE_MAX_VALUE (gnu_type)
1265         = convert (TREE_TYPE (gnu_type),
1266                    elaborate_expression (Type_High_Bound (gnat_entity),
1267                                          gnat_entity,
1268                                          get_identifier ("U"), definition, 1,
1269                                          Needs_Debug_Info (gnat_entity)));
1270
1271       /* One of the above calls might have caused us to be elaborated,
1272          so don't blow up if so.  */
1273       if (present_gnu_tree (gnat_entity))
1274         {
1275           maybe_present = 1;
1276           break;
1277         }
1278
1279       TYPE_BIASED_REPRESENTATION_P (gnu_type)
1280         = Has_Biased_Representation (gnat_entity);
1281
1282      /* This should be an unsigned type if the lower bound is constant
1283          and non-negative or if the base type is unsigned; a signed type
1284          otherwise.    */
1285       TREE_UNSIGNED (gnu_type)
1286         = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1287            || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1288                && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1289            || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1290            || Is_Unsigned_Type (gnat_entity));
1291
1292       layout_type (gnu_type);
1293
1294       if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1295         {
1296           tree gnu_field_type = gnu_type;
1297           tree gnu_field;
1298
1299           TYPE_RM_SIZE_INT (gnu_field_type)
1300             = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1301           gnu_type = make_node (RECORD_TYPE);
1302           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1303           TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1304           TYPE_PACKED (gnu_type) = 1;
1305           gnu_field = create_field_decl (get_identifier ("OBJECT"),
1306                                          gnu_field_type, gnu_type, 1, 0, 0, 1),
1307           finish_record_type (gnu_type, gnu_field, 0, 0);
1308           TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1309           TYPE_ADA_SIZE (gnu_type) = bitsize_int (esize);
1310         }
1311
1312       break;
1313
1314     case E_Floating_Point_Type:
1315       /* If this is a VAX floating-point type, use an integer of the proper
1316          size.  All the operations will be handled with ASM statements.  */
1317       if (Vax_Float (gnat_entity))
1318         {
1319           gnu_type = make_signed_type (esize);
1320           TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1321           TYPE_DIGITS_VALUE (gnu_type)
1322             = UI_To_Int (Digits_Value (gnat_entity));
1323           break;
1324         }
1325
1326       /* The type of the Low and High bounds can be our type if this is
1327          a type from Standard, so set them at the end of the function.  */
1328       gnu_type = make_node (REAL_TYPE);
1329       TYPE_PRECISION (gnu_type) = esize;
1330       layout_type (gnu_type);
1331       break;
1332
1333     case E_Floating_Point_Subtype:
1334       if (Vax_Float (gnat_entity))
1335         {
1336           gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1337           break;
1338         }
1339
1340       {
1341         enum machine_mode mode;
1342
1343         if (definition == 0
1344             && Present (Ancestor_Subtype (gnat_entity))
1345             && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1346             && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1347                 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1348           gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1349                               gnu_expr, definition);
1350
1351         for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1352              (GET_MODE_WIDER_MODE (mode) != VOIDmode
1353               && GET_MODE_BITSIZE (GET_MODE_WIDER_MODE (mode)) <= esize);
1354              mode = GET_MODE_WIDER_MODE (mode))
1355           ;
1356
1357         gnu_type = make_node (REAL_TYPE);
1358         TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1359         TYPE_PRECISION (gnu_type) = GET_MODE_BITSIZE (mode);
1360
1361         TYPE_MIN_VALUE (gnu_type)
1362           = convert (TREE_TYPE (gnu_type),
1363                      elaborate_expression (Type_Low_Bound (gnat_entity),
1364                                            gnat_entity, get_identifier ("L"),
1365                                            definition, 1,
1366                                            Needs_Debug_Info (gnat_entity)));
1367
1368         TYPE_MAX_VALUE (gnu_type)
1369           = convert (TREE_TYPE (gnu_type),
1370                      elaborate_expression (Type_High_Bound (gnat_entity),
1371                                            gnat_entity, get_identifier ("U"),
1372                                            definition, 1,
1373                                            Needs_Debug_Info (gnat_entity)));
1374
1375         /* One of the above calls might have caused us to be elaborated,
1376            so don't blow up if so.  */
1377         if (present_gnu_tree (gnat_entity))
1378           {
1379             maybe_present = 1;
1380             break;
1381           }
1382
1383         layout_type (gnu_type);
1384       }
1385     break;
1386
1387       /* Array and String Types and Subtypes
1388
1389          Unconstrained array types are represented by E_Array_Type and
1390          constrained array types are represented by E_Array_Subtype.  There
1391          are no actual objects of an unconstrained array type; all we have
1392          are pointers to that type.
1393
1394          The following fields are defined on array types and subtypes:
1395
1396                 Component_Type     Component type of the array.
1397                 Number_Dimensions  Number of dimensions (an int).
1398                 First_Index        Type of first index.  */
1399
1400     case E_String_Type:
1401     case E_Array_Type:
1402       {
1403         tree gnu_template_fields = NULL_TREE;
1404         tree gnu_template_type = make_node (RECORD_TYPE);
1405         tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1406         tree gnu_fat_type = make_node (RECORD_TYPE);
1407         int ndim = Number_Dimensions (gnat_entity);
1408         int firstdim
1409           = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1410         int nextdim
1411           = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1412         tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1413         tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1414         tree gnu_comp_size = 0;
1415         tree gnu_max_size = size_one_node;
1416         tree gnu_max_size_unit;
1417         int index;
1418         Entity_Id gnat_ind_subtype;
1419         Entity_Id gnat_ind_base_subtype;
1420         tree gnu_template_reference;
1421         tree tem;
1422
1423         TYPE_NAME (gnu_template_type)
1424           = create_concat_name (gnat_entity, "XUB");
1425         TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1426         TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1427         TREE_READONLY (gnu_template_type) = 1;
1428
1429         /* Make a node for the array.  If we are not defining the array
1430            suppress expanding incomplete types and save the node as the type
1431            for GNAT_ENTITY.  */
1432         gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1433         if (! definition)
1434           {
1435             defer_incomplete_level++;
1436             this_deferred = this_made_decl = 1;
1437             gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1438                                          ! Comes_From_Source (gnat_entity),
1439                                          debug_info_p);
1440             save_gnu_tree (gnat_entity, gnu_decl, 0);
1441             saved = 1;
1442           }
1443
1444         /* Build the fat pointer type.  Use a "void *" object instead of
1445            a pointer to the array type since we don't have the array type
1446            yet (it will reference the fat pointer via the bounds).  */
1447         tem = chainon (chainon (NULL_TREE,
1448                                 create_field_decl (get_identifier ("P_ARRAY"),
1449                                                    ptr_void_type_node,
1450                                                    gnu_fat_type, 0, 0, 0, 0)),
1451                        create_field_decl (get_identifier ("P_BOUNDS"),
1452                                           gnu_ptr_template,
1453                                           gnu_fat_type, 0, 0, 0, 0));
1454
1455         /* Make sure we can put this into a register.  */
1456         TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1457         finish_record_type (gnu_fat_type, tem, 0, 1);
1458
1459         /* Build a reference to the template from a PLACEHOLDER_EXPR that
1460            is the fat pointer.  This will be used to access the individual
1461            fields once we build them.  */
1462         tem = build (COMPONENT_REF, gnu_ptr_template,
1463                      build (PLACEHOLDER_EXPR, gnu_fat_type),
1464                      TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1465         gnu_template_reference
1466           = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1467         TREE_READONLY (gnu_template_reference) = 1;
1468
1469         /* Now create the GCC type for each index and add the fields for
1470            that index to the template.  */
1471         for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1472              gnat_ind_base_subtype
1473                = First_Index (Implementation_Base_Type (gnat_entity));
1474              index < ndim && index >= 0;
1475              index += nextdim,
1476              gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1477              gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1478           {
1479             char field_name[10];
1480             tree gnu_ind_subtype
1481               = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1482             tree gnu_base_subtype
1483               = get_unpadded_type (Etype (gnat_ind_base_subtype));
1484             tree gnu_base_min
1485               = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1486             tree gnu_base_max
1487               = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1488             tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1489
1490             /* Make the FIELD_DECLs for the minimum and maximum of this
1491                type and then make extractions of that field from the
1492                template.  */
1493             set_lineno (gnat_entity, 0);
1494             sprintf (field_name, "LB%d", index);
1495             gnu_min_field = create_field_decl (get_identifier (field_name),
1496                                                gnu_ind_subtype,
1497                                                gnu_template_type, 0, 0, 0, 0);
1498             field_name[0] = 'U';
1499             gnu_max_field = create_field_decl (get_identifier (field_name),
1500                                                gnu_ind_subtype,
1501                                                gnu_template_type, 0, 0, 0, 0);
1502
1503             gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1504
1505             /* We can't use build_component_ref here since the template
1506                type isn't complete yet.  */
1507             gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1508                              gnu_template_reference, gnu_min_field);
1509             gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1510                              gnu_template_reference, gnu_max_field);
1511             TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1512
1513             /* Make a range type with the new ranges, but using
1514                the Ada subtype.  Then we convert to sizetype.  */
1515             gnu_index_types[index]
1516               = create_index_type (convert (sizetype, gnu_min),
1517                                    convert (sizetype, gnu_max),
1518                                    build_range_type (gnu_ind_subtype,
1519                                                      gnu_min, gnu_max));
1520             /* Update the maximum size of the array, in elements. */
1521             gnu_max_size
1522               = size_binop (MULT_EXPR, gnu_max_size,
1523                             size_binop (PLUS_EXPR, size_one_node,
1524                                         size_binop (MINUS_EXPR, gnu_base_max,
1525                                                     gnu_base_min)));
1526
1527
1528             TYPE_NAME (gnu_index_types[index])
1529               = create_concat_name (gnat_entity, field_name);
1530           }
1531
1532         for (index = 0; index < ndim; index++)
1533           gnu_template_fields
1534             = chainon (gnu_template_fields, gnu_temp_fields[index]);
1535
1536         /* Install all the fields into the template.  */
1537         finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1538         TREE_READONLY (gnu_template_type) = 1;
1539
1540         /* Now make the array of arrays and update the pointer to the array
1541            in the fat pointer.  Note that it is the first field.  */
1542
1543         tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1544
1545         /* Get and validate any specified Component_Size, but if Packed,
1546            ignore it since the front end will have taken care of it.  Also,
1547            allow sizes not a multiple of Storage_Unit if packed.  */
1548         gnu_comp_size
1549           = validate_size (Component_Size (gnat_entity), tem,
1550                            gnat_entity,
1551                            (Is_Bit_Packed_Array (gnat_entity)
1552                             ? TYPE_DECL : VAR_DECL), 1,
1553                            Has_Component_Size_Clause (gnat_entity));
1554
1555         if (Has_Atomic_Components (gnat_entity))
1556           check_ok_for_atomic (tem, gnat_entity, 1);
1557
1558         /* If the component type is a RECORD_TYPE that has a self-referential
1559            size, use the maxium size.  */
1560         if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
1561             && TREE_CODE (TYPE_SIZE (tem)) != INTEGER_CST
1562             && contains_placeholder_p (TYPE_SIZE (tem)))
1563           gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1564
1565         if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1566           {
1567             tem = make_type_from_size (tem, gnu_comp_size, 0);
1568             tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1569                                   "C_PAD", 0, definition, 1);
1570           }
1571
1572         if (Has_Volatile_Components (gnat_entity))
1573           tem = build_qualified_type (tem,
1574                                       TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1575
1576         /* If Component_Size is not already specified, annotate it with the
1577            size of the component.  */
1578         if (Unknown_Component_Size (gnat_entity))
1579           Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1580
1581         gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1582                                         size_binop (MULT_EXPR, gnu_max_size,
1583                                                     TYPE_SIZE_UNIT (tem)));
1584         gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1585                                    size_binop (MULT_EXPR,
1586                                                convert (bitsizetype,
1587                                                         gnu_max_size),
1588                                                TYPE_SIZE (tem)));
1589
1590         for (index = ndim - 1; index >= 0; index--)
1591           {
1592             tem = build_array_type (tem, gnu_index_types[index]);
1593             TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1594             TYPE_NONALIASED_COMPONENT (tem)
1595               = ! Has_Aliased_Components (gnat_entity);
1596           }
1597
1598         /* If an alignment is specified, use it if valid.  But ignore it for
1599            types that represent the unpacked base type for packed arrays.  */
1600         if (No (Packed_Array_Type (gnat_entity))
1601             && Known_Alignment (gnat_entity))
1602           {
1603             if (No (Alignment (gnat_entity)))
1604               gigi_abort (124);
1605
1606             TYPE_ALIGN (tem)
1607               = validate_alignment (Alignment (gnat_entity), gnat_entity,
1608                                     TYPE_ALIGN (tem));
1609           }
1610
1611         TYPE_CONVENTION_FORTRAN_P (tem)
1612           = (Convention (gnat_entity) == Convention_Fortran);
1613         TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1614
1615         /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1616            corresponding fat pointer.  */
1617         TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1618           = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1619         TYPE_MODE (gnu_type) = BLKmode;
1620         TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1621         TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type) = gnu_type;
1622
1623         /* If the maximum size doesn't overflow, use it.  */
1624         if (TREE_CODE (gnu_max_size) == INTEGER_CST
1625             && ! TREE_OVERFLOW (gnu_max_size))
1626           {
1627             TYPE_SIZE (tem)
1628               = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1629             TYPE_SIZE_UNIT (tem)
1630               = size_binop (MIN_EXPR, gnu_max_size_unit,
1631                             TYPE_SIZE_UNIT (tem));
1632           }
1633
1634         create_type_decl (create_concat_name (gnat_entity, "XUA"),
1635                           tem, 0, ! Comes_From_Source (gnat_entity),
1636                           debug_info_p);
1637         rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1638
1639
1640         /* Create a record type for the object and its template and
1641            set the template at a negative offset.  */
1642         tem = build_unc_object_type (gnu_template_type, tem,
1643                                      create_concat_name (gnat_entity, "XUT"));
1644         DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1645           = size_binop (MINUS_EXPR, size_zero_node,
1646                         byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1647         DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1648         DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1649           = bitsize_zero_node;
1650         TYPE_UNCONSTRAINED_ARRAY (tem) = gnu_type;
1651         TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1652
1653         /* Give the thin pointer type a name.  */
1654         create_type_decl (create_concat_name (gnat_entity, "XUX"),
1655                           build_pointer_type (tem), 0,
1656                           ! Comes_From_Source (gnat_entity), debug_info_p);
1657       }
1658       break;
1659
1660     case E_String_Subtype:
1661     case E_Array_Subtype:
1662
1663       /* This is the actual data type for array variables.  Multidimensional
1664          arrays are implemented in the gnu tree as arrays of arrays.  Note
1665          that for the moment arrays which have sparse enumeration subtypes as
1666          index components create sparse arrays, which is obviously space
1667          inefficient but so much easier to code for now.
1668
1669          Also note that the subtype never refers to the unconstrained
1670          array type, which is somewhat at variance with Ada semantics.
1671
1672          First check to see if this is simply a renaming of the array
1673          type.  If so, the result is the array type.  */
1674
1675       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1676       if (! Is_Constrained (gnat_entity))
1677         break;
1678       else
1679         {
1680           int index;
1681           int array_dim = Number_Dimensions (gnat_entity);
1682           int first_dim
1683             = ((Convention (gnat_entity) == Convention_Fortran)
1684                ? array_dim - 1 : 0);
1685           int next_dim
1686             = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1687           Entity_Id gnat_ind_subtype;
1688           Entity_Id gnat_ind_base_subtype;
1689           tree gnu_base_type = gnu_type;
1690           tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1691           tree gnu_comp_size = 0;
1692           tree gnu_max_size = size_one_node;
1693           tree gnu_max_size_unit;
1694           int need_index_type_struct = 0;
1695           int max_overflow = 0;
1696
1697           /* First create the gnu types for each index.  Create types for
1698              debugging information to point to the index types if the
1699              are not integer types, have variable bounds, or are
1700              wider than sizetype.  */
1701
1702           for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1703                gnat_ind_base_subtype
1704                  = First_Index (Implementation_Base_Type (gnat_entity));
1705                index < array_dim && index >= 0;
1706                index += next_dim,
1707                gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1708                gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1709             {
1710               tree gnu_index_subtype
1711                 = get_unpadded_type (Etype (gnat_ind_subtype));
1712               tree gnu_min
1713                 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1714               tree gnu_max
1715                 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1716               tree gnu_base_subtype
1717                 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1718               tree gnu_base_min
1719                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1720               tree gnu_base_max
1721                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1722               tree gnu_base_type = get_base_type (gnu_base_subtype);
1723               tree gnu_base_base_min
1724                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1725               tree gnu_base_base_max
1726                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1727               tree gnu_high;
1728               tree gnu_this_max;
1729
1730               /* If the minimum and maximum values both overflow in
1731                  SIZETYPE, but the difference in the original type
1732                  does not overflow in SIZETYPE, ignore the overflow
1733                  indications.  */
1734               if ((TYPE_PRECISION (gnu_index_subtype)
1735                    > TYPE_PRECISION (sizetype))
1736                   && TREE_CODE (gnu_min) == INTEGER_CST
1737                   && TREE_CODE (gnu_max) == INTEGER_CST
1738                   && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1739                   && (! TREE_OVERFLOW
1740                       (fold (build (MINUS_EXPR, gnu_index_subtype,
1741                                     TYPE_MAX_VALUE (gnu_index_subtype),
1742                                     TYPE_MIN_VALUE (gnu_index_subtype))))))
1743                 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1744                   = TREE_CONSTANT_OVERFLOW (gnu_min)
1745                   = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1746
1747               /* Similarly, if the range is null, use bounds of 1..0 for
1748                  the sizetype bounds.  */
1749               else if ((TYPE_PRECISION (gnu_index_subtype)
1750                         > TYPE_PRECISION (sizetype))
1751                        && TREE_CODE (gnu_min) == INTEGER_CST
1752                        && TREE_CODE (gnu_max) == INTEGER_CST
1753                        && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1754                        && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1755                                            TYPE_MIN_VALUE (gnu_index_subtype)))
1756                 gnu_min = size_one_node, gnu_max = size_zero_node;
1757
1758               /* Now compute the size of this bound.  We need to provide
1759                  GCC with an upper bound to use but have to deal with the
1760                  "superflat" case.  There are three ways to do this.  If we
1761                  can prove that the array can never be superflat, we can
1762                  just use the high bound of the index subtype.  If we can
1763                  prove that the low bound minus one can't overflow, we
1764                  can do this as MAX (hb, lb - 1).  Otherwise, we have to use
1765                  the expression hb >= lb ? hb : lb - 1.  */
1766               gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1767
1768               /* See if the base array type is already flat.  If it is, we
1769                  are probably compiling an ACVC test, but it will cause the
1770                  code below to malfunction if we don't handle it specially.  */
1771               if (TREE_CODE (gnu_base_min) == INTEGER_CST
1772                   && TREE_CODE (gnu_base_max) == INTEGER_CST
1773                   && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1774                   && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1775                   && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1776                 gnu_high = size_zero_node, gnu_min = size_one_node;
1777
1778               /* If gnu_high is now an integer which overflowed, the array
1779                  cannot be superflat.  */
1780               else if (TREE_CODE (gnu_high) == INTEGER_CST
1781                        && TREE_OVERFLOW (gnu_high))
1782                 gnu_high = gnu_max;
1783               else if (TREE_UNSIGNED (gnu_base_subtype)
1784                        || TREE_CODE (gnu_high) == INTEGER_CST)
1785                 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1786               else
1787                 gnu_high
1788                   = build_cond_expr
1789                     (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1790                                                 gnu_max, gnu_min),
1791                      gnu_max, gnu_high);
1792
1793               gnu_index_type[index]
1794                 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1795
1796               /* Also compute the maximum size of the array.  Here we
1797                  see if any constraint on the index type of the base type
1798                  can be used in the case of self-referential bound on
1799                  the index type of the subtype.  We look for a non-"infinite"
1800                  and non-self-referential bound from any type involved and
1801                  handle each bound separately.  */
1802
1803               if ((TREE_CODE (gnu_min) == INTEGER_CST
1804                    && ! TREE_OVERFLOW (gnu_min)
1805                    && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
1806                   || (TREE_CODE (gnu_min) != INTEGER_CST
1807                       && ! contains_placeholder_p (gnu_min)))
1808                 gnu_base_min = gnu_min;
1809
1810               if ((TREE_CODE (gnu_max) == INTEGER_CST
1811                    && ! TREE_OVERFLOW (gnu_max)
1812                    && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
1813                   || (TREE_CODE (gnu_max) != INTEGER_CST
1814                       && ! contains_placeholder_p (gnu_max)))
1815                 gnu_base_max = gnu_max;
1816
1817               if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1818                    && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1819                   || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1820                   || (TREE_CODE (gnu_base_max) == INTEGER_CST
1821                       && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1822                   || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1823                 max_overflow = 1;
1824
1825               gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1826               gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1827
1828               gnu_this_max
1829                 = size_binop (MAX_EXPR,
1830                               size_binop (PLUS_EXPR, size_one_node,
1831                                           size_binop (MINUS_EXPR, gnu_base_max,
1832                                                       gnu_base_min)),
1833                               size_zero_node);
1834
1835               if (TREE_CODE (gnu_this_max) == INTEGER_CST
1836                   && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1837                 max_overflow = 1;
1838
1839               gnu_max_size
1840                 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1841
1842               if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1843                   || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1844                       != INTEGER_CST)
1845                   || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1846                   || (TREE_TYPE (gnu_index_subtype) != 0
1847                       && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1848                           != INTEGER_TYPE))
1849                   || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1850                   || (TYPE_PRECISION (gnu_index_subtype)
1851                       > TYPE_PRECISION (sizetype)))
1852                 need_index_type_struct = 1;
1853             }
1854
1855           /* Then flatten: create the array of arrays.  */
1856
1857           gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1858
1859           /* One of the above calls might have caused us to be elaborated,
1860              so don't blow up if so.  */
1861           if (present_gnu_tree (gnat_entity))
1862             {
1863               maybe_present = 1;
1864               break;
1865             }
1866
1867           /* Get and validate any specified Component_Size, but if Packed,
1868              ignore it since the front end will have taken care of it.  Also,
1869              allow sizes not a multiple of Storage_Unit if packed.  */
1870           gnu_comp_size
1871             = validate_size (Component_Size (gnat_entity), gnu_type,
1872                              gnat_entity,
1873                              (Is_Bit_Packed_Array (gnat_entity)
1874                               ? TYPE_DECL : VAR_DECL),
1875                              1, Has_Component_Size_Clause (gnat_entity));
1876
1877           /* If the component type is a RECORD_TYPE that has a self-referential
1878              size, use the maxium size.  */
1879           if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
1880               && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1881               && contains_placeholder_p (TYPE_SIZE (gnu_type)))
1882             gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1883
1884           if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1885             {
1886               gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1887               gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1888                                          gnat_entity, "C_PAD", 0,
1889                                          definition, 1);
1890             }
1891
1892           if (Has_Volatile_Components (Base_Type (gnat_entity)))
1893             gnu_type = build_qualified_type (gnu_type,
1894                                              (TYPE_QUALS (gnu_type)
1895                                               | TYPE_QUAL_VOLATILE));
1896
1897           gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1898                                           TYPE_SIZE_UNIT (gnu_type));
1899           gnu_max_size = size_binop (MULT_EXPR,
1900                                      convert (bitsizetype, gnu_max_size),
1901                                      TYPE_SIZE (gnu_type));
1902
1903           /* We don't want any array types shared for two reasons: first,
1904              we want to keep differently-named types distinct; second,
1905              setting TYPE_MULTI_ARRAY_TYPE of one type can clobber
1906              another.  */
1907           debug_no_type_hash = 1;
1908           for (index = array_dim - 1; index >= 0; index --)
1909             {
1910               gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1911               TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1912               TYPE_NONALIASED_COMPONENT (gnu_type)
1913                 = ! Has_Aliased_Components (gnat_entity);
1914             }
1915
1916           /* If we are at file level and this is a multi-dimensional array, we
1917              need to make a variable corresponding to the stride of the
1918              inner dimensions.   */
1919           if (global_bindings_p () && array_dim > 1)
1920             {
1921               tree gnu_str_name = get_identifier ("ST");
1922               tree gnu_arr_type;
1923
1924               for (gnu_arr_type = TREE_TYPE (gnu_type);
1925                    TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1926                    gnu_arr_type = TREE_TYPE (gnu_arr_type),
1927                    gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1928                 {
1929                   TYPE_SIZE (gnu_arr_type)
1930                     = elaborate_expression_1 (gnat_entity, gnat_entity,
1931                                               TYPE_SIZE (gnu_arr_type),
1932                                               gnu_str_name, definition, 0);
1933                   TYPE_SIZE_UNIT (gnu_arr_type)
1934                     = elaborate_expression_1
1935                       (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1936                        concat_id_with_name (gnu_str_name, "U"), definition, 0);
1937                 }
1938             }
1939
1940           /* If we need to write out a record type giving the names of
1941              the bounds, do it now.  */
1942           if (need_index_type_struct && debug_info_p)
1943             {
1944               tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1945               tree gnu_field_list = 0;
1946               tree gnu_field;
1947
1948               TYPE_NAME (gnu_bound_rec_type)
1949                 = create_concat_name (gnat_entity, "XA");
1950
1951               for (index = array_dim - 1; index >= 0; index--)
1952                 {
1953                   tree gnu_type_name
1954                     = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1955
1956                   if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1957                     gnu_type_name = DECL_NAME (gnu_type_name);
1958
1959                   gnu_field = create_field_decl (gnu_type_name,
1960                                                  integer_type_node,
1961                                                  gnu_bound_rec_type,
1962                                                  0, NULL_TREE, NULL_TREE, 0);
1963                   TREE_CHAIN (gnu_field) = gnu_field_list;
1964                   gnu_field_list = gnu_field;
1965                 }
1966
1967               finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
1968             }
1969
1970           debug_no_type_hash = 0;
1971           TYPE_CONVENTION_FORTRAN_P (gnu_type)
1972             = (Convention (gnat_entity) == Convention_Fortran);
1973
1974           /* If our size depends on a placeholder and the maximum size doesn't
1975              overflow, use it.  */
1976           if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1977               && contains_placeholder_p (TYPE_SIZE (gnu_type))
1978               && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
1979                     && TREE_OVERFLOW (gnu_max_size))
1980               && ! max_overflow)
1981             {
1982               TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
1983                                                  TYPE_SIZE (gnu_type));
1984               TYPE_SIZE_UNIT (gnu_type)
1985                 = size_binop (MIN_EXPR, gnu_max_size_unit,
1986                               TYPE_SIZE_UNIT (gnu_type));
1987             }
1988
1989           /* Set our alias set to that of our base type.  This gives all
1990              array subtypes the same alias set.  */
1991           TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
1992           record_component_aliases (gnu_type);
1993         }
1994
1995       /* If this is a packed type, make this type the same as the packed
1996          array type, but do some adjusting in the type first.   */
1997
1998       if (Present (Packed_Array_Type (gnat_entity)))
1999         {
2000           Entity_Id gnat_index;
2001           tree gnu_inner_type;
2002
2003           /* First finish the type we had been making so that we output
2004              debugging information for it  */
2005           gnu_type = build_qualified_type (gnu_type,
2006                                            (TYPE_QUALS (gnu_type)
2007                                             | (TYPE_QUAL_VOLATILE
2008                                                * Is_Volatile (gnat_entity))));
2009           set_lineno (gnat_entity, 0);
2010           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2011                                        ! Comes_From_Source (gnat_entity),
2012                                        debug_info_p);
2013           if (! Comes_From_Source (gnat_entity))
2014             DECL_ARTIFICIAL (gnu_decl) = 1;
2015
2016           /* Save it as our equivalent in case the call below elaborates
2017              this type again.  */
2018           save_gnu_tree (gnat_entity, gnu_decl, 0);
2019
2020           gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2021                                          NULL_TREE, 0);
2022           this_made_decl = 1;
2023           gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2024           save_gnu_tree (gnat_entity, NULL_TREE, 0);
2025
2026           if (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2027               && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2028                   || TYPE_IS_PADDING_P (gnu_inner_type)))
2029             gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2030
2031           /* We need to point the type we just made to our index type so
2032              the actual bounds can be put into a template.  */
2033
2034           if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2035                && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2036               || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2037                   && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2038             {
2039               if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2040                 {
2041                   /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2042                      If it is, we need to make another type.  */
2043                   if (TYPE_MODULAR_P (gnu_inner_type))
2044                     {
2045                       tree gnu_subtype;
2046
2047                       gnu_subtype = make_node (INTEGER_TYPE);
2048
2049                       TREE_TYPE (gnu_subtype) = gnu_inner_type;
2050                       TYPE_MIN_VALUE (gnu_subtype)
2051                         = TYPE_MIN_VALUE (gnu_inner_type);
2052                       TYPE_MAX_VALUE (gnu_subtype)
2053                         = TYPE_MAX_VALUE (gnu_inner_type);
2054                       TYPE_PRECISION (gnu_subtype)
2055                         = TYPE_PRECISION (gnu_inner_type);
2056                       TREE_UNSIGNED (gnu_subtype)
2057                         = TREE_UNSIGNED (gnu_inner_type);
2058                       TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2059                       layout_type (gnu_subtype);
2060
2061                       gnu_inner_type = gnu_subtype;
2062                     }
2063
2064                   TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2065                 }
2066
2067               TYPE_ACTUAL_BOUNDS (gnu_inner_type) = NULL_TREE;
2068
2069               for (gnat_index = First_Index (gnat_entity);
2070                    Present (gnat_index); gnat_index = Next_Index (gnat_index))
2071                 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2072                   = tree_cons (NULL_TREE,
2073                                get_unpadded_type (Etype (gnat_index)),
2074                                TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2075
2076               if (Convention (gnat_entity) != Convention_Fortran)
2077                 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2078                   = nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2079
2080               if (TREE_CODE (gnu_type) == RECORD_TYPE
2081                   && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2082                 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2083             }
2084         }
2085
2086       /* Abort if packed array with no packed array type field set. */
2087       else if (Is_Packed (gnat_entity))
2088         gigi_abort (107);
2089
2090       break;
2091
2092     case E_String_Literal_Subtype:
2093       /* Create the type for a string literal. */
2094       {
2095         Entity_Id gnat_full_type
2096           = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2097              && Present (Full_View (Etype (gnat_entity)))
2098              ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2099         tree gnu_string_type = get_unpadded_type (gnat_full_type);
2100         tree gnu_string_array_type
2101           = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2102         tree gnu_string_index_type
2103           = TREE_TYPE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_string_array_type)));
2104         tree gnu_lower_bound
2105           = convert (gnu_string_index_type,
2106                      gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2107         int length = UI_To_Int (String_Literal_Length (gnat_entity));
2108         tree gnu_length = ssize_int (length - 1);
2109         tree gnu_upper_bound
2110           = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2111                              gnu_lower_bound,
2112                              convert (gnu_string_index_type, gnu_length));
2113         tree gnu_range_type
2114           = build_range_type (gnu_string_index_type,
2115                               gnu_lower_bound, gnu_upper_bound);
2116         tree gnu_index_type
2117           = create_index_type (convert (sizetype,
2118                                         TYPE_MIN_VALUE (gnu_range_type)),
2119                                convert (sizetype,
2120                                         TYPE_MAX_VALUE (gnu_range_type)),
2121                                gnu_range_type);
2122
2123         gnu_type
2124           = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2125                               gnu_index_type);
2126       }
2127       break;
2128
2129     /* Record Types and Subtypes
2130
2131        The following fields are defined on record types:
2132
2133                 Has_Discriminants       True if the record has discriminants
2134                 First_Discriminant      Points to head of list of discriminants
2135                 First_Entity            Points to head of list of fields
2136                 Is_Tagged_Type          True if the record is tagged
2137
2138        Implementation of Ada records and discriminated records:
2139
2140        A record type definition is transformed into the equivalent of a C
2141        struct definition.  The fields that are the discriminants which are
2142        found in the Full_Type_Declaration node and the elements of the
2143        Component_List found in the Record_Type_Definition node.  The
2144        Component_List can be a recursive structure since each Variant of
2145        the Variant_Part of the Component_List has a Component_List.
2146
2147        Processing of a record type definition comprises starting the list of
2148        field declarations here from the discriminants and the calling the
2149        function components_to_record to add the rest of the fields from the
2150        component list and return the gnu type node. The function
2151        components_to_record will call itself recursively as it traverses
2152        the tree.  */
2153
2154     case E_Record_Type:
2155 #if 0
2156       if (Has_Complex_Representation (gnat_entity))
2157         {
2158           gnu_type
2159             = build_complex_type
2160               (get_unpadded_type
2161                (Etype (Defining_Entity
2162                        (First (Component_Items
2163                                (Component_List
2164                                 (Type_Definition
2165                                  (Declaration_Node (gnat_entity)))))))));
2166
2167           /* ??? For now, don't use Complex if the real type is shorter than
2168              a word.  */
2169           if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (gnu_type)))
2170               >= BITS_PER_WORD)
2171             break;
2172         }
2173 #endif
2174
2175       {
2176         Node_Id full_definition = Declaration_Node (gnat_entity);
2177         Node_Id record_definition = Type_Definition (full_definition);
2178         Entity_Id gnat_field;
2179         tree gnu_field;
2180         tree gnu_field_list = NULL_TREE;
2181         tree gnu_get_parent;
2182         int packed = (Is_Packed (gnat_entity) ? 1
2183                       : (Component_Alignment (gnat_entity)
2184                          == Calign_Storage_Unit) ? -1
2185                       : 0);
2186         int has_rep = Has_Specified_Layout (gnat_entity);
2187         int all_rep = has_rep;
2188         int is_extension
2189           = (Is_Tagged_Type (gnat_entity)
2190              && Nkind (record_definition) == N_Derived_Type_Definition);
2191
2192         /* See if all fields have a rep clause.  Stop when we find one
2193            that doesn't.  */
2194         for (gnat_field = First_Entity (gnat_entity);
2195              Present (gnat_field) && all_rep;
2196              gnat_field = Next_Entity (gnat_field))
2197           if ((Ekind (gnat_field) == E_Component
2198                || Ekind (gnat_field) == E_Discriminant)
2199               && No (Component_Clause (gnat_field)))
2200             all_rep = 0;
2201
2202         /* If this is a record extension, go a level further to find the
2203            record definition.  Also, verify we have a Parent_Subtype.  */
2204         if (is_extension)
2205           {
2206             if (! type_annotate_only
2207                 || Present (Record_Extension_Part (record_definition)))
2208               record_definition = Record_Extension_Part (record_definition);
2209
2210             if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2211               gigi_abort (121);
2212           }
2213
2214         /* Make a node for the record.  If we are not defining the record,
2215            suppress expanding incomplete types and save the node as the type
2216            for GNAT_ENTITY.  We use the same RECORD_TYPE as was made
2217            for a dummy type and then show it's no longer a dummy.  */
2218         gnu_type = make_dummy_type (gnat_entity);
2219         TYPE_DUMMY_P (gnu_type) = 0;
2220         if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2221           DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2222
2223         TYPE_ALIGN (gnu_type) = 0;
2224         TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2225
2226         if (! definition)
2227           {
2228             defer_incomplete_level++;
2229             this_deferred = 1;
2230             set_lineno (gnat_entity, 0);
2231             gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2232                                          ! Comes_From_Source (gnat_entity),
2233                                          debug_info_p);
2234             save_gnu_tree (gnat_entity, gnu_decl, 0);
2235             this_made_decl = saved = 1;
2236           }
2237
2238         /* If both a size and rep clause was specified, put the size in
2239            the record type now so that it can get the proper mode.  */
2240         if (has_rep && Known_Esize (gnat_entity))
2241           TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2242
2243         /* Always set the alignment here so that it can be used to
2244            set the mode, if it is making the alignment stricter.  If
2245            it is invalid, it will be checked again below.  If this is to
2246            be Atomic, choose a default alignment of a word.  */
2247
2248         if (Known_Alignment (gnat_entity))
2249           TYPE_ALIGN (gnu_type)
2250             = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2251         else if (Is_Atomic (gnat_entity))
2252           TYPE_ALIGN (gnu_type) = BITS_PER_WORD;
2253
2254         /* If we have a Parent_Subtype, make a field for the parent.  If
2255            this record has rep clauses, force the position to zero.  */
2256         if (Present (Parent_Subtype (gnat_entity)))
2257           {
2258             tree gnu_parent;
2259
2260             /* A major complexity here is that the parent subtype will
2261                reference our discriminants.  But those must reference
2262                the parent component of this record.  So here we will
2263                initialize each of those components to a COMPONENT_REF.
2264                The first operand of that COMPONENT_REF is another
2265                COMPONENT_REF which will be filled in below, once
2266                the parent type can be safely built.  */
2267
2268             gnu_get_parent = build (COMPONENT_REF, void_type_node,
2269                                     build (PLACEHOLDER_EXPR, gnu_type),
2270                                     build_decl (FIELD_DECL, NULL_TREE,
2271                                                 NULL_TREE));
2272
2273             if (Has_Discriminants (gnat_entity))
2274               for (gnat_field = First_Girder_Discriminant (gnat_entity);
2275                    Present (gnat_field);
2276                    gnat_field = Next_Girder_Discriminant (gnat_field))
2277                 if (Present (Corresponding_Discriminant (gnat_field)))
2278                   save_gnu_tree
2279                     (gnat_field,
2280                      build (COMPONENT_REF,
2281                             get_unpadded_type (Etype (gnat_field)),
2282                             gnu_get_parent,
2283                             gnat_to_gnu_entity (Corresponding_Discriminant
2284                                                 (gnat_field),
2285                                                 NULL_TREE, 0)),
2286                      1);
2287
2288             gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2289
2290             gnu_field_list
2291               = create_field_decl (get_identifier
2292                                    (Get_Name_String (Name_uParent)),
2293                                    gnu_parent, gnu_type, 0,
2294                                    has_rep ? TYPE_SIZE (gnu_parent) : 0,
2295                                    has_rep ? bitsize_zero_node : 0, 1);
2296             DECL_INTERNAL_P (gnu_field_list) = 1;
2297
2298             TREE_TYPE (gnu_get_parent) = gnu_parent;
2299             TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2300           }
2301
2302         /* Add the fields for the discriminants into the record.  */
2303         if (! Is_Unchecked_Union (gnat_entity)
2304             && Has_Discriminants (gnat_entity))
2305           for (gnat_field = First_Girder_Discriminant (gnat_entity);
2306                Present (gnat_field);
2307                gnat_field = Next_Girder_Discriminant (gnat_field))
2308             {
2309               /* If this is a record extension and this discriminant
2310                  is the renaming of another discriminant, we've already
2311                  handled the discriminant above.  */
2312               if (Present (Parent_Subtype (gnat_entity))
2313                   && Present (Corresponding_Discriminant (gnat_field)))
2314                 continue;
2315
2316               gnu_field
2317                 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2318
2319               /* Make an expression using a PLACEHOLDER_EXPR from the
2320                  FIELD_DECL node just created and link that with the
2321                  corresponding GNAT defining identifier.  Then add to the
2322                  list of fields.  */
2323               save_gnu_tree (gnat_field,
2324                              build (COMPONENT_REF, TREE_TYPE (gnu_field),
2325                                     build (PLACEHOLDER_EXPR,
2326                                            DECL_CONTEXT (gnu_field)),
2327                                     gnu_field),
2328                              1);
2329
2330               TREE_CHAIN (gnu_field) = gnu_field_list;
2331               gnu_field_list = gnu_field;
2332             }
2333
2334         /* Put the discriminants into the record (backwards), so we can
2335            know the appropriate discriminant to use for the names of the
2336            variants.  */
2337         TYPE_FIELDS (gnu_type) = gnu_field_list;
2338
2339         /* Add the listed fields into the record and finish up.  */
2340         components_to_record (gnu_type, Component_List (record_definition),
2341                               gnu_field_list, packed, definition, 0,
2342                               0, all_rep);
2343
2344         TYPE_DUMMY_P (gnu_type) = 0;
2345         TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2346         TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2347
2348         /* If this is an extension type, reset the tree for any
2349            inherited discriminants.  Also remove the PLACEHOLDER_EXPR
2350            for non-inherited discriminants.  */
2351         if (! Is_Unchecked_Union (gnat_entity)
2352             && Has_Discriminants (gnat_entity))
2353           for (gnat_field = First_Girder_Discriminant (gnat_entity);
2354                Present (gnat_field);
2355                gnat_field = Next_Girder_Discriminant (gnat_field))
2356             {
2357               if (Present (Parent_Subtype (gnat_entity))
2358                   && Present (Corresponding_Discriminant (gnat_field)))
2359                 save_gnu_tree (gnat_field, NULL_TREE, 0);
2360               else
2361                 {
2362                   gnu_field = get_gnu_tree (gnat_field);
2363                   save_gnu_tree (gnat_field, NULL_TREE, 0);
2364                   save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2365                 }
2366             }
2367
2368         /* If it is a tagged record force the type to BLKmode to insure
2369            that these objects will always be placed in memory. Do the
2370            same thing for limited record types. */
2371
2372         if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2373           TYPE_MODE (gnu_type) = BLKmode;
2374
2375         /* Fill in locations of fields.  */
2376         annotate_rep (gnat_entity, gnu_type);
2377
2378         /* If there are any entities in the chain corresponding to
2379            components that we did not elaborate, ensure we elaborate their
2380            types if they are Itypes.  */
2381         for (gnat_temp = First_Entity (gnat_entity);
2382              Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2383           if ((Ekind (gnat_temp) == E_Component
2384                || Ekind (gnat_temp) == E_Discriminant)
2385               && Is_Itype (Etype (gnat_temp))
2386               && ! present_gnu_tree (gnat_temp))
2387             gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2388       }
2389       break;
2390
2391     case E_Class_Wide_Subtype:
2392       /* If an equivalent type is present, that is what we should use.
2393          Otherwise, fall through to handle this like a record subtype
2394          since it may have constraints.  */
2395
2396       if (Present (Equivalent_Type (gnat_entity)))
2397         {
2398           gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2399           maybe_present = 1;
2400           break;
2401         }
2402
2403       /* ... fall through ... */
2404
2405     case E_Record_Subtype:
2406
2407       /* If Cloned_Subtype is Present it means this record subtype has
2408          identical layout to that type or subtype and we should use
2409          that GCC type for this one.  The front end guarantees that
2410          the component list is shared.  */
2411       if (Present (Cloned_Subtype (gnat_entity)))
2412         {
2413           gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2414                                          NULL_TREE, 0);
2415           maybe_present = 1;
2416         }
2417
2418       /* Otherwise, first ensure the base type is elaborated.  Then, if we are
2419          changing the type, make a new type with each field having the
2420          type of the field in the new subtype but having the position
2421          computed by transforming every discriminant reference according
2422          to the constraints.  We don't see any difference between
2423          private and nonprivate type here since derivations from types should
2424          have been deferred until the completion of the private type.  */
2425       else
2426         {
2427           Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2428           tree gnu_base_type;
2429           tree gnu_orig_type;
2430
2431           if (! definition)
2432             defer_incomplete_level++, this_deferred = 1;
2433
2434           /* Get the base type initially for its alignment and sizes.  But
2435              if it is a padded type, we do all the other work with the
2436              unpadded type.  */
2437           gnu_type = gnu_orig_type = gnu_base_type
2438             = gnat_to_gnu_type (gnat_base_type);
2439
2440           if (TREE_CODE (gnu_type) == RECORD_TYPE
2441               && TYPE_IS_PADDING_P (gnu_type))
2442             gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2443
2444           if (present_gnu_tree (gnat_entity))
2445             {
2446               maybe_present = 1;
2447               break;
2448             }
2449
2450           /* When the type has discriminants, and these discriminants
2451              affect the shape of what it built, factor them in.
2452
2453              If we are making a subtype of an Unchecked_Union (must be an
2454              Itype), just return the type.
2455
2456              We can't just use Is_Constrained because private subtypes without
2457              discriminants of full types with discriminants with default
2458              expressions are Is_Constrained but aren't constrained!  */
2459
2460           if (IN (Ekind (gnat_base_type), Record_Kind)
2461               && ! Is_For_Access_Subtype (gnat_entity)
2462               && ! Is_Unchecked_Union (gnat_base_type)
2463               && Is_Constrained (gnat_entity)
2464               && Girder_Constraint (gnat_entity) != No_Elist
2465               && Present (Discriminant_Constraint (gnat_entity)))
2466             {
2467               Entity_Id gnat_field;
2468               Entity_Id gnat_root_type;
2469               tree gnu_field_list = 0;
2470               tree gnu_pos_list
2471                 = compute_field_positions (gnu_orig_type, NULL_TREE,
2472                                            size_zero_node, bitsize_zero_node);
2473               tree gnu_subst_list
2474                 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2475                                      definition);
2476               tree gnu_temp;
2477
2478               /* If this is a derived type, we may be seeing fields from any
2479                  original records, so add those positions and discriminant
2480                  substitutions to our lists.  */
2481               for (gnat_root_type = gnat_base_type;
2482                    Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2483                    gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2484                 {
2485                   gnu_pos_list
2486                     = compute_field_positions
2487                       (gnat_to_gnu_type (Etype (gnat_root_type)),
2488                        gnu_pos_list, size_zero_node, bitsize_zero_node);
2489
2490                   if (Present (Parent_Subtype (gnat_root_type)))
2491                     gnu_subst_list
2492                       = substitution_list (Parent_Subtype (gnat_root_type),
2493                                            Empty, gnu_subst_list, definition);
2494                 }
2495
2496               gnu_type = make_node (RECORD_TYPE);
2497               TYPE_NAME (gnu_type) = gnu_entity_id;
2498               TYPE_STUB_DECL (gnu_type)
2499                 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2500               TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2501
2502               for (gnat_field = First_Entity (gnat_entity);
2503                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2504                 if (Ekind (gnat_field) == E_Component
2505                     || Ekind (gnat_field) == E_Discriminant)
2506                   {
2507                     tree gnu_old_field
2508                       = gnat_to_gnu_entity
2509                         (Original_Record_Component (gnat_field), NULL_TREE, 0);
2510                     tree gnu_offset
2511                       = TREE_VALUE (purpose_member (gnu_old_field,
2512                                                     gnu_pos_list));
2513                     tree gnu_pos = TREE_PURPOSE (gnu_offset);
2514                     tree gnu_bitpos = TREE_VALUE (gnu_offset);
2515                     tree gnu_field_type
2516                       = gnat_to_gnu_type (Etype (gnat_field));
2517                     tree gnu_size = TYPE_SIZE (gnu_field_type);
2518                     tree gnu_new_pos = 0;
2519                     tree gnu_field;
2520
2521                     /* If there was a component clause, the field types must be
2522                        the same for the type and subtype, so copy the data from
2523                        the old field to avoid recomputation here.  */
2524                     if (Present (Component_Clause
2525                                  (Original_Record_Component (gnat_field))))
2526                       {
2527                         gnu_size = DECL_SIZE (gnu_old_field);
2528                         gnu_field_type = TREE_TYPE (gnu_old_field);
2529                       }
2530
2531                     /* If this was a bitfield, get the size from the old field.
2532                        Also ensure the type can be placed into a bitfield.  */
2533                     else if (DECL_BIT_FIELD (gnu_old_field))
2534                       {
2535                         gnu_size = DECL_SIZE (gnu_old_field);
2536                         if (TYPE_MODE (gnu_field_type) == BLKmode
2537                             && TREE_CODE (gnu_field_type) == RECORD_TYPE
2538                             && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2539                           gnu_field_type = make_packable_type (gnu_field_type);
2540                       }
2541
2542                     if (TREE_CODE (gnu_pos) != INTEGER_CST
2543                         && contains_placeholder_p (gnu_pos))
2544                       for (gnu_temp = gnu_subst_list;
2545                            gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2546                         gnu_pos = substitute_in_expr (gnu_pos,
2547                                                       TREE_PURPOSE (gnu_temp),
2548                                                       TREE_VALUE (gnu_temp));
2549
2550                     /* If the size is now a constant, we can set it as the
2551                        size of the field when we make it.  Otherwise, we need
2552                        to deal with it specially.  */
2553                     if (TREE_CONSTANT (gnu_pos))
2554                       gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2555
2556                     gnu_field
2557                       = create_field_decl
2558                         (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2559                          0, gnu_size, gnu_new_pos,
2560                          ! DECL_NONADDRESSABLE_P (gnu_old_field));
2561
2562                     if (! TREE_CONSTANT (gnu_pos))
2563                       {
2564                         normalize_offset (&gnu_pos, &gnu_bitpos, 
2565                                           DECL_OFFSET_ALIGN (gnu_old_field));
2566                         DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2567                         DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2568                         SET_DECL_OFFSET_ALIGN
2569                           (gnu_field, DECL_OFFSET_ALIGN (gnu_old_field));
2570                         DECL_SIZE (gnu_field) = gnu_size;
2571                         DECL_SIZE_UNIT (gnu_field)
2572                           = convert (sizetype,
2573                                      size_binop (CEIL_DIV_EXPR, gnu_size,
2574                                                  bitsize_unit_node));
2575                         layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2576                       }
2577
2578                     DECL_INTERNAL_P (gnu_field)
2579                       = DECL_INTERNAL_P (gnu_old_field);
2580                     DECL_ORIGINAL_FIELD (gnu_field)
2581                       = DECL_ORIGINAL_FIELD (gnu_old_field) != 0
2582                         ? DECL_ORIGINAL_FIELD (gnu_old_field) : gnu_old_field;
2583                     DECL_DISCRIMINANT_NUMBER (gnu_field)
2584                       = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2585                     TREE_THIS_VOLATILE (gnu_field)
2586                       = TREE_THIS_VOLATILE (gnu_old_field);
2587                     TREE_CHAIN (gnu_field) = gnu_field_list;
2588                     gnu_field_list = gnu_field;
2589                     save_gnu_tree (gnat_field, gnu_field, 0);
2590                   }
2591
2592               finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2593
2594               /* Now set the size, alignment and alias set of the new type to
2595                  match that of the old one, doing any substitutions, as
2596                  above.  */
2597               TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2598               TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2599               TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2600               TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2601               TYPE_ADA_SIZE (gnu_type) = TYPE_ADA_SIZE (gnu_base_type);
2602
2603               if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
2604                   && contains_placeholder_p (TYPE_SIZE (gnu_type)))
2605                 for (gnu_temp = gnu_subst_list;
2606                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2607                   TYPE_SIZE (gnu_type)
2608                     = substitute_in_expr (TYPE_SIZE (gnu_type),
2609                                           TREE_PURPOSE (gnu_temp),
2610                                           TREE_VALUE (gnu_temp));
2611
2612               if (TREE_CODE (TYPE_SIZE_UNIT (gnu_type)) != INTEGER_CST
2613                   && contains_placeholder_p (TYPE_SIZE_UNIT (gnu_type)))
2614                 for (gnu_temp = gnu_subst_list;
2615                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2616                   TYPE_SIZE_UNIT (gnu_type)
2617                     = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2618                                           TREE_PURPOSE (gnu_temp),
2619                                           TREE_VALUE (gnu_temp));
2620
2621               if (TYPE_ADA_SIZE (gnu_type) != 0
2622                   && TREE_CODE (TYPE_ADA_SIZE (gnu_type)) != INTEGER_CST
2623                   && contains_placeholder_p (TYPE_ADA_SIZE (gnu_type)))
2624                 for (gnu_temp = gnu_subst_list;
2625                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2626                   TYPE_ADA_SIZE (gnu_type)
2627                     = substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2628                                           TREE_PURPOSE (gnu_temp),
2629                                           TREE_VALUE (gnu_temp));
2630
2631               /* Recompute the mode of this record type now that we know its
2632                  actual size.  */
2633               compute_record_mode (gnu_type);
2634
2635               /* Fill in locations of fields.  */
2636               annotate_rep (gnat_entity, gnu_type);
2637             }
2638
2639           /* If we've made a new type, record it and make an XVS type to show
2640              what this is a subtype of.  Some debuggers require the  XVS
2641              type to be output first, so do it in that order.  */
2642           if (gnu_type != gnu_orig_type)
2643             {
2644               if (debug_info_p)
2645                 {
2646                   tree gnu_subtype_marker = make_node (RECORD_TYPE);
2647                   tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2648
2649                   if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2650                     gnu_orig_name = DECL_NAME (gnu_orig_name);
2651
2652                   TYPE_NAME (gnu_subtype_marker)
2653                     = create_concat_name (gnat_entity, "XVS");
2654                   finish_record_type (gnu_subtype_marker,
2655                                       create_field_decl (gnu_orig_name,
2656                                                          integer_type_node,
2657                                                          gnu_subtype_marker,
2658                                                          0, NULL_TREE,
2659                                                          NULL_TREE, 0),
2660                                       0, 0);
2661                 }
2662
2663               TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2664               TYPE_NAME (gnu_type) = gnu_entity_id;
2665               TYPE_STUB_DECL (gnu_type)
2666                 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2667                                       gnu_type));
2668               DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2669               DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2670               rest_of_type_compilation (gnu_type, global_bindings_p ());
2671             }
2672
2673           /* Otherwise, go down all the components in the new type and
2674              make them equivalent to those in the base type.  */
2675           else
2676             for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2677                  gnat_temp = Next_Entity (gnat_temp))
2678               if ((Ekind (gnat_temp) == E_Discriminant
2679                    && ! Is_Unchecked_Union (gnat_base_type))
2680                   || Ekind (gnat_temp) == E_Component)
2681                 save_gnu_tree (gnat_temp,
2682                                get_gnu_tree
2683                                (Original_Record_Component (gnat_temp)), 0);
2684         }
2685       break;
2686
2687     case E_Access_Subprogram_Type:
2688       /* If we are not defining this entity, and we have incomplete
2689          entities being processed above us, make a dummy type and
2690          fill it in later.  */
2691       if (! definition && defer_incomplete_level != 0)
2692         {
2693           struct incomplete *p
2694             = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2695
2696           gnu_type
2697             = build_pointer_type
2698               (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2699           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2700                                        ! Comes_From_Source (gnat_entity),
2701                                        debug_info_p);
2702           save_gnu_tree (gnat_entity, gnu_decl, 0);
2703           this_made_decl = saved = 1;
2704
2705           p->old_type = TREE_TYPE (gnu_type);
2706           p->full_type = Directly_Designated_Type (gnat_entity);
2707           p->next = defer_incomplete_list;
2708           defer_incomplete_list = p;
2709           break;
2710         }
2711
2712       /* ... fall through ... */
2713
2714     case E_Allocator_Type:
2715     case E_Access_Type:
2716     case E_Access_Attribute_Type:
2717     case E_Anonymous_Access_Type:
2718     case E_General_Access_Type:
2719       {
2720         Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2721         Entity_Id gnat_desig_full
2722           = ((IN (Ekind (Etype (gnat_desig_type)),
2723                   Incomplete_Or_Private_Kind))
2724              ? Full_View (gnat_desig_type) : 0);
2725         /* We want to know if we'll be seeing the freeze node for any
2726            incomplete type we may be pointing to.  */
2727         int in_main_unit
2728           = (Present (gnat_desig_full)
2729              ? In_Extended_Main_Code_Unit (gnat_desig_full)
2730              : In_Extended_Main_Code_Unit (gnat_desig_type));
2731         int got_fat_p = 0;
2732         int made_dummy = 0;
2733         tree gnu_desig_type = 0;
2734
2735         if (No (gnat_desig_full)
2736             && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2737                 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2738                     && Present (Equivalent_Type (gnat_desig_type)))))
2739           {
2740             if (Present (Equivalent_Type (gnat_desig_type)))
2741               {
2742                 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2743                 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2744                   gnat_desig_full = Full_View (gnat_desig_full);
2745               }
2746             else if (IN (Ekind (Root_Type (gnat_desig_type)),
2747                          Incomplete_Or_Private_Kind))
2748               gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2749           }
2750
2751         if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2752           gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2753
2754         /* If either the designated type or its full view is an
2755            unconstrained array subtype, replace it with the type it's a
2756            subtype of.  This avoids problems with multiple copies of
2757            unconstrained array types.  */
2758         if (Ekind (gnat_desig_type) == E_Array_Subtype
2759             && ! Is_Constrained (gnat_desig_type))
2760           gnat_desig_type = Etype (gnat_desig_type);
2761         if (Present (gnat_desig_full)
2762             && Ekind (gnat_desig_full) == E_Array_Subtype
2763             && ! Is_Constrained (gnat_desig_full))
2764           gnat_desig_full = Etype (gnat_desig_full);
2765
2766         /* If we are pointing to an incomplete type whose completion is an
2767            unconstrained array, make a fat pointer type instead of a pointer
2768            to VOID.  The two types in our fields will be pointers to VOID and
2769            will be replaced in update_pointer_to.  Similiarly, if the type
2770            itself is a dummy type or an unconstrained array.  Also make
2771            a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2772            pointers to it.  */
2773
2774         if ((Present (gnat_desig_full)
2775              && Is_Array_Type (gnat_desig_full)
2776              && ! Is_Constrained (gnat_desig_full))
2777             || (present_gnu_tree (gnat_desig_type)
2778                 && TYPE_IS_DUMMY_P (TREE_TYPE
2779                                      (get_gnu_tree (gnat_desig_type)))
2780                 && Is_Array_Type (gnat_desig_type)
2781                 && ! Is_Constrained (gnat_desig_type))
2782             || (present_gnu_tree (gnat_desig_type)
2783                 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2784                     == UNCONSTRAINED_ARRAY_TYPE)
2785                 && (TYPE_POINTER_TO (TREE_TYPE
2786                                      (get_gnu_tree (gnat_desig_type)))
2787                     == 0))
2788             || (No (gnat_desig_full) && ! in_main_unit
2789                 && defer_incomplete_level != 0
2790                 && ! present_gnu_tree (gnat_desig_type)
2791                 && Is_Array_Type (gnat_desig_type)
2792                 && ! Is_Constrained (gnat_desig_type)))
2793           {
2794             tree gnu_old
2795               = (present_gnu_tree (gnat_desig_type)
2796                  ? gnat_to_gnu_type (gnat_desig_type)
2797                  : make_dummy_type (gnat_desig_type));
2798             tree fields;
2799
2800             /* Show the dummy we get will be a fat pointer.  */
2801             got_fat_p = made_dummy = 1;
2802
2803             /* If the call above got something that has a pointer, that
2804                pointer is our type.  This could have happened either
2805                because the type was elaborated or because somebody
2806                else executed the code below.  */
2807             gnu_type = TYPE_POINTER_TO (gnu_old);
2808             if (gnu_type == 0)
2809               {
2810                 gnu_type = make_node (RECORD_TYPE);
2811                 TYPE_UNCONSTRAINED_ARRAY (gnu_type) = gnu_old;
2812                 TYPE_POINTER_TO (gnu_old) = gnu_type;
2813
2814                 set_lineno (gnat_entity, 0);
2815                 fields
2816                   = chainon (chainon (NULL_TREE,
2817                                       create_field_decl
2818                                       (get_identifier ("P_ARRAY"),
2819                                        ptr_void_type_node, gnu_type,
2820                                        0, 0, 0, 0)),
2821                              create_field_decl (get_identifier ("P_BOUNDS"),
2822                                                 ptr_void_type_node,
2823                                                 gnu_type, 0, 0, 0, 0));
2824
2825                 /* Make sure we can place this into a register.  */
2826                 TYPE_ALIGN (gnu_type)
2827                   = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2828                 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2829                 finish_record_type (gnu_type, fields, 0, 1);
2830
2831                 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2832                 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2833                   = concat_id_with_name (get_entity_name (gnat_desig_type),
2834                                          "XUT");
2835                 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2836               }
2837           }
2838
2839         /* If we already know what the full type is, use it.  */
2840         else if (Present (gnat_desig_full)
2841                  && present_gnu_tree (gnat_desig_full))
2842           gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2843
2844         /* Get the type of the thing we are to point to and build a pointer
2845            to it.  If it is a reference to an incomplete or private type with a
2846            full view that is a record, make a dummy type node and get the
2847            actual type later when we have verified it is safe.  */
2848         else if (! in_main_unit
2849                  && ! present_gnu_tree (gnat_desig_type)
2850                  && Present (gnat_desig_full)
2851                  && ! present_gnu_tree (gnat_desig_full)
2852                  && Is_Record_Type (gnat_desig_full))
2853           {
2854             gnu_desig_type = make_dummy_type (gnat_desig_type);
2855             made_dummy = 1;
2856           }
2857
2858         /* Likewise if we are pointing to a record or array and we are to defer
2859            elaborating incomplete types.  We do this since this access type
2860            may be the full view of some private type.  Note that the
2861            unconstrained array case is handled above. */
2862         else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2863                  && ! present_gnu_tree (gnat_desig_type)
2864                  && ((Is_Record_Type (gnat_desig_type)
2865                       || Is_Array_Type (gnat_desig_type))
2866                      || (Present (gnat_desig_full)
2867                          && (Is_Record_Type (gnat_desig_full)
2868                              || Is_Array_Type (gnat_desig_full)))))
2869           {
2870             gnu_desig_type = make_dummy_type (gnat_desig_type);
2871             made_dummy = 1;
2872           }
2873         else if (gnat_desig_type == gnat_entity)
2874           {
2875             gnu_type = build_pointer_type (make_node (VOID_TYPE));
2876             TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2877           }
2878         else
2879           gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
2880
2881         /* It is possible that the above call to gnat_to_gnu_type resolved our
2882            type.  If so, just return it.  */
2883         if (present_gnu_tree (gnat_entity))
2884           {
2885             maybe_present = 1;
2886             break;
2887           }
2888
2889         /* If we have a GCC type for the designated type, possibly
2890            modify it if we are pointing only to constant objects and then
2891            make a pointer to it.  Don't do this for unconstrained arrays.  */
2892         if (gnu_type == 0 && gnu_desig_type != 0)
2893           {
2894             if (Is_Access_Constant (gnat_entity)
2895                 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
2896               gnu_desig_type
2897                 = build_qualified_type (gnu_desig_type,
2898                                         (TYPE_QUALS (gnu_desig_type)
2899                                          | TYPE_QUAL_CONST));
2900
2901             gnu_type = build_pointer_type (gnu_desig_type);
2902           }
2903
2904         /* If we are not defining this object and we made a dummy pointer,
2905            save our current definition, evaluate the actual type, and replace
2906            the tentative type we made with the actual one.  If we are to defer
2907            actually looking up the actual type, make an entry in the
2908            deferred list.  */
2909
2910         if (! in_main_unit && made_dummy)
2911           {
2912             tree gnu_old_type
2913               = TYPE_FAT_POINTER_P (gnu_type)
2914                 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
2915
2916             if (esize == POINTER_SIZE
2917                 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
2918               gnu_type
2919                 = build_pointer_type
2920                   (TYPE_OBJECT_RECORD_TYPE
2921                    (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
2922
2923             gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2924                                          ! Comes_From_Source (gnat_entity),
2925                                          debug_info_p);
2926             save_gnu_tree (gnat_entity, gnu_decl, 0);
2927             this_made_decl = saved = 1;
2928
2929             if (defer_incomplete_level == 0)
2930               update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
2931                                  gnat_to_gnu_type (gnat_desig_type));
2932             else
2933               {
2934                 struct incomplete *p
2935                   = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2936
2937                 p->old_type = gnu_old_type;
2938                 p->full_type = gnat_desig_type;
2939                 p->next = defer_incomplete_list;
2940                 defer_incomplete_list = p;
2941               }
2942           }
2943       }
2944       break;
2945
2946     case E_Access_Protected_Subprogram_Type:
2947       if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
2948         gnu_type = build_pointer_type (void_type_node);
2949       else
2950         /* The runtime representation is the equivalent type. */
2951         gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2952
2953       if (Is_Itype (Directly_Designated_Type (gnat_entity))
2954           && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2955           && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
2956           && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
2957         gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2958                             NULL_TREE, 0);
2959
2960       break;
2961
2962     case E_Access_Subtype:
2963
2964       /* We treat this as identical to its base type; any constraint is
2965          meaningful only to the front end.
2966
2967          The designated type must be elaborated as well, if it does
2968          not have its own freeze node. Designated (sub)types created
2969          for constrained components of records with discriminants are
2970          not frozen by the front end and thus not elaborated by gigi,
2971          because their use may appear before the base type is frozen,
2972          and because it is not clear that they are needed anywhere in
2973          Gigi. With the current model, there is no correct place where
2974          they could be elaborated.  */
2975
2976       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2977       if (Is_Itype (Directly_Designated_Type (gnat_entity))
2978           && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2979           && Is_Frozen (Directly_Designated_Type (gnat_entity))
2980           && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
2981         {
2982           /* If we are not defining this entity, and we have incomplete
2983              entities being processed above us, make a dummy type and
2984              elaborate it later.  */
2985           if (! definition && defer_incomplete_level != 0)
2986             {
2987               struct incomplete *p
2988                 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2989               tree gnu_ptr_type
2990                 = build_pointer_type
2991                   (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2992
2993               p->old_type = TREE_TYPE (gnu_ptr_type);
2994               p->full_type = Directly_Designated_Type (gnat_entity);
2995               p->next = defer_incomplete_list;
2996               defer_incomplete_list = p;
2997             }
2998           else
2999             gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3000                                 NULL_TREE, 0);
3001         }
3002
3003       maybe_present = 1;
3004       break;
3005
3006     /* Subprogram Entities
3007
3008        The following access functions are defined for subprograms (functions
3009        or procedures):
3010
3011                 First_Formal    The first formal parameter.
3012                 Is_Imported     Indicates that the subprogram has appeared in
3013                                 an INTERFACE or IMPORT pragma. For now we
3014                                 assume that the external language is C.
3015                 Is_Inlined      True if the subprogram is to be inlined.
3016
3017        In addition for function subprograms we have:
3018
3019                 Etype           Return type of the function.
3020
3021        Each parameter is first checked by calling must_pass_by_ref on its
3022        type to determine if it is passed by reference.  For parameters which
3023        are copied in, if they are Ada IN OUT or OUT parameters, their return
3024        value becomes part of a record which becomes the return type of the
3025        function (C function - note that this applies only to Ada procedures
3026        so there is no Ada return type). Additional code to store back the
3027        parameters will be generated on the caller side.  This transformation
3028        is done here, not in the front-end.
3029
3030        The intended result of the transformation can be seen from the
3031        equivalent source rewritings that follow:
3032
3033                                                    struct temp {int a,b};
3034        procedure P (A,B: IN OUT ...) is            temp P (int A,B) {
3035         ..                                            ..
3036        end P;                                        return {A,B};
3037                                                    }
3038                               procedure call
3039
3040                                               {
3041                                                   temp t;
3042        P(X,Y);                                    t = P(X,Y);
3043                                                   X = t.a , Y = t.b;
3044                                               }
3045
3046        For subprogram types we need to perform mainly the same conversions to
3047        GCC form that are needed for procedures and function declarations.  The
3048        only difference is that at the end, we make a type declaration instead
3049        of a function declaration.  */
3050
3051     case E_Subprogram_Type:
3052     case E_Function:
3053     case E_Procedure:
3054       {
3055         /* The first GCC parameter declaration (a PARM_DECL node).  The
3056            PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3057            actually is the head of this parameter list.  */
3058         tree gnu_param_list = NULL_TREE;
3059         /* The type returned by a function. If the subprogram is a procedure
3060            this type should be void_type_node.  */
3061         tree gnu_return_type = void_type_node;
3062         /* List of fields in return type of procedure with copy in copy out
3063            parameters.  */
3064         tree gnu_field_list = NULL_TREE;
3065         /* Non-null for subprograms containing  parameters passed by copy in
3066            copy out (Ada IN OUT or OUT parameters not passed by reference),
3067            in which case it is the list of nodes used to specify the values of
3068            the in out/out parameters that are returned as a record upon
3069            procedure return.  The TREE_PURPOSE of an element of this list is
3070            a field of the record and the TREE_VALUE is the PARM_DECL
3071            corresponding to that field.  This list will be saved in the
3072            TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create.  */
3073         tree gnu_return_list = NULL_TREE;
3074         Entity_Id gnat_param;
3075         int inline_flag = Is_Inlined (gnat_entity);
3076         int public_flag = Is_Public (gnat_entity);
3077         int extern_flag
3078           = (Is_Public (gnat_entity) && !definition) || imported_p;
3079         int pure_flag = Is_Pure (gnat_entity);
3080         int volatile_flag = No_Return (gnat_entity);
3081         int returns_by_ref = 0;
3082         int returns_unconstrained = 0;
3083         tree gnu_ext_name = NULL_TREE;
3084         int has_copy_in_out = 0;
3085         int parmnum;
3086
3087         if (kind == E_Subprogram_Type && ! definition)
3088           /* A parameter may refer to this type, so defer completion
3089              of any incomplete types.  */
3090           defer_incomplete_level++, this_deferred = 1;
3091
3092         /* If the subprogram has an alias, it is probably inherited, so
3093            we can use the original one.  If the original "subprogram"
3094            is actually an enumeration literal, it may be the first use
3095            of its type, so we must elaborate that type now.  */
3096         if (Present (Alias (gnat_entity)))
3097           {
3098             if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3099               gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3100
3101             gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3102                                            gnu_expr, 0);
3103
3104             /* Elaborate any Itypes in the parameters of this entity.  */
3105             for (gnat_temp = First_Formal (gnat_entity);
3106                  Present (gnat_temp);
3107                  gnat_temp = Next_Formal_With_Extras (gnat_temp))
3108               if (Is_Itype (Etype (gnat_temp)))
3109                 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3110
3111             break;
3112           }
3113
3114         if (kind == E_Function || kind == E_Subprogram_Type)
3115           gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3116
3117         /* If this function returns by reference, make the actual
3118            return type of this function the pointer and mark the decl.  */
3119         if (Returns_By_Ref (gnat_entity))
3120           {
3121             returns_by_ref = 1;
3122
3123             gnu_return_type = build_pointer_type (gnu_return_type);
3124           }
3125
3126         /* If we are supposed to return an unconstrained array,
3127            actually return a fat pointer and make a note of that.  Return
3128            a pointer to an unconstrained record of variable size.  */
3129         else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3130           {
3131             gnu_return_type = TREE_TYPE (gnu_return_type);
3132             returns_unconstrained = 1;
3133           }
3134
3135         /* If the type requires a transient scope, the result is allocated
3136            on the secondary stack, so the result type of the function is
3137            just a pointer.  */
3138         else if (Requires_Transient_Scope (Etype (gnat_entity)))
3139           {
3140             gnu_return_type = build_pointer_type (gnu_return_type);
3141             returns_unconstrained = 1;
3142           }
3143
3144         /* If the type is a padded type and the underlying type would not
3145            be passed by reference or this function has a foreign convention,
3146            return the underlying type.  */
3147         else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3148                  && TYPE_IS_PADDING_P (gnu_return_type)
3149                  && (! default_pass_by_ref (TREE_TYPE
3150                                             (TYPE_FIELDS (gnu_return_type)))
3151                      || Has_Foreign_Convention (gnat_entity)))
3152           gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3153
3154         /* Look at all our parameters and get the type of
3155            each.  While doing this, build a copy-out structure if
3156            we need one.  */
3157
3158         for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3159              Present (gnat_param);
3160              gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3161           {
3162             tree gnu_param_name = get_entity_name (gnat_param);
3163             tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3164             tree gnu_param, gnu_field;
3165             int by_ref_p = 0;
3166             int by_descr_p = 0;
3167             int by_component_ptr_p = 0;
3168             int copy_in_copy_out_flag = 0;
3169             int req_by_copy = 0, req_by_ref = 0;
3170
3171             /* See if a Mechanism was supplied that forced this
3172                parameter to be passed one way or another.  */
3173             if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3174               req_by_copy = 1;
3175             else if (Mechanism (gnat_param) == Default)
3176               ;
3177             else if (Mechanism (gnat_param) == By_Copy)
3178               req_by_copy = 1;
3179             else if (Mechanism (gnat_param) == By_Reference)
3180               req_by_ref = 1;
3181             else if (Mechanism (gnat_param) <= By_Descriptor)
3182               by_descr_p = 1;
3183             else if (Mechanism (gnat_param) > 0)
3184               {
3185                 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3186                     || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3187                     || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3188                                              Mechanism (gnat_param)))
3189                   req_by_ref = 1;
3190                 else
3191                   req_by_copy = 1;
3192               }
3193             else
3194               post_error ("unsupported mechanism for&", gnat_param);
3195
3196             /* If this is either a foreign function or if the
3197                underlying type won't be passed by refererence, strip off
3198                possible padding type.  */
3199             if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3200                 && TYPE_IS_PADDING_P (gnu_param_type)
3201                 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3202                     || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3203                                                       (gnu_param_type)))))
3204               gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3205
3206             /* If this is an IN parameter it is read-only, so make a variant
3207                of the type that is read-only.
3208
3209                ??? However, if this is an unconstrained array, that type can
3210                be very complex.  So skip it for now.  Likewise for any other
3211                self-referential type.  */
3212             if (Ekind (gnat_param) == E_In_Parameter
3213                 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3214                 && ! (TYPE_SIZE (gnu_param_type) != 0
3215                       && TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3216                       && contains_placeholder_p (TYPE_SIZE (gnu_param_type))))
3217               gnu_param_type
3218                 = build_qualified_type (gnu_param_type,
3219                                         (TYPE_QUALS (gnu_param_type)
3220                                          | TYPE_QUAL_CONST));
3221
3222             /* For foreign conventions, pass arrays as a pointer to the
3223                underlying type.  First check for unconstrained array and get
3224                the underlying array.  Then get the component type and build
3225                a pointer to it.  */
3226             if (Has_Foreign_Convention (gnat_entity)
3227                 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3228               gnu_param_type
3229                 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3230                                         (TREE_TYPE (gnu_param_type))));
3231
3232             if (by_descr_p)
3233               gnu_param_type
3234                 = build_pointer_type
3235                   (build_vms_descriptor (gnu_param_type,
3236                                          Mechanism (gnat_param),
3237                                          gnat_entity));
3238
3239             else if (Has_Foreign_Convention (gnat_entity)
3240                      && ! req_by_copy
3241                      && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3242               {
3243                 /* Strip off any multi-dimensional entries, then strip
3244                    off the last array to get the component type.  */
3245                 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3246                        && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3247                   gnu_param_type = TREE_TYPE (gnu_param_type);
3248
3249                 by_component_ptr_p = 1;
3250                 gnu_param_type = TREE_TYPE (gnu_param_type);
3251
3252                 if (Ekind (gnat_param) == E_In_Parameter)
3253                   gnu_param_type
3254                     = build_qualified_type (gnu_param_type,
3255                                             (TYPE_QUALS (gnu_param_type)
3256                                              | TYPE_QUAL_CONST));
3257
3258                 gnu_param_type = build_pointer_type (gnu_param_type);
3259               }
3260
3261             /* Fat pointers are passed as thin pointers for foreign
3262                conventions.  */
3263             else if (Has_Foreign_Convention (gnat_entity)
3264                      && TYPE_FAT_POINTER_P (gnu_param_type))
3265               gnu_param_type
3266                 = make_type_from_size (gnu_param_type,
3267                                        size_int (POINTER_SIZE), 0);
3268
3269             /* If we must pass or were requested to pass by reference, do so.
3270                If we were requested to pass by copy, do so.
3271                Otherwise, for foreign conventions, pass all in out parameters
3272                or aggregates by reference.  For COBOL and Fortran, pass
3273                all integer and FP types that way too.  For Convention Ada,
3274                use the standard Ada default.  */
3275             else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3276                      || (! req_by_copy
3277                          && ((Has_Foreign_Convention (gnat_entity)
3278                               && (Ekind (gnat_param) != E_In_Parameter
3279                                   || AGGREGATE_TYPE_P (gnu_param_type)))
3280                              || (((Convention (gnat_entity)
3281                                    == Convention_Fortran)
3282                                   || (Convention (gnat_entity)
3283                                       == Convention_COBOL))
3284                                  && (INTEGRAL_TYPE_P (gnu_param_type)
3285                                      || FLOAT_TYPE_P (gnu_param_type)))
3286                              /* For convention Ada, see if we pass by reference
3287                                 by default.  */
3288                              || (! Has_Foreign_Convention (gnat_entity)
3289                                  && default_pass_by_ref (gnu_param_type)))))
3290               {
3291                 gnu_param_type = build_reference_type (gnu_param_type);
3292                 by_ref_p = 1;
3293               }
3294
3295             else if (Ekind (gnat_param) != E_In_Parameter)
3296               copy_in_copy_out_flag = 1;
3297
3298             if (req_by_copy && (by_ref_p || by_component_ptr_p))
3299               post_error ("?cannot pass & by copy", gnat_param);
3300
3301             /* If this is an OUT parameter that isn't passed by reference
3302                and isn't a pointer or aggregate, we don't make a PARM_DECL
3303                for it.  Instead, it will be a VAR_DECL created when we process
3304                the procedure.  For the special parameter of Valued_Procedure,
3305                never pass it in.  */
3306             if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3307                 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3308                     || (! by_descr_p
3309                         && ! POINTER_TYPE_P (gnu_param_type)
3310                         && ! AGGREGATE_TYPE_P (gnu_param_type))))
3311               gnu_param = 0;
3312             else
3313               {
3314                 set_lineno (gnat_param, 0);
3315                 gnu_param
3316                   = create_param_decl
3317                     (gnu_param_name, gnu_param_type,
3318                      by_ref_p || by_component_ptr_p
3319                      || Ekind (gnat_param) == E_In_Parameter);
3320
3321                 DECL_BY_REF_P (gnu_param) = by_ref_p;
3322                 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3323                 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3324                 DECL_POINTS_TO_READONLY_P (gnu_param)
3325                   = (Ekind (gnat_param) == E_In_Parameter
3326                      && (by_ref_p || by_component_ptr_p));
3327                 save_gnu_tree (gnat_param, gnu_param, 0);
3328                 gnu_param_list = chainon (gnu_param, gnu_param_list);
3329
3330                 /* If a parameter is a pointer, this function may modify
3331                    memory through it and thus shouldn't be considered
3332                    a pure function.  Also, the memory may be modified
3333                    between two calls, so they can't be CSE'ed.  The latter
3334                    case also handles by-ref parameters.  */
3335                 if (POINTER_TYPE_P (gnu_param_type)
3336                     ||  TYPE_FAT_POINTER_P (gnu_param_type))
3337                   pure_flag = 0;
3338               }
3339
3340             if (copy_in_copy_out_flag)
3341               {
3342                 if (! has_copy_in_out)
3343                   {
3344                     if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3345                       gigi_abort (111);
3346
3347                     gnu_return_type = make_node (RECORD_TYPE);
3348                     TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3349                     has_copy_in_out = 1;
3350                   }
3351
3352                 set_lineno (gnat_param, 0);
3353                 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3354                                                gnu_return_type, 0, 0, 0, 0);
3355                 TREE_CHAIN (gnu_field) = gnu_field_list;
3356                 gnu_field_list = gnu_field;
3357                 gnu_return_list = tree_cons (gnu_field, gnu_param,
3358                                              gnu_return_list);
3359               }
3360           }
3361
3362         /* Do not compute record for out parameters if subprogram is
3363            stubbed since structures are incomplete for the back-end.  */
3364         if (gnu_field_list != 0
3365             && Convention (gnat_entity) != Convention_Stubbed)
3366           finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3367                               0, 0);
3368
3369         /* If we have a CICO list but it has only one entry, we convert
3370            this function into a function that simply returns that one
3371            object.  */
3372         if (list_length (gnu_return_list) == 1)
3373           gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3374
3375         if (Convention (gnat_entity) == Convention_Stdcall)
3376           {
3377             struct attrib *attr
3378               = (struct attrib *) xmalloc (sizeof (struct attrib));
3379
3380             attr->next = attr_list;
3381             attr->type = ATTR_MACHINE_ATTRIBUTE;
3382             attr->name = get_identifier ("stdcall");
3383             attr->arg = NULL_TREE;
3384             attr->error_point = gnat_entity;
3385             attr_list = attr;
3386           }
3387
3388         /* Both lists ware built in reverse.  */
3389         gnu_param_list = nreverse (gnu_param_list);
3390         gnu_return_list = nreverse (gnu_return_list);
3391
3392         gnu_type
3393           = create_subprog_type (gnu_return_type, gnu_param_list,
3394                                  gnu_return_list, returns_unconstrained,
3395                                  returns_by_ref,
3396                                  Function_Returns_With_DSP (gnat_entity));
3397
3398         /* ??? For now, don't consider nested fuctions pure.  */
3399         if (! global_bindings_p ())
3400           pure_flag = 0;
3401
3402         gnu_type
3403           = build_qualified_type (gnu_type,
3404                                   (TYPE_QUALS (gnu_type)
3405                                    | (TYPE_QUAL_CONST * pure_flag)
3406                                    | (TYPE_QUAL_VOLATILE * volatile_flag)));
3407
3408         /* Top-level or external functions need to have an assembler name.
3409            This is passed to create_subprog_decl through the ext_name argument.
3410            For Pragma Interface subprograms with no Pragma Interface_Name, the
3411            simple name already in entity_name is correct, and this is what is
3412            gotten when ext_name is NULL.  If Interface_Name is specified, then
3413            the name is extracted from the N_String_Literal node containing the
3414            string specified in the Pragma.  If there is no Pragma Interface,
3415            then the Ada fully qualified name is created. */
3416
3417         if (Present (Interface_Name (gnat_entity))
3418             || ! (Is_Imported (gnat_entity) || Is_Exported (gnat_entity)))
3419           gnu_ext_name = create_concat_name (gnat_entity, 0);
3420
3421         set_lineno (gnat_entity, 0);
3422
3423         /* If we are defining the subprogram and it has an Address clause
3424            we must get the address expression from the saved GCC tree for the
3425            subprogram if it has a Freeze_Node.  Otherwise, we elaborate
3426            the address expression here since the front-end has guaranteed
3427            in that case that the elaboration has no effects.  If there is
3428            an Address clause and we are not defining the object, just
3429            make it a constant.  */
3430         if (Present (Address_Clause (gnat_entity)))
3431           {
3432             tree gnu_address = 0;
3433
3434             if (definition)
3435               gnu_address
3436                 = (present_gnu_tree (gnat_entity)
3437                    ? get_gnu_tree (gnat_entity)
3438                    : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3439
3440             save_gnu_tree (gnat_entity, NULL_TREE, 0);
3441
3442             gnu_type = build_reference_type (gnu_type);
3443             if (gnu_address != 0)
3444               gnu_address = convert (gnu_type, gnu_address);
3445
3446             gnu_decl
3447               = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3448                                  gnu_address, 0, Is_Public (gnat_entity),
3449                                  extern_flag, 0, 0);
3450             DECL_BY_REF_P (gnu_decl) = 1;
3451           }
3452
3453         else if (kind == E_Subprogram_Type)
3454           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3455                                        ! Comes_From_Source (gnat_entity),
3456                                        debug_info_p);
3457         else
3458           {
3459             gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3460                                             gnu_type, gnu_param_list,
3461                                             inline_flag, public_flag,
3462                                             extern_flag, attr_list);
3463             DECL_STUBBED_P (gnu_decl)
3464               = Convention (gnat_entity) == Convention_Stubbed;
3465           }
3466       }
3467       break;
3468
3469     case E_Incomplete_Type:
3470     case E_Private_Type:
3471     case E_Limited_Private_Type:
3472     case E_Record_Type_With_Private:
3473     case E_Private_Subtype:
3474     case E_Limited_Private_Subtype:
3475     case E_Record_Subtype_With_Private:
3476
3477       /* If this type does not have a full view in the unit we are
3478          compiling, then just get the type from its Etype.  */
3479       if (No (Full_View (gnat_entity)))
3480         {
3481           /* If this is an incomplete type with no full view, it must
3482              be a Taft Amendement type, so just return a dummy type.  */
3483           if (kind == E_Incomplete_Type)
3484             gnu_type = make_dummy_type (gnat_entity);
3485
3486           else if (Present (Underlying_Full_View (gnat_entity)))
3487              gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3488                                             NULL_TREE, 0);
3489           else
3490             {
3491               gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3492                                              NULL_TREE, 0);
3493               maybe_present = 1;
3494             }
3495
3496           break;
3497         }
3498
3499       /* Otherwise, if we are not defining the type now, get the
3500          type from the full view. But always get the type from the full
3501          view for define on use types, since otherwise we won't see them! */
3502
3503       else if (! definition
3504                || (Is_Itype (Full_View (gnat_entity))
3505                    && No (Freeze_Node (gnat_entity)))
3506                || (Is_Itype (gnat_entity)
3507                    && No (Freeze_Node (Full_View (gnat_entity)))))
3508         {
3509           gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3510                                          NULL_TREE, 0);
3511           maybe_present = 1;
3512           break;
3513         }
3514
3515       /* For incomplete types, make a dummy type entry which will be
3516          replaced later.  */
3517       gnu_type = make_dummy_type (gnat_entity);
3518
3519       /* Save this type as the full declaration's type so we can do any needed
3520          updates when we see it.  */
3521       set_lineno (gnat_entity, 0);
3522       gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3523                                    ! Comes_From_Source (gnat_entity),
3524                                    debug_info_p);
3525       save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3526       break;
3527
3528       /* Simple class_wide types are always viewed as their root_type
3529          by Gigi unless an Equivalent_Type is specified.  */
3530     case E_Class_Wide_Type:
3531       if (Present (Equivalent_Type (gnat_entity)))
3532         gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3533       else
3534         gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3535
3536       maybe_present = 1;
3537       break;
3538
3539     case E_Task_Type:
3540     case E_Task_Subtype:
3541     case E_Protected_Type:
3542     case E_Protected_Subtype:
3543       if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3544         gnu_type = void_type_node;
3545       else
3546         gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3547
3548       maybe_present = 1;
3549       break;
3550
3551     case E_Label:
3552       gnu_decl = create_label_decl (gnu_entity_id);
3553       break;
3554
3555     case E_Block:
3556     case E_Loop:
3557       /* Nothing at all to do here, so just return an ERROR_MARK and claim
3558          we've already saved it, so we don't try to.  */
3559       gnu_decl = error_mark_node;
3560       saved = 1;
3561       break;
3562
3563     default:
3564       gigi_abort (113);
3565     }
3566
3567   /* If we had a case where we evaluated another type and it might have
3568      defined this one, handle it here.  */
3569   if (maybe_present && present_gnu_tree (gnat_entity))
3570     {
3571       gnu_decl = get_gnu_tree (gnat_entity);
3572       saved = 1;
3573     }
3574
3575   /* If we are processing a type and there is either no decl for it or
3576      we just made one, do some common processing for the type, such as
3577      handling alignment and possible padding.  */
3578
3579   if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3580     {
3581       if (Is_Tagged_Type (gnat_entity))
3582         TYPE_ALIGN_OK_P (gnu_type) = 1;
3583
3584       if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3585         TYPE_BY_REFERENCE_P (gnu_type) = 1;
3586
3587       /* ??? Don't set the size for a String_Literal since it is either
3588          confirming or we don't handle it properly (if the low bound is
3589          non-constant).  */
3590       if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3591         gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3592                                   TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3593
3594       /* If a size was specified, see if we can make a new type of that size
3595          by rearranging the type, for example from a fat to a thin pointer.  */
3596       if (gnu_size != 0)
3597         {
3598           gnu_type
3599             = make_type_from_size (gnu_type, gnu_size,
3600                                    Has_Biased_Representation (gnat_entity));
3601
3602           if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3603               && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3604             gnu_size = 0;
3605         }
3606
3607       /* If the alignment hasn't already been processed and this is
3608          not an unconstrained array, see if an alignment is specified.
3609          If not, we pick a default alignment for atomic objects.  */
3610       if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3611         ;
3612       else if (Known_Alignment (gnat_entity))
3613         align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3614                                     TYPE_ALIGN (gnu_type));
3615       else if (Is_Atomic (gnat_entity) && gnu_size == 0
3616                && host_integerp (TYPE_SIZE (gnu_type), 1)
3617                && integer_pow2p (TYPE_SIZE (gnu_type)))
3618         align = MIN (BIGGEST_ALIGNMENT,
3619                      tree_low_cst (TYPE_SIZE (gnu_type), 1));
3620       else if (Is_Atomic (gnat_entity) && gnu_size != 0
3621                && host_integerp (gnu_size, 1)
3622                && integer_pow2p (gnu_size))
3623         align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3624
3625       /* See if we need to pad the type.  If we did, and made a record,
3626          the name of the new type may be changed.  So get it back for
3627          us when we make the new TYPE_DECL below.  */
3628       gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3629                                  gnat_entity, "PAD", 1, definition, 0);
3630       if (TREE_CODE (gnu_type) == RECORD_TYPE
3631           && TYPE_IS_PADDING_P (gnu_type))
3632         {
3633           gnu_entity_id = TYPE_NAME (gnu_type);
3634           if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3635             gnu_entity_id = DECL_NAME (gnu_entity_id);
3636         }
3637
3638       set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3639
3640       /* If we are at global level, GCC will have applied variable_size to
3641          the type, but that won't have done anything.  So, if it's not
3642          a constant or self-referential, call elaborate_expression_1 to
3643          make a variable for the size rather than calculating it each time.
3644          Handle both the RM size and the actual size.  */
3645       if (global_bindings_p ()
3646           && TYPE_SIZE (gnu_type) != 0
3647           && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
3648           && ! contains_placeholder_p (TYPE_SIZE (gnu_type)))
3649         {
3650           if (TREE_CODE (gnu_type) == RECORD_TYPE
3651               && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3652                                   TYPE_SIZE (gnu_type), 0))
3653             TYPE_ADA_SIZE (gnu_type) = TYPE_SIZE (gnu_type)
3654               = elaborate_expression_1 (gnat_entity, gnat_entity,
3655                                         TYPE_SIZE (gnu_type),
3656                                         get_identifier ("SIZE"),
3657                                         definition, 0);
3658           else if (TREE_CODE (gnu_type) == RECORD_TYPE)
3659             {
3660               TYPE_ADA_SIZE (gnu_type)
3661                 = elaborate_expression_1 (gnat_entity, gnat_entity,
3662                                           TYPE_ADA_SIZE (gnu_type),
3663                                           get_identifier ("RM_SIZE"),
3664                                           definition, 0);
3665               TYPE_SIZE (gnu_type)
3666                 = elaborate_expression_1 (gnat_entity, gnat_entity,
3667                                           TYPE_SIZE (gnu_type),
3668                                           get_identifier ("SIZE"),
3669                                           definition, 0);
3670               TYPE_SIZE_UNIT (gnu_type)
3671                 = elaborate_expression_1 (gnat_entity, gnat_entity,
3672                                           TYPE_SIZE_UNIT (gnu_type),
3673                                           get_identifier ("SIZE_UNIT"),
3674                                           definition, 0);
3675             }
3676           else
3677             {
3678               TYPE_SIZE (gnu_type)
3679                 = elaborate_expression_1 (gnat_entity, gnat_entity,
3680                                           TYPE_SIZE (gnu_type),
3681                                           get_identifier ("SIZE"),
3682                                           definition, 0);
3683               TYPE_SIZE_UNIT (gnu_type)
3684                 = elaborate_expression_1 (gnat_entity, gnat_entity,
3685                                           TYPE_SIZE_UNIT (gnu_type),
3686                                           get_identifier ("SIZE_UNIT"),
3687                                           definition, 0);
3688             }
3689         }
3690
3691       /* If this is a record type or subtype, call elaborate_expression_1 on
3692          any field position.  Do this for both global and local types.
3693          Skip any fields that we haven't made trees for to avoid problems with
3694          class wide types.  */
3695       if (IN (kind, Record_Kind))
3696         for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3697              gnat_temp = Next_Entity (gnat_temp))
3698           if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3699             {
3700               tree gnu_field = get_gnu_tree (gnat_temp);
3701
3702               if (TREE_CODE (DECL_FIELD_OFFSET (gnu_field)) != INTEGER_CST
3703                   && ! contains_placeholder_p (DECL_FIELD_OFFSET (gnu_field)))
3704                 DECL_FIELD_OFFSET (gnu_field)
3705                   = elaborate_expression_1 (gnat_temp, gnat_temp, 
3706                                             DECL_FIELD_OFFSET (gnu_field),
3707                                             get_identifier ("OFFSET"),
3708                                             definition, 0);
3709             }
3710
3711       gnu_type = build_qualified_type (gnu_type,
3712                                        (TYPE_QUALS (gnu_type)
3713                                         | (TYPE_QUAL_VOLATILE
3714                                            * Is_Volatile (gnat_entity))));
3715
3716       if (Is_Atomic (gnat_entity))
3717         check_ok_for_atomic (gnu_type, gnat_entity, 0);
3718
3719       if (Known_Alignment (gnat_entity))
3720         TYPE_USER_ALIGN (gnu_type) = 1;
3721
3722       if (gnu_decl == 0)
3723         {
3724           set_lineno (gnat_entity, 0);
3725           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3726                                        ! Comes_From_Source (gnat_entity),
3727                                        debug_info_p);
3728         }
3729       else
3730         TREE_TYPE (gnu_decl) = gnu_type;
3731     }
3732
3733   if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3734     {
3735       gnu_type = TREE_TYPE (gnu_decl);
3736
3737       /* Back-annotate the Alignment of the type if not already in the
3738          tree.  Likewise for sizes.  */
3739       if (Unknown_Alignment (gnat_entity))
3740         Set_Alignment (gnat_entity,
3741                        UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3742
3743       if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3744         {
3745           /* If the size is self-referential, we annotate the maximum
3746              value of that size.  */
3747           tree gnu_size = TYPE_SIZE (gnu_type);
3748
3749           if (contains_placeholder_p (gnu_size))
3750             gnu_size = max_size (gnu_size, 1);
3751
3752           Set_Esize (gnat_entity, annotate_value (gnu_size));
3753         }
3754
3755       if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3756         Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3757     }
3758
3759   if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3760     DECL_ARTIFICIAL (gnu_decl) = 1;
3761
3762   if (! debug_info_p && DECL_P (gnu_decl)
3763       && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3764     DECL_IGNORED_P (gnu_decl) = 1;
3765
3766   /* If this decl is really indirect, adjust it.  */
3767   if (TREE_CODE (gnu_decl) == VAR_DECL)
3768     adjust_decl_rtl (gnu_decl);
3769
3770   /* If we haven't already, associate the ..._DECL node that we just made with
3771      the input GNAT entity node. */
3772   if (! saved)
3773     save_gnu_tree (gnat_entity, gnu_decl, 0);
3774
3775   /* If this is an enumeral or floating-point type, we were not able to set
3776      the bounds since they refer to the type.  These bounds are always static.
3777
3778      For enumeration types, also write debugging information and declare the
3779      enumeration literal  table, if needed.  */
3780
3781   if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3782       || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3783     {
3784       tree gnu_scalar_type = gnu_type;
3785
3786       /* If this is a padded type, we need to use the underlying type.  */
3787       if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3788           && TYPE_IS_PADDING_P (gnu_scalar_type))
3789         gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3790
3791       /* If this is a floating point type and we haven't set a floating
3792          point type yet, use this in the evaluation of the bounds.  */
3793       if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3794         longest_float_type_node = gnu_type;
3795
3796       TYPE_MIN_VALUE (gnu_scalar_type)
3797         = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3798       TYPE_MAX_VALUE (gnu_scalar_type)
3799         = gnat_to_gnu (Type_High_Bound (gnat_entity));
3800
3801       if (kind == E_Enumeration_Type)
3802         {
3803           TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3804
3805           /* Since this has both a typedef and a tag, avoid outputting
3806              the name twice.  */
3807           DECL_ARTIFICIAL (gnu_decl) = 1;
3808           rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
3809         }
3810     }
3811
3812   /* If we deferred processing of incomplete types, re-enable it.  If there
3813      were no other disables and we have some to process, do so.  */
3814   if (this_deferred && --defer_incomplete_level == 0
3815       && defer_incomplete_list != 0)
3816     {
3817       struct incomplete *incp = defer_incomplete_list;
3818       struct incomplete *next;
3819
3820       defer_incomplete_list = 0;
3821       for (; incp; incp = next)
3822         {
3823           next = incp->next;
3824
3825           if (incp->old_type != 0)
3826             update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3827                                gnat_to_gnu_type (incp->full_type));
3828           free (incp);
3829         }
3830     }
3831
3832   /* If we are not defining this type, see if it's in the incomplete list.
3833      If so, handle that list entry now.  */
3834   else if (! definition)
3835     {
3836       struct incomplete *incp;
3837
3838       for (incp = defer_incomplete_list; incp; incp = incp->next)
3839         if (incp->old_type != 0 && incp->full_type == gnat_entity)
3840           {
3841             update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3842                                TREE_TYPE (gnu_decl));
3843             incp->old_type = 0;
3844           }
3845     }
3846
3847   if (this_global)
3848     force_global--;
3849
3850   if (Is_Packed_Array_Type (gnat_entity)
3851       && Is_Itype (Associated_Node_For_Itype (gnat_entity))
3852       && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
3853       && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
3854     gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
3855
3856   return gnu_decl;
3857 }
3858 \f
3859 /* Given GNAT_ENTITY, elaborate all expressions that are required to
3860    be elaborated at the point of its definition, but do nothing else.  */
3861
3862 void
3863 elaborate_entity (gnat_entity)
3864      Entity_Id gnat_entity;
3865 {
3866   switch (Ekind (gnat_entity))
3867     {
3868     case E_Signed_Integer_Subtype:
3869     case E_Modular_Integer_Subtype:
3870     case E_Enumeration_Subtype:
3871     case E_Ordinary_Fixed_Point_Subtype:
3872     case E_Decimal_Fixed_Point_Subtype:
3873     case E_Floating_Point_Subtype:
3874       {
3875         Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
3876         Node_Id gnat_hb = Type_High_Bound (gnat_entity);
3877
3878         /* ??? Tests for avoiding static constaint error expression
3879            is needed until the front stops generating bogus conversions
3880            on bounds of real types. */
3881
3882         if (! Raises_Constraint_Error (gnat_lb))
3883           elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
3884                                 1, 0, Needs_Debug_Info (gnat_entity));
3885         if (! Raises_Constraint_Error (gnat_hb))
3886           elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
3887                                 1, 0, Needs_Debug_Info (gnat_entity));
3888       break;
3889       }
3890
3891     case E_Record_Type:
3892       {
3893         Node_Id full_definition = Declaration_Node (gnat_entity);
3894         Node_Id record_definition = Type_Definition (full_definition);
3895
3896         /* If this is a record extension, go a level further to find the
3897            record definition.  */
3898         if (Nkind (record_definition) == N_Derived_Type_Definition)
3899           record_definition = Record_Extension_Part (record_definition);
3900       }
3901       break;
3902
3903     case E_Record_Subtype:
3904     case E_Private_Subtype:
3905     case E_Limited_Private_Subtype:
3906     case E_Record_Subtype_With_Private:
3907       if (Is_Constrained (gnat_entity)
3908           && Has_Discriminants (Base_Type (gnat_entity))
3909           && Present (Discriminant_Constraint (gnat_entity)))
3910         {
3911           Node_Id gnat_discriminant_expr;
3912           Entity_Id gnat_field;
3913
3914           for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
3915                gnat_discriminant_expr
3916                = First_Elmt (Discriminant_Constraint (gnat_entity));
3917                Present (gnat_field);
3918                gnat_field = Next_Discriminant (gnat_field),
3919                gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
3920             /* ??? For now, ignore access discriminants.  */
3921             if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
3922               elaborate_expression (Node (gnat_discriminant_expr),
3923                                     gnat_entity,
3924                                     get_entity_name (gnat_field), 1, 0, 0);
3925         }
3926       break;
3927
3928     }
3929 }
3930 \f
3931 /* Mark GNAT_ENTITY as going out of scope at this point.  Recursively mark
3932    any entities on its entity chain similarly.  */
3933
3934 void
3935 mark_out_of_scope (gnat_entity)
3936      Entity_Id gnat_entity;
3937 {
3938   Entity_Id gnat_sub_entity;
3939   unsigned int kind = Ekind (gnat_entity);
3940
3941   /* If this has an entity list, process all in the list.  */
3942   if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
3943       || IN (kind, Private_Kind)
3944       || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
3945       || kind == E_Function || kind == E_Generic_Function
3946       || kind == E_Generic_Package || kind == E_Generic_Procedure
3947       || kind == E_Loop || kind == E_Operator || kind == E_Package
3948       || kind == E_Package_Body || kind == E_Procedure
3949       || kind == E_Record_Type || kind == E_Record_Subtype
3950       || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
3951     for (gnat_sub_entity = First_Entity (gnat_entity);
3952          Present (gnat_sub_entity);
3953          gnat_sub_entity = Next_Entity (gnat_sub_entity))
3954             if (Scope (gnat_sub_entity) == gnat_entity
3955                 && gnat_sub_entity != gnat_entity)
3956         mark_out_of_scope (gnat_sub_entity);
3957
3958   /* Now clear this if it has been defined, but only do so if it isn't
3959      a subprogram or parameter.  We could refine this, but it isn't
3960      worth it.  If this is statically allocated, it is supposed to
3961      hang around out of cope.  */
3962   if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
3963       && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
3964     {
3965       save_gnu_tree (gnat_entity, NULL_TREE, 1);
3966       save_gnu_tree (gnat_entity, error_mark_node, 1);
3967     }
3968 }
3969 \f
3970 /* Return a TREE_LIST describing the substitutions needed to reflect
3971    discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
3972    them to GNU_LIST.  If GNAT_TYPE is not specified, use the base type
3973    of GNAT_SUBTYPE. The substitions can be in any order.  TREE_PURPOSE
3974    gives the tree for the discriminant and TREE_VALUES is the replacement
3975    value.  They are in the form of operands to substitute_in_expr.
3976    DEFINITION is as in gnat_to_gnu_entity.  */
3977
3978 static tree
3979 substitution_list (gnat_subtype, gnat_type, gnu_list, definition)
3980      Entity_Id gnat_subtype;
3981      Entity_Id gnat_type;
3982      tree gnu_list;
3983      int definition;
3984 {
3985   Entity_Id gnat_discrim;
3986   Node_Id gnat_value;
3987
3988   if (No (gnat_type))
3989     gnat_type = Implementation_Base_Type (gnat_subtype);
3990
3991   if (Has_Discriminants (gnat_type))
3992     for (gnat_discrim = First_Girder_Discriminant (gnat_type),
3993          gnat_value = First_Elmt (Girder_Constraint (gnat_subtype));
3994          Present (gnat_discrim);
3995          gnat_discrim = Next_Girder_Discriminant (gnat_discrim),
3996          gnat_value = Next_Elmt (gnat_value))
3997       /* Ignore access discriminants.  */
3998       if (! Is_Access_Type (Etype (Node (gnat_value))))
3999         gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4000                             elaborate_expression
4001                               (Node (gnat_value), gnat_subtype,
4002                                get_entity_name (gnat_discrim), definition,
4003                                1, 0),
4004                               gnu_list);
4005
4006   return gnu_list;
4007 }
4008 \f
4009 /* For the following two functions: for each GNAT entity, the GCC
4010    tree node used as a dummy for that entity, if any.  */
4011
4012 static tree *dummy_node_table;
4013
4014 /* Initialize the above table.  */
4015
4016 void
4017 init_dummy_type ()
4018 {
4019   Node_Id gnat_node;
4020
4021   dummy_node_table = (tree *) xmalloc (max_gnat_nodes * sizeof (tree));
4022   ggc_add_tree_root (dummy_node_table, max_gnat_nodes);
4023
4024   for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4025     dummy_node_table[gnat_node] = NULL_TREE;
4026
4027   dummy_node_table -= First_Node_Id;
4028 }
4029
4030 /* Make a dummy type corresponding to GNAT_TYPE.  */
4031
4032 tree
4033 make_dummy_type (gnat_type)
4034      Entity_Id gnat_type;
4035 {
4036   Entity_Id gnat_underlying;
4037   tree gnu_type;
4038
4039   /* Find a full type for GNAT_TYPE, taking into account any class wide
4040      types.  */
4041   if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4042     gnat_type = Equivalent_Type (gnat_type);
4043   else if (Ekind (gnat_type) == E_Class_Wide_Type)
4044     gnat_type = Root_Type (gnat_type);
4045
4046   for (gnat_underlying = gnat_type;
4047        (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4048         && Present (Full_View (gnat_underlying)));
4049        gnat_underlying = Full_View (gnat_underlying))
4050     ;
4051
4052   /* If it there already a dummy type, use that one.  Else make one.  */
4053   if (dummy_node_table[gnat_underlying])
4054     return dummy_node_table[gnat_underlying];
4055
4056   /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4057      it a VOID_TYPE.  */
4058   if (Is_Record_Type (gnat_underlying))
4059     gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4060                           ? UNION_TYPE : RECORD_TYPE);
4061   else
4062     gnu_type = make_node (ENUMERAL_TYPE);
4063
4064   TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4065   if (AGGREGATE_TYPE_P (gnu_type))
4066     TYPE_STUB_DECL (gnu_type)
4067       = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4068
4069   TYPE_DUMMY_P (gnu_type) = 1;
4070   dummy_node_table[gnat_underlying] = gnu_type;
4071
4072   return gnu_type;
4073 }
4074 \f
4075 /* Return 1 if the size represented by GNU_SIZE can be handled by an
4076    allocation.  If STATIC_P is non-zero, consider only what can be
4077    done with a static allocation.  */
4078
4079 static int
4080 allocatable_size_p (gnu_size, static_p)
4081      tree gnu_size;
4082      int static_p;
4083 {
4084   /* If this is not a static allocation, the only case we want to forbid
4085      is an overflowing size.  That will be converted into a raise a
4086      Storage_Error.  */
4087   if (! static_p)
4088     return ! (TREE_CODE (gnu_size) == INTEGER_CST
4089               && TREE_CONSTANT_OVERFLOW (gnu_size));
4090
4091   /* Otherwise, we need to deal with both variable sizes and constant
4092      sizes that won't fit in a host int.  */
4093   return host_integerp (gnu_size, 1);
4094 }
4095 \f
4096 /* Return a list of attributes for GNAT_ENTITY, if any.  */
4097
4098 static struct attrib *
4099 build_attr_list (gnat_entity)
4100      Entity_Id gnat_entity;
4101 {
4102   struct attrib *attr_list = 0;
4103   Node_Id gnat_temp;
4104
4105   for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4106        gnat_temp = Next_Rep_Item (gnat_temp))
4107     if (Nkind (gnat_temp) == N_Pragma)
4108       {
4109         struct attrib *attr;
4110         tree gnu_arg0 = 0, gnu_arg1 = 0;
4111         Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4112         enum attr_type etype;
4113
4114         if (Present (gnat_assoc) && Present (First (gnat_assoc))
4115             && Present (Next (First (gnat_assoc)))
4116             && (Nkind (Expression (Next (First (gnat_assoc))))
4117                 == N_String_Literal))
4118           {
4119             gnu_arg0 = get_identifier (TREE_STRING_POINTER
4120                                        (gnat_to_gnu
4121                                         (Expression (Next
4122                                                      (First (gnat_assoc))))));
4123             if (Present (Next (Next (First (gnat_assoc))))
4124                 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4125                     == N_String_Literal))
4126               gnu_arg1 = get_identifier (TREE_STRING_POINTER
4127                                          (gnat_to_gnu
4128                                           (Expression
4129                                            (Next (Next
4130                                                   (First (gnat_assoc)))))));
4131           }
4132
4133         switch (Get_Pragma_Id (Chars (gnat_temp)))
4134           {
4135           case Pragma_Machine_Attribute:
4136             etype = ATTR_MACHINE_ATTRIBUTE;
4137             break;
4138
4139           case Pragma_Linker_Alias:
4140             etype = ATTR_LINK_ALIAS;
4141             break;
4142
4143           case Pragma_Linker_Section:
4144             etype = ATTR_LINK_SECTION;
4145             break;
4146
4147           case Pragma_Weak_External:
4148             etype = ATTR_WEAK_EXTERNAL;
4149             break;
4150
4151           default:
4152             continue;
4153           }
4154
4155         attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4156         attr->next = attr_list;
4157         attr->type = etype;
4158         attr->name = gnu_arg0;
4159         attr->arg = gnu_arg1;
4160         attr->error_point
4161           = Present (Next (First (gnat_assoc)))
4162             ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4163         attr_list = attr;
4164       }
4165
4166   return attr_list;
4167 }
4168 \f
4169 /* Get the unpadded version of a GNAT type.  */
4170
4171 tree
4172 get_unpadded_type (gnat_entity)
4173      Entity_Id gnat_entity;
4174 {
4175   tree type = gnat_to_gnu_type (gnat_entity);
4176
4177   if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4178     type = TREE_TYPE (TYPE_FIELDS (type));
4179
4180   return type;
4181 }
4182 \f
4183 /* Called when we need to protect a variable object using a save_expr.  */
4184
4185 tree
4186 maybe_variable (gnu_operand, gnat_node)
4187      tree gnu_operand;
4188      Node_Id gnat_node;
4189 {
4190   if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4191       || TREE_CODE (gnu_operand) == SAVE_EXPR
4192       || TREE_CODE (gnu_operand) == NULL_EXPR)
4193     return gnu_operand;
4194
4195   /* If we will be generating code, make sure we are at the proper
4196      line number.  */
4197   if (! global_bindings_p () && ! TREE_CONSTANT (gnu_operand)
4198       && ! contains_placeholder_p (gnu_operand))
4199   set_lineno (gnat_node, 1);
4200
4201   if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4202     return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4203                    variable_size (TREE_OPERAND (gnu_operand, 0)));
4204   else
4205     return variable_size (gnu_operand);
4206 }
4207 \f
4208 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4209    type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4210    return the GCC tree to use for that expression.  GNU_NAME is the
4211    qualification to use if an external name is appropriate and DEFINITION is
4212    nonzero if this is a definition of GNAT_ENTITY.  If NEED_VALUE is nonzero,
4213    we need a result.  Otherwise, we are just elaborating this for
4214    side-effects.  If NEED_DEBUG is nonzero we need the symbol for debugging
4215    purposes even if it isn't needed for code generation.  */
4216
4217 static tree
4218 elaborate_expression (gnat_expr, gnat_entity, gnu_name, definition,
4219                       need_value, need_debug)
4220      Node_Id gnat_expr;
4221      Entity_Id gnat_entity;
4222      tree gnu_name;
4223      int definition;
4224      int need_value;
4225      int need_debug;
4226 {
4227   tree gnu_expr;
4228
4229   /* If we already elaborated this expression (e.g., it was involved
4230      in the definition of a private type), use the old value.  */
4231   if (present_gnu_tree (gnat_expr))
4232     return get_gnu_tree (gnat_expr);
4233
4234   /* If we don't need a value and this is static or a discriment, we
4235      don't need to do anything.  */
4236   else if (! need_value
4237            && (Is_OK_Static_Expression (gnat_expr)
4238                || (Nkind (gnat_expr) == N_Identifier
4239                    && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4240     return 0;
4241
4242   /* Otherwise, convert this tree to its GCC equivalant.  */
4243   gnu_expr
4244     = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4245                               gnu_name, definition, need_debug);
4246
4247   /* Save the expression in case we try to elaborate this entity again.
4248      Since this is not a DECL, don't check it.  If this is a constant,
4249      don't save it since GNAT_EXPR might be used more than once.  Also,
4250      don't save if it's a discriminant.  */
4251   if (! TREE_CONSTANT (gnu_expr) && ! contains_placeholder_p (gnu_expr))
4252     save_gnu_tree (gnat_expr, gnu_expr, 1);
4253
4254   return need_value ? gnu_expr : error_mark_node;
4255 }
4256
4257 /* Similar, but take a GNU expression.  */
4258
4259 static tree
4260 elaborate_expression_1 (gnat_expr, gnat_entity, gnu_expr, gnu_name, definition,
4261                         need_debug)
4262      Node_Id gnat_expr;
4263      Entity_Id gnat_entity;
4264      tree gnu_expr;
4265      tree gnu_name;
4266      int definition;
4267      int need_debug;
4268 {
4269   tree gnu_decl = 0;
4270   tree gnu_inner_expr = gnu_expr;
4271   int expr_variable;
4272   int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4273
4274   /* Strip any conversions to see if the expression is a readonly variable.
4275      ??? This really should remain readonly, but we have to think about
4276      the typing of the tree here.  */
4277   while (TREE_CODE (gnu_inner_expr) == NOP_EXPR
4278          && TREE_CODE (gnu_inner_expr) == CONVERT_EXPR)
4279     gnu_inner_expr = TREE_OPERAND (gnu_inner_expr, 0);
4280
4281   /* In most cases, we won't see a naked FIELD_DECL here because a
4282      discriminant reference will have been replaced with a COMPONENT_REF
4283      when the type is being elaborated.  However, there are some cases
4284      involving child types where we will.  So convert it to a COMPONENT_REF
4285      here.  We have to hope it will be at the highest level of the
4286      expression in these cases.  */
4287   if (TREE_CODE (gnu_expr) == FIELD_DECL)
4288     gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4289                       build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4290                       gnu_expr);
4291
4292
4293   /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4294      that is a constant, make a variable that is initialized to contain the
4295      bound when the package containing the definition is elaborated.  If
4296      this entity is defined at top level and a bound or discriminant value
4297      isn't a constant or a reference to a discriminant, replace the bound
4298      by the variable; otherwise use a SAVE_EXPR if needed.  Note that we
4299      rely here on the fact that an expression cannot contain both the
4300      discriminant and some other variable.  */
4301
4302   expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4303                    && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4304                          && TREE_READONLY (gnu_inner_expr))
4305                    && ! contains_placeholder_p (gnu_expr));
4306
4307   /* If this is a static expression or contains a discriminant, we don't
4308      need the variable for debugging (and can't elaborate anyway if a
4309      discriminant).  */
4310   if (need_debug
4311       && (Is_OK_Static_Expression (gnat_expr)
4312           || contains_placeholder_p (gnu_expr)))
4313     need_debug = 0;
4314
4315   /* Now create the variable if we need it.  */
4316   if (need_debug || (expr_variable && expr_global))
4317     {
4318       set_lineno (gnat_entity, ! global_bindings_p ());
4319       gnu_decl
4320         = create_var_decl (create_concat_name (gnat_entity,
4321                                                IDENTIFIER_POINTER (gnu_name)),
4322                            NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4323                            Is_Public (gnat_entity), ! definition, 0, 0);
4324     }
4325
4326   /* We only need to use this variable if we are in global context since GCC
4327      can do the right thing in the local case.  */
4328   if (expr_global && expr_variable)
4329     return gnu_decl;
4330   else
4331     return maybe_variable (gnu_expr, gnat_expr);
4332 }
4333 \f
4334 /* Create a record type that contains a field of TYPE with a starting bit
4335    position so that it is aligned to ALIGN bits and is SIZE bytes long.  */
4336
4337 tree
4338 make_aligning_type (type, align, size)
4339      tree type;
4340      int align;
4341      tree size;
4342 {
4343   tree record_type = make_node (RECORD_TYPE);
4344   tree place = build (PLACEHOLDER_EXPR, record_type);
4345   tree size_addr_place = convert (sizetype,
4346                                   build_unary_op (ADDR_EXPR, NULL_TREE,
4347                                                   place));
4348   tree name = TYPE_NAME (type);
4349   tree pos, field;
4350
4351   if (TREE_CODE (name) == TYPE_DECL)
4352     name = DECL_NAME (name);
4353
4354   TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4355
4356   /* The bit position is obtained by "and"ing the alignment minus 1
4357      with the two's complement of the address and  multiplying
4358      by the number of bits per unit.  Do all this in sizetype.  */
4359
4360   pos = size_binop (MULT_EXPR,
4361                     convert (bitsizetype,
4362                              size_binop (BIT_AND_EXPR, 
4363                                          size_diffop (size_zero_node,
4364                                                       size_addr_place),
4365                                          ssize_int ((align / BITS_PER_UNIT)
4366                                                     - 1))),
4367                     bitsize_unit_node);
4368
4369   field = create_field_decl (get_identifier ("F"), type, record_type,
4370                              1, size, pos, 1);
4371   DECL_BIT_FIELD (field) = 0;
4372
4373   finish_record_type (record_type, field, 1, 0);
4374   TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4375   TYPE_SIZE (record_type)
4376     = size_binop (PLUS_EXPR,
4377                   size_binop (MULT_EXPR, convert (bitsizetype, size),
4378                               bitsize_unit_node),
4379                   bitsize_int (align));
4380   TYPE_SIZE_UNIT (record_type)
4381     = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4382
4383   return record_type;
4384 }
4385 \f
4386 /* TYPE is a RECORD_TYPE with BLKmode that's being used as the field
4387    type of a packed record.  See if we can rewrite it as a record that has
4388    a non-BLKmode type, which we can pack tighter.  If so, return the
4389    new type.  If not, return the original type.  */
4390
4391 static tree
4392 make_packable_type (type)
4393      tree type;
4394 {
4395   tree new_type = make_node (RECORD_TYPE);
4396   tree field_list = NULL_TREE;
4397   tree old_field;
4398
4399   /* Copy the name and flags from the old type to that of the new and set
4400      the alignment to try for an integral type.  */
4401   TYPE_NAME (new_type) = TYPE_NAME (type);
4402   TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4403     = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4404   TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4405
4406   TYPE_ALIGN (new_type)
4407     = ((HOST_WIDE_INT) 1
4408        << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4409
4410   /* Now copy the fields, keeping the position and size.  */
4411   for (old_field = TYPE_FIELDS (type); old_field != 0;
4412        old_field = TREE_CHAIN (old_field))
4413     {
4414       tree new_field
4415         = create_field_decl (DECL_NAME (old_field), TREE_TYPE (old_field),
4416                              new_type, TYPE_PACKED (type),
4417                              DECL_SIZE (old_field),
4418                              bit_position (old_field),
4419                              ! DECL_NONADDRESSABLE_P (old_field));
4420
4421       DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4422       DECL_ORIGINAL_FIELD (new_field)
4423         = (DECL_ORIGINAL_FIELD (old_field) != 0
4424            ? DECL_ORIGINAL_FIELD (old_field) : old_field);
4425       TREE_CHAIN (new_field) = field_list;
4426       field_list = new_field;
4427     }
4428
4429   finish_record_type (new_type, nreverse (field_list), 1, 1);
4430   return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4431 }
4432 \f
4433 /* Ensure that TYPE has SIZE and ALIGN.  Make and return a new padded type
4434    if needed.  We have already verified that SIZE and TYPE are large enough.
4435
4436    GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4437    to issue a warning.
4438
4439    IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4440
4441    DEFINITION is nonzero if this type is being defined.
4442
4443    SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4444    set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4445    type.  */
4446
4447 static tree
4448 maybe_pad_type (type, size, align, gnat_entity, name_trailer,
4449                 is_user_type, definition, same_rm_size)
4450      tree type;
4451      tree size;
4452      unsigned int align;
4453      Entity_Id gnat_entity;
4454      const char *name_trailer;
4455      int is_user_type;
4456      int definition;
4457      int same_rm_size;
4458 {
4459   tree orig_size = TYPE_SIZE (type);
4460   tree record;
4461   tree field;
4462
4463   /* If TYPE is a padded type, see if it agrees with any size and alignment
4464      we were given.  If so, return the original type.  Otherwise, strip
4465      off the padding, since we will either be returning the inner type
4466      or repadding it.  If no size or alignment is specified, use that of
4467      the original padded type.  */
4468
4469   if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4470     {
4471       if ((size == 0
4472            || operand_equal_p (round_up (size,
4473                                          MAX (align, TYPE_ALIGN (type))),
4474                                round_up (TYPE_SIZE (type),
4475                                          MAX (align, TYPE_ALIGN (type))),
4476                                0))
4477           && (align == 0 || align == TYPE_ALIGN (type)))
4478         return type;
4479
4480       if (size == 0)
4481         size = TYPE_SIZE (type);
4482       if (align == 0)
4483         align = TYPE_ALIGN (type);
4484
4485       type = TREE_TYPE (TYPE_FIELDS (type));
4486       orig_size = TYPE_SIZE (type);
4487     }
4488
4489   /* If the size is either not being changed or is being made smaller (which
4490      is not done here (and is only valid for bitfields anyway), show the size
4491      isn't changing.  Likewise, clear the alignment if it isn't being
4492      changed.  Then return if we aren't doing anything.  */
4493
4494   if (size != 0
4495       && (operand_equal_p (size, orig_size, 0)
4496           || (TREE_CODE (orig_size) == INTEGER_CST
4497               && tree_int_cst_lt (size, orig_size))))
4498     size = 0;
4499
4500   if (align == TYPE_ALIGN (type))
4501     align = 0;
4502
4503   if (align == 0 && size == 0)
4504     return type;
4505
4506   /* We used to modify the record in place in some cases, but that could
4507      generate incorrect debugging information.  So make a new record
4508      type and name.  */
4509   record = make_node (RECORD_TYPE);
4510
4511   if (Present (gnat_entity))
4512     TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4513
4514   /* If we were making a type, complete the original type and give it a
4515      name.  */
4516   if (is_user_type)
4517     create_type_decl (get_entity_name (gnat_entity), type,
4518                       0, ! Comes_From_Source (gnat_entity),
4519                       ! (TYPE_NAME (type) != 0
4520                          && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4521                          && DECL_IGNORED_P (TYPE_NAME (type))));
4522
4523   /* If we are changing the alignment and the input type is a record with
4524      BLKmode and a small constant size, try to make a form that has an
4525      integral mode.  That might allow this record to have an integral mode,
4526      which will be much more efficient.  There is no point in doing this if a
4527      size is specified unless it is also smaller than the biggest alignment
4528      and it is incorrect to do this if the size of the original type is not a
4529      multiple of the alignment.  */
4530   if (align != 0
4531       && TREE_CODE (type) == RECORD_TYPE
4532       && TYPE_MODE (type) == BLKmode
4533       && host_integerp (orig_size, 1)
4534       && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4535       && (size == 0
4536           || (TREE_CODE (size) == INTEGER_CST
4537               && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4538       && tree_low_cst (orig_size, 1) % align == 0)
4539     type = make_packable_type (type);
4540
4541   field  = create_field_decl (get_identifier ("F"), type, record, 0,
4542                               NULL_TREE, bitsize_zero_node, 1);
4543
4544   DECL_INTERNAL_P (field) = 1;
4545   TYPE_SIZE (record) = size != 0 ? size : orig_size;
4546   TYPE_SIZE_UNIT (record)
4547     = convert (sizetype,
4548                size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4549                            bitsize_unit_node));
4550   TYPE_ALIGN (record) = align;
4551   TYPE_IS_PADDING_P (record) = 1;
4552   TYPE_VOLATILE (record)
4553     = Present (gnat_entity) && Is_Volatile (gnat_entity);
4554   finish_record_type (record, field, 1, 0);
4555
4556   /* Keep the RM_Size of the padded record as that of the old record
4557      if requested.  */
4558   TYPE_ADA_SIZE (record) = same_rm_size ? size : rm_size (type);
4559
4560   /* Unless debugging information isn't being written for the input type,
4561      write a record that shows what we are a subtype of and also make a
4562      variable that indicates our size, if variable. */
4563   if (TYPE_NAME (record) != 0
4564       && AGGREGATE_TYPE_P (type)
4565       && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4566           || ! DECL_IGNORED_P (TYPE_NAME (type))))
4567     {
4568       tree marker = make_node (RECORD_TYPE);
4569       tree name = DECL_NAME (TYPE_NAME (record));
4570       tree orig_name = TYPE_NAME (type);
4571
4572       if (TREE_CODE (orig_name) == TYPE_DECL)
4573         orig_name = DECL_NAME (orig_name);
4574
4575       TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4576       finish_record_type (marker,
4577                           create_field_decl (orig_name, integer_type_node,
4578                                              marker, 0, NULL_TREE, NULL_TREE,
4579                                              0),
4580                           0, 0);
4581
4582       if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4583         create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4584                          sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4585                          0);
4586     }
4587
4588   type = record;
4589
4590   if (TREE_CODE (orig_size) != INTEGER_CST
4591       && contains_placeholder_p (orig_size))
4592     orig_size = max_size (orig_size, 1);
4593
4594   /* If the size was widened explicitly, maybe give a warning.  */
4595   if (size != 0 && Present (gnat_entity)
4596       && ! operand_equal_p (size, orig_size, 0)
4597       && ! (TREE_CODE (size) == INTEGER_CST
4598             && TREE_CODE (orig_size) == INTEGER_CST
4599             && tree_int_cst_lt (size, orig_size)))
4600     {
4601       Node_Id gnat_error_node = Empty;
4602
4603       if (Is_Packed_Array_Type (gnat_entity))
4604         gnat_entity = Associated_Node_For_Itype (gnat_entity);
4605
4606       if ((Ekind (gnat_entity) == E_Component
4607            || Ekind (gnat_entity) == E_Discriminant)
4608           && Present (Component_Clause (gnat_entity)))
4609         gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4610       else if (Present (Size_Clause (gnat_entity)))
4611         gnat_error_node = Expression (Size_Clause (gnat_entity));
4612
4613       /* Generate message only for entities that come from source, since
4614          if we have an entity created by expansion, the message will be
4615          generated for some other corresponding source entity.  */
4616       if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4617         post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4618                             gnat_entity,
4619                             size_diffop (size, orig_size));
4620
4621       else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4622         post_error_ne_tree ("component of& padded{ by ^ bits}?",
4623                             gnat_entity, gnat_entity,
4624                             size_diffop (size, orig_size));
4625     }
4626
4627   return type;
4628 }
4629 \f
4630 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4631    the value passed against the list of choices.  */
4632
4633 tree
4634 choices_to_gnu (operand, choices)
4635      tree operand;
4636      Node_Id choices;
4637 {
4638   Node_Id choice;
4639   Node_Id gnat_temp;
4640   tree result = integer_zero_node;
4641   tree this_test, low = 0, high = 0, single = 0;
4642
4643   for (choice = First (choices); Present (choice); choice = Next (choice))
4644     {
4645       switch (Nkind (choice))
4646         {
4647         case N_Range:
4648           low = gnat_to_gnu (Low_Bound (choice));
4649           high = gnat_to_gnu (High_Bound (choice));
4650
4651           /* There's no good type to use here, so we might as well use
4652              integer_type_node.  */
4653           this_test
4654             = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4655                                build_binary_op (GE_EXPR, integer_type_node,
4656                                                 operand, low),
4657                                build_binary_op (LE_EXPR, integer_type_node,
4658                                                 operand, high));
4659
4660           break;
4661
4662         case N_Subtype_Indication:
4663           gnat_temp = Range_Expression (Constraint (choice));
4664           low = gnat_to_gnu (Low_Bound (gnat_temp));
4665           high = gnat_to_gnu (High_Bound (gnat_temp));
4666
4667           this_test
4668             = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4669                                build_binary_op (GE_EXPR, integer_type_node,
4670                                                 operand, low),
4671                                build_binary_op (LE_EXPR, integer_type_node,
4672                                                 operand, high));
4673           break;
4674
4675         case N_Identifier:
4676         case N_Expanded_Name:
4677           /* This represents either a subtype range, an enumeration
4678              literal, or a constant  Ekind says which.  If an enumeration
4679              literal or constant, fall through to the next case.  */
4680           if (Ekind (Entity (choice)) != E_Enumeration_Literal
4681               && Ekind (Entity (choice)) != E_Constant)
4682             {
4683               tree type = gnat_to_gnu_type (Entity (choice));
4684
4685               low = TYPE_MIN_VALUE (type);
4686               high = TYPE_MAX_VALUE (type);
4687
4688               this_test
4689                 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4690                                    build_binary_op (GE_EXPR, integer_type_node,
4691                                                     operand, low),
4692                                    build_binary_op (LE_EXPR, integer_type_node,
4693                                                     operand, high));
4694               break;
4695             }
4696           /* ... fall through ... */
4697         case N_Character_Literal:
4698         case N_Integer_Literal:
4699           single = gnat_to_gnu (choice);
4700           this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4701                                        single);
4702           break;
4703
4704         case N_Others_Choice:
4705           this_test = integer_one_node;
4706           break;
4707
4708         default:
4709           gigi_abort (114);
4710         }
4711
4712       result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4713                                 result, this_test);
4714     }
4715
4716   return result;
4717 }
4718 \f
4719 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4720    placed in GNU_RECORD_TYPE.
4721
4722    PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4723    record has a Component_Alignment of Storage_Unit.
4724
4725    DEFINITION is nonzero if this field is for a record being defined.  */
4726
4727 static tree
4728 gnat_to_gnu_field (gnat_field, gnu_record_type, packed, definition)
4729      Entity_Id gnat_field;
4730      tree gnu_record_type;
4731      int packed;
4732      int definition;
4733 {
4734   tree gnu_field_id = get_entity_name (gnat_field);
4735   tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4736   tree gnu_orig_field_type = gnu_field_type;
4737   tree gnu_pos = 0;
4738   tree gnu_size = 0;
4739   tree gnu_field;
4740   int needs_strict_alignment
4741     = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
4742        || Is_Volatile (gnat_field));
4743
4744   /* If this field requires strict alignment pretend it isn't packed.  */
4745   if (needs_strict_alignment)
4746     packed = 0;
4747
4748   /* For packed records, this is one of the few occasions on which we use
4749      the official RM size for discrete or fixed-point components, instead
4750      of the normal GNAT size stored in Esize. See description in Einfo:
4751      "Handling of Type'Size Values" for further details.  */
4752
4753   if (packed == 1)
4754     gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4755                               gnat_field, FIELD_DECL, 0, 1);
4756
4757   if (Known_Static_Esize (gnat_field))
4758     gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4759                               gnat_field, FIELD_DECL, 0, 1);
4760
4761   /* If we are packing this record and the field type is also a record
4762      that's BLKmode and with a small constant size, see if we can get a
4763      better form of the type that allows more packing.  If we can, show
4764      a size was specified for it if there wasn't one so we know to
4765      make this a bitfield and avoid making things wider.  */
4766   if (packed && TREE_CODE (gnu_field_type) == RECORD_TYPE
4767       && TYPE_MODE (gnu_field_type) == BLKmode
4768       && host_integerp (TYPE_SIZE (gnu_field_type), 1)
4769       && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0)
4770     {
4771       gnu_field_type = make_packable_type (gnu_field_type);
4772
4773       if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
4774         gnu_size = rm_size (gnu_field_type);
4775     }
4776
4777   if (Present (Component_Clause (gnat_field)))
4778     {
4779       gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
4780       gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4781                                 gnat_field, FIELD_DECL, 0, 1);
4782
4783       /* Ensure the position does not overlap with the parent subtype,
4784          if there is one.  */
4785       if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
4786         {
4787           tree gnu_parent
4788             = gnat_to_gnu_type (Parent_Subtype
4789                                 (Underlying_Type (Scope (gnat_field))));
4790
4791           if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
4792               && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
4793             {
4794               post_error_ne_tree
4795                 ("offset of& must be beyond parent{, minimum allowed is ^}",
4796                  First_Bit (Component_Clause (gnat_field)), gnat_field,
4797                  TYPE_SIZE_UNIT (gnu_parent));
4798             }
4799         }
4800
4801       /* If this field needs strict alignment, ensure the record is
4802          sufficiently aligned and that that position and size are
4803          consistent with the alignment.  */
4804       if (needs_strict_alignment)
4805         {
4806           tree gnu_min_size = round_up (rm_size (gnu_field_type),
4807                                         TYPE_ALIGN (gnu_field_type));
4808
4809           TYPE_ALIGN (gnu_record_type)
4810             = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
4811
4812           /* If Atomic, the size must match exactly and if aliased, the size
4813              must not be less than the rounded size.  */
4814           if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
4815               && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
4816             {
4817               post_error_ne_tree
4818                 ("atomic field& must be natural size of type{ (^)}",
4819                  Last_Bit (Component_Clause (gnat_field)), gnat_field,
4820                  TYPE_SIZE (gnu_field_type));
4821
4822               gnu_size = 0;
4823             }
4824
4825           else if (Is_Aliased (gnat_field)
4826                    && gnu_size != 0
4827                    && tree_int_cst_lt (gnu_size, gnu_min_size))
4828             {
4829               post_error_ne_tree
4830                 ("size of aliased field& too small{, minimum required is ^}",
4831                  Last_Bit (Component_Clause (gnat_field)), gnat_field,
4832                  gnu_min_size);
4833               gnu_size = 0;
4834             }
4835
4836           if (! integer_zerop (size_binop 
4837                                (TRUNC_MOD_EXPR, gnu_pos,
4838                                 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
4839             {
4840               if (Is_Aliased (gnat_field))
4841                 post_error_ne_num
4842                   ("position of aliased field& must be multiple of ^ bits",
4843                    Component_Clause (gnat_field), gnat_field,
4844                    TYPE_ALIGN (gnu_field_type));
4845
4846               else if (Is_Volatile (gnat_field))
4847                 post_error_ne_num
4848                   ("position of volatile field& must be multiple of ^ bits",
4849                    First_Bit (Component_Clause (gnat_field)), gnat_field,
4850                    TYPE_ALIGN (gnu_field_type));
4851
4852               else if (Strict_Alignment (Etype (gnat_field)))
4853                 post_error_ne_num
4854   ("position of & with aliased or tagged components not multiple of ^ bits",
4855                    First_Bit (Component_Clause (gnat_field)), gnat_field,
4856                    TYPE_ALIGN (gnu_field_type));
4857               else
4858                 gigi_abort (124);
4859
4860               gnu_pos = 0;
4861             }
4862
4863           /* If an error set the size to zero, show we have no position
4864              either.  */
4865           if (gnu_size == 0)
4866             gnu_pos = 0;
4867         }
4868
4869       if (Is_Atomic (gnat_field))
4870         check_ok_for_atomic (gnu_field_type, gnat_field, 0);
4871
4872       if (gnu_pos !=0 && TYPE_MODE (gnu_field_type) == BLKmode
4873           && (! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
4874                                            bitsize_unit_node))))
4875         {
4876           /* Try to see if we can make this a packable type.   If we
4877              can, it's OK. */
4878           if (TREE_CODE (gnu_field_type) == RECORD_TYPE)
4879             gnu_field_type = make_packable_type (gnu_field_type);
4880
4881           if (TYPE_MODE (gnu_field_type) == BLKmode)
4882             {
4883               post_error_ne ("fields of& must start at storage unit boundary",
4884                              First_Bit (Component_Clause (gnat_field)),
4885                              Etype (gnat_field));
4886               gnu_pos = 0;
4887             }
4888         }
4889     }
4890
4891   /* If the record has rep clauses and this is the tag field, make a rep
4892      clause for it as well.  */
4893   else if (Has_Specified_Layout (Scope (gnat_field))
4894            && Chars (gnat_field) == Name_uTag)
4895     {
4896       gnu_pos = bitsize_zero_node;
4897       gnu_size = TYPE_SIZE (gnu_field_type);
4898     }
4899
4900   /* We need to make the size the maximum for the type if it is
4901      self-referential and an unconstrained type.  */
4902   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4903       && gnu_size == 0
4904       && ! TREE_CONSTANT (TYPE_SIZE (gnu_field_type))
4905       && contains_placeholder_p (TYPE_SIZE (gnu_field_type))
4906       && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
4907     gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
4908
4909   /* If no size is specified (or if there was an error), don't specify a
4910      position.  */
4911   if (gnu_size == 0)
4912     gnu_pos = 0;
4913   else
4914     {
4915       /* Unless this field is aliased, we can remove any left-justified
4916          modular type since it's only needed in the unchecked conversion
4917          case, which doesn't apply here.  */
4918       if (! needs_strict_alignment
4919           && TREE_CODE (gnu_field_type) == RECORD_TYPE
4920           && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4921         gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4922
4923       gnu_field_type
4924         = make_type_from_size (gnu_field_type, gnu_size,
4925                                Has_Biased_Representation (gnat_field));
4926       gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
4927                                        gnat_field, "PAD", 0, definition, 1);
4928     }
4929
4930   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4931       && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
4932     gigi_abort (118);
4933
4934   set_lineno (gnat_field, 0);
4935   gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
4936                                  packed, gnu_size, gnu_pos,
4937                                  Is_Aliased (gnat_field));
4938
4939   TREE_THIS_VOLATILE (gnu_field) = Is_Volatile (gnat_field);
4940
4941   if (Ekind (gnat_field) == E_Discriminant)
4942     DECL_DISCRIMINANT_NUMBER (gnu_field)
4943       = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
4944
4945   return gnu_field;
4946 }
4947 \f
4948 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
4949    of GCC trees for fields that are in the record and have already been
4950    processed.  When called from gnat_to_gnu_entity during the processing of a
4951    record type definition, the GCC nodes for the discriminants will be on
4952    the chain.  The other calls to this function are recursive calls from
4953    itself for the Component_List of a variant and the chain is empty.
4954
4955    PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
4956    for a record type with "pragma component_alignment (storage_unit)".
4957
4958    FINISH_RECORD is nonzero if this call will supply all of the remaining
4959    fields of the record.
4960
4961    P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
4962    with a rep clause is to be added.  If it is nonzero, that is all that
4963    should be done with such fields.
4964
4965    CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
4966    before laying out the record.  This means the alignment only serves
4967    to force fields to be bitfields, but not require the record to be
4968    that aligned.  This is used for variants.
4969
4970    ALL_REP, if nonzero, means that a rep clause was found for all the
4971    fields.  This simplifies the logic since we know we're not in the mixed
4972    case.
4973
4974    The processing of the component list fills in the chain with all of the
4975    fields of the record and then the record type is finished.  */
4976
4977 static void
4978 components_to_record (gnu_record_type, component_list, gnu_field_list, packed,
4979                       definition, p_gnu_rep_list, cancel_alignment, all_rep)
4980      tree gnu_record_type;
4981      Node_Id component_list;
4982      tree gnu_field_list;
4983      int packed;
4984      int definition;
4985      tree *p_gnu_rep_list;
4986      int cancel_alignment;
4987      int all_rep;
4988 {
4989   Node_Id component_decl;
4990   Entity_Id gnat_field;
4991   Node_Id variant_part;
4992   Node_Id variant;
4993   tree gnu_our_rep_list = NULL_TREE;
4994   tree gnu_field, gnu_last;
4995   int layout_with_rep = 0;
4996
4997   /* For each variable within each component declaration create a GCC field
4998      and add it to the list, skipping any pragmas in the list.  */
4999
5000   if (Present (Component_Items (component_list)))
5001     for (component_decl = First_Non_Pragma (Component_Items (component_list));
5002          Present (component_decl);
5003          component_decl = Next_Non_Pragma (component_decl))
5004       {
5005         gnat_field = Defining_Entity (component_decl);
5006
5007         if (Chars (gnat_field) == Name_uParent)
5008           gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5009         else
5010           {
5011             gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5012                                            packed, definition);
5013
5014             /* If this is the _Tag field, put it before any discriminants,
5015                instead of after them as is the case for all other fields.  */
5016             if (Chars (gnat_field) == Name_uTag)
5017               gnu_field_list = chainon (gnu_field_list, gnu_field);
5018             else
5019               {
5020                 TREE_CHAIN (gnu_field) = gnu_field_list;
5021                 gnu_field_list = gnu_field;
5022               }
5023           }
5024
5025           save_gnu_tree (gnat_field, gnu_field, 0);
5026         }
5027
5028   /* At the end of the component list there may be a variant part.  */
5029   variant_part = Variant_Part (component_list);
5030
5031   /* If this is an unchecked union, each variant must have exactly one
5032      component, each of which becomes one component of this union.  */
5033   if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5034     for (variant = First_Non_Pragma (Variants (variant_part));
5035          Present (variant);
5036          variant = Next_Non_Pragma (variant))
5037       {
5038         component_decl
5039           = First_Non_Pragma (Component_Items (Component_List (variant)));
5040         gnat_field = Defining_Entity (component_decl);
5041         gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5042                                        definition);
5043         TREE_CHAIN (gnu_field) = gnu_field_list;
5044         gnu_field_list = gnu_field;
5045         save_gnu_tree (gnat_field, gnu_field, 0);
5046       }
5047
5048   /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5049      mutually exclusive and should go in the same memory.  To do this we need
5050      to treat each variant as a record whose elements are created from the
5051      component list for the variant.  So here we create the records from the
5052      lists for the variants and put them all into the QUAL_UNION_TYPE.  */
5053   else if (Present (variant_part))
5054     {
5055       tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5056       Node_Id variant;
5057       tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5058       tree gnu_union_field;
5059       tree gnu_variant_list = NULL_TREE;
5060       tree gnu_name = TYPE_NAME (gnu_record_type);
5061       tree gnu_var_name
5062         = concat_id_with_name
5063           (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5064            "XVN");
5065
5066       if (TREE_CODE (gnu_name) == TYPE_DECL)
5067         gnu_name = DECL_NAME (gnu_name);
5068
5069       TYPE_NAME (gnu_union_type)
5070         = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5071       TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5072
5073       for (variant = First_Non_Pragma (Variants (variant_part));
5074            Present (variant);
5075            variant = Next_Non_Pragma (variant))
5076         {
5077           tree gnu_variant_type = make_node (RECORD_TYPE);
5078           tree gnu_inner_name;
5079           tree gnu_qual;
5080
5081           Get_Variant_Encoding (variant);
5082           gnu_inner_name = get_identifier (Name_Buffer);
5083           TYPE_NAME (gnu_variant_type)
5084             = concat_id_with_name (TYPE_NAME (gnu_union_type),
5085                                    IDENTIFIER_POINTER (gnu_inner_name));
5086
5087           /* Set the alignment of the inner type in case we need to make
5088              inner objects into bitfields, but then clear it out
5089              so the record actually gets only the alignment required.  */
5090           TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5091           TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5092           components_to_record (gnu_variant_type, Component_List (variant),
5093                                 NULL_TREE, packed, definition,
5094                                 &gnu_our_rep_list, 1, all_rep);
5095
5096           gnu_qual = choices_to_gnu (gnu_discriminant,
5097                                      Discrete_Choices (variant));
5098
5099           Set_Present_Expr (variant, annotate_value (gnu_qual));
5100           gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5101                                          gnu_union_type, 0, 0, 0, 1);
5102           DECL_INTERNAL_P (gnu_field) = 1;
5103           DECL_QUALIFIER (gnu_field) = gnu_qual;
5104           TREE_CHAIN (gnu_field) = gnu_variant_list;
5105           gnu_variant_list = gnu_field;
5106         }
5107
5108       /* We can delete any empty variants from the end.  This may leave none
5109          left.  Note we cannot delete variants from anywhere else.  */
5110       while (gnu_variant_list != 0
5111              && TYPE_FIELDS (TREE_TYPE (gnu_variant_list)) == 0)
5112         gnu_variant_list = TREE_CHAIN (gnu_variant_list);
5113
5114       /* Only make the QUAL_UNION_TYPE if there are any non-empty variants.  */
5115       if (gnu_variant_list != 0)
5116         {
5117           finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5118                               0, 0);
5119
5120           gnu_union_field
5121             = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5122                                  packed,
5123                                  all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5124                                  all_rep ? bitsize_zero_node : 0, 1);
5125
5126           DECL_INTERNAL_P (gnu_union_field) = 1;
5127           TREE_CHAIN (gnu_union_field) = gnu_field_list;
5128           gnu_field_list = gnu_union_field;
5129         }
5130     }
5131
5132   /* Scan GNU_FIELD_LIST and see if any fields have rep clauses.  If they
5133      do, pull them out and put them into GNU_OUR_REP_LIST.  We have to do this
5134      in a separate pass since we want to handle the discriminants but can't
5135      play with them until we've used them in debugging data above.
5136
5137      ??? Note: if we then reorder them, debugging information will be wrong,
5138      but there's nothing that can be done about this at the moment.  */
5139
5140   for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5141     {
5142       if (DECL_FIELD_OFFSET (gnu_field) != 0)
5143         {
5144           tree gnu_next = TREE_CHAIN (gnu_field);
5145
5146           if (gnu_last == 0)
5147             gnu_field_list = gnu_next;
5148           else
5149             TREE_CHAIN (gnu_last) = gnu_next;
5150
5151           TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5152           gnu_our_rep_list = gnu_field;
5153           gnu_field = gnu_next;
5154         }
5155       else
5156         {
5157           gnu_last = gnu_field;
5158           gnu_field = TREE_CHAIN (gnu_field);
5159         }
5160     }
5161
5162   /* If we have any items in our rep'ed field list, it is not the case that all
5163      the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5164      set it and ignore the items.  Otherwise, sort the fields by bit position
5165      and put them into their own record if we have any fields without
5166      rep clauses. */
5167   if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5168     *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5169   else if (gnu_our_rep_list != 0)
5170     {
5171       tree gnu_rep_type
5172         = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5173       int len = list_length (gnu_our_rep_list);
5174       tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5175       int i;
5176
5177       /* Set DECL_SECTION_NAME to increasing integers so we have a
5178          stable sort.  */
5179       for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5180            gnu_field = TREE_CHAIN (gnu_field), i++)
5181         {
5182           gnu_arr[i] = gnu_field;
5183           DECL_SECTION_NAME (gnu_field) = size_int (i);
5184         }
5185
5186       qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5187
5188       /* Put the fields in the list in order of increasing position, which
5189          means we start from the end.  */
5190       gnu_our_rep_list = NULL_TREE;
5191       for (i = len - 1; i >= 0; i--)
5192         {
5193           TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5194           gnu_our_rep_list = gnu_arr[i];
5195           DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5196           DECL_SECTION_NAME (gnu_arr[i]) = 0;
5197         }
5198
5199       if (gnu_field_list != 0)
5200         {
5201           finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5202           gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5203                                          gnu_record_type, 0, 0, 0, 1);
5204           DECL_INTERNAL_P (gnu_field) = 1;
5205           gnu_field_list = chainon (gnu_field_list, gnu_field);
5206         }
5207       else
5208         {
5209           layout_with_rep = 1;
5210           gnu_field_list = nreverse (gnu_our_rep_list);
5211         }
5212     }
5213
5214   if (cancel_alignment)
5215     TYPE_ALIGN (gnu_record_type) = 0;
5216
5217   finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5218                       layout_with_rep, 0);
5219 }
5220 \f
5221 /* Called via qsort from the above.  Returns -1, 1, depending on the
5222    bit positions and ordinals of the two fields.  */
5223
5224 static int
5225 compare_field_bitpos (rt1, rt2)
5226      const PTR rt1;
5227      const PTR rt2;
5228 {
5229   tree *t1 = (tree *) rt1;
5230   tree *t2 = (tree *) rt2;
5231
5232   if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5233     return
5234       (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5235        ? -1 : 1);
5236   else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5237     return -1;
5238   else
5239     return 1;
5240 }
5241 \f
5242 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5243    placed into an Esize, Component_Bit_Offset, or Component_Size value
5244    in the GNAT tree.  */
5245
5246 static Uint
5247 annotate_value (gnu_size)
5248      tree gnu_size;
5249 {
5250   int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5251   TCode tcode;
5252   Node_Ref_Or_Val ops[3];
5253   int i;
5254   unsigned int size;
5255
5256   /* If we do not return inside this switch, TCODE will be set to the
5257      code to use for a Create_Node operand and LEN (set above) will be
5258      the number of recursive calls for us to make.  */
5259
5260   switch (TREE_CODE (gnu_size))
5261     {
5262     case INTEGER_CST:
5263       if (TREE_OVERFLOW (gnu_size))
5264         return No_Uint;
5265
5266       /* This may have come from a conversion from some smaller type,
5267          so ensure this is in bitsizetype.  */
5268       gnu_size = convert (bitsizetype, gnu_size);
5269
5270       /* For negative values, use NEGATE_EXPR of the supplied value.  */
5271       if (tree_int_cst_sgn (gnu_size) < 0)
5272         {
5273           /* The rediculous code below is to handle the case of the largest
5274              negative integer.  */
5275           tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5276           int adjust = 0;
5277           tree temp;
5278
5279           if (TREE_CONSTANT_OVERFLOW (negative_size))
5280             {
5281               negative_size
5282                 = size_binop (MINUS_EXPR, bitsize_zero_node,
5283                               size_binop (PLUS_EXPR, gnu_size,
5284                                           bitsize_one_node));
5285               adjust = 1;
5286             }
5287
5288           temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5289           if (adjust)
5290             temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5291
5292           return annotate_value (temp);
5293         }
5294
5295       if (! host_integerp (gnu_size, 1))
5296         return No_Uint;
5297
5298       size = tree_low_cst (gnu_size, 1);
5299
5300       /* This peculiar test is to make sure that the size fits in an int
5301          on machines where HOST_WIDE_INT is not "int".  */
5302       if (tree_low_cst (gnu_size, 1) == size)
5303         return UI_From_Int (size);
5304       else
5305         return No_Uint;
5306
5307     case COMPONENT_REF:
5308       /* The only case we handle here is a simple discriminant reference.  */
5309       if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5310           && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5311           && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5312         return Create_Node (Discrim_Val,
5313                             annotate_value (DECL_DISCRIMINANT_NUMBER
5314                                             (TREE_OPERAND (gnu_size, 1))),
5315                             No_Uint, No_Uint);
5316       else
5317         return No_Uint;
5318
5319     case NOP_EXPR:  case CONVERT_EXPR:   case NON_LVALUE_EXPR:
5320       return annotate_value (TREE_OPERAND (gnu_size, 0));
5321
5322       /* Now just list the operations we handle.  */
5323     case COND_EXPR:             tcode = Cond_Expr; break;
5324     case PLUS_EXPR:             tcode = Plus_Expr; break;
5325     case MINUS_EXPR:            tcode = Minus_Expr; break;
5326     case MULT_EXPR:             tcode = Mult_Expr; break;
5327     case TRUNC_DIV_EXPR:        tcode = Trunc_Div_Expr; break;
5328     case CEIL_DIV_EXPR:         tcode = Ceil_Div_Expr; break;
5329     case FLOOR_DIV_EXPR:        tcode = Floor_Div_Expr; break;
5330     case TRUNC_MOD_EXPR:        tcode = Trunc_Mod_Expr; break;
5331     case CEIL_MOD_EXPR:         tcode = Ceil_Mod_Expr; break;
5332     case FLOOR_MOD_EXPR:        tcode = Floor_Mod_Expr; break;
5333     case EXACT_DIV_EXPR:        tcode = Exact_Div_Expr; break;
5334     case NEGATE_EXPR:           tcode = Negate_Expr; break;
5335     case MIN_EXPR:              tcode = Min_Expr; break;
5336     case MAX_EXPR:              tcode = Max_Expr; break;
5337     case ABS_EXPR:              tcode = Abs_Expr; break;
5338     case TRUTH_ANDIF_EXPR:      tcode = Truth_Andif_Expr; break;
5339     case TRUTH_ORIF_EXPR:       tcode = Truth_Orif_Expr; break;
5340     case TRUTH_AND_EXPR:        tcode = Truth_And_Expr; break;
5341     case TRUTH_OR_EXPR:         tcode = Truth_Or_Expr; break;
5342     case TRUTH_XOR_EXPR:        tcode = Truth_Xor_Expr; break;
5343     case TRUTH_NOT_EXPR:        tcode = Truth_Not_Expr; break;
5344     case LT_EXPR:               tcode = Lt_Expr; break;
5345     case LE_EXPR:               tcode = Le_Expr; break;
5346     case GT_EXPR:               tcode = Gt_Expr; break;
5347     case GE_EXPR:               tcode = Ge_Expr; break;
5348     case EQ_EXPR:               tcode = Eq_Expr; break;
5349     case NE_EXPR:               tcode = Ne_Expr; break;
5350
5351     default:
5352       return No_Uint;
5353     }
5354
5355   /* Now get each of the operands that's relevant for this code.  If any
5356      cannot be expressed as a repinfo node, say we can't.  */
5357   for (i = 0; i < 3; i++)
5358     ops[i] = No_Uint;
5359
5360   for (i = 0; i < len; i++)
5361     {
5362       ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5363       if (ops[i] == No_Uint)
5364         return No_Uint;
5365     }
5366
5367   return Create_Node (tcode, ops[0], ops[1], ops[2]);
5368 }
5369
5370 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5371    GCC type, set Component_Bit_Offset and Esize to the position and size
5372    used by Gigi.  */
5373
5374 static void
5375 annotate_rep (gnat_entity, gnu_type)
5376      Entity_Id gnat_entity;
5377      tree gnu_type;
5378 {
5379   tree gnu_list;
5380   tree gnu_entry;
5381   Entity_Id gnat_field;
5382
5383   /* We operate by first making a list of all field and their positions
5384      (we can get the sizes easily at any time) by a recursive call
5385      and then update all the sizes into the tree.  */
5386   gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5387                                       size_zero_node, bitsize_zero_node);
5388
5389   for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5390        gnat_field = Next_Entity (gnat_field))
5391     if ((Ekind (gnat_field) == E_Component
5392          || (Ekind (gnat_field) == E_Discriminant
5393              && ! Is_Unchecked_Union (Scope (gnat_field))))
5394         && 0 != (gnu_entry = purpose_member (gnat_to_gnu_entity (gnat_field,
5395                                                                  NULL_TREE, 0),
5396                                              gnu_list)))
5397       {
5398         Set_Component_Bit_Offset
5399           (gnat_field,
5400            annotate_value (bit_from_pos
5401                            (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5402                             TREE_VALUE (TREE_VALUE (gnu_entry)))));
5403
5404         Set_Esize (gnat_field,
5405                    annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5406       }
5407 }
5408
5409 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is
5410    the FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the
5411    byte position and TREE_VALUE being the bit position.  GNU_POS is to
5412    be added to the position, GNU_BITPOS to the bit position, and GNU_LIST
5413    is the entries so far.  */
5414
5415 static tree
5416 compute_field_positions (gnu_type, gnu_list, gnu_pos, gnu_bitpos)
5417      tree gnu_type;
5418      tree gnu_list;
5419      tree gnu_pos;
5420      tree gnu_bitpos;
5421 {
5422   tree gnu_field;
5423   tree gnu_result = gnu_list;
5424
5425   for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5426        gnu_field = TREE_CHAIN (gnu_field))
5427     {
5428       tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5429                                         DECL_FIELD_BIT_OFFSET (gnu_field));
5430       tree gnu_our_pos = size_binop (PLUS_EXPR, gnu_pos,
5431                                      DECL_FIELD_OFFSET (gnu_field));
5432
5433       gnu_result
5434         = tree_cons (gnu_field,
5435                      tree_cons (gnu_our_pos, gnu_our_bitpos, NULL_TREE),
5436                      gnu_result);
5437
5438       if (DECL_INTERNAL_P (gnu_field))
5439         gnu_result
5440           = compute_field_positions (TREE_TYPE (gnu_field),
5441                                      gnu_result, gnu_our_pos, gnu_our_bitpos);
5442     }
5443
5444   return gnu_result;
5445 }
5446 \f
5447 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5448    corresponding to GNAT_OBJECT.  If size is valid, return a tree corresponding
5449    to its value.  Otherwise return 0.  KIND is VAR_DECL is we are specifying
5450    the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5451    for the size of a field.  COMPONENT_P is true if we are being called
5452    to process the Component_Size of GNAT_OBJECT.  This is used for error
5453    message handling and to indicate to use the object size of GNU_TYPE.
5454    ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5455    it means that a size of zero should be treated as an unspecified size.  */
5456
5457 static tree
5458 validate_size (uint_size, gnu_type, gnat_object, kind, component_p, zero_ok)
5459      Uint uint_size;
5460      tree gnu_type;
5461      Entity_Id gnat_object;
5462      enum tree_code kind;
5463      int component_p;
5464      int zero_ok;
5465 {
5466   Node_Id gnat_error_node;
5467   tree type_size
5468     = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5469   tree size;
5470
5471   if (type_size != 0 && TREE_CODE (type_size) != INTEGER_CST
5472       && contains_placeholder_p (type_size))
5473     type_size = max_size (type_size, 1);
5474
5475   if (TYPE_FAT_POINTER_P (gnu_type))
5476     type_size = bitsize_int (POINTER_SIZE);
5477
5478   if ((Ekind (gnat_object) == E_Component
5479        || Ekind (gnat_object) == E_Discriminant)
5480       && Present (Component_Clause (gnat_object)))
5481     gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5482   else if (Present (Size_Clause (gnat_object)))
5483     gnat_error_node = Expression (Size_Clause (gnat_object));
5484   else
5485     gnat_error_node = gnat_object;
5486
5487   /* Don't give errors on packed array types; we'll be giving the error on
5488      the type itself soon enough.  */
5489   if (Is_Packed_Array_Type (gnat_object))
5490     gnat_error_node = Empty;
5491
5492   /* Get the size as a tree.  Return 0 if none was specified, either because
5493      Esize was not Present or if the specified size was zero.  Give an error
5494      if a size was specified, but cannot be represented as in sizetype.  If
5495      the size is negative, it was a back-annotation of a variable size and
5496      should be treated as not specified.  */
5497   if (No (uint_size) || uint_size == No_Uint)
5498     return 0;
5499
5500   size = UI_To_gnu (uint_size, bitsizetype);
5501   if (TREE_OVERFLOW (size))
5502     {
5503       if (component_p)
5504         post_error_ne ("component size of & is too large",
5505                        gnat_error_node, gnat_object);
5506       else
5507         post_error_ne ("size of & is too large", gnat_error_node, gnat_object);
5508
5509       return 0;
5510     }
5511
5512   /* Ignore a negative size since that corresponds to our back-annotation.
5513      Also ignore a zero size unless a size clause exists.  */
5514   else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
5515       return 0;
5516
5517   /* The size of objects is always a multiple of a byte.  */
5518   if (kind == VAR_DECL
5519       && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5520                                       bitsize_unit_node)))
5521     {
5522       if (component_p)
5523         post_error_ne ("component size for& is not a multiple of Storage_Unit",
5524                        gnat_error_node, gnat_object);
5525       else
5526         post_error_ne ("size for& is not a multiple of Storage_Unit",
5527                        gnat_error_node, gnat_object);
5528       return 0;
5529     }
5530
5531   /* If this is an integral type, the front-end has verified the size, so we
5532      need not do it here (which would entail checking against the bounds).
5533      However, if this is an aliased object, it may not be smaller than the
5534      type of the object.  */
5535   if (INTEGRAL_TYPE_P (gnu_type) && ! TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
5536       && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5537     return size;
5538
5539   /* If the object is a record that contains a template, add the size of
5540      the template to the specified size.  */
5541   if (TREE_CODE (gnu_type) == RECORD_TYPE
5542       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5543     size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5544
5545   /* If the size of the object is a constant, the new size must not be
5546      smaller.  */
5547   if (TREE_CODE (type_size) != INTEGER_CST
5548       || TREE_OVERFLOW (type_size)
5549       || tree_int_cst_lt (size, type_size))
5550     {
5551       if (component_p)
5552         post_error_ne_tree
5553           ("component size for& too small{, minimum allowed is ^}",
5554            gnat_error_node, gnat_object, type_size);
5555       else
5556         post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5557                             gnat_error_node, gnat_object, type_size);
5558
5559       if (kind == VAR_DECL && ! component_p
5560           && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5561           && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5562         post_error_ne_tree_2
5563           ("\\size of ^ rounded up to multiple of alignment (^ bits)",
5564            gnat_error_node, gnat_object, rm_size (gnu_type),
5565            TYPE_ALIGN (gnu_type));
5566
5567       else if (INTEGRAL_TYPE_P (gnu_type))
5568         post_error_ne ("\\size would be legal if & were not aliased!",
5569                        gnat_error_node, gnat_object);
5570
5571       return 0;
5572     }
5573
5574   return size;
5575 }
5576 \f
5577 /* Similarly, but both validate and process a value of RM_Size.  This
5578    routine is only called for types.  */
5579
5580 static void
5581 set_rm_size (uint_size, gnu_type, gnat_entity)
5582      Uint uint_size;
5583      tree gnu_type;
5584      Entity_Id gnat_entity;
5585 {
5586   /* Only give an error if a Value_Size clause was explicitly given.
5587      Otherwise, we'd be duplicating an error on the Size clause.  */
5588   Node_Id gnat_attr_node
5589     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5590   tree old_size = rm_size (gnu_type);
5591   tree size;
5592
5593   /* Get the size as a tree.  Do nothing if none was specified, either
5594      because RM_Size was not Present or if the specified size was zero.
5595      Give an error if a size was specified, but cannot be represented as
5596      in sizetype.  */
5597   if (No (uint_size) || uint_size == No_Uint)
5598     return;
5599
5600   size = UI_To_gnu (uint_size, bitsizetype);
5601   if (TREE_OVERFLOW (size))
5602     {
5603       if (Present (gnat_attr_node))
5604         post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5605                        gnat_entity);
5606
5607       return;
5608     }
5609
5610   /* Ignore a negative size since that corresponds to our back-annotation.
5611      Also ignore a zero size unless a size clause exists, a Value_Size
5612      clause exists, or this is an integer type, in which case the
5613      front end will have always set it.  */
5614   else if (tree_int_cst_sgn (size) < 0
5615            || (integer_zerop (size) && No (gnat_attr_node)
5616                && ! Has_Size_Clause (gnat_entity)
5617                && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5618     return;
5619
5620   /* If the old size is self-referential, get the maximum size.  */
5621   if (TREE_CODE (old_size) != INTEGER_CST
5622       && contains_placeholder_p (old_size))
5623     old_size = max_size (old_size, 1);
5624
5625   /* If the size of the object is a constant, the new size must not be
5626      smaller (the front end checks this for scalar types).  */
5627   if (TREE_CODE (old_size) != INTEGER_CST
5628       || TREE_OVERFLOW (old_size)
5629       || (AGGREGATE_TYPE_P (gnu_type)
5630           && tree_int_cst_lt (size, old_size)))
5631     {
5632       if (Present (gnat_attr_node))
5633         post_error_ne_tree
5634           ("Value_Size for& too small{, minimum allowed is ^}",
5635            gnat_attr_node, gnat_entity, old_size);
5636
5637       return;
5638     }
5639
5640   /* Otherwise, set the RM_Size.  */
5641   if (TREE_CODE (gnu_type) == INTEGER_TYPE
5642       && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5643     TYPE_RM_SIZE_INT (gnu_type) = size;
5644   else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
5645     TYPE_RM_SIZE_ENUM (gnu_type) = size;
5646   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5647             || TREE_CODE (gnu_type) == UNION_TYPE
5648             || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5649            && ! TYPE_IS_FAT_POINTER_P (gnu_type))
5650     TYPE_ADA_SIZE (gnu_type) = size;
5651 }
5652 \f
5653 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5654    If TYPE is the best type, return it.  Otherwise, make a new type.  We
5655    only support new integral and pointer types.  BIASED_P is nonzero if
5656    we are making a biased type.  */
5657
5658 static tree
5659 make_type_from_size (type, size_tree, biased_p)
5660      tree type;
5661      tree size_tree;
5662      int biased_p;
5663 {
5664   tree new_type;
5665   unsigned HOST_WIDE_INT size;
5666
5667   /* If size indicates an error, just return TYPE to avoid propagating the
5668      error.  Likewise if it's too large to represent.  */
5669   if (size_tree == 0 || ! host_integerp (size_tree, 1))
5670     return type;
5671
5672   size = tree_low_cst (size_tree, 1);
5673   switch (TREE_CODE (type))
5674     {
5675     case INTEGER_TYPE:
5676     case ENUMERAL_TYPE:
5677       /* Only do something if the type is not already the proper size and is
5678          not a packed array type.  */
5679       if (TYPE_PACKED_ARRAY_TYPE_P (type)
5680           || (TYPE_PRECISION (type) == size
5681               && biased_p == (TREE_CODE (type) == INTEGER_CST
5682                               && TYPE_BIASED_REPRESENTATION_P (type))))
5683         break;
5684
5685       size = MIN (size, LONG_LONG_TYPE_SIZE);
5686       new_type = make_signed_type (size);
5687       TREE_TYPE (new_type)
5688         = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
5689       TYPE_MIN_VALUE (new_type)
5690         = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
5691       TYPE_MAX_VALUE (new_type)
5692         = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
5693       TYPE_BIASED_REPRESENTATION_P (new_type)
5694         = ((TREE_CODE (type) == INTEGER_TYPE
5695             && TYPE_BIASED_REPRESENTATION_P (type))
5696            || biased_p);
5697       TREE_UNSIGNED (new_type)
5698         = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
5699       TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
5700       return new_type;
5701
5702     case RECORD_TYPE:
5703       /* Do something if this is a fat pointer, in which case we
5704          may need to return the thin pointer.  */
5705       if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
5706         return
5707           build_pointer_type
5708             (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
5709       break;
5710
5711     case POINTER_TYPE:
5712       /* Only do something if this is a thin pointer, in which case we
5713          may need to return the fat pointer.  */
5714       if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
5715         return
5716           build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
5717
5718       break;
5719
5720     default:
5721       break;
5722     }
5723
5724   return type;
5725 }
5726 \f
5727 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
5728    a type or object whose present alignment is ALIGN.  If this alignment is
5729    valid, return it.  Otherwise, give an error and return ALIGN.  */
5730
5731 static unsigned int
5732 validate_alignment (alignment, gnat_entity, align)
5733      Uint alignment;
5734      Entity_Id gnat_entity;
5735      unsigned int align;
5736 {
5737   Node_Id gnat_error_node = gnat_entity;
5738   unsigned int new_align;
5739
5740 #ifndef MAX_OFILE_ALIGNMENT
5741 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
5742 #endif
5743
5744   if (Present (Alignment_Clause (gnat_entity)))
5745     gnat_error_node = Expression (Alignment_Clause (gnat_entity));
5746
5747   /* Within GCC, an alignment is an integer, so we must make sure a
5748      value is specified that fits in that range.  Also, alignments of
5749      more than MAX_OFILE_ALIGNMENT can't be supported.  */
5750
5751   if (! UI_Is_In_Int_Range (alignment)
5752       || ((new_align = UI_To_Int (alignment))
5753            > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
5754     post_error_ne_num ("largest supported alignment for& is ^",
5755                        gnat_error_node, gnat_entity,
5756                        MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
5757   else if (! (Present (Alignment_Clause (gnat_entity))
5758               && From_At_Mod (Alignment_Clause (gnat_entity)))
5759            && new_align * BITS_PER_UNIT < align)
5760     post_error_ne_num ("alignment for& must be at least ^",
5761                        gnat_error_node, gnat_entity,
5762                        align / BITS_PER_UNIT);
5763   else
5764     align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
5765
5766   return align;
5767 }
5768 \f
5769 /* Verify that OBJECT, a type or decl, is something we can implement
5770    atomically.  If not, give an error for GNAT_ENTITY.  COMP_P is nonzero
5771    if we require atomic components.  */
5772
5773 static void
5774 check_ok_for_atomic (object, gnat_entity, comp_p)
5775      tree object;
5776      Entity_Id gnat_entity;
5777      int comp_p;
5778 {
5779   Node_Id gnat_error_point = gnat_entity;
5780   Node_Id gnat_node;
5781   enum machine_mode mode;
5782   unsigned int align;
5783   tree size;
5784
5785   /* There are three case of what OBJECT can be.  It can be a type, in which
5786      case we take the size, alignment and mode from the type.  It can be a
5787      declaration that was indirect, in which case the relevant values are
5788      that of the type being pointed to, or it can be a normal declaration,
5789      in which case the values are of the decl.  The code below assumes that
5790      OBJECT is either a type or a decl.  */
5791   if (TYPE_P (object))
5792     {
5793       mode = TYPE_MODE (object);
5794       align = TYPE_ALIGN (object);
5795       size = TYPE_SIZE (object);
5796     }
5797   else if (DECL_BY_REF_P (object))
5798     {
5799       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
5800       align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
5801       size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
5802     }
5803   else
5804     {
5805       mode = DECL_MODE (object);
5806       align = DECL_ALIGN (object);
5807       size = DECL_SIZE (object);
5808     }
5809
5810   /* Consider all floating-point types atomic and any types that that are
5811      represented by integers no wider than a machine word.  */
5812   if (GET_MODE_CLASS (mode) == MODE_FLOAT
5813       || ((GET_MODE_CLASS (mode) == MODE_INT
5814            || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5815           && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
5816     return;
5817
5818   /* For the moment, also allow anything that has an alignment equal
5819      to its size and which is smaller than a word.  */
5820   if (TREE_CODE (size) == INTEGER_CST
5821       && compare_tree_int (size, align) == 0
5822       && align <= BITS_PER_WORD)
5823     return;
5824
5825   for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
5826        gnat_node = Next_Rep_Item (gnat_node))
5827     {
5828       if (! comp_p && Nkind (gnat_node) == N_Pragma
5829           && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
5830         gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5831       else if (comp_p && Nkind (gnat_node) == N_Pragma
5832                && (Get_Pragma_Id (Chars (gnat_node))
5833                    == Pragma_Atomic_Components))
5834         gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5835     }
5836
5837   if (comp_p)
5838     post_error_ne ("atomic access to component of & cannot be guaranteed",
5839                    gnat_error_point, gnat_entity);
5840   else
5841     post_error_ne ("atomic access to & cannot be guaranteed",
5842                    gnat_error_point, gnat_entity);
5843 }
5844 \f
5845 /* Given a type T, a FIELD_DECL F, and a replacement value R,
5846    return a new type with all size expressions that contain F
5847    updated by replacing F with R.  This is identical to GCC's
5848    substitute_in_type except that it knows about TYPE_INDEX_TYPE.
5849    If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
5850    changed.  */
5851
5852 tree
5853 gnat_substitute_in_type (t, f, r)
5854      tree t, f, r;
5855 {
5856   tree new = t;
5857   tree tem;
5858
5859   switch (TREE_CODE (t))
5860     {
5861     case INTEGER_TYPE:
5862     case ENUMERAL_TYPE:
5863     case BOOLEAN_TYPE:
5864     case CHAR_TYPE:
5865       if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
5866            && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5867           || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
5868               && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5869         {
5870           tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5871           tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5872
5873           if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5874             return t;
5875
5876           new = build_range_type (TREE_TYPE (t), low, high);
5877           if (TYPE_INDEX_TYPE (t))
5878             TYPE_INDEX_TYPE (new)
5879               = gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r);
5880           return new;
5881         }
5882
5883       return t;
5884
5885     case REAL_TYPE:
5886       if ((TYPE_MIN_VALUE (t) != 0
5887            && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
5888            && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5889           || (TYPE_MAX_VALUE (t) != 0
5890               && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
5891               && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5892         {
5893           tree low = 0, high = 0;
5894
5895           if (TYPE_MIN_VALUE (t))
5896             low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5897           if (TYPE_MAX_VALUE (t))
5898             high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5899
5900           if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5901             return t;
5902
5903           t = copy_type (t);
5904           TYPE_MIN_VALUE (t) = low;
5905           TYPE_MAX_VALUE (t) = high;
5906         }
5907       return t;
5908
5909     case COMPLEX_TYPE:
5910       tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5911       if (tem == TREE_TYPE (t))
5912         return t;
5913
5914       return build_complex_type (tem);
5915
5916     case OFFSET_TYPE:
5917     case METHOD_TYPE:
5918     case FILE_TYPE:
5919     case SET_TYPE:
5920     case FUNCTION_TYPE:
5921     case LANG_TYPE:
5922       /* Don't know how to do these yet.  */
5923       abort ();
5924
5925     case ARRAY_TYPE:
5926       {
5927         tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5928         tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
5929
5930         if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5931           return t;
5932
5933         new = build_array_type (component, domain);
5934         TYPE_SIZE (new) = 0;
5935         TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
5936         TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
5937         layout_type (new);
5938         TYPE_ALIGN (new) = TYPE_ALIGN (t);
5939         return new;
5940       }
5941
5942     case RECORD_TYPE:
5943     case UNION_TYPE:
5944     case QUAL_UNION_TYPE:
5945       {
5946         tree field;
5947         int changed_field
5948           = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
5949         int field_has_rep = 0;
5950         tree last_field = 0;
5951
5952         tree new = copy_type (t);
5953
5954         /* Start out with no fields, make new fields, and chain them
5955            in.  If we haven't actually changed the type of any field,
5956            discard everything we've done and return the old type.  */
5957
5958         TYPE_FIELDS (new) = 0;
5959         TYPE_SIZE (new) = 0;
5960
5961         for (field = TYPE_FIELDS (t); field;
5962              field = TREE_CHAIN (field))
5963           {
5964             tree new_field = copy_node (field);
5965
5966             TREE_TYPE (new_field)
5967               = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
5968
5969             if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
5970               field_has_rep = 1;
5971             else if (TREE_TYPE (new_field) != TREE_TYPE (field))
5972               changed_field = 1;
5973
5974             /* If this is an internal field and the type of this field is
5975                a UNION_TYPE or RECORD_TYPE with no elements, ignore it.  If
5976                the type just has one element, treat that as the field.
5977                But don't do this if we are processing a QUAL_UNION_TYPE.  */
5978             if (TREE_CODE (t) != QUAL_UNION_TYPE
5979                 && DECL_INTERNAL_P (new_field)
5980                 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
5981                     || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
5982               {
5983                 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
5984                   continue;
5985
5986                 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
5987                   {
5988                     tree next_new_field
5989                       = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
5990
5991                     /* Make sure omitting the union doesn't change
5992                        the layout.  */
5993                     DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
5994                     new_field = next_new_field;
5995                   }
5996               }
5997
5998             DECL_CONTEXT (new_field) = new;
5999             DECL_ORIGINAL_FIELD (new_field)
6000               = DECL_ORIGINAL_FIELD (field) != 0
6001                 ? DECL_ORIGINAL_FIELD (field) : field;
6002
6003             /* If the size of the old field was set at a constant,
6004                propagate the size in case the type's size was variable.
6005                (This occurs in the case of a variant or discriminated
6006                record with a default size used as a field of another
6007                record.)  */
6008             DECL_SIZE (new_field)
6009               = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6010                 ? DECL_SIZE (field) : 0;
6011             DECL_SIZE_UNIT (new_field)
6012               = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6013                 ? DECL_SIZE_UNIT (field) : 0;
6014
6015             if (TREE_CODE (t) == QUAL_UNION_TYPE)
6016               {
6017                 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6018
6019                 if (new_q != DECL_QUALIFIER (new_field))
6020                   changed_field = 1;
6021
6022                 /* Do the substitution inside the qualifier and if we find
6023                    that this field will not be present, omit it.  */
6024                 DECL_QUALIFIER (new_field) = new_q;
6025
6026                 if (integer_zerop (DECL_QUALIFIER (new_field)))
6027                   continue;
6028               }
6029
6030             if (last_field == 0)
6031               TYPE_FIELDS (new) = new_field;
6032             else
6033               TREE_CHAIN (last_field) = new_field;
6034
6035             last_field = new_field;
6036
6037             /* If this is a qualified type and this field will always be
6038                present, we are done.  */
6039             if (TREE_CODE (t) == QUAL_UNION_TYPE
6040                 && integer_onep (DECL_QUALIFIER (new_field)))
6041               break;
6042           }
6043
6044         /* If this used to be a qualified union type, but we now know what
6045            field will be present, make this a normal union.  */
6046         if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6047             && (TYPE_FIELDS (new) == 0
6048                 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6049           TREE_SET_CODE (new, UNION_TYPE);
6050         else if (! changed_field)
6051           return t;
6052
6053         if (field_has_rep)
6054           gigi_abort (117);
6055
6056         layout_type (new);
6057
6058         /* If the size was originally a constant use it.  */
6059         if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6060             && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6061           {
6062             TYPE_SIZE (new) = TYPE_SIZE (t);
6063             TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6064             TYPE_ADA_SIZE (new) = TYPE_ADA_SIZE (t);
6065           }
6066
6067         return new;
6068       }
6069
6070     default:
6071       return t;
6072     }
6073 }
6074 \f
6075 /* Return the "RM size" of GNU_TYPE.  This is the actual number of bits
6076    needed to represent the object.  */
6077
6078 tree
6079 rm_size (gnu_type)
6080      tree gnu_type;
6081 {
6082   /* For integer types, this is the precision.  For record types, we store
6083      the size explicitly.  For other types, this is just the size.  */
6084
6085   if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6086     return TYPE_RM_SIZE (gnu_type);
6087   else if (TREE_CODE (gnu_type) == RECORD_TYPE
6088            && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6089     /* Return the rm_size of the actual data plus the size of the template.  */
6090     return
6091       size_binop (PLUS_EXPR,
6092                   rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6093                   DECL_SIZE (TYPE_FIELDS (gnu_type)));
6094   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6095             || TREE_CODE (gnu_type) == UNION_TYPE
6096             || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6097            && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6098            && TYPE_ADA_SIZE (gnu_type) != 0)
6099     return TYPE_ADA_SIZE (gnu_type);
6100   else
6101     return TYPE_SIZE (gnu_type);
6102 }
6103 \f
6104 /* Return an identifier representing the external name to be used for
6105    GNAT_ENTITY.  If SUFFIX is specified, the name is followed by "___"
6106    and the specified suffix.  */
6107
6108 tree
6109 create_concat_name (gnat_entity, suffix)
6110      Entity_Id gnat_entity;
6111      const char *suffix;
6112 {
6113   const char *str = (suffix == 0 ? "" : suffix);
6114   String_Template temp = {1, strlen (str)};
6115   Fat_Pointer fp = {str, &temp};
6116
6117   Get_External_Name_With_Suffix (gnat_entity, fp);
6118
6119   return get_identifier (Name_Buffer);
6120 }
6121
6122 /* Return the name to be used for GNAT_ENTITY.  If a type, create a
6123    fully-qualified name, possibly with type information encoding.
6124    Otherwise, return the name.  */
6125
6126 tree
6127 get_entity_name (gnat_entity)
6128      Entity_Id gnat_entity;
6129 {
6130   Get_Encoded_Name (gnat_entity);
6131   return get_identifier (Name_Buffer);
6132 }
6133
6134 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6135    string, return a new IDENTIFIER_NODE that is the concatenation of
6136    the name in GNU_ID and SUFFIX.  */
6137
6138 tree
6139 concat_id_with_name (gnu_id, suffix)
6140      tree gnu_id;
6141      const char *suffix;
6142 {
6143   int len = IDENTIFIER_LENGTH (gnu_id);
6144
6145   strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6146            IDENTIFIER_LENGTH (gnu_id));
6147   strncpy (Name_Buffer + len, "___", 3);
6148   len += 3;
6149   strcpy (Name_Buffer + len, suffix);
6150   return get_identifier (Name_Buffer);
6151 }