OSDN Git Service

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