OSDN Git Service

* gcc-interface/cuintp.c (UI_To_gnu): Fix long line.
[pf3gnuchains/gcc-fork.git] / gcc / ada / gcc-interface / decl.c
index a333170..35f7f0a 100644 (file)
@@ -129,6 +129,8 @@ 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 (tree, Entity_Id, tree, bool, bool);
+static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
+                                   unsigned int);
 static tree make_packable_type (tree, bool);
 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
@@ -182,6 +184,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   const Entity_Kind kind = Ekind (gnat_entity);
   /* True if this is a type.  */
   const bool is_type = IN (kind, Type_Kind);
+  /* True if debug info is requested for this entity.  */
+  const bool debug_info_p = Needs_Debug_Info (gnat_entity);
+  /* True if this entity is to be considered as imported.  */
+  const bool imported_p
+    = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
   /* For a type, contains the equivalent GNAT node to be used in gigi.  */
   Entity_Id gnat_equiv_type = Empty;
   /* Temporary used to walk the GNAT tree.  */
@@ -207,8 +214,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   /* True if we made GNU_DECL and its type here.  */
   bool this_made_decl = false;
   /* True if debug info is requested for this entity.  */
-  bool debug_info_p = (Needs_Debug_Info (gnat_entity)
-                      || debug_info_level == DINFO_LEVEL_VERBOSE);
+  bool debug_info_p = Needs_Debug_Info (gnat_entity);
   /* True if this entity is to be considered as imported.  */
   bool imported_p = (Is_Imported (gnat_entity)
                     && No (Address_Clause (gnat_entity)));
@@ -549,10 +555,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_Out_Parameter:
     case E_Variable:
 
-      /* Simple variables, loop variables, Out parameters, and exceptions.  */
+      /* Simple variables, loop variables, Out parameters and exceptions.  */
     object:
       {
-       bool used_by_ref = false;
        bool const_flag
          = ((kind == E_Constant || kind == E_Variable)
             && Is_True_Constant (gnat_entity)
@@ -560,10 +565,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
             && (((Nkind (Declaration_Node (gnat_entity))
                   == N_Object_Declaration)
                  && Present (Expression (Declaration_Node (gnat_entity))))
-                || Present (Renamed_Object (gnat_entity))));
+                || Present (Renamed_Object (gnat_entity))
+                || imported_p));
        bool inner_const_flag = const_flag;
        bool static_p = Is_Statically_Allocated (gnat_entity);
        bool mutable_p = false;
+       bool used_by_ref = false;
        tree gnu_ext_name = NULL_TREE;
        tree renamed_obj = NULL_TREE;
        tree gnu_object_size;
@@ -580,6 +587,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        /* Get the type after elaborating the renamed object.  */
        gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
 
+       /* If this is a standard exception definition, then use the standard
+          exception type.  This is necessary to make sure that imported and
+          exported views of exceptions are properly merged in LTO mode.  */
+       if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
+           && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
+         gnu_type = except_type_node;
+
        /* For a debug renaming declaration, build a pure debug entity.  */
        if (Present (Debug_Renaming_Link (gnat_entity)))
          {
@@ -605,9 +619,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (kind == E_Loop_Parameter)
          gnu_type = get_base_type (gnu_type);
 
-       /* Reject non-renamed objects whose types are unconstrained arrays or
-          any object whose type is a dummy type or VOID_TYPE.  */
-
+       /* Reject non-renamed objects whose type is an unconstrained array or
+          any object whose type is a dummy type or void.  */
        if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
             && No (Renamed_Object (gnat_entity)))
            || TYPE_IS_DUMMY_P (gnu_type)
@@ -627,6 +640,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            gcc_assert (Present (Alignment (gnat_entity)));
            align = validate_alignment (Alignment (gnat_entity), gnat_entity,
                                        TYPE_ALIGN (gnu_type));
+
            /* No point in changing the type if there is an address clause
               as the final type of the object will be a reference type.  */
            if (Present (Address_Clause (gnat_entity)))
@@ -637,11 +651,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                  false, false, definition, true);
          }
 
-       /* If we are defining the object, see if it has a Size value and
-          validate it if so.  If we are not defining the object and a Size
-          clause applies, simply retrieve the value.  We don't want to ignore
-          the clause and it is expected to have been validated already.  Then
-          get the new type, if any.  */
+       /* If we are defining the object, see if it has a Size and validate it
+          if so.  If we are not defining the object and a Size clause applies,
+          simply retrieve the value.  We don't want to ignore the clause and
+          it is expected to have been validated already.  Then get the new
+          type, if any.  */
        if (definition)
          gnu_size = validate_size (Esize (gnat_entity), gnu_type,
                                    gnat_entity, VAR_DECL, false,
@@ -660,8 +674,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          }
 
        /* If this object has self-referential size, it must be a record with
-          a default value.  We are supposed to allocate an object of the
-          maximum size in this case unless it is a constant with an
+          a default discriminant.  We are supposed to allocate an object of
+          the maximum size in this case, unless it is a constant with an
           initializing expression, in which case we can get the size from
           that.  Note that the resulting size may still be a variable, so
           this may end up with an indirect allocation.  */
@@ -708,8 +722,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              }
          }
 
-       /* If the size is zero bytes, make it one byte since some linkers have
-          trouble with zero-sized objects.  If the object will have a
+       /* If the size is zero byte, make it one byte since some linkers have
+          troubles with zero-sized objects.  If the object will have a
           template, that will make it nonzero so don't bother.  Also avoid
           doing that for an object renaming or an object with an address
           clause, as we would lose useful information on the view size
@@ -740,6 +754,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    && kind != E_Out_Parameter
                    && Is_Composite_Type (Etype (gnat_entity))
                    && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
+                   && !Is_Exported (gnat_entity)
                    && !imported_p
                    && No (Renamed_Object (gnat_entity))
                    && No (Address_Clause (gnat_entity))))
@@ -775,7 +790,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
           ??? Note that we ignore Has_Volatile_Components on objects; it's
           not at all clear what to do in that case.  */
-
        if (Has_Atomic_Components (gnat_entity))
          {
            tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
@@ -790,10 +804,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Now check if the type of the object allows atomic access.  Note
           that we must test the type, even if this object has size and
-          alignment to allow such access, because we will be going
-          inside the padded record to assign to the object.  We could fix
-          this by always copying via an intermediate value, but it's not
-          clear it's worth the effort.  */
+          alignment to allow such access, because we will be going inside
+          the padded record to assign to the object.  We could fix this by
+          always copying via an intermediate value, but it's not clear it's
+          worth the effort.  */
        if (Is_Atomic (gnat_entity))
          check_ok_for_atomic (gnu_type, gnat_entity, false);
 
@@ -809,7 +823,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_name (gnu_entity_name,
-                                                          "UNC"));
+                                                          "UNC"),
+                                             debug_info_p);
        }
 
 #ifdef MINIMUM_ATOMIC_ALIGNMENT
@@ -983,8 +998,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                           as we have a VAR_DECL for the pointer we make.  */
                      }
 
-                   gnu_expr
-                     = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
+                   gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
+                                              maybe_stable_expr);
 
                    gnu_size = NULL_TREE;
                    used_by_ref = true;
@@ -997,8 +1012,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           and disallow any optimizations for such a non-constant object.  */
        if ((Treat_As_Volatile (gnat_entity)
             || (!const_flag
+                && gnu_type != except_type_node
                 && (Is_Exported (gnat_entity)
-                    || Is_Imported (gnat_entity)
+                    || imported_p
                     || Present (Address_Clause (gnat_entity)))))
            && !TYPE_VOLATILE (gnu_type))
          gnu_type = build_qualified_type (gnu_type,
@@ -1025,16 +1041,15 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              = TYPE_PADDING_P (gnu_type)
                ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
                : TYPE_FIELDS (gnu_type);
-
            gnu_expr
              = gnat_build_constructor
-             (gnu_type,
-              tree_cons
-              (template_field,
-               build_template (TREE_TYPE (template_field),
-                               TREE_TYPE (TREE_CHAIN (template_field)),
-                               NULL_TREE),
-               NULL_TREE));
+               (gnu_type,
+                tree_cons
+                (template_field,
+                 build_template (TREE_TYPE (template_field),
+                                 TREE_TYPE (TREE_CHAIN (template_field)),
+                                 NULL_TREE),
+                 NULL_TREE));
          }
 
        /* Convert the expression to the type of the object except in the
@@ -1051,12 +1066,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
          gnu_expr = convert (gnu_type, gnu_expr);
 
-       /* If this is a pointer and it does not have an initializing
-          expression, initialize it to NULL, unless the object is
-          imported.  */
+       /* If this is a pointer that doesn't have an initializing expression,
+          initialize it to NULL, unless the object is imported.  */
        if (definition
            && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
-           && !Is_Imported (gnat_entity) && !gnu_expr)
+           && !gnu_expr
+           && !Is_Imported (gnat_entity))
          gnu_expr = integer_zero_node;
 
        /* If we are defining the object and it has an Address clause, we must
@@ -1066,10 +1081,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           effects in this case.  */
        if (definition && Present (Address_Clause (gnat_entity)))
          {
+           Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
            tree gnu_address
              = present_gnu_tree (gnat_entity)
-               ? get_gnu_tree (gnat_entity)
-               : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
+               ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
 
            save_gnu_tree (gnat_entity, NULL_TREE, false);
 
@@ -1082,9 +1097,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              = build_reference_type_for_mode (gnu_type, ptr_mode, true);
            gnu_address = convert (gnu_type, gnu_address);
            used_by_ref = true;
-           const_flag = !Is_Public (gnat_entity)
-             || compile_time_known_address_p (Expression (Address_Clause
-                                                          (gnat_entity)));
+           const_flag
+             = !Is_Public (gnat_entity)
+               || compile_time_known_address_p (gnat_expr);
 
            /* If this is a deferred constant, the initializer is attached to
               the full view.  */
@@ -1155,14 +1170,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           If the object's size overflows, make an allocator too, so that
           Storage_Error gets raised.  Note that we will never free
           such memory, so we presume it never will get allocated.  */
-
        if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
-                                global_bindings_p () || !definition
+                                global_bindings_p ()
+                                || !definition
                                 || static_p)
-           || (gnu_size
-               && ! allocatable_size_p (gnu_size,
-                                        global_bindings_p () || !definition
-                                        || static_p)))
+           || (gnu_size && !allocatable_size_p (gnu_size,
+                                                global_bindings_p ()
+                                                || !definition
+                                                || static_p)))
          {
            gnu_type = build_reference_type (gnu_type);
            gnu_size = NULL_TREE;
@@ -1181,7 +1196,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
               If we are elaborating a mutable object, tell build_allocator to
               ignore a possibly simpler size from the initializer, if any, as
               we must allocate the maximum possible size in this case.  */
-
            if (definition)
              {
                tree gnu_alloc_type = TREE_TYPE (gnu_type);
@@ -1291,10 +1305,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                        || Is_Exported (gnat_entity)))))
          gnu_ext_name = create_concat_name (gnat_entity, NULL);
 
-       /* If this is constant initialized to a static constant and the
-          object has an aggregate type, force it to be statically
-          allocated.  This will avoid an initialization copy.  */
-       if (!static_p && const_flag
+       /* If this is an aggregate constant initialized to a constant, force it
+          to be statically allocated.  This saves an initialization copy.  */
+       if (!static_p
+           && const_flag
            && gnu_expr && TREE_CONSTANT (gnu_expr)
            && AGGREGATE_TYPE_P (gnu_type)
            && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
@@ -1303,13 +1317,31 @@ 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_name, gnu_ext_name, gnu_type,
-                                   gnu_expr, const_flag,
-                                   Is_Public (gnat_entity),
-                                   imported_p || !definition,
-                                   static_p, attr_list, gnat_entity);
+       gnu_decl
+         = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
+                            gnu_expr, const_flag, Is_Public (gnat_entity),
+                            imported_p || !definition, static_p, attr_list,
+                            gnat_entity);
        DECL_BY_REF_P (gnu_decl) = used_by_ref;
        DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
+
+       /* If we are defining an Out parameter and optimization isn't enabled,
+          create a fake PARM_DECL for debugging purposes and make it point to
+          the VAR_DECL.  Suppress debug info for the latter but make sure it
+          will live on the stack so that it can be accessed from within the
+          debugger through the PARM_DECL.  */
+       if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
+         {
+           tree param = create_param_decl (gnu_entity_name, gnu_type, false);
+           gnat_pushdecl (param, gnat_entity);
+           SET_DECL_VALUE_EXPR (param, gnu_decl);
+           DECL_HAS_VALUE_EXPR_P (param) = 1;
+           DECL_IGNORED_P (gnu_decl) = 1;
+           TREE_ADDRESSABLE (gnu_decl) = 1;
+         }
+
+       /* If this is a renaming pointer, attach the renamed object to it and
+          register it if we are at top level.  */
        if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
          {
            SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
@@ -1320,46 +1352,18 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              }
          }
 
-       if (definition && DECL_SIZE_UNIT (gnu_decl)
-           && get_block_jmpbuf_decl ()
-           && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
-               || (flag_stack_check == GENERIC_STACK_CHECK
-                   && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
-                                        STACK_CHECK_MAX_VAR_SIZE) > 0)))
-         add_stmt_with_node (build_call_1_expr
-                             (update_setjmp_buf_decl,
-                              build_unary_op (ADDR_EXPR, NULL_TREE,
-                                              get_block_jmpbuf_decl ())),
-                             gnat_entity);
-
-       /* If we are defining an Out parameter and we're not optimizing,
-          create a fake PARM_DECL for debugging purposes and make it
-          point to the VAR_DECL.  Suppress debug info for the latter
-          but make sure it will still live on the stack so it can be
-          accessed from within the debugger through the PARM_DECL.  */
-       if (kind == E_Out_Parameter && definition && !optimize)
-         {
-           tree param = create_param_decl (gnu_entity_name, gnu_type, false);
-           gnat_pushdecl (param, gnat_entity);
-           SET_DECL_VALUE_EXPR (param, gnu_decl);
-           DECL_HAS_VALUE_EXPR_P (param) = 1;
-           if (debug_info_p)
-             debug_info_p = false;
-           else
-             DECL_IGNORED_P (param) = 1;
-           TREE_ADDRESSABLE (gnu_decl) = 1;
-         }
-
-       /* If this is a public constant or we're not optimizing and we're not
-          making a VAR_DECL for it, make one just for export or debugger use.
-          Likewise if the address is taken or if either the object or type is
-          aliased.  Make an external declaration for a reference, unless this
-          is a Standard entity since there no real symbol at the object level
-          for these.  */
+       /* If this is a constant and we are defining it or it generates a real
+          symbol at the object level and we are referencing it, we may want
+          or need to have a true variable to represent it:
+            - if optimization isn't enabled, for debugging purposes,
+            - if the constant is public and not overlaid on something else,
+            - if its address is taken,
+            - if either itself or its type is aliased.  */
        if (TREE_CODE (gnu_decl) == CONST_DECL
            && (definition || Sloc (gnat_entity) > Standard_Location)
-           && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
-               || !optimize
+           && ((!optimize && debug_info_p)
+               || (Is_Public (gnat_entity)
+                   && No (Address_Clause (gnat_entity)))
                || Address_Taken (gnat_entity)
                || Is_Aliased (gnat_entity)
                || Is_Aliased (Etype (gnat_entity))))
@@ -1373,8 +1377,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
 
            /* As debugging information will be generated for the variable,
-              do not generate information for the constant.  */
-           DECL_IGNORED_P (gnu_decl) = 1;
+              do not generate debugging information for the constant.  */
+           if (debug_info_p)
+             DECL_IGNORED_P (gnu_decl) = 1;
+           else
+             DECL_IGNORED_P (gnu_corr_var) = 1;
          }
 
        /* If this is a constant, even if we don't need a true variable, we
@@ -1393,6 +1400,23 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            && Exception_Mechanism != Back_End_Exceptions)
          TREE_ADDRESSABLE (gnu_decl) = 1;
 
+       /* If we are defining an object with variable size or an object with
+          fixed size that will be dynamically allocated, and we are using the
+          setjmp/longjmp exception mechanism, update the setjmp buffer.  */
+       if (definition
+           && Exception_Mechanism == Setjmp_Longjmp
+           && get_block_jmpbuf_decl ()
+           && DECL_SIZE_UNIT (gnu_decl)
+           && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
+               || (flag_stack_check == GENERIC_STACK_CHECK
+                   && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
+                                        STACK_CHECK_MAX_VAR_SIZE) > 0)))
+         add_stmt_with_node (build_call_1_expr
+                             (update_setjmp_buf_decl,
+                              build_unary_op (ADDR_EXPR, NULL_TREE,
+                                              get_block_jmpbuf_decl ())),
+                             gnat_entity);
+
        /* Back-annotate Esize and Alignment of the object if not already
           known.  Note that we pick the values of the type, not those of
           the object, to shield ourselves from low-level platform-dependent
@@ -1845,12 +1869,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        tem = chainon (chainon (NULL_TREE,
                                create_field_decl (get_identifier ("P_ARRAY"),
                                                   ptr_void_type_node,
-                                                  gnu_fat_type, 0,
-                                                  NULL_TREE, NULL_TREE, 0)),
+                                                  gnu_fat_type, NULL_TREE,
+                                                  NULL_TREE, 0, 0)),
                       create_field_decl (get_identifier ("P_BOUNDS"),
                                          gnu_ptr_template,
-                                         gnu_fat_type, 0,
-                                         NULL_TREE, NULL_TREE, 0));
+                                         gnu_fat_type, NULL_TREE,
+                                         NULL_TREE, 0, 0));
 
        /* Make sure we can put this into a register.  */
        TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
@@ -1881,55 +1905,59 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            char field_name[16];
            tree gnu_index_base_type
              = get_unpadded_type (Base_Type (Etype (gnat_index)));
-           tree gnu_low_field, gnu_high_field, gnu_low, gnu_high, gnu_max;
+           tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
+           tree gnu_min, gnu_max, gnu_high;
 
            /* Make the FIELD_DECLs for the low and high bounds of this
               type and then make extractions of these fields from the
               template.  */
            sprintf (field_name, "LB%d", index);
-           gnu_low_field = create_field_decl (get_identifier (field_name),
-                                              gnu_index_base_type,
-                                              gnu_template_type, 0,
-                                              NULL_TREE, NULL_TREE, 0);
+           gnu_lb_field = create_field_decl (get_identifier (field_name),
+                                             gnu_index_base_type,
+                                             gnu_template_type, NULL_TREE,
+                                             NULL_TREE, 0, 0);
            Sloc_to_locus (Sloc (gnat_entity),
-                          &DECL_SOURCE_LOCATION (gnu_low_field));
+                          &DECL_SOURCE_LOCATION (gnu_lb_field));
 
            field_name[0] = 'U';
-           gnu_high_field = create_field_decl (get_identifier (field_name),
-                                               gnu_index_base_type,
-                                               gnu_template_type, 0,
-                                               NULL_TREE, NULL_TREE, 0);
+           gnu_hb_field = create_field_decl (get_identifier (field_name),
+                                             gnu_index_base_type,
+                                             gnu_template_type, NULL_TREE,
+                                             NULL_TREE, 0, 0);
            Sloc_to_locus (Sloc (gnat_entity),
-                          &DECL_SOURCE_LOCATION (gnu_high_field));
+                          &DECL_SOURCE_LOCATION (gnu_hb_field));
 
-           gnu_temp_fields[index] = chainon (gnu_low_field, gnu_high_field);
+           gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
 
            /* We can't use build_component_ref here since the template type
               isn't complete yet.  */
-           gnu_low = build3 (COMPONENT_REF, gnu_index_base_type,
-                             gnu_template_reference, gnu_low_field,
-                             NULL_TREE);
-           gnu_high = build3 (COMPONENT_REF, gnu_index_base_type,
-                              gnu_template_reference, gnu_high_field,
-                              NULL_TREE);
-           TREE_READONLY (gnu_low) = TREE_READONLY (gnu_high) = 1;
-
-           /* Compute the size of this dimension.  */
-           gnu_max
-             = build3 (COND_EXPR, gnu_index_base_type,
-                       build2 (GE_EXPR, integer_type_node, gnu_high, gnu_low),
-                       gnu_high,
-                       build2 (MINUS_EXPR, gnu_index_base_type,
-                               gnu_low, fold_convert (gnu_index_base_type,
-                                                      integer_one_node)));
+           gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
+                                  gnu_template_reference, gnu_lb_field,
+                                  NULL_TREE);
+           gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
+                                  gnu_template_reference, gnu_hb_field,
+                                  NULL_TREE);
+           TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
+
+           gnu_min = convert (sizetype, gnu_orig_min);
+           gnu_max = convert (sizetype, gnu_orig_max);
+
+           /* Compute the size of this dimension.  See the E_Array_Subtype
+              case below for the rationale.  */
+           gnu_high
+             = build3 (COND_EXPR, sizetype,
+                       build2 (GE_EXPR, boolean_type_node,
+                               gnu_orig_max, gnu_orig_min),
+                       gnu_max,
+                       size_binop (MINUS_EXPR, gnu_min, size_one_node));
 
            /* Make a range type with the new range in the Ada base type.
               Then make an index type with the size range in sizetype.  */
            gnu_index_types[index]
-             = create_index_type (convert (sizetype, gnu_low),
-                                  convert (sizetype, gnu_max),
+             = create_index_type (gnu_min, gnu_high,
                                   create_range_type (gnu_index_base_type,
-                                                     gnu_low, gnu_high),
+                                                     gnu_orig_min,
+                                                     gnu_orig_max),
                                   gnat_entity);
 
            /* Update the maximum size of the array in elements.  */
@@ -1970,7 +1998,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
        /* Now make the array of arrays and update the pointer to the array
           in the fat pointer.  Note that it is the first field.  */
-        tem = gnat_to_gnu_component_type (gnat_entity, definition,
+       tem = gnat_to_gnu_component_type (gnat_entity, definition,
                                          debug_info_p);
 
        /* If Component_Size is not already specified, annotate it with the
@@ -2024,7 +2052,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
 
        /* If the maximum size doesn't overflow, use it.  */
-        if (gnu_max_size
+       if (gnu_max_size
            && TREE_CODE (gnu_max_size) == INTEGER_CST
            && !TREE_OVERFLOW (gnu_max_size)
            && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
@@ -2050,11 +2078,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                          gnu_fat_type, NULL, true,
                          debug_info_p, gnat_entity);
 
-       /* Create the type to be used as what a thin pointer designates: an
-          record type for the object and its template with the field offsets
-          shifted to have the template at a negative offset.  */
+       /* Create the type to be used as what a thin pointer designates:
+          a record type for the object and its template with the fields
+          shifted to have the template at a negative offset.  */
        tem = build_unc_object_type (gnu_template_type, tem,
-                                    create_concat_name (gnat_name, "XUT"));
+                                    create_concat_name (gnat_name, "XUT"),
+                                    debug_info_p);
        shift_unc_components_for_thin_pointers (tem);
 
        SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
@@ -2104,14 +2133,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
               gnat_base_index = Next_Index (gnat_base_index))
            {
              tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
-             const int prec_comp
-               = compare_tree_int (TYPE_RM_SIZE (gnu_index_type),
-                                   TYPE_PRECISION (sizetype));
-             const bool subrange_p = (prec_comp < 0)
-                                     || (prec_comp == 0
-                                         && TYPE_UNSIGNED (gnu_index_type)
-                                            == TYPE_UNSIGNED (sizetype));
-             const bool wider_p = (prec_comp > 0);
              tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
              tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
              tree gnu_min = convert (sizetype, gnu_orig_min);
@@ -2120,7 +2141,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                = get_unpadded_type (Etype (gnat_base_index));
              tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
              tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
-             tree gnu_high, gnu_low;
+             tree gnu_high;
 
              /* See if the base array type is already flat.  If it is, we
                 are probably compiling an ACATS test but it will cause the
@@ -2136,8 +2157,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
              /* Similarly, if one of the values overflows in sizetype and the
                 range is null, use 1..0 for the sizetype bounds.  */
-             else if (!subrange_p
-                      && TREE_CODE (gnu_min) == INTEGER_CST
+             else if (TREE_CODE (gnu_min) == INTEGER_CST
                       && TREE_CODE (gnu_max) == INTEGER_CST
                       && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
                       && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
@@ -2150,8 +2170,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              /* If the minimum and maximum values both overflow in sizetype,
                 but the difference in the original type does not overflow in
                 sizetype, ignore the overflow indication.  */
-             else if (!subrange_p
-                      && TREE_CODE (gnu_min) == INTEGER_CST
+             else if (TREE_CODE (gnu_min) == INTEGER_CST
                       && TREE_CODE (gnu_max) == INTEGER_CST
                       && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
                       && !TREE_OVERFLOW
@@ -2170,57 +2189,49 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                 deal with the "superflat" case.  There are three ways to do
                 this.  If we can prove that the array can never be superflat,
                 we can just use the high bound of the index type.  */
-             else if (Nkind (gnat_index) == N_Range
-                      && cannot_be_superflat_p (gnat_index))
+             else if ((Nkind (gnat_index) == N_Range
+                       && cannot_be_superflat_p (gnat_index))
+                      /* Packed Array Types are never superflat.  */
+                      || Is_Packed_Array_Type (gnat_entity))
                gnu_high = gnu_max;
 
-             /* Otherwise, if we can prove that the low bound minus one and
-                the high bound cannot overflow, we can just use the expression
-                MAX (hb, lb - 1).  Similarly, if we can prove that the high
-                bound plus one and the low bound cannot overflow, we can use
-                the high bound as-is and MIN (hb + 1, lb) for the low bound.
-                Otherwise, we have to fall back to the most general expression
-                (hb >= lb) ? hb : lb - 1.  Note that the comparison must be
-                done in the original index type, to avoid any overflow during
-                the conversion.  */
-             else
+             /* Otherwise, if the high bound is constant but the low bound is
+                not, we use the expression (hb >= lb) ? lb : hb + 1 for the
+                lower bound.  Note that the comparison must be done in the
+                original type to avoid any overflow during the conversion.  */
+             else if (TREE_CODE (gnu_max) == INTEGER_CST
+                      && TREE_CODE (gnu_min) != INTEGER_CST)
                {
-                 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
-                 gnu_low = size_binop (PLUS_EXPR, gnu_max, size_one_node);
-
-                 /* If gnu_high is a constant that has overflowed, the low
-                    bound is the smallest integer so cannot be the maximum.
-                    If gnu_low is a constant that has overflowed, the high
-                    bound is the highest integer so cannot be the minimum.  */
-                 if ((TREE_CODE (gnu_high) == INTEGER_CST
-                      && TREE_OVERFLOW (gnu_high))
-                     || (TREE_CODE (gnu_low) == INTEGER_CST
-                          && TREE_OVERFLOW (gnu_low)))
-                   gnu_high = gnu_max;
-
-                 /* If the index type is a subrange and gnu_high a constant
-                    that hasn't overflowed, we can use the maximum.  */
-                 else if (subrange_p && TREE_CODE (gnu_high) == INTEGER_CST)
-                   gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
-
-                 /* If the index type is a subrange and gnu_low a constant
-                    that hasn't overflowed, we can use the minimum.  */
-                 else if (subrange_p && TREE_CODE (gnu_low) == INTEGER_CST)
-                   {
-                     gnu_high = gnu_max;
-                     gnu_min = size_binop (MIN_EXPR, gnu_min, gnu_low);
-                   }
-
-                 else
-                   gnu_high
-                     = build_cond_expr (sizetype,
-                                        build_binary_op (GE_EXPR,
-                                                         integer_type_node,
-                                                         gnu_orig_max,
-                                                         gnu_orig_min),
-                                        gnu_max, gnu_high);
+                 gnu_high = gnu_max;
+                 gnu_min
+                   = build_cond_expr (sizetype,
+                                      build_binary_op (GE_EXPR,
+                                                       boolean_type_node,
+                                                       gnu_orig_max,
+                                                       gnu_orig_min),
+                                      gnu_min,
+                                      size_binop (PLUS_EXPR, gnu_max,
+                                                  size_one_node));
                }
 
+             /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
+                in all the other cases.  Note that, here as well as above,
+                the condition used in the comparison must be equivalent to
+                the condition (length != 0).  This is relied upon in order
+                to optimize array comparisons in compare_arrays.  */
+             else
+               gnu_high
+                 = build_cond_expr (sizetype,
+                                    build_binary_op (GE_EXPR,
+                                                     boolean_type_node,
+                                                     gnu_orig_max,
+                                                     gnu_orig_min),
+                                    gnu_max,
+                                    size_binop (MINUS_EXPR, gnu_min,
+                                                size_one_node));
+
+             /* Reuse the index type for the range type.  Then make an index
+                type with the size range in sizetype.  */
              gnu_index_types[index]
                = create_index_type (gnu_min, gnu_high, gnu_index_type,
                                     gnat_entity);
@@ -2290,7 +2301,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                      && TREE_CODE (TREE_TYPE (gnu_index_type))
                         != INTEGER_TYPE)
                  || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
-                 || wider_p)
+                 || compare_tree_int (rm_size (gnu_index_type),
+                                      TYPE_PRECISION (sizetype)) > 0)
                need_index_type_struct = true;
            }
 
@@ -2357,35 +2369,30 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
             inner dimensions.   */
          if (global_bindings_p () && ndim > 1)
            {
-             tree gnu_str_name = get_identifier ("ST");
+             tree gnu_st_name = get_identifier ("ST");
              tree gnu_arr_type;
 
              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_name (gnu_str_name, "ST"))
+                  gnu_st_name = concat_name (gnu_st_name, "ST"))
                {
                  tree eltype = TREE_TYPE (gnu_arr_type);
 
                  TYPE_SIZE (gnu_arr_type)
                    = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
-                                             gnat_entity, gnu_str_name,
+                                             gnat_entity, gnu_st_name,
                                              definition, false);
 
                  /* ??? For now, store the size as a multiple of the
                     alignment of the element type in bytes so that we
                     can see the alignment from the tree.  */
                  TYPE_SIZE_UNIT (gnu_arr_type)
-                   = build_binary_op
-                     (MULT_EXPR, sizetype,
-                      elaborate_expression_1
-                      (build_binary_op (EXACT_DIV_EXPR, sizetype,
-                                        TYPE_SIZE_UNIT (gnu_arr_type),
-                                        size_int (TYPE_ALIGN (eltype)
-                                                  / BITS_PER_UNIT)),
-                       gnat_entity, concat_name (gnu_str_name, "A_U"),
-                       definition, false),
-                      size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
+                   = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
+                                             gnat_entity,
+                                             concat_name (gnu_st_name, "A_U"),
+                                             definition, false,
+                                             TYPE_ALIGN (eltype));
 
                  /* ??? create_type_decl is not invoked on the inner types so
                     the MULT_EXPR node built above will never be marked.  */
@@ -2419,8 +2426,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  /* 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);
+                                                gnu_bound_rec, NULL_TREE,
+                                                NULL_TREE, 0, 0);
                  TREE_CHAIN (gnu_field) = gnu_field_list;
                  gnu_field_list = gnu_field;
                }
@@ -2851,13 +2858,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
            /* ...and reference the _Parent field of this record.  */
            gnu_field
-             = create_field_decl (get_identifier
-                                  (Get_Name_String (Name_uParent)),
+             = create_field_decl (parent_name_id,
                                   gnu_parent, gnu_type, 0,
                                   has_rep
                                   ? TYPE_SIZE (gnu_parent) : NULL_TREE,
                                   has_rep
-                                  ? bitsize_zero_node : NULL_TREE, 1);
+                                  ? bitsize_zero_node : NULL_TREE,
+                                  0, 1);
            DECL_INTERNAL_P (gnu_field) = 1;
            TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
            TYPE_FIELDS (gnu_type) = gnu_field;
@@ -2926,6 +2933,21 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              && Is_Itype (Etype (gnat_temp))
              && !present_gnu_tree (gnat_temp))
            gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
+
+       /* If this is a record type associated with an exception definition,
+          equate its fields to those of the standard exception type.  This
+          will make it possible to convert between them.  */
+       if (gnu_entity_name == exception_data_name_id)
+         {
+           tree gnu_std_field;
+           for (gnu_field = TYPE_FIELDS (gnu_type),
+                gnu_std_field = TYPE_FIELDS (except_type_node);
+                gnu_field;
+                gnu_field = TREE_CHAIN (gnu_field),
+                gnu_std_field = TREE_CHAIN (gnu_std_field))
+             SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
+           gcc_assert (!gnu_std_field);
+         }
       }
       break;
 
@@ -2981,6 +3003,20 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
              break;
            }
 
+         /* If this is a record subtype associated with a dispatch table,
+            strip the suffix.  This is necessary to make sure 2 different
+            subtypes associated with the imported and exported views of a
+            dispatch table are properly merged in LTO mode.  */
+         if (Is_Dispatch_Table_Entity (gnat_entity))
+           {
+             char *p;
+             Get_Encoded_Name (gnat_entity);
+             p = strchr (Name_Buffer, '_');
+             gcc_assert (p);
+             strcpy (p+2, "dtS");
+             gnu_entity_name = get_identifier (Name_Buffer);
+           }
+
          /* When the subtype has discriminants and these discriminants affect
             the initial shape it has inherited, factor them in.  But for an
             Unchecked_Union (it must be an Itype), just return the type.
@@ -3254,8 +3290,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                                         build_reference_type
                                                         (gnu_unpad_base_type),
                                                         gnu_subtype_marker,
-                                                        0, NULL_TREE,
-                                                        NULL_TREE, 0),
+                                                        NULL_TREE, NULL_TREE,
+                                                        0, 0),
                                      0, true);
 
                  add_parallel_type (TYPE_STUB_DECL (gnu_type),
@@ -3264,9 +3300,12 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                  if (definition
                      && TREE_CODE (gnu_size_unit) != INTEGER_CST
                      && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
-                   create_var_decl (create_concat_name (gnat_entity, "XVZ"),
-                                    NULL_TREE, sizetype, gnu_size_unit, false,
-                                    false, false, false, NULL, gnat_entity);
+                   TYPE_SIZE_UNIT (gnu_subtype_marker)
+                     = create_var_decl (create_concat_name (gnat_entity,
+                                                            "XVZ"),
+                                        NULL_TREE, sizetype, gnu_size_unit,
+                                        false, false, false, false, NULL,
+                                        gnat_entity);
                }
 
              /* Now we can finalize it.  */
@@ -3345,13 +3384,14 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
     case E_Anonymous_Access_Type:
     case E_General_Access_Type:
       {
+       /* The designated type and its equivalent type for gigi.  */
        Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
        Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
+       /* Whether it comes from a limited with.  */
        bool is_from_limited_with
          = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
             && From_With_Type (gnat_desig_equiv));
-
-       /* Get the "full view" of this entity.  If this is an incomplete
+       /* The "full view" of the designated type.  If this is an incomplete
           entity from a limited with, treat its non-limited view as the full
           view.  Otherwise, if this is an incomplete or private type, use the
           full view.  In the former case, we might point to a private type,
@@ -3359,7 +3399,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           actual type used for the representation, so this takes a total of
           three steps.  */
        Entity_Id gnat_desig_full_direct_first
-         = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
+         = (is_from_limited_with
+            ? Non_Limited_View (gnat_desig_equiv)
             : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
                ? Full_View (gnat_desig_equiv) : Empty));
        Entity_Id gnat_desig_full_direct
@@ -3370,27 +3411,25 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
             : gnat_desig_full_direct_first);
        Entity_Id gnat_desig_full
          = Gigi_Equivalent_Type (gnat_desig_full_direct);
-
-       /* This the type actually used to represent the designated type,
-          either gnat_desig_full or gnat_desig_equiv.  */
+       /* The type actually used to represent the designated type, either
+          gnat_desig_full or gnat_desig_equiv.  */
        Entity_Id gnat_desig_rep;
-
        /* 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
           incomplete type we may be pointing to.  */
        bool in_main_unit
          = (Present (gnat_desig_full)
             ? In_Extended_Main_Code_Unit (gnat_desig_full)
             : In_Extended_Main_Code_Unit (gnat_desig_type));
-
        /* True if we make a dummy type here.  */
-       bool got_fat_p = false;
-       /* True if the dummy is a fat pointer.  */
        bool made_dummy = false;
-       tree gnu_desig_type = NULL_TREE;
+       /* True if the dummy type is a fat pointer.  */
+       bool got_fat_p = false;
+       /* The mode to be used for the pointer type.  */
        enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
+       /* The GCC type used for the designated type.  */
+       tree gnu_desig_type = NULL_TREE;
 
        if (!targetm.valid_pointer_mode (p_mode))
          p_mode = ptr_mode;
@@ -3403,22 +3442,21 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
           issues.  This can lose some code efficiency, but there is no
           alternative.  */
        if (Ekind (gnat_desig_equiv) == E_Array_Subtype
-           && ! Is_Constrained (gnat_desig_equiv))
+           && !Is_Constrained (gnat_desig_equiv))
          gnat_desig_equiv = Etype (gnat_desig_equiv);
        if (Present (gnat_desig_full)
            && ((Ekind (gnat_desig_full) == E_Array_Subtype
-                && ! Is_Constrained (gnat_desig_full))
+                && !Is_Constrained (gnat_desig_full))
                || (Ekind (gnat_desig_full) == E_Record_Subtype
                    && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
          gnat_desig_full = Etype (gnat_desig_full);
 
-       /* Now set the type that actually marks the representation of
-          the designated type and also flag whether we have a unconstrained
-          array.  */
-       gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
+       /* Set the type that's actually the representation of the designated
+          type and also flag whether we have a unconstrained array.  */
+       gnat_desig_rep
+         = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
        is_unconstrained_array
-         = (Is_Array_Type (gnat_desig_rep)
-            && ! Is_Constrained (gnat_desig_rep));
+         = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
 
        /* If we are pointing to an incomplete type whose completion is an
           unconstrained array, make a fat pointer type.  The two types in our
@@ -3429,31 +3467,28 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (is_unconstrained_array
            && (Present (gnat_desig_full)
                || (present_gnu_tree (gnat_desig_equiv)
-                   && TYPE_IS_DUMMY_P (TREE_TYPE
-                                       (get_gnu_tree (gnat_desig_equiv))))
-               || (No (gnat_desig_full) && ! in_main_unit
-                   && defer_incomplete_level != 0
-                   && ! present_gnu_tree (gnat_desig_equiv))
-               || (in_main_unit && is_from_limited_with
-                   && Present (Freeze_Node (gnat_desig_rep)))))
+                   && TYPE_IS_DUMMY_P
+                      (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
+               || (!in_main_unit
+                   && defer_incomplete_level
+                   && !present_gnu_tree (gnat_desig_equiv))
+               || (in_main_unit
+                   && is_from_limited_with
+                   && Present (Freeze_Node (gnat_desig_equiv)))))
          {
-           tree gnu_old;
-
            if (present_gnu_tree (gnat_desig_rep))
-             gnu_old = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
+             gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
            else
              {
-               gnu_old = make_dummy_type (gnat_desig_rep);
-
+               gnu_desig_type = 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
-              because the type was elaborated or because somebody
-              else executed the code below.  */
-           gnu_type = TYPE_POINTER_TO (gnu_old);
+           /* If the call above got something that has a pointer, the pointer
+              is our type.  This could have happened either because the type
+              was elaborated or because somebody else executed the code.  */
+           gnu_type = TYPE_POINTER_TO (gnu_desig_type);
            if (!gnu_type)
              {
                tree gnu_template_type = make_node (ENUMERAL_TYPE);
@@ -3471,19 +3506,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                TYPE_DUMMY_P (gnu_array_type) = 1;
 
                gnu_type = make_node (RECORD_TYPE);
-               SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
-               TYPE_POINTER_TO (gnu_old) = gnu_type;
+               SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_desig_type);
+               TYPE_POINTER_TO (gnu_desig_type) = gnu_type;
 
-               Sloc_to_locus (Sloc (gnat_entity), &input_location);
                fields
-                 = chainon (chainon (NULL_TREE,
-                                     create_field_decl
-                                     (get_identifier ("P_ARRAY"),
-                                      gnu_ptr_array,
-                                      gnu_type, 0, 0, 0, 0)),
-                            create_field_decl (get_identifier ("P_BOUNDS"),
-                                               gnu_ptr_template,
-                                               gnu_type, 0, 0, 0, 0));
+                 = create_field_decl (get_identifier ("P_ARRAY"),
+                                      gnu_ptr_array, gnu_type,
+                                      NULL_TREE, NULL_TREE, 0, 0);
+               TREE_CHAIN (fields)
+                 = create_field_decl (get_identifier ("P_BOUNDS"),
+                                      gnu_ptr_template, gnu_type,
+                                      NULL_TREE, NULL_TREE, 0, 0);
 
                /* Make sure we can place this into a register.  */
                TYPE_ALIGN (gnu_type)
@@ -3494,10 +3527,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   of its fields are incomplete.  */
                finish_record_type (gnu_type, fields, 0, false);
 
-               TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
-               TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
+               TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)
+                 = make_node (RECORD_TYPE);
+               TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type))
                  = create_concat_name (gnat_desig_equiv, "XUT");
-               TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
+               TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)) = 1;
              }
          }
 
@@ -3506,35 +3540,35 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                 && present_gnu_tree (gnat_desig_full))
          gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
 
-       /* Get the type of the thing we are to point to and build a pointer
-          to it.  If it is a reference to an incomplete or private type with a
+       /* Get the type of the thing we are to point to and build a pointer to
+          it.  If it is a reference to an incomplete or private type with a
           full view that is a record, make a dummy type node and get the
           actual type later when we have verified it is safe.  */
-       else if ((! in_main_unit
-                 && ! present_gnu_tree (gnat_desig_equiv)
+       else if ((!in_main_unit
+                 && !present_gnu_tree (gnat_desig_equiv)
                  && Present (gnat_desig_full)
-                 && ! present_gnu_tree (gnat_desig_full)
+                 && !present_gnu_tree (gnat_desig_full)
                  && Is_Record_Type (gnat_desig_full))
-                /* Likewise if we are pointing to a record or array and we
-                   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.  */
-                || ((! in_main_unit || imported_p)
-                    && defer_incomplete_level != 0
-                    && ! present_gnu_tree (gnat_desig_equiv)
-                    && ((Is_Record_Type (gnat_desig_rep)
-                         || Is_Array_Type (gnat_desig_rep))))
+                /* Likewise if we are pointing to a record or array and we are
+                   to defer elaborating incomplete types.  We do this as this
+                   access type may be the full view of a private type.  Note
+                   that the unconstrained array case is handled above.  */
+                || ((!in_main_unit || imported_p)
+                    && defer_incomplete_level
+                    && !present_gnu_tree (gnat_desig_equiv)
+                    && (Is_Record_Type (gnat_desig_rep)
+                        || Is_Array_Type (gnat_desig_rep)))
                 /* If this is a reference from a limited_with type back to our
-                   main unit and there's a Freeze_Node for it, either we have
+                   main unit and there's a freeze node for it, either we have
                    already processed the declaration and made the dummy type,
                    in which case we just reuse the latter, or we have not yet,
                    in which case we make the dummy type and it will be reused
-                   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
+                   when the declaration is finally 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.  */
-                ||  (in_main_unit && is_from_limited_with
+                ||  (in_main_unit
+                     && is_from_limited_with
                      && Present (Freeze_Node (gnat_desig_rep))))
          {
            gnu_desig_type = make_dummy_type (gnat_desig_equiv);
@@ -3550,13 +3584,13 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
          }
 
-       /* If expansion is disabled, the equivalent type of a concurrent
-          type is absent, so build a dummy pointer type.  */
+       /* If expansion is disabled, the equivalent type of a concurrent type
+          is absent, so build a dummy pointer type.  */
        else if (type_annotate_only && No (gnat_desig_equiv))
          gnu_type = ptr_void_type_node;
 
-       /* Finally, handle the straightforward case where we can just
-          elaborate our designated type and point to it.  */
+       /* Finally, handle the default case where we can just elaborate our
+          designated type.  */
        else
          gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
 
@@ -3568,11 +3602,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            break;
          }
 
-       /* If we have a GCC type for the designated type, possibly modify it
-          if we are pointing only to constant objects and then make a pointer
-          to it.  Don't do this for unconstrained arrays.  */
-       if (!gnu_type && gnu_desig_type)
+       /* If we have not done it yet, build the pointer type the usual way.  */
+       if (!gnu_type)
          {
+           /* Modify the designated type if we are pointing only to constant
+              objects, but don't do it for unconstrained arrays.  */
            if (Is_Access_Constant (gnat_entity)
                && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
              {
@@ -3611,17 +3645,20 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                             No_Strict_Aliasing (gnat_entity));
          }
 
-       /* If we are not defining this object and we made a dummy pointer,
+       /* If we are not defining this object and we have made a dummy pointer,
           save our current definition, evaluate the actual type, and replace
           the tentative type we made with the actual one.  If we are to defer
-          actually looking up the actual type, make an entry in the
-          deferred list.  If this is from a limited with, we have to defer
-          to the end of the current spec in two cases: first if the
-          designated type is in the current unit and second if the access
-          type is.  */
-       if ((! in_main_unit || is_from_limited_with) && made_dummy)
+          actually looking up the actual type, make an entry in the deferred
+          list.  If this is from a limited with, we have to defer to the end
+          of the current spec in two cases: first if the designated type is
+          in the current unit and second if the access type itself is.  */
+       if ((!in_main_unit || is_from_limited_with) && made_dummy)
          {
-           tree gnu_old_type
+           bool is_from_limited_with_in_main_unit
+             = (is_from_limited_with
+                && (in_main_unit
+                    || In_Extended_Main_Code_Unit (gnat_entity)));
+           tree gnu_old_desig_type
              = TYPE_IS_FAT_POINTER_P (gnu_type)
                ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
 
@@ -3640,37 +3677,27 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            save_gnu_tree (gnat_entity, gnu_decl, false);
            saved = true;
 
-           if (defer_incomplete_level == 0
-               && ! (is_from_limited_with
-                     && (in_main_unit
-                         || In_Extended_Main_Code_Unit (gnat_entity))))
-             update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
-                                gnat_to_gnu_type (gnat_desig_equiv));
-
-             /* Note that the call to gnat_to_gnu_type here might have
-                updated gnu_old_type directly, in which case it is not a
-                dummy type any more when we get into update_pointer_to.
+           /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
+              update gnu_old_desig_type directly, in which case it will not be
+              a dummy type any more when we get into update_pointer_to.
 
-                This may happen for instance when the designated type is a
-                record type, because their elaboration starts with an
-                initial node from make_dummy_type, which may yield the same
-                node as the one we got.
+              This can happen e.g. when the designated type is a record type,
+              because their elaboration starts with an initial node from
+              make_dummy_type, which may be the same 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
-                properly take care of those situations.  */
+              Besides, variants of this non-dummy type might have been created
+              along the way.  update_pointer_to is expected to properly take
+              care of those situations.  */
+           if (!defer_incomplete_level && !is_from_limited_with_in_main_unit)
+             update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
+                                gnat_to_gnu_type (gnat_desig_equiv));
            else
              {
-               struct incomplete *p
-                 = (struct incomplete *) xmalloc (sizeof
-                                                  (struct incomplete));
+               struct incomplete *p = XNEW (struct incomplete);
                struct incomplete **head
-                 = (is_from_limited_with
-                    && (in_main_unit
-                        || In_Extended_Main_Code_Unit (gnat_entity))
+                 = (is_from_limited_with_in_main_unit
                     ? &defer_limited_with : &defer_incomplete_list);
-
-               p->old_type = gnu_old_type;
+               p->old_type = gnu_old_desig_type;
                p->full_type = gnat_desig_equiv;
                p->next = *head;
                *head = p;
@@ -4092,8 +4119,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                    has_copy_in_out = true;
                  }
 
-               gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
-                                              gnu_return_type, 0, 0, 0, 0);
+               gnu_field
+                 = create_field_decl (gnu_param_name, gnu_param_type,
+                                      gnu_return_type, NULL_TREE, NULL_TREE,
+                                      0, 0);
                Sloc_to_locus (Sloc (gnat_param),
                               &DECL_SOURCE_LOCATION (gnu_field));
                TREE_CHAIN (gnu_field) = gnu_field_list;
@@ -4171,8 +4200,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                  | (TYPE_QUAL_CONST * const_flag)
                                  | (TYPE_QUAL_VOLATILE * volatile_flag));
 
-       Sloc_to_locus (Sloc (gnat_entity), &input_location);
-
        if (has_stub)
          gnu_stub_type
            = build_qualified_type (gnu_stub_type,
@@ -4344,9 +4371,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        break;
       }
 
-      /* Simple class_wide types are always viewed as their root_type
-        by Gigi unless an Equivalent_Type is specified.  */
     case E_Class_Wide_Type:
+      /* Class-wide types are always transformed into their root type.  */
       gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
       maybe_present = true;
       break;
@@ -4392,6 +4418,10 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
      handling alignment and possible padding.  */
   if (is_type && (!gnu_decl || this_made_decl))
     {
+      /* Tell the middle-end that objects of tagged types are guaranteed to
+        be properly aligned.  This is necessary because conversions to the
+        class-wide type are translated into conversions to the root type,
+        which can be less aligned than some of its derived types.  */
       if (Is_Tagged_Type (gnat_entity)
          || Is_Class_Wide_Equivalent_Type (gnat_entity))
        TYPE_ALIGN_OK (gnu_type) = 1;
@@ -4496,45 +4526,92 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
          && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
        {
-         if (TREE_CODE (gnu_type) == RECORD_TYPE
-             && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
-                                 TYPE_SIZE (gnu_type), 0))
-           {
-             TYPE_SIZE (gnu_type)
-               = 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
+         tree size = TYPE_SIZE (gnu_type);
+
+         TYPE_SIZE (gnu_type)
+           = elaborate_expression_1 (size, 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.  */
+         TYPE_SIZE_UNIT (gnu_type)
+           = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
+                                     get_identifier ("SIZE_A_UNIT"),
+                                     definition, false,
+                                     TYPE_ALIGN (gnu_type));
+
+         /* ??? gnu_type may come from an existing type so the MULT_EXPR node
+            may not be marked by the call to create_type_decl below.  */
+         MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
+
+         if (TREE_CODE (gnu_type) == RECORD_TYPE)
            {
-             TYPE_SIZE (gnu_type)
-               = elaborate_expression_1 (TYPE_SIZE (gnu_type),
-                                         gnat_entity, get_identifier ("SIZE"),
-                                         definition, false);
+             tree variant_part = get_variant_part (gnu_type);
+             tree ada_size = TYPE_ADA_SIZE (gnu_type);
 
-             /* ??? For now, store the size as a multiple of the alignment
-                in bytes so that we can see the alignment from the tree.  */
-             TYPE_SIZE_UNIT (gnu_type)
-               = build_binary_op
-                 (MULT_EXPR, sizetype,
-                  elaborate_expression_1
-                  (build_binary_op (EXACT_DIV_EXPR, sizetype,
-                                    TYPE_SIZE_UNIT (gnu_type),
-                                    size_int (TYPE_ALIGN (gnu_type)
-                                              / BITS_PER_UNIT)),
-                   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 (TYPE_ADA_SIZE (gnu_type),
-                                          gnat_entity,
-                                          get_identifier ("RM_SIZE"),
-                                          definition, false));
-                }
+             if (variant_part)
+               {
+                 tree union_type = TREE_TYPE (variant_part);
+                 tree offset = DECL_FIELD_OFFSET (variant_part);
+
+                 /* If the position of the variant part is constant, subtract
+                    it from the size of the type of the parent to get the new
+                    size.  This manual CSE reduces the data size.  */
+                 if (TREE_CODE (offset) == INTEGER_CST)
+                   {
+                     tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
+                     TYPE_SIZE (union_type)
+                       = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
+                                     bit_from_pos (offset, bitpos));
+                     TYPE_SIZE_UNIT (union_type)
+                       = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
+                                     byte_from_pos (offset, bitpos));
+                   }
+                 else
+                   {
+                     TYPE_SIZE (union_type)
+                       = elaborate_expression_1 (TYPE_SIZE (union_type),
+                                                 gnat_entity,
+                                                 get_identifier ("VSIZE"),
+                                                 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.  */
+                     TYPE_SIZE_UNIT (union_type)
+                       = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
+                                                 gnat_entity,
+                                                 get_identifier
+                                                 ("VSIZE_A_UNIT"),
+                                                 definition, false,
+                                                 TYPE_ALIGN (union_type));
+
+                     /* ??? For now, store the offset as a multiple of the
+                        alignment in bytes so that we can see the alignment
+                        from the tree.  */
+                     DECL_FIELD_OFFSET (variant_part)
+                       = elaborate_expression_2 (offset,
+                                                 gnat_entity,
+                                                 get_identifier ("VOFFSET"),
+                                                 definition, false,
+                                                 DECL_OFFSET_ALIGN
+                                                 (variant_part));
+                   }
+
+                 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
+                 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
+               }
+
+             if (operand_equal_p (ada_size, size, 0))
+               ada_size = TYPE_SIZE (gnu_type);
+             else
+               ada_size
+                 = elaborate_expression_1 (ada_size, gnat_entity,
+                                           get_identifier ("RM_SIZE"),
+                                           definition, false);
+             SET_TYPE_ADA_SIZE (gnu_type, ada_size);
+           }
        }
 
       /* If this is a record type or subtype, call elaborate_expression_1 on
@@ -4548,30 +4625,22 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              tree gnu_field = get_gnu_tree (gnat_temp);
 
-             /* ??? Unfortunately, GCC needs to be able to prove the
-                alignment of this offset and if it's a variable, it can't.
-                In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
-                right now, we have to put in an explicit multiply and
-                divide by that value.  */
+             /* ??? For now, store the offset as a multiple of the alignment
+                in bytes so that we can see the alignment from the tree.  */
              if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
                {
-               DECL_FIELD_OFFSET (gnu_field)
-                 = build_binary_op
-                   (MULT_EXPR, sizetype,
-                    elaborate_expression_1
-                    (build_binary_op (EXACT_DIV_EXPR, sizetype,
-                                      DECL_FIELD_OFFSET (gnu_field),
-                                      size_int (DECL_OFFSET_ALIGN (gnu_field)
-                                                / BITS_PER_UNIT)),
-                     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
-                  the MULT_EXPR node built above may not be marked by the call
-                  to create_type_decl below.  */
-               if (global_bindings_p ())
-                 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
+                 DECL_FIELD_OFFSET (gnu_field)
+                   = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
+                                             gnat_temp,
+                                             get_identifier ("OFFSET"),
+                                             definition, false,
+                                             DECL_OFFSET_ALIGN (gnu_field));
+
+                 /* ??? The context of gnu_field is not necessarily gnu_type
+                    so the MULT_EXPR node built above may not be marked by
+                    the call to create_type_decl below.  */
+                 if (global_bindings_p ())
+                   MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
                }
            }
 
@@ -4703,38 +4772,40 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
 
       if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
        {
-         /* If the size is self-referential, we annotate the maximum
-            value of that size.  */
          tree gnu_size = TYPE_SIZE (gnu_type);
 
+         /* If the size is self-referential, annotate the maximum value.  */
          if (CONTAINS_PLACEHOLDER_P (gnu_size))
            gnu_size = max_size (gnu_size, true);
 
-         Set_Esize (gnat_entity, annotate_value (gnu_size));
-
          if (type_annotate_only && Is_Tagged_Type (gnat_entity))
            {
-             /* In this mode the tag and the parent components are not
-                generated by the front-end, so the sizes must be adjusted
-                explicitly now.  */
-             int size_offset, new_size;
+             /* In this mode, the tag and the parent components are not
+                generated by the front-end so the sizes must be adjusted.  */
+             tree pointer_size = bitsize_int (POINTER_SIZE), offset;
+             Uint uint_size;
 
              if (Is_Derived_Type (gnat_entity))
                {
-                 size_offset
-                   = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
+                 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
+                                     bitsizetype);
                  Set_Alignment (gnat_entity,
                                 Alignment (Etype (Base_Type (gnat_entity))));
                }
              else
-               size_offset = POINTER_SIZE;
-
-             new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
-             Set_Esize (gnat_entity,
-                        UI_From_Int (((new_size + (POINTER_SIZE - 1))
-                                      / POINTER_SIZE) * POINTER_SIZE));
-             Set_RM_Size (gnat_entity, Esize (gnat_entity));
+               offset = pointer_size;
+
+             gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
+             gnu_size = size_binop (MULT_EXPR, pointer_size,
+                                               size_binop (CEIL_DIV_EXPR,
+                                                           gnu_size,
+                                                           pointer_size));
+             uint_size = annotate_value (gnu_size);
+             Set_Esize (gnat_entity, uint_size);
+             Set_RM_Size (gnat_entity, uint_size);
            }
+         else
+           Set_Esize (gnat_entity, annotate_value (gnu_size));
        }
 
       if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
@@ -5364,15 +5435,14 @@ compile_time_known_address_p (Node_Id gnat_address)
   return Compile_Time_Known_Value (gnat_address);
 }
 
-/* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e.
-   cannot verify HB < LB-1 when LB and HB are the low and high bounds.  */
+/* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
+   inequality HB >= LB-1 is true.  LB and HB are the low and high bounds.  */
 
 static bool
 cannot_be_superflat_p (Node_Id gnat_range)
 {
   Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
   Node_Id scalar_range;
-
   tree gnu_lb, gnu_hb;
 
   /* If the low bound is not constant, try to find an upper bound.  */
@@ -5393,19 +5463,37 @@ cannot_be_superflat_p (Node_Id gnat_range)
             || Nkind (scalar_range) == N_Range))
     gnat_hb = Low_Bound (scalar_range);
 
-  if (!(Nkind (gnat_lb) == N_Integer_Literal
-       && Nkind (gnat_hb) == N_Integer_Literal))
+  /* If we have failed to find constant bounds, punt.  */
+  if (Nkind (gnat_lb) != N_Integer_Literal
+      || Nkind (gnat_hb) != N_Integer_Literal)
     return false;
 
-  gnu_lb = UI_To_gnu (Intval (gnat_lb), bitsizetype);
-  gnu_hb = UI_To_gnu (Intval (gnat_hb), bitsizetype);
+  /* We need at least a signed 64-bit type to catch most cases.  */
+  gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
+  gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
+  if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
+    return false;
 
   /* If the low bound is the smallest integer, nothing can be smaller.  */
-  gnu_lb = size_binop (MINUS_EXPR, gnu_lb, bitsize_one_node);
-  if (TREE_OVERFLOW (gnu_lb))
+  gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
+  if (TREE_OVERFLOW (gnu_lb_minus_one))
     return true;
 
-  return (tree_int_cst_lt (gnu_hb, gnu_lb) == 0);
+  return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
+}
+
+/* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR.  */
+
+static bool
+constructor_address_p (tree gnu_expr)
+{
+  while (TREE_CODE (gnu_expr) == NOP_EXPR
+        || TREE_CODE (gnu_expr) == CONVERT_EXPR
+        || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
+    gnu_expr = TREE_OPERAND (gnu_expr, 0);
+
+  return (TREE_CODE (gnu_expr) == ADDR_EXPR
+         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
 }
 
 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR.  */
@@ -5868,7 +5956,6 @@ make_aligning_type (tree type, unsigned int align, tree size,
   /* We will be crafting a record type with one field at a position set to be
      the next multiple of ALIGN past record'address + room bytes.  We use a
      record placeholder to express record'address.  */
-
   tree record_type = make_node (RECORD_TYPE);
   tree record = build0 (PLACEHOLDER_EXPR, record_type);
 
@@ -5888,7 +5975,6 @@ make_aligning_type (tree type, unsigned int align, tree size,
 
      Every length is in sizetype bytes there, except "pos" which has to be
      set as a bit position in the GCC tree for the record.  */
-
   tree room_st = size_int (room);
   tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
   tree voffset_st, pos, field;
@@ -5897,19 +5983,17 @@ 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_name (name, "_ALIGN");
+  name = concat_name (name, "ALIGN");
+  TYPE_NAME (record_type) = name;
 
   /* Compute VOFFSET and then POS.  The next byte position multiple of some
      alignment after some address is obtained by "and"ing the alignment minus
      1 with the two's complement of the address.   */
-
   voffset_st = size_binop (BIT_AND_EXPR,
-                          size_diffop (size_zero_node, vblock_addr_st),
-                          ssize_int ((align / BITS_PER_UNIT) - 1));
+                          fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
+                          size_int ((align / BITS_PER_UNIT) - 1));
 
   /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype.  */
-
   pos = size_binop (MULT_EXPR,
                    convert (bitsizetype,
                             size_binop (PLUS_EXPR, room_st, voffset_st)),
@@ -5928,9 +6012,8 @@ make_aligning_type (tree type, unsigned int align, tree size,
      consequences on the alignment computation, and create_field_decl would
      make one without this special argument, for instance because of the
      complex position expression.  */
-
-  field = create_field_decl (get_identifier ("F"), type, record_type,
-                             1, size, pos, -1);
+  field = create_field_decl (get_identifier ("F"), type, record_type, size,
+                            pos, 1, -1);
   TYPE_FIELDS (record_type) = field;
 
   TYPE_ALIGN (record_type) = base_align;
@@ -5946,8 +6029,12 @@ make_aligning_type (tree type, unsigned int align, tree size,
                  size_int (room + align / BITS_PER_UNIT));
 
   SET_TYPE_MODE (record_type, BLKmode);
-
   relate_alias_sets (record_type, type, ALIAS_SET_COPY);
+
+  /* Declare it now since it will never be declared otherwise.  This is
+     necessary to ensure that its subtrees are properly marked.  */
+  create_type_decl (name, record_type, NULL, true, false, Empty);
+
   return record_type;
 }
 \f
@@ -6051,10 +6138,11 @@ make_packable_type (tree type, bool in_record)
       else
        new_size = DECL_SIZE (old_field);
 
-      new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
-                                    new_type, TYPE_PACKED (type), new_size,
-                                    bit_position (old_field),
-                                    !DECL_NONADDRESSABLE_P (old_field));
+      new_field
+       = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
+                            new_size, bit_position (old_field),
+                            TYPE_PACKED (type),
+                            !DECL_NONADDRESSABLE_P (old_field));
 
       DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
       SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
@@ -6218,8 +6306,8 @@ maybe_pad_type (tree type, tree size, unsigned int align,
     }
 
   /* Now create the field with the original size.  */
-  field  = create_field_decl (get_identifier ("F"), type, record, 0,
-                             orig_size, bitsize_zero_node, 1);
+  field  = create_field_decl (get_identifier ("F"), type, record, orig_size,
+                             bitsize_zero_node, 0, 1);
   DECL_INTERNAL_P (field) = 1;
 
   /* Do not emit debug info until after the auxiliary record is built.  */
@@ -6252,16 +6340,17 @@ maybe_pad_type (tree type, tree size, unsigned int align,
       finish_record_type (marker,
                          create_field_decl (orig_name,
                                             build_reference_type (type),
-                                            marker, 0, NULL_TREE, NULL_TREE,
-                                            0),
+                                            marker, NULL_TREE, NULL_TREE,
+                                            0, 0),
                          0, true);
 
       add_parallel_type (TYPE_STUB_DECL (record), marker);
 
       if (definition && size && TREE_CODE (size) != INTEGER_CST)
-       create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
-                        TYPE_SIZE_UNIT (record), false, false, false,
-                        false, NULL, gnat_entity);
+       TYPE_SIZE_UNIT (marker)
+         = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
+                            TYPE_SIZE_UNIT (record), false, false, false,
+                            false, NULL, gnat_entity);
     }
 
   rest_of_record_type_compilation (record);
@@ -6279,10 +6368,13 @@ maybe_pad_type (tree type, tree size, unsigned int align,
   if (Present (gnat_entity)
       && size
       && TREE_CODE (size) != MAX_EXPR
+      && TREE_CODE (size) != COND_EXPR
       && !operand_equal_p (size, orig_size, 0)
       && !(TREE_CODE (size) == INTEGER_CST
           && TREE_CODE (orig_size) == INTEGER_CST
-          && tree_int_cst_lt (size, orig_size)))
+          && (TREE_OVERFLOW (size)
+              || TREE_OVERFLOW (orig_size)
+              || tree_int_cst_lt (size, orig_size))))
     {
       Node_Id gnat_error_node = Empty;
 
@@ -6334,13 +6426,11 @@ choices_to_gnu (tree operand, Node_Id choices)
          low = gnat_to_gnu (Low_Bound (choice));
          high = gnat_to_gnu (High_Bound (choice));
 
-         /* There's no good type to use here, so we might as well use
-            integer_type_node.  */
          this_test
-           = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                              build_binary_op (GE_EXPR, integer_type_node,
+           = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                              build_binary_op (GE_EXPR, boolean_type_node,
                                                operand, low),
-                              build_binary_op (LE_EXPR, integer_type_node,
+                              build_binary_op (LE_EXPR, boolean_type_node,
                                                operand, high));
 
          break;
@@ -6351,10 +6441,10 @@ choices_to_gnu (tree operand, Node_Id choices)
          high = gnat_to_gnu (High_Bound (gnat_temp));
 
          this_test
-           = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                              build_binary_op (GE_EXPR, integer_type_node,
+           = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                              build_binary_op (GE_EXPR, boolean_type_node,
                                                operand, low),
-                              build_binary_op (LE_EXPR, integer_type_node,
+                              build_binary_op (LE_EXPR, boolean_type_node,
                                                operand, high));
          break;
 
@@ -6372,10 +6462,10 @@ choices_to_gnu (tree operand, Node_Id choices)
              high = TYPE_MAX_VALUE (type);
 
              this_test
-               = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
-                                  build_binary_op (GE_EXPR, integer_type_node,
+               = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
+                                  build_binary_op (GE_EXPR, boolean_type_node,
                                                    operand, low),
-                                  build_binary_op (LE_EXPR, integer_type_node,
+                                  build_binary_op (LE_EXPR, boolean_type_node,
                                                    operand, high));
              break;
            }
@@ -6385,7 +6475,7 @@ choices_to_gnu (tree operand, Node_Id choices)
        case N_Character_Literal:
        case N_Integer_Literal:
          single = gnat_to_gnu (choice);
-         this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
+         this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
                                       single);
          break;
 
@@ -6397,8 +6487,8 @@ choices_to_gnu (tree operand, Node_Id choices)
          gcc_unreachable ();
        }
 
-      result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
-                               result, this_test);
+      result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
+                               this_test);
     }
 
   return result;
@@ -6679,9 +6769,9 @@ gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
              || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
 
   /* Now create the decl for the field.  */
-  gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
-                                packed, gnu_size, gnu_pos,
-                                Is_Aliased (gnat_field));
+  gnu_field
+    = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
+                        gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
   Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
   TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
 
@@ -6933,14 +7023,14 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
              create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
                                NULL, true, debug_info_p, gnat_component_list);
 
-             gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
-                                            gnu_union_type, field_packed,
-                                            (all_rep_and_size
-                                             ? TYPE_SIZE (gnu_variant_type)
-                                             : 0),
-                                            (all_rep_and_size
-                                             ? bitsize_zero_node : 0),
-                                            0);
+             gnu_field
+               = create_field_decl (gnu_inner_name, gnu_variant_type,
+                                    gnu_union_type,
+                                    all_rep_and_size
+                                    ? TYPE_SIZE (gnu_variant_type) : 0,
+                                    all_rep_and_size
+                                    ? bitsize_zero_node : 0,
+                                    field_packed, 0);
 
              DECL_INTERNAL_P (gnu_field) = 1;
 
@@ -6987,9 +7077,9 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
 
          gnu_union_field
            = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
-                                union_field_packed,
                                 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
-                                all_rep ? bitsize_zero_node : 0, 0);
+                                all_rep ? bitsize_zero_node : 0,
+                                union_field_packed, 0);
 
          DECL_INTERNAL_P (gnu_union_field) = 1;
          TREE_CHAIN (gnu_union_field) = gnu_field_list;
@@ -7060,7 +7150,7 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
          finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, debug_info_p);
          gnu_field
            = create_field_decl (get_identifier ("REP"), gnu_rep_type,
-                                gnu_record_type, 0, NULL_TREE, NULL_TREE, 1);
+                                gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
          DECL_INTERNAL_P (gnu_field) = 1;
          gnu_field_list = chainon (gnu_field_list, gnu_field);
        }
@@ -7085,12 +7175,10 @@ components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
 static Uint
 annotate_value (tree gnu_size)
 {
-  int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
   TCode tcode;
   Node_Ref_Or_Val ops[3], ret;
-  int i;
-  int size;
   struct tree_int_map **h = NULL;
+  int size, i;
 
   /* See if we've already saved the value for this node.  */
   if (EXPR_P (gnu_size))
@@ -7117,46 +7205,21 @@ annotate_value (tree gnu_size)
       if (TREE_OVERFLOW (gnu_size))
        return No_Uint;
 
-      /* This may have come from a conversion from some smaller type,
-        so ensure this is in bitsizetype.  */
+      /* This may come from a conversion from some smaller type, so ensure
+        this is in bitsizetype.  */
       gnu_size = convert (bitsizetype, gnu_size);
 
-      /* For negative values, use NEGATE_EXPR of the supplied value.  */
-      if (tree_int_cst_sgn (gnu_size) < 0)
+      /* For a negative value, build NEGATE_EXPR of the opposite.  Such values
+        appear in expressions containing aligning patterns.  Note that, since
+        sizetype is sign-extended but nonetheless unsigned, we don't directly
+        use tree_int_cst_sgn.  */
+      if (TREE_INT_CST_HIGH (gnu_size) < 0)
        {
-         /* The ridiculous code below is to handle the case of the largest
-            negative integer.  */
-         tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
-         bool adjust = false;
-         tree temp;
-
-         if (TREE_OVERFLOW (negative_size))
-           {
-             negative_size
-               = size_binop (MINUS_EXPR, bitsize_zero_node,
-                             size_binop (PLUS_EXPR, gnu_size,
-                                         bitsize_one_node));
-             adjust = true;
-           }
-
-         temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
-         if (adjust)
-           temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
-
-         return annotate_value (temp);
+         tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
+         return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
        }
 
-      if (!host_integerp (gnu_size, 1))
-       return No_Uint;
-
-      size = tree_low_cst (gnu_size, 1);
-
-      /* This peculiar test is to make sure that the size fits in an int
-        on machines where HOST_WIDE_INT is not "int".  */
-      if (tree_low_cst (gnu_size, 1) == size)
-       return UI_From_Int (size);
-      else
-       return No_Uint;
+      return UI_From_gnu (gnu_size);
 
     case COMPONENT_REF:
       /* The only case we handle here is a simple discriminant reference.  */
@@ -7221,7 +7284,7 @@ annotate_value (tree gnu_size)
   for (i = 0; i < 3; i++)
     ops[i] = No_Uint;
 
-  for (i = 0; i < len; i++)
+  for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
     {
       ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
       if (ops[i] == No_Uint)
@@ -7514,13 +7577,9 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   Node_Id gnat_error_node;
   tree type_size, size;
 
-  if (kind == VAR_DECL
-      /* If a type needs strict alignment, a component of this type in
-        a packed record cannot be packed and thus uses the type size.  */
-      || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
-    type_size = TYPE_SIZE (gnu_type);
-  else
-    type_size = rm_size (gnu_type);
+  /* Return 0 if no size was specified.  */
+  if (uint_size == No_Uint)
+    return NULL_TREE;
 
   /* Find the node to use for errors.  */
   if ((Ekind (gnat_object) == E_Component
@@ -7532,25 +7591,22 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   else
     gnat_error_node = gnat_object;
 
-  /* Return 0 if no size was specified, either because Esize was not Present
-     or the specified size was zero.  */
-  if (No (uint_size) || uint_size == No_Uint)
-    return NULL_TREE;
-
   /* Get the size as a tree.  Issue an error if a size was specified but
      cannot be represented in sizetype.  */
   size = UI_To_gnu (uint_size, bitsizetype);
   if (TREE_OVERFLOW (size))
     {
-      post_error_ne (component_p ? "component size of & is too large"
-                    : "size of & is too large",
-                    gnat_error_node, gnat_object);
+      if (component_p)
+       post_error_ne ("component size of & is too large", gnat_error_node,
+                      gnat_object);
+      else
+       post_error_ne ("size of & is too large", gnat_error_node,
+                      gnat_object);
       return NULL_TREE;
     }
 
-  /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size if it is not permitted.  */
-  if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
+  /* Ignore a zero size if it is not permitted.  */
+  if (!zero_ok && integer_zerop (size))
     return NULL_TREE;
 
   /* The size of objects is always a multiple of a byte.  */
@@ -7580,6 +7636,14 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
     size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
 
+  if (kind == VAR_DECL
+      /* If a type needs strict alignment, a component of this type in
+        a packed record cannot be packed and thus uses the type size.  */
+      || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
+    type_size = TYPE_SIZE (gnu_type);
+  else
+    type_size = rm_size (gnu_type);
+
   /* Modify the size of the type to be that of the maximum size if it has a
      discriminant.  */
   if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
@@ -7589,13 +7653,9 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
      by the smallest integral mode that's valid for pointers.  */
   if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
     {
-      enum machine_mode p_mode;
-
-      for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
-          !targetm.valid_pointer_mode (p_mode);
-          p_mode = GET_MODE_WIDER_MODE (p_mode))
-       ;
-
+      enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
+      while (!targetm.valid_pointer_mode (p_mode))
+       p_mode = GET_MODE_WIDER_MODE (p_mode);
       type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
     }
 
@@ -7610,22 +7670,11 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
          ("component size for& too small{, minimum allowed is ^}",
           gnat_error_node, gnat_object, type_size);
       else
-       post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
-                           gnat_error_node, gnat_object, type_size);
-
-      if (kind == VAR_DECL && !component_p
-         && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
-         && !tree_int_cst_lt (size, rm_size (gnu_type)))
-       post_error_ne_tree_2
-         ("\\size of ^ is not a multiple of alignment (^ bits)",
-          gnat_error_node, gnat_object, rm_size (gnu_type),
-          TYPE_ALIGN (gnu_type));
-
-      else if (INTEGRAL_TYPE_P (gnu_type))
-       post_error_ne ("\\size would be legal if & were not aliased!",
-                      gnat_error_node, gnat_object);
+       post_error_ne_tree
+         ("size for& too small{, minimum allowed is ^}",
+          gnat_error_node, gnat_object, type_size);
 
-      return NULL_TREE;
+      size = NULL_TREE;
     }
 
   return size;
@@ -7637,16 +7686,17 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
 static void
 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
 {
+  Node_Id gnat_attr_node;
+  tree old_size, size;
+
+  /* Do nothing if no size was specified.  */
+  if (uint_size == No_Uint)
+    return;
+
   /* Only issue an error if a Value_Size clause was explicitly given.
      Otherwise, we'd be duplicating an error on the Size clause.  */
-  Node_Id gnat_attr_node
+  gnat_attr_node
     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
-  tree old_size = rm_size (gnu_type), size;
-
-  /* Do nothing if no size was specified, either because RM size was not
-     Present or if the specified size was zero.  */
-  if (No (uint_size) || uint_size == No_Uint)
-    return;
 
   /* Get the size as a tree.  Issue an error if a size was specified but
      cannot be represented in sizetype.  */
@@ -7659,17 +7709,17 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
       return;
     }
 
-  /* Ignore a negative size since that corresponds to our back-annotation.
-     Also ignore a zero size unless a Value_Size clause exists, or a size
-     clause exists, or this is an integer type, in which case the front-end
-     will have always set it.  */
-  if (tree_int_cst_sgn (size) < 0
-      || (integer_zerop (size)
-         && No (gnat_attr_node)
-         && !Has_Size_Clause (gnat_entity)
-         && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
+  /* Ignore a zero size unless a Value_Size clause exists, or a size clause
+     exists, or this is an integer type, in which case the front-end will
+     have always set it.  */
+  if (No (gnat_attr_node)
+      && integer_zerop (size)
+      && !Has_Size_Clause (gnat_entity)
+      && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
     return;
 
+  old_size = rm_size (gnu_type);
+
   /* If the old size is self-referential, get the maximum size.  */
   if (CONTAINS_PLACEHOLDER_P (old_size))
     old_size = max_size (old_size, true);
@@ -7683,7 +7733,8 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
               && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
          && !(TYPE_IS_PADDING_P (gnu_type)
               && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
-              && TYPE_PACKED_ARRAY_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_type))))
+              && TYPE_PACKED_ARRAY_TYPE_P
+                 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
          && tree_int_cst_lt (size, old_size)))
     {
       if (Present (gnat_attr_node))
@@ -7760,14 +7811,9 @@ make_type_from_size (tree type, tree size_tree, bool for_biased)
       SET_TYPE_RM_MAX_VALUE (new_type,
                             convert (TREE_TYPE (new_type),
                                      TYPE_MAX_VALUE (type)));
-      /* Propagate the name to avoid creating a fake subrange type.  */
-      if (TYPE_NAME (type))
-       {
-         if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
-           TYPE_NAME (new_type) = DECL_NAME (TYPE_NAME (type));
-         else
-           TYPE_NAME (new_type) = TYPE_NAME (type);
-       }
+      /* Copy the name to show that it's essentially the same type and
+        not a subrange type.  */
+      TYPE_NAME (new_type) = TYPE_NAME (type);
       TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
       SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
       return new_type;
@@ -8038,7 +8084,7 @@ create_field_decl_from (tree old_field, tree field_type, tree record_type,
 
   new_field
     = create_field_decl (DECL_NAME (old_field), field_type, record_type,
-                        DECL_PACKED (old_field), size, new_pos,
+                        size, new_pos, DECL_PACKED (old_field),
                         !DECL_NONADDRESSABLE_P (old_field));
 
   if (!new_pos)
@@ -8106,7 +8152,6 @@ create_variant_part_from (tree old_variant_part, tree variant_list,
                          tree record_type, tree pos_list, tree subst_list)
 {
   tree offset = DECL_FIELD_OFFSET (old_variant_part);
-  tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
   tree old_union_type = TREE_TYPE (old_variant_part);
   tree new_union_type, new_variant_part, t;
   tree union_field_list = NULL_TREE;
@@ -8118,8 +8163,9 @@ create_variant_part_from (tree old_variant_part, tree variant_list,
   /* If the position of the variant part is constant, subtract it from the
      size of the type of the parent to get the new size.  This manual CSE
      reduces the code size when not optimizing.  */
-  if (TREE_CODE (offset) == INTEGER_CST && TREE_CODE (bitpos) == INTEGER_CST)
+  if (TREE_CODE (offset) == INTEGER_CST)
     {
+      tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
       tree first_bit = bit_from_pos (offset, bitpos);
       TYPE_SIZE (new_union_type)
        = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);