OSDN Git Service

* gcc-interface/cuintp.c (UI_To_gnu): Fix long line.
[pf3gnuchains/gcc-fork.git] / gcc / ada / gcc-interface / decl.c
index fba552b..35f7f0a 100644 (file)
@@ -213,6 +213,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
   bool maybe_present = false;
   /* True if we made GNU_DECL and its type here.  */
   bool this_made_decl = false;
+  /* True if debug info is requested for this entity.  */
+  bool debug_info_p = Needs_Debug_Info (gnat_entity);
+  /* True if this entity is to be considered as imported.  */
+  bool imported_p = (Is_Imported (gnat_entity)
+                    && No (Address_Clause (gnat_entity)));
   /* Size and alignment of the GCC node, if meaningful.  */
   unsigned int esize = 0, align = 0;
   /* Contains the list of attributes directly attached to the entity.  */
@@ -561,7 +566,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                   == N_Object_Declaration)
                  && Present (Expression (Declaration_Node (gnat_entity))))
                 || Present (Renamed_Object (gnat_entity))
-                || Is_Imported (gnat_entity)));
+                || imported_p));
        bool inner_const_flag = const_flag;
        bool static_p = Is_Statically_Allocated (gnat_entity);
        bool mutable_p = false;
@@ -582,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)))
          {
@@ -742,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))))
@@ -999,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,
@@ -1303,7 +1317,6 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                                    (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
          static_p = true;
 
-       /* Now create the variable or the constant and set various flags.  */
        gnu_decl
          = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
                             gnu_expr, const_flag, Is_Public (gnat_entity),
@@ -1380,12 +1393,11 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
        if (TREE_CODE (gnu_decl) == CONST_DECL)
          DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
 
-       /* If this object is declared in a block that contains a block with an
-          exception handler, and we aren't using the GCC exception mechanism,
-          we must force this variable in memory in order to avoid an invalid
-          optimization.  */
-       if (Exception_Mechanism != Back_End_Exceptions
-           && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
+       /* If this is declared in a block that contains a block with an
+          exception handler, we must force this variable in memory to
+          suppress an invalid optimization.  */
+       if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
+           && Exception_Mechanism != Back_End_Exceptions)
          TREE_ADDRESSABLE (gnu_decl) = 1;
 
        /* If we are defining an object with variable size or an object with
@@ -1671,9 +1683,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          /* Don't notify the field as "addressable", since we won't be taking
             it's address and it would prevent create_field_decl from making a
             bitfield.  */
-         gnu_field
-           = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
-                                gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
+         gnu_field = create_field_decl (get_identifier ("OBJECT"),
+                                        gnu_field_type, gnu_type, 1,
+                                        NULL_TREE, bitsize_zero_node, 0);
 
          /* Do not emit debug info until after the parallel type is added.  */
          finish_record_type (gnu_type, gnu_field, 2, false);
@@ -1722,9 +1734,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
          /* Don't notify the field as "addressable", since we won't be taking
             it's address and it would prevent create_field_decl from making a
             bitfield.  */
-         gnu_field
-           = create_field_decl (get_identifier ("F"), gnu_field_type,
-                                gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
+         gnu_field = create_field_decl (get_identifier ("F"),
+                                        gnu_field_type, gnu_type, 1,
+                                        NULL_TREE, bitsize_zero_node, 0);
 
          finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
          compute_record_mode (gnu_type);
@@ -2847,7 +2859,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            /* ...and reference the _Parent field of this record.  */
            gnu_field
              = create_field_decl (parent_name_id,
-                                  gnu_parent, gnu_type,
+                                  gnu_parent, gnu_type, 0,
                                   has_rep
                                   ? TYPE_SIZE (gnu_parent) : NULL_TREE,
                                   has_rep
@@ -2898,7 +2910,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
                              debug_info_p, false);
 
        /* If it is passed by reference, force BLKmode to ensure that objects
-          of this type will always be put in memory.  */
++         of this type will always be put in memory.  */
        if (Is_By_Reference_Type (gnat_entity))
          SET_TYPE_MODE (gnu_type, BLKmode);
 
@@ -2921,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;
 
@@ -2984,9 +3011,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
            {
              char *p;
              Get_Encoded_Name (gnat_entity);
-             p = strrchr (Name_Buffer, '_');
+             p = strchr (Name_Buffer, '_');
              gcc_assert (p);
-             strcpy (p+1, "dtS");
+             strcpy (p+2, "dtS");
              gnu_entity_name = get_identifier (Name_Buffer);
            }
 
@@ -3357,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,
@@ -3371,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
@@ -3382,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;
@@ -3415,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
@@ -3441,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);
@@ -3483,18 +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;
 
                fields
-                 = chainon (chainon (NULL_TREE,
-                                     create_field_decl
-                                     (get_identifier ("P_ARRAY"),
+                 = create_field_decl (get_identifier ("P_ARRAY"),
                                       gnu_ptr_array, gnu_type,
-                                      NULL_TREE, NULL_TREE, 0, 0)),
-                            create_field_decl (get_identifier ("P_BOUNDS"),
-                                               gnu_ptr_template, gnu_type,
-                                               NULL_TREE, NULL_TREE, 0, 0));
+                                      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)
@@ -3505,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;
              }
          }
 
@@ -3517,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);
@@ -3561,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);
 
@@ -3579,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)
              {
@@ -3622,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);
 
@@ -3651,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;
@@ -5427,7 +5443,7 @@ 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, gnu_lb_minus_one;
+  tree gnu_lb, gnu_hb;
 
   /* If the low bound is not constant, try to find an upper bound.  */
   while (Nkind (gnat_lb) != N_Integer_Literal
@@ -5479,6 +5495,20 @@ constructor_address_p (tree gnu_expr)
   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.  */
+
+static bool
+constructor_address_p (tree gnu_expr)
+{
+  while (TREE_CODE (gnu_expr) == NOP_EXPR
+        || TREE_CODE (gnu_expr) == CONVERT_EXPR
+        || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
+    gnu_expr = TREE_OPERAND (gnu_expr, 0);
+
+  return (TREE_CODE (gnu_expr) == ADDR_EXPR
+         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
+}
 \f
 /* Given GNAT_ENTITY, elaborate all expressions that are required to
    be elaborated at the point of its definition, but do nothing else.  */
@@ -5913,23 +5943,6 @@ elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
 
   return expr_variable ? gnat_save_expr (gnu_expr) : gnu_expr;
 }
-
-/* Similar, but take an alignment factor and make it explicit in the tree.  */
-
-static tree
-elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
-                       bool definition, bool need_debug, unsigned int align)
-{
-  tree unit_align = size_int (align / BITS_PER_UNIT);
-  return
-    size_binop (MULT_EXPR,
-               elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
-                                                   gnu_expr,
-                                                   unit_align),
-                                       gnat_entity, gnu_name, definition,
-                                       need_debug),
-               unit_align);
-}
 \f
 /* Create a record type that contains a SIZE bytes long field of TYPE with a
    starting bit position so that it is aligned to ALIGN bits, and leaving at
@@ -7165,7 +7178,7 @@ annotate_value (tree gnu_size)
   TCode tcode;
   Node_Ref_Or_Val ops[3], ret;
   struct tree_int_map **h = NULL;
-  int i;
+  int size, i;
 
   /* See if we've already saved the value for this node.  */
   if (EXPR_P (gnu_size))
@@ -7568,10 +7581,6 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
   if (uint_size == No_Uint)
     return NULL_TREE;
 
-  /* Ignore a negative size since that corresponds to our back-annotation.  */
-  if (UI_Lt (uint_size, Uint_0))
-    return NULL_TREE;
-
   /* Find the node to use for errors.  */
   if ((Ekind (gnat_object) == E_Component
        || Ekind (gnat_object) == E_Discriminant)
@@ -7684,10 +7693,6 @@ set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
   if (uint_size == No_Uint)
     return;
 
-  /* Ignore a negative size since that corresponds to our back-annotation.  */
-  if (UI_Lt (uint_size, Uint_0))
-    return;
-
   /* Only issue an error if a Value_Size clause was explicitly given.
      Otherwise, we'd be duplicating an error on the Size clause.  */
   gnat_attr_node