OSDN Git Service

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