OSDN Git Service

* gcc-interface/utils.c (record_builtin_type): Pass location
[pf3gnuchains/gcc-fork.git] / gcc / ada / gcc-interface / decl.c
index be30b85..63ade27 100644 (file)
@@ -6,7 +6,7 @@
  *                                                                          *
  *                          C Implementation File                           *
  *                                                                          *
- *          Copyright (C) 1992-2008, Free Software Foundation, Inc.         *
+ *          Copyright (C) 1992-2009, 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- *
@@ -30,9 +30,7 @@
 #include "tree.h"
 #include "flags.h"
 #include "toplev.h"
-#include "convert.h"
 #include "ggc.h"
-#include "obstack.h"
 #include "target.h"
 #include "expr.h"
 
@@ -49,7 +47,6 @@
 #include "fe.h"
 #include "sinfo.h"
 #include "einfo.h"
-#include "hashtab.h"
 #include "ada-tree.h"
 #include "gigi.h"
 
@@ -76,7 +73,7 @@
    support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
 
    It is defined on targets where the circuitry is available, and indicates
-   whether the realignment is needed for 'main'. We use this to decide for
+   whether the realignment is needed for 'main'.  We use this to decide for
    foreign subprograms as well.
 
    It is not defined on targets where the circuitry is not implemented, and
@@ -115,22 +112,30 @@ static VEC (tree,heap) *defer_finalize_list;
 static GTY ((if_marked ("tree_int_map_marked_p"),
             param_is (struct tree_int_map))) htab_t annotate_value_cache;
 
-static void copy_alias_set (tree, tree);
-static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
+enum alias_set_op
+{
+  ALIAS_SET_COPY,
+  ALIAS_SET_SUBSET,
+  ALIAS_SET_SUPERSET
+};
+
+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);
 static void prepend_attributes (Entity_Id, struct attrib **);
 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
 static bool is_variable_size (tree);
-static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
-                                   bool, bool);
+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_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
                               bool *);
 static bool same_discriminant_p (Entity_Id, Entity_Id);
 static bool array_type_has_nonaliased_component (Entity_Id, tree);
+static bool compile_time_known_address_p (Node_Id);
 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
                                  bool, bool, bool, bool);
 static Uint annotate_value (tree);
@@ -144,49 +149,18 @@ 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 void rest_of_type_decl_compilation_no_defer (tree);
-
-/* Return true if GNAT_ADDRESS is a compile time known value.
-   In particular catch System'To_Address.  */
-
-static bool
-compile_time_known_address_p (Node_Id gnat_address)
-{
-  return ((Nkind (gnat_address) == N_Unchecked_Type_Conversion
-          && Compile_Time_Known_Value (Expression (gnat_address)))
-         || Compile_Time_Known_Value (gnat_address));
-}
-
-/* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
-   GCC type corresponding to that entity.  GNAT_ENTITY is assumed to
-   refer to an Ada type.  */
-
-tree
-gnat_to_gnu_type (Entity_Id gnat_entity)
-{
-  tree gnu_decl;
-
-  /* The back end never attempts to annotate generic types */
-  if (Is_Generic_Type (gnat_entity) && type_annotate_only)
-     return void_type_node;
-
-  /* Convert the ada entity type into a GCC TYPE_DECL node.  */
-  gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
-  gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
-  return TREE_TYPE (gnu_decl);
-}
 \f
 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
-   entity, this routine returns the equivalent GCC tree for that entity
-   (an ..._DECL node) and associates the ..._DECL node with the input GNAT
-   defining identifier.
+   entity, return the equivalent GCC tree for that entity (a ..._DECL node)
+   and associate the ..._DECL node with the input GNAT defining identifier.
 
    If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
-   initial value (in GCC tree form). This is optional for variables.
-   For renamed entities, GNU_EXPR gives the object being renamed.
+   initial value (in GCC tree form).  This is optional for a variable.  For
+   a renamed entity, GNU_EXPR gives the object being renamed.
 
    DEFINITION is nonzero if this call is intended for a definition.  This is
-   used for separate compilation where it necessary to know whether an
-   external declaration or a definition should be created if the GCC equivalent
+   used for separate compilation where it is necessary to know whether an
+   external declaration or a definition must be created if the GCC equivalent
    was not created previously.  The value of 1 is normally used for a nonzero
    DEFINITION, but a value of 2 is used in special circumstances, defined in
    the code.  */
@@ -194,53 +168,56 @@ gnat_to_gnu_type (Entity_Id gnat_entity)
 tree
 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 {
-  Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
-  tree gnu_entity_id;
-  tree gnu_type = NULL_TREE;
-  /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
-     GNAT tree. This node will be associated with the GNAT node by calling
-     the save_gnu_tree routine at the end of the `switch' statement.  */
+  /* Contains the kind of the input GNAT node.  */
+  const Entity_Kind kind = Ekind (gnat_entity);
+  /* True if this is a type.  */
+  const bool is_type = IN (kind, Type_Kind);
+  /* 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.  */
+  Entity_Id gnat_temp;
+  /* Contains the GCC DECL node which is equivalent to the input GNAT node.
+     This node will be associated with the GNAT node by calling at the end
+     of the `switch' statement.  */
   tree gnu_decl = NULL_TREE;
-  /* true if we have already saved gnu_decl as a gnat association.  */
+  /* Contains the GCC type to be used for the GCC node.  */
+  tree gnu_type = NULL_TREE;
+  /* Contains the GCC size tree to be used for the GCC node.  */
+  tree gnu_size = NULL_TREE;
+  /* Contains the GCC name to be used for the GCC node.  */
+  tree gnu_entity_name;
+  /* True if we have already saved gnu_decl as a GNAT association.  */
   bool saved = false;
-  /* Nonzero if we incremented defer_incomplete_level.  */
+  /* True if we incremented defer_incomplete_level.  */
   bool this_deferred = false;
-  /* Nonzero if we incremented force_global.  */
+  /* True if we incremented force_global.  */
   bool this_global = false;
-  /* Nonzero if we should check to see if elaborated during processing.  */
+  /* True if we should check to see if elaborated during processing.  */
   bool maybe_present = false;
-  /* Nonzero if we made GNU_DECL and its type here.  */
+  /* True if we made GNU_DECL and its type here.  */
   bool this_made_decl = false;
-  struct attrib *attr_list = NULL;
+  /* True if debug info is requested for this entity.  */
   bool debug_info_p = (Needs_Debug_Info (gnat_entity)
                       || debug_info_level == DINFO_LEVEL_VERBOSE);
-  Entity_Kind kind = Ekind (gnat_entity);
-  Entity_Id gnat_temp;
-  unsigned int esize
-    = ((Known_Esize (gnat_entity)
-       && UI_Is_In_Int_Range (Esize (gnat_entity)))
-       ? MIN (UI_To_Int (Esize (gnat_entity)),
-             IN (kind, Float_Kind)
-             ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
-             : IN (kind, Access_Kind) ? POINTER_SIZE * 2
-             : LONG_LONG_TYPE_SIZE)
-       : LONG_LONG_TYPE_SIZE);
-  tree gnu_size = 0;
-  bool imported_p
-    = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
-  unsigned int align = 0;
+  /* 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.  */
+  struct attrib *attr_list = NULL;
 
   /* Since a use of an Itype is a definition, process it as such if it
-     is not in a with'ed unit. */
-
-  if (!definition && Is_Itype (gnat_entity)
+     is not in a with'ed unit.  */
+  if (!definition
+      && is_type
+      && Is_Itype (gnat_entity)
       && !present_gnu_tree (gnat_entity)
       && In_Extended_Main_Code_Unit (gnat_entity))
     {
-      /* Ensure that we are in a subprogram mentioned in the Scope
-        chain of this entity, our current scope is global,
-        or that we encountered a task or entry (where we can't currently
-        accurately check scoping).  */
+      /* Ensure that we are in a subprogram mentioned in the Scope chain of
+        this entity, our current scope is global, or we encountered a task
+        or entry (where we can't currently accurately check scoping).  */
       if (!current_function_decl
          || DECL_ELABORATION_PROC_P (current_function_decl))
        {
@@ -249,7 +226,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        }
 
       for (gnat_temp = Scope (gnat_entity);
-          Present (gnat_temp); gnat_temp = Scope (gnat_temp))
+          Present (gnat_temp);
+          gnat_temp = Scope (gnat_temp))
        {
          if (Is_Type (gnat_temp))
            gnat_temp = Underlying_Type (gnat_temp);
@@ -275,23 +253,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            }
        }
 
-      /* This abort means the entity "gnat_entity" has an incorrect scope,
-        i.e. that its scope does not correspond to the subprogram in which
-        it is declared */
+      /* This abort means the Itype has an incorrect scope, i.e. that its
+        scope does not correspond to the subprogram it is declared in.  */
       gcc_unreachable ();
     }
 
-  /* If this is entity 0, something went badly wrong.  */
-  gcc_assert (Present (gnat_entity));
-
   /* If we've already processed this entity, return what we got last time.
      If we are defining the node, we should not have already processed it.
-     In that case, we will abort below when we try to save a new GCC tree for
-     this object.   We also need to handle the case of getting a dummy type
-     when a Full_View exists.  */
-
-  if (present_gnu_tree (gnat_entity)
-      && (!definition || (Is_Type (gnat_entity) && imported_p)))
+     In that case, we will abort below when we try to save a new GCC tree
+     for this object.  We also need to handle the case of getting a dummy
+     type when a Full_View exists.  */
+  if ((!definition || (is_type && imported_p))
+      && present_gnu_tree (gnat_entity))
     {
       gnu_decl = get_gnu_tree (gnat_entity);
 
@@ -300,9 +273,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          && IN (kind, Incomplete_Or_Private_Kind)
          && Present (Full_View (gnat_entity)))
        {
-         gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
-                                        NULL_TREE, 0);
-
+         gnu_decl
+           = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
          save_gnu_tree (gnat_entity, NULL_TREE, false);
          save_gnu_tree (gnat_entity, gnu_decl, false);
        }
@@ -314,55 +286,85 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
      Esize must be specified unless it was specified by the programmer.  */
   gcc_assert (!Unknown_Esize (gnat_entity)
              || Has_Size_Clause (gnat_entity)
-             || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
+             || (!IN (kind, Numeric_Kind)
+                 && !IN (kind, Enumeration_Kind)
                  && (!IN (kind, Access_Kind)
                      || kind == E_Access_Protected_Subprogram_Type
                      || kind == E_Anonymous_Access_Protected_Subprogram_Type
                      || kind == E_Access_Subtype)));
 
-  /* Likewise, RM_Size must be specified for all discrete and fixed-point
-     types.  */
-  gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
-             || !Unknown_RM_Size (gnat_entity));
+  /* The RM size must be specified for all discrete and fixed-point types.  */
+  gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
+               && Unknown_RM_Size (gnat_entity)));
+
+  /* If we get here, it means we have not yet done anything with this entity.
+     If we are not defining it, it must be a type or an entity that is defined
+     elsewhere or externally, otherwise we should have defined it already.  */
+  gcc_assert (definition
+             || type_annotate_only
+             || is_type
+             || kind == E_Discriminant
+             || kind == E_Component
+             || kind == E_Label
+             || (kind == E_Constant && Present (Full_View (gnat_entity)))
+             || Is_Public (gnat_entity));
 
   /* Get the name of the entity and set up the line number and filename of
      the original definition for use in any decl we make.  */
-  gnu_entity_id = get_entity_name (gnat_entity);
+  gnu_entity_name = get_entity_name (gnat_entity);
   Sloc_to_locus (Sloc (gnat_entity), &input_location);
 
-  /* If we get here, it means we have not yet done anything with this
-     entity.  If we are not defining it here, it must be external,
-     otherwise we should have defined it already.  */
-  gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
-             || kind == E_Discriminant || kind == E_Component
-             || kind == E_Label
-             || (kind == E_Constant && Present (Full_View (gnat_entity)))
-             || IN (kind, Type_Kind));
-
   /* For cases when we are not defining (i.e., we are referencing from
-     another compilation unit) Public entities, show we are at global level
+     another compilation unit) public entities, show we are at global level
      for the purpose of computing scopes.  Don't do this for components or
      discriminants since the relevant test is whether or not the record is
-     being defined.  But do this for Imported functions or procedures in
-     all cases.  */
-  if ((!definition && Is_Public (gnat_entity)
-       && !Is_Statically_Allocated (gnat_entity)
-       && kind != E_Discriminant && kind != E_Component)
-      || (Is_Imported (gnat_entity)
-         && (kind == E_Function || kind == E_Procedure)))
+     being defined.  */
+  if (!definition
+      && kind != E_Component
+      && kind != E_Discriminant
+      && Is_Public (gnat_entity)
+      && !Is_Statically_Allocated (gnat_entity))
     force_global++, this_global = true;
 
   /* Handle any attributes directly attached to the entity.  */
   if (Has_Gigi_Rep_Item (gnat_entity))
     prepend_attributes (gnat_entity, &attr_list);
 
-  /* Machine_Attributes on types are expected to be propagated to subtypes.
-     The corresponding Gigi_Rep_Items are only attached to the first subtype
-     though, so we handle the propagation here.  */
-  if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
-      && !Is_First_Subtype (gnat_entity)
-      && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
-    prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
+  /* Do some common processing for types.  */
+  if (is_type)
+    {
+      /* Compute the equivalent type to be used in gigi.  */
+      gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
+
+      /* Machine_Attributes on types are expected to be propagated to
+        subtypes.  The corresponding Gigi_Rep_Items are only attached
+        to the first subtype though, so we handle the propagation here.  */
+      if (Base_Type (gnat_entity) != gnat_entity
+         && !Is_First_Subtype (gnat_entity)
+         && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
+       prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
+                           &attr_list);
+
+      /* Compute a default value for the size of the type.  */
+      if (Known_Esize (gnat_entity)
+         && UI_Is_In_Int_Range (Esize (gnat_entity)))
+       {
+         unsigned int max_esize;
+         esize = UI_To_Int (Esize (gnat_entity));
+
+         if (IN (kind, Float_Kind))
+           max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
+         else if (IN (kind, Access_Kind))
+           max_esize = POINTER_SIZE * 2;
+         else
+           max_esize = LONG_LONG_TYPE_SIZE;
+
+         if (esize > max_esize)
+          esize = max_esize;
+       }
+      else
+       esize = LONG_LONG_TYPE_SIZE;
+    }
 
   switch (kind)
     {
@@ -431,13 +433,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
         the regular processing take place, which leaves us with a regular
         exception data object for VMS exceptions too.  The condition code
         mapping is taken care of by the front end and the bitmasking by the
-        runtime library.   */
+        runtime library.  */
       goto object;
 
     case E_Discriminant:
     case E_Component:
       {
-       /* The GNAT record where the component was defined. */
+       /* The GNAT record where the component was defined.  */
        Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
 
        /* If the variable is an inherited record component (in the case of
@@ -447,7 +449,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           stored discriminants, return the entity for the corresponding
           stored discriminant.  Also use Original_Record_Component
           if the record has a private extension.  */
-
        if (Present (Original_Record_Component (gnat_entity))
            && Original_Record_Component (gnat_entity) != gnat_entity)
          {
@@ -462,14 +463,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           then it is an untagged record.  If the Corresponding_Discriminant
           is not empty then this must be a renamed discriminant and its
           Original_Record_Component must point to the corresponding explicit
-          stored discriminant (i.e., we should have taken the previous
+          stored discriminant (i.e. we should have taken the previous
           branch).  */
-
        else if (Present (Corresponding_Discriminant (gnat_entity))
                 && Is_Tagged_Type (gnat_record))
          {
-           /* A tagged record has no explicit stored discriminants. */
-
+           /* A tagged record has no explicit stored discriminants.  */
            gcc_assert (First_Discriminant (gnat_record)
                       == First_Stored_Discriminant (gnat_record));
            gnu_decl
@@ -488,13 +487,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            break;
          }
 
-       /* If the enclosing record has explicit stored discriminants,
-          then it is an untagged record. If the Corresponding_Discriminant
+       /* If the enclosing record has explicit stored discriminants, then
+          it is an untagged record.  If the Corresponding_Discriminant
           is not empty then this must be a renamed discriminant and its
           Original_Record_Component must point to the corresponding explicit
-          stored discriminant (i.e., we should have taken the first
+          stored discriminant (i.e. we should have taken the first
           branch).  */
-
        else if (Present (Corresponding_Discriminant (gnat_entity))
                 && (First_Discriminant (gnat_record)
                     != First_Stored_Discriminant (gnat_record)))
@@ -532,7 +530,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        else
          /* Here we have no GCC type and this is a reference rather than a
-            definition. This should never happen. Most likely the cause is a
+            definition.  This should never happen.  Most likely the cause is
             reference before declaration in the gnat tree for gnat_entity.  */
          gcc_unreachable ();
       }
@@ -576,7 +574,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (Present (Debug_Renaming_Link (gnat_entity)))
          {
            rtx addr;
-           gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
+           gnu_decl = build_decl (input_location,
+                                  VAR_DECL, gnu_entity_name, gnu_type);
            /* The (MEM (CONST (0))) pattern is prescribed by STABS.  */
            if (global_bindings_p ())
              addr = gen_rtx_CONST (VOIDmode, const0_rtx);
@@ -597,7 +596,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          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. */
+          any object whose type is a dummy type or VOID_TYPE.  */
 
        if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
             && No (Renamed_Object (gnat_entity)))
@@ -610,22 +609,27 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            return error_mark_node;
          }
 
-       /* If an alignment is specified, use it if valid.   Note that
-          exceptions are objects but don't have alignments.  We must do this
-          before we validate the size, since the alignment can affect the
-          size.  */
+       /* If an alignment is specified, use it if valid.  Note that exceptions
+          are objects but don't have an alignment.  We must do this before we
+          validate the size, since the alignment can affect the size.  */
        if (kind != E_Exception && Known_Alignment (gnat_entity))
          {
            gcc_assert (Present (Alignment (gnat_entity)));
            align = validate_alignment (Alignment (gnat_entity), gnat_entity,
                                        TYPE_ALIGN (gnu_type));
-           gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
-                                      "PAD", false, definition, true);
+           /* 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)))
+             align = 0;
+           else
+             gnu_type
+               = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
+                                 "PAD", 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
+          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)
@@ -711,8 +715,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
            && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
                || !Is_Array_Type (Etype (gnat_entity)))
-           && !Present (Renamed_Object (gnat_entity))
-           && !Present (Address_Clause (gnat_entity)))
+           && No (Renamed_Object (gnat_entity))
+           && No (Address_Clause (gnat_entity)))
          gnu_size = bitsize_unit_node;
 
        /* If this is an object with no specified size and alignment, and
@@ -762,7 +766,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           type and validate it.
 
           ??? Note that we ignore Has_Volatile_Components on objects; it's
-          not at all clear what to do in that case. */
+          not at all clear what to do in that case.  */
 
        if (Has_Atomic_Components (gnat_entity))
          {
@@ -796,8 +800,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
          gnu_type
            = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
-                                    concat_id_with_name (gnu_entity_id,
-                                                         "UNC"));
+                                             concat_name (gnu_entity_name,
+                                                          "UNC"));
        }
 
 #ifdef MINIMUM_ATOMIC_ALIGNMENT
@@ -843,7 +847,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    == 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)
+               || (TREE_CODE (gnu_expr) == NOP_EXPR
+                   && gnat_types_compatible_p
+                      (TREE_TYPE (gnu_expr),
+                       TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
              {
                gnu_expr = TREE_OPERAND (gnu_expr, 0);
                gnu_type = TREE_TYPE (gnu_expr);
@@ -907,7 +914,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                /* Case 3: If this is a constant renaming and creating a
                   new object is allowed and cheap, treat it as a normal
                   object whose initial value is what is being renamed.  */
-               if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
+               if (const_flag
+                   && !Is_Composite_Type
+                       (Underlying_Type (Etype (gnat_entity))))
                  ;
 
                /* Case 4: Make this into a constant pointer to the object we
@@ -977,15 +986,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Make a volatile version of this object's type if we are to make
           the object volatile.  We also interpret 13.3(19) conservatively
-          and disallow any optimizations for an object covered by it.  */
+          and disallow any optimizations for such a non-constant object.  */
        if ((Treat_As_Volatile (gnat_entity)
-            || (Is_Exported (gnat_entity)
-                /* Exclude exported constants created by the compiler,
-                   which should boil down to static dispatch tables and
-                   make it possible to put them in read-only memory.  */
-                && (Comes_From_Source (gnat_entity) || !const_flag))
-            || Is_Imported (gnat_entity)
-            || Present (Address_Clause (gnat_entity)))
+            || (!const_flag
+                && (Is_Exported (gnat_entity)
+                    || Is_Imported (gnat_entity)
+                    || Present (Address_Clause (gnat_entity)))))
            && !TYPE_VOLATILE (gnu_type))
          gnu_type = build_qualified_type (gnu_type,
                                           (TYPE_QUALS (gnu_type)
@@ -1198,8 +1204,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  post_error ("?Storage_Error will be raised at run-time!",
                              gnat_entity);
 
-               gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
-                                           0, 0, gnat_entity, mutable_p);
+               gnu_expr
+                 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
+                                    Empty, Empty, gnat_entity, mutable_p);
              }
            else
              {
@@ -1277,7 +1284,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                || (Is_Public (gnat_entity)
                    && (!Is_Imported (gnat_entity)
                        || Is_Exported (gnat_entity)))))
-         gnu_ext_name = create_concat_name (gnat_entity, 0);
+         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
@@ -1292,7 +1299,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                    (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
          static_p = true;
 
-       gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
+       gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
                                    gnu_expr, const_flag,
                                    Is_Public (gnat_entity),
                                    imported_p || !definition,
@@ -1328,7 +1335,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           accessed from within the debugger through the PARM_DECL.  */
        if (kind == E_Out_Parameter && definition && !optimize)
          {
-           tree param = create_param_decl (gnu_entity_id, gnu_type, false);
+           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;
@@ -1347,15 +1354,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           for these.  */
        if (TREE_CODE (gnu_decl) == CONST_DECL
            && (definition || Sloc (gnat_entity) > Standard_Location)
-           && ((Is_Public (gnat_entity)
-                && !Present (Address_Clause (gnat_entity)))
+           && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
                || !optimize
                || Address_Taken (gnat_entity)
                || Is_Aliased (gnat_entity)
                || Is_Aliased (Etype (gnat_entity))))
          {
            tree gnu_corr_var
-             = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
+             = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
                                      gnu_expr, true, Is_Public (gnat_entity),
                                      !definition, static_p, NULL,
                                      gnat_entity);
@@ -1405,36 +1411,32 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
     case E_Void:
       /* Return a TYPE_DECL for "void" that we previously made.  */
-      gnu_decl = void_type_decl_node;
+      gnu_decl = TYPE_NAME (void_type_node);
       break;
 
     case E_Enumeration_Type:
-      /* A special case, for the types Character and Wide_Character in
-        Standard, we do not list all the literals. So if the literals
+      /* A special case: for the types Character and Wide_Character in
+        Standard, we do not list all the literals.  So if the literals
         are not specified, make this an unsigned type.  */
       if (No (First_Literal (gnat_entity)))
        {
          gnu_type = make_unsigned_type (esize);
-         TYPE_NAME (gnu_type) = gnu_entity_id;
+         TYPE_NAME (gnu_type) = gnu_entity_name;
 
-         /* Set the TYPE_STRING_FLAG for Ada Character and
-            Wide_Character types. This is needed by the dwarf-2 debug writer to
-            distinguish between unsigned integer types and character types.  */
+         /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
+            This is needed by the DWARF-2 back-end to distinguish between
+            unsigned integer types and character types.  */
          TYPE_STRING_FLAG (gnu_type) = 1;
          break;
        }
 
-      /* Normal case of non-character type, or non-Standard character type */
+      /* 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 places into TYPE_FIELDS.  Each node in the list
-          is a TREE_LIST node whose TREE_VALUE is the literal name
-          and whose TREE_PURPOSE is the value of the literal.
-
-          Esize contains the number of bits needed to represent the enumeral
-          type, Type_Low_Bound also points to the first literal and
-          Type_High_Bound points to the last literal.  */
+          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;
        tree gnu_literal_list = NULL_TREE;
@@ -1465,8 +1467,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
 
        /* Note that the bounds are updated at the end of this function
-          because to avoid an infinite recursion when we get the bounds of
-          this type, since those bounds are objects of this type.    */
+          to avoid an infinite recursion since they refer to the type.  */
       }
       break;
 
@@ -1479,28 +1480,15 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       break;
 
     case E_Modular_Integer_Type:
-      /* For modular types, make the unsigned type of the proper number of
-        bits and then set up the modulus, if required.  */
       {
-       enum machine_mode mode;
-       tree gnu_modulus;
-       tree gnu_high = 0;
-
-       if (Is_Packed_Array_Type (gnat_entity))
-         esize = UI_To_Int (RM_Size (gnat_entity));
+       /* For modular types, make the unsigned type of the proper number
+          of bits and then set up the modulus, if required.  */
+       tree gnu_modulus, gnu_high = NULL_TREE;
 
-       /* Find the smallest mode at least ESIZE bits wide and make a class
-          using that mode.  */
+       /* Packed array types are supposed to be subtypes only.  */
+       gcc_assert (!Is_Packed_Array_Type (gnat_entity));
 
-       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
-            GET_MODE_BITSIZE (mode) < esize;
-            mode = GET_MODE_WIDER_MODE (mode))
-         ;
-
-       gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
-       TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
-         = (Is_Packed_Array_Type (gnat_entity)
-            && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
+       gnu_type = make_unsigned_type (esize);
 
        /* Get the modulus in this type.  If it overflows, assume it is because
           it is equal to 2**Esize.  Note that there is no overflow checking
@@ -1516,29 +1504,15 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                    convert (gnu_type, integer_one_node));
          }
 
-       /* If we have to set TYPE_PRECISION different from its natural value,
-          make a subtype to do do.  Likewise if there is a modulus and
-          it is not one greater than TYPE_MAX_VALUE.  */
-       if (TYPE_PRECISION (gnu_type) != esize
-           || (TYPE_MODULAR_P (gnu_type)
-               && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
+       /* If the upper bound is not maximal, make an extra subtype.  */
+       if (gnu_high
+           && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
          {
-           tree gnu_subtype = make_node (INTEGER_TYPE);
-
-           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
+           tree gnu_subtype = make_unsigned_type (esize);
+           SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
            TREE_TYPE (gnu_subtype) = gnu_type;
-           TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
-           TYPE_MAX_VALUE (gnu_subtype)
-             = TYPE_MODULAR_P (gnu_type)
-               ? gnu_high : TYPE_MAX_VALUE (gnu_type);
-           TYPE_PRECISION (gnu_subtype) = esize;
-           TYPE_UNSIGNED (gnu_subtype) = 1;
            TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
-           TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
-             = (Is_Packed_Array_Type (gnat_entity)
-                && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
-           layout_type (gnu_subtype);
-
+           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
            gnu_type = gnu_subtype;
          }
       }
@@ -1550,58 +1524,58 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_Ordinary_Fixed_Point_Subtype:
     case E_Decimal_Fixed_Point_Subtype:
 
-      /* For integral subtypes, we make a new INTEGER_TYPE.  Note
-        that we do not want to call build_range_type since we would
-        like each subtype node to be distinct.  This will be important
-        when memory aliasing is implemented.
+      /* For integral subtypes, we make a new INTEGER_TYPE.  Note that we do
+        not want to call create_range_type since we would like each subtype
+        node to be distinct.  ??? Historically this was in preparation for
+        when memory aliasing is implemented, but that's obsolete now given
+        the call to relate_alias_sets below.
 
-        The TREE_TYPE field of the INTEGER_TYPE we make points to the
-        parent type; this fact is used by the arithmetic conversion
-        functions.
+        The TREE_TYPE field of the INTEGER_TYPE points to the base type;
+        this fact is used by the arithmetic conversion functions.
 
-        We elaborate the Ancestor_Subtype if it is not in the current
-        unit and one of our bounds is non-static.  We do this to ensure
-        consistent naming in the case where several subtypes share the same
-        bounds by always elaborating the first such subtype first, thus
-        using its name. */
+        We elaborate the Ancestor_Subtype if it is not in the current unit
+        and one of our bounds is non-static.  We do this to ensure consistent
+        naming in the case where several subtypes share the same bounds, by
+        elaborating the first such subtype first, thus using its name.  */
 
       if (!definition
          && Present (Ancestor_Subtype (gnat_entity))
          && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
          && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
              || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
-       gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
-                           gnu_expr, 0);
-
-      gnu_type = make_node (INTEGER_TYPE);
-      TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
+       gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
 
-      /* Set the precision to the Esize except for bit-packed arrays and
-        subtypes of Standard.Boolean.  */
+      /* Set the precision to the Esize except for bit-packed arrays.  */
       if (Is_Packed_Array_Type (gnat_entity)
          && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
-       {
-         esize = UI_To_Int (RM_Size (gnat_entity));
-         TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
-       }
-      else if (TREE_CODE (TREE_TYPE (gnu_type)) == BOOLEAN_TYPE)
-        esize = 1;
-
-      TYPE_PRECISION (gnu_type) = esize;
-
-      TYPE_MIN_VALUE (gnu_type)
-       = convert (TREE_TYPE (gnu_type),
-                  elaborate_expression (Type_Low_Bound (gnat_entity),
-                                        gnat_entity,
-                                        get_identifier ("L"), definition, 1,
-                                        Needs_Debug_Info (gnat_entity)));
+       esize = UI_To_Int (RM_Size (gnat_entity));
+
+      /* This should be an unsigned type if the base type is unsigned or
+        if the lower bound is constant and non-negative or if the type
+        is biased.  */
+      if (Is_Unsigned_Type (Etype (gnat_entity))
+         || Is_Unsigned_Type (gnat_entity)
+         || Has_Biased_Representation (gnat_entity))
+       gnu_type = make_unsigned_type (esize);
+      else
+       gnu_type = make_signed_type (esize);
+      TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
 
-      TYPE_MAX_VALUE (gnu_type)
-       = convert (TREE_TYPE (gnu_type),
-                  elaborate_expression (Type_High_Bound (gnat_entity),
-                                        gnat_entity,
-                                        get_identifier ("U"), definition, 1,
-                                        Needs_Debug_Info (gnat_entity)));
+      SET_TYPE_RM_MIN_VALUE
+       (gnu_type,
+        convert (TREE_TYPE (gnu_type),
+                 elaborate_expression (Type_Low_Bound (gnat_entity),
+                                       gnat_entity, get_identifier ("L"),
+                                       definition, true,
+                                       Needs_Debug_Info (gnat_entity))));
+
+      SET_TYPE_RM_MAX_VALUE
+       (gnu_type,
+        convert (TREE_TYPE (gnu_type),
+                 elaborate_expression (Type_High_Bound (gnat_entity),
+                                       gnat_entity, get_identifier ("U"),
+                                       definition, true,
+                                       Needs_Debug_Info (gnat_entity))));
 
       /* One of the above calls might have caused us to be elaborated,
         so don't blow up if so.  */
@@ -1614,43 +1588,46 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       TYPE_BIASED_REPRESENTATION_P (gnu_type)
        = Has_Biased_Representation (gnat_entity);
 
-     /* This should be an unsigned type if the lower bound is constant
-        and non-negative or if the base type is unsigned; a signed type
-        otherwise.    */
-      TYPE_UNSIGNED (gnu_type)
-       = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
-          || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
-              && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
-          || TYPE_BIASED_REPRESENTATION_P (gnu_type)
-          || Is_Unsigned_Type (gnat_entity));
-
-      layout_type (gnu_type);
+      /* Attach the TYPE_STUB_DECL in case we have a parallel type.  */
+      TYPE_STUB_DECL (gnu_type)
+       = create_type_stub_decl (gnu_entity_name, gnu_type);
 
       /* Inherit our alias set from what we're a subtype of.  Subtypes
         are not different types and a pointer can designate any instance
         within a subtype hierarchy.  */
-      copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
+      relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
+
+      /* For a packed array, make the original array type a parallel type.  */
+      if (debug_info_p
+         && Is_Packed_Array_Type (gnat_entity)
+         && 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 the type we are dealing with is to represent a packed array,
+      /* 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
         ensure that when the value is read (e.g. for comparison of two
         such values), we only get the good bits, since the unused bits
-        are uninitialized.  Both goals are accomplished by wrapping the
-        modular value in an enclosing struct.  */
+        are uninitialized.  Both goals are accomplished by wrapping up
+        the modular type in an enclosing record type.  */
       if (Is_Packed_Array_Type (gnat_entity)
          && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
        {
-         tree gnu_field_type = gnu_type;
-         tree gnu_field;
+         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));
+         TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
+         gnu_field_type = gnu_type;
 
-         TYPE_RM_SIZE_NUM (gnu_field_type)
-           = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
          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 bitpacked arrays have "ceil" alignment for
+            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);
@@ -1658,8 +1635,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
          /* Create a stripped-down declaration of the original type, mainly
             for debugging.  */
-         create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
-                           NULL, true, debug_info_p, gnat_entity);
+         create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
+                           debug_info_p, gnat_entity);
 
          /* 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
@@ -1667,23 +1644,36 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          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);
+         /* Do not finalize it until after the parallel type is added.  */
+         finish_record_type (gnu_type, gnu_field, 0, true);
          TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
-         SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
 
-         copy_alias_set (gnu_type, gnu_field_type);
+         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)));
+
+         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 (Known_Alignment (gnat_entity)
+      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))
        {
-         tree gnu_field_type = gnu_type;
-         tree gnu_field;
+         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));
+         gnu_field_type = gnu_type;
 
          gnu_type = make_node (RECORD_TYPE);
          TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
@@ -1693,8 +1683,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
          /* Create a stripped-down declaration of the original type, mainly
             for debugging.  */
-         create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
-                           NULL, true, debug_info_p, gnat_entity);
+         create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
+                           debug_info_p, gnat_entity);
 
          /* 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
@@ -1704,9 +1694,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
          finish_record_type (gnu_type, gnu_field, 0, false);
          TYPE_IS_PADDING_P (gnu_type) = 1;
-         SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
 
-         copy_alias_set (gnu_type, gnu_field_type);
+         relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
        }
 
       /* Otherwise reset the alignment lest we computed it above.  */
@@ -1754,20 +1743,27 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_type = make_node (REAL_TYPE);
        TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
        TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
+       TYPE_GCC_MIN_VALUE (gnu_type)
+         = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
+       TYPE_GCC_MAX_VALUE (gnu_type)
+         = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
+       layout_type (gnu_type);
 
-       TYPE_MIN_VALUE (gnu_type)
-         = convert (TREE_TYPE (gnu_type),
-                    elaborate_expression (Type_Low_Bound (gnat_entity),
-                                          gnat_entity, get_identifier ("L"),
-                                          definition, 1,
-                                          Needs_Debug_Info (gnat_entity)));
-
-       TYPE_MAX_VALUE (gnu_type)
-         = convert (TREE_TYPE (gnu_type),
-                    elaborate_expression (Type_High_Bound (gnat_entity),
-                                          gnat_entity, get_identifier ("U"),
-                                          definition, 1,
-                                          Needs_Debug_Info (gnat_entity)));
+       SET_TYPE_RM_MIN_VALUE
+         (gnu_type,
+          convert (TREE_TYPE (gnu_type),
+                   elaborate_expression (Type_Low_Bound (gnat_entity),
+                                         gnat_entity, get_identifier ("L"),
+                                         definition, true,
+                                         Needs_Debug_Info (gnat_entity))));
+
+       SET_TYPE_RM_MAX_VALUE
+         (gnu_type,
+          convert (TREE_TYPE (gnu_type),
+                   elaborate_expression (Type_High_Bound (gnat_entity),
+                                         gnat_entity, get_identifier ("U"),
+                                         definition, true,
+                                         Needs_Debug_Info (gnat_entity))));
 
        /* One of the above calls might have caused us to be elaborated,
           so don't blow up if so.  */
@@ -1777,11 +1773,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            break;
          }
 
-       layout_type (gnu_type);
-
        /* Inherit our alias set from what we're a subtype of, as for
           integer subtypes.  */
-       copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
+       relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
       }
     break;
 
@@ -1801,25 +1795,23 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_String_Type:
     case E_Array_Type:
       {
-       tree gnu_template_fields = NULL_TREE;
-       tree gnu_template_type = make_node (RECORD_TYPE);
-       tree gnu_ptr_template = build_pointer_type (gnu_template_type);
-       tree gnu_fat_type = make_node (RECORD_TYPE);
+       Entity_Id gnat_ind_subtype;
+       Entity_Id gnat_ind_base_subtype;
        int ndim = Number_Dimensions (gnat_entity);
-       int firstdim
+       int first_dim
          = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
-       int nextdim
+       int next_dim
          = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
        int index;
-       tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
-       tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
-       tree gnu_comp_size = 0;
-       tree gnu_max_size = size_one_node;
-       tree gnu_max_size_unit;
-       Entity_Id gnat_ind_subtype;
-       Entity_Id gnat_ind_base_subtype;
+       tree gnu_template_fields = NULL_TREE;
+       tree gnu_template_type = make_node (RECORD_TYPE);
        tree gnu_template_reference;
-       tree tem;
+       tree gnu_ptr_template = build_pointer_type (gnu_template_type);
+       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;
 
        TYPE_NAME (gnu_template_type)
          = create_concat_name (gnat_entity, "XUB");
@@ -1829,7 +1821,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
 
        if (!definition)
-         defer_incomplete_level++, this_deferred = true;
+         {
+           defer_incomplete_level++;
+           this_deferred = true;
+         }
 
        /* Build the fat pointer type.  Use a "void *" object instead of
           a pointer to the array type since we don't have the array type
@@ -1862,11 +1857,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Now create the GCC type for each index and add the fields for
           that index to the template.  */
-       for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
+       for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
             gnat_ind_base_subtype
               = First_Index (Implementation_Base_Type (gnat_entity));
             index < ndim && index >= 0;
-            index += nextdim,
+            index += next_dim,
             gnat_ind_subtype = Next_Index (gnat_ind_subtype),
             gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
          {
@@ -1914,10 +1909,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            gnu_index_types[index]
              = create_index_type (convert (sizetype, gnu_min),
                                   convert (sizetype, gnu_max),
-                                  build_range_type (gnu_ind_subtype,
-                                                    gnu_min, gnu_max),
+                                  create_range_type (gnu_ind_subtype,
+                                                     gnu_min, gnu_max),
                                   gnat_entity);
-           /* Update the maximum size of the array, in elements. */
+           /* Update the maximum size of the array, in elements.  */
            gnu_max_size
              = size_binop (MULT_EXPR, gnu_max_size,
                            size_binop (PLUS_EXPR, size_one_node,
@@ -1947,6 +1942,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            && !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);
 
@@ -1954,7 +1950,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          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. */
+          ignore it since the front end will have taken care of it.  */
        gnu_comp_size
          = validate_size (Component_Size (gnat_entity), tem,
                           gnat_entity,
@@ -1964,17 +1960,35 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* 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
+       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;
+           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);
-           orig_tem = tem;
+           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.  */
@@ -2009,14 +2023,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              TYPE_NONALIASED_COMPONENT (tem) = 1;
          }
 
-       /* If an alignment is specified, use it if valid.  But ignore it for
-          types that represent the unpacked base type for packed arrays.  If
-          the alignment was requested with an explicit user alignment clause,
-          state so.  */
+       /* If an alignment is specified, use it if valid.  But ignore it
+          for the original type of packed array types.  If the alignment
+          was requested with an explicit alignment clause, state so.  */
        if (No (Packed_Array_Type (gnat_entity))
            && Known_Alignment (gnat_entity))
          {
-           gcc_assert (Present (Alignment (gnat_entity)));
            TYPE_ALIGN (tem)
              = validate_alignment (Alignment (gnat_entity), gnat_entity,
                                    TYPE_ALIGN (tem));
@@ -2032,7 +2044,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           corresponding fat pointer.  */
        TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
          = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
-       TYPE_MODE (gnu_type) = BLKmode;
+       SET_TYPE_MODE (gnu_type, BLKmode);
        TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
        SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
 
@@ -2053,7 +2065,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Give the fat pointer type a name.  */
        create_type_decl (create_concat_name (gnat_entity, "XUP"),
-                         gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
+                         gnu_fat_type, NULL, true,
                          debug_info_p, gnat_entity);
 
        /* Create the type to be used as what a thin pointer designates: an
@@ -2068,9 +2080,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Give the thin pointer type a name.  */
        create_type_decl (create_concat_name (gnat_entity, "XUX"),
-                         build_pointer_type (tem), NULL,
-                         !Comes_From_Source (gnat_entity), debug_info_p,
-                         gnat_entity);
+                         build_pointer_type (tem), NULL, true,
+                         debug_info_p, gnat_entity);
       }
       break;
 
@@ -2094,20 +2105,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        break;
       else
        {
-         int index;
-         int array_dim = Number_Dimensions (gnat_entity);
+         Entity_Id gnat_ind_subtype;
+         Entity_Id gnat_ind_base_subtype;
+         int dim = Number_Dimensions (gnat_entity);
          int first_dim
-           = ((Convention (gnat_entity) == Convention_Fortran)
-              ? array_dim - 1 : 0);
+           = (Convention (gnat_entity) == Convention_Fortran) ? dim - 1 : 0;
          int next_dim
            = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
-         Entity_Id gnat_ind_subtype;
-         Entity_Id gnat_ind_base_subtype;
+         int index;
          tree gnu_base_type = gnu_type;
-         tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
-         tree gnu_comp_size = NULL_TREE;
-         tree gnu_max_size = size_one_node;
-         tree gnu_max_size_unit;
+         tree *gnu_index_type = (tree *) alloca (dim * sizeof (tree));
+         tree gnu_max_size = size_one_node, gnu_max_size_unit;
          bool need_index_type_struct = false;
          bool max_overflow = false;
 
@@ -2119,7 +2127,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
               gnat_ind_base_subtype
                 = First_Index (Implementation_Base_Type (gnat_entity));
-              index < array_dim && index >= 0;
+              index < dim && index >= 0;
               index += next_dim,
               gnat_ind_subtype = Next_Index (gnat_ind_subtype),
               gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
@@ -2155,62 +2163,85 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  && TREE_CODE (gnu_min) == INTEGER_CST
                  && TREE_CODE (gnu_max) == INTEGER_CST
                  && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
-                 && (!TREE_OVERFLOW
+                 && !TREE_OVERFLOW
                      (fold_build2 (MINUS_EXPR, gnu_index_subtype,
                                    TYPE_MAX_VALUE (gnu_index_subtype),
-                                   TYPE_MIN_VALUE (gnu_index_subtype)))))
+                                   TYPE_MIN_VALUE (gnu_index_subtype))))
                {
                  TREE_OVERFLOW (gnu_min) = 0;
                  TREE_OVERFLOW (gnu_max) = 0;
+                 if (tree_int_cst_lt (gnu_max, gnu_min))
+                   {
+                     gnu_min = size_one_node;
+                     gnu_max = size_zero_node;
+                   }
+                 gnu_high = gnu_max;
                }
 
              /* Similarly, if the range is null, use bounds of 1..0 for
                 the sizetype bounds.  */
              else if ((TYPE_PRECISION (gnu_index_subtype)
                        > TYPE_PRECISION (sizetype)
-                      || TYPE_UNSIGNED (gnu_index_subtype)
-                         != TYPE_UNSIGNED (sizetype))
+                       || TYPE_UNSIGNED (gnu_index_subtype)
+                          != TYPE_UNSIGNED (sizetype))
                       && TREE_CODE (gnu_min) == INTEGER_CST
                       && TREE_CODE (gnu_max) == INTEGER_CST
                       && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
                       && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
                                           TYPE_MIN_VALUE (gnu_index_subtype)))
-               gnu_min = size_one_node, gnu_max = size_zero_node;
-
-             /* Now compute the size of this bound.  We need to provide
-                GCC with an upper bound to use but have to 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 subtype.  If we can
-                prove that the low bound minus one can't overflow, we
-                can do this as MAX (hb, lb - 1).  Otherwise, we have to use
-                the expression hb >= lb ? hb : lb - 1.  */
-             gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
+               {
+                 gnu_min = size_one_node;
+                 gnu_max = size_zero_node;
+                 gnu_high = gnu_max;
+               }
 
              /* See if the base array type is already flat.  If it is, we
-                are probably compiling an ACVC test, but it will cause the
+                are probably compiling an ACATS test, but it will cause the
                 code below to malfunction if we don't handle it specially.  */
-             if (TREE_CODE (gnu_base_min) == INTEGER_CST
-                 && TREE_CODE (gnu_base_max) == INTEGER_CST
-                 && !TREE_OVERFLOW (gnu_base_min)
-                 && !TREE_OVERFLOW (gnu_base_max)
-                 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
-               gnu_high = size_zero_node, gnu_min = size_one_node;
-
-             /* If gnu_high is now an integer which overflowed, the array
-                cannot be superflat.  */
-             else if (TREE_CODE (gnu_high) == INTEGER_CST
-                      && TREE_OVERFLOW (gnu_high))
-               gnu_high = gnu_max;
-             else if (TYPE_UNSIGNED (gnu_base_subtype)
-                      || TREE_CODE (gnu_high) == INTEGER_CST)
-               gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
+             else if (TREE_CODE (gnu_base_min) == INTEGER_CST
+                      && TREE_CODE (gnu_base_max) == INTEGER_CST
+                      && !TREE_OVERFLOW (gnu_base_min)
+                      && !TREE_OVERFLOW (gnu_base_max)
+                      && tree_int_cst_lt (gnu_base_max, gnu_base_min))
+               {
+                 gnu_min = size_one_node;
+                 gnu_max = size_zero_node;
+                 gnu_high = gnu_max;
+               }
+
              else
-               gnu_high
-                 = build_cond_expr
-                   (sizetype, build_binary_op (GE_EXPR, integer_type_node,
-                                               gnu_max, gnu_min),
-                    gnu_max, gnu_high);
+               {
+                 /* Now compute the size of this bound.  We need to provide
+                    GCC with an upper bound to use but have to 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 subtype.  If we
+                    can prove that the low bound minus one can't overflow,
+                    we can do this as MAX (hb, lb - 1).  Otherwise, we have
+                    to use the expression hb >= lb ? hb : lb - 1.  */
+                 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
+
+                 /* If gnu_high is now an integer which overflowed, the array
+                    cannot be superflat.  */
+                 if (TREE_CODE (gnu_high) == INTEGER_CST
+                     && TREE_OVERFLOW (gnu_high))
+                   gnu_high = gnu_max;
+
+                 /* gnu_high cannot overflow if the subtype is unsigned since
+                    sizetype is signed, or if it is now a constant that hasn't
+                    overflowed.  */
+                 else if (TYPE_UNSIGNED (gnu_base_subtype)
+                          || 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_max, gnu_min),
+                                        gnu_max, gnu_high);
+               }
 
              gnu_index_type[index]
                = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
@@ -2285,7 +2316,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
            {
              gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
-             for (index = array_dim - 1; index >= 0; index--)
+             for (index = dim - 1; index >= 0; index--)
                gnu_type = TREE_TYPE (gnu_type);
 
              /* One of the above calls might have caused us to be elaborated,
@@ -2298,6 +2329,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));
 
              /* One of the above calls might have caused us to be elaborated,
@@ -2315,11 +2348,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  && !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. */
+                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,
@@ -2336,13 +2370,31 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
              if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
                {
-                 tree orig_gnu_type;
+                 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);
-                 orig_gnu_type = gnu_type;
+                 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.  */
@@ -2363,7 +2415,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                     convert (bitsizetype, gnu_max_size),
                                     TYPE_SIZE (gnu_type));
 
-         for (index = array_dim - 1; index >= 0; index --)
+         for (index = dim - 1; index >= 0; index --)
            {
              gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
              TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
@@ -2371,10 +2423,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
            }
 
+         /* Attach the TYPE_STUB_DECL in case we have a parallel type.  */
+         TYPE_STUB_DECL (gnu_type)
+           = create_type_stub_decl (gnu_entity_name, gnu_type);
+
          /* If we are at file level and this is a multi-dimensional array, we
             need to make a variable corresponding to the stride of the
             inner dimensions.   */
-         if (global_bindings_p () && array_dim > 1)
+         if (global_bindings_p () && dim > 1)
            {
              tree gnu_str_name = get_identifier ("ST");
              tree gnu_arr_type;
@@ -2382,14 +2438,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              for (gnu_arr_type = TREE_TYPE (gnu_type);
                   TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
                   gnu_arr_type = TREE_TYPE (gnu_arr_type),
-                  gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
+                  gnu_str_name = concat_name (gnu_str_name, "ST"))
                {
                  tree eltype = TREE_TYPE (gnu_arr_type);
 
                  TYPE_SIZE (gnu_arr_type)
-                   = elaborate_expression_1 (gnat_entity, gnat_entity,
-                                             TYPE_SIZE (gnu_arr_type),
-                                             gnu_str_name, definition, 0);
+                   = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
+                                             gnat_entity, gnu_str_name,
+                                             definition, false);
 
                  /* ??? For now, store the size as a multiple of the
                     alignment of the element type in bytes so that we
@@ -2398,13 +2454,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    = build_binary_op
                      (MULT_EXPR, sizetype,
                       elaborate_expression_1
-                      (gnat_entity, gnat_entity,
-                       build_binary_op (EXACT_DIV_EXPR, sizetype,
+                      (build_binary_op (EXACT_DIV_EXPR, sizetype,
                                         TYPE_SIZE_UNIT (gnu_arr_type),
                                         size_int (TYPE_ALIGN (eltype)
                                                   / BITS_PER_UNIT)),
-                       concat_id_with_name (gnu_str_name, "A_U"),
-                       definition, 0),
+                       gnat_entity, concat_name (gnu_str_name, "A_U"),
+                       definition, false),
                       size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
 
                  /* ??? create_type_decl is not invoked on the inner types so
@@ -2413,43 +2468,51 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                }
            }
 
-         /* If we need to write out a record type giving the names of
-            the bounds, do it now.  */
-         if (need_index_type_struct && debug_info_p)
+         /* If we need to write out a record type giving the names of the
+            bounds for debugging purposes, do it now and make the record
+            type a parallel type.  This is not needed for a packed array
+            since the bounds are conveyed by the original array type.  */
+         if (need_index_type_struct
+             && debug_info_p
+             && !Is_Packed_Array_Type (gnat_entity))
            {
-             tree gnu_bound_rec_type = make_node (RECORD_TYPE);
+             tree gnu_bound_rec = make_node (RECORD_TYPE);
              tree gnu_field_list = NULL_TREE;
              tree gnu_field;
 
-             TYPE_NAME (gnu_bound_rec_type)
+             TYPE_NAME (gnu_bound_rec)
                = create_concat_name (gnat_entity, "XA");
 
-             for (index = array_dim - 1; index >= 0; index--)
+             for (index = dim - 1; index >= 0; index--)
                {
-                 tree gnu_type_name
-                   = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
+                 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_type[index]);
+                 tree gnu_index_name = TYPE_NAME (gnu_index);
 
-                 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
-                   gnu_type_name = DECL_NAME (gnu_type_name);
+                 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
+                   gnu_index_name = DECL_NAME (gnu_index_name);
 
-                 gnu_field = create_field_decl (gnu_type_name,
-                                                integer_type_node,
-                                                gnu_bound_rec_type,
+                 /* Make sure to reference the types themselves, and not just
+                    their names, as the debugger may fall back on them.  */
+                 gnu_field = create_field_decl (gnu_index_name, gnu_index,
+                                                gnu_bound_rec,
                                                 0, NULL_TREE, NULL_TREE, 0);
                  TREE_CHAIN (gnu_field) = gnu_field_list;
                  gnu_field_list = gnu_field;
                }
 
-             finish_record_type (gnu_bound_rec_type, gnu_field_list,
-                                 0, false);
-
-             TYPE_STUB_DECL (gnu_type)
-               = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
-
-             add_parallel_type
-               (TYPE_STUB_DECL (gnu_type), gnu_bound_rec_type);
+             finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
+             add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
            }
 
+         /* Otherwise, for a packed array, make the original array type a
+            parallel type.  */
+         else if (debug_info_p
+                  && Is_Packed_Array_Type (gnat_entity)
+                  && 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)));
+
          TYPE_CONVENTION_FORTRAN_P (gnu_type)
            = (Convention (gnat_entity) == Convention_Fortran);
          TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
@@ -2474,29 +2537,33 @@ 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.  */
-         copy_alias_set (gnu_type, gnu_base_type);
+         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.   */
-
+        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  */
+            debugging information for it.  */
          gnu_type
            = build_qualified_type (gnu_type,
                                    (TYPE_QUALS (gnu_type)
                                     | (TYPE_QUAL_VOLATILE
                                        * Treat_As_Volatile (gnat_entity))));
-         gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
-                                      !Comes_From_Source (gnat_entity),
-                                      debug_info_p, gnat_entity);
-         if (!Comes_From_Source (gnat_entity))
-           DECL_ARTIFICIAL (gnu_decl) = 1;
+
+         /* 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.  */
@@ -2514,9 +2581,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                     || TYPE_IS_PADDING_P (gnu_inner_type)))
            gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
 
-         /* We need to point the type we just made to our index type so
-            the actual bounds can be put into a template.  */
-
+         /* 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
@@ -2524,32 +2590,34 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
                {
-                 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
-                    If it is, we need to make another type.  */
+                 /* 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))
                    {
-                     tree gnu_subtype;
-
-                     gnu_subtype = make_node (INTEGER_TYPE);
-
+                     tree gnu_subtype
+                       = make_unsigned_type (TYPE_PRECISION (gnu_inner_type));
                      TREE_TYPE (gnu_subtype) = gnu_inner_type;
-                     TYPE_MIN_VALUE (gnu_subtype)
-                       = TYPE_MIN_VALUE (gnu_inner_type);
-                     TYPE_MAX_VALUE (gnu_subtype)
-                       = TYPE_MAX_VALUE (gnu_inner_type);
-                     TYPE_PRECISION (gnu_subtype)
-                       = TYPE_PRECISION (gnu_inner_type);
-                     TYPE_UNSIGNED (gnu_subtype)
-                       = TYPE_UNSIGNED (gnu_inner_type);
                      TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
-                     layout_type (gnu_subtype);
-
+                     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;
+
+#ifdef ENABLE_CHECKING
+                 /* Check for other cases of overloading.  */
+                 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
+#endif
                }
 
+             /* ??? This is necessary to make sure that the container is
+                allocated with a null tree upfront; otherwise, it could
+                be allocated with an uninitialized tree that is accessed
+                before being set below.  See ada-tree.h for details.  */
              SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
 
              for (gnat_index = First_Index (gnat_entity);
@@ -2571,14 +2639,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            }
        }
 
-      /* Abort if packed array with no packed array type field set. */
+      /* Abort if packed array with no packed array type field set.  */
       else
        gcc_assert (!Is_Packed (gnat_entity));
 
       break;
 
     case E_String_Literal_Subtype:
-      /* Create the type for a string literal. */
+      /* Create the type for a string literal.  */
       {
        Entity_Id gnat_full_type
          = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
@@ -2599,20 +2667,20 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          = build_binary_op (PLUS_EXPR, gnu_string_index_type,
                             gnu_lower_bound,
                             convert (gnu_string_index_type, gnu_length));
-       tree gnu_range_type
-         = build_range_type (gnu_string_index_type,
-                             gnu_lower_bound, gnu_upper_bound);
        tree gnu_index_type
-         = create_index_type (convert (sizetype,
-                                       TYPE_MIN_VALUE (gnu_range_type)),
-                              convert (sizetype,
-                                       TYPE_MAX_VALUE (gnu_range_type)),
-                              gnu_range_type, gnat_entity);
+         = create_index_type (convert (sizetype, gnu_lower_bound),
+                              convert (sizetype, gnu_upper_bound),
+                              create_range_type (gnu_string_index_type,
+                                                 gnu_lower_bound,
+                                                 gnu_upper_bound),
+                              gnat_entity);
 
        gnu_type
          = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
                              gnu_index_type);
-       copy_alias_set (gnu_type,  gnu_string_type);
+       if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
+         TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
+       relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
       }
       break;
 
@@ -2637,7 +2705,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        Processing of a record type definition comprises starting the list of
        field declarations here from the discriminants and the calling the
        function components_to_record to add the rest of the fields from the
-       component list and return the gnu type node. The function
+       component list and return the gnu type node.  The function
        components_to_record will call itself recursively as it traverses
        the tree.  */
 
@@ -2660,9 +2728,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        Node_Id full_definition = Declaration_Node (gnat_entity);
        Node_Id record_definition = Type_Definition (full_definition);
        Entity_Id gnat_field;
-       tree gnu_field;
-       tree gnu_field_list = NULL_TREE;
-       tree gnu_get_parent;
+       tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
        /* Set PACKED in keeping with gnat_to_gnu_field.  */
        int packed
          = Is_Packed (gnat_entity)
@@ -2674,21 +2740,27 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                     && Known_Static_Esize (gnat_entity)))
                ? -2
                : 0;
+       bool has_discr = Has_Discriminants (gnat_entity);
        bool has_rep = Has_Specified_Layout (gnat_entity);
        bool all_rep = has_rep;
        bool is_extension
          = (Is_Tagged_Type (gnat_entity)
             && Nkind (record_definition) == N_Derived_Type_Definition);
+       bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
 
        /* See if all fields have a rep clause.  Stop when we find one
           that doesn't.  */
-       for (gnat_field = First_Entity (gnat_entity);
-            Present (gnat_field) && all_rep;
-            gnat_field = Next_Entity (gnat_field))
-         if ((Ekind (gnat_field) == E_Component
-              || Ekind (gnat_field) == E_Discriminant)
-             && No (Component_Clause (gnat_field)))
-           all_rep = false;
+       if (all_rep)
+         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)
+               && No (Component_Clause (gnat_field)))
+             {
+               all_rep = false;
+               break;
+             }
 
        /* If this is a record extension, go a level further to find the
           record definition.  Also, verify we have a Parent_Subtype.  */
@@ -2705,11 +2777,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* Make a node for the record.  If we are not defining the record,
           suppress expanding incomplete types.  */
        gnu_type = make_node (tree_code_for_record_type (gnat_entity));
-       TYPE_NAME (gnu_type) = gnu_entity_id;
+       TYPE_NAME (gnu_type) = gnu_entity_name;
        TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
 
        if (!definition)
-         defer_incomplete_level++, this_deferred = true;
+         {
+           defer_incomplete_level++;
+           this_deferred = true;
+         }
 
        /* If both a size and rep clause was specified, put the size in
           the record type now so that it can get the proper mode.  */
@@ -2759,11 +2834,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
               base type of the parent subtype.  */
            gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
                                     build0 (PLACEHOLDER_EXPR, gnu_type),
-                                    build_decl (FIELD_DECL, NULL_TREE,
+                                    build_decl (input_location,
+                                                FIELD_DECL, NULL_TREE,
                                                 void_type_node),
                                     NULL_TREE);
 
-           if (Has_Discriminants (gnat_entity))
+           if (has_discr)
              for (gnat_field = First_Stored_Discriminant (gnat_entity);
                   Present (gnat_field);
                   gnat_field = Next_Stored_Discriminant (gnat_field))
@@ -2778,55 +2854,106 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                             NULL_TREE),
                     true);
 
-           /* Then we build the parent subtype.  */
-           gnu_parent = gnat_to_gnu_type (gnat_parent);
+           /* Then we build the parent subtype.  If it has discriminants but
+              the type itself has unknown discriminants, this means that it
+              doesn't contain information about how the discriminants are
+              derived from those of the ancestor type, so it cannot be used
+              directly.  Instead it is built by cloning the parent subtype
+              of the underlying record view of the type, for which the above
+              derivation of discriminants has been made explicit.  */
+           if (Has_Discriminants (gnat_parent)
+               && Has_Unknown_Discriminants (gnat_entity))
+             {
+               Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
+
+               /* If we are defining the type, the underlying record
+                  view must already have been elaborated at this point.
+                  Otherwise do it now as its parent subtype cannot be
+                  technically elaborated on its own.  */
+               if (definition)
+                 gcc_assert (present_gnu_tree (gnat_uview));
+               else
+                 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
+
+               gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
+
+               /* Substitute the "get to the parent" of the type for that
+                  of its underlying record view in the cloned type.  */
+               for (gnat_field = First_Stored_Discriminant (gnat_uview);
+                    Present (gnat_field);
+                    gnat_field = Next_Stored_Discriminant (gnat_field))
+                 if (Present (Corresponding_Discriminant (gnat_field)))
+                   {
+                     gnu_field = gnat_to_gnu_field_decl (gnat_field);
+                     tree gnu_ref
+                       = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
+                                 gnu_get_parent, gnu_field, NULL_TREE);
+                     gnu_parent
+                       = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
+                   }
+             }
+           else
+             gnu_parent = gnat_to_gnu_type (gnat_parent);
 
            /* Finally we fix up both kinds of twisted COMPONENT_REF we have
               initially built.  The discriminants must reference the fields
               of the parent subtype and not those of its base type for the
               placeholder machinery to properly work.  */
-           if (Has_Discriminants (gnat_entity))
-             for (gnat_field = First_Stored_Discriminant (gnat_entity);
-                  Present (gnat_field);
-                  gnat_field = Next_Stored_Discriminant (gnat_field))
-               if (Present (Corresponding_Discriminant (gnat_field)))
+           if (has_discr)
+             {
+               /* The actual parent subtype is the full view.  */
+               if (IN (Ekind (gnat_parent), Private_Kind))
                  {
-                   Entity_Id field = Empty;
-                   for (field = First_Stored_Discriminant (gnat_parent);
-                        Present (field);
-                        field = Next_Stored_Discriminant (field))
-                     if (same_discriminant_p (gnat_field, field))
-                       break;
-                   gcc_assert (Present (field));
-                   TREE_OPERAND (get_gnu_tree (gnat_field), 1)
-                     = gnat_to_gnu_field_decl (field);
+                   if (Present (Full_View (gnat_parent)))
+                     gnat_parent = Full_View (gnat_parent);
+                   else
+                     gnat_parent = Underlying_Full_View (gnat_parent);
                  }
 
+               for (gnat_field = First_Stored_Discriminant (gnat_entity);
+                    Present (gnat_field);
+                    gnat_field = Next_Stored_Discriminant (gnat_field))
+                 if (Present (Corresponding_Discriminant (gnat_field)))
+                   {
+                     Entity_Id field = Empty;
+                     for (field = First_Stored_Discriminant (gnat_parent);
+                          Present (field);
+                          field = Next_Stored_Discriminant (field))
+                       if (same_discriminant_p (gnat_field, field))
+                         break;
+                     gcc_assert (Present (field));
+                     TREE_OPERAND (get_gnu_tree (gnat_field), 1)
+                       = gnat_to_gnu_field_decl (field);
+                   }
+             }
+
            /* The "get to the parent" COMPONENT_REF must be given its
               proper type...  */
            TREE_TYPE (gnu_get_parent) = gnu_parent;
 
-           /* ...and reference the _parent field of this record.  */
-           gnu_field_list
+           /* ...and reference the _Parent field of this record.  */
+           gnu_field
              = create_field_decl (get_identifier
                                   (Get_Name_String (Name_uParent)),
                                   gnu_parent, gnu_type, 0,
-                                  has_rep ? TYPE_SIZE (gnu_parent) : 0,
-                                  has_rep ? bitsize_zero_node : 0, 1);
-           DECL_INTERNAL_P (gnu_field_list) = 1;
-           TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
+                                  has_rep
+                                  ? TYPE_SIZE (gnu_parent) : NULL_TREE,
+                                  has_rep
+                                  ? bitsize_zero_node : NULL_TREE, 1);
+           DECL_INTERNAL_P (gnu_field) = 1;
+           TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
+           TYPE_FIELDS (gnu_type) = gnu_field;
          }
 
        /* Make the fields for the discriminants and put them into the record
           unless it's an Unchecked_Union.  */
-       if (Has_Discriminants (gnat_entity))
+       if (has_discr)
          for (gnat_field = First_Stored_Discriminant (gnat_entity);
               Present (gnat_field);
               gnat_field = Next_Stored_Discriminant (gnat_field))
            {
-             /* If this is a record extension and this discriminant
-                is the renaming of another discriminant, we've already
-                handled the discriminant above.  */
+             /* If this is a record extension and this discriminant is the
+                renaming of another discriminant, we've handled it above.  */
              if (Present (Parent_Subtype (gnat_entity))
                  && Present (Corresponding_Discriminant (gnat_field)))
                continue;
@@ -2836,61 +2963,45 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
              /* Make an expression using a PLACEHOLDER_EXPR from the
                 FIELD_DECL node just created and link that with the
-                corresponding GNAT defining identifier.  Then add to the
-                list of fields.  */
+                corresponding GNAT defining identifier.  */
              save_gnu_tree (gnat_field,
                             build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
-                                    build0 (PLACEHOLDER_EXPR,
-                                            DECL_CONTEXT (gnu_field)),
+                                    build0 (PLACEHOLDER_EXPR, gnu_type),
                                     gnu_field, NULL_TREE),
                             true);
 
-             if (!Is_Unchecked_Union (gnat_entity))
+             if (!is_unchecked_union)
                {
                  TREE_CHAIN (gnu_field) = gnu_field_list;
                  gnu_field_list = gnu_field;
                }
            }
 
-       /* Put the discriminants into the record (backwards), so we can
-          know the appropriate discriminant to use for the names of the
-          variants.  */
-       TYPE_FIELDS (gnu_type) = gnu_field_list;
-
-       /* Add the listed fields into the record and finish it up.  */
+       /* 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 (gnat_entity));
-
-       /* We used to remove the associations of the discriminants and
-          _Parent for validity checking, but we may need them if there's
-          Freeze_Node for a subtype used in this record.  */
-       TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
-       TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
+                             false, all_rep, false, is_unchecked_union);
 
-       /* If it is a tagged record force the type to BLKmode to insure
-          that these objects will always be placed in memory. Do the
-          same thing for limited record types. */
+       /* 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))
-         TYPE_MODE (gnu_type) = BLKmode;
+         SET_TYPE_MODE (gnu_type, BLKmode);
 
-       /* If this is a derived type, we must make the alias set of this type
-          the same as that of the type we are derived from.  We assume here
-          that the other type is already frozen. */
-       if (Etype (gnat_entity) != gnat_entity
-           && !(Is_Private_Type (Etype (gnat_entity))
-                && Full_View (Etype (gnat_entity)) == gnat_entity))
-         copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
+       /* We used to remove the associations of the discriminants and _Parent
+          for validity checking but we may need them if there's a Freeze_Node
+          for a subtype used in this record.  */
+       TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
 
        /* Fill in locations of fields.  */
        annotate_rep (gnat_entity, gnu_type);
 
-       /* If there are any entities in the chain corresponding to
-          components that we did not elaborate, ensure we elaborate their
-          types if they are Itypes.  */
+       /* If there are any entities in the chain corresponding to components
+          that we did not elaborate, ensure we elaborate their types if they
+          are Itypes.  */
        for (gnat_temp = First_Entity (gnat_entity);
-            Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
+            Present (gnat_temp);
+            gnat_temp = Next_Entity (gnat_temp))
          if ((Ekind (gnat_temp) == E_Component
               || Ekind (gnat_temp) == E_Discriminant)
              && Is_Itype (Etype (gnat_temp))
@@ -2913,7 +3024,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       /* ... fall through ... */
 
     case E_Record_Subtype:
-
       /* If Cloned_Subtype is Present it means this record subtype has
         identical layout to that type or subtype and we should use
         that GCC type for this one.  The front end guarantees that
@@ -2923,72 +3033,58 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
                                         NULL_TREE, 0);
          maybe_present = true;
+         break;
        }
 
       /* Otherwise, first ensure the base type is elaborated.  Then, if we are
-        changing the type, make a new type with each field having the
-        type of the field in the new subtype but having the position
-        computed by transforming every discriminant reference according
-        to the constraints.  We don't see any difference between
-        private and nonprivate type here since derivations from types should
-        have been deferred until the completion of the private type.  */
+        changing the type, make a new type with each field having the type of
+        the field in the new subtype but the position computed by transforming
+        every discriminant reference according to the constraints.  We don't
+        see any difference between private and non-private type here since
+        derivations from types should have been deferred until the completion
+        of the private type.  */
       else
        {
          Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
          tree gnu_base_type;
-         tree gnu_orig_type;
 
          if (!definition)
-           defer_incomplete_level++, this_deferred = true;
+           {
+             defer_incomplete_level++;
+             this_deferred = true;
+           }
 
-         /* Get the base type initially for its alignment and sizes.  But
-            if it is a padded type, we do all the other work with the
-            unpadded type.  */
          gnu_base_type = gnat_to_gnu_type (gnat_base_type);
 
-         if (TREE_CODE (gnu_base_type) == RECORD_TYPE
-             && TYPE_IS_PADDING_P (gnu_base_type))
-           gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
-         else
-           gnu_type = gnu_orig_type = gnu_base_type;
-
          if (present_gnu_tree (gnat_entity))
            {
              maybe_present = true;
              break;
            }
 
-         /* When the type has discriminants, and these discriminants
-            affect the shape of what it built, factor them in.
-
-            If we are making a subtype of an Unchecked_Union (must be an
-            Itype), just return the type.
-
-            We can't just use Is_Constrained because private subtypes without
-            discriminants of full types with discriminants with default
-            expressions are Is_Constrained but aren't constrained!  */
+         /* 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.
 
+            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!  */
          if (IN (Ekind (gnat_base_type), Record_Kind)
-             && !Is_For_Access_Subtype (gnat_entity)
              && !Is_Unchecked_Union (gnat_base_type)
+             && !Is_For_Access_Subtype (gnat_entity)
              && Is_Constrained (gnat_entity)
-             && Stored_Constraint (gnat_entity) != No_Elist
-             && Present (Discriminant_Constraint (gnat_entity)))
+             && Has_Discriminants (gnat_entity)
+             && Present (Discriminant_Constraint (gnat_entity))
+             && Stored_Constraint (gnat_entity) != No_Elist)
            {
-             Entity_Id gnat_field;
-             tree gnu_field_list = 0;
-             tree gnu_pos_list
-               = compute_field_positions (gnu_orig_type, NULL_TREE,
-                                          size_zero_node, bitsize_zero_node,
-                                          BIGGEST_ALIGNMENT);
              tree gnu_subst_list
-               = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
-                                    definition);
-             tree gnu_temp;
+               = build_subst_list (gnat_entity, gnat_base_type, definition);
+             tree gnu_pos_list, gnu_field_list = NULL_TREE;
+             tree gnu_unpad_base_type, t;
+             Entity_Id gnat_field;
 
              gnu_type = make_node (RECORD_TYPE);
-             TYPE_NAME (gnu_type) = gnu_entity_id;
-             TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
+             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.
@@ -2998,66 +3094,80 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              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);
-             copy_alias_set (gnu_type, gnu_base_type);
+             relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
 
              if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
-               for (gnu_temp = gnu_subst_list;
-                    gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
+               for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
                  TYPE_SIZE (gnu_type)
                    = substitute_in_expr (TYPE_SIZE (gnu_type),
-                                         TREE_PURPOSE (gnu_temp),
-                                         TREE_VALUE (gnu_temp));
+                                         TREE_PURPOSE (t),
+                                         TREE_VALUE (t));
 
              if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
-               for (gnu_temp = gnu_subst_list;
-                    gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
+               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 (gnu_temp),
-                                         TREE_VALUE (gnu_temp));
+                                         TREE_PURPOSE (t),
+                                         TREE_VALUE (t));
 
              if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
-               for (gnu_temp = gnu_subst_list;
-                    gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
+               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 (gnu_temp),
-                                                  TREE_VALUE (gnu_temp)));
+                                                  TREE_PURPOSE (t),
+                                                  TREE_VALUE (t)));
+
+             if (TREE_CODE (gnu_base_type) == RECORD_TYPE
+                 && 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;
+
+             gnu_pos_list
+               = compute_field_positions (gnu_unpad_base_type, NULL_TREE,
+                                          size_zero_node, bitsize_zero_node,
+                                          BIGGEST_ALIGNMENT);
 
              for (gnat_field = First_Entity (gnat_entity);
-                  Present (gnat_field); gnat_field = Next_Entity (gnat_field))
+                  Present (gnat_field);
+                  gnat_field = Next_Entity (gnat_field))
                if ((Ekind (gnat_field) == E_Component
                     || Ekind (gnat_field) == E_Discriminant)
-                   && (Underlying_Type (Scope (Original_Record_Component
-                                               (gnat_field)))
-                       == gnat_base_type)
-                   && (No (Corresponding_Discriminant (gnat_field))
-                       || !Is_Tagged_Type (gnat_base_type)))
+                   && !(Present (Corresponding_Discriminant (gnat_field))
+                        && Is_Tagged_Type (gnat_base_type))
+                   && Underlying_Type (Scope (Original_Record_Component
+                                              (gnat_field)))
+                      == gnat_base_type)
                  {
+                   Name_Id gnat_name = Chars (gnat_field);
+                   Entity_Id gnat_old_field
+                     = Original_Record_Component (gnat_field);
                    tree gnu_old_field
-                     = gnat_to_gnu_field_decl (Original_Record_Component
-                                               (gnat_field));
+                     = gnat_to_gnu_field_decl (gnat_old_field);
                    tree gnu_offset
-                     = TREE_VALUE (purpose_member (gnu_old_field,
-                                                   gnu_pos_list));
+                     = 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_type
-                     = gnat_to_gnu_type (Etype (gnat_field));
-                   tree gnu_size = TYPE_SIZE (gnu_field_type);
-                   tree gnu_new_pos = NULL_TREE;
+                   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_field;
+                     = tree_low_cst
+                       (TREE_PURPOSE (TREE_VALUE (gnu_offset)), 1);
+
+                   /* If the type is the same, retrieve the GCC type from the
+                      old field to take into account possible adjustments.  */
+                   if (Etype (gnat_field) == Etype (gnat_old_field))
+                     gnu_field_type = TREE_TYPE (gnu_old_field);
+                   else
+                     gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
 
                    /* If there was a component clause, the field types must be
                       the same for the type and subtype, so copy the data from
                       the old field to avoid recomputation here.  Also if the
                       field is justified modular and the optimization in
                       gnat_to_gnu_field was applied.  */
-                   if (Present (Component_Clause
-                                (Original_Record_Component (gnat_field)))
+                   if (Present (Component_Clause (gnat_old_field))
                        || (TREE_CODE (gnu_field_type) == RECORD_TYPE
                            && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
                            && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
@@ -3077,23 +3187,25 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                == INTEGER_CST)
                      {
                        gnu_size = DECL_SIZE (gnu_old_field);
-                       if (TYPE_MODE (gnu_field_type) == BLKmode
-                           && TREE_CODE (gnu_field_type) == RECORD_TYPE
+                       if (TREE_CODE (gnu_field_type) == RECORD_TYPE
+                           && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
                            && host_integerp (TYPE_SIZE (gnu_field_type), 1))
                          gnu_field_type
                            = make_packable_type (gnu_field_type, true);
                      }
 
+                   else
+                     gnu_size = TYPE_SIZE (gnu_field_type);
+
                    if (CONTAINS_PLACEHOLDER_P (gnu_pos))
-                     for (gnu_temp = gnu_subst_list;
-                          gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
+                     for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
                        gnu_pos = substitute_in_expr (gnu_pos,
-                                                     TREE_PURPOSE (gnu_temp),
-                                                     TREE_VALUE (gnu_temp));
+                                                     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.  */
+                      position of the field when we make it.  Otherwise, we
+                      need to deal with it specially below.  */
                    if (TREE_CONSTANT (gnu_pos))
                      {
                        gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
@@ -3108,6 +3220,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                                 TYPE_SIZE (gnu_type)))
                          continue;
                      }
+                   else
+                     gnu_new_pos = NULL_TREE;
 
                    gnu_field
                      = create_field_decl
@@ -3139,8 +3253,32 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                      = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
                    TREE_THIS_VOLATILE (gnu_field)
                      = TREE_THIS_VOLATILE (gnu_old_field);
-                   TREE_CHAIN (gnu_field) = gnu_field_list;
-                   gnu_field_list = gnu_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)
+                     gnu_field_list = chainon (gnu_field_list, gnu_field);
+
+                   /* Similarly, if this is the _Controller field, put
+                      it before the other fields except for the _Tag or
+                      _Parent field.  */
+                   else if (gnat_name == Name_uController && gnu_last)
+                     {
+                       TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
+                       TREE_CHAIN (gnu_last) = gnu_field;
+                     }
+
+                   /* Otherwise, if this is a regular field, put it after
+                      the other fields.  */
+                   else
+                     {
+                       TREE_CHAIN (gnu_field) = gnu_field_list;
+                       gnu_field_list = gnu_field;
+                       if (!gnu_last)
+                         gnu_last = gnu_field;
+                     }
+
                    save_gnu_tree (gnat_field, gnu_field, false);
                  }
 
@@ -3163,7 +3301,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              TYPE_SIZE_UNIT (gnu_type)
                = variable_size (TYPE_SIZE_UNIT (gnu_type));
 
-             compute_record_mode (gnu_type);
+             /* See the E_Record_Type case for the rationale.  */
+             if (Is_Tagged_Type (gnat_entity)
+                 || Is_Limited_Record (gnat_entity))
+               SET_TYPE_MODE (gnu_type, BLKmode);
+             else
+               compute_record_mode (gnu_type);
+
+             TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
 
              /* Fill in locations of fields.  */
              annotate_rep (gnat_entity, gnu_type);
@@ -3174,16 +3319,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              if (debug_info_p)
                {
                  tree gnu_subtype_marker = make_node (RECORD_TYPE);
-                 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
+                 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
 
-                 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
-                   gnu_orig_name = DECL_NAME (gnu_orig_name);
+                 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
+                   gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
 
                  TYPE_NAME (gnu_subtype_marker)
                    = create_concat_name (gnat_entity, "XVS");
                  finish_record_type (gnu_subtype_marker,
-                                     create_field_decl (gnu_orig_name,
-                                                        integer_type_node,
+                                     create_field_decl (gnu_unpad_base_name,
+                                                        build_reference_type
+                                                        (gnu_unpad_base_type),
                                                         gnu_subtype_marker,
                                                         0, NULL_TREE,
                                                         NULL_TREE, 0),
@@ -3197,17 +3343,23 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              rest_of_record_type_compilation (gnu_type);
            }
 
-         /* Otherwise, go down all the components in the new type and
-            make them equivalent to those in the base type.  */
+         /* Otherwise, go down all the components in the new type and make
+            them equivalent to those in the base type.  */
          else
-           for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
-                gnat_temp = Next_Entity (gnat_temp))
-             if ((Ekind (gnat_temp) == E_Discriminant
-                  && !Is_Unchecked_Union (gnat_base_type))
-                 || Ekind (gnat_temp) == E_Component)
-               save_gnu_tree (gnat_temp,
-                              gnat_to_gnu_field_decl
-                              (Original_Record_Component (gnat_temp)), false);
+           {
+             gnu_type = gnu_base_type;
+
+             for (gnat_temp = First_Entity (gnat_entity);
+                  Present (gnat_temp);
+                  gnat_temp = Next_Entity (gnat_temp))
+               if ((Ekind (gnat_temp) == E_Discriminant
+                    && !Is_Unchecked_Union (gnat_base_type))
+                   || Ekind (gnat_temp) == E_Component)
+                 save_gnu_tree (gnat_temp,
+                                gnat_to_gnu_field_decl
+                                (Original_Record_Component (gnat_temp)),
+                                false);
+           }
        }
       break;
 
@@ -3240,7 +3392,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          gnu_type
            = build_pointer_type
              (make_dummy_type (Directly_Designated_Type (gnat_entity)));
-         gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
+         gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
                                       !Comes_From_Source (gnat_entity),
                                       debug_info_p, gnat_entity);
          this_made_decl = true;
@@ -3293,7 +3445,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           either gnat_desig_full or gnat_desig_equiv.  */
        Entity_Id gnat_desig_rep;
 
-       /* Nonzero if this is a pointer to an unconstrained array.  */
+       /* True if this is a pointer to an unconstrained array.  */
        bool is_unconstrained_array;
 
        /* We want to know if we'll be seeing the freeze node for any
@@ -3303,9 +3455,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
             ? In_Extended_Main_Code_Unit (gnat_desig_full)
             : In_Extended_Main_Code_Unit (gnat_desig_type));
 
-       /* Nonzero if we make a dummy type here.  */
+       /* True if we make a dummy type here.  */
        bool got_fat_p = false;
-       /* Nonzero if the dummy is a fat pointer. */
+       /* True if the dummy is a fat pointer.  */
        bool made_dummy = false;
        tree gnu_desig_type = NULL_TREE;
        enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
@@ -3354,15 +3506,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    && ! present_gnu_tree (gnat_desig_equiv))
                || (in_main_unit && is_from_limited_with
                    && Present (Freeze_Node (gnat_desig_rep)))))
-         {
-           tree gnu_old
-             = (present_gnu_tree (gnat_desig_rep)
-                ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
-                : make_dummy_type (gnat_desig_rep));
-           tree fields;
+         {
+           tree gnu_old;
 
-           /* Show the dummy we get will be a fat pointer.  */
-           got_fat_p = made_dummy = true;
+           if (present_gnu_tree (gnat_desig_rep))
+             gnu_old = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
+           else
+             {
+               gnu_old = make_dummy_type (gnat_desig_rep);
+
+               /* Show the dummy we get will be a fat pointer.  */
+               got_fat_p = made_dummy = true;
+             }
 
            /* If the call above got something that has a pointer, that
               pointer is our type.  This could have happened either
@@ -3375,15 +3530,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                tree gnu_ptr_template = build_pointer_type (gnu_template_type);
                tree gnu_array_type = make_node (ENUMERAL_TYPE);
                tree gnu_ptr_array = build_pointer_type (gnu_array_type);
+               tree fields;
 
                TYPE_NAME (gnu_template_type)
-                 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
-                                        "XUB");
+                 = create_concat_name (gnat_desig_equiv, "XUB");
                TYPE_DUMMY_P (gnu_template_type) = 1;
 
                TYPE_NAME (gnu_array_type)
-                 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
-                                        "XUA");
+                 = create_concat_name (gnat_desig_equiv, "XUA");
                TYPE_DUMMY_P (gnu_array_type) = 1;
 
                gnu_type = make_node (RECORD_TYPE);
@@ -3412,8 +3566,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
                TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
                TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
-                 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
-                                        "XUT");
+                 = create_concat_name (gnat_desig_equiv, "XUT");
                TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
              }
          }
@@ -3436,7 +3589,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    are to defer elaborating incomplete types.  We do this
                    since this access type may be the full view of some
                    private type.  Note that the unconstrained array case is
-                   handled above. */
+                   handled above.  */
                 || ((! in_main_unit || imported_p)
                     && defer_incomplete_level != 0
                     && ! present_gnu_tree (gnat_desig_equiv)
@@ -3450,7 +3603,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    when the declaration is processed.  In both cases, the
                    pointer eventually created below will be automatically
                    adjusted when the Freeze_Node is processed.  Note that the
-                   unconstrained array case is handled above. */
+                   unconstrained array case is handled above.  */
                 ||  (in_main_unit && is_from_limited_with
                      && Present (Freeze_Node (gnat_desig_rep))))
          {
@@ -3499,7 +3652,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                     TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
 
                /* Some extra processing is required if we are building a
-                  pointer to an incomplete type (in the GCC sense). We might
+                  pointer to an incomplete type (in the GCC sense).  We might
                   have such a type if we just made a dummy, or directly out
                   of the call to gnat_to_gnu_type above if we are processing
                   an access type for a record component designating the
@@ -3508,16 +3661,16 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  {
                    /* We must ensure that the pointer to variant we make will
                       be processed by update_pointer_to when the initial type
-                      is completed. Pretend we made a dummy and let further
+                      is completed.  Pretend we made a dummy and let further
                       processing act as usual.  */
                    made_dummy = true;
 
                    /* We must ensure that update_pointer_to will not retrieve
                       the dummy variant when building a properly qualified
-                      version of the complete type. We take advantage of the
+                      version of the complete type.  We take advantage of the
                       fact that get_qualified_type is requiring TYPE_NAMEs to
                       match to influence build_qualified_type and then also
-                      update_pointer_to here. */
+                      update_pointer_to here.  */
                    TYPE_NAME (gnu_desig_type)
                      = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
                  }
@@ -3549,7 +3702,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  (TYPE_OBJECT_RECORD_TYPE
                   (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
 
-           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
+           gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
                                         !Comes_From_Source (gnat_entity),
                                         debug_info_p, gnat_entity);
            this_made_decl = true;
@@ -3574,7 +3727,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                 node as the one we got.
 
                 Besides, variants of this non-dummy type might have been
-                created along the way. update_pointer_to is expected to
+                created along the way.  update_pointer_to is expected to
                 properly take care of those situations.  */
            else
              {
@@ -3602,9 +3755,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        gnu_type = ptr_void_type_node;
       else
        {
-         /* The runtime representation is the equivalent type. */
+         /* The runtime representation is the equivalent type.  */
          gnu_type = gnat_to_gnu_type (gnat_equiv_type);
-         maybe_present = 1;
+         maybe_present = true;
        }
 
       if (Is_Itype (Directly_Designated_Type (gnat_entity))
@@ -3622,12 +3775,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
         meaningful only to the front end.
 
         The designated type must be elaborated as well, if it does
-        not have its own freeze node. Designated (sub)types created
+        not have its own freeze node.  Designated (sub)types created
         for constrained components of records with discriminants are
         not frozen by the front end and thus not elaborated by gigi,
         because their use may appear before the base type is frozen,
         and because it is not clear that they are needed anywhere in
-        Gigi. With the current model, there is no correct place where
+        Gigi.  With the current model, there is no correct place where
         they could be elaborated.  */
 
       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
@@ -3669,7 +3822,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
                First_Formal    The first formal parameter.
                Is_Imported     Indicates that the subprogram has appeared in
-                               an INTERFACE or IMPORT pragma. For now we
+                               an INTERFACE or IMPORT pragma.  For now we
                                assume that the external language is C.
                Is_Exported     Likewise but for an EXPORT pragma.
                Is_Inlined      True if the subprogram is to be inlined.
@@ -3683,7 +3836,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        are copied in, if they are Ada In Out or Out parameters, their return
        value becomes part of a record which becomes the return type of the
        function (C function - note that this applies only to Ada procedures
-       so there is no Ada return type). Additional code to store back the
+       so there is no Ada return type).  Additional code to store back the
        parameters will be generated on the caller side.  This transformation
        is done here, not in the front-end.
 
@@ -3716,7 +3869,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        tree gnu_param_list = NULL_TREE;
        /* Likewise for the stub associated with an exported procedure.  */
        tree gnu_stub_param_list = NULL_TREE;
-       /* The type returned by a function. If the subprogram is a procedure
+       /* The type returned by a function.  If the subprogram is a procedure
           this type should be void_type_node.  */
        tree gnu_return_type = void_type_node;
        /* List of fields in return type of procedure with copy-in copy-out
@@ -3763,10 +3916,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        bool has_stub = false;
        int parmnum;
 
+       /* A parameter may refer to this type, so defer completion of any
+          incomplete types.  */
        if (kind == E_Subprogram_Type && !definition)
-         /* A parameter may refer to this type, so defer completion
-            of any incomplete types.  */
-         defer_incomplete_level++, this_deferred = true;
+         {
+           defer_incomplete_level++;
+           this_deferred = true;
+         }
 
        /* If the subprogram has an alias, it is probably inherited, so
           we can use the original one.  If the original "subprogram"
@@ -3802,7 +3958,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* ??? What if we don't find the builtin node above ? warn ? err ?
           In the current state we neither warn nor err, and calls will just
-          be handled as for regular subprograms. */
+          be handled as for regular subprograms.  */
 
        if (kind == E_Function || kind == E_Subprogram_Type)
          gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
@@ -4071,7 +4227,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* A subprogram (something that doesn't return anything) shouldn't
           be considered const since there would be no reason for such a
           subprogram.  Note that procedures with Out (or In Out) parameters
-          have already been converted into a function with a return type. */
+          have already been converted into a function with a return type.  */
        if (TREE_CODE (gnu_return_type) == VOID_TYPE)
          const_flag = false;
 
@@ -4110,7 +4266,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* If there was no specified Interface_Name and the external and
           internal names of the subprogram are the same, only use the
           internal name to allow disambiguation of nested subprograms.  */
-       if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
+       if (No (Interface_Name (gnat_entity))
+           && gnu_ext_name == gnu_entity_name)
          gnu_ext_name = NULL_TREE;
 
        /* If we are defining the subprogram and it has an Address clause
@@ -4140,14 +4297,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              gnu_address = convert (gnu_type, gnu_address);
 
            gnu_decl
-             = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
+             = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
                                 gnu_address, false, Is_Public (gnat_entity),
                                 extern_flag, false, NULL, gnat_entity);
            DECL_BY_REF_P (gnu_decl) = 1;
          }
 
        else if (kind == E_Subprogram_Type)
-         gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
+         gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
                                       !Comes_From_Source (gnat_entity),
                                       debug_info_p, gnat_entity);
        else
@@ -4159,7 +4316,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                public_flag = false;
              }
 
-           gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
+           gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
                                            gnu_type, gnu_param_list,
                                            inline_flag, public_flag,
                                            extern_flag, attr_list,
@@ -4167,7 +4324,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            if (has_stub)
              {
                tree gnu_stub_decl
-                 = create_subprog_decl (gnu_entity_id, gnu_stub_name,
+                 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
                                         gnu_stub_type, gnu_stub_param_list,
                                         inline_flag, true,
                                         extern_flag, attr_list,
@@ -4210,7 +4367,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (No (full_view))
          {
            if (kind == E_Incomplete_Type)
-             gnu_type = make_dummy_type (gnat_entity);
+             {
+               gnu_type = make_dummy_type (gnat_entity);
+               gnu_decl = TYPE_STUB_DECL (gnu_type);
+             }
            else
              {
                gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
@@ -4242,14 +4402,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          }
 
        /* For incomplete types, make a dummy type entry which will be
-          replaced later.  */
+          replaced later.  Save it as the full declaration's type so
+          we can do any needed updates when we see it.  */
        gnu_type = make_dummy_type (gnat_entity);
-
-       /* Save this type as the full declaration's type so we can do any
-          needed updates when we see it.  */
-       gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
-                                    !Comes_From_Source (gnat_entity),
-                                    debug_info_p, gnat_entity);
+       gnu_decl = TYPE_STUB_DECL (gnu_type);
        save_gnu_tree (full_view, gnu_decl, 0);
        break;
       }
@@ -4274,7 +4430,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       break;
 
     case E_Label:
-      gnu_decl = create_label_decl (gnu_entity_id);
+      gnu_decl = create_label_decl (gnu_entity_name);
       break;
 
     case E_Block:
@@ -4300,8 +4456,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   /* If we are processing a type and there is either no decl for it or
      we just made one, do some common processing for the type, such as
      handling alignment and possible padding.  */
-
-  if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
+  if (is_type && (!gnu_decl || this_made_decl))
     {
       if (Is_Tagged_Type (gnat_entity)
          || Is_Class_Wide_Equivalent_Type (gnat_entity))
@@ -4389,9 +4544,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       if (TREE_CODE (gnu_type) == RECORD_TYPE
          && TYPE_IS_PADDING_P (gnu_type))
        {
-         gnu_entity_id = TYPE_NAME (gnu_type);
-         if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
-           gnu_entity_id = DECL_NAME (gnu_entity_id);
+         gnu_entity_name = TYPE_NAME (gnu_type);
+         if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
+           gnu_entity_name = DECL_NAME (gnu_entity_name);
        }
 
       set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
@@ -4411,19 +4566,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                  TYPE_SIZE (gnu_type), 0))
            {
              TYPE_SIZE (gnu_type)
-               = elaborate_expression_1 (gnat_entity, gnat_entity,
-                                         TYPE_SIZE (gnu_type),
-                                         get_identifier ("SIZE"),
-                                         definition, 0);
+               = elaborate_expression_1 (TYPE_SIZE (gnu_type),
+                                         gnat_entity, get_identifier ("SIZE"),
+                                         definition, false);
              SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
            }
          else
            {
              TYPE_SIZE (gnu_type)
-               = elaborate_expression_1 (gnat_entity, gnat_entity,
-                                         TYPE_SIZE (gnu_type),
-                                         get_identifier ("SIZE"),
-                                         definition, 0);
+               = elaborate_expression_1 (TYPE_SIZE (gnu_type),
+                                         gnat_entity, get_identifier ("SIZE"),
+                                         definition, false);
 
              /* ??? For now, store the size as a multiple of the alignment
                 in bytes so that we can see the alignment from the tree.  */
@@ -4431,23 +4584,21 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                = build_binary_op
                  (MULT_EXPR, sizetype,
                   elaborate_expression_1
-                  (gnat_entity, gnat_entity,
-                   build_binary_op (EXACT_DIV_EXPR, sizetype,
+                  (build_binary_op (EXACT_DIV_EXPR, sizetype,
                                     TYPE_SIZE_UNIT (gnu_type),
                                     size_int (TYPE_ALIGN (gnu_type)
                                               / BITS_PER_UNIT)),
-                   get_identifier ("SIZE_A_UNIT"),
-                   definition, 0),
+                   gnat_entity, get_identifier ("SIZE_A_UNIT"),
+                   definition, false),
                   size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
 
              if (TREE_CODE (gnu_type) == RECORD_TYPE)
                SET_TYPE_ADA_SIZE
                  (gnu_type,
-                  elaborate_expression_1 (gnat_entity,
+                  elaborate_expression_1 (TYPE_ADA_SIZE (gnu_type),
                                           gnat_entity,
-                                          TYPE_ADA_SIZE (gnu_type),
                                           get_identifier ("RM_SIZE"),
-                                          definition, 0));
+                                          definition, false));
                 }
        }
 
@@ -4473,13 +4624,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  = build_binary_op
                    (MULT_EXPR, sizetype,
                     elaborate_expression_1
-                    (gnat_temp, gnat_temp,
-                     build_binary_op (EXACT_DIV_EXPR, sizetype,
+                    (build_binary_op (EXACT_DIV_EXPR, sizetype,
                                       DECL_FIELD_OFFSET (gnu_field),
                                       size_int (DECL_OFFSET_ALIGN (gnu_field)
                                                 / BITS_PER_UNIT)),
-                     get_identifier ("OFFSET"),
-                     definition, 0),
+                     gnat_temp, get_identifier ("OFFSET"),
+                     definition, false),
                     size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
 
                /* ??? The context of gnu_field is not necessarily gnu_type so
@@ -4505,22 +4655,86 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
 
       if (!gnu_decl)
-       gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
+       gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
                                     !Comes_From_Source (gnat_entity),
                                     debug_info_p, gnat_entity);
       else
        TREE_TYPE (gnu_decl) = gnu_type;
     }
 
-  if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
+  if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
     {
       gnu_type = TREE_TYPE (gnu_decl);
 
+      /* If this is a derived type, relate its alias set to that of its parent
+        to avoid troubles when a call to an inherited primitive is inlined in
+        a context where a derived object is accessed.  The inlined code works
+        on the parent view so the resulting code may access the same object
+        using both the parent and the derived alias sets, which thus have to
+        conflict.  As the same issue arises with component references, the
+        parent alias set also has to conflict with composite types enclosing
+        derived components.  For instance, if we have:
+
+           type D is new T;
+           type R is record
+              Component : D;
+           end record;
+
+        we want T to conflict with both D and R, in addition to R being a
+        superset of D by record/component construction.
+
+        One way to achieve this is to perform an alias set copy from the
+        parent to the derived type.  This is not quite appropriate, though,
+        as we don't want separate derived types to conflict with each other:
+
+           type I1 is new Integer;
+           type I2 is new Integer;
+
+        We want I1 and I2 to both conflict with Integer but we do not want
+        I1 to conflict with I2, and an alias set copy on derivation would
+        have that effect.
+
+        The option chosen is to make the alias set of the derived type a
+        superset of that of its parent type.  It trivially fulfills the
+        simple requirement for the Integer derivation example above, and
+        the component case as well by superset transitivity:
+
+                  superset      superset
+               R ----------> D ----------> T
+
+        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);
+       }
+
       /* Back-annotate the Alignment of the type if not already in the
         tree.  Likewise for sizes.  */
       if (Unknown_Alignment (gnat_entity))
-       Set_Alignment (gnat_entity,
-                      UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
+       {
+         unsigned int double_align, align;
+         bool is_capped_double, align_clause;
+
+         /* If the default alignment of "double" or larger scalar types is
+            specifically capped and this is not an array with an alignment
+            clause on the component type, return the cap.  */
+         if ((double_align = double_float_alignment) > 0)
+           is_capped_double
+             = is_double_float_or_array (gnat_entity, &align_clause);
+         else if ((double_align = double_scalar_alignment) > 0)
+           is_capped_double
+             = is_double_scalar_or_array (gnat_entity, &align_clause);
+         else
+           is_capped_double = align_clause = false;
+
+         if (is_capped_double && !align_clause)
+           align = double_align;
+         else
+           align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
+
+         Set_Alignment (gnat_entity, UI_From_Int (align));
+       }
 
       if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
        {
@@ -4537,7 +4751,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              /* In this mode the tag and the parent components are not
                 generated by the front-end, so the sizes must be adjusted
-                explicitly now. */
+                explicitly now.  */
              int size_offset, new_size;
 
              if (Is_Derived_Type (gnat_entity))
@@ -4571,20 +4785,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     DECL_IGNORED_P (gnu_decl) = 1;
 
   /* If we haven't already, associate the ..._DECL node that we just made with
-     the input GNAT entity node. */
+     the input GNAT entity node.  */
   if (!saved)
     save_gnu_tree (gnat_entity, gnu_decl, false);
 
-  /* If this is an enumeral or floating-point type, we were not able to set
-     the bounds since they refer to the type.  These bounds are always static.
-
-     For enumeration types, also write debugging information and declare the
-     enumeration literal  table, if needed.  */
-
+  /* If this is an enumeration or floating-point type, we were not able to set
+     the bounds since they refer to the type.  These are always static.  */
   if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
       || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
     {
       tree gnu_scalar_type = gnu_type;
+      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
@@ -4594,20 +4805,29 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
       /* If this is a floating point type and we haven't set a floating
         point type yet, use this in the evaluation of the bounds.  */
       if (!longest_float_type_node && kind == E_Floating_Point_Type)
-       longest_float_type_node = gnu_type;
+       longest_float_type_node = gnu_scalar_type;
 
-      TYPE_MIN_VALUE (gnu_scalar_type)
-       = gnat_to_gnu (Type_Low_Bound (gnat_entity));
-      TYPE_MAX_VALUE (gnu_scalar_type)
-       = gnat_to_gnu (Type_High_Bound (gnat_entity));
+      gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
+      gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
 
-      if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
+      if (kind == E_Enumeration_Type)
        {
-         /* Since this has both a typedef and a tag, avoid outputting
-            the name twice.  */
+         /* Enumeration types have specific RM bounds.  */
+         SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
+         SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
+
+         /* Write full debugging information.  Since this has both a
+            typedef and a tag, avoid outputting the name twice.  */
          DECL_ARTIFICIAL (gnu_decl) = 1;
          rest_of_type_decl_compilation (gnu_decl);
        }
+
+      else
+       {
+         /* Floating-point types don't have specific RM bounds.  */
+         TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
+         TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
+       }
     }
 
   /* If we deferred processing of incomplete types, re-enable it.  If there
@@ -4674,11 +4894,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   if (this_global)
     force_global--;
 
+  /* If this is a packed array type whose original array type is itself
+     an Itype without freeze node, make sure the latter is processed.  */
   if (Is_Packed_Array_Type (gnat_entity)
-      && Is_Itype (Associated_Node_For_Itype (gnat_entity))
-      && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
-      && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
-    gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
+      && Is_Itype (Original_Array_Type (gnat_entity))
+      && No (Freeze_Node (Original_Array_Type (gnat_entity)))
+      && !present_gnu_tree (Original_Array_Type (gnat_entity)))
+    gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
 
   return gnu_decl;
 }
@@ -4697,6 +4919,38 @@ gnat_to_gnu_field_decl (Entity_Id gnat_entity)
   return gnu_field;
 }
 
+/* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type.  Return
+   the GCC type corresponding to that entity.  */
+
+tree
+gnat_to_gnu_type (Entity_Id gnat_entity)
+{
+  tree gnu_decl;
+
+  /* The back end never attempts to annotate generic types.  */
+  if (Is_Generic_Type (gnat_entity) && type_annotate_only)
+     return void_type_node;
+
+  gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
+  gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
+
+  return TREE_TYPE (gnu_decl);
+}
+
+/* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type.  Return
+   the unpadded version of the GCC type corresponding to that entity.  */
+
+tree
+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))
+    type = TREE_TYPE (TYPE_FIELDS (type));
+
+  return type;
+}
+\f
 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
    Every TYPE_DECL generated for a type definition must be passed
    to this function once everything else has been done for it.  */
@@ -4730,10 +4984,7 @@ rest_of_type_decl_compilation_no_defer (tree decl)
        continue;
 
       if (!TYPE_STUB_DECL (t))
-       {
-         TYPE_STUB_DECL (t) = build_decl (TYPE_DECL, DECL_NAME (decl), t);
-         DECL_ARTIFICIAL (TYPE_STUB_DECL (t)) = 1;
-       }
+       TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
 
       rest_of_type_compilation (t, toplev);
     }
@@ -5033,6 +5284,18 @@ array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
 
   return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
 }
+
+/* Return true if GNAT_ADDRESS is a value known at compile-time.  */
+
+static bool
+compile_time_known_address_p (Node_Id gnat_address)
+{
+  /* Catch System'To_Address.  */
+  if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
+    gnat_address = Expression (gnat_address);
+
+  return Compile_Time_Known_Value (gnat_address);
+}
 \f
 /* Given GNAT_ENTITY, elaborate all expressions that are required to
    be elaborated at the point of its definition, but do nothing else.  */
@@ -5052,16 +5315,15 @@ elaborate_entity (Entity_Id gnat_entity)
        Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
        Node_Id gnat_hb = Type_High_Bound (gnat_entity);
 
-       /* ??? Tests for avoiding static constraint error expression
-          is needed until the front stops generating bogus conversions
-          on bounds of real types. */
-
+       /* ??? Tests to avoid Constraint_Error in static expressions
+          are needed until after the front stops generating bogus
+          conversions on bounds of real types.  */
        if (!Raises_Constraint_Error (gnat_lb))
          elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
-                               1, 0, Needs_Debug_Info (gnat_entity));
+                               true, false, Needs_Debug_Info (gnat_entity));
        if (!Raises_Constraint_Error (gnat_hb))
          elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
-                               1, 0, Needs_Debug_Info (gnat_entity));
+                               true, false, Needs_Debug_Info (gnat_entity));
       break;
       }
 
@@ -5082,13 +5344,14 @@ elaborate_entity (Entity_Id gnat_entity)
     case E_Limited_Private_Subtype:
     case E_Record_Subtype_With_Private:
       if (Is_Constrained (gnat_entity)
-         && Has_Discriminants (Base_Type (gnat_entity))
+         && Has_Discriminants (gnat_entity)
          && Present (Discriminant_Constraint (gnat_entity)))
        {
          Node_Id gnat_discriminant_expr;
          Entity_Id gnat_field;
 
-         for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
+         for (gnat_field
+              = First_Discriminant (Implementation_Base_Type (gnat_entity)),
               gnat_discriminant_expr
               = First_Elmt (Discriminant_Constraint (gnat_entity));
               Present (gnat_field);
@@ -5097,8 +5360,8 @@ elaborate_entity (Entity_Id gnat_entity)
            /* ??? For now, ignore access discriminants.  */
            if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
              elaborate_expression (Node (gnat_discriminant_expr),
-                                   gnat_entity,
-                                   get_entity_name (gnat_field), 1, 0, 0);
+                                   gnat_entity, get_entity_name (gnat_field),
+                                   true, false, false);
        }
       break;
 
@@ -5143,11 +5406,16 @@ mark_out_of_scope (Entity_Id gnat_entity)
     }
 }
 \f
-/* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE.  If this
-   is a multi-dimensional array type, do this recursively.  */
+/* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
+   If this is a multi-dimensional array type, do this recursively.
+
+   OP may be
+   - ALIAS_SET_COPY:     the new set is made a copy of the old one.
+   - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
+   - ALIAS_SET_SUBSET:   the new set is made a subset of the old one.  */
 
 static void
-copy_alias_set (tree gnu_new_type, tree gnu_old_type)
+relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
 {
   /* Remove any padding from GNU_OLD_TYPE.  It doesn't matter in the case
      of a one-dimensional array, since the padding has the same alias set
@@ -5158,54 +5426,90 @@ copy_alias_set (tree gnu_new_type, tree gnu_old_type)
             || TYPE_IS_PADDING_P (gnu_old_type)))
     gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
 
-  /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
-     array.  In that case, it doesn't have the same shape as GNU_NEW_TYPE,
-     so we need to go down to what does.  */
+  /* Unconstrained array types are deemed incomplete and would thus be given
+     alias set 0.  Retrieve the underlying array type.  */
   if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
     gnu_old_type
       = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
+  if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
+    gnu_new_type
+      = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
 
   if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
       && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
       && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
-    copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
+    relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
+
+  switch (op)
+    {
+    case ALIAS_SET_COPY:
+      /* The alias set shouldn't be copied between array types with different
+        aliasing settings because this can break the aliasing relationship
+        between the array type and its element type.  */
+#ifndef ENABLE_CHECKING
+      if (flag_strict_aliasing)
+#endif
+       gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
+                     && TREE_CODE (gnu_old_type) == ARRAY_TYPE
+                     && TYPE_NONALIASED_COMPONENT (gnu_new_type)
+                        != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
+
+      TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
+      break;
+
+    case ALIAS_SET_SUBSET:
+    case ALIAS_SET_SUPERSET:
+      {
+       alias_set_type old_set = get_alias_set (gnu_old_type);
+       alias_set_type new_set = get_alias_set (gnu_new_type);
+
+       /* Do nothing if the alias sets conflict.  This ensures that we
+          never call record_alias_subset several times for the same pair
+          or at all for alias set 0.  */
+       if (!alias_sets_conflict_p (old_set, new_set))
+         {
+           if (op == ALIAS_SET_SUBSET)
+             record_alias_subset (old_set, new_set);
+           else
+             record_alias_subset (new_set, old_set);
+         }
+      }
+      break;
+
+    default:
+      gcc_unreachable ();
+    }
 
-  TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
   record_component_aliases (gnu_new_type);
 }
 \f
-/* Return a TREE_LIST describing the substitutions needed to reflect
-   discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
-   them to GNU_LIST.  If GNAT_TYPE is not specified, use the base type
-   of GNAT_SUBTYPE. The substitutions can be in any order.  TREE_PURPOSE
-   gives the tree for the discriminant and TREE_VALUES is the replacement
-   value.  They are in the form of operands to substitute_in_expr.
-   DEFINITION is as in gnat_to_gnu_entity.  */
+/* 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
-substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
-                  tree gnu_list, bool definition)
+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;
 
-  if (No (gnat_type))
-    gnat_type = Implementation_Base_Type (gnat_subtype);
-
-  if (Has_Discriminants (gnat_type))
-    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,
-                              1, 0),
-                             gnu_list);
+  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;
 }
@@ -5317,6 +5621,10 @@ prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
            etype = ATTR_WEAK_EXTERNAL;
            break;
 
+         case Pragma_Thread_Local_Storage:
+           etype = ATTR_THREAD_LOCAL_STORAGE;
+           break;
+
          default:
            continue;
          }
@@ -5334,108 +5642,96 @@ prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
       }
 }
 \f
-/* Get the unpadded version of a GNAT type.  */
-
-tree
-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))
-    type = TREE_TYPE (TYPE_FIELDS (type));
-
-  return type;
-}
-\f
-/* Called when we need to protect a variable object using a save_expr.  */
+/* 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)
+  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 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;
     }
-  else
-    return variable_size (gnu_operand);
+
+  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
-   qualification to use if an external name is appropriate and DEFINITION is
-   nonzero if this is a definition of GNAT_ENTITY.  If NEED_VALUE is nonzero,
-   we need a result.  Otherwise, we are just elaborating this for
-   side-effects.  If NEED_DEBUG is nonzero we need the symbol for debugging
-   purposes even if it isn't needed for code generation.  */
+   return the GCC tree to use for that expression.  GNU_NAME is the suffix
+   to use if a variable needs to be created and DEFINITION is true if this
+   is a definition of GNAT_ENTITY.  If NEED_VALUE is true, we need a result;
+   otherwise, we are just elaborating the expression for side-effects.  If
+   NEED_DEBUG is true, we need a variable for debugging purposes even if it
+   isn't needed for code generation.  */
 
 static tree
-elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
-                     tree gnu_name, bool definition, bool need_value,
-                     bool need_debug)
+elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
+                     bool definition, bool need_value, bool need_debug)
 {
   tree gnu_expr;
 
-  /* If we already elaborated this expression (e.g., it was involved
+  /* If we already elaborated this expression (e.g. it was involved
      in the definition of a private type), use the old value.  */
   if (present_gnu_tree (gnat_expr))
     return get_gnu_tree (gnat_expr);
 
-  /* If we don't need a value and this is static or a discriminant, we
-     don't need to do anything.  */
-  else if (!need_value
-          && (Is_OK_Static_Expression (gnat_expr)
-              || (Nkind (gnat_expr) == N_Identifier
-                  && Ekind (Entity (gnat_expr)) == E_Discriminant)))
-    return 0;
+  /* If we don't need a value and this is static or a discriminant,
+     we don't need to do anything.  */
+  if (!need_value
+      && (Is_OK_Static_Expression (gnat_expr)
+         || (Nkind (gnat_expr) == N_Identifier
+             && Ekind (Entity (gnat_expr)) == E_Discriminant)))
+    return NULL_TREE;
+
+  /* If it's a static expression, we don't need a variable for debugging.  */
+  if (need_debug && Is_OK_Static_Expression (gnat_expr))
+    need_debug = false;
 
-  /* Otherwise, convert this tree to its GCC equivalent.  */
-  gnu_expr
-    = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
-                             gnu_name, definition, need_debug);
+  /* Otherwise, convert this tree to its GCC equivalent and elaborate it.  */
+  gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
+                                    gnu_name, definition, need_debug);
 
   /* Save the expression in case we try to elaborate this entity again.  Since
-     this is not a DECL, don't check it.  Don't save if it's a discriminant. */
+     it's not a DECL, don't check it.  Don't save if it's a discriminant.  */
   if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
     save_gnu_tree (gnat_expr, gnu_expr, true);
 
   return need_value ? gnu_expr : error_mark_node;
 }
 
-/* Similar, but take a GNU expression.  */
+/* Similar, but take a GNU expression and always return a result.  */
 
 static tree
-elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
-                       tree gnu_expr, tree gnu_name, bool definition,
-                       bool need_debug)
+elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
+                       bool definition, bool need_debug)
 {
-  tree gnu_decl = NULL_TREE;
   /* Skip any conversions and simple arithmetics to see if the expression
      is a read-only variable.
      ??? This really should remain read-only, but we have to think about
      the typing of the tree here.  */
   tree gnu_inner_expr
     = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
+  tree gnu_decl = NULL_TREE;
   bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
   bool expr_variable;
 
-  /* In most cases, we won't see a naked FIELD_DECL here because a
-     discriminant reference will have been replaced with a COMPONENT_REF
-     when the type is being elaborated.  However, there are some cases
-     involving child types where we will.  So convert it to a COMPONENT_REF
-     here.  We have to hope it will be at the highest level of the
-     expression in these cases.  */
+  /* In most cases, we won't see a naked FIELD_DECL because a discriminant
+     reference will have been replaced with a COMPONENT_REF when the type
+     is being elaborated.  However, there are some cases involving child
+     types where we will.  So convert it to a COMPONENT_REF.  We hope it
+     will be at the highest level of the expression in these cases.  */
   if (TREE_CODE (gnu_expr) == FIELD_DECL)
     gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
                       build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
@@ -5449,19 +5745,14 @@ elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
      by the variable; otherwise use a SAVE_EXPR if needed.  Note that we
      rely here on the fact that an expression cannot contain both the
      discriminant and some other variable.  */
-
   expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
                   && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
                        && (TREE_READONLY (gnu_inner_expr)
                            || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
                   && !CONTAINS_PLACEHOLDER_P (gnu_expr));
 
-  /* If this is a static expression or contains a discriminant, we don't
-     need the variable for debugging (and can't elaborate anyway if a
-     discriminant).  */
-  if (need_debug
-      && (Is_OK_Static_Expression (gnat_expr)
-         || CONTAINS_PLACEHOLDER_P (gnu_expr)))
+  /* If GNU_EXPR contains a discriminant, we can't elaborate a variable.  */
+  if (need_debug && CONTAINS_PLACEHOLDER_P (gnu_expr))
     need_debug = false;
 
   /* Now create the variable if we need it.  */
@@ -5477,10 +5768,8 @@ elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
      can do the right thing in the local case.  */
   if (expr_global && expr_variable)
     return gnu_decl;
-  else if (!expr_variable)
-    return gnu_expr;
-  else
-    return maybe_variable (gnu_expr);
+
+  return expr_variable ? maybe_variable (gnu_expr) : gnu_expr;
 }
 \f
 /* Create a record type that contains a SIZE bytes long field of TYPE with a
@@ -5525,7 +5814,7 @@ make_aligning_type (tree type, unsigned int align, tree size,
   if (TREE_CODE (name) == TYPE_DECL)
     name = DECL_NAME (name);
 
-  TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
+  TYPE_NAME (record_type) = concat_name (name, "_ALIGN");
 
   /* Compute VOFFSET and then POS.  The next byte position multiple of some
      alignment after some address is obtained by "and"ing the alignment minus
@@ -5572,9 +5861,9 @@ make_aligning_type (tree type, unsigned int align, tree size,
     = size_binop (PLUS_EXPR, size,
                  size_int (room + align / BITS_PER_UNIT));
 
-  TYPE_MODE (record_type) = BLKmode;
+  SET_TYPE_MODE (record_type, BLKmode);
 
-  copy_alias_set (record_type, type);
+  relate_alias_sets (record_type, type, ALIAS_SET_COPY);
   return record_type;
 }
 \f
@@ -5593,8 +5882,8 @@ round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
    as the field type of a packed record if IN_RECORD is true, or as the
    component type of a packed array if IN_RECORD is false.  See if we can
    rewrite it either as a type that has a non-BLKmode, which we can pack
-   tighter in the packed record case, or as a smaller type with BLKmode.
-   If so, return the new type.  If not, return the original type.  */
+   tighter in the packed record case, or as a smaller type.  If so, return
+   the new type.  If not, return the original type.  */
 
 static tree
 make_packable_type (tree type, bool in_record)
@@ -5656,15 +5945,15 @@ make_packable_type (tree type, bool in_record)
       tree new_field_type = TREE_TYPE (old_field);
       tree new_field, new_size;
 
-      if (TYPE_MODE (new_field_type) == BLKmode
-         && (TREE_CODE (new_field_type) == RECORD_TYPE
-             || TREE_CODE (new_field_type) == UNION_TYPE
-             || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
+      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)
          && host_integerp (TYPE_SIZE (new_field_type), 1))
        new_field_type = make_packable_type (new_field_type, true);
 
       /* However, for the last field in a not already packed record type
-        that is of an aggregate type, we need to use the RM_Size in the
+        that is of an aggregate type, we need to use the RM size in the
         packable version of the record type, see finish_record_type.  */
       if (!TREE_CHAIN (old_field)
          && !TYPE_PACKED (type)
@@ -5696,7 +5985,7 @@ make_packable_type (tree type, bool in_record)
     }
 
   finish_record_type (new_type, nreverse (field_list), 2, true);
-  copy_alias_set (new_type, type);
+  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.  */
@@ -5721,8 +6010,8 @@ make_packable_type (tree type, bool in_record)
   /* Try harder to get a packable type if necessary, for example
      in case the record itself contains a BLKmode field.  */
   if (in_record && TYPE_MODE (new_type) == BLKmode)
-    TYPE_MODE (new_type)
-      = mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1);
+    SET_TYPE_MODE (new_type,
+                  mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
 
   /* If neither the mode nor the size has shrunk, return the old type.  */
   if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
@@ -5741,8 +6030,8 @@ make_packable_type (tree type, bool in_record)
 
    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.  */
+   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,
@@ -5780,7 +6069,7 @@ maybe_pad_type (tree type, tree size, unsigned int align,
     }
 
   /* If the size is either not being changed or is being made smaller (which
-     is not done here (and is only valid for bitfields anyway), show the size
+     is not done here and is only valid for bitfields anyway), show the size
      isn't changing.  Likewise, clear the alignment if it isn't being
      changed.  Then return if we aren't doing anything.  */
   if (size
@@ -5843,7 +6132,7 @@ maybe_pad_type (tree type, tree size, unsigned int align,
       && TREE_CODE (type) == RECORD_TYPE
       && TYPE_MODE (type) == BLKmode
       && TREE_CODE (orig_size) == INTEGER_CST
-      && !TREE_CONSTANT_OVERFLOW (orig_size)
+      && !TREE_OVERFLOW (orig_size)
       && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
       && (!size
          || (TREE_CODE (size) == INTEGER_CST
@@ -5863,13 +6152,13 @@ maybe_pad_type (tree type, tree size, unsigned int align,
   /* Do not finalize it until after the auxiliary record is built.  */
   finish_record_type (record, field, 1, true);
 
-  /* Set the same size for its RM_size if requested; otherwise reuse
-     the RM_size of the original type.  */
+  /* Set the same size for its RM size if requested; otherwise reuse
+     the RM size of the original type.  */
   SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
 
   /* 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. */
+     variable that indicates our size, if still variable.  */
   if (TYPE_NAME (record)
       && AGGREGATE_TYPE_P (type)
       && TREE_CODE (orig_size) != INTEGER_CST
@@ -5886,9 +6175,10 @@ maybe_pad_type (tree type, tree size, unsigned int align,
       if (TREE_CODE (orig_name) == TYPE_DECL)
        orig_name = DECL_NAME (orig_name);
 
-      TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
+      TYPE_NAME (marker) = concat_name (name, "XVS");
       finish_record_type (marker,
-                         create_field_decl (orig_name, integer_type_node,
+                         create_field_decl (orig_name,
+                                            build_reference_type (type),
                                             marker, 0, NULL_TREE, NULL_TREE,
                                             0),
                          0, false);
@@ -5896,9 +6186,9 @@ maybe_pad_type (tree type, tree size, unsigned int align,
       add_parallel_type (TYPE_STUB_DECL (record), marker);
 
       if (size && TREE_CODE (size) != INTEGER_CST && definition)
-       create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
-                        sizetype, TYPE_SIZE_UNIT (record), false, false,
-                        false, false, NULL, gnat_entity);
+       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);
@@ -6012,7 +6302,9 @@ choices_to_gnu (tree operand, Node_Id choices)
                                                    operand, high));
              break;
            }
+
          /* ... fall through ... */
+
        case N_Character_Literal:
        case N_Integer_Literal:
          single = gnat_to_gnu (choice);
@@ -6101,11 +6393,10 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
     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 also a record that's
-     BLKmode, see if we can get either an integral mode form of the type or
-     a smaller BLKmode 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.
+     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.
 
      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
@@ -6125,7 +6416,7 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
      from a component clause.  */
 
   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
-      && TYPE_MODE (gnu_field_type) == BLKmode
+      && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
       && host_integerp (TYPE_SIZE (gnu_field_type), 1)
       && (packed == 1
          || (gnu_size
@@ -6369,12 +6660,13 @@ compare_field_bitpos (const PTR rt1, const PTR rt2)
   return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
 }
 
-/* Return a GCC tree for a record type given a GNAT Component_List and a chain
-   of GCC trees for fields that are in the record and have already been
-   processed.  When called from gnat_to_gnu_entity during the processing of a
-   record type definition, the GCC nodes for the discriminants will be on
-   the chain.  The other calls to this function are recursive calls from
-   itself for the Component_List of a variant and the chain is empty.
+/* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
+   the result as the field list of GNU_RECORD_TYPE and finish it up.  When
+   called from gnat_to_gnu_entity during the processing of a record type
+   definition, the GCC node for the parent, if any, will be the single field
+   of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
+   GNU_FIELD_LIST.  The other calls to this function are recursive calls for
+   the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
 
    PACKED is 1 if this is for a packed record, -1 if this is for a record
    with Component_Alignment of Storage_Unit, -2 if this is for a record
@@ -6383,73 +6675,85 @@ compare_field_bitpos (const PTR rt1, const PTR rt2)
    DEFINITION is true if we are defining this record.
 
    P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
-   with a rep clause is to be added.  If it is nonzero, that is all that
-   should be done with such fields.
+   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.
+   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.
 
    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.
 
    DO_NOT_FINALIZE, if true, means that the record type is expected to be
-   modified afterwards so it will not be sent to the back-end for finalization.
+   modified afterwards so it will not be finalized here.
 
    UNCHECKED_UNION, if true, means that we are building a type for a record
-   with a Pragma Unchecked_Union.
-
-   The processing of the component list fills in the chain with all of the
-   fields of the record and then the record type is finished.  */
+   with a Pragma Unchecked_Union.  */
 
 static void
-components_to_record (tree gnu_record_type, Node_Id component_list,
+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)
 {
-  Node_Id component_decl;
-  Entity_Id gnat_field;
-  Node_Id variant_part;
-  tree gnu_our_rep_list = NULL_TREE;
-  tree gnu_field, gnu_last;
-  bool layout_with_rep = false;
   bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
+  bool layout_with_rep = false;
+  Node_Id component_decl, variant_part;
+  tree gnu_our_rep_list = NULL_TREE;
+  tree gnu_field, gnu_next, gnu_last = tree_last (gnu_field_list);
 
-  /* For each variable within each component declaration create a GCC field
-     and add it to the list, skipping any pragmas in the list.  */
-  if (Present (Component_Items (component_list)))
-    for (component_decl = First_Non_Pragma (Component_Items (component_list));
+  /* For each component referenced in a component declaration create a GCC
+     field and add it to the list, skipping pragmas in the GNAT list.  */
+  if (Present (Component_Items (gnat_component_list)))
+    for (component_decl
+          = First_Non_Pragma (Component_Items (gnat_component_list));
         Present (component_decl);
         component_decl = Next_Non_Pragma (component_decl))
       {
-       gnat_field = Defining_Entity (component_decl);
+       Entity_Id gnat_field = Defining_Entity (component_decl);
+       Name_Id gnat_name = Chars (gnat_field);
 
-       if (Chars (gnat_field) == Name_uParent)
-         gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
+       /* If present, the _Parent field must have been created as the single
+          field of the record type.  Put it before any other fields.  */
+       if (gnat_name == Name_uParent)
+         {
+           gnu_field = TYPE_FIELDS (gnu_record_type);
+           gnu_field_list = chainon (gnu_field_list, gnu_field);
+         }
        else
          {
            gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
                                           packed, definition);
 
-           /* If this is the _Tag field, put it before any discriminants,
-              instead of after them as is the case for all other fields.
-              Ignore field of void type if only annotating.  */
-           if (Chars (gnat_field) == Name_uTag)
+           /* If this is the _Tag field, put it before any other fields.  */
+           if (gnat_name == Name_uTag)
              gnu_field_list = chainon (gnu_field_list, gnu_field);
+
+           /* If this is the _Controller field, put it before the other
+              fields except for the _Tag or _Parent field.  */
+           else if (gnat_name == Name_uController && gnu_last)
+             {
+               TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
+               TREE_CHAIN (gnu_last) = gnu_field;
+             }
+
+           /* If this is a regular field, put it after the other fields.  */
            else
              {
                TREE_CHAIN (gnu_field) = gnu_field_list;
                gnu_field_list = gnu_field;
+               if (!gnu_last)
+                 gnu_last = gnu_field;
              }
          }
 
-         save_gnu_tree (gnat_field, gnu_field, false);
+       save_gnu_tree (gnat_field, gnu_field, false);
       }
 
   /* At the end of the component list there may be a variant part.  */
-  variant_part = Variant_Part (component_list);
+  variant_part = Variant_Part (gnat_component_list);
 
   /* We create a QUAL_UNION_TYPE for the variant part since the variants are
      mutually exclusive and should go in the same memory.  To do this we need
@@ -6460,23 +6764,20 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
      use GNU_RECORD_TYPE if there are no fields so far.  */
   if (Present (variant_part))
     {
-      tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
-      Node_Id variant;
+      Node_Id gnat_discr = Name (variant_part), variant;
+      tree gnu_discr = gnat_to_gnu (gnat_discr);
       tree gnu_name = TYPE_NAME (gnu_record_type);
       tree gnu_var_name
-       = concat_id_with_name (get_identifier (Get_Name_String
-                                              (Chars (Name (variant_part)))),
-                              "XVN");
-      tree gnu_union_type;
-      tree gnu_union_name;
-      tree gnu_union_field;
+       = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
+                      "XVN");
+      tree gnu_union_type, gnu_union_name, gnu_union_field;
       tree gnu_variant_list = NULL_TREE;
 
       if (TREE_CODE (gnu_name) == TYPE_DECL)
        gnu_name = DECL_NAME (gnu_name);
 
-      gnu_union_name = concat_id_with_name (gnu_name,
-                                           IDENTIFIER_POINTER (gnu_var_name));
+      gnu_union_name
+       = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
 
       /* Reuse an enclosing union if all fields are in the variant part
         and there is no representation clause on the record, to match
@@ -6504,20 +6805,20 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
          tree gnu_qual;
 
          Get_Variant_Encoding (variant);
-         gnu_inner_name = get_identifier (Name_Buffer);
+         gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
          TYPE_NAME (gnu_variant_type)
-           = concat_id_with_name (gnu_union_name,
-                                  IDENTIFIER_POINTER (gnu_inner_name));
+           = concat_name (gnu_union_name,
+                          IDENTIFIER_POINTER (gnu_inner_name));
 
          /* Set the alignment of the inner type in case we need to make
-            inner objects into bitfields, but then clear it out
-            so the record actually gets only the alignment required.  */
+            inner objects into bitfields, but then clear it out so the
+            record actually gets only the alignment required.  */
          TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
          TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
 
-         /* Similarly, if the outer record has a size specified and all fields
-            have record rep clauses, we can propagate the size into the
-            variant part.  */
+         /* Similarly, if the outer record has a size specified and all
+            fields have record rep clauses, we can propagate the size
+            into the variant part.  */
          if (all_rep_and_size)
            {
              TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
@@ -6525,15 +6826,14 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
                = TYPE_SIZE_UNIT (gnu_record_type);
            }
 
-         /* Create the record type for the variant.  Note that we defer
-            finalizing it until after we are sure to actually use it.  */
+         /* 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.  */
          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);
 
-         gnu_qual = choices_to_gnu (gnu_discriminant,
-                                    Discrete_Choices (variant));
+         gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
 
          Set_Present_Expr (variant, annotate_value (gnu_qual));
 
@@ -6575,7 +6875,7 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
          gnu_variant_list = gnu_field;
        }
 
-      /* Only make the QUAL_UNION_TYPE if there are any non-empty variants.  */
+      /* Only make the QUAL_UNION_TYPE if there are non-empty variants.  */
       if (gnu_variant_list)
        {
          int union_field_packed;
@@ -6618,18 +6918,19 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
     }
 
   /* Scan GNU_FIELD_LIST and see if any fields have rep clauses.  If they
-     do, pull them out and put them into GNU_OUR_REP_LIST.  We have to do this
-     in a separate pass since we want to handle the discriminants but can't
-     play with them until we've used them in debugging data above.
-
-     ??? Note: if we then reorder them, debugging information will be wrong,
-     but there's nothing that can be done about this at the moment.  */
-  for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
+     do, pull them out and put them into GNU_OUR_REP_LIST.  We have to do
+     this in a separate pass since we want to handle the discriminants but
+     can't play with them until we've used them in debugging data above.
+
+     ??? If we then reorder them, debugging information will be wrong but
+     there's nothing that can be done about this at the moment.  */
+  gnu_last = NULL_TREE;
+  for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
     {
+      gnu_next = TREE_CHAIN (gnu_field);
+
       if (DECL_FIELD_OFFSET (gnu_field))
        {
-         tree gnu_next = TREE_CHAIN (gnu_field);
-
          if (!gnu_last)
            gnu_field_list = gnu_next;
          else
@@ -6637,31 +6938,28 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
 
          TREE_CHAIN (gnu_field) = gnu_our_rep_list;
          gnu_our_rep_list = gnu_field;
-         gnu_field = gnu_next;
        }
       else
-       {
-         gnu_last = gnu_field;
-         gnu_field = TREE_CHAIN (gnu_field);
-       }
+       gnu_last = gnu_field;
     }
 
-  /* If we have any items in our rep'ed field list, it is not the case that all
-     the fields in the record have rep clauses, and P_REP_LIST is nonzero,
-     set it and ignore the items.  */
+  /* If we have any fields in our rep'ed field list and it is not the case that
+     all the fields in the record have rep clauses and P_REP_LIST is nonzero,
+     set it and ignore these fields.  */
   if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
     *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
+
+  /* Otherwise, sort the fields by bit position and put them into their own
+     record, before the others, if we also have fields without rep clauses.  */
   else if (gnu_our_rep_list)
     {
-      /* Otherwise, sort the fields by bit position and put them into their
-        own record if we have any fields without rep clauses. */
       tree gnu_rep_type
        = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
-      int len = list_length (gnu_our_rep_list);
+      int i, len = list_length (gnu_our_rep_list);
       tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
-      int i;
 
-      for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
+      for (gnu_field = gnu_our_rep_list, i = 0;
+          gnu_field;
           gnu_field = TREE_CHAIN (gnu_field), i++)
        gnu_arr[i] = gnu_field;
 
@@ -6680,8 +6978,9 @@ components_to_record (tree gnu_record_type, Node_Id component_list,
       if (gnu_field_list)
        {
          finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
-         gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
-                                        gnu_record_type, 0, 0, 0, 1);
+         gnu_field
+           = create_field_decl (get_identifier ("REP"), gnu_rep_type,
+                                gnu_record_type, 0, NULL_TREE, NULL_TREE, 1);
          DECL_INTERNAL_P (gnu_field) = 1;
          gnu_field_list = chainon (gnu_field_list, gnu_field);
        }
@@ -7003,13 +7302,13 @@ 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.  */
+  /* 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.  Give an error if a size was specified, but cannot
-     be represented as in sizetype. */
+  /* 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))
     {
@@ -7020,8 +7319,8 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
     }
 
   /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size unless a size clause exists.  */
-  else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
+     Also ignore a zero size if it is not permitted.  */
+  if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
     return NULL_TREE;
 
   /* The size of objects is always a multiple of a byte.  */
@@ -7039,8 +7338,8 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
 
   /* If this is an integral type or a packed array type, the front-end has
      verified the size, so we need not do it here (which would entail
-     checking against the bounds).  However, if this is an aliased object, it
-     may not be smaller than the type of the object.  */
+     checking against the bounds).  However, if this is an aliased object,
+     it may not be smaller than the type of the object.  */
   if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
       && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
     return size;
@@ -7102,72 +7401,77 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   return size;
 }
 \f
-/* Similarly, but both validate and process a value of RM_Size.  This
+/* Similarly, but both validate and process a value of RM size.  This
    routine is only called for types.  */
 
 static void
 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
 {
-  /* Only give an error if a Value_Size clause was explicitly given.
+  /* 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
     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
-  tree old_size = rm_size (gnu_type);
-  tree size;
+  tree old_size = rm_size (gnu_type), size;
 
-  /* Get the size as a tree.  Do nothing if none was specified, either
-     because RM_Size was not Present or if the specified size was zero.
-     Give an error if a size was specified, but cannot be represented as
-     in sizetype.  */
+  /* 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.  */
   size = UI_To_gnu (uint_size, bitsizetype);
   if (TREE_OVERFLOW (size))
     {
       if (Present (gnat_attr_node))
        post_error_ne ("Value_Size of & is too large", gnat_attr_node,
                       gnat_entity);
-
       return;
     }
 
   /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size unless a size clause exists, a Value_Size
-     clause exists, or this is an integer type, in which case the
-     front end will have always set it.  */
-  else 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)))
+     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)))
     return;
 
   /* If the old size is self-referential, get the maximum size.  */
   if (CONTAINS_PLACEHOLDER_P (old_size))
     old_size = max_size (old_size, true);
 
-  /* If the size of the object is a constant, the new size must not be
-     smaller (the front end checks this for scalar types).  */
+  /* If the size of the object is a constant, the new size must not be smaller
+     (the front-end has verified this for scalar and packed array types).  */
   if (TREE_CODE (old_size) != INTEGER_CST
       || TREE_OVERFLOW (old_size)
       || (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)
+              && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
+              && TYPE_PACKED_ARRAY_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_type))))
          && tree_int_cst_lt (size, old_size)))
     {
       if (Present (gnat_attr_node))
        post_error_ne_tree
          ("Value_Size for& too small{, minimum allowed is ^}",
           gnat_attr_node, gnat_entity, old_size);
-
       return;
     }
 
-  /* Otherwise, set the RM_Size.  */
-  if (TREE_CODE (gnu_type) == INTEGER_TYPE
-      && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
-    TYPE_RM_SIZE_NUM (gnu_type) = size;
-  else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE
-          || TREE_CODE (gnu_type) == BOOLEAN_TYPE)
-    TYPE_RM_SIZE_NUM (gnu_type) = size;
+  /* Otherwise, set the RM size proper for integral types...  */
+  if ((TREE_CODE (gnu_type) == INTEGER_TYPE
+       && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
+      || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
+         || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
+    SET_TYPE_RM_SIZE (gnu_type, size);
+
+  /* ...or the Ada size for record and union types.  */
   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
            || TREE_CODE (gnu_type) == UNION_TYPE
            || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
@@ -7177,7 +7481,7 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
 \f
 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
    If TYPE is the best type, return it.  Otherwise, make a new type.  We
-   only support new integral and pointer types.  FOR_BIASED is nonzero if
+   only support new integral and pointer types.  FOR_BIASED is true if
    we are making a biased type.  */
 
 static tree
@@ -7209,17 +7513,20 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
        break;
 
       biased_p |= for_biased;
-      size = MIN (size, LONG_LONG_TYPE_SIZE);
+      if (size > LONG_LONG_TYPE_SIZE)
+       size = LONG_LONG_TYPE_SIZE;
 
       if (TYPE_UNSIGNED (type) || biased_p)
        new_type = make_unsigned_type (size);
       else
        new_type = make_signed_type (size);
       TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
-      TYPE_MIN_VALUE (new_type)
-       = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
-      TYPE_MAX_VALUE (new_type)
-       = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
+      SET_TYPE_RM_MIN_VALUE (new_type,
+                            convert (TREE_TYPE (new_type),
+                                     TYPE_MIN_VALUE (type)));
+      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))
        {
@@ -7229,7 +7536,7 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
            TYPE_NAME (new_type) = TYPE_NAME (type);
        }
       TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
-      TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
+      SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
       return new_type;
 
     case RECORD_TYPE:
@@ -7294,9 +7601,47 @@ validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
   else if (!(Present (Alignment_Clause (gnat_entity))
             && From_At_Mod (Alignment_Clause (gnat_entity)))
           && new_align * BITS_PER_UNIT < align)
-    post_error_ne_num ("alignment for& must be at least ^",
-                      gnat_error_node, gnat_entity,
-                      align / BITS_PER_UNIT);
+    {
+      unsigned int double_align;
+      bool is_capped_double, align_clause;
+
+      /* If the default alignment of "double" or larger scalar types is
+        specifically capped and the new alignment is above the cap, do
+        not post an error and change the alignment only if there is an
+        alignment clause; this makes it possible to have the associated
+        GCC type overaligned by default for performance reasons.  */
+      if ((double_align = double_float_alignment) > 0)
+       {
+         Entity_Id gnat_type
+           = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
+         is_capped_double
+           = is_double_float_or_array (gnat_type, &align_clause);
+       }
+      else if ((double_align = double_scalar_alignment) > 0)
+       {
+         Entity_Id gnat_type
+           = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
+         is_capped_double
+           = is_double_scalar_or_array (gnat_type, &align_clause);
+       }
+      else
+       is_capped_double = align_clause = false;
+
+      if (is_capped_double && new_align >= double_align)
+       {
+         if (align_clause)
+           align = new_align * BITS_PER_UNIT;
+       }
+      else
+       {
+         if (is_capped_double)
+           align = double_align * BITS_PER_UNIT;
+
+         post_error_ne_num ("alignment for& must be at least ^",
+                            gnat_error_node, gnat_entity,
+                            align / BITS_PER_UNIT);
+       }
+    }
   else
     {
       new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
@@ -7391,7 +7736,7 @@ check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
 \f
 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
    have compatible signatures so that a call using one type may be safely
-   issued if the actual target function type is the other. Return 1 if it is
+   issued if the actual target function type is the other.  Return 1 if it is
    the case, 0 otherwise, and post errors on the incompatibilities.
 
    This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
@@ -7417,72 +7762,80 @@ compatible_signatures_p (tree ftype1, tree ftype2)
   return 1;
 }
 \f
-/* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
-   type with all size expressions that contain F updated by replacing F
-   with R.  If F is NULL_TREE, always make a new RECORD_TYPE, even if
-   nothing has changed.  */
+/* 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.
+
+   The function doesn't update the layout of the type, i.e. it assumes
+   that the substitution is purely formal.  That's why the replacement
+   value R must itself contain a PLACEHOLDER_EXPR.  */
 
 tree
 substitute_in_type (tree t, tree f, tree r)
 {
-  tree new = t;
-  tree tem;
+  tree new;
+
+  gcc_assert (CONTAINS_PLACEHOLDER_P (r));
 
   switch (TREE_CODE (t))
     {
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-      if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
-         || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
+    case REAL_TYPE:
+
+      /* First the domain types of arrays.  */
+      if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
+         || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
        {
-         tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
-         tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
+         tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
+         tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
 
-         if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
+         if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
            return t;
 
-         new = build_range_type (TREE_TYPE (t), low, high);
-         if (TYPE_INDEX_TYPE (t))
+         new = copy_type (t);
+         TYPE_GCC_MIN_VALUE (new) = low;
+         TYPE_GCC_MAX_VALUE (new) = high;
+
+         if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
            SET_TYPE_INDEX_TYPE
              (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
+
          return new;
        }
 
-      return t;
-
-    case REAL_TYPE:
-      if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
-         || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
+      /* Then the subtypes.  */
+      if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
+         || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
        {
-         tree low = NULL_TREE, high = NULL_TREE;
+         tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
+         tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
 
-         if (TYPE_MIN_VALUE (t))
-           low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
-         if (TYPE_MAX_VALUE (t))
-           high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
-
-         if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
+         if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
            return t;
 
-         t = copy_type (t);
-         TYPE_MIN_VALUE (t) = low;
-         TYPE_MAX_VALUE (t) = high;
+         new = copy_type (t);
+         SET_TYPE_RM_MIN_VALUE (new, low);
+         SET_TYPE_RM_MAX_VALUE (new, high);
+
+         return new;
        }
+
       return t;
 
     case COMPLEX_TYPE:
-      tem = substitute_in_type (TREE_TYPE (t), f, r);
-      if (tem == TREE_TYPE (t))
+      new = substitute_in_type (TREE_TYPE (t), f, r);
+      if (new == TREE_TYPE (t))
        return t;
 
-      return build_complex_type (tem);
+      return build_complex_type (new);
 
     case OFFSET_TYPE:
     case METHOD_TYPE:
     case FUNCTION_TYPE:
     case LANG_TYPE:
-      /* Don't know how to do these yet.  */
+      /* These should never show up here.  */
       gcc_unreachable ();
 
     case ARRAY_TYPE:
@@ -7494,23 +7847,14 @@ substitute_in_type (tree t, tree f, tree r)
          return t;
 
        new = build_array_type (component, domain);
-       TYPE_SIZE (new) = 0;
-       TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
-       TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
-       layout_type (new);
        TYPE_ALIGN (new) = TYPE_ALIGN (t);
        TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
-
-       /* If we had bounded the sizes of T by a constant, bound the sizes of
-          NEW by the same constant.  */
-       if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
-         TYPE_SIZE (new)
-           = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
-                         TYPE_SIZE (new));
-       if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
-         TYPE_SIZE_UNIT (new)
-           = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
-                         TYPE_SIZE_UNIT (new));
+       SET_TYPE_MODE (new, TYPE_MODE (t));
+       TYPE_SIZE (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
+       TYPE_SIZE_UNIT (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
+       TYPE_NONALIASED_COMPONENT (new) = TYPE_NONALIASED_COMPONENT (t);
+       TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
+       TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
        return new;
       }
 
@@ -7518,54 +7862,41 @@ substitute_in_type (tree t, tree f, tree r)
     case UNION_TYPE:
     case QUAL_UNION_TYPE:
       {
+       bool changed_field = false;
        tree field;
-       bool changed_field
-         = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
-       bool field_has_rep = false;
-       tree last_field = NULL_TREE;
-
-       tree new = copy_type (t);
 
        /* Start out with no fields, make new fields, and chain them
           in.  If we haven't actually changed the type of any field,
           discard everything we've done and return the old type.  */
-
+       new = copy_type (t);
        TYPE_FIELDS (new) = NULL_TREE;
-       TYPE_SIZE (new) = NULL_TREE;
 
        for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
          {
-           tree new_field = copy_node (field);
-
-           TREE_TYPE (new_field)
-             = substitute_in_type (TREE_TYPE (new_field), f, r);
-
-           if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
-             field_has_rep = true;
-           else if (TREE_TYPE (new_field) != TREE_TYPE (field))
-             changed_field = true;
-
-           /* If this is an internal field and the type of this field is
-              a UNION_TYPE or RECORD_TYPE with no elements, ignore it.  If
-              the type just has one element, treat that as the field.
-              But don't do this if we are processing a QUAL_UNION_TYPE.  */
-           if (TREE_CODE (t) != QUAL_UNION_TYPE
-               && DECL_INTERNAL_P (new_field)
-               && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
-                   || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
+           tree new_field = copy_node (field), new_n;
+
+           new_n = substitute_in_type (TREE_TYPE (field), f, r);
+           if (new_n != TREE_TYPE (field))
              {
-               if (!TYPE_FIELDS (TREE_TYPE (new_field)))
-                 continue;
+               TREE_TYPE (new_field) = new_n;
+               changed_field = true;
+             }
 
-               if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
-                 {
-                   tree next_new_field
-                     = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
+           new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
+           if (new_n != DECL_FIELD_OFFSET (field))
+             {
+               DECL_FIELD_OFFSET (new_field) = new_n;
+               changed_field = true;
+             }
 
-                   /* Make sure omitting the union doesn't change
-                      the layout.  */
-                   DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
-                   new_field = next_new_field;
+           /* Do the substitution inside the qualifier, if any.  */
+           if (TREE_CODE (t) == QUAL_UNION_TYPE)
+             {
+               new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
+               if (new_n != DECL_QUALIFIER (field))
+                 {
+                   DECL_QUALIFIER (new_field) = new_n;
+                   changed_field = true;
                  }
              }
 
@@ -7574,68 +7905,17 @@ substitute_in_type (tree t, tree f, tree r)
                                     (DECL_ORIGINAL_FIELD (field)
                                      ? DECL_ORIGINAL_FIELD (field) : field));
 
-           /* If the size of the old field was set at a constant,
-              propagate the size in case the type's size was variable.
-              (This occurs in the case of a variant or discriminated
-              record with a default size used as a field of another
-              record.)  */
-           DECL_SIZE (new_field)
-             = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
-               ? DECL_SIZE (field) : NULL_TREE;
-           DECL_SIZE_UNIT (new_field)
-             = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
-               ? DECL_SIZE_UNIT (field) : NULL_TREE;
-
-           if (TREE_CODE (t) == QUAL_UNION_TYPE)
-             {
-               tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
-
-               if (new_q != DECL_QUALIFIER (new_field))
-                 changed_field = true;
-
-               /* Do the substitution inside the qualifier and if we find
-                  that this field will not be present, omit it.  */
-               DECL_QUALIFIER (new_field) = new_q;
-
-               if (integer_zerop (DECL_QUALIFIER (new_field)))
-                 continue;
-             }
-
-           if (!last_field)
-             TYPE_FIELDS (new) = new_field;
-           else
-             TREE_CHAIN (last_field) = new_field;
-
-           last_field = new_field;
-
-           /* If this is a qualified type and this field will always be
-              present, we are done.  */
-           if (TREE_CODE (t) == QUAL_UNION_TYPE
-               && integer_onep (DECL_QUALIFIER (new_field)))
-             break;
+           TREE_CHAIN (new_field) = TYPE_FIELDS (new);
+           TYPE_FIELDS (new) = new_field;
          }
 
-       /* If this used to be a qualified union type, but we now know what
-          field will be present, make this a normal union.  */
-       if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
-           && (!TYPE_FIELDS (new)
-               || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
-         TREE_SET_CODE (new, UNION_TYPE);
-       else if (!changed_field)
+       if (!changed_field)
          return t;
 
-       gcc_assert (!field_has_rep);
-       layout_type (new);
-
-       /* If the size was originally a constant use it.  */
-       if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
-           && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
-         {
-           TYPE_SIZE (new) = TYPE_SIZE (t);
-           TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
-           SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
-         }
-
+       TYPE_FIELDS (new) = nreverse (TYPE_FIELDS (new));
+       TYPE_SIZE (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
+       TYPE_SIZE_UNIT (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
+       SET_TYPE_ADA_SIZE (new, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
        return new;
       }
 
@@ -7644,34 +7924,47 @@ substitute_in_type (tree t, tree f, tree r)
     }
 }
 \f
-/* Return the "RM size" of GNU_TYPE.  This is the actual number of bits
+/* Return the RM size of GNU_TYPE.  This is the actual number of bits
    needed to represent the object.  */
 
 tree
 rm_size (tree gnu_type)
 {
-  /* For integer types, this is the precision.  For record types, we store
-     the size explicitly.  For other types, this is just the size.  */
-
+  /* For integral types, we store the RM size explicitly.  */
   if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
     return TYPE_RM_SIZE (gnu_type);
-  else if (TREE_CODE (gnu_type) == RECORD_TYPE
-          && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
-    /* Return the rm_size of the actual data plus the size of the template.  */
+
+  /* Return the RM size of the actual data plus the size of the template.  */
+  if (TREE_CODE (gnu_type) == RECORD_TYPE
+      && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
     return
       size_binop (PLUS_EXPR,
                  rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
                  DECL_SIZE (TYPE_FIELDS (gnu_type)));
-  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_ADA_SIZE (gnu_type))
+
+  /* For record types, we store the size explicitly.  */
+  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_ADA_SIZE (gnu_type))
     return TYPE_ADA_SIZE (gnu_type);
-  else
-    return TYPE_SIZE (gnu_type);
+
+  /* For other types, this is just the size.  */
+  return TYPE_SIZE (gnu_type);
 }
 \f
+/* Return the name to be used for GNAT_ENTITY.  If a type, create a
+   fully-qualified name, possibly with type information encoding.
+   Otherwise, return the name.  */
+
+tree
+get_entity_name (Entity_Id gnat_entity)
+{
+  Get_Encoded_Name (gnat_entity);
+  return get_identifier_with_length (Name_Buffer, Name_Len);
+}
+
 /* Return an identifier representing the external name to be used for
    GNAT_ENTITY.  If SUFFIX is specified, the name is followed by "___"
    and the specified suffix.  */
@@ -7681,55 +7974,44 @@ create_concat_name (Entity_Id gnat_entity, const char *suffix)
 {
   Entity_Kind kind = Ekind (gnat_entity);
 
-  const char *str = (!suffix ? "" : suffix);
-  String_Template temp = {1, strlen (str)};
-  Fat_Pointer fp = {str, &temp};
-
-  Get_External_Name_With_Suffix (gnat_entity, fp);
+  if (suffix)
+    {
+      String_Template temp = {1, strlen (suffix)};
+      Fat_Pointer fp = {suffix, &temp};
+      Get_External_Name_With_Suffix (gnat_entity, fp);
+    }
+  else
+    Get_External_Name (gnat_entity, 0);
 
-  /* A variable using the Stdcall convention (meaning we are running
-     on a Windows box) live in a DLL. Here we adjust its name to use
-     the jump-table, the _imp__NAME contains the address for the NAME
-     variable. */
+  /* A variable using the Stdcall convention lives in a DLL.  We adjust
+     its name to use the jump table, the _imp__NAME contains the address
+     for the NAME variable.  */
   if ((kind == E_Variable || kind == E_Constant)
       && Has_Stdcall_Convention (gnat_entity))
     {
-      const char *prefix = "_imp__";
-      int k, plen = strlen (prefix);
-
-      for (k = 0; k <= Name_Len; k++)
-       Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
-      strncpy (Name_Buffer, prefix, plen);
+      const int len = 6 + Name_Len;
+      char *new_name = (char *) alloca (len + 1);
+      strcpy (new_name, "_imp__");
+      strcat (new_name, Name_Buffer);
+      return get_identifier_with_length (new_name, len);
     }
 
-  return get_identifier (Name_Buffer);
-}
-
-/* Return the name to be used for GNAT_ENTITY.  If a type, create a
-   fully-qualified name, possibly with type information encoding.
-   Otherwise, return the name.  */
-
-tree
-get_entity_name (Entity_Id gnat_entity)
-{
-  Get_Encoded_Name (gnat_entity);
-  return get_identifier (Name_Buffer);
+  return get_identifier_with_length (Name_Buffer, Name_Len);
 }
 
-/* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
+/* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
    string, return a new IDENTIFIER_NODE that is the concatenation of
-   the name in GNU_ID and SUFFIX.  */
+   the name followed by "___" and the specified suffix.  */
 
 tree
-concat_id_with_name (tree gnu_id, const char *suffix)
+concat_name (tree gnu_name, const char *suffix)
 {
-  int len = IDENTIFIER_LENGTH (gnu_id);
-
-  strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
-  strncpy (Name_Buffer + len, "___", 3);
-  len += 3;
-  strcpy (Name_Buffer + len, suffix);
-  return get_identifier (Name_Buffer);
+  const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
+  char *new_name = (char *) alloca (len + 1);
+  strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
+  strcat (new_name, "___");
+  strcat (new_name, suffix);
+  return get_identifier_with_length (new_name, len);
 }
 
 #include "gt-ada-decl.h"