OSDN Git Service

* exp_dbug.ads: Fix outdated description. Mention link between XVS
[pf3gnuchains/gcc-fork.git] / gcc / ada / gcc-interface / decl.c
index 7d96c9a..ccedee0 100644 (file)
@@ -6,7 +6,7 @@
  *                                                                          *
  *                          C Implementation File                           *
  *                                                                          *
- *          Copyright (C) 1992-2009, Free Software Foundation, Inc.         *
+ *          Copyright (C) 1992-2010, Free Software Foundation, Inc.         *
  *                                                                          *
  * GNAT is free software;  you can  redistribute it  and/or modify it under *
  * terms of the  GNU General Public License as published  by the Free Soft- *
@@ -122,7 +122,6 @@ enum alias_set_op
 
 static void relate_alias_sets (tree, tree, enum alias_set_op);
 
-static tree build_subst_list (Entity_Id, Entity_Id, bool);
 static bool allocatable_size_p (tree, bool);
 static void prepend_one_attribute_to (struct attrib **,
                                      enum attr_type, tree, tree, Node_Id);
@@ -131,25 +130,34 @@ static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
 static bool is_variable_size (tree);
 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
 static tree make_packable_type (tree, bool);
-static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
+static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
                               bool *);
+static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
 static bool same_discriminant_p (Entity_Id, Entity_Id);
-static bool array_type_has_nonaliased_component (Entity_Id, tree);
+static bool array_type_has_nonaliased_component (tree, Entity_Id);
 static bool compile_time_known_address_p (Node_Id);
 static bool cannot_be_superflat_p (Node_Id);
+static bool constructor_address_p (tree);
 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
-                                 bool, bool, bool, bool);
+                                 bool, bool, bool, bool, bool);
 static Uint annotate_value (tree);
 static void annotate_rep (Entity_Id, tree);
-static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
+static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
+static tree build_subst_list (Entity_Id, Entity_Id, bool);
+static tree build_variant_list (tree, tree, tree);
 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
 static void set_rm_size (Uint, tree, Entity_Id);
 static tree make_type_from_size (tree, tree, bool);
 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
 static void check_ok_for_atomic (tree, Entity_Id, bool);
-static int compatible_signatures_p (tree ftype1, tree ftype2);
+static int compatible_signatures_p (tree, tree);
+static tree create_field_decl_from (tree, tree, tree, tree, tree, tree);
+static tree get_rep_part (tree);
+static tree get_variant_part (tree);
+static tree create_variant_part_from (tree, tree, tree, tree, tree);
+static void copy_and_substitute_in_size (tree, tree, tree);
 static void rest_of_type_decl_compilation_no_defer (tree);
 \f
 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
@@ -174,6 +182,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   const Entity_Kind kind = Ekind (gnat_entity);
   /* True if this is a type.  */
   const bool is_type = IN (kind, Type_Kind);
+  /* True if debug info is requested for this entity.  */
+  const bool debug_info_p = Needs_Debug_Info (gnat_entity);
+  /* True if this entity is to be considered as imported.  */
+  const bool imported_p
+    = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
   /* For a type, contains the equivalent GNAT node to be used in gigi.  */
   Entity_Id gnat_equiv_type = Empty;
   /* Temporary used to walk the GNAT tree.  */
@@ -198,12 +211,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   bool maybe_present = false;
   /* True if we made GNU_DECL and its type here.  */
   bool this_made_decl = false;
-  /* True if debug info is requested for this entity.  */
-  bool debug_info_p = (Needs_Debug_Info (gnat_entity)
-                      || debug_info_level == DINFO_LEVEL_VERBOSE);
-  /* True if this entity is to be considered as imported.  */
-  bool imported_p = (Is_Imported (gnat_entity)
-                    && No (Address_Clause (gnat_entity)));
   /* Size and alignment of the GCC node, if meaningful.  */
   unsigned int esize = 0, align = 0;
   /* Contains the list of attributes directly attached to the entity.  */
@@ -541,10 +548,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_Out_Parameter:
     case E_Variable:
 
-      /* Simple variables, loop variables, Out parameters, and exceptions.  */
+      /* Simple variables, loop variables, Out parameters and exceptions.  */
     object:
       {
-       bool used_by_ref = false;
        bool const_flag
          = ((kind == E_Constant || kind == E_Variable)
             && Is_True_Constant (gnat_entity)
@@ -556,6 +562,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        bool inner_const_flag = const_flag;
        bool static_p = Is_Statically_Allocated (gnat_entity);
        bool mutable_p = false;
+       bool used_by_ref = false;
        tree gnu_ext_name = NULL_TREE;
        tree renamed_obj = NULL_TREE;
        tree gnu_object_size;
@@ -597,9 +604,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (kind == E_Loop_Parameter)
          gnu_type = get_base_type (gnu_type);
 
-       /* Reject non-renamed objects whose types are unconstrained arrays or
-          any object whose type is a dummy type or VOID_TYPE.  */
-
+       /* Reject non-renamed objects whose type is an unconstrained array or
+          any object whose type is a dummy type or void.  */
        if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
             && No (Renamed_Object (gnat_entity)))
            || TYPE_IS_DUMMY_P (gnu_type)
@@ -619,6 +625,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            gcc_assert (Present (Alignment (gnat_entity)));
            align = validate_alignment (Alignment (gnat_entity), gnat_entity,
                                        TYPE_ALIGN (gnu_type));
+
            /* No point in changing the type if there is an address clause
               as the final type of the object will be a reference type.  */
            if (Present (Address_Clause (gnat_entity)))
@@ -626,14 +633,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            else
              gnu_type
                = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
-                                 "PAD", false, definition, true);
+                                 false, false, definition, true);
          }
 
-       /* If we are defining the object, see if it has a Size value and
-          validate it if so.  If we are not defining the object and a Size
-          clause applies, simply retrieve the value.  We don't want to ignore
-          the clause and it is expected to have been validated already.  Then
-          get the new type, if any.  */
+       /* If we are defining the object, see if it has a Size and validate it
+          if so.  If we are not defining the object and a Size clause applies,
+          simply retrieve the value.  We don't want to ignore the clause and
+          it is expected to have been validated already.  Then get the new
+          type, if any.  */
        if (definition)
          gnu_size = validate_size (Esize (gnat_entity), gnu_type,
                                    gnat_entity, VAR_DECL, false,
@@ -652,8 +659,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          }
 
        /* If this object has self-referential size, it must be a record with
-          a default value.  We are supposed to allocate an object of the
-          maximum size in this case unless it is a constant with an
+          a default discriminant.  We are supposed to allocate an object of
+          the maximum size in this case, unless it is a constant with an
           initializing expression, in which case we can get the size from
           that.  Note that the resulting size may still be a variable, so
           this may end up with an indirect allocation.  */
@@ -669,8 +676,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                       despite having a nominal type with self-referential
                       size, we can get the size directly from it.  */
                    if (TREE_CODE (gnu_expr) == COMPONENT_REF
-                       && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
-                          == RECORD_TYPE
                        && TYPE_IS_PADDING_P
                           (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
                        && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
@@ -702,8 +707,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              }
          }
 
-       /* If the size is zero bytes, make it one byte since some linkers have
-          trouble with zero-sized objects.  If the object will have a
+       /* If the size is zero byte, make it one byte since some linkers have
+          troubles with zero-sized objects.  If the object will have a
           template, that will make it nonzero so don't bother.  Also avoid
           doing that for an object renaming or an object with an address
           clause, as we would lose useful information on the view size
@@ -769,7 +774,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
           ??? Note that we ignore Has_Volatile_Components on objects; it's
           not at all clear what to do in that case.  */
-
        if (Has_Atomic_Components (gnat_entity))
          {
            tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
@@ -784,10 +788,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Now check if the type of the object allows atomic access.  Note
           that we must test the type, even if this object has size and
-          alignment to allow such access, because we will be going
-          inside the padded record to assign to the object.  We could fix
-          this by always copying via an intermediate value, but it's not
-          clear it's worth the effort.  */
+          alignment to allow such access, because we will be going inside
+          the padded record to assign to the object.  We could fix this by
+          always copying via an intermediate value, but it's not clear it's
+          worth the effort.  */
        if (Is_Atomic (gnat_entity))
          check_ok_for_atomic (gnu_type, gnat_entity, false);
 
@@ -831,7 +835,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
        if (gnu_size || align > 0)
          gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
-                                    "PAD", false, definition,
+                                    false, false, definition,
                                     gnu_size ? true : false);
 
        /* If this is a renaming, avoid as much as possible to create a new
@@ -845,8 +849,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            /* If the renamed object had padding, strip off the reference
               to the inner object and reset our type.  */
            if ((TREE_CODE (gnu_expr) == COMPONENT_REF
-                && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
-                   == RECORD_TYPE
                 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
                /* Strip useless conversions around the object.  */
                || (TREE_CODE (gnu_expr) == NOP_EXPR
@@ -894,15 +896,15 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                            && !TREE_SIDE_EFFECTS (gnu_expr))))
                  {
                    maybe_stable_expr
-                     = maybe_stabilize_reference (gnu_expr, true, &stable);
+                     = gnat_stabilize_reference (gnu_expr, true, &stable);
 
                    if (stable)
                      {
-                       gnu_decl = maybe_stable_expr;
                        /* ??? No DECL_EXPR is created so we need to mark
                           the expression manually lest it is shared.  */
                        if (global_bindings_p ())
-                         mark_visited (&gnu_decl);
+                         MARK_VISITED (maybe_stable_expr);
+                       gnu_decl = maybe_stable_expr;
                        save_gnu_tree (gnat_entity, gnu_decl, true);
                        saved = true;
                        annotate_object (gnat_entity, gnu_type, NULL_TREE,
@@ -970,7 +972,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    else
                     {
                        maybe_stable_expr
-                         = maybe_stabilize_reference (gnu_expr, true, &stable);
+                         = gnat_stabilize_reference (gnu_expr, true, &stable);
 
                        if (stable)
                          renamed_obj = maybe_stable_expr;
@@ -979,8 +981,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                           as we have a VAR_DECL for the pointer we make.  */
                      }
 
-                   gnu_expr
-                     = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
+                   gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
+                                              maybe_stable_expr);
 
                    gnu_size = NULL_TREE;
                    used_by_ref = true;
@@ -1010,28 +1012,26 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            && !gnu_expr
            && TREE_CODE (gnu_type) == RECORD_TYPE
            && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
-               /* Beware that padding might have been introduced
-                  via maybe_pad_type above.  */
-               || (TYPE_IS_PADDING_P (gnu_type)
+               /* Beware that padding might have been introduced above.  */
+               || (TYPE_PADDING_P (gnu_type)
                    && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
                       == RECORD_TYPE
                    && TYPE_CONTAINS_TEMPLATE_P
                       (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
          {
            tree template_field
-             = TYPE_IS_PADDING_P (gnu_type)
+             = TYPE_PADDING_P (gnu_type)
                ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
                : TYPE_FIELDS (gnu_type);
-
            gnu_expr
              = gnat_build_constructor
-             (gnu_type,
-              tree_cons
-              (template_field,
-               build_template (TREE_TYPE (template_field),
-                               TREE_TYPE (TREE_CHAIN (template_field)),
-                               NULL_TREE),
-               NULL_TREE));
+               (gnu_type,
+                tree_cons
+                (template_field,
+                 build_template (TREE_TYPE (template_field),
+                                 TREE_TYPE (TREE_CHAIN (template_field)),
+                                 NULL_TREE),
+                 NULL_TREE));
          }
 
        /* Convert the expression to the type of the object except in the
@@ -1043,18 +1043,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (gnu_expr
            && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
            && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
-           && !(TREE_CODE (gnu_type) == RECORD_TYPE
-                && TYPE_IS_PADDING_P (gnu_type)
-                && (CONTAINS_PLACEHOLDER_P
-                    (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
+           && !(TYPE_IS_PADDING_P (gnu_type)
+                && CONTAINS_PLACEHOLDER_P
+                   (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
          gnu_expr = convert (gnu_type, gnu_expr);
 
-       /* If this is a pointer and it does not have an initializing
-          expression, initialize it to NULL, unless the object is
-          imported.  */
+       /* If this is a pointer that doesn't have an initializing expression,
+          initialize it to NULL, unless the object is imported.  */
        if (definition
-           && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
-           && !Is_Imported (gnat_entity) && !gnu_expr)
+           && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
+           && !gnu_expr
+           && !Is_Imported (gnat_entity))
          gnu_expr = integer_zero_node;
 
        /* If we are defining the object and it has an Address clause, we must
@@ -1064,10 +1063,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           effects in this case.  */
        if (definition && Present (Address_Clause (gnat_entity)))
          {
+           Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
            tree gnu_address
              = present_gnu_tree (gnat_entity)
-               ? get_gnu_tree (gnat_entity)
-               : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
+               ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
 
            save_gnu_tree (gnat_entity, NULL_TREE, false);
 
@@ -1080,9 +1079,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              = build_reference_type_for_mode (gnu_type, ptr_mode, true);
            gnu_address = convert (gnu_type, gnu_address);
            used_by_ref = true;
-           const_flag = !Is_Public (gnat_entity)
-             || compile_time_known_address_p (Expression (Address_Clause
-                                                          (gnat_entity)));
+           const_flag
+             = !Is_Public (gnat_entity)
+               || compile_time_known_address_p (gnat_expr);
 
            /* If this is a deferred constant, the initializer is attached to
               the full view.  */
@@ -1153,14 +1152,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           If the object's size overflows, make an allocator too, so that
           Storage_Error gets raised.  Note that we will never free
           such memory, so we presume it never will get allocated.  */
-
        if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
-                                global_bindings_p () || !definition
+                                global_bindings_p ()
+                                || !definition
                                 || static_p)
-           || (gnu_size
-               && ! allocatable_size_p (gnu_size,
-                                        global_bindings_p () || !definition
-                                        || static_p)))
+           || (gnu_size && !allocatable_size_p (gnu_size,
+                                                global_bindings_p ()
+                                                || !definition
+                                                || static_p)))
          {
            gnu_type = build_reference_type (gnu_type);
            gnu_size = NULL_TREE;
@@ -1179,7 +1178,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
               If we are elaborating a mutable object, tell build_allocator to
               ignore a possibly simpler size from the initializer, if any, as
               we must allocate the maximum possible size in this case.  */
-
            if (definition)
              {
                tree gnu_alloc_type = TREE_TYPE (gnu_type);
@@ -1272,10 +1270,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (gnu_expr
            && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
            && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
-           && !(TREE_CODE (gnu_type) == RECORD_TYPE
-                && TYPE_IS_PADDING_P (gnu_type)
-                && (CONTAINS_PLACEHOLDER_P
-                    (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
+           && !(TYPE_IS_PADDING_P (gnu_type)
+                && CONTAINS_PLACEHOLDER_P
+                   (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
          gnu_expr = convert (gnu_type, gnu_expr);
 
        /* If this name is external or there was a name specified, use it,
@@ -1290,26 +1287,44 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                        || Is_Exported (gnat_entity)))))
          gnu_ext_name = create_concat_name (gnat_entity, NULL);
 
-       /* If this is constant initialized to a static constant and the
-          object has an aggregate type, force it to be statically
-          allocated.  This will avoid an initialization copy.  */
-       if (!static_p && const_flag
+       /* If this is an aggregate constant initialized to a constant, force it
+          to be statically allocated.  This saves an initialization copy.  */
+       if (!static_p
+           && const_flag
            && gnu_expr && TREE_CONSTANT (gnu_expr)
            && AGGREGATE_TYPE_P (gnu_type)
            && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
-           && !(TREE_CODE (gnu_type) == RECORD_TYPE
-                && TYPE_IS_PADDING_P (gnu_type)
+           && !(TYPE_IS_PADDING_P (gnu_type)
                 && !host_integerp (TYPE_SIZE_UNIT
                                    (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
          static_p = true;
 
-       gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
-                                   gnu_expr, const_flag,
-                                   Is_Public (gnat_entity),
-                                   imported_p || !definition,
-                                   static_p, attr_list, gnat_entity);
+       /* Now create the variable or the constant and set various flags.  */
+       gnu_decl
+         = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
+                            gnu_expr, const_flag, Is_Public (gnat_entity),
+                            imported_p || !definition, static_p, attr_list,
+                            gnat_entity);
        DECL_BY_REF_P (gnu_decl) = used_by_ref;
        DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
+
+       /* If we are defining an Out parameter and optimization isn't enabled,
+          create a fake PARM_DECL for debugging purposes and make it point to
+          the VAR_DECL.  Suppress debug info for the latter but make sure it
+          will live on the stack so that it can be accessed from within the
+          debugger through the PARM_DECL.  */
+       if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
+         {
+           tree param = create_param_decl (gnu_entity_name, gnu_type, false);
+           gnat_pushdecl (param, gnat_entity);
+           SET_DECL_VALUE_EXPR (param, gnu_decl);
+           DECL_HAS_VALUE_EXPR_P (param) = 1;
+           DECL_IGNORED_P (gnu_decl) = 1;
+           TREE_ADDRESSABLE (gnu_decl) = 1;
+         }
+
+       /* If this is a renaming pointer, attach the renamed object to it and
+          register it if we are at top level.  */
        if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
          {
            SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
@@ -1320,46 +1335,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              }
          }
 
-       if (definition && DECL_SIZE_UNIT (gnu_decl)
-           && get_block_jmpbuf_decl ()
-           && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
-               || (flag_stack_check == GENERIC_STACK_CHECK
-                   && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
-                                        STACK_CHECK_MAX_VAR_SIZE) > 0)))
-         add_stmt_with_node (build_call_1_expr
-                             (update_setjmp_buf_decl,
-                              build_unary_op (ADDR_EXPR, NULL_TREE,
-                                              get_block_jmpbuf_decl ())),
-                             gnat_entity);
-
-       /* If we are defining an Out parameter and we're not optimizing,
-          create a fake PARM_DECL for debugging purposes and make it
-          point to the VAR_DECL.  Suppress debug info for the latter
-          but make sure it will still live on the stack so it can be
-          accessed from within the debugger through the PARM_DECL.  */
-       if (kind == E_Out_Parameter && definition && !optimize)
-         {
-           tree param = create_param_decl (gnu_entity_name, gnu_type, false);
-           gnat_pushdecl (param, gnat_entity);
-           SET_DECL_VALUE_EXPR (param, gnu_decl);
-           DECL_HAS_VALUE_EXPR_P (param) = 1;
-           if (debug_info_p)
-             debug_info_p = false;
-           else
-             DECL_IGNORED_P (param) = 1;
-           TREE_ADDRESSABLE (gnu_decl) = 1;
-         }
-
-       /* If this is a public constant or we're not optimizing and we're not
-          making a VAR_DECL for it, make one just for export or debugger use.
-          Likewise if the address is taken or if either the object or type is
-          aliased.  Make an external declaration for a reference, unless this
-          is a Standard entity since there no real symbol at the object level
-          for these.  */
+       /* If this is a constant and we are defining it or it generates a real
+          symbol at the object level and we are referencing it, we may want
+          or need to have a true variable to represent it:
+            - if optimization isn't enabled, for debugging purposes,
+            - if the constant is public and not overlaid on something else,
+            - if its address is taken,
+            - if either itself or its type is aliased.  */
        if (TREE_CODE (gnu_decl) == CONST_DECL
            && (definition || Sloc (gnat_entity) > Standard_Location)
-           && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
-               || !optimize
+           && ((!optimize && debug_info_p)
+               || (Is_Public (gnat_entity)
+                   && No (Address_Clause (gnat_entity)))
                || Address_Taken (gnat_entity)
                || Is_Aliased (gnat_entity)
                || Is_Aliased (Etype (gnat_entity))))
@@ -1367,23 +1354,53 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            tree gnu_corr_var
              = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
                                      gnu_expr, true, Is_Public (gnat_entity),
-                                     !definition, static_p, NULL,
+                                     !definition, static_p, attr_list,
                                      gnat_entity);
 
            SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
 
            /* As debugging information will be generated for the variable,
-              do not generate information for the constant.  */
-           DECL_IGNORED_P (gnu_decl) = 1;
+              do not generate debugging information for the constant.  */
+           if (debug_info_p)
+             DECL_IGNORED_P (gnu_decl) = 1;
+           else
+             DECL_IGNORED_P (gnu_corr_var) = 1;
          }
 
-       /* If this is declared in a block that contains a block with an
-          exception handler, we must force this variable in memory to
-          suppress an invalid optimization.  */
-       if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
-           && Exception_Mechanism != Back_End_Exceptions)
+       /* If this is a constant, even if we don't need a true variable, we
+          may need to avoid returning the initializer in every case.  That
+          can happen for the address of a (constant) constructor because,
+          upon dereferencing it, the constructor will be reinjected in the
+          tree, which may not be valid in every case; see lvalue_required_p
+          for more details.  */
+       if (TREE_CODE (gnu_decl) == CONST_DECL)
+         DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
+
+       /* If this object is declared in a block that contains a block with an
+          exception handler, and we aren't using the GCC exception mechanism,
+          we must force this variable in memory in order to avoid an invalid
+          optimization.  */
+       if (Exception_Mechanism != Back_End_Exceptions
+           && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
          TREE_ADDRESSABLE (gnu_decl) = 1;
 
+       /* If we are defining an object with variable size or an object with
+          fixed size that will be dynamically allocated, and we are using the
+          setjmp/longjmp exception mechanism, update the setjmp buffer.  */
+       if (definition
+           && Exception_Mechanism == Setjmp_Longjmp
+           && get_block_jmpbuf_decl ()
+           && DECL_SIZE_UNIT (gnu_decl)
+           && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
+               || (flag_stack_check == GENERIC_STACK_CHECK
+                   && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
+                                        STACK_CHECK_MAX_VAR_SIZE) > 0)))
+         add_stmt_with_node (build_call_1_expr
+                             (update_setjmp_buf_decl,
+                              build_unary_op (ADDR_EXPR, NULL_TREE,
+                                              get_block_jmpbuf_decl ())),
+                             gnat_entity);
+
        /* Back-annotate Esize and Alignment of the object if not already
           known.  Note that we pick the values of the type, not those of
           the object, to shield ourselves from low-level platform-dependent
@@ -1417,30 +1434,31 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          break;
        }
 
-      /* Normal case of non-character type or non-Standard character type.  */
       {
-       /* Here we have a list of enumeral constants in First_Literal.
-          We make a CONST_DECL for each and build into GNU_LITERAL_LIST
-          the list to be placed into TYPE_FIELDS.  Each node in the list
-          is a TREE_LIST whose TREE_VALUE is the literal name and whose
-          TREE_PURPOSE is the value of the literal.  */
-
-       Entity_Id gnat_literal;
+       /* We have a list of enumeral constants in First_Literal.  We make a
+          CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
+          be placed into TYPE_FIELDS.  Each node in the list is a TREE_LIST
+          whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
+          value of the literal.  But when we have a regular boolean type, we
+          simplify this a little by using a BOOLEAN_TYPE.  */
+       bool is_boolean = Is_Boolean_Type (gnat_entity)
+                         && !Has_Non_Standard_Rep (gnat_entity);
        tree gnu_literal_list = NULL_TREE;
+       Entity_Id gnat_literal;
 
        if (Is_Unsigned_Type (gnat_entity))
          gnu_type = make_unsigned_type (esize);
        else
          gnu_type = make_signed_type (esize);
 
-       TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
+       TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
 
        for (gnat_literal = First_Literal (gnat_entity);
             Present (gnat_literal);
             gnat_literal = Next_Literal (gnat_literal))
          {
-           tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
-                                       gnu_type);
+           tree gnu_value
+             = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
            tree gnu_literal
              = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
                                 gnu_type, gnu_value, true, false, false,
@@ -1451,7 +1469,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                          gnu_value, gnu_literal_list);
          }
 
-       TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
+       if (!is_boolean)
+         TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
 
        /* Note that the bounds are updated at the end of this function
           to avoid an infinite recursion since they refer to the type.  */
@@ -1592,6 +1611,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                           gnat_to_gnu_type
                           (Original_Array_Type (gnat_entity)));
 
+      /* We have to handle clauses that under-align the type specially.  */
+      if ((Present (Alignment_Clause (gnat_entity))
+          || (Is_Packed_Array_Type (gnat_entity)
+              && Present
+                 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
+         && UI_Is_In_Int_Range (Alignment (gnat_entity)))
+       {
+         align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
+         if (align >= TYPE_ALIGN (gnu_type))
+           align = 0;
+       }
+
       /* If the type we are dealing with represents a bit-packed array,
         we need to have the bits left justified on big-endian targets
         and right justified on little-endian targets.  We also need to
@@ -1604,91 +1635,98 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        {
          tree gnu_field_type, gnu_field;
 
-         /* Set the RM size before wrapping up the type.  */
+         /* Set the RM size before wrapping up the original type.  */
          SET_TYPE_RM_SIZE (gnu_type,
                            UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
          TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
+
+         /* Create a stripped-down declaration, mainly for debugging.  */
+         create_type_decl (gnu_entity_name, gnu_type, NULL, true,
+                           debug_info_p, gnat_entity);
+
+         /* Now save it and build the enclosing record type.  */
          gnu_field_type = gnu_type;
 
          gnu_type = make_node (RECORD_TYPE);
          TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
-
-         /* Propagate the alignment of the modular type to the record.
-            This means that bit-packed arrays have "ceil" alignment for
-            their size, which may seem counter-intuitive but makes it
-            possible to easily overlay them on modular types.  */
-         TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
          TYPE_PACKED (gnu_type) = 1;
+         TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
+         TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
+         SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
+
+         /* Propagate the alignment of the modular type to the record type,
+            unless there is an alignment clause that under-aligns the type.
+            This means that bit-packed arrays are given "ceil" alignment for
+            their size by default, which may seem counter-intuitive but makes
+            it possible to overlay them on modular types easily.  */
+         TYPE_ALIGN (gnu_type)
+           = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
 
-         /* Create a stripped-down declaration of the original type, mainly
-            for debugging.  */
-         create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
-                           debug_info_p, gnat_entity);
+         relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
 
          /* Don't notify the field as "addressable", since we won't be taking
             it's address and it would prevent create_field_decl from making a
             bitfield.  */
          gnu_field = create_field_decl (get_identifier ("OBJECT"),
-                                        gnu_field_type, gnu_type, 1, 0, 0, 0);
+                                        gnu_field_type, gnu_type, 1,
+                                        NULL_TREE, bitsize_zero_node, 0);
 
-         /* Do not finalize it until after the parallel type is added.  */
-         finish_record_type (gnu_type, gnu_field, 0, true);
+         /* Do not emit debug info until after the parallel type is added.  */
+         finish_record_type (gnu_type, gnu_field, 2, false);
+         compute_record_mode (gnu_type);
          TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
 
-         relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
-
-         /* Make the original array type a parallel type.  */
-         if (debug_info_p
-             && present_gnu_tree (Original_Array_Type (gnat_entity)))
-           add_parallel_type (TYPE_STUB_DECL (gnu_type),
-                              gnat_to_gnu_type
-                              (Original_Array_Type (gnat_entity)));
+         if (debug_info_p)
+           {
+             /* Make the original array type a parallel type.  */
+             if (present_gnu_tree (Original_Array_Type (gnat_entity)))
+               add_parallel_type (TYPE_STUB_DECL (gnu_type),
+                                  gnat_to_gnu_type
+                                  (Original_Array_Type (gnat_entity)));
 
-         rest_of_record_type_compilation (gnu_type);
+             rest_of_record_type_compilation (gnu_type);
+           }
        }
 
       /* If the type we are dealing with has got a smaller alignment than the
         natural one, we need to wrap it up in a record type and under-align
         the latter.  We reuse the padding machinery for this purpose.  */
-      else if (Present (Alignment_Clause (gnat_entity))
-              && UI_Is_In_Int_Range (Alignment (gnat_entity))
-              && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
-              && align < TYPE_ALIGN (gnu_type))
+      else if (align > 0)
        {
          tree gnu_field_type, gnu_field;
 
          /* Set the RM size before wrapping up the type.  */
          SET_TYPE_RM_SIZE (gnu_type,
                            UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
+
+         /* Create a stripped-down declaration, mainly for debugging.  */
+         create_type_decl (gnu_entity_name, gnu_type, NULL, true,
+                           debug_info_p, gnat_entity);
+
+         /* Now save it and build the enclosing record type.  */
          gnu_field_type = gnu_type;
 
          gnu_type = make_node (RECORD_TYPE);
          TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
-
-         TYPE_ALIGN (gnu_type) = align;
          TYPE_PACKED (gnu_type) = 1;
-
-         /* Create a stripped-down declaration of the original type, mainly
-            for debugging.  */
-         create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
-                           debug_info_p, gnat_entity);
+         TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
+         TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
+         SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
+         TYPE_ALIGN (gnu_type) = align;
+         relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
 
          /* Don't notify the field as "addressable", since we won't be taking
             it's address and it would prevent create_field_decl from making a
             bitfield.  */
-         gnu_field = create_field_decl (get_identifier ("OBJECT"),
-                                        gnu_field_type, gnu_type, 1, 0, 0, 0);
-
-         finish_record_type (gnu_type, gnu_field, 0, false);
-         TYPE_IS_PADDING_P (gnu_type) = 1;
+         gnu_field = create_field_decl (get_identifier ("F"),
+                                        gnu_field_type, gnu_type, 1,
+                                        NULL_TREE, bitsize_zero_node, 0);
 
-         relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
+         finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
+         compute_record_mode (gnu_type);
+         TYPE_PADDING_P (gnu_type) = 1;
        }
 
-      /* Otherwise reset the alignment lest we computed it above.  */
-      else
-       align = 0;
-
       break;
 
     case E_Floating_Point_Type:
@@ -1782,7 +1820,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_String_Type:
     case E_Array_Type:
       {
-       Entity_Id gnat_index;
+       Entity_Id gnat_index, gnat_name;
        const bool convention_fortran_p
          = (Convention (gnat_entity) == Convention_Fortran);
        const int ndim = Number_Dimensions (gnat_entity);
@@ -1793,8 +1831,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        tree gnu_fat_type = make_node (RECORD_TYPE);
        tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
        tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
-       tree gnu_max_size = size_one_node, gnu_max_size_unit;
-       tree gnu_comp_size, tem;
+       tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
        int index;
 
        TYPE_NAME (gnu_template_type)
@@ -1826,10 +1863,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* Make sure we can put this into a register.  */
        TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
 
-       /* Do not finalize this record type since the types of its fields
-          are still incomplete at this point.  */
-       finish_record_type (gnu_fat_type, tem, 0, true);
-       TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
+       /* Do not emit debug info for this record type since the types of its
+          fields are still incomplete at this point.  */
+       finish_record_type (gnu_fat_type, tem, 0, false);
+       TYPE_FAT_POINTER_P (gnu_fat_type) = 1;
 
        /* Build a reference to the template from a PLACEHOLDER_EXPR that
           is the fat pointer.  This will be used to access the individual
@@ -1852,46 +1889,59 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            char field_name[16];
            tree gnu_index_base_type
              = get_unpadded_type (Base_Type (Etype (gnat_index)));
-           tree gnu_low_field, gnu_high_field, gnu_low, gnu_high;
+           tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
+           tree gnu_min, gnu_max, gnu_high;
 
            /* Make the FIELD_DECLs for the low and high bounds of this
               type and then make extractions of these fields from the
               template.  */
            sprintf (field_name, "LB%d", index);
-           gnu_low_field = create_field_decl (get_identifier (field_name),
-                                              gnu_index_base_type,
-                                              gnu_template_type, 0,
-                                              NULL_TREE, NULL_TREE, 0);
+           gnu_lb_field = create_field_decl (get_identifier (field_name),
+                                             gnu_index_base_type,
+                                             gnu_template_type, 0,
+                                             NULL_TREE, NULL_TREE, 0);
            Sloc_to_locus (Sloc (gnat_entity),
-                          &DECL_SOURCE_LOCATION (gnu_low_field));
+                          &DECL_SOURCE_LOCATION (gnu_lb_field));
 
            field_name[0] = 'U';
-           gnu_high_field = create_field_decl (get_identifier (field_name),
-                                               gnu_index_base_type,
-                                               gnu_template_type, 0,
-                                               NULL_TREE, NULL_TREE, 0);
+           gnu_hb_field = create_field_decl (get_identifier (field_name),
+                                             gnu_index_base_type,
+                                             gnu_template_type, 0,
+                                             NULL_TREE, NULL_TREE, 0);
            Sloc_to_locus (Sloc (gnat_entity),
-                          &DECL_SOURCE_LOCATION (gnu_high_field));
+                          &DECL_SOURCE_LOCATION (gnu_hb_field));
 
-           gnu_temp_fields[index] = chainon (gnu_low_field, gnu_high_field);
+           gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
 
            /* We can't use build_component_ref here since the template type
               isn't complete yet.  */
-           gnu_low = build3 (COMPONENT_REF, gnu_index_base_type,
-                             gnu_template_reference, gnu_low_field,
-                             NULL_TREE);
-           gnu_high = build3 (COMPONENT_REF, gnu_index_base_type,
-                              gnu_template_reference, gnu_high_field,
-                              NULL_TREE);
-           TREE_READONLY (gnu_low) = TREE_READONLY (gnu_high) = 1;
+           gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
+                                  gnu_template_reference, gnu_lb_field,
+                                  NULL_TREE);
+           gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
+                                  gnu_template_reference, gnu_hb_field,
+                                  NULL_TREE);
+           TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
+
+           gnu_min = convert (sizetype, gnu_orig_min);
+           gnu_max = convert (sizetype, gnu_orig_max);
+
+           /* Compute the size of this dimension.  See the E_Array_Subtype
+              case below for the rationale.  */
+           gnu_high
+             = build3 (COND_EXPR, sizetype,
+                       build2 (GE_EXPR, boolean_type_node,
+                               gnu_orig_max, gnu_orig_min),
+                       gnu_max,
+                       size_binop (MINUS_EXPR, gnu_min, size_one_node));
 
            /* Make a range type with the new range in the Ada base type.
-              Then make an index type with the new range in sizetype.  */
+              Then make an index type with the size range in sizetype.  */
            gnu_index_types[index]
-             = create_index_type (convert (sizetype, gnu_low),
-                                  convert (sizetype, gnu_high),
+             = create_index_type (gnu_min, gnu_high,
                                   create_range_type (gnu_index_base_type,
-                                                     gnu_low, gnu_high),
+                                                     gnu_orig_min,
+                                                     gnu_orig_max),
                                   gnat_entity);
 
            /* Update the maximum size of the array in elements.  */
@@ -1926,78 +1976,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            = chainon (gnu_template_fields, gnu_temp_fields[index]);
 
        /* Install all the fields into the template.  */
-       finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
+       finish_record_type (gnu_template_type, gnu_template_fields, 0,
+                           debug_info_p);
        TYPE_READONLY (gnu_template_type) = 1;
 
        /* Now make the array of arrays and update the pointer to the array
           in the fat pointer.  Note that it is the first field.  */
-       tem = gnat_to_gnu_type (Component_Type (gnat_entity));
-
-       /* Try to get a smaller form of the component if needed.  */
-       if ((Is_Packed (gnat_entity)
-            || Has_Component_Size_Clause (gnat_entity))
-           && !Is_Bit_Packed_Array (gnat_entity)
-           && !Has_Aliased_Components (gnat_entity)
-           && !Strict_Alignment (Component_Type (gnat_entity))
-           && TREE_CODE (tem) == RECORD_TYPE
-           && !TYPE_IS_FAT_POINTER_P (tem)
-           && host_integerp (TYPE_SIZE (tem), 1))
-         tem = make_packable_type (tem, false);
-
-       if (Has_Atomic_Components (gnat_entity))
-         check_ok_for_atomic (tem, gnat_entity, true);
-
-       /* Get and validate any specified Component_Size, but if Packed,
-          ignore it since the front end will have taken care of it.  */
-       gnu_comp_size
-         = validate_size (Component_Size (gnat_entity), tem,
-                          gnat_entity,
-                          (Is_Bit_Packed_Array (gnat_entity)
-                           ? TYPE_DECL : VAR_DECL),
-                          true, Has_Component_Size_Clause (gnat_entity));
-
-       /* If the component type is a RECORD_TYPE that has a self-referential
-          size, use the maximum size.  */
-       if (!gnu_comp_size
-           && TREE_CODE (tem) == RECORD_TYPE
-           && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
-         gnu_comp_size = max_size (TYPE_SIZE (tem), true);
-
-       if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
-         {
-           tree orig_tem = tem;
-           unsigned int max_align;
-
-           /* If an alignment is specified, use it as a cap on the component
-              type so that it can be honored for the whole type.  But ignore
-              it for the original type of packed array types.  */
-           if (No (Packed_Array_Type (gnat_entity))
-               && Known_Alignment (gnat_entity))
-             max_align = validate_alignment (Alignment (gnat_entity),
-                                             gnat_entity, 0);
-           else
-             max_align = 0;
-
-           tem = make_type_from_size (tem, gnu_comp_size, false);
-           if (max_align > 0 && TYPE_ALIGN (tem) > max_align)
-             tem = orig_tem;
-           else
-             orig_tem = tem;
-
-           tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
-                                 "C_PAD", false, definition, true);
-
-           /* If a padding record was made, declare it now since it will
-              never be declared otherwise.  This is necessary to ensure
-              that its subtrees are properly marked.  */
-           if (tem != orig_tem)
-             create_type_decl (TYPE_NAME (tem), tem, NULL, true,
-                               debug_info_p, gnat_entity);
-         }
-
-       if (Has_Volatile_Components (gnat_entity))
-         tem = build_qualified_type (tem,
-                                     TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
+       tem = gnat_to_gnu_component_type (gnat_entity, definition,
+                                         debug_info_p);
 
        /* If Component_Size is not already specified, annotate it with the
           size of the component.  */
@@ -2021,7 +2007,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          {
            tem = build_array_type (tem, gnu_index_types[index]);
            TYPE_MULTI_ARRAY_P (tem) = (index > 0);
-           if (array_type_has_nonaliased_component (gnat_entity, tem))
+           if (array_type_has_nonaliased_component (tem, gnat_entity))
              TYPE_NONALIASED_COMPONENT (tem) = 1;
          }
 
@@ -2050,7 +2036,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
 
        /* If the maximum size doesn't overflow, use it.  */
-        if (gnu_max_size
+       if (gnu_max_size
            && TREE_CODE (gnu_max_size) == INTEGER_CST
            && !TREE_OVERFLOW (gnu_max_size)
            && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
@@ -2066,25 +2052,25 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                          tem, NULL, !Comes_From_Source (gnat_entity),
                          debug_info_p, gnat_entity);
 
-       /* Give the fat pointer type a name.  */
-       create_type_decl (create_concat_name (gnat_entity, "XUP"),
+       /* Give the fat pointer type a name.  If this is a packed type, tell
+          the debugger how to interpret the underlying bits.  */
+       if (Present (Packed_Array_Type (gnat_entity)))
+         gnat_name = Packed_Array_Type (gnat_entity);
+       else
+         gnat_name = gnat_entity;
+       create_type_decl (create_concat_name (gnat_name, "XUP"),
                          gnu_fat_type, NULL, true,
                          debug_info_p, gnat_entity);
 
-       /* Create the type to be used as what a thin pointer designates: an
-          record type for the object and its template with the field offsets
-          shifted to have the template at a negative offset.  */
+       /* Create the type to be used as what a thin pointer designates:
+          a record type for the object and its template with the fields
+          shifted to have the template at a negative offset.  */
        tem = build_unc_object_type (gnu_template_type, tem,
-                                    create_concat_name (gnat_entity, "XUT"));
+                                    create_concat_name (gnat_name, "XUT"));
        shift_unc_components_for_thin_pointers (tem);
 
        SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
        TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
-
-       /* Give the thin pointer type a name.  */
-       create_type_decl (create_concat_name (gnat_entity, "XUX"),
-                         build_pointer_type (tem), NULL, true,
-                         debug_info_p, gnat_entity);
       }
       break;
 
@@ -2093,7 +2079,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
       /* This is the actual data type for array variables.  Multidimensional
         arrays are implemented as arrays of arrays.  Note that arrays which
-        have sparse enumeration subtypes as index components create sparse
+        have sparse enumeration subtypes as index components create sparse
         arrays, which is obviously space inefficient but so much easier to
         code for now.
 
@@ -2105,7 +2091,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
       if (!Is_Constrained (gnat_entity))
-       break;
+       ;
       else
        {
          Entity_Id gnat_index, gnat_base_index;
@@ -2130,12 +2116,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
               gnat_base_index = Next_Index (gnat_base_index))
            {
              tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
-             tree prec = TYPE_RM_SIZE (gnu_index_type);
-             const bool wider_p
-               = (compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0
-                  || (compare_tree_int (prec, TYPE_PRECISION (sizetype)) == 0
-                      && TYPE_UNSIGNED (gnu_index_type)
-                         != TYPE_UNSIGNED (sizetype)));
              tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
              tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
              tree gnu_min = convert (sizetype, gnu_orig_min);
@@ -2160,8 +2140,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
              /* Similarly, if one of the values overflows in sizetype and the
                 range is null, use 1..0 for the sizetype bounds.  */
-             else if (wider_p
-                      && TREE_CODE (gnu_min) == INTEGER_CST
+             else if (TREE_CODE (gnu_min) == INTEGER_CST
                       && TREE_CODE (gnu_max) == INTEGER_CST
                       && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
                       && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
@@ -2174,8 +2153,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              /* If the minimum and maximum values both overflow in sizetype,
                 but the difference in the original type does not overflow in
                 sizetype, ignore the overflow indication.  */
-             else if (wider_p
-                      && TREE_CODE (gnu_min) == INTEGER_CST
+             else if (TREE_CODE (gnu_min) == INTEGER_CST
                       && TREE_CODE (gnu_max) == INTEGER_CST
                       && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
                       && !TREE_OVERFLOW
@@ -2194,41 +2172,49 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                 deal with the "superflat" case.  There are three ways to do
                 this.  If we can prove that the array can never be superflat,
                 we can just use the high bound of the index type.  */
-             else if (Nkind (gnat_index) == N_Range
-                      && cannot_be_superflat_p (gnat_index))
+             else if ((Nkind (gnat_index) == N_Range
+                       && cannot_be_superflat_p (gnat_index))
+                      /* Packed Array Types are never superflat.  */
+                      || Is_Packed_Array_Type (gnat_entity))
                gnu_high = gnu_max;
 
-             /* Otherwise, if we can prove that the low bound minus one and
-                the high bound cannot overflow, we can just use the expression
-                MAX (hb, lb - 1).  Otherwise, we have to use the most general
-                expression (hb >= lb) ? hb : lb - 1.  Note that the comparison
-                must be done in the original index type, to avoid any overflow
-                during the conversion.  */
-             else
+             /* Otherwise, if the high bound is constant but the low bound is
+                not, we use the expression (hb >= lb) ? lb : hb + 1 for the
+                lower bound.  Note that the comparison must be done in the
+                original type to avoid any overflow during the conversion.  */
+             else if (TREE_CODE (gnu_max) == INTEGER_CST
+                      && TREE_CODE (gnu_min) != INTEGER_CST)
                {
-                 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
-
-                 /* If gnu_high is a constant that has overflowed, the bound
-                    is the smallest integer so cannot be the maximum.  */
-                 if (TREE_CODE (gnu_high) == INTEGER_CST
-                     && TREE_OVERFLOW (gnu_high))
-                   gnu_high = gnu_max;
-
-                 /* If the index type is not wider and gnu_high is a constant
-                    that hasn't overflowed, we can use the maximum.  */
-                 else if (!wider_p && TREE_CODE (gnu_high) == INTEGER_CST)
-                   gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
-
-                 else
-                   gnu_high
-                     = build_cond_expr (sizetype,
-                                        build_binary_op (GE_EXPR,
-                                                         integer_type_node,
-                                                         gnu_orig_max,
-                                                         gnu_orig_min),
-                                        gnu_max, gnu_high);
+                 gnu_high = gnu_max;
+                 gnu_min
+                   = build_cond_expr (sizetype,
+                                      build_binary_op (GE_EXPR,
+                                                       boolean_type_node,
+                                                       gnu_orig_max,
+                                                       gnu_orig_min),
+                                      gnu_min,
+                                      size_binop (PLUS_EXPR, gnu_max,
+                                                  size_one_node));
                }
 
+             /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
+                in all the other cases.  Note that, here as well as above,
+                the condition used in the comparison must be equivalent to
+                the condition (length != 0).  This is relied upon in order
+                to optimize array comparisons in compare_arrays.  */
+             else
+               gnu_high
+                 = build_cond_expr (sizetype,
+                                    build_binary_op (GE_EXPR,
+                                                     boolean_type_node,
+                                                     gnu_orig_max,
+                                                     gnu_orig_min),
+                                    gnu_max,
+                                    size_binop (MINUS_EXPR, gnu_min,
+                                                size_one_node));
+
+             /* Reuse the index type for the range type.  Then make an index
+                type with the size range in sizetype.  */
              gnu_index_types[index]
                = create_index_type (gnu_min, gnu_high, gnu_index_type,
                                     gnat_entity);
@@ -2298,7 +2284,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                      && TREE_CODE (TREE_TYPE (gnu_index_type))
                         != INTEGER_TYPE)
                  || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
-                 || compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0)
+                 || compare_tree_int (rm_size (gnu_index_type),
+                                      TYPE_PRECISION (sizetype)) > 0)
                need_index_type_struct = true;
            }
 
@@ -2323,9 +2310,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            }
          else
            {
-             tree gnu_comp_size;
-
-             gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
+             gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
+                                                    debug_info_p);
 
              /* One of the above calls might have caused us to be elaborated,
                 so don't blow up if so.  */
@@ -2334,73 +2320,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  maybe_present = true;
                  break;
                }
-
-             /* Try to get a smaller form of the component if needed.  */
-             if ((Is_Packed (gnat_entity)
-                  || Has_Component_Size_Clause (gnat_entity))
-                 && !Is_Bit_Packed_Array (gnat_entity)
-                 && !Has_Aliased_Components (gnat_entity)
-                 && !Strict_Alignment (Component_Type (gnat_entity))
-                 && TREE_CODE (gnu_type) == RECORD_TYPE
-                 && !TYPE_IS_FAT_POINTER_P (gnu_type)
-                 && host_integerp (TYPE_SIZE (gnu_type), 1))
-               gnu_type = make_packable_type (gnu_type, false);
-
-             /* Get and validate any specified Component_Size, but if Packed,
-                ignore it since the front end will have taken care of it.  */
-             gnu_comp_size
-               = validate_size (Component_Size (gnat_entity), gnu_type,
-                                gnat_entity,
-                                (Is_Bit_Packed_Array (gnat_entity)
-                                 ? TYPE_DECL : VAR_DECL), true,
-                                Has_Component_Size_Clause (gnat_entity));
-
-             /* If the component type is a RECORD_TYPE that has a
-                self-referential size, use the maximum size.  */
-             if (!gnu_comp_size
-                 && TREE_CODE (gnu_type) == RECORD_TYPE
-                 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
-               gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
-
-             if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
-               {
-                 tree orig_gnu_type = gnu_type;
-                 unsigned int max_align;
-
-                 /* If an alignment is specified, use it as a cap on the
-                    component type so that it can be honored for the whole
-                    type.  But ignore it for the original type of packed
-                    array types.  */
-                 if (No (Packed_Array_Type (gnat_entity))
-                     && Known_Alignment (gnat_entity))
-                   max_align = validate_alignment (Alignment (gnat_entity),
-                                                   gnat_entity, 0);
-                 else
-                   max_align = 0;
-
-                 gnu_type
-                   = make_type_from_size (gnu_type, gnu_comp_size, false);
-                 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
-                   gnu_type = orig_gnu_type;
-                 else
-                   orig_gnu_type = gnu_type;
-
-                 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
-                                            gnat_entity, "C_PAD", false,
-                                            definition, true);
-
-                 /* If a padding record was made, declare it now since it
-                    will never be declared otherwise.  This is necessary
-                    to ensure that its subtrees are properly marked.  */
-                 if (gnu_type != orig_gnu_type)
-                   create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
-                                     true, debug_info_p, gnat_entity);
-               }
-
-             if (Has_Volatile_Components (Base_Type (gnat_entity)))
-               gnu_type = build_qualified_type (gnu_type,
-                                                (TYPE_QUALS (gnu_type)
-                                                 | TYPE_QUAL_VOLATILE));
            }
 
          /* Compute the maximum size of the array in units and bits.  */
@@ -2420,7 +2339,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
              TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
-             if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
+             if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
                TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
            }
 
@@ -2465,7 +2384,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
                  /* ??? create_type_decl is not invoked on the inner types so
                     the MULT_EXPR node built above will never be marked.  */
-                 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
+                 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
                }
            }
 
@@ -2501,7 +2420,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  gnu_field_list = gnu_field;
                }
 
-             finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
+             finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
              add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
            }
 
@@ -2538,105 +2457,104 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          /* Set our alias set to that of our base type.  This gives all
             array subtypes the same alias set.  */
          relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
-       }
 
-      /* If this is a packed type, make this type the same as the packed
-        array type, but do some adjusting in the type first.  */
-      if (Present (Packed_Array_Type (gnat_entity)))
-       {
-         Entity_Id gnat_index;
-         tree gnu_inner_type;
-
-         /* First finish the type we had been making so that we output
-            debugging information for it.  */
-         gnu_type
-           = build_qualified_type (gnu_type,
-                                   (TYPE_QUALS (gnu_type)
-                                    | (TYPE_QUAL_VOLATILE
-                                       * Treat_As_Volatile (gnat_entity))));
-
-         /* Make it artificial only if the base type was artificial as well.
-            That's sort of "morally" true and will make it possible for the
-            debugger to look it up by name in DWARF, which is necessary in
-            order to decode the packed array type.  */
-         gnu_decl
-           = create_type_decl (gnu_entity_name, gnu_type, attr_list,
-                               !Comes_From_Source (gnat_entity)
-                               && !Comes_From_Source (Etype (gnat_entity)),
-                               debug_info_p, gnat_entity);
-
-         /* Save it as our equivalent in case the call below elaborates
-            this type again.  */
-         save_gnu_tree (gnat_entity, gnu_decl, false);
-
-         gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
-                                        NULL_TREE, 0);
-         this_made_decl = true;
-         gnu_type = TREE_TYPE (gnu_decl);
-         save_gnu_tree (gnat_entity, NULL_TREE, false);
-
-         gnu_inner_type = gnu_type;
-         while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
-                && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
-                    || TYPE_IS_PADDING_P (gnu_inner_type)))
-           gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
-
-         /* We need to attach the index type to the type we just made so
-            that the actual bounds can later be put into a template.  */
-         if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
-              && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
-             || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
-                 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
+         /* If this is a packed type, make this type the same as the packed
+            array type, but do some adjusting in the type first.  */
+         if (Present (Packed_Array_Type (gnat_entity)))
            {
-             if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
+             Entity_Id gnat_index;
+             tree gnu_inner;
+
+             /* First finish the type we had been making so that we output
+                debugging information for it.  */
+             if (Treat_As_Volatile (gnat_entity))
+               gnu_type
+                 = build_qualified_type (gnu_type,
+                                         TYPE_QUALS (gnu_type)
+                                         | TYPE_QUAL_VOLATILE);
+
+             /* Make it artificial only if the base type was artificial too.
+                That's sort of "morally" true and will make it possible for
+                the debugger to look it up by name in DWARF, which is needed
+                in order to decode the packed array type.  */
+             gnu_decl
+               = create_type_decl (gnu_entity_name, gnu_type, attr_list,
+                                   !Comes_From_Source (Etype (gnat_entity))
+                                   && !Comes_From_Source (gnat_entity),
+                                   debug_info_p, gnat_entity);
+
+             /* Save it as our equivalent in case the call below elaborates
+                this type again.  */
+             save_gnu_tree (gnat_entity, gnu_decl, false);
+
+             gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
+                                            NULL_TREE, 0);
+             this_made_decl = true;
+             gnu_type = TREE_TYPE (gnu_decl);
+             save_gnu_tree (gnat_entity, NULL_TREE, false);
+
+             gnu_inner = gnu_type;
+             while (TREE_CODE (gnu_inner) == RECORD_TYPE
+                    && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
+                        || TYPE_PADDING_P (gnu_inner)))
+               gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
+
+             /* We need to attach the index type to the type we just made so
+                that the actual bounds can later be put into a template.  */
+             if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
+                  && !TYPE_ACTUAL_BOUNDS (gnu_inner))
+                 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
+                     && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
                {
-                 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
-                    TYPE_MODULUS for modular types so we make an extra
-                    subtype if necessary.  */
-                 if (TYPE_MODULAR_P (gnu_inner_type))
+                 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
                    {
-                     tree gnu_subtype
-                       = make_unsigned_type (TYPE_PRECISION (gnu_inner_type));
-                     TREE_TYPE (gnu_subtype) = gnu_inner_type;
-                     TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
-                     SET_TYPE_RM_MIN_VALUE (gnu_subtype,
-                                            TYPE_MIN_VALUE (gnu_inner_type));
-                     SET_TYPE_RM_MAX_VALUE (gnu_subtype,
-                                            TYPE_MAX_VALUE (gnu_inner_type));
-                     gnu_inner_type = gnu_subtype;
-                   }
-
-                 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
+                     /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
+                        TYPE_MODULUS for modular types so we make an extra
+                        subtype if necessary.  */
+                     if (TYPE_MODULAR_P (gnu_inner))
+                       {
+                         tree gnu_subtype
+                           = make_unsigned_type (TYPE_PRECISION (gnu_inner));
+                         TREE_TYPE (gnu_subtype) = gnu_inner;
+                         TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
+                         SET_TYPE_RM_MIN_VALUE (gnu_subtype,
+                                                TYPE_MIN_VALUE (gnu_inner));
+                         SET_TYPE_RM_MAX_VALUE (gnu_subtype,
+                                                TYPE_MAX_VALUE (gnu_inner));
+                         gnu_inner = gnu_subtype;
+                       }
+
+                     TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
 
 #ifdef ENABLE_CHECKING
-                 /* Check for other cases of overloading.  */
-                 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
+                     /* Check for other cases of overloading.  */
+                     gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
 #endif
-               }
+                   }
 
-             for (gnat_index = First_Index (gnat_entity);
-                  Present (gnat_index); gnat_index = Next_Index (gnat_index))
-               SET_TYPE_ACTUAL_BOUNDS
-                 (gnu_inner_type,
-                  tree_cons (NULL_TREE,
-                             get_unpadded_type (Etype (gnat_index)),
-                             TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
-
-             if (Convention (gnat_entity) != Convention_Fortran)
-               SET_TYPE_ACTUAL_BOUNDS
-                 (gnu_inner_type,
-                  nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
-
-             if (TREE_CODE (gnu_type) == RECORD_TYPE
-                 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
-               TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
+                 for (gnat_index = First_Index (gnat_entity);
+                      Present (gnat_index);
+                      gnat_index = Next_Index (gnat_index))
+                   SET_TYPE_ACTUAL_BOUNDS
+                     (gnu_inner,
+                      tree_cons (NULL_TREE,
+                                 get_unpadded_type (Etype (gnat_index)),
+                                 TYPE_ACTUAL_BOUNDS (gnu_inner)));
+
+                 if (Convention (gnat_entity) != Convention_Fortran)
+                   SET_TYPE_ACTUAL_BOUNDS
+                     (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
+
+                 if (TREE_CODE (gnu_type) == RECORD_TYPE
+                     && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
+                   TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
+               }
            }
-       }
-
-      /* Abort if packed array with no packed array type field set.  */
-      else
-       gcc_assert (!Is_Packed (gnat_entity));
 
+         else
+           /* Abort if packed array with no Packed_Array_Type field set.  */
+           gcc_assert (!Is_Packed (gnat_entity));
+       }
       break;
 
     case E_String_Literal_Subtype:
@@ -2672,7 +2590,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_type
          = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
                              gnu_index_type);
-       if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
+       if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
          TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
        relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
       }
@@ -2838,15 +2756,16 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   Present (gnat_field);
                   gnat_field = Next_Stored_Discriminant (gnat_field))
                if (Present (Corresponding_Discriminant (gnat_field)))
-                 save_gnu_tree
-                   (gnat_field,
-                    build3 (COMPONENT_REF,
-                            get_unpadded_type (Etype (gnat_field)),
-                            gnu_get_parent,
-                            gnat_to_gnu_field_decl (Corresponding_Discriminant
-                                                    (gnat_field)),
-                            NULL_TREE),
-                    true);
+                 {
+                   tree gnu_field
+                     = gnat_to_gnu_field_decl (Corresponding_Discriminant
+                                               (gnat_field));
+                   save_gnu_tree
+                     (gnat_field,
+                      build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
+                              gnu_get_parent, gnu_field, NULL_TREE),
+                      true);
+                 }
 
            /* Then we build the parent subtype.  If it has discriminants but
               the type itself has unknown discriminants, this means that it
@@ -2927,8 +2846,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
            /* ...and reference the _Parent field of this record.  */
            gnu_field
-             = create_field_decl (get_identifier
-                                  (Get_Name_String (Name_uParent)),
+             = create_field_decl (parent_name_id,
                                   gnu_parent, gnu_type, 0,
                                   has_rep
                                   ? TYPE_SIZE (gnu_parent) : NULL_TREE,
@@ -2953,7 +2871,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                continue;
 
              gnu_field
-               = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
+               = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
+                                    debug_info_p);
 
              /* Make an expression using a PLACEHOLDER_EXPR from the
                 FIELD_DECL node just created and link that with the
@@ -2974,12 +2893,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* Add the fields into the record type and finish it up.  */
        components_to_record (gnu_type, Component_List (record_definition),
                              gnu_field_list, packed, definition, NULL,
-                             false, all_rep, false, is_unchecked_union);
+                             false, all_rep, is_unchecked_union,
+                             debug_info_p, false);
 
-       /* If it is a tagged record force the type to BLKmode to insure that
-          these objects will always be put in memory.  Likewise for limited
-          record types.  */
-       if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
+       /* If it is passed by reference, force BLKmode to ensure that objects
+          of this type will always be put in memory.  */
+       if (Is_By_Reference_Type (gnat_entity))
          SET_TYPE_MODE (gnu_type, BLKmode);
 
        /* We used to remove the associations of the discriminants and _Parent
@@ -3057,9 +2976,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            }
 
          /* When the subtype has discriminants and these discriminants affect
-            the initial shape it has inherited, factor them in.  But for the
-            of an Unchecked_Union (it must be an Itype), just return the type.
-
+            the initial shape it has inherited, factor them in.  But for an
+            Unchecked_Union (it must be an Itype), just return the type.
             We can't just test Is_Constrained because private subtypes without
             discriminants of types with discriminants with default expressions
             are Is_Constrained but aren't constrained!  */
@@ -3073,54 +2991,75 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              tree gnu_subst_list
                = build_subst_list (gnat_entity, gnat_base_type, definition);
-             tree gnu_pos_list, gnu_field_list = NULL_TREE;
-             tree gnu_unpad_base_type, t;
+             tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
+             tree gnu_variant_list, gnu_pos_list, gnu_field_list = NULL_TREE;
+             bool selected_variant = false;
              Entity_Id gnat_field;
 
              gnu_type = make_node (RECORD_TYPE);
              TYPE_NAME (gnu_type) = gnu_entity_name;
 
              /* Set the size, alignment and alias set of the new type to
-                match that of the old one, doing required substitutions.
-                We do it this early because we need the size of the new
-                type below to discard old fields if necessary.  */
-             TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
-             TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
-             SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
-             TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
-             relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
-
-             if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
-               for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
-                 TYPE_SIZE (gnu_type)
-                   = substitute_in_expr (TYPE_SIZE (gnu_type),
-                                         TREE_PURPOSE (t),
-                                         TREE_VALUE (t));
-
-             if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
-               for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
-                 TYPE_SIZE_UNIT (gnu_type)
-                   = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
-                                         TREE_PURPOSE (t),
-                                         TREE_VALUE (t));
-
-             if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
-               for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
-                 SET_TYPE_ADA_SIZE
-                   (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
-                                                  TREE_PURPOSE (t),
-                                                  TREE_VALUE (t)));
-
-             if (TREE_CODE (gnu_base_type) == RECORD_TYPE
-                 && TYPE_IS_PADDING_P (gnu_base_type))
+                match that of the old one, doing required substitutions.  */
+             copy_and_substitute_in_size (gnu_type, gnu_base_type,
+                                          gnu_subst_list);
+
+             if (TYPE_IS_PADDING_P (gnu_base_type))
                gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
              else
                gnu_unpad_base_type = gnu_base_type;
 
+             /* Look for a REP part in the base type.  */
+             gnu_rep_part = get_rep_part (gnu_unpad_base_type);
+
+             /* Look for a variant part in the base type.  */
+             gnu_variant_part = get_variant_part (gnu_unpad_base_type);
+
+             /* If there is a variant part, we must compute whether the
+                constraints statically select a particular variant.  If
+                so, we simply drop the qualified union and flatten the
+                list of fields.  Otherwise we'll build a new qualified
+                union for the variants that are still relevant.  */
+             if (gnu_variant_part)
+               {
+                 gnu_variant_list
+                   = build_variant_list (TREE_TYPE (gnu_variant_part),
+                                         gnu_subst_list, NULL_TREE);
+
+                 /* If all the qualifiers are unconditionally true, the
+                    innermost variant is statically selected.  */
+                 selected_variant = true;
+                 for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
+                   if (!integer_onep (TREE_VEC_ELT (TREE_VALUE (t), 1)))
+                     {
+                       selected_variant = false;
+                       break;
+                     }
+
+                 /* Otherwise, create the new variants.  */
+                 if (!selected_variant)
+                   for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
+                     {
+                       tree old_variant = TREE_PURPOSE (t);
+                       tree new_variant = make_node (RECORD_TYPE);
+                       TYPE_NAME (new_variant)
+                         = DECL_NAME (TYPE_NAME (old_variant));
+                       copy_and_substitute_in_size (new_variant, old_variant,
+                                                    gnu_subst_list);
+                       TREE_VEC_ELT (TREE_VALUE (t), 2) = new_variant;
+                     }
+               }
+             else
+               {
+                 gnu_variant_list = NULL_TREE;
+                 selected_variant = false;
+               }
+
              gnu_pos_list
-               = compute_field_positions (gnu_unpad_base_type, NULL_TREE,
-                                          size_zero_node, bitsize_zero_node,
-                                          BIGGEST_ALIGNMENT);
+               = build_position_list (gnu_unpad_base_type,
+                                      gnu_variant_list && !selected_variant,
+                                      size_zero_node, bitsize_zero_node,
+                                      BIGGEST_ALIGNMENT, NULL_TREE);
 
              for (gnat_field = First_Entity (gnat_entity);
                   Present (gnat_field);
@@ -3138,16 +3077,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                      = Original_Record_Component (gnat_field);
                    tree gnu_old_field
                      = gnat_to_gnu_field_decl (gnat_old_field);
-                   tree gnu_offset
-                     = TREE_VALUE
-                       (purpose_member (gnu_old_field, gnu_pos_list));
-                   tree gnu_pos = TREE_PURPOSE (gnu_offset);
-                   tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
-                   tree gnu_field, gnu_field_type, gnu_size, gnu_new_pos;
-                   tree gnu_last = NULL_TREE;
-                   unsigned int offset_align
-                     = tree_low_cst
-                       (TREE_PURPOSE (TREE_VALUE (gnu_offset)), 1);
+                   tree gnu_context = DECL_CONTEXT (gnu_old_field);
+                   tree gnu_field, gnu_field_type, gnu_size;
+                   tree gnu_cont_type, gnu_last = NULL_TREE;
 
                    /* If the type is the same, retrieve the GCC type from the
                       old field to take into account possible adjustments.  */
@@ -3182,7 +3114,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                      {
                        gnu_size = DECL_SIZE (gnu_old_field);
                        if (TREE_CODE (gnu_field_type) == RECORD_TYPE
-                           && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
+                           && !TYPE_FAT_POINTER_P (gnu_field_type)
                            && host_integerp (TYPE_SIZE (gnu_field_type), 1))
                          gnu_field_type
                            = make_packable_type (gnu_field_type, true);
@@ -3191,67 +3123,50 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    else
                      gnu_size = TYPE_SIZE (gnu_field_type);
 
-                   if (CONTAINS_PLACEHOLDER_P (gnu_pos))
-                     for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
-                       gnu_pos = substitute_in_expr (gnu_pos,
-                                                     TREE_PURPOSE (t),
-                                                     TREE_VALUE (t));
-
-                   /* If the position is now a constant, we can set it as the
-                      position of the field when we make it.  Otherwise, we
-                      need to deal with it specially below.  */
-                   if (TREE_CONSTANT (gnu_pos))
+                   /* If the context of the old field is the base type or its
+                      REP part (if any), put the field directly in the new
+                      type; otherwise look up the context in the variant list
+                      and put the field either in the new type if there is a
+                      selected variant or in one of the new variants.  */
+                   if (gnu_context == gnu_unpad_base_type
+                       || (gnu_rep_part
+                           && gnu_context == TREE_TYPE (gnu_rep_part)))
+                     gnu_cont_type = gnu_type;
+                   else
                      {
-                       gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
-
-                       /* Discard old fields that are outside the new type.
-                          This avoids confusing code scanning it to decide
-                          how to pass it to functions on some platforms.  */
-                       if (TREE_CODE (gnu_new_pos) == INTEGER_CST
-                           && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
-                           && !integer_zerop (gnu_size)
-                           && !tree_int_cst_lt (gnu_new_pos,
-                                                TYPE_SIZE (gnu_type)))
+                       t = purpose_member (gnu_context, gnu_variant_list);
+                       if (t)
+                         {
+                           if (selected_variant)
+                             gnu_cont_type = gnu_type;
+                           else
+                             gnu_cont_type = TREE_VEC_ELT (TREE_VALUE (t), 2);
+                         }
+                       else
+                         /* The front-end may pass us "ghost" components if
+                            it fails to recognize that a constrained subtype
+                            is statically constrained.  Discard them.  */
                          continue;
                      }
-                   else
-                     gnu_new_pos = NULL_TREE;
 
+                   /* Now create the new field modeled on the old one.  */
                    gnu_field
-                     = create_field_decl
-                       (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
-                        DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
-                        !DECL_NONADDRESSABLE_P (gnu_old_field));
+                     = create_field_decl_from (gnu_old_field, gnu_field_type,
+                                               gnu_cont_type, gnu_size,
+                                               gnu_pos_list, gnu_subst_list);
 
-                   if (!TREE_CONSTANT (gnu_pos))
+                   /* Put it in one of the new variants directly.  */
+                   if (gnu_cont_type != gnu_type)
                      {
-                       normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
-                       DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
-                       DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
-                       SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
-                       DECL_SIZE (gnu_field) = gnu_size;
-                       DECL_SIZE_UNIT (gnu_field)
-                         = convert (sizetype,
-                                    size_binop (CEIL_DIV_EXPR, gnu_size,
-                                                bitsize_unit_node));
-                       layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
+                       TREE_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
+                       TYPE_FIELDS (gnu_cont_type) = gnu_field;
                      }
 
-                   DECL_INTERNAL_P (gnu_field)
-                     = DECL_INTERNAL_P (gnu_old_field);
-                   SET_DECL_ORIGINAL_FIELD
-                     (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
-                                  ? DECL_ORIGINAL_FIELD (gnu_old_field)
-                                  : gnu_old_field));
-                   DECL_DISCRIMINANT_NUMBER (gnu_field)
-                     = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
-                   TREE_THIS_VOLATILE (gnu_field)
-                     = TREE_THIS_VOLATILE (gnu_old_field);
-
                    /* To match the layout crafted in components_to_record,
                       if this is the _Tag or _Parent field, put it before
                       any other fields.  */
-                   if (gnat_name == Name_uTag || gnat_name == Name_uParent)
+                   else if (gnat_name == Name_uTag
+                            || gnat_name == Name_uParent)
                      gnu_field_list = chainon (gnu_field_list, gnu_field);
 
                    /* Similarly, if this is the _Controller field, put
@@ -3276,6 +3191,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    save_gnu_tree (gnat_field, gnu_field, false);
                  }
 
+             /* If there is a variant list and no selected variant, we need
+                to create the nest of variant parts from the old nest.  */
+             if (gnu_variant_list && !selected_variant)
+               {
+                 tree new_variant_part
+                   = create_variant_part_from (gnu_variant_part,
+                                               gnu_variant_list, gnu_type,
+                                               gnu_pos_list, gnu_subst_list);
+                 TREE_CHAIN (new_variant_part) = gnu_field_list;
+                 gnu_field_list = new_variant_part;
+               }
+
              /* Now go through the entities again looking for Itypes that
                 we have not elaborated but should (e.g., Etypes of fields
                 that have Original_Components).  */
@@ -3286,18 +3213,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    && !present_gnu_tree (Etype (gnat_field)))
                  gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
 
-             /* Do not finalize it since we're going to modify it below.  */
+             /* Do not emit debug info for the type yet since we're going to
+                modify it below.  */
              gnu_field_list = nreverse (gnu_field_list);
-             finish_record_type (gnu_type, gnu_field_list, 2, true);
-
-             /* Finalize size and mode.  */
-             TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
-             TYPE_SIZE_UNIT (gnu_type)
-               = variable_size (TYPE_SIZE_UNIT (gnu_type));
+             finish_record_type (gnu_type, gnu_field_list, 2, false);
 
              /* See the E_Record_Type case for the rationale.  */
-             if (Is_Tagged_Type (gnat_entity)
-                 || Is_Limited_Record (gnat_entity))
+             if (Is_By_Reference_Type (gnat_entity))
                SET_TYPE_MODE (gnu_type, BLKmode);
              else
                compute_record_mode (gnu_type);
@@ -3307,13 +3229,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              /* Fill in locations of fields.  */
              annotate_rep (gnat_entity, gnu_type);
 
-             /* We've built a new type, make an XVS type to show what this
-                is a subtype of.  Some debuggers require the XVS type to be
-                output first, so do it in that order.  */
+             /* If debugging information is being written for the type, write
+                a record that shows what we are a subtype of and also make a
+                variable that indicates our size, if still variable.  */
              if (debug_info_p)
                {
                  tree gnu_subtype_marker = make_node (RECORD_TYPE);
                  tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
+                 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
 
                  if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
                    gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
@@ -3327,10 +3250,20 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                                         gnu_subtype_marker,
                                                         0, NULL_TREE,
                                                         NULL_TREE, 0),
-                                     0, false);
+                                     0, true);
 
                  add_parallel_type (TYPE_STUB_DECL (gnu_type),
                                     gnu_subtype_marker);
+
+                 if (definition
+                     && TREE_CODE (gnu_size_unit) != INTEGER_CST
+                     && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
+                   TYPE_SIZE_UNIT (gnu_subtype_marker)
+                     = create_var_decl (create_concat_name (gnat_entity,
+                                                            "XVZ"),
+                                        NULL_TREE, sizetype, gnu_size_unit,
+                                        false, false, false, false, NULL,
+                                        gnat_entity);
                }
 
              /* Now we can finalize it.  */
@@ -3538,7 +3471,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
                TYPE_POINTER_TO (gnu_old) = gnu_type;
 
-               Sloc_to_locus (Sloc (gnat_entity), &input_location);
                fields
                  = chainon (chainon (NULL_TREE,
                                      create_field_decl
@@ -3552,11 +3484,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                /* Make sure we can place this into a register.  */
                TYPE_ALIGN (gnu_type)
                  = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
-               TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
+               TYPE_FAT_POINTER_P (gnu_type) = 1;
 
-               /* Do not finalize this record type since the types of
-                  its fields are incomplete.  */
-               finish_record_type (gnu_type, fields, 0, true);
+               /* Do not emit debug info for this record type since the types
+                  of its fields are incomplete.  */
+               finish_record_type (gnu_type, fields, 0, false);
 
                TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
                TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
@@ -3686,11 +3618,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if ((! in_main_unit || is_from_limited_with) && made_dummy)
          {
            tree gnu_old_type
-             = TYPE_FAT_POINTER_P (gnu_type)
+             = TYPE_IS_FAT_POINTER_P (gnu_type)
                ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
 
            if (esize == POINTER_SIZE
-               && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
+               && (got_fat_p || TYPE_IS_FAT_POINTER_P (gnu_type)))
              gnu_type
                = build_pointer_type
                  (TYPE_OBJECT_RECORD_TYPE
@@ -3871,13 +3803,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        tree gnu_field_list = NULL_TREE;
        /* Non-null for subprograms containing parameters passed by copy-in
           copy-out (Ada In Out or Out parameters not passed by reference),
-          in which case it is the list of nodes used to specify the values of
-          the in out/out parameters that are returned as a record upon
+          in which case it is the list of nodes used to specify the values
+          of the In Out/Out parameters that are returned as a record upon
           procedure return.  The TREE_PURPOSE of an element of this list is
           a field of the record and the TREE_VALUE is the PARM_DECL
           corresponding to that field.  This list will be saved in the
           TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create.  */
-       tree gnu_return_list = NULL_TREE;
+       tree gnu_cico_list = NULL_TREE;
        /* If an import pragma asks to map this subprogram to a GCC builtin,
           this is the builtin DECL node.  */
        tree gnu_builtin_decl = NULL_TREE;
@@ -3903,9 +3835,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
             && Is_Pure (gnat_entity));
 
        bool volatile_flag = No_Return (gnat_entity);
-       bool returns_by_ref = false;
-       bool returns_unconstrained = false;
-       bool returns_by_target_ptr = false;
+       bool return_by_direct_ref_p = false;
+       bool return_by_invisi_ref_p = false;
+       bool return_unconstrained_p = false;
        bool has_copy_in_out = false;
        bool has_stub = false;
        int parmnum;
@@ -3957,37 +3889,39 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (kind == E_Function || kind == E_Subprogram_Type)
          gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
 
-       /* If this function returns by reference, make the actual
-          return type of this function the pointer and mark the decl.  */
+       /* If this function returns by reference, make the actual return
+          type of this function the pointer and mark the decl.  */
        if (Returns_By_Ref (gnat_entity))
          {
-           returns_by_ref = true;
            gnu_return_type = build_pointer_type (gnu_return_type);
+           return_by_direct_ref_p = true;
          }
 
-       /* If the Mechanism is By_Reference, ensure the return type uses
-          the machine's by-reference mechanism, which may not the same
-          as above (e.g., it might be by passing a fake parameter).  */
-       else if (kind == E_Function
-                && Mechanism (gnat_entity) == By_Reference)
-         {
-           TREE_ADDRESSABLE (gnu_return_type) = 1;
-
-           /* We expect this bit to be reset by gigi shortly, so can avoid a
-              type node copy here.  This actually also prevents troubles with
-              the generation of debug information for the function, because
-              we might have issued such info for this type already, and would
-              be attaching a distinct type node to the function if we made a
-              copy here.  */
-         }
-
-       /* If we are supposed to return an unconstrained array,
-          actually return a fat pointer and make a note of that.  Return
-          a pointer to an unconstrained record of variable size.  */
+       /* If the Mechanism is By_Reference, ensure this function uses the
+          target's by-invisible-reference mechanism, which may not be the
+          same as above (e.g. it might be passing an extra parameter).
+
+          Prior to GCC 4, this was handled by just setting TREE_ADDRESSABLE
+          on the result type.  Everything required to pass by invisible
+          reference using the target's mechanism (e.g. an extra parameter)
+          was handled at RTL expansion time.
+
+          This doesn't work with GCC 4 any more for several reasons.  First,
+          the gimplification process might need to create temporaries of this
+          type and the gimplifier ICEs on such attempts; that's why the flag
+          is now set on the function type instead.  Second, the middle-end
+          now also relies on a different attribute, DECL_BY_REFERENCE on the
+          RESULT_DECL, and expects the by-invisible-reference-ness to be made
+          explicit in the function body.  */
+       else if (kind == E_Function && Mechanism (gnat_entity) == By_Reference)
+         return_by_invisi_ref_p = true;
+
+       /* If we are supposed to return an unconstrained array, actually return
+          a fat pointer and make a note of that.  */
        else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
          {
            gnu_return_type = TREE_TYPE (gnu_return_type);
-           returns_unconstrained = true;
+           return_unconstrained_p = true;
          }
 
        /* If the type requires a transient scope, the result is allocated
@@ -3996,33 +3930,29 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        else if (Requires_Transient_Scope (Etype (gnat_entity)))
          {
            gnu_return_type = build_pointer_type (gnu_return_type);
-           returns_unconstrained = true;
+           return_unconstrained_p = true;
          }
 
        /* If the type is a padded type and the underlying type would not
           be passed by reference or this function has a foreign convention,
           return the underlying type.  */
-       else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
-                && TYPE_IS_PADDING_P (gnu_return_type)
+       else if (TYPE_IS_PADDING_P (gnu_return_type)
                 && (!default_pass_by_ref (TREE_TYPE
                                           (TYPE_FIELDS (gnu_return_type)))
                     || Has_Foreign_Convention (gnat_entity)))
          gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
 
-       /* If the return type has a non-constant size, we convert the function
-          into a procedure and its caller will pass a pointer to an object as
-          the first parameter when we call the function.  This can happen for
-          an unconstrained type with a maximum size or a constrained type with
-          a size not known at compile time.  */
-       if (TYPE_SIZE_UNIT (gnu_return_type)
-           && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
+       /* If the return type is unconstrained, that means it must have a
+          maximum size.  Use the padded type as the effective return type.
+          And ensure the function uses the target's by-invisible-reference
+          mechanism to avoid copying too much data when it returns.  */
+       if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
          {
-           returns_by_target_ptr = true;
-           gnu_param_list
-             = create_param_decl (get_identifier ("TARGET"),
-                                  build_reference_type (gnu_return_type),
-                                  true);
-           gnu_return_type = void_type_node;
+           gnu_return_type
+             = maybe_pad_type (gnu_return_type,
+                               max_size (TYPE_SIZE (gnu_return_type), true),
+                               0, gnat_entity, false, false, false, true);
+           return_by_invisi_ref_p = true;
          }
 
        /* If the return type has a size that overflows, we cannot have
@@ -4141,7 +4071,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   between two calls, so they can't be CSE'ed.  The latter
                   case also handles by-ref parameters.  */
                if (POINTER_TYPE_P (gnu_param_type)
-                   || TYPE_FAT_POINTER_P (gnu_param_type))
+                   || TYPE_IS_FAT_POINTER_P (gnu_param_type))
                  const_flag = false;
              }
 
@@ -4152,6 +4082,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
                    gnu_return_type = make_node (RECORD_TYPE);
                    TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
+                   /* Set a default alignment to speed up accesses.  */
+                   TYPE_ALIGN (gnu_return_type)
+                     = get_mode_alignment (ptr_mode);
                    has_copy_in_out = true;
                  }
 
@@ -4161,8 +4094,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                               &DECL_SOURCE_LOCATION (gnu_field));
                TREE_CHAIN (gnu_field) = gnu_field_list;
                gnu_field_list = gnu_field;
-               gnu_return_list = tree_cons (gnu_field, gnu_param,
-                                            gnu_return_list);
+               gnu_cico_list
+                 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
              }
          }
 
@@ -4170,13 +4103,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           stubbed since structures are incomplete for the back-end.  */
        if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
          finish_record_type (gnu_return_type, nreverse (gnu_field_list),
-                             0, false);
+                             0, debug_info_p);
 
        /* If we have a CICO list but it has only one entry, we convert
           this function into a function that simply returns that one
           object.  */
-       if (list_length (gnu_return_list) == 1)
-         gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
+       if (list_length (gnu_cico_list) == 1)
+         gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
 
        if (Has_Stdcall_Convention (gnat_entity))
          prepend_one_attribute_to
@@ -4201,22 +4134,25 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_param_list = nreverse (gnu_param_list);
        if (has_stub)
          gnu_stub_param_list = nreverse (gnu_stub_param_list);
-       gnu_return_list = nreverse (gnu_return_list);
+       gnu_cico_list = nreverse (gnu_cico_list);
 
        if (Ekind (gnat_entity) == E_Function)
-         Set_Mechanism (gnat_entity,
-                        (returns_by_ref || returns_unconstrained
-                         ? By_Reference : By_Copy));
+         Set_Mechanism (gnat_entity, return_unconstrained_p
+                                     || return_by_direct_ref_p
+                                     || return_by_invisi_ref_p
+                                     ? By_Reference : By_Copy);
        gnu_type
          = create_subprog_type (gnu_return_type, gnu_param_list,
-                                gnu_return_list, returns_unconstrained,
-                                returns_by_ref, returns_by_target_ptr);
+                                gnu_cico_list, return_unconstrained_p,
+                                return_by_direct_ref_p,
+                                return_by_invisi_ref_p);
 
        if (has_stub)
          gnu_stub_type
            = create_subprog_type (gnu_return_type, gnu_stub_param_list,
-                                  gnu_return_list, returns_unconstrained,
-                                  returns_by_ref, returns_by_target_ptr);
+                                  gnu_cico_list, return_unconstrained_p,
+                                  return_by_direct_ref_p,
+                                  return_by_invisi_ref_p);
 
        /* A subprogram (something that doesn't return anything) shouldn't
           be considered const since there would be no reason for such a
@@ -4231,8 +4167,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                  | (TYPE_QUAL_CONST * const_flag)
                                  | (TYPE_QUAL_VOLATILE * volatile_flag));
 
-       Sloc_to_locus (Sloc (gnat_entity), &input_location);
-
        if (has_stub)
          gnu_stub_type
            = build_qualified_type (gnu_stub_type,
@@ -4404,9 +4338,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        break;
       }
 
-      /* Simple class_wide types are always viewed as their root_type
-        by Gigi unless an Equivalent_Type is specified.  */
     case E_Class_Wide_Type:
+      /* Class-wide types are always transformed into their root type.  */
       gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
       maybe_present = true;
       break;
@@ -4452,12 +4385,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
      handling alignment and possible padding.  */
   if (is_type && (!gnu_decl || this_made_decl))
     {
+      /* Tell the middle-end that objects of tagged types are guaranteed to
+        be properly aligned.  This is necessary because conversions to the
+        class-wide type are translated into conversions to the root type,
+        which can be less aligned than some of its derived types.  */
       if (Is_Tagged_Type (gnat_entity)
          || Is_Class_Wide_Equivalent_Type (gnat_entity))
        TYPE_ALIGN_OK (gnu_type) = 1;
 
-      if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
-       TYPE_BY_REFERENCE_P (gnu_type) = 1;
+      /* If the type is passed by reference, objects of this type must be
+        fully addressable and cannot be copied.  */
+      if (Is_By_Reference_Type (gnat_entity))
+       TREE_ADDRESSABLE (gnu_type) = 1;
 
       /* ??? Don't set the size for a String_Literal since it is either
         confirming or we don't handle it properly (if the low bound is
@@ -4504,7 +4443,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              else if ((TREE_CODE (gnu_type) == RECORD_TYPE
                        || TREE_CODE (gnu_type) == UNION_TYPE
                        || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
-                      && !TYPE_IS_FAT_POINTER_P (gnu_type))
+                      && !TYPE_FAT_POINTER_P (gnu_type))
                size = rm_size (gnu_type);
              else
                size = TYPE_SIZE (gnu_type);
@@ -4533,10 +4472,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
         us when we make the new TYPE_DECL below.  */
       if (gnu_size || align > 0)
        gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
-                                  "PAD", true, definition, false);
+                                  false, !gnu_decl, definition, false);
 
-      if (TREE_CODE (gnu_type) == RECORD_TYPE
-         && TYPE_IS_PADDING_P (gnu_type))
+      if (TYPE_IS_PADDING_P (gnu_type))
        {
          gnu_entity_name = TYPE_NAME (gnu_type);
          if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
@@ -4630,14 +4568,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   the MULT_EXPR node built above may not be marked by the call
                   to create_type_decl below.  */
                if (global_bindings_p ())
-                 mark_visited (&DECL_FIELD_OFFSET (gnu_field));
+                 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
                }
            }
 
-      gnu_type = build_qualified_type (gnu_type,
-                                      (TYPE_QUALS (gnu_type)
-                                       | (TYPE_QUAL_VOLATILE
-                                          * Treat_As_Volatile (gnat_entity))));
+      if (Treat_As_Volatile (gnat_entity))
+       gnu_type
+         = build_qualified_type (gnu_type,
+                                 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
 
       if (Is_Atomic (gnat_entity))
        check_ok_for_atomic (gnu_type, gnat_entity, false);
@@ -4653,7 +4591,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                     !Comes_From_Source (gnat_entity),
                                     debug_info_p, gnat_entity);
       else
-       TREE_TYPE (gnu_decl) = gnu_type;
+       {
+         TREE_TYPE (gnu_decl) = gnu_type;
+         TYPE_STUB_DECL (gnu_type) = gnu_decl;
+       }
     }
 
   if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
@@ -4696,11 +4637,38 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   superset      superset
                R ----------> D ----------> T
 
+        However, for composite types, conversions between derived types are
+        translated into VIEW_CONVERT_EXPRs so a sequence like:
+
+           type Comp1 is new Comp;
+           type Comp2 is new Comp;
+           procedure Proc (C : Comp1);
+
+           C : Comp2;
+           Proc (Comp1 (C));
+
+        is translated into:
+
+           C : Comp2;
+           Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
+
+        and gimplified into:
+
+           C : Comp2;
+           Comp1 *C.0;
+           C.0 = (Comp1 *) &C;
+           Proc (C.0);
+
+        i.e. generates code involving type punning.  Therefore, Comp1 needs
+        to conflict with Comp2 and an alias set copy is required.
+
         The language rules ensure the parent type is already frozen here.  */
       if (Is_Derived_Type (gnat_entity))
        {
          tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
-         relate_alias_sets (gnu_type, gnu_parent_type, ALIAS_SET_SUPERSET);
+         relate_alias_sets (gnu_type, gnu_parent_type,
+                            Is_Composite_Type (gnat_entity)
+                            ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
        }
 
       /* Back-annotate the Alignment of the type if not already in the
@@ -4732,38 +4700,40 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
       if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
        {
-         /* If the size is self-referential, we annotate the maximum
-            value of that size.  */
          tree gnu_size = TYPE_SIZE (gnu_type);
 
+         /* If the size is self-referential, annotate the maximum value.  */
          if (CONTAINS_PLACEHOLDER_P (gnu_size))
            gnu_size = max_size (gnu_size, true);
 
-         Set_Esize (gnat_entity, annotate_value (gnu_size));
-
          if (type_annotate_only && Is_Tagged_Type (gnat_entity))
            {
-             /* In this mode the tag and the parent components are not
-                generated by the front-end, so the sizes must be adjusted
-                explicitly now.  */
-             int size_offset, new_size;
+             /* In this mode, the tag and the parent components are not
+                generated by the front-end so the sizes must be adjusted.  */
+             tree pointer_size = bitsize_int (POINTER_SIZE), offset;
+             Uint uint_size;
 
              if (Is_Derived_Type (gnat_entity))
                {
-                 size_offset
-                   = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
+                 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
+                                     bitsizetype);
                  Set_Alignment (gnat_entity,
                                 Alignment (Etype (Base_Type (gnat_entity))));
                }
              else
-               size_offset = POINTER_SIZE;
-
-             new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
-             Set_Esize (gnat_entity,
-                        UI_From_Int (((new_size + (POINTER_SIZE - 1))
-                                      / POINTER_SIZE) * POINTER_SIZE));
-             Set_RM_Size (gnat_entity, Esize (gnat_entity));
+               offset = pointer_size;
+
+             gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
+             gnu_size = size_binop (MULT_EXPR, pointer_size,
+                                               size_binop (CEIL_DIV_EXPR,
+                                                           gnu_size,
+                                                           pointer_size));
+             uint_size = annotate_value (gnu_size);
+             Set_Esize (gnat_entity, uint_size);
+             Set_RM_Size (gnat_entity, uint_size);
            }
+         else
+           Set_Esize (gnat_entity, annotate_value (gnu_size));
        }
 
       if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
@@ -4792,8 +4762,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       tree gnu_low_bound, gnu_high_bound;
 
       /* If this is a padded type, we need to use the underlying type.  */
-      if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
-         && TYPE_IS_PADDING_P (gnu_scalar_type))
+      if (TYPE_IS_PADDING_P (gnu_scalar_type))
        gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
 
       /* If this is a floating point type and we haven't set a floating
@@ -4939,7 +4908,7 @@ get_unpadded_type (Entity_Id gnat_entity)
 {
   tree type = gnat_to_gnu_type (gnat_entity);
 
-  if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
+  if (TYPE_IS_PADDING_P (type))
     type = TREE_TYPE (TYPE_FIELDS (type));
 
   return type;
@@ -5033,9 +5002,7 @@ Gigi_Equivalent_Type (Entity_Id gnat_entity)
       break;
 
     case E_Class_Wide_Type:
-      gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
-                   ? Equivalent_Type (gnat_entity)
-                   : Root_Type (gnat_entity));
+      gnat_equiv = Root_Type (gnat_entity);
       break;
 
     case E_Task_Type:
@@ -5053,6 +5020,95 @@ Gigi_Equivalent_Type (Entity_Id gnat_entity)
   return gnat_equiv;
 }
 
+/* Return a GCC tree for a type corresponding to the component type of the
+   array type or subtype GNAT_ARRAY.  DEFINITION is true if this component
+   is for an array being defined.  DEBUG_INFO_P is true if we need to write
+   debug information for other types that we may create in the process.  */
+
+static tree
+gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
+                           bool debug_info_p)
+{
+  tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
+  tree gnu_comp_size;
+
+  /* Try to get a smaller form of the component if needed.  */
+  if ((Is_Packed (gnat_array)
+       || Has_Component_Size_Clause (gnat_array))
+      && !Is_Bit_Packed_Array (gnat_array)
+      && !Has_Aliased_Components (gnat_array)
+      && !Strict_Alignment (Component_Type (gnat_array))
+      && TREE_CODE (gnu_type) == RECORD_TYPE
+      && !TYPE_FAT_POINTER_P (gnu_type)
+      && host_integerp (TYPE_SIZE (gnu_type), 1))
+    gnu_type = make_packable_type (gnu_type, false);
+
+  if (Has_Atomic_Components (gnat_array))
+    check_ok_for_atomic (gnu_type, gnat_array, true);
+
+  /* Get and validate any specified Component_Size.  */
+  gnu_comp_size
+    = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
+                    Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
+                    true, Has_Component_Size_Clause (gnat_array));
+
+  /* If the array has aliased components and the component size can be zero,
+     force at least unit size to ensure that the components have distinct
+     addresses.  */
+  if (!gnu_comp_size
+      && Has_Aliased_Components (gnat_array)
+      && (integer_zerop (TYPE_SIZE (gnu_type))
+         || (TREE_CODE (gnu_type) == ARRAY_TYPE
+             && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
+    gnu_comp_size
+      = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
+
+  /* If the component type is a RECORD_TYPE that has a self-referential size,
+     then use the maximum size for the component size.  */
+  if (!gnu_comp_size
+      && TREE_CODE (gnu_type) == RECORD_TYPE
+      && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
+    gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
+
+  /* Honor the component size.  This is not needed for bit-packed arrays.  */
+  if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
+    {
+      tree orig_type = gnu_type;
+      unsigned int max_align;
+
+      /* If an alignment is specified, use it as a cap on the component type
+        so that it can be honored for the whole type.  But ignore it for the
+        original type of packed array types.  */
+      if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
+       max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
+      else
+       max_align = 0;
+
+      gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
+      if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
+       gnu_type = orig_type;
+      else
+       orig_type = gnu_type;
+
+      gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
+                                true, false, definition, true);
+
+      /* If a padding record was made, declare it now since it will never be
+        declared otherwise.  This is necessary to ensure that its subtrees
+        are properly marked.  */
+      if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
+       create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
+                         debug_info_p, gnat_array);
+    }
+
+  if (Has_Volatile_Components (Base_Type (gnat_array)))
+    gnu_type
+      = build_qualified_type (gnu_type,
+                             TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
+
+  return gnu_type;
+}
+
 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
    using MECH as its passing mechanism, to be placed in the parameter
    list built for GNAT_SUBPROG.  Assume a foreign convention for the
@@ -5087,8 +5143,7 @@ gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
 
   /* If this is either a foreign function or if the underlying type won't
      be passed by reference, strip off possible padding type.  */
-  if (TREE_CODE (gnu_param_type) == RECORD_TYPE
-      && TYPE_IS_PADDING_P (gnu_param_type))
+  if (TYPE_IS_PADDING_P (gnu_param_type))
     {
       tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
 
@@ -5160,7 +5215,7 @@ gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
     }
 
   /* Fat pointers are passed as thin pointers for foreign conventions.  */
-  else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
+  else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
     gnu_param_type
       = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
 
@@ -5261,21 +5316,38 @@ same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
     Original_Record_Component (discr1) == Original_Record_Component (discr2);
 }
 
-/* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
-   a non-aliased component in the back-end sense.  */
+/* Return true if the array type GNU_TYPE, which represents a dimension of
+   GNAT_TYPE, has a non-aliased component in the back-end sense.  */
 
 static bool
-array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
+array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
 {
-  /* If the type below this is a multi-array type, then
-     this does not have aliased components.  */
+  /* If the array type is not the innermost dimension of the GNAT type,
+     then it has a non-aliased component.  */
   if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
       && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
     return true;
 
+  /* If the array type has an aliased component in the front-end sense,
+     then it also has an aliased component in the back-end sense.  */
   if (Has_Aliased_Components (gnat_type))
     return false;
 
+  /* If this is a derived type, then it has a non-aliased component if
+     and only if its parent type also has one.  */
+  if (Is_Derived_Type (gnat_type))
+    {
+      tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
+      int index;
+      if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
+       gnu_parent_type
+         = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
+      for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
+       gnu_parent_type = TREE_TYPE (gnu_parent_type);
+      return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
+    }
+
+  /* Otherwise, rely exclusively on properties of the element type.  */
   return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
 }
 
@@ -5291,42 +5363,65 @@ compile_time_known_address_p (Node_Id gnat_address)
   return Compile_Time_Known_Value (gnat_address);
 }
 
-/* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e.
-   cannot verify HB < LB-1 when LB and HB are the low and high bounds.  */
+/* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
+   inequality HB >= LB-1 is true.  LB and HB are the low and high bounds.  */
 
 static bool
 cannot_be_superflat_p (Node_Id gnat_range)
 {
   Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
-  tree gnu_lb, gnu_hb;
+  Node_Id scalar_range;
+  tree gnu_lb, gnu_hb, gnu_lb_minus_one;
 
   /* If the low bound is not constant, try to find an upper bound.  */
   while (Nkind (gnat_lb) != N_Integer_Literal
         && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
             || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
-        && Nkind (Scalar_Range (Etype (gnat_lb))) == N_Range)
-    gnat_lb = High_Bound (Scalar_Range (Etype (gnat_lb)));
+        && (scalar_range = Scalar_Range (Etype (gnat_lb)))
+        && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
+            || Nkind (scalar_range) == N_Range))
+    gnat_lb = High_Bound (scalar_range);
 
   /* If the high bound is not constant, try to find a lower bound.  */
   while (Nkind (gnat_hb) != N_Integer_Literal
         && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
             || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
-        && Nkind (Scalar_Range (Etype (gnat_hb))) == N_Range)
-    gnat_hb = Low_Bound (Scalar_Range (Etype (gnat_hb)));
-
-  if (!(Nkind (gnat_lb) == N_Integer_Literal
-       && Nkind (gnat_hb) == N_Integer_Literal))
+        && (scalar_range = Scalar_Range (Etype (gnat_hb)))
+        && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
+            || Nkind (scalar_range) == N_Range))
+    gnat_hb = Low_Bound (scalar_range);
+
+  /* If we have failed to find constant bounds, punt.  */
+  if (Nkind (gnat_lb) != N_Integer_Literal
+      || Nkind (gnat_hb) != N_Integer_Literal)
     return false;
 
-  gnu_lb = UI_To_gnu (Intval (gnat_lb), bitsizetype);
-  gnu_hb = UI_To_gnu (Intval (gnat_hb), bitsizetype);
+  /* We need at least a signed 64-bit type to catch most cases.  */
+  gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
+  gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
+  if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
+    return false;
 
   /* If the low bound is the smallest integer, nothing can be smaller.  */
-  gnu_lb = size_binop (MINUS_EXPR, gnu_lb, bitsize_one_node);
-  if (TREE_OVERFLOW (gnu_lb))
+  gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
+  if (TREE_OVERFLOW (gnu_lb_minus_one))
     return true;
 
-  return (tree_int_cst_lt (gnu_hb, gnu_lb) == 0);
+  return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
+}
+
+/* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR.  */
+
+static bool
+constructor_address_p (tree gnu_expr)
+{
+  while (TREE_CODE (gnu_expr) == NOP_EXPR
+        || TREE_CODE (gnu_expr) == CONVERT_EXPR
+        || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
+    gnu_expr = TREE_OPERAND (gnu_expr, 0);
+
+  return (TREE_CODE (gnu_expr) == ADDR_EXPR
+         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
 }
 \f
 /* Given GNAT_ENTITY, elaborate all expressions that are required to
@@ -5455,7 +5550,7 @@ relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
      see the inner types.  */
   while (TREE_CODE (gnu_old_type) == RECORD_TYPE
         && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
-            || TYPE_IS_PADDING_P (gnu_old_type)))
+            || TYPE_PADDING_P (gnu_old_type)))
     gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
 
   /* Unconstrained array types are deemed incomplete and would thus be given
@@ -5515,37 +5610,6 @@ relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
   record_component_aliases (gnu_new_type);
 }
 \f
-/* Return a TREE_LIST describing the substitutions needed to reflect the
-   discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE.  They can
-   be in any order.  TREE_PURPOSE gives the tree for the discriminant and
-   TREE_VALUE is the replacement value.  They are in the form of operands
-   to substitute_in_expr.  DEFINITION is true if this is for a definition
-   of GNAT_SUBTYPE.  */
-
-static tree
-build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
-{
-  tree gnu_list = NULL_TREE;
-  Entity_Id gnat_discrim;
-  Node_Id gnat_value;
-
-  for (gnat_discrim = First_Stored_Discriminant (gnat_type),
-       gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
-       Present (gnat_discrim);
-       gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
-       gnat_value = Next_Elmt (gnat_value))
-    /* Ignore access discriminants.  */
-    if (!Is_Access_Type (Etype (Node (gnat_value))))
-      gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
-                           elaborate_expression
-                           (Node (gnat_value), gnat_subtype,
-                            get_entity_name (gnat_discrim), definition,
-                            true, false),
-                           gnu_list);
-
-  return gnu_list;
-}
-\f
 /* Return true if the size represented by GNU_SIZE can be handled by an
    allocation.  If STATIC_P is true, consider only what can be done with a
    static allocation.  */
@@ -5689,31 +5753,6 @@ prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
       }
 }
 \f
-/* Called when we need to protect a variable object using a SAVE_EXPR.  */
-
-tree
-maybe_variable (tree gnu_operand)
-{
-  if (TREE_CONSTANT (gnu_operand)
-      || TREE_READONLY (gnu_operand)
-      || TREE_CODE (gnu_operand) == SAVE_EXPR
-      || TREE_CODE (gnu_operand) == NULL_EXPR)
-    return gnu_operand;
-
-  if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
-    {
-      tree gnu_result
-       = build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
-                 variable_size (TREE_OPERAND (gnu_operand, 0)));
-
-      TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
-       = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
-      return gnu_result;
-    }
-
-  return variable_size (gnu_operand);
-}
-\f
 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
    type definition (either a bound or a discriminant value) for GNAT_ENTITY,
    return the GCC tree to use for that expression.  GNU_NAME is the suffix
@@ -5816,7 +5855,7 @@ elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
   if (expr_global && expr_variable)
     return gnu_decl;
 
-  return expr_variable ? maybe_variable (gnu_expr) : gnu_expr;
+  return expr_variable ? gnat_save_expr (gnu_expr) : gnu_expr;
 }
 \f
 /* Create a record type that contains a SIZE bytes long field of TYPE with a
@@ -5831,7 +5870,6 @@ make_aligning_type (tree type, unsigned int align, tree size,
   /* We will be crafting a record type with one field at a position set to be
      the next multiple of ALIGN past record'address + room bytes.  We use a
      record placeholder to express record'address.  */
-
   tree record_type = make_node (RECORD_TYPE);
   tree record = build0 (PLACEHOLDER_EXPR, record_type);
 
@@ -5851,7 +5889,6 @@ make_aligning_type (tree type, unsigned int align, tree size,
 
      Every length is in sizetype bytes there, except "pos" which has to be
      set as a bit position in the GCC tree for the record.  */
-
   tree room_st = size_int (room);
   tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
   tree voffset_st, pos, field;
@@ -5866,13 +5903,11 @@ make_aligning_type (tree type, unsigned int align, tree size,
   /* Compute VOFFSET and then POS.  The next byte position multiple of some
      alignment after some address is obtained by "and"ing the alignment minus
      1 with the two's complement of the address.   */
-
   voffset_st = size_binop (BIT_AND_EXPR,
-                          size_diffop (size_zero_node, vblock_addr_st),
-                          ssize_int ((align / BITS_PER_UNIT) - 1));
+                          fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
+                          size_int ((align / BITS_PER_UNIT) - 1));
 
   /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype.  */
-
   pos = size_binop (MULT_EXPR,
                    convert (bitsizetype,
                             size_binop (PLUS_EXPR, room_st, voffset_st)),
@@ -5891,7 +5926,6 @@ make_aligning_type (tree type, unsigned int align, tree size,
      consequences on the alignment computation, and create_field_decl would
      make one without this special argument, for instance because of the
      complex position expression.  */
-
   field = create_field_decl (get_identifier ("F"), type, record_type,
                              1, size, pos, -1);
   TYPE_FIELDS (record_type) = field;
@@ -5952,7 +5986,7 @@ make_packable_type (tree type, bool in_record)
   TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
   TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
   if (TREE_CODE (type) == RECORD_TYPE)
-    TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
+    TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
 
   /* If we are in a record and have a small size, set the alignment to
      try for an integral mode.  Otherwise set it to try for a smaller
@@ -5995,7 +6029,7 @@ make_packable_type (tree type, bool in_record)
       if ((TREE_CODE (new_field_type) == RECORD_TYPE
           || TREE_CODE (new_field_type) == UNION_TYPE
           || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
-         && !TYPE_IS_FAT_POINTER_P (new_field_type)
+         && !TYPE_FAT_POINTER_P (new_field_type)
          && host_integerp (TYPE_SIZE (new_field_type), 1))
        new_field_type = make_packable_type (new_field_type, true);
 
@@ -6007,7 +6041,7 @@ make_packable_type (tree type, bool in_record)
          && (TREE_CODE (new_field_type) == RECORD_TYPE
              || TREE_CODE (new_field_type) == UNION_TYPE
              || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
-         && !TYPE_IS_FAT_POINTER_P (new_field_type)
+         && !TYPE_FAT_POINTER_P (new_field_type)
          && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
          && TYPE_ADA_SIZE (new_field_type))
        new_size = TYPE_ADA_SIZE (new_field_type);
@@ -6020,10 +6054,7 @@ make_packable_type (tree type, bool in_record)
                                     !DECL_NONADDRESSABLE_P (old_field));
 
       DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
-      SET_DECL_ORIGINAL_FIELD
-       (new_field, (DECL_ORIGINAL_FIELD (old_field)
-                    ? DECL_ORIGINAL_FIELD (old_field) : old_field));
-
+      SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
       if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
        DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
 
@@ -6031,16 +6062,16 @@ make_packable_type (tree type, bool in_record)
       field_list = new_field;
     }
 
-  finish_record_type (new_type, nreverse (field_list), 2, true);
+  finish_record_type (new_type, nreverse (field_list), 2, false);
   relate_alias_sets (new_type, type, ALIAS_SET_COPY);
 
   /* If this is a padding record, we never want to make the size smaller
      than what was specified.  For QUAL_UNION_TYPE, also copy the size.  */
-  if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
-      || TREE_CODE (type) == QUAL_UNION_TYPE)
+  if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
     {
       TYPE_SIZE (new_type) = TYPE_SIZE (type);
       TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
+      new_size = size;
     }
   else
     {
@@ -6069,25 +6100,20 @@ make_packable_type (tree type, bool in_record)
 \f
 /* Ensure that TYPE has SIZE and ALIGN.  Make and return a new padded type
    if needed.  We have already verified that SIZE and TYPE are large enough.
-
-   GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
-   to issue a warning.
-
-   IS_USER_TYPE is true if we must complete the original type.
-
-   DEFINITION is true if this type is being defined.
-
-   SAME_RM_SIZE is true if the RM size of the resulting type is to be set
-   to SIZE too; otherwise, it's set to the RM size of the original type.  */
+   GNAT_ENTITY is used to name the resulting record and to issue a warning.
+   IS_COMPONENT_TYPE is true if this is being done for the component type
+   of an array.  IS_USER_TYPE is true if we must complete the original type.
+   DEFINITION is true if this type is being defined.  SAME_RM_SIZE is true
+   if the RM size of the resulting type is to be set to SIZE too; otherwise,
+   it's set to the RM size of the original type.  */
 
 tree
 maybe_pad_type (tree type, tree size, unsigned int align,
-               Entity_Id gnat_entity, const char *name_trailer,
+               Entity_Id gnat_entity, bool is_component_type,
                bool is_user_type, bool definition, bool same_rm_size)
 {
   tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
   tree orig_size = TYPE_SIZE (type);
-  unsigned int orig_align = align;
   tree record, field;
 
   /* If TYPE is a padded type, see if it agrees with any size and alignment
@@ -6095,7 +6121,7 @@ maybe_pad_type (tree type, tree size, unsigned int align,
      off the padding, since we will either be returning the inner type
      or repadding it.  If no size or alignment is specified, use that of
      the original padded type.  */
-  if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
+  if (TYPE_IS_PADDING_P (type))
     {
       if ((!size
           || operand_equal_p (round_up (size,
@@ -6144,18 +6170,15 @@ maybe_pad_type (tree type, tree size, unsigned int align,
      generate incorrect debugging information.  So make a new record
      type and name.  */
   record = make_node (RECORD_TYPE);
-  TYPE_IS_PADDING_P (record) = 1;
+  TYPE_PADDING_P (record) = 1;
 
   if (Present (gnat_entity))
-    TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
+    TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
 
   TYPE_VOLATILE (record)
     = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
 
   TYPE_ALIGN (record) = align;
-  if (orig_align)
-    TYPE_USER_ALIGN (record) = align;
-
   TYPE_SIZE (record) = size ? size : orig_size;
   TYPE_SIZE_UNIT (record)
     = convert (sizetype,
@@ -6196,8 +6219,8 @@ maybe_pad_type (tree type, tree size, unsigned int align,
                              orig_size, bitsize_zero_node, 1);
   DECL_INTERNAL_P (field) = 1;
 
-  /* Do not finalize it until after the auxiliary record is built.  */
-  finish_record_type (record, field, 1, true);
+  /* Do not emit debug info until after the auxiliary record is built.  */
+  finish_record_type (record, field, 1, false);
 
   /* Set the same size for its RM size if requested; otherwise reuse
      the RM size of the original type.  */
@@ -6206,9 +6229,9 @@ maybe_pad_type (tree type, tree size, unsigned int align,
   /* Unless debugging information isn't being written for the input type,
      write a record that shows what we are a subtype of and also make a
      variable that indicates our size, if still variable.  */
-  if (TYPE_NAME (record)
-      && AGGREGATE_TYPE_P (type)
-      && TREE_CODE (orig_size) != INTEGER_CST
+  if (TREE_CODE (orig_size) != INTEGER_CST
+      && TYPE_NAME (record)
+      && TYPE_NAME (type)
       && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
           && DECL_IGNORED_P (TYPE_NAME (type))))
     {
@@ -6228,14 +6251,15 @@ maybe_pad_type (tree type, tree size, unsigned int align,
                                             build_reference_type (type),
                                             marker, 0, NULL_TREE, NULL_TREE,
                                             0),
-                         0, false);
+                         0, true);
 
       add_parallel_type (TYPE_STUB_DECL (record), marker);
 
-      if (size && TREE_CODE (size) != INTEGER_CST && definition)
-       create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
-                        TYPE_SIZE_UNIT (record), false, false, false,
-                        false, NULL, gnat_entity);
+      if (definition && size && TREE_CODE (size) != INTEGER_CST)
+       TYPE_SIZE_UNIT (marker)
+         = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
+                            TYPE_SIZE_UNIT (record), false, false, false,
+                            false, NULL, gnat_entity);
     }
 
   rest_of_record_type_compilation (record);
@@ -6250,11 +6274,16 @@ maybe_pad_type (tree type, tree size, unsigned int align,
   if (align)
     orig_size = round_up (orig_size, align);
 
-  if (size && Present (gnat_entity)
+  if (Present (gnat_entity)
+      && size
+      && TREE_CODE (size) != MAX_EXPR
+      && TREE_CODE (size) != COND_EXPR
       && !operand_equal_p (size, orig_size, 0)
       && !(TREE_CODE (size) == INTEGER_CST
           && TREE_CODE (orig_size) == INTEGER_CST
-          && tree_int_cst_lt (size, orig_size)))
+          && (TREE_OVERFLOW (size)
+              || TREE_OVERFLOW (orig_size)
+              || tree_int_cst_lt (size, orig_size))))
     {
       Node_Id gnat_error_node = Empty;
 
@@ -6271,15 +6300,17 @@ maybe_pad_type (tree type, tree size, unsigned int align,
       /* Generate message only for entities that come from source, since
         if we have an entity created by expansion, the message will be
         generated for some other corresponding source entity.  */
-      if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
-       post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
-                           gnat_entity,
-                           size_diffop (size, orig_size));
-
-      else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
-       post_error_ne_tree ("component of& padded{ by ^ bits}?",
-                           gnat_entity, gnat_entity,
-                           size_diffop (size, orig_size));
+      if (Comes_From_Source (gnat_entity))
+       {
+         if (Present (gnat_error_node))
+           post_error_ne_tree ("{^ }bits of & unused?",
+                               gnat_error_node, gnat_entity,
+                               size_diffop (size, orig_size));
+         else if (is_component_type)
+           post_error_ne_tree ("component of& padded{ by ^ bits}?",
+                               gnat_entity, gnat_entity,
+                               size_diffop (size, orig_size));
+       }
     }
 
   return record;
@@ -6304,13 +6335,11 @@ choices_to_gnu (tree operand, Node_Id choices)
          low = gnat_to_gnu (Low_Bound (choice));
          high = gnat_to_gnu (High_Bound (choice));
 
-         /* There's no good type to use here, so we might as well use
-            integer_type_node.  */
          this_test
-           = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                              build_binary_op (GE_EXPR, integer_type_node,
+           = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                              build_binary_op (GE_EXPR, boolean_type_node,
                                                operand, low),
-                              build_binary_op (LE_EXPR, integer_type_node,
+                              build_binary_op (LE_EXPR, boolean_type_node,
                                                operand, high));
 
          break;
@@ -6321,10 +6350,10 @@ choices_to_gnu (tree operand, Node_Id choices)
          high = gnat_to_gnu (High_Bound (gnat_temp));
 
          this_test
-           = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                              build_binary_op (GE_EXPR, integer_type_node,
+           = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                              build_binary_op (GE_EXPR, boolean_type_node,
                                                operand, low),
-                              build_binary_op (LE_EXPR, integer_type_node,
+                              build_binary_op (LE_EXPR, boolean_type_node,
                                                operand, high));
          break;
 
@@ -6342,10 +6371,10 @@ choices_to_gnu (tree operand, Node_Id choices)
              high = TYPE_MAX_VALUE (type);
 
              this_test
-               = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                                  build_binary_op (GE_EXPR, integer_type_node,
+               = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                                  build_binary_op (GE_EXPR, boolean_type_node,
                                                    operand, low),
-                                  build_binary_op (LE_EXPR, integer_type_node,
+                                  build_binary_op (LE_EXPR, boolean_type_node,
                                                    operand, high));
              break;
            }
@@ -6355,7 +6384,7 @@ choices_to_gnu (tree operand, Node_Id choices)
        case N_Character_Literal:
        case N_Integer_Literal:
          single = gnat_to_gnu (choice);
-         this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
+         this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
                                       single);
          break;
 
@@ -6367,8 +6396,8 @@ choices_to_gnu (tree operand, Node_Id choices)
          gcc_unreachable ();
        }
 
-      result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
-                               result, this_test);
+      result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
+                               this_test);
     }
 
   return result;
@@ -6407,11 +6436,14 @@ adjust_packed (tree field_type, tree record_type, int packed)
    record has Component_Alignment of Storage_Unit, -2 if the enclosing
    record has a specified alignment.
 
-   DEFINITION is true if this field is for a record being defined.  */
+   DEFINITION is true if this field is for a record being defined.
+
+   DEBUG_INFO_P is true if we need to write debug information for types
+   that we may create in the process.  */
 
 static tree
 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
-                  bool definition)
+                  bool definition, bool debug_info_p)
 {
   tree gnu_field_id = get_entity_name (gnat_field);
   tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
@@ -6439,67 +6471,44 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
   else
     gnu_size = NULL_TREE;
 
-  /* If we have a specified size that's smaller than that of the field type,
-     or a position is specified, and the field type is a record, see if we can
-     get either an integral mode form of the type or a smaller form.  If we
-     can, show a size was specified for the field if there wasn't one already,
-     so we know to make this a bitfield and avoid making things wider.
+  /* If we have a specified size that is smaller than that of the field's type,
+     or a position is specified, and the field's type is a record that doesn't
+     require strict alignment, see if we can get either an integral mode form
+     of the type or a smaller form.  If we can, show a size was specified for
+     the field if there wasn't one already, so we know to make this a bitfield
+     and avoid making things wider.
 
-     Doing this is first useful if the record is packed because we may then
-     place the field at a non-byte-aligned position and so achieve tighter
-     packing.
+     Changing to an integral mode form is useful when the record is packed as
+     we can then place the field at a non-byte-aligned position and so achieve
+     tighter packing.  This is in addition required if the field shares a byte
+     with another field and the front-end lets the back-end handle the access
+     to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
 
-     This is in addition *required* if the field shares a byte with another
-     field and the front-end lets the back-end handle the references, because
-     GCC does not handle BLKmode bitfields properly.
+     Changing to a smaller form is required if the specified size is smaller
+     than that of the field's type and the type contains sub-fields that are
+     padded, in order to avoid generating accesses to these sub-fields that
+     are wider than the field.
 
      We avoid the transformation if it is not required or potentially useful,
      as it might entail an increase of the field's alignment and have ripple
      effects on the outer record type.  A typical case is a field known to be
-     byte aligned and not to share a byte with another field.
-
-     Besides, we don't even look the possibility of a transformation in cases
-     known to be in error already, for instance when an invalid size results
-     from a component clause.  */
-
-  if (TREE_CODE (gnu_field_type) == RECORD_TYPE
-      && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
+     byte-aligned and not to share a byte with another field.  */
+  if (!needs_strict_alignment
+      && TREE_CODE (gnu_field_type) == RECORD_TYPE
+      && !TYPE_FAT_POINTER_P (gnu_field_type)
       && host_integerp (TYPE_SIZE (gnu_field_type), 1)
       && (packed == 1
          || (gnu_size
              && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
-                 || Present (Component_Clause (gnat_field))))))
+                 || (Present (Component_Clause (gnat_field))
+                     && !(UI_To_Int (Component_Bit_Offset (gnat_field))
+                          % BITS_PER_UNIT == 0
+                          && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
     {
-      /* See what the alternate type and size would be.  */
       tree gnu_packable_type = make_packable_type (gnu_field_type, true);
-
-      bool has_byte_aligned_clause
-       = Present (Component_Clause (gnat_field))
-         && (UI_To_Int (Component_Bit_Offset (gnat_field))
-             % BITS_PER_UNIT == 0);
-
-      /* Compute whether we should avoid the substitution.  */
-      bool reject
-       /* There is no point substituting if there is no change...  */
-       = (gnu_packable_type == gnu_field_type)
-        /* ... nor when the field is known to be byte aligned and not to
-           share a byte with another field.  */
-         || (has_byte_aligned_clause
-             && value_factor_p (gnu_size, BITS_PER_UNIT))
-        /* The size of an aliased field must be an exact multiple of the
-           type's alignment, which the substitution might increase.  Reject
-           substitutions that would so invalidate a component clause when the
-           specified position is byte aligned, as the change would have no
-           real benefit from the packing standpoint anyway.  */
-         || (Is_Aliased (gnat_field)
-             && has_byte_aligned_clause
-             && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
-
-      /* Substitute unless told otherwise.  */
-      if (!reject)
+      if (gnu_packable_type != gnu_field_type)
        {
          gnu_field_type = gnu_packable_type;
-
          if (!gnu_size)
            gnu_size = rm_size (gnu_field_type);
        }
@@ -6630,6 +6639,8 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
   /* If a size is specified, adjust the field's type to it.  */
   if (gnu_size)
     {
+      tree orig_field_type;
+
       /* If the field's type is justified modular, we would need to remove
         the wrapper to (better) meet the layout requirements.  However we
         can do so only if the field is not aliased to preserve the unique
@@ -6645,8 +6656,18 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
       gnu_field_type
        = make_type_from_size (gnu_field_type, gnu_size,
                               Has_Biased_Representation (gnat_field));
+
+      orig_field_type = gnu_field_type;
       gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
-                                      "PAD", false, definition, true);
+                                      false, false, definition, true);
+
+      /* If a padding record was made, declare it now since it will never be
+        declared otherwise.  This is necessary to ensure that its subtrees
+        are properly marked.  */
+      if (gnu_field_type != orig_field_type
+         && !DECL_P (TYPE_NAME (gnu_field_type)))
+       create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
+                         true, debug_info_p, gnat_field);
     }
 
   /* Otherwise (or if there was an error), don't specify a position.  */
@@ -6681,8 +6702,7 @@ is_variable_size (tree type)
   if (!TREE_CONSTANT (TYPE_SIZE (type)))
     return true;
 
-  if (TREE_CODE (type) == RECORD_TYPE
-      && TYPE_IS_PADDING_P (type)
+  if (TYPE_IS_PADDING_P (type)
       && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
     return true;
 
@@ -6723,31 +6743,34 @@ compare_field_bitpos (const PTR rt1, const PTR rt2)
    with Component_Alignment of Storage_Unit, -2 if this is for a record
    with a specified alignment.
 
-   DEFINITION is true if we are defining this record.
+   DEFINITION is true if we are defining this record type.
 
    P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
    with a rep clause is to be added; in this case, that is all that should
    be done with such fields.
 
-   CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
-   laying out the record.  This means the alignment only serves to force
-   fields to be bitfields, but not require the record to be that aligned.
-   This is used for variants.
+   CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
+   out the record.  This means the alignment only serves to force fields to
+   be bitfields, but not to require the record to be that aligned.  This is
+   used for variants.
+
+   ALL_REP is true if a rep clause is present for all the fields.
 
-   ALL_REP, if true, means a rep clause was found for all the fields.  This
-   simplifies the logic since we know we're not in the mixed case.
+   UNCHECKED_UNION is true if we are building this type for a record with a
+   Pragma Unchecked_Union.
 
-   DO_NOT_FINALIZE, if true, means that the record type is expected to be
-   modified afterwards so it will not be finalized here.
+   DEBUG_INFO_P is true if we need to write debug information about the type.
+
+   MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
+   mean that its contents may be unused as well, but only the container.  */
 
-   UNCHECKED_UNION, if true, means that we are building a type for a record
-   with a Pragma Unchecked_Union.  */
 
 static void
 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
                      tree gnu_field_list, int packed, bool definition,
                      tree *p_gnu_rep_list, bool cancel_alignment,
-                     bool all_rep, bool do_not_finalize, bool unchecked_union)
+                     bool all_rep, bool unchecked_union, bool debug_info_p,
+                     bool maybe_unused)
 {
   bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
   bool layout_with_rep = false;
@@ -6775,8 +6798,8 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
          }
        else
          {
-           gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
-                                          packed, definition);
+           gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
+                                          definition, debug_info_p);
 
            /* If this is the _Tag field, put it before any other fields.  */
            if (gnat_name == Name_uTag)
@@ -6877,12 +6900,12 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
                = TYPE_SIZE_UNIT (gnu_record_type);
            }
 
-         /* Add the fields into the record type for the variant.  Note that we
-            defer finalizing it until after we are sure to really use it.  */
+         /* Add the fields into the record type for the variant.  Note that
+            we aren't sure to really use it at this point, see below.  */
          components_to_record (gnu_variant_type, Component_List (variant),
                                NULL_TREE, packed, definition,
                                &gnu_our_rep_list, !all_rep_and_size, all_rep,
-                               true, unchecked_union);
+                               unchecked_union, debug_info_p, true);
 
          gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
 
@@ -6906,6 +6929,8 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
                 otherwise, the union type definition will be lacking
                 the fields associated with these empty variants.  */
              rest_of_record_type_compilation (gnu_variant_type);
+             create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
+                               NULL, true, debug_info_p, gnat_component_list);
 
              gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
                                             gnu_union_type, field_packed,
@@ -6939,7 +6964,7 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
            }
 
          finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
-                             all_rep_and_size ? 1 : 0, false);
+                             all_rep_and_size ? 1 : 0, debug_info_p);
 
          /* If GNU_UNION_TYPE is our record type, it means we must have an
             Unchecked_Union with no fields.  Verify that and, if so, just
@@ -6952,6 +6977,9 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
              return;
            }
 
+         create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
+                           NULL, true, debug_info_p, gnat_component_list);
+
          /* Deal with packedness like in gnat_to_gnu_field.  */
          union_field_packed
            = adjust_packed (gnu_union_type, gnu_record_type, packed);
@@ -7028,7 +7056,7 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
 
       if (gnu_field_list)
        {
-         finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
+         finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, debug_info_p);
          gnu_field
            = create_field_decl (get_identifier ("REP"), gnu_rep_type,
                                 gnu_record_type, 0, NULL_TREE, NULL_TREE, 1);
@@ -7046,7 +7074,7 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
     TYPE_ALIGN (gnu_record_type) = 0;
 
   finish_record_type (gnu_record_type, nreverse (gnu_field_list),
-                     layout_with_rep ? 1 : 0, do_not_finalize);
+                     layout_with_rep ? 1 : 0, debug_info_p && !maybe_unused);
 }
 \f
 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
@@ -7056,12 +7084,10 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
 static Uint
 annotate_value (tree gnu_size)
 {
-  int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
   TCode tcode;
   Node_Ref_Or_Val ops[3], ret;
-  int i;
-  int size;
   struct tree_int_map **h = NULL;
+  int i;
 
   /* See if we've already saved the value for this node.  */
   if (EXPR_P (gnu_size))
@@ -7088,46 +7114,21 @@ annotate_value (tree gnu_size)
       if (TREE_OVERFLOW (gnu_size))
        return No_Uint;
 
-      /* This may have come from a conversion from some smaller type,
-        so ensure this is in bitsizetype.  */
+      /* This may come from a conversion from some smaller type, so ensure
+        this is in bitsizetype.  */
       gnu_size = convert (bitsizetype, gnu_size);
 
-      /* For negative values, use NEGATE_EXPR of the supplied value.  */
-      if (tree_int_cst_sgn (gnu_size) < 0)
+      /* For a negative value, build NEGATE_EXPR of the opposite.  Such values
+        appear in expressions containing aligning patterns.  Note that, since
+        sizetype is sign-extended but nonetheless unsigned, we don't directly
+        use tree_int_cst_sgn.  */
+      if (TREE_INT_CST_HIGH (gnu_size) < 0)
        {
-         /* The ridiculous code below is to handle the case of the largest
-            negative integer.  */
-         tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
-         bool adjust = false;
-         tree temp;
-
-         if (TREE_OVERFLOW (negative_size))
-           {
-             negative_size
-               = size_binop (MINUS_EXPR, bitsize_zero_node,
-                             size_binop (PLUS_EXPR, gnu_size,
-                                         bitsize_one_node));
-             adjust = true;
-           }
-
-         temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
-         if (adjust)
-           temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
-
-         return annotate_value (temp);
+         tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
+         return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
        }
 
-      if (!host_integerp (gnu_size, 1))
-       return No_Uint;
-
-      size = tree_low_cst (gnu_size, 1);
-
-      /* This peculiar test is to make sure that the size fits in an int
-        on machines where HOST_WIDE_INT is not "int".  */
-      if (tree_low_cst (gnu_size, 1) == size)
-       return UI_From_Int (size);
-      else
-       return No_Uint;
+      return UI_From_gnu (gnu_size);
 
     case COMPONENT_REF:
       /* The only case we handle here is a simple discriminant reference.  */
@@ -7192,7 +7193,7 @@ annotate_value (tree gnu_size)
   for (i = 0; i < 3; i++)
     ops[i] = No_Uint;
 
-  for (i = 0; i < len; i++)
+  for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
     {
       ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
       if (ops[i] == No_Uint)
@@ -7222,7 +7223,7 @@ annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
 {
   if (by_ref)
     {
-      if (TYPE_FAT_POINTER_P (gnu_type))
+      if (TYPE_IS_FAT_POINTER_P (gnu_type))
        gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
       else
        gnu_type = TREE_TYPE (gnu_type);
@@ -7245,94 +7246,109 @@ annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
                   UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
 }
 
-/* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
-   GCC type, set Component_Bit_Offset and Esize to the position and size
-   used by Gigi.  */
+/* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
+   Return NULL_TREE if there is no such element in the list.  */
+
+static tree
+purpose_member_field (const_tree elem, tree list)
+{
+  while (list)
+    {
+      tree field = TREE_PURPOSE (list);
+      if (SAME_FIELD_P (field, elem))
+       return list;
+      list = TREE_CHAIN (list);
+    }
+  return NULL_TREE;
+}
+
+/* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
+   set Component_Bit_Offset and Esize of the components to the position and
+   size used by Gigi.  */
 
 static void
 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
 {
-  tree gnu_list;
-  tree gnu_entry;
   Entity_Id gnat_field;
+  tree gnu_list;
 
-  /* We operate by first making a list of all fields and their positions
-     (we can get the sizes easily at any time) by a recursive call
-     and then update all the sizes into the tree.  */
-  gnu_list = compute_field_positions (gnu_type, NULL_TREE,
-                                     size_zero_node, bitsize_zero_node,
-                                     BIGGEST_ALIGNMENT);
+  /* We operate by first making a list of all fields and their position (we
+     can get the size easily) and then update all the sizes in the tree.  */
+  gnu_list
+    = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
+                          BIGGEST_ALIGNMENT, NULL_TREE);
 
-  for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
+  for (gnat_field = First_Entity (gnat_entity);
+       Present (gnat_field);
        gnat_field = Next_Entity (gnat_field))
-    if ((Ekind (gnat_field) == E_Component
-        || (Ekind (gnat_field) == E_Discriminant
-            && !Is_Unchecked_Union (Scope (gnat_field)))))
+    if (Ekind (gnat_field) == E_Component
+       || (Ekind (gnat_field) == E_Discriminant
+           && !Is_Unchecked_Union (Scope (gnat_field))))
       {
-       tree parent_offset = bitsize_zero_node;
-
-       gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
-                                   gnu_list);
-
-       if (gnu_entry)
+       tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
+                                      gnu_list);
+       if (t)
          {
+           tree parent_offset;
+
            if (type_annotate_only && Is_Tagged_Type (gnat_entity))
              {
-               /* In this mode the tag and parent components have not been
+               /* In this mode the tag and parent components are not
                   generated, so we add the appropriate offset to each
                   component.  For a component appearing in the current
                   extension, the offset is the size of the parent.  */
-           if (Is_Derived_Type (gnat_entity)
-               && Original_Record_Component (gnat_field) == gnat_field)
-             parent_offset
-               = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
-                            bitsizetype);
-           else
-             parent_offset = bitsize_int (POINTER_SIZE);
+               if (Is_Derived_Type (gnat_entity)
+                   && Original_Record_Component (gnat_field) == gnat_field)
+                 parent_offset
+                   = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
+                                bitsizetype);
+               else
+                 parent_offset = bitsize_int (POINTER_SIZE);
              }
+           else
+             parent_offset = bitsize_zero_node;
 
-         Set_Component_Bit_Offset
-           (gnat_field,
-            annotate_value
-            (size_binop (PLUS_EXPR,
-                         bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
-                                       TREE_VALUE (TREE_VALUE
-                                                   (TREE_VALUE (gnu_entry)))),
-                         parent_offset)));
+           Set_Component_Bit_Offset
+             (gnat_field,
+              annotate_value
+                (size_binop (PLUS_EXPR,
+                             bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
+                                           TREE_VEC_ELT (TREE_VALUE (t), 2)),
+                             parent_offset)));
 
            Set_Esize (gnat_field,
-                      annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
+                      annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
          }
-       else if (Is_Tagged_Type (gnat_entity)
-                && Is_Derived_Type (gnat_entity))
+       else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
          {
-           /* If there is no gnu_entry, this is an inherited component whose
+           /* If there is no entry, this is an inherited component whose
               position is the same as in the parent type.  */
            Set_Component_Bit_Offset
              (gnat_field,
               Component_Bit_Offset (Original_Record_Component (gnat_field)));
+
            Set_Esize (gnat_field,
                       Esize (Original_Record_Component (gnat_field)));
          }
       }
 }
-
-/* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
-   FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
-   position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
-   placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position.  GNU_POS is
-   to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
-   the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
-   so far.  */
+\f
+/* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
+   the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
+   value to be placed into DECL_OFFSET_ALIGN and the bit position.  The list
+   of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
+   is set to true.  GNU_POS is to be added to the position, GNU_BITPOS to the
+   bit position, OFFSET_ALIGN is the present offset alignment.  GNU_LIST is a
+   pre-existing list to be chained to the newly created entries.  */
 
 static tree
-compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
-                        tree gnu_bitpos, unsigned int offset_align)
+build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
+                    tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
 {
   tree gnu_field;
-  tree gnu_result = gnu_list;
 
-  for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
+  for (gnu_field = TYPE_FIELDS (gnu_type);
+       gnu_field;
        gnu_field = TREE_CHAIN (gnu_field))
     {
       tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
@@ -7341,23 +7357,116 @@ compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
                                        DECL_FIELD_OFFSET (gnu_field));
       unsigned int our_offset_align
        = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
+      tree v = make_tree_vec (3);
 
-      gnu_result
-       = tree_cons (gnu_field,
-                    tree_cons (gnu_our_offset,
-                               tree_cons (size_int (our_offset_align),
-                                          gnu_our_bitpos, NULL_TREE),
-                               NULL_TREE),
-                    gnu_result);
+      TREE_VEC_ELT (v, 0) = gnu_our_offset;
+      TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
+      TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
+      gnu_list = tree_cons (gnu_field, v, gnu_list);
 
+      /* Recurse on internal fields, flattening the nested fields except for
+        those in the variant part, if requested.  */
       if (DECL_INTERNAL_P (gnu_field))
-       gnu_result
-         = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
+       {
+         tree gnu_field_type = TREE_TYPE (gnu_field);
+         if (do_not_flatten_variant
+             && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
+           gnu_list
+             = build_position_list (gnu_field_type, do_not_flatten_variant,
+                                    size_zero_node, bitsize_zero_node,
+                                    BIGGEST_ALIGNMENT, gnu_list);
+         else
+           gnu_list
+             = build_position_list (gnu_field_type, do_not_flatten_variant,
                                     gnu_our_offset, gnu_our_bitpos,
-                                    our_offset_align);
+                                    our_offset_align, gnu_list);
+       }
+    }
+
+  return gnu_list;
+}
+
+/* Return a TREE_LIST describing the substitutions needed to reflect the
+   discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE.  They can
+   be in any order.  TREE_PURPOSE gives the tree for the discriminant and
+   TREE_VALUE is the replacement value.  They are in the form of operands
+   to SUBSTITUTE_IN_EXPR.  DEFINITION is true if this is for a definition
+   of GNAT_SUBTYPE.  */
+
+static tree
+build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
+{
+  tree gnu_list = NULL_TREE;
+  Entity_Id gnat_discrim;
+  Node_Id gnat_value;
+
+  for (gnat_discrim = First_Stored_Discriminant (gnat_type),
+       gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
+       Present (gnat_discrim);
+       gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
+       gnat_value = Next_Elmt (gnat_value))
+    /* Ignore access discriminants.  */
+    if (!Is_Access_Type (Etype (Node (gnat_value))))
+      {
+       tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
+       gnu_list = tree_cons (gnu_field,
+                             convert (TREE_TYPE (gnu_field),
+                                      elaborate_expression
+                                      (Node (gnat_value), gnat_subtype,
+                                       get_entity_name (gnat_discrim),
+                                       definition, true, false)),
+                             gnu_list);
+      }
+
+  return gnu_list;
+}
+
+/* Scan all fields in QUAL_UNION_TYPE and return a TREE_LIST describing the
+   variants of QUAL_UNION_TYPE that are still relevant after applying the
+   substitutions described in SUBST_LIST.  TREE_PURPOSE is the type of the
+   variant and TREE_VALUE is a TREE_VEC containing the field, the new value
+   of the qualifier and NULL_TREE respectively.  GNU_LIST is a pre-existing
+   list to be chained to the newly created entries.  */
+
+static tree
+build_variant_list (tree qual_union_type, tree subst_list, tree gnu_list)
+{
+  tree gnu_field;
+
+  for (gnu_field = TYPE_FIELDS (qual_union_type);
+       gnu_field;
+       gnu_field = TREE_CHAIN (gnu_field))
+    {
+      tree t, qual = DECL_QUALIFIER (gnu_field);
+
+      for (t = subst_list; t; t = TREE_CHAIN (t))
+       qual = SUBSTITUTE_IN_EXPR (qual, TREE_PURPOSE (t), TREE_VALUE (t));
+
+      /* If the new qualifier is not unconditionally false, its variant may
+        still be accessed.  */
+      if (!integer_zerop (qual))
+       {
+         tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
+         tree v = make_tree_vec (3);
+         TREE_VEC_ELT (v, 0) = gnu_field;
+         TREE_VEC_ELT (v, 1) = qual;
+         TREE_VEC_ELT (v, 2) = NULL_TREE;
+         gnu_list = tree_cons (variant_type, v, gnu_list);
+
+         /* Recurse on the variant subpart of the variant, if any.  */
+         variant_subpart = get_variant_part (variant_type);
+         if (variant_subpart)
+           gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
+                                          subst_list, gnu_list);
+
+         /* If the new qualifier is unconditionally true, the subsequent
+            variants cannot be accessed.  */
+         if (integer_onep (qual))
+           break;
+       }
     }
 
-  return gnu_result;
+  return gnu_list;
 }
 \f
 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
@@ -7377,13 +7486,13 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   Node_Id gnat_error_node;
   tree type_size, size;
 
-  if (kind == VAR_DECL
-      /* If a type needs strict alignment, a component of this type in
-        a packed record cannot be packed and thus uses the type size.  */
-      || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
-    type_size = TYPE_SIZE (gnu_type);
-  else
-    type_size = rm_size (gnu_type);
+  /* Return 0 if no size was specified.  */
+  if (uint_size == No_Uint)
+    return NULL_TREE;
+
+  /* Ignore a negative size since that corresponds to our back-annotation.  */
+  if (UI_Lt (uint_size, Uint_0))
+    return NULL_TREE;
 
   /* Find the node to use for errors.  */
   if ((Ekind (gnat_object) == E_Component
@@ -7395,25 +7504,22 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   else
     gnat_error_node = gnat_object;
 
-  /* Return 0 if no size was specified, either because Esize was not Present
-     or the specified size was zero.  */
-  if (No (uint_size) || uint_size == No_Uint)
-    return NULL_TREE;
-
   /* Get the size as a tree.  Issue an error if a size was specified but
      cannot be represented in sizetype.  */
   size = UI_To_gnu (uint_size, bitsizetype);
   if (TREE_OVERFLOW (size))
     {
-      post_error_ne (component_p ? "component size of & is too large"
-                    : "size of & is too large",
-                    gnat_error_node, gnat_object);
+      if (component_p)
+       post_error_ne ("component size of & is too large", gnat_error_node,
+                      gnat_object);
+      else
+       post_error_ne ("size of & is too large", gnat_error_node,
+                      gnat_object);
       return NULL_TREE;
     }
 
-  /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size if it is not permitted.  */
-  if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
+  /* Ignore a zero size if it is not permitted.  */
+  if (!zero_ok && integer_zerop (size))
     return NULL_TREE;
 
   /* The size of objects is always a multiple of a byte.  */
@@ -7443,6 +7549,14 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
     size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
 
+  if (kind == VAR_DECL
+      /* If a type needs strict alignment, a component of this type in
+        a packed record cannot be packed and thus uses the type size.  */
+      || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
+    type_size = TYPE_SIZE (gnu_type);
+  else
+    type_size = rm_size (gnu_type);
+
   /* Modify the size of the type to be that of the maximum size if it has a
      discriminant.  */
   if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
@@ -7450,15 +7564,11 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
 
   /* If this is an access type or a fat pointer, the minimum size is that given
      by the smallest integral mode that's valid for pointers.  */
-  if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
+  if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
     {
-      enum machine_mode p_mode;
-
-      for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
-          !targetm.valid_pointer_mode (p_mode);
-          p_mode = GET_MODE_WIDER_MODE (p_mode))
-       ;
-
+      enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
+      while (!targetm.valid_pointer_mode (p_mode))
+       p_mode = GET_MODE_WIDER_MODE (p_mode);
       type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
     }
 
@@ -7473,22 +7583,11 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
          ("component size for& too small{, minimum allowed is ^}",
           gnat_error_node, gnat_object, type_size);
       else
-       post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
-                           gnat_error_node, gnat_object, type_size);
-
-      if (kind == VAR_DECL && !component_p
-         && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
-         && !tree_int_cst_lt (size, rm_size (gnu_type)))
-       post_error_ne_tree_2
-         ("\\size of ^ is not a multiple of alignment (^ bits)",
-          gnat_error_node, gnat_object, rm_size (gnu_type),
-          TYPE_ALIGN (gnu_type));
-
-      else if (INTEGRAL_TYPE_P (gnu_type))
-       post_error_ne ("\\size would be legal if & were not aliased!",
-                      gnat_error_node, gnat_object);
+       post_error_ne_tree
+         ("size for& too small{, minimum allowed is ^}",
+          gnat_error_node, gnat_object, type_size);
 
-      return NULL_TREE;
+      size = NULL_TREE;
     }
 
   return size;
@@ -7500,16 +7599,21 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
 static void
 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
 {
+  Node_Id gnat_attr_node;
+  tree old_size, size;
+
+  /* Do nothing if no size was specified.  */
+  if (uint_size == No_Uint)
+    return;
+
+  /* Ignore a negative size since that corresponds to our back-annotation.  */
+  if (UI_Lt (uint_size, Uint_0))
+    return;
+
   /* Only issue an error if a Value_Size clause was explicitly given.
      Otherwise, we'd be duplicating an error on the Size clause.  */
-  Node_Id gnat_attr_node
+  gnat_attr_node
     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
-  tree old_size = rm_size (gnu_type), size;
-
-  /* Do nothing if no size was specified, either because RM size was not
-     Present or if the specified size was zero.  */
-  if (No (uint_size) || uint_size == No_Uint)
-    return;
 
   /* Get the size as a tree.  Issue an error if a size was specified but
      cannot be represented in sizetype.  */
@@ -7522,17 +7626,17 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
       return;
     }
 
-  /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size unless a Value_Size clause exists, or a size
-     clause exists, or this is an integer type, in which case the front-end
-     will have always set it.  */
-  if (tree_int_cst_sgn (size) < 0
-      || (integer_zerop (size)
-         && No (gnat_attr_node)
-         && !Has_Size_Clause (gnat_entity)
-         && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
+  /* Ignore a zero size unless a Value_Size clause exists, or a size clause
+     exists, or this is an integer type, in which case the front-end will
+     have always set it.  */
+  if (No (gnat_attr_node)
+      && integer_zerop (size)
+      && !Has_Size_Clause (gnat_entity)
+      && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
     return;
 
+  old_size = rm_size (gnu_type);
+
   /* If the old size is self-referential, get the maximum size.  */
   if (CONTAINS_PLACEHOLDER_P (old_size))
     old_size = max_size (old_size, true);
@@ -7544,10 +7648,10 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
       || (AGGREGATE_TYPE_P (gnu_type)
          && !(TREE_CODE (gnu_type) == ARRAY_TYPE
               && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
-         && !(TREE_CODE (gnu_type) == RECORD_TYPE
-              && TYPE_IS_PADDING_P (gnu_type)
+         && !(TYPE_IS_PADDING_P (gnu_type)
               && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
-              && TYPE_PACKED_ARRAY_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_type))))
+              && TYPE_PACKED_ARRAY_TYPE_P
+                 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
          && tree_int_cst_lt (size, old_size)))
     {
       if (Present (gnat_attr_node))
@@ -7568,7 +7672,7 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
            || TREE_CODE (gnu_type) == UNION_TYPE
            || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
-          && !TYPE_IS_FAT_POINTER_P (gnu_type))
+          && !TYPE_FAT_POINTER_P (gnu_type))
     SET_TYPE_ADA_SIZE (gnu_type, size);
 }
 \f
@@ -7599,6 +7703,10 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
       biased_p = (TREE_CODE (type) == INTEGER_TYPE
                  && TYPE_BIASED_REPRESENTATION_P (type));
 
+      /* Integer types with precision 0 are forbidden.  */
+      if (size == 0)
+       size = 1;
+
       /* Only do something if the type is not a packed array type and
         doesn't already have the proper size.  */
       if (TYPE_PACKED_ARRAY_TYPE_P (type)
@@ -7620,14 +7728,9 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
       SET_TYPE_RM_MAX_VALUE (new_type,
                             convert (TREE_TYPE (new_type),
                                      TYPE_MAX_VALUE (type)));
-      /* Propagate the name to avoid creating a fake subrange type.  */
-      if (TYPE_NAME (type))
-       {
-         if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
-           TYPE_NAME (new_type) = DECL_NAME (TYPE_NAME (type));
-         else
-           TYPE_NAME (new_type) = TYPE_NAME (type);
-       }
+      /* Copy the name to show that it's essentially the same type and
+        not a subrange type.  */
+      TYPE_NAME (new_type) = TYPE_NAME (type);
       TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
       SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
       return new_type;
@@ -7635,7 +7738,7 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
     case RECORD_TYPE:
       /* Do something if this is a fat pointer, in which case we
         may need to return the thin pointer.  */
-      if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
+      if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
        {
          enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
          if (!targetm.valid_pointer_mode (p_mode))
@@ -7650,7 +7753,7 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
     case POINTER_TYPE:
       /* Only do something if this is a thin pointer, in which case we
         may need to return the fat pointer.  */
-      if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
+      if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
        return
          build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
       break;
@@ -7784,6 +7887,11 @@ check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
      OBJECT is either a type or a decl.  */
   if (TYPE_P (object))
     {
+      /* If this is an anonymous base type, nothing to check.  Error will be
+        reported on the source type.  */
+      if (!Comes_From_Source (gnat_entity))
+       return;
+
       mode = TYPE_MODE (object);
       align = TYPE_ALIGN (object);
       size = TYPE_SIZE (object);
@@ -7865,6 +7973,248 @@ compatible_signatures_p (tree ftype1, tree ftype2)
   return 1;
 }
 \f
+/* Return a FIELD_DECL node modeled on OLD_FIELD.  FIELD_TYPE is its type
+   and RECORD_TYPE is the type of the parent.  If SIZE is nonzero, it is the
+   specified size for this field.  POS_LIST is a position list describing
+   the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
+   to this layout.  */
+
+static tree
+create_field_decl_from (tree old_field, tree field_type, tree record_type,
+                       tree size, tree pos_list, tree subst_list)
+{
+  tree t = TREE_VALUE (purpose_member (old_field, pos_list));
+  tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
+  unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
+  tree new_pos, new_field;
+
+  if (CONTAINS_PLACEHOLDER_P (pos))
+    for (t = subst_list; t; t = TREE_CHAIN (t))
+      pos = SUBSTITUTE_IN_EXPR (pos, TREE_PURPOSE (t), TREE_VALUE (t));
+
+  /* If the position is now a constant, we can set it as the position of the
+     field when we make it.  Otherwise, we need to deal with it specially.  */
+  if (TREE_CONSTANT (pos))
+    new_pos = bit_from_pos (pos, bitpos);
+  else
+    new_pos = NULL_TREE;
+
+  new_field
+    = create_field_decl (DECL_NAME (old_field), field_type, record_type,
+                        DECL_PACKED (old_field), size, new_pos,
+                        !DECL_NONADDRESSABLE_P (old_field));
+
+  if (!new_pos)
+    {
+      normalize_offset (&pos, &bitpos, offset_align);
+      DECL_FIELD_OFFSET (new_field) = pos;
+      DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
+      SET_DECL_OFFSET_ALIGN (new_field, offset_align);
+      DECL_SIZE (new_field) = size;
+      DECL_SIZE_UNIT (new_field)
+       = convert (sizetype,
+                  size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
+      layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
+    }
+
+  DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
+  SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
+  DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
+  TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
+
+  return new_field;
+}
+
+/* Return the REP part of RECORD_TYPE, if any.  Otherwise return NULL.  */
+
+static tree
+get_rep_part (tree record_type)
+{
+  tree field = TYPE_FIELDS (record_type);
+
+  /* The REP part is the first field, internal, another record, and its name
+     doesn't start with an underscore (i.e. is not generated by the FE).  */
+  if (DECL_INTERNAL_P (field)
+      && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
+      && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
+    return field;
+
+  return NULL_TREE;
+}
+
+/* Return the variant part of RECORD_TYPE, if any.  Otherwise return NULL.  */
+
+static tree
+get_variant_part (tree record_type)
+{
+  tree field;
+
+  /* The variant part is the only internal field that is a qualified union.  */
+  for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
+    if (DECL_INTERNAL_P (field)
+       && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
+      return field;
+
+  return NULL_TREE;
+}
+
+/* Return a new variant part modeled on OLD_VARIANT_PART.  VARIANT_LIST is
+   the list of variants to be used and RECORD_TYPE is the type of the parent.
+   POS_LIST is a position list describing the layout of fields present in
+   OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
+   layout.  */
+
+static tree
+create_variant_part_from (tree old_variant_part, tree variant_list,
+                         tree record_type, tree pos_list, tree subst_list)
+{
+  tree offset = DECL_FIELD_OFFSET (old_variant_part);
+  tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
+  tree old_union_type = TREE_TYPE (old_variant_part);
+  tree new_union_type, new_variant_part, t;
+  tree union_field_list = NULL_TREE;
+
+  /* First create the type of the variant part from that of the old one.  */
+  new_union_type = make_node (QUAL_UNION_TYPE);
+  TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
+
+  /* If the position of the variant part is constant, subtract it from the
+     size of the type of the parent to get the new size.  This manual CSE
+     reduces the code size when not optimizing.  */
+  if (TREE_CODE (offset) == INTEGER_CST && TREE_CODE (bitpos) == INTEGER_CST)
+    {
+      tree first_bit = bit_from_pos (offset, bitpos);
+      TYPE_SIZE (new_union_type)
+       = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
+      TYPE_SIZE_UNIT (new_union_type)
+       = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
+                     byte_from_pos (offset, bitpos));
+      SET_TYPE_ADA_SIZE (new_union_type,
+                        size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
+                                    first_bit));
+      TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
+      relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
+    }
+  else
+    copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
+
+  /* Now finish up the new variants and populate the union type.  */
+  for (t = variant_list; t; t = TREE_CHAIN (t))
+    {
+      tree old_field = TREE_VEC_ELT (TREE_VALUE (t), 0), new_field;
+      tree old_variant, old_variant_subpart, new_variant, field_list;
+
+      /* Skip variants that don't belong to this nesting level.  */
+      if (DECL_CONTEXT (old_field) != old_union_type)
+       continue;
+
+      /* Retrieve the list of fields already added to the new variant.  */
+      new_variant = TREE_VEC_ELT (TREE_VALUE (t), 2);
+      field_list = TYPE_FIELDS (new_variant);
+
+      /* If the old variant had a variant subpart, we need to create a new
+        variant subpart and add it to the field list.  */
+      old_variant = TREE_PURPOSE (t);
+      old_variant_subpart = get_variant_part (old_variant);
+      if (old_variant_subpart)
+       {
+         tree new_variant_subpart
+           = create_variant_part_from (old_variant_subpart, variant_list,
+                                       new_variant, pos_list, subst_list);
+         TREE_CHAIN (new_variant_subpart) = field_list;
+         field_list = new_variant_subpart;
+       }
+
+      /* Finish up the new variant and create the field.  No need for debug
+        info thanks to the XVS type.  */
+      finish_record_type (new_variant, nreverse (field_list), 2, false);
+      compute_record_mode (new_variant);
+      create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
+                       true, false, Empty);
+
+      new_field
+       = create_field_decl_from (old_field, new_variant, new_union_type,
+                                 TYPE_SIZE (new_variant),
+                                 pos_list, subst_list);
+      DECL_QUALIFIER (new_field) = TREE_VEC_ELT (TREE_VALUE (t), 1);
+      DECL_INTERNAL_P (new_field) = 1;
+      TREE_CHAIN (new_field) = union_field_list;
+      union_field_list = new_field;
+    }
+
+  /* Finish up the union type and create the variant part.  No need for debug
+     info thanks to the XVS type.  */
+  finish_record_type (new_union_type, union_field_list, 2, false);
+  compute_record_mode (new_union_type);
+  create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
+                   true, false, Empty);
+
+  new_variant_part
+    = create_field_decl_from (old_variant_part, new_union_type, record_type,
+                             TYPE_SIZE (new_union_type),
+                             pos_list, subst_list);
+  DECL_INTERNAL_P (new_variant_part) = 1;
+
+  /* With multiple discriminants it is possible for an inner variant to be
+     statically selected while outer ones are not; in this case, the list
+     of fields of the inner variant is not flattened and we end up with a
+     qualified union with a single member.  Drop the useless container.  */
+  if (!TREE_CHAIN (union_field_list))
+    {
+      DECL_CONTEXT (union_field_list) = record_type;
+      DECL_FIELD_OFFSET (union_field_list)
+       = DECL_FIELD_OFFSET (new_variant_part);
+      DECL_FIELD_BIT_OFFSET (union_field_list)
+       = DECL_FIELD_BIT_OFFSET (new_variant_part);
+      SET_DECL_OFFSET_ALIGN (union_field_list,
+                            DECL_OFFSET_ALIGN (new_variant_part));
+      new_variant_part = union_field_list;
+    }
+
+  return new_variant_part;
+}
+
+/* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
+   which are both RECORD_TYPE, after applying the substitutions described
+   in SUBST_LIST.  */
+
+static void
+copy_and_substitute_in_size (tree new_type, tree old_type, tree subst_list)
+{
+  tree t;
+
+  TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
+  TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
+  SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
+  TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
+  relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
+
+  if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
+    for (t = subst_list; t; t = TREE_CHAIN (t))
+      TYPE_SIZE (new_type)
+       = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
+                             TREE_PURPOSE (t),
+                             TREE_VALUE (t));
+
+  if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
+    for (t = subst_list; t; t = TREE_CHAIN (t))
+      TYPE_SIZE_UNIT (new_type)
+       = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
+                             TREE_PURPOSE (t),
+                             TREE_VALUE (t));
+
+  if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
+    for (t = subst_list; t; t = TREE_CHAIN (t))
+      SET_TYPE_ADA_SIZE
+       (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
+                                      TREE_PURPOSE (t),
+                                      TREE_VALUE (t)));
+
+  /* Finalize the size.  */
+  TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
+  TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
+}
+\f
 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
    type with all size expressions that contain F in a PLACEHOLDER_EXPR
    updated by replacing F with R.
@@ -8004,9 +8354,7 @@ substitute_in_type (tree t, tree f, tree r)
              }
 
            DECL_CONTEXT (new_field) = nt;
-           SET_DECL_ORIGINAL_FIELD (new_field,
-                                    (DECL_ORIGINAL_FIELD (field)
-                                     ? DECL_ORIGINAL_FIELD (field) : field));
+           SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
 
            TREE_CHAIN (new_field) = TYPE_FIELDS (nt);
            TYPE_FIELDS (nt) = new_field;
@@ -8049,7 +8397,7 @@ rm_size (tree gnu_type)
   if ((TREE_CODE (gnu_type) == RECORD_TYPE
        || TREE_CODE (gnu_type) == UNION_TYPE
        || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
-      && !TYPE_IS_FAT_POINTER_P (gnu_type)
+      && !TYPE_FAT_POINTER_P (gnu_type)
       && TYPE_ADA_SIZE (gnu_type))
     return TYPE_ADA_SIZE (gnu_type);