OSDN Git Service

* gcc-interface/gigi.h (gnat_mark_addressable): Rename parameter.
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_aggr.adb
index 3d8265f..6e3edc1 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -28,12 +28,14 @@ with Checks;   use Checks;
 with Debug;    use Debug;
 with Einfo;    use Einfo;
 with Elists;   use Elists;
+with Errout;   use Errout;
 with Expander; use Expander;
 with Exp_Util; use Exp_Util;
 with Exp_Ch3;  use Exp_Ch3;
 with Exp_Ch7;  use Exp_Ch7;
 with Exp_Ch9;  use Exp_Ch9;
 with Exp_Tss;  use Exp_Tss;
+with Fname;    use Fname;
 with Freeze;   use Freeze;
 with Itypes;   use Itypes;
 with Lib;      use Lib;
@@ -46,6 +48,7 @@ with Rident;   use Rident;
 with Rtsfind;  use Rtsfind;
 with Ttypes;   use Ttypes;
 with Sem;      use Sem;
+with Sem_Aux;  use Sem_Aux;
 with Sem_Ch3;  use Sem_Ch3;
 with Sem_Eval; use Sem_Eval;
 with Sem_Res;  use Sem_Res;
@@ -169,12 +172,15 @@ package body Exp_Aggr is
    -- Local Subprograms for Array Aggregate Expansion --
    -----------------------------------------------------
 
-   function Aggr_Size_OK (Typ : Entity_Id) return Boolean;
+   function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
    --  Very large static aggregates present problems to the back-end, and
    --  are transformed into assignments and loops. This function verifies
    --  that the total number of components of an aggregate is acceptable
    --  for transformation into a purely positional static form. It is called
    --  prior to calling Flatten.
+   --  This function also detects and warns about one-component aggregates
+   --  that appear in a non-static context. Even if the component value is
+   --  static, such an aggregate must be expanded into an assignment.
 
    procedure Convert_Array_Aggr_In_Allocator
      (Decl   : Node_Id;
@@ -211,7 +217,7 @@ package body Exp_Aggr is
 
    function Backend_Processing_Possible (N : Node_Id) return Boolean;
    --  This function checks if array aggregate N can be processed directly
-   --  by Gigi. If this is the case True is returned.
+   --  by the backend. If this is the case True is returned.
 
    function Build_Array_Aggr_Code
      (N           : Node_Id;
@@ -291,7 +297,7 @@ package body Exp_Aggr is
    -- Aggr_Size_OK --
    ------------------
 
-   function Aggr_Size_OK (Typ : Entity_Id) return Boolean is
+   function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
       Lo   : Node_Id;
       Hi   : Node_Id;
       Indx : Node_Id;
@@ -300,7 +306,7 @@ package body Exp_Aggr is
       Hiv  : Uint;
 
       --  The following constant determines the maximum size of an
-      --  aggregate produced by converting named to positional
+      --  array aggregate produced by converting named to positional
       --  notation (e.g. from others clauses). This avoids running
       --  away with attempts to convert huge aggregates, which hit
       --  memory limits in the backend.
@@ -322,7 +328,7 @@ package body Exp_Aggr is
       --  The limit is applied to the total number of components that the
       --  aggregate will have, which is the number of static expressions
       --  that will appear in the flattened array. This requires a recursive
-      --  computation of the the number of scalar components of the structure.
+      --  computation of the number of scalar components of the structure.
 
       ---------------------
       -- Component_Count --
@@ -399,6 +405,43 @@ package body Exp_Aggr is
             return True;
          end if;
 
+         --  One-component aggregates are suspicious, and if the context type
+         --  is an object declaration with non-static bounds it will trip gcc;
+         --  such an aggregate must be expanded into a single assignment.
+
+         if Hiv = Lov
+           and then Nkind (Parent (N)) = N_Object_Declaration
+         then
+            declare
+               Index_Type : constant Entity_Id :=
+                              Etype
+                                (First_Index
+                                   (Etype (Defining_Identifier (Parent (N)))));
+               Indx       : Node_Id;
+
+            begin
+               if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
+                  or else not Compile_Time_Known_Value
+                                (Type_High_Bound (Index_Type))
+               then
+                  if Present (Component_Associations (N)) then
+                     Indx :=
+                       First (Choices (First (Component_Associations (N))));
+                     if Is_Entity_Name (Indx)
+                       and then not Is_Type (Entity (Indx))
+                     then
+                        Error_Msg_N
+                          ("single component aggregate in non-static context?",
+                            Indx);
+                        Error_Msg_N ("\maybe subtype name was meant?", Indx);
+                     end if;
+                  end if;
+
+                  return False;
+               end if;
+            end;
+         end if;
+
          declare
             Rng : constant Uint := Hiv - Lov + 1;
 
@@ -464,6 +507,10 @@ package body Exp_Aggr is
    --    9. There cannot be any discriminated record components, since the
    --       back end cannot handle this complex case.
 
+   --   10. No controlled actions need to be generated for components
+
+   --   11. For a VM back end, the array should have no aliased components
+
    function Backend_Processing_Possible (N : Node_Id) return Boolean is
       Typ : constant Entity_Id := Etype (N);
       --  Typ is the correct constrained array subtype of the aggregate
@@ -538,9 +585,16 @@ package body Exp_Aggr is
    --  Start of processing for Backend_Processing_Possible
 
    begin
-      --  Checks 2 (array must not be bit packed)
+      --  Checks 2 (array not bit packed) and 10 (no controlled actions)
+
+      if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
+         return False;
+      end if;
+
+      --  If component is limited, aggregate must be expanded because each
+      --  component assignment must be built in place.
 
-      if Is_Bit_Packed_Array (Typ) then
+      if Is_Inherently_Limited_Type (Component_Type (Typ)) then
          return False;
       end if;
 
@@ -571,7 +625,9 @@ package body Exp_Aggr is
       --    with tagged components, but not clear whether it's worthwhile ???;
       --    in the case of the JVM, object tags are handled implicitly)
 
-      if Is_Tagged_Type (Component_Type (Typ)) and then VM_Target = No_VM then
+      if Is_Tagged_Type (Component_Type (Typ))
+        and then Tagged_Type_Expansion
+      then
          return False;
       end if;
 
@@ -581,6 +637,16 @@ package body Exp_Aggr is
          return False;
       end if;
 
+      --  Checks 11: Array aggregates with aliased components are currently
+      --  not well supported by the VM backend; disable temporarily this
+      --  backend processing until it is definitely supported.
+
+      if VM_Target /= No_VM
+        and then Has_Aliased_Components (Base_Type (Typ))
+      then
+         return False;
+      end if;
+
       --  Backend processing is possible
 
       Set_Size_Known_At_Compile_Time (Etype (N), True);
@@ -925,7 +991,7 @@ package body Exp_Aggr is
          if Present (Flist) then
             F := New_Copy_Tree (Flist);
 
-         elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then
+         elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
             if Is_Entity_Name (Into)
               and then Present (Scope (Entity (Into)))
             then
@@ -1018,16 +1084,14 @@ package body Exp_Aggr is
          --  default initialized components (otherwise Expr_Q is not present).
 
          if Present (Expr_Q)
-           and then (Nkind (Expr_Q) = N_Aggregate
-                     or else Nkind (Expr_Q) = N_Extension_Aggregate)
+           and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
          then
-            --  At this stage the Expression may not have been
-            --  analyzed yet because the array aggregate code has not
-            --  been updated to use the Expansion_Delayed flag and
-            --  avoid analysis altogether to solve the same problem
-            --  (see Resolve_Aggr_Expr). So let us do the analysis of
-            --  non-array aggregates now in order to get the value of
-            --  Expansion_Delayed flag for the inner aggregate ???
+            --  At this stage the Expression may not have been analyzed yet
+            --  because the array aggregate code has not been updated to use
+            --  the Expansion_Delayed flag and avoid analysis altogether to
+            --  solve the same problem (see Resolve_Aggr_Expr). So let us do
+            --  the analysis of non-array aggregates now in order to get the
+            --  value of Expansion_Delayed flag for the inner aggregate ???
 
             if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
                Analyze_And_Resolve (Expr_Q, Comp_Type);
@@ -1063,9 +1127,17 @@ package body Exp_Aggr is
 
          --  Ada 2005 (AI-287): In case of default initialized component, call
          --  the initialization subprogram associated with the component type.
+         --  If the component type is an access type, add an explicit null
+         --  assignment, because for the back-end there is an initialization
+         --  present for the whole aggregate, and no default initialization
+         --  will take place.
+
+         --  In addition, if the component type is controlled, we must call
+         --  its Initialize procedure explicitly, because there is no explicit
+         --  object creation that will invoke it otherwise.
 
          if No (Expr) then
-            if Present (Base_Init_Proc (Etype (Ctype)))
+            if Present (Base_Init_Proc (Base_Type (Ctype)))
               or else Has_Task (Base_Type (Ctype))
             then
                Append_List_To (L,
@@ -1073,26 +1145,41 @@ package body Exp_Aggr is
                    Id_Ref            => Indexed_Comp,
                    Typ               => Ctype,
                    With_Default_Init => True));
+
+            elsif Is_Access_Type (Ctype) then
+               Append_To (L,
+                  Make_Assignment_Statement (Loc,
+                     Name => Indexed_Comp,
+                     Expression => Make_Null (Loc)));
+            end if;
+
+            if Needs_Finalization (Ctype) then
+               Append_List_To (L,
+                 Make_Init_Call (
+                   Ref         => New_Copy_Tree (Indexed_Comp),
+                   Typ         => Ctype,
+                   Flist_Ref   => Find_Final_List (Current_Scope),
+                   With_Attach => Make_Integer_Literal (Loc, 1)));
             end if;
 
          else
             --  Now generate the assignment with no associated controlled
-            --  actions since the target of the assignment may not have
-            --  been initialized, it is not possible to Finalize it as
-            --  expected by normal controlled assignment. The rest of the
-            --  controlled actions are done manually with the proper
-            --  finalization list coming from the context.
+            --  actions since the target of the assignment may not have been
+            --  initialized, it is not possible to Finalize it as expected by
+            --  normal controlled assignment. The rest of the controlled
+            --  actions are done manually with the proper finalization list
+            --  coming from the context.
 
             A :=
               Make_OK_Assignment_Statement (Loc,
                 Name       => Indexed_Comp,
                 Expression => New_Copy_Tree (Expr));
 
-            if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
+            if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
                Set_No_Ctrl_Actions (A);
 
                --  If this is an aggregate for an array of arrays, each
-               --  subaggregate will be expanded as well, and even with
+               --  sub-aggregate will be expanded as well, and even with
                --  No_Ctrl_Actions the assignments of inner components will
                --  require attachment in their assignments to temporaries.
                --  These temporaries must be finalized for each subaggregate,
@@ -1115,12 +1202,12 @@ package body Exp_Aggr is
             Append_To (L, A);
 
             --  Adjust the tag if tagged (because of possible view
-            --  conversions), unless compiling for the Java VM
-            --  where tags are implicit.
+            --  conversions), unless compiling for a VM where
+            --  tags are implicit.
 
             if Present (Comp_Type)
               and then Is_Tagged_Type (Comp_Type)
-              and then VM_Target = No_VM
+              and then Tagged_Type_Expansion
             then
                A :=
                  Make_OK_Assignment_Statement (Loc,
@@ -1147,16 +1234,17 @@ package body Exp_Aggr is
             --  If the component is itself an array of controlled types, whose
             --  value is given by a sub-aggregate, then the attach calls have
             --  been generated when individual subcomponent are assigned, and
-            --  and must not be done again to prevent malformed finalization
-            --  chains (see comments above, concerning the creation of a block
-            --  to hold inner finalization actions).
+            --  must not be done again to prevent malformed finalization chains
+            --  (see comments above, concerning the creation of a block to hold
+            --  inner finalization actions).
 
             if Present (Comp_Type)
-              and then Controlled_Type (Comp_Type)
-              and then
-                (not Is_Array_Type (Comp_Type)
-                   or else not Is_Controlled (Component_Type (Comp_Type))
-                   or else Nkind (Expr) /= N_Aggregate)
+              and then Needs_Finalization (Comp_Type)
+              and then not Is_Limited_Type (Comp_Type)
+              and then not
+                (Is_Array_Type (Comp_Type)
+                   and then Is_Controlled (Component_Type (Comp_Type))
+                   and then Nkind (Expr) = N_Aggregate)
             then
                Append_List_To (L,
                  Make_Adjust_Call (
@@ -1177,6 +1265,12 @@ package body Exp_Aggr is
       function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
          L_J : Node_Id;
 
+         L_L : Node_Id;
+         --  Index_Base'(L)
+
+         L_H : Node_Id;
+         --  Index_Base'(H)
+
          L_Range : Node_Id;
          --  Index_Base'(L) .. Index_Base'(H)
 
@@ -1230,9 +1324,9 @@ package body Exp_Aggr is
          elsif Equal (L, H) then
             return Gen_Assign (New_Copy_Tree (L), Expr);
 
-         --  If H - L <= 2 then generate a sequence of assignments
-         --  when we are processing the bottom most aggregate and it contains
-         --  scalar components.
+         --  If H - L <= 2 then generate a sequence of assignments when we are
+         --  processing the bottom most aggregate and it contains scalar
+         --  components.
 
          elsif No (Next_Index (Index))
            and then Scalar_Comp
@@ -1255,19 +1349,32 @@ package body Exp_Aggr is
 
          L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
 
-         --  Construct "L .. H"
+         --  Construct "L .. H" in Index_Base. We use a qualified expression
+         --  for the bound to convert to the index base, but we don't need
+         --  to do that if we already have the base type at hand.
+
+         if Etype (L) = Index_Base then
+            L_L := L;
+         else
+            L_L :=
+              Make_Qualified_Expression (Loc,
+                Subtype_Mark => Index_Base_Name,
+                Expression   => L);
+         end if;
+
+         if Etype (H) = Index_Base then
+            L_H := H;
+         else
+            L_H :=
+              Make_Qualified_Expression (Loc,
+                Subtype_Mark => Index_Base_Name,
+                Expression   => H);
+         end if;
 
          L_Range :=
-           Make_Range
-             (Loc,
-              Low_Bound  => Make_Qualified_Expression
-                              (Loc,
-                               Subtype_Mark => Index_Base_Name,
-                               Expression   => L),
-              High_Bound => Make_Qualified_Expression
-                              (Loc,
-                               Subtype_Mark => Index_Base_Name,
-                               Expression => H));
+           Make_Range (Loc,
+             Low_Bound => L_L,
+             High_Bound => L_H);
 
          --  Construct "for L_J in Index_Base range L .. H"
 
@@ -1292,9 +1399,9 @@ package body Exp_Aggr is
                           Iteration_Scheme => L_Iteration_Scheme,
                           Statements       => L_Body));
 
-         --  A small optimization: if the aggregate is initialized with a
-         --  box and the component type has no initialization procedure,
-         --  remove the useless empty loop.
+         --  A small optimization: if the aggregate is initialized with a box
+         --  and the component type has no initialization procedure, remove the
+         --  useless empty loop.
 
          if Nkind (First (S)) = N_Loop_Statement
            and then Is_Empty_List (Statements (First (S)))
@@ -1490,11 +1597,23 @@ package body Exp_Aggr is
                  Make_Integer_Literal (Loc, Uint_0))));
       end if;
 
-      --  We can skip this
+      --  If the component type contains tasks, we need to build a Master
+      --  entity in the current scope, because it will be needed if build-
+      --  in-place functions are called in the expanded code.
+
+      if Nkind (Parent (N)) = N_Object_Declaration
+        and then Has_Task (Typ)
+      then
+         Build_Master_Entity (Defining_Identifier (Parent (N)));
+      end if;
+
       --  STEP 1: Process component associations
+
       --  For those associations that may generate a loop, initialize
       --  Loop_Actions to collect inserted actions that may be crated.
 
+      --  Skip this if no component associations
+
       if No (Expressions (N)) then
 
          --  STEP 1 (a): Sort the discrete choices
@@ -1673,10 +1792,11 @@ package body Exp_Aggr is
       Comp_Expr : Node_Id;
       Expr_Q    : Node_Id;
 
-      Internal_Final_List : Node_Id;
+      Internal_Final_List : Node_Id := Empty;
 
       --  If this is an internal aggregate, the External_Final_List is an
       --  expression for the controller record of the enclosing type.
+
       --  If the current aggregate has several controlled components, this
       --  expression will appear in several calls to attach to the finali-
       --  zation list, and it must not be shared.
@@ -1693,15 +1813,15 @@ package body Exp_Aggr is
       --  after the first do nothing.
 
       function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
-      --  Returns the value that the given discriminant of an ancestor
-      --  type should receive (in the absence of a conflict with the
-      --  value provided by an ancestor part of an extension aggregate).
+      --  Returns the value that the given discriminant of an ancestor type
+      --  should receive (in the absence of a conflict with the value provided
+      --  by an ancestor part of an extension aggregate).
 
       procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
-      --  Check that each of the discriminant values defined by the
-      --  ancestor part of an extension aggregate match the corresponding
-      --  values provided by either an association of the aggregate or
-      --  by the constraint imposed by a parent type (RM95-4.3.2(8)).
+      --  Check that each of the discriminant values defined by the ancestor
+      --  part of an extension aggregate match the corresponding values
+      --  provided by either an association of the aggregate or by the
+      --  constraint imposed by a parent type (RM95-4.3.2(8)).
 
       function Compatible_Int_Bounds
         (Agg_Bounds : Node_Id;
@@ -1747,8 +1867,8 @@ package body Exp_Aggr is
          Save_Assoc   : Node_Id := Empty;
 
       begin
-         --  First check any discriminant associations to see if
-         --  any of them provide a value for the discriminant.
+         --  First check any discriminant associations to see if any of them
+         --  provide a value for the discriminant.
 
          if Present (Discriminant_Specifications (Parent (Current_Typ))) then
             Assoc := First (Component_Associations (N));
@@ -1760,9 +1880,10 @@ package body Exp_Aggr is
 
                   Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
                   while Present (Corresp_Disc) loop
-                     --  If found a corresponding discriminant then return
-                     --  the value given in the aggregate. (Note: this is
-                     --  not correct in the presence of side effects. ???)
+
+                     --  If found a corresponding discriminant then return the
+                     --  value given in the aggregate. (Note: this is not
+                     --  correct in the presence of side effects. ???)
 
                      if Disc = Corresp_Disc then
                         return Duplicate_Subexpr (Expression (Assoc));
@@ -1782,7 +1903,9 @@ package body Exp_Aggr is
 
          Parent_Typ := Etype (Current_Typ);
          while Current_Typ /= Parent_Typ loop
-            if Has_Discriminants (Parent_Typ) then
+            if Has_Discriminants (Parent_Typ)
+              and then not Has_Unknown_Discriminants (Parent_Typ)
+            then
                Parent_Disc := First_Discriminant (Parent_Typ);
 
                --  We either get the association from the subtype indication
@@ -1818,13 +1941,13 @@ package body Exp_Aggr is
                         Assoc := Expression (Assoc);
                      end if;
 
-                     --  If the located association directly denotes
-                     --  discriminant, then use the value of a saved
-                     --  association of the aggregate. This is a kludge
-                     --  to handle certain cases involving multiple
-                     --  discriminants mapped to a single discriminant
-                     --  of a descendant. It's not clear how to locate the
-                     --  appropriate discriminant value for such cases. ???
+                     --  If the located association directly denotes a
+                     --  discriminant, then use the value of a saved
+                     --  association of the aggregate. This is a kludge to
+                     --  handle certain cases involving multiple discriminants
+                     --  mapped to a single discriminant of a descendant. It's
+                     --  not clear how to locate the appropriate discriminant
+                     --  value for such cases. ???
 
                      if Is_Entity_Name (Assoc)
                        and then Ekind (Entity (Assoc)) = E_Discriminant
@@ -1941,9 +2064,10 @@ package body Exp_Aggr is
          Attach  : Node_Id;
          Init_Pr : Boolean) return List_Id
       is
-         L   : constant List_Id := New_List;
-         Ref : Node_Id;
-         RC  : RE_Id;
+         L           : constant List_Id := New_List;
+         Ref         : Node_Id;
+         RC          : RE_Id;
+         Target_Type : Entity_Id;
 
       begin
          --  Generate:
@@ -1957,27 +2081,46 @@ package body Exp_Aggr is
              Selector_Name => Make_Identifier (Loc, Name_uController));
          Set_Assignment_OK (Ref);
 
-         --  Ada 2005 (AI-287): Give support to default initialization of
-         --  limited types and components.
+         --  Ada 2005 (AI-287): Give support to aggregates of limited types.
+         --  If the type is intrinsically limited the controller is limited as
+         --  well. If it is tagged and limited then so is the controller.
+         --  Otherwise an untagged type may have limited components without its
+         --  full view being limited, so the controller is not limited.
 
-         if (Nkind (Target) = N_Identifier
-              and then Present (Etype (Target))
-              and then Is_Limited_Type (Etype (Target)))
-           or else
-            (Nkind (Target) = N_Selected_Component
-              and then Present (Etype (Selector_Name (Target)))
-              and then Is_Limited_Type (Etype (Selector_Name (Target))))
-           or else
-            (Nkind (Target) = N_Unchecked_Type_Conversion
-              and then Present (Etype (Target))
-              and then Is_Limited_Type (Etype (Target)))
-           or else
-            (Nkind (Target) = N_Unchecked_Expression
-              and then Nkind (Expression (Target)) = N_Indexed_Component
-              and then Present (Etype (Prefix (Expression (Target))))
-              and then Is_Limited_Type (Etype (Prefix (Expression (Target)))))
+         if Nkind (Target) = N_Identifier then
+            Target_Type := Etype (Target);
+
+         elsif Nkind (Target) = N_Selected_Component then
+            Target_Type := Etype (Selector_Name (Target));
+
+         elsif Nkind (Target) = N_Unchecked_Type_Conversion then
+            Target_Type := Etype (Target);
+
+         elsif Nkind (Target) = N_Unchecked_Expression
+           and then Nkind (Expression (Target)) = N_Indexed_Component
+         then
+            Target_Type := Etype (Prefix (Expression (Target)));
+
+         else
+            Target_Type := Etype (Target);
+         end if;
+
+         --  If the target has not been analyzed yet, as will happen with
+         --  delayed expansion, use the given type (either the aggregate type
+         --  or an ancestor) to determine limitedness.
+
+         if No (Target_Type) then
+            Target_Type := Typ;
+         end if;
+
+         if (Is_Tagged_Type (Target_Type))
+           and then Is_Limited_Type (Target_Type)
          then
             RC := RE_Limited_Record_Controller;
+
+         elsif Is_Inherently_Limited_Type (Target_Type) then
+            RC := RE_Limited_Record_Controller;
+
          else
             RC := RE_Record_Controller;
          end if;
@@ -2061,7 +2204,7 @@ package body Exp_Aggr is
          --  proper scope is the scope of the target rather than the
          --  potentially transient current scope.
 
-         if Controlled_Type (Typ) then
+         if Needs_Finalization (Typ) then
 
             --  The current aggregate belongs to an allocator which creates
             --  an object through an anonymous access type or acts as the root
@@ -2141,7 +2284,7 @@ package body Exp_Aggr is
          end if;
 
          --  In the Has_Controlled component case, all the intermediate
-         --  controllers must be initialized
+         --  controllers must be initialized.
 
          if Has_Controlled_Component (Typ)
            and not Is_Limited_Ancestor_Expansion
@@ -2161,8 +2304,8 @@ package body Exp_Aggr is
                   Outer_Typ := Etype (Outer_Typ);
                end loop;
 
-               --  Attach it to the outer record controller to the
-               --  external final list
+               --  Attach it to the outer record controller to the external
+               --  final list.
 
                if Outer_Typ = Init_Typ then
                   Append_List_To (L,
@@ -2268,10 +2411,33 @@ package body Exp_Aggr is
          end if;
       end Gen_Ctrl_Actions_For_Aggr;
 
+      function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
+      --  If default expression of a component mentions a discriminant of the
+      --  type, it must be rewritten as the discriminant of the target object.
+
       function Replace_Type (Expr : Node_Id) return Traverse_Result;
-      --  If the aggregate contains a self-reference, traverse each
-      --  expression to replace a possible self-reference with a reference
-      --  to the proper component of the target of the assignment.
+      --  If the aggregate contains a self-reference, traverse each expression
+      --  to replace a possible self-reference with a reference to the proper
+      --  component of the target of the assignment.
+
+      --------------------------
+      -- Rewrite_Discriminant --
+      --------------------------
+
+      function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
+      begin
+         if Nkind (Expr) = N_Identifier
+           and then Present (Entity (Expr))
+           and then Ekind (Entity (Expr)) = E_In_Parameter
+           and then Present (Discriminal_Link (Entity (Expr)))
+         then
+            Rewrite (Expr,
+              Make_Selected_Component (Loc,
+                Prefix        => New_Occurrence_Of (Obj, Loc),
+                Selector_Name => Make_Identifier (Loc, Chars (Expr))));
+         end if;
+         return OK;
+      end Rewrite_Discriminant;
 
       ------------------
       -- Replace_Type --
@@ -2279,9 +2445,19 @@ package body Exp_Aggr is
 
       function Replace_Type (Expr : Node_Id) return Traverse_Result is
       begin
+         --  Note regarding the Root_Type test below: Aggregate components for
+         --  self-referential types include attribute references to the current
+         --  instance, of the form: Typ'access, etc.. These references are
+         --  rewritten as references to the target of the aggregate: the
+         --  left-hand side of an assignment, the entity in a declaration,
+         --  or a temporary. Without this test, we would improperly extended
+         --  this rewriting to attribute references whose prefix was not the
+         --  type of the aggregate.
+
          if Nkind (Expr) = N_Attribute_Reference
-           and  then Is_Entity_Name (Prefix (Expr))
+           and then Is_Entity_Name (Prefix (Expr))
            and then Is_Type (Entity (Prefix (Expr)))
+           and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
          then
             if Is_Entity_Name (Lhs) then
                Rewrite (Prefix (Expr),
@@ -2309,6 +2485,9 @@ package body Exp_Aggr is
       procedure Replace_Self_Reference is
         new Traverse_Proc (Replace_Type);
 
+      procedure Replace_Discriminants is
+        new Traverse_Proc (Rewrite_Discriminant);
+
    --  Start of processing for Build_Record_Aggr_Code
 
    begin
@@ -2321,15 +2500,15 @@ package body Exp_Aggr is
       --  are visible. We know already that the types are compatible.
 
       if Present (Etype (Lhs))
-        and then Is_Interface (Etype (Lhs))
+        and then Is_Class_Wide_Type (Etype (Lhs))
       then
          Target := Unchecked_Convert_To (Typ, Lhs);
       else
          Target := Lhs;
       end if;
 
-      --  Deal with the ancestor part of extension aggregates
-      --  or with the discriminants of the root type
+      --  Deal with the ancestor part of extension aggregates or with the
+      --  discriminants of the root type.
 
       if Nkind (N) = N_Extension_Aggregate then
          declare
@@ -2341,7 +2520,7 @@ package body Exp_Aggr is
 
             --     init-proc (T(tmp));  if T is constrained and
             --     init-proc (S(tmp));  where S applies an appropriate
-            --                           constraint if T is unconstrained
+            --                          constraint if T is unconstrained
 
             if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
                Ancestor_Is_Subtype_Mark := True;
@@ -2349,12 +2528,12 @@ package body Exp_Aggr is
                if Is_Constrained (Entity (A)) then
                   Init_Typ := Entity (A);
 
-               --  For an ancestor part given by an unconstrained type
-               --  mark, create a subtype constrained by appropriate
-               --  corresponding discriminant values coming from either
-               --  associations of the aggregate or a constraint on
-               --  a parent type. The subtype will be used to generate
-               --  the correct default value for the ancestor part.
+               --  For an ancestor part given by an unconstrained type mark,
+               --  create a subtype constrained by appropriate corresponding
+               --  discriminant values coming from either associations of the
+               --  aggregate or a constraint on a parent type. The subtype will
+               --  be used to generate the correct default value for the
+               --  ancestor part.
 
                elsif Has_Discriminants (Entity (A)) then
                   declare
@@ -2387,9 +2566,9 @@ package body Exp_Aggr is
                          Defining_Identifier => Init_Typ,
                          Subtype_Indication  => New_Indic);
 
-                     --  Itypes must be analyzed with checks off
-                     --  Declaration must have a parent for proper
-                     --  handling of subsidiary actions.
+                     --  Itypes must be analyzed with checks off Declaration
+                     --  must have a parent for proper handling of subsidiary
+                     --  actions.
 
                      Set_Parent (Subt_Decl, N);
                      Analyze (Subt_Decl, Suppress => All_Checks);
@@ -2399,44 +2578,59 @@ package body Exp_Aggr is
                Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
                Set_Assignment_OK (Ref);
 
-               if Has_Default_Init_Comps (N)
-                 or else Has_Task (Base_Type (Init_Typ))
-               then
+               if not Is_Interface (Init_Typ) then
                   Append_List_To (L,
                     Build_Initialization_Call (Loc,
-                      Id_Ref       => Ref,
-                      Typ          => Init_Typ,
-                      In_Init_Proc => Within_Init_Proc,
-                      With_Default_Init => True));
-               else
-                  Append_List_To (L,
-                    Build_Initialization_Call (Loc,
-                      Id_Ref       => Ref,
-                      Typ          => Init_Typ,
-                      In_Init_Proc => Within_Init_Proc));
+                      Id_Ref            => Ref,
+                      Typ               => Init_Typ,
+                      In_Init_Proc      => Within_Init_Proc,
+                      With_Default_Init => Has_Default_Init_Comps (N)
+                                             or else
+                                           Has_Task (Base_Type (Init_Typ))));
+
+                  if Is_Constrained (Entity (A))
+                    and then Has_Discriminants (Entity (A))
+                  then
+                     Check_Ancestor_Discriminants (Entity (A));
+                  end if;
                end if;
 
-               if Is_Constrained (Entity (A))
-                 and then Has_Discriminants (Entity (A))
-               then
-                  Check_Ancestor_Discriminants (Entity (A));
-               end if;
+            --  Handle calls to C++ constructors
+
+            elsif Is_CPP_Constructor_Call (A) then
+               Init_Typ := Etype (A);
+               Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
+               Set_Assignment_OK (Ref);
+
+               Append_List_To (L,
+                 Build_Initialization_Call (Loc,
+                   Id_Ref            => Ref,
+                   Typ               => Init_Typ,
+                   In_Init_Proc      => Within_Init_Proc,
+                   With_Default_Init => Has_Default_Init_Comps (N),
+                   Constructor_Ref   => A));
 
             --  Ada 2005 (AI-287): If the ancestor part is an aggregate of
             --  limited type, a recursive call expands the ancestor. Note that
             --  in the limited case, the ancestor part must be either a
             --  function call (possibly qualified, or wrapped in an unchecked
             --  conversion) or aggregate (definitely qualified).
+            --  The ancestor part can also be a function call (that may be
+            --  transformed into an explicit dereference) or a qualification
+            --  of one such.
 
             elsif Is_Limited_Type (Etype (A))
-              and then Nkind (Unqualify (A)) /= N_Function_Call --  aggregate?
-              and then
-                (Nkind (Unqualify (A)) /= N_Unchecked_Type_Conversion
-                   or else
-                 Nkind (Expression (Unqualify (A))) /= N_Function_Call)
+              and then Nkind_In (Unqualify (A), N_Aggregate,
+                                                N_Extension_Aggregate)
             then
                Ancestor_Is_Expression := True;
 
+               --  Set up  finalization data for enclosing record, because
+               --  controlled subcomponents of the ancestor part will be
+               --  attached to it.
+
+               Gen_Ctrl_Actions_For_Aggr;
+
                Append_List_To (L,
                   Build_Record_Aggr_Code (
                     N                             => Unqualify (A),
@@ -2447,10 +2641,12 @@ package body Exp_Aggr is
                     Is_Limited_Ancestor_Expansion => True));
 
             --  If the ancestor part is an expression "E", we generate
+
             --     T(tmp) := E;
+
             --  In Ada 2005, this includes the case of a (possibly qualified)
             --  limited function call. The assignment will turn into a
-            --  build-in-place function call (see
+            --  build-in-place function call (for further details, see
             --  Make_Build_In_Place_Call_In_Assignment).
 
             else
@@ -2460,8 +2656,8 @@ package body Exp_Aggr is
                --  If the ancestor part is an aggregate, force its full
                --  expansion, which was delayed.
 
-               if Nkind (Unqualify (A)) = N_Aggregate
-                 or else Nkind (Unqualify (A)) = N_Extension_Aggregate
+               if Nkind_In (Unqualify (A), N_Aggregate,
+                                           N_Extension_Aggregate)
                then
                   Set_Analyzed (A, False);
                   Set_Analyzed (Expression (A), False);
@@ -2472,7 +2668,7 @@ package body Exp_Aggr is
 
                --  Make the assignment without usual controlled actions since
                --  we only want the post adjust but not the pre finalize here
-               --  Add manual adjust when necessary
+               --  Add manual adjust when necessary.
 
                Assign := New_List (
                  Make_OK_Assignment_Statement (Loc,
@@ -2484,7 +2680,7 @@ package body Exp_Aggr is
                --  the subsequent deep_adjust works properly (unless VM_Target,
                --  where tags are implicit).
 
-               if VM_Target = No_VM then
+               if Tagged_Type_Expansion then
                   Instr :=
                     Make_OK_Assignment_Statement (Loc,
                       Name =>
@@ -2507,11 +2703,7 @@ package body Exp_Aggr is
                   --  Ada 2005 (AI-251): If tagged type has progenitors we must
                   --  also initialize tags of the secondary dispatch tables.
 
-                  if Present (Abstract_Interfaces (Base_Type (Typ)))
-                    and then not
-                      Is_Empty_Elmt_List
-                        (Abstract_Interfaces (Base_Type (Typ)))
-                  then
+                  if Has_Interfaces (Base_Type (Typ)) then
                      Init_Secondary_Tags
                        (Typ        => Base_Type (Typ),
                         Target     => Target,
@@ -2521,7 +2713,9 @@ package body Exp_Aggr is
 
                --  Call Adjust manually
 
-               if Controlled_Type (Etype (A)) then
+               if Needs_Finalization (Etype (A))
+                 and then not Is_Limited_Type (Etype (A))
+               then
                   Append_List_To (Assign,
                     Make_Adjust_Call (
                       Ref         => New_Copy_Tree (Ref),
@@ -2639,6 +2833,18 @@ package body Exp_Aggr is
          end if;
       end if;
 
+      --  For CPP types we generate an implicit call to the C++ default
+      --  constructor to ensure the proper initialization of the _Tag
+      --  component.
+
+      if Is_CPP_Class (Typ) then
+         pragma Assert (Present (Base_Init_Proc (Typ)));
+         Append_List_To (L,
+           Build_Initialization_Call (Loc,
+             Id_Ref => Lhs,
+             Typ    => Typ));
+      end if;
+
       --  Generate the assignments, component by component
 
       --    tmp.comp1 := Expr1_From_Aggr;
@@ -2649,10 +2855,24 @@ package body Exp_Aggr is
       while Present (Comp) loop
          Selector := Entity (First (Choices (Comp)));
 
-         --  Ada 2005 (AI-287): For each default-initialized component genarate
+         --  C++ constructors
+
+         if Is_CPP_Constructor_Call (Expression (Comp)) then
+            Append_List_To (L,
+              Build_Initialization_Call (Loc,
+                Id_Ref => Make_Selected_Component (Loc,
+                            Prefix => New_Copy_Tree (Target),
+                            Selector_Name => New_Occurrence_Of (Selector,
+                                                                   Loc)),
+                Typ    => Etype (Selector),
+                Enclos_Type => Typ,
+                With_Default_Init => True,
+                Constructor_Ref => Expression (Comp)));
+
+         --  Ada 2005 (AI-287): For each default-initialized component generate
          --  a call to the corresponding IP subprogram if available.
 
-         if Box_Present (Comp)
+         elsif Box_Present (Comp)
            and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
          then
             if Ekind (Selector) /= E_Discriminant then
@@ -2696,15 +2916,13 @@ package body Exp_Aggr is
                 Enclos_Type => Typ,
                 With_Default_Init => True));
 
-            goto Next_Comp;
-         end if;
-
          --  Prepare for component assignment
 
-         if Ekind (Selector) /= E_Discriminant
+         elsif Ekind (Selector) /= E_Discriminant
            or else Nkind (N) = N_Extension_Aggregate
          then
             --  All the discriminants have now been assigned
+
             --  This is now a good moment to initialize and attach all the
             --  controllers. Their position may depend on the discriminants.
 
@@ -2724,10 +2942,10 @@ package body Exp_Aggr is
                Expr_Q := Expression (Comp);
             end if;
 
-            --  The controller is the one of the parent type defining
-            --  the component (in case of inherited components).
+            --  The controller is the one of the parent type defining the
+            --  component (in case of inherited components).
 
-            if Controlled_Type (Comp_Type) then
+            if Needs_Finalization (Comp_Type) then
                Internal_Final_List :=
                  Make_Selected_Component (Loc,
                    Prefix => Convert_To (
@@ -2758,11 +2976,11 @@ package body Exp_Aggr is
                --  an object declaration:
 
                --    type Arr_Typ is array (Integer range <>) of ...;
-               --
+
                --    type Rec_Typ (...) is record
                --       Obj_Arr_Typ : Arr_Typ (A .. B);
                --    end record;
-               --
+
                --    Obj_Rec_Typ : Rec_Typ := (...,
                --      Obj_Arr_Typ => (X => (...), Y => (...)));
 
@@ -2792,33 +3010,31 @@ package body Exp_Aggr is
                   declare
                      SubE : constant Entity_Id :=
                               Make_Defining_Identifier (Loc,
-                                New_Internal_Name ('T'));
+                                Chars => New_Internal_Name ('T'));
 
                      SubD : constant Node_Id :=
                               Make_Subtype_Declaration (Loc,
-                                Defining_Identifier =>
-                                  SubE,
+                                Defining_Identifier => SubE,
                                 Subtype_Indication  =>
                                   Make_Subtype_Indication (Loc,
-                                    Subtype_Mark => New_Reference_To (
-                                      Etype (Comp_Type), Loc),
+                                    Subtype_Mark =>
+                                      New_Reference_To
+                                        (Etype (Comp_Type), Loc),
                                     Constraint =>
-                                      Make_Index_Or_Discriminant_Constraint (
-                                        Loc, Constraints => New_List (
-                                          New_Copy_Tree (Aggregate_Bounds (
-                                            Expr_Q))))));
+                                      Make_Index_Or_Discriminant_Constraint
+                                        (Loc,
+                                         Constraints => New_List (
+                                          New_Copy_Tree
+                                            (Aggregate_Bounds (Expr_Q))))));
 
                      --  Create a temporary array of the above subtype which
                      --  will be used to capture the aggregate assignments.
 
-                     TmpE : constant Entity_Id :=
-                              Make_Defining_Identifier (Loc,
-                                New_Internal_Name ('A'));
+                     TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
 
                      TmpD : constant Node_Id :=
                               Make_Object_Declaration (Loc,
-                                Defining_Identifier =>
-                                  TmpE,
+                                Defining_Identifier => TmpE,
                                 Object_Definition   =>
                                   New_Reference_To (SubE, Loc));
 
@@ -2861,10 +3077,14 @@ package body Exp_Aggr is
             --  Expr_Q is not delayed aggregate
 
             else
+               if Has_Discriminants (Typ) then
+                  Replace_Discriminants (Expr_Q);
+               end if;
+
                Instr :=
                  Make_OK_Assignment_Statement (Loc,
                    Name       => Comp_Expr,
-                   Expression => Expression (Comp));
+                   Expression => Expr_Q);
 
                Set_No_Ctrl_Actions (Instr);
                Append_To (L, Instr);
@@ -2875,7 +3095,9 @@ package body Exp_Aggr is
 
                --    tmp.comp._tag := comp_typ'tag;
 
-               if Is_Tagged_Type (Comp_Type) and then VM_Target = No_VM then
+               if Is_Tagged_Type (Comp_Type)
+                 and then Tagged_Type_Expansion
+               then
                   Instr :=
                     Make_OK_Assignment_Statement (Loc,
                       Name =>
@@ -2895,11 +3117,14 @@ package body Exp_Aggr is
                end if;
 
                --  Adjust and Attach the component to the proper controller
+
                --     Adjust (tmp.comp);
                --     Attach_To_Final_List (tmp.comp,
                --       comp_typ (tmp)._record_controller.f)
 
-               if Controlled_Type (Comp_Type) then
+               if Needs_Finalization (Comp_Type)
+                 and then not Is_Limited_Type (Comp_Type)
+               then
                   Append_List_To (L,
                     Make_Adjust_Call (
                       Ref         => New_Copy_Tree (Comp_Expr),
@@ -2952,8 +3177,8 @@ package body Exp_Aggr is
                       Reason => CE_Discriminant_Check_Failed));
 
                else
-                  --  Find self-reference in previous discriminant
-                  --  assignment, and replace with proper expression.
+                  --  Find self-reference in previous discriminant assignment,
+                  --  and replace with proper expression.
 
                   declare
                      Ass : Node_Id;
@@ -2977,8 +3202,6 @@ package body Exp_Aggr is
             end;
          end if;
 
-         <<Next_Comp>>
-
          Next (Comp);
       end loop;
 
@@ -2990,7 +3213,14 @@ package body Exp_Aggr is
       if Ancestor_Is_Expression then
          null;
 
-      elsif Is_Tagged_Type (Typ) and then VM_Target = No_VM then
+      --  For CPP types we generated a call to the C++ default constructor
+      --  before the components have been initialized to ensure the proper
+      --  initialization of the _Tag component (see above).
+
+      elsif Is_CPP_Class (Typ) then
+         null;
+
+      elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
          Instr :=
            Make_OK_Assignment_Statement (Loc,
              Name =>
@@ -3012,10 +3242,7 @@ package body Exp_Aggr is
          --  abstract interfaces we must also initialize the tags of the
          --  secondary dispatch tables.
 
-         if Present (Abstract_Interfaces (Base_Type (Typ)))
-           and then not
-             Is_Empty_Elmt_List (Abstract_Interfaces (Base_Type (Typ)))
-         then
+         if Has_Interfaces (Base_Type (Typ)) then
             Init_Secondary_Tags
               (Typ        => Base_Type (Typ),
                Target     => Target,
@@ -3073,8 +3300,14 @@ package body Exp_Aggr is
                                               N_Discriminant_Specification
       then
          Flist := Empty;
-      else
+
+      elsif Needs_Finalization (Typ) then
          Flist := Find_Final_List (Access_Type);
+
+      --  Otherwise there are no controlled actions to be performed.
+
+      else
+         Flist := Empty;
       end if;
 
       if Is_Array_Type (Typ) then
@@ -3092,8 +3325,8 @@ package body Exp_Aggr is
                  Flist,
                  Associated_Final_Chain (Base_Type (Access_Type)));
 
-            --  ??? Dubious actual for Obj: expect 'the original object
-            --  being initialized'
+            --  ??? Dubious actual for Obj: expect 'the original object being
+            --  initialized'
 
             if Has_Task (Typ) then
                Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
@@ -3109,8 +3342,8 @@ package body Exp_Aggr is
              (Aggr, Typ, Occ, Flist,
               Associated_Final_Chain (Base_Type (Access_Type))));
 
-         --  ??? Dubious actual for Obj: expect 'the original object
-         --  being initialized'
+         --  ??? Dubious actual for Obj: expect 'the original object being
+         --  initialized'
 
       end if;
    end Convert_Aggr_In_Allocator;
@@ -3120,9 +3353,9 @@ package body Exp_Aggr is
    --------------------------------
 
    procedure Convert_Aggr_In_Assignment (N : Node_Id) is
-      Aggr : Node_Id             := Expression (N);
-      Typ  : constant Entity_Id  := Etype (Aggr);
-      Occ  : constant Node_Id    := New_Copy_Tree (Name (N));
+      Aggr : Node_Id            := Expression (N);
+      Typ  : constant Entity_Id := Etype (Aggr);
+      Occ  : constant Node_Id   := New_Copy_Tree (Name (N));
 
    begin
       if Nkind (Aggr) = N_Qualified_Expression then
@@ -3237,11 +3470,18 @@ package body Exp_Aggr is
       --  the finalization list of the return must be moved to the caller's
       --  finalization list to complete the return.
 
+      --  However, if the aggregate is limited, it is built in place, and the
+      --  controlled components are not assigned to intermediate temporaries
+      --  so there is no need for a transient scope in this case either.
+
       if Requires_Transient_Scope (Typ)
         and then Ekind (Current_Scope) /= E_Return_Statement
+        and then not Is_Limited_Type (Typ)
       then
-         Establish_Transient_Scope (Aggr, Sec_Stack =>
-           Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
+         Establish_Transient_Scope
+           (Aggr,
+            Sec_Stack =>
+              Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
       end if;
 
       Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
@@ -3250,7 +3490,7 @@ package body Exp_Aggr is
    end Convert_Aggr_In_Object_Decl;
 
    -------------------------------------
-   -- Convert_array_Aggr_In_Allocator --
+   -- Convert_Array_Aggr_In_Allocator --
    -------------------------------------
 
    procedure Convert_Array_Aggr_In_Allocator
@@ -3283,6 +3523,7 @@ package body Exp_Aggr is
 
    procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
       Loc  : constant Source_Ptr := Sloc (N);
+      T    : Entity_Id;
       Temp : Entity_Id;
 
       Instr       : Node_Id;
@@ -3319,8 +3560,8 @@ package body Exp_Aggr is
          end;
       end if;
 
-      --  Just set the Delay flag in the cases where the transformation
-      --  will be done top down from above.
+      --  Just set the Delay flag in the cases where the transformation will be
+      --  done top down from above.
 
       if False
 
@@ -3356,37 +3597,61 @@ package body Exp_Aggr is
          --  in place within the caller's scope).
 
          or else
-          (Is_Inherently_Limited_Type (Typ)
-            and then
-              (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
-                or else Nkind (Parent_Node) = N_Simple_Return_Statement))
+           (Is_Inherently_Limited_Type (Typ)
+             and then
+               (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
+                 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
       then
          Set_Expansion_Delayed (N);
          return;
       end if;
 
       if Requires_Transient_Scope (Typ) then
-         Establish_Transient_Scope (N, Sec_Stack =>
-              Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
+         Establish_Transient_Scope
+           (N, Sec_Stack =>
+                 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
       end if;
 
-      --  Create the temporary
+      --  If the aggregate is non-limited, create a temporary. If it is limited
+      --  and the context is an assignment, this is a subaggregate for an
+      --  enclosing aggregate being expanded. It must be built in place, so use
+      --  the target of the current assignment.
+
+      if Is_Limited_Type (Typ)
+        and then Nkind (Parent (N)) = N_Assignment_Statement
+      then
+         Target_Expr := New_Copy_Tree (Name (Parent (N)));
+         Insert_Actions
+           (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
+         Rewrite (Parent (N), Make_Null_Statement (Loc));
 
-      Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
+      else
+         Temp := Make_Temporary (Loc, 'A', N);
 
-      Instr :=
-        Make_Object_Declaration (Loc,
-          Defining_Identifier => Temp,
-          Object_Definition => New_Occurrence_Of (Typ, Loc));
+         --  If the type inherits unknown discriminants, use the view with
+         --  known discriminants if available.
 
-      Set_No_Initialization (Instr);
-      Insert_Action (N, Instr);
-      Initialize_Discriminants (Instr, Typ);
-      Target_Expr := New_Occurrence_Of (Temp, Loc);
+         if Has_Unknown_Discriminants (Typ)
+            and then Present (Underlying_Record_View (Typ))
+         then
+            T := Underlying_Record_View (Typ);
+         else
+            T := Typ;
+         end if;
 
-      Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
-      Rewrite (N, New_Occurrence_Of (Temp, Loc));
-      Analyze_And_Resolve (N, Typ);
+         Instr :=
+           Make_Object_Declaration (Loc,
+             Defining_Identifier => Temp,
+             Object_Definition   => New_Occurrence_Of (T, Loc));
+
+         Set_No_Initialization (Instr);
+         Insert_Action (N, Instr);
+         Initialize_Discriminants (Instr, T);
+         Target_Expr := New_Occurrence_Of (Temp, Loc);
+         Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
+         Rewrite (N, New_Occurrence_Of (Temp, Loc));
+         Analyze_And_Resolve (N, T);
+      end if;
    end Convert_To_Assignments;
 
    ---------------------------
@@ -3403,21 +3668,21 @@ package body Exp_Aggr is
       Static_Components : Boolean := True;
 
       procedure Check_Static_Components;
-      --  Check whether all components of the aggregate are compile-time
-      --  known values, and can be passed as is to the back-end without
-      --  further expansion.
+      --  Check whether all components of the aggregate are compile-time known
+      --  values, and can be passed as is to the back-end without further
+      --  expansion.
 
       function Flatten
         (N   : Node_Id;
          Ix  : Node_Id;
          Ixb : Node_Id) return Boolean;
-      --  Convert the aggregate into a purely positional form if possible.
-      --  On entry the bounds of all dimensions are known to be static,
-      --  and the total number of components is safe enough to expand.
+      --  Convert the aggregate into a purely positional form if possible. On
+      --  entry the bounds of all dimensions are known to be static, and the
+      --  total number of components is safe enough to expand.
 
       function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
-      --  Return True iff the array N is flat (which is not rivial
-      --  in the case of multidimensionsl aggregates).
+      --  Return True iff the array N is flat (which is not trivial in the case
+      --  of multidimensionsl aggregates).
 
       -----------------------------
       -- Check_Static_Components --
@@ -3505,8 +3770,8 @@ package body Exp_Aggr is
             return False;
          end if;
 
-         --  Determine if set of alternatives is suitable for conversion
-         --  and build an array containing the values in sequence.
+         --  Determine if set of alternatives is suitable for conversion and
+         --  build an array containing the values in sequence.
 
          declare
             Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
@@ -3531,7 +3796,7 @@ package body Exp_Aggr is
                   if Nkind (Elmt) = N_Aggregate
                     and then Present (Next_Index (Ix))
                     and then
-                         not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
+                      not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
                   then
                      return False;
                   end if;
@@ -3577,24 +3842,43 @@ package body Exp_Aggr is
                            --  Check for maximum others replication. Note that
                            --  we skip this test if either of the restrictions
                            --  No_Elaboration_Code or No_Implicit_Loops is
-                           --  active, or if this is a preelaborable unit.
+                           --  active, if this is a preelaborable unit or a
+                           --  predefined unit. This ensures that predefined
+                           --  units get the same level of constant folding in
+                           --  Ada 95 and Ada 05, where their categorization
+                           --  has changed.
 
                            declare
                               P : constant Entity_Id :=
                                     Cunit_Entity (Current_Sem_Unit);
 
                            begin
+                              --  Check if duplication OK and if so continue
+                              --  processing.
+
                               if Restriction_Active (No_Elaboration_Code)
                                 or else Restriction_Active (No_Implicit_Loops)
                                 or else Is_Preelaborated (P)
                                 or else (Ekind (P) = E_Package_Body
                                           and then
                                             Is_Preelaborated (Spec_Entity (P)))
+                                or else
+                                  Is_Predefined_File_Name
+                                    (Unit_File_Name (Get_Source_Unit (P)))
                               then
                                  null;
 
+                              --  If duplication not OK, then we return False
+                              --  if the replication count is too high
+
                               elsif Rep_Count > Max_Others_Replicate then
                                  return False;
+
+                              --  Continue on if duplication not OK, but the
+                              --  replication count is not excessive.
+
+                              else
+                                 null;
                               end if;
                            end;
                         end if;
@@ -3635,7 +3919,7 @@ package body Exp_Aggr is
                      end if;
                   end if;
 
-                  --  Range cases merge with Lo,Hi said
+                  --  Range cases merge with Lo,Hi set
 
                   if not Compile_Time_Known_Value (Lo)
                        or else
@@ -3723,8 +4007,8 @@ package body Exp_Aggr is
          return;
       end if;
 
-      --  Do not convert to positional if controlled components are
-      --  involved since these require special processing
+      --  Do not convert to positional if controlled components are involved
+      --  since these require special processing
 
       if Has_Controlled_Component (Typ) then
          return;
@@ -3745,7 +4029,7 @@ package body Exp_Aggr is
       --  assignments to the target anyway, but it is conceivable that
       --  it will eventually be able to treat such aggregates statically???
 
-      if Aggr_Size_OK (Typ)
+      if Aggr_Size_OK (N, Typ)
         and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
       then
          if Static_Components then
@@ -3900,10 +4184,10 @@ package body Exp_Aggr is
             end loop;
 
          else
-            --  We know the aggregate type is unconstrained and the
-            --  aggregate is not processable by the back end, therefore
-            --  not necessarily positional. Retrieve the bounds of each
-            --  dimension as computed earlier.
+            --  We know the aggregate type is unconstrained and the aggregate
+            --  is not processable by the back end, therefore not necessarily
+            --  positional. Retrieve each dimension bounds (computed earlier).
+            --  earlier.
 
             for D in 1 .. Number_Dimensions (Typ) loop
                Append (
@@ -3955,8 +4239,8 @@ package body Exp_Aggr is
          --    [constraint_error when
          --      Aggr_Lo <= Aggr_Hi and then
          --        (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
-         --
-         --  As an optimization try to see if some tests are trivially vacuos
+
+         --  As an optimization try to see if some tests are trivially vacuous
          --  because we are comparing an expression against itself.
 
          if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
@@ -4024,16 +4308,15 @@ package body Exp_Aggr is
          --  The index type for this dimension.xxx
 
          Cond  : Node_Id := Empty;
-
          Assoc : Node_Id;
          Expr  : Node_Id;
 
       begin
          --  If index checks are on generate the test
-         --
+
          --    [constraint_error when
          --      Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
-         --
+
          --  As an optimization try to see if some tests are trivially vacuos
          --  because we are comparing an expression against itself. Also for
          --  the first dimension the test is trivially vacuous because there
@@ -4193,7 +4476,7 @@ package body Exp_Aggr is
          Obj_Hi  : Node_Id;
 
          function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
-         --   Aggregates that consist of a single Others choice are safe
+         --  Aggregates that consist of a single Others choice are safe
          --  if the single expression is.
 
          function Safe_Aggregate (Aggr : Node_Id) return Boolean;
@@ -4360,6 +4643,7 @@ package body Exp_Aggr is
             end if;
 
             Aggr_In := First_Index (Etype (N));
+
             if Nkind (Parent (N)) = N_Assignment_Statement then
                Obj_In  := First_Index (Etype (Name (Parent (N))));
 
@@ -4446,8 +4730,8 @@ package body Exp_Aggr is
                Need_To_Check := False;
 
             else
-               --  Count the number of discrete choices. Start with -1
-               --  because the others choice does not count.
+               --  Count the number of discrete choices. Start with -1 because
+               --  the others choice does not count.
 
                Nb_Choices := -1;
                Assoc := First (Component_Associations (Sub_Aggr));
@@ -4470,8 +4754,8 @@ package body Exp_Aggr is
             Need_To_Check := False;
          end if;
 
-         --  If we are dealing with a positional sub-aggregate with an
-         --  others choice then compute the number or positional elements.
+         --  If we are dealing with a positional sub-aggregate with an others
+         --  choice then compute the number or positional elements.
 
          if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
             Expr := First (Expressions (Sub_Aggr));
@@ -4529,9 +4813,9 @@ package body Exp_Aggr is
          if not Need_To_Check then
             Cond := Empty;
 
-         --  If we are dealing with an aggregate containing an others
-         --  choice and positional components, we generate the following test:
-         --
+         --  If we are dealing with an aggregate containing an others choice
+         --  and positional components, we generate the following test:
+
          --    if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
          --            Ind_Typ'Pos (Aggr_Hi)
          --    then
@@ -4559,9 +4843,9 @@ package body Exp_Aggr is
                     Expressions    => New_List (
                       Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
 
-         --  If we are dealing with an aggregate containing an others
-         --  choice and discrete choices we generate the following test:
-         --
+         --  If we are dealing with an aggregate containing an others choice
+         --  and discrete choices we generate the following test:
+
          --    [constraint_error when
          --      Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
 
@@ -4588,6 +4872,8 @@ package body Exp_Aggr is
               Make_Raise_Constraint_Error (Loc,
                 Condition => Cond,
                 Reason    => CE_Length_Check_Failed));
+            --  Questionable reason code, shouldn't that be a
+            --  CE_Range_Check_Failed ???
          end if;
 
          --  Now look inside the sub-aggregate to see if there is more work
@@ -4674,16 +4960,16 @@ package body Exp_Aggr is
             if not Range_Checks_Suppressed (Etype (Index_Constraint))
               and then not Others_Present (J)
             then
-               --  We don't use Checks.Apply_Range_Check here because it
-               --  emits a spurious check. Namely it checks that the range
-               --  defined by the aggregate bounds is non empty. But we know
-               --  this already if we get here.
+               --  We don't use Checks.Apply_Range_Check here because it emits
+               --  a spurious check. Namely it checks that the range defined by
+               --  the aggregate bounds is non empty. But we know this already
+               --  if we get here.
 
                Check_Bounds (Aggr_Index_Range, Index_Constraint);
             end if;
 
-            --  Save the low and high bounds of the aggregate index as well
-            --  as the index type for later use in checks (b) and (c) below.
+            --  Save the low and high bounds of the aggregate index as well as
+            --  the index type for later use in checks (b) and (c) below.
 
             Aggr_Low  (J) := Low_Bound (Aggr_Index_Range);
             Aggr_High (J) := High_Bound (Aggr_Index_Range);
@@ -4697,8 +4983,8 @@ package body Exp_Aggr is
 
       --  STEP 1b
 
-      --  If an others choice is present check that no aggregate
-      --  index is outside the bounds of the index constraint.
+      --  If an others choice is present check that no aggregate index is
+      --  outside the bounds of the index constraint.
 
       Others_Check (N, 1);
 
@@ -4713,10 +4999,10 @@ package body Exp_Aggr is
 
       --  STEP 2
 
-      --  Here we test for is packed array aggregate that we can handle
-      --  at compile time. If so, return with transformation done. Note
-      --  that we do this even if the aggregate is nested, because once
-      --  we have done this processing, there is no more nested aggregate!
+      --  Here we test for is packed array aggregate that we can handle at
+      --  compile time. If so, return with transformation done. Note that we do
+      --  this even if the aggregate is nested, because once we have done this
+      --  processing, there is no more nested aggregate!
 
       if Packed_Array_Aggregate_Handled (N) then
          return;
@@ -4798,8 +5084,8 @@ package body Exp_Aggr is
 
       --  STEP 3
 
-      --  Delay expansion for nested aggregates it will be taken care of
-      --  when the parent aggregate is expanded
+      --  Delay expansion for nested aggregates: it will be taken care of
+      --  when the parent aggregate is expanded.
 
       Parent_Node := Parent (N);
       Parent_Kind := Nkind (Parent_Node);
@@ -4813,7 +5099,7 @@ package body Exp_Aggr is
         or else Parent_Kind = N_Extension_Aggregate
         or else Parent_Kind = N_Component_Association
         or else (Parent_Kind = N_Object_Declaration
-                  and then Controlled_Type (Typ))
+                  and then Needs_Finalization (Typ))
         or else (Parent_Kind = N_Assignment_Statement
                   and then Inside_Init_Proc)
       then
@@ -4861,14 +5147,21 @@ package body Exp_Aggr is
       else
          Maybe_In_Place_OK :=
           (Nkind (Parent (N)) = N_Assignment_Statement
-             and then Comes_From_Source (N)
-             and then In_Place_Assign_OK)
+            and then Comes_From_Source (N)
+            and then In_Place_Assign_OK)
 
           or else
             (Nkind (Parent (Parent (N))) = N_Allocator
               and then In_Place_Assign_OK);
       end if;
 
+      --  If this is an array of tasks, it will be expanded into build-in-place
+      --  assignments. Build an activation chain for the tasks now.
+
+      if Has_Task (Etype (N)) then
+         Build_Activation_Chain_Entity (N);
+      end if;
+
       if not Has_Default_Init_Comps (N)
          and then Comes_From_Source (Parent (N))
          and then Nkind (Parent (N)) = N_Object_Declaration
@@ -4949,7 +5242,7 @@ package body Exp_Aggr is
 
       else
          Maybe_In_Place_OK := False;
-         Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
+         Tmp := Make_Temporary (Loc, 'A', N);
          Tmp_Decl :=
            Make_Object_Declaration
              (Loc,
@@ -4958,8 +5251,8 @@ package body Exp_Aggr is
          Set_No_Initialization (Tmp_Decl, True);
 
          --  If we are within a loop, the temporary will be pushed on the
-         --  stack at each iteration. If the aggregate is the expression for
-         --  an allocator, it will be immediately copied to the heap and can
+         --  stack at each iteration. If the aggregate is the expression for an
+         --  allocator, it will be immediately copied to the heap and can
          --  be reclaimed at once. We create a transient scope around the
          --  aggregate for this purpose.
 
@@ -4972,9 +5265,9 @@ package body Exp_Aggr is
          Insert_Action (N, Tmp_Decl);
       end if;
 
-      --  Construct and insert the aggregate code. We can safely suppress
-      --  index checks because this code is guaranteed not to raise CE
-      --  on index checks. However we should *not* suppress all checks.
+      --  Construct and insert the aggregate code. We can safely suppress index
+      --  checks because this code is guaranteed not to raise CE on index
+      --  checks. However we should *not* suppress all checks.
 
       declare
          Target : Node_Id;
@@ -5077,7 +5370,7 @@ package body Exp_Aggr is
       else
          Set_Etype (N, Typ);
 
-         if VM_Target = No_VM then
+         if Tagged_Type_Expansion then
             Expand_Record_Aggregate (N,
               Orig_Tag    =>
                 New_Occurrence_Of
@@ -5120,6 +5413,19 @@ package body Exp_Aggr is
       --  of assignment statements. Cases checked for are a nested aggregate
       --  needing Late_Expansion, the presence of a tagged component which may
       --  need tag adjustment, and a bit unaligned component reference.
+      --
+      --  We also force expansion into assignments if a component is of a
+      --  mutable type (including a private type with discriminants) because
+      --  in that case the size of the component to be copied may be smaller
+      --  than the side of the target, and there is no simple way for gigi
+      --  to compute the size of the object to be copied.
+      --
+      --  NOTE: This is part of the ongoing work to define precisely the
+      --  interface between front-end and back-end handling of aggregates.
+      --  In general it is desirable to pass aggregates as they are to gigi,
+      --  in order to minimize elaboration code. This is one case where the
+      --  semantics of Ada complicate the analysis and lead to anomalies in
+      --  the gcc back-end if the aggregate is not expanded into assignments.
 
       ----------------------------------
       -- Component_Not_OK_For_Backend --
@@ -5142,20 +5448,20 @@ package body Exp_Aggr is
                Expr_Q := Expression (C);
             end if;
 
-            --  Return true if the aggregate has any associations for
-            --  tagged components that may require tag adjustment.
-            --  These are cases where the source expression may have
-            --  a tag that could differ from the component tag (e.g.,
-            --  can occur for type conversions and formal parameters).
-            --  (Tag adjustment is not needed if VM_Target because object
-            --  tags are implicit in the JVM.)
+            --  Return true if the aggregate has any associations for tagged
+            --  components that may require tag adjustment.
+
+            --  These are cases where the source expression may have a tag that
+            --  could differ from the component tag (e.g., can occur for type
+            --  conversions and formal parameters). (Tag adjustment not needed
+            --  if VM_Target because object tags are implicit in the machine.)
 
             if Is_Tagged_Type (Etype (Expr_Q))
               and then (Nkind (Expr_Q) = N_Type_Conversion
                          or else (Is_Entity_Name (Expr_Q)
-                                   and then
-                                    Ekind (Entity (Expr_Q)) in Formal_Kind))
-              and then VM_Target = No_VM
+                                    and then
+                                      Ekind (Entity (Expr_Q)) in Formal_Kind))
+              and then Tagged_Type_Expansion
             then
                Static_Components := False;
                return True;
@@ -5178,6 +5484,12 @@ package body Exp_Aggr is
               or else not Compile_Time_Known_Aggregate (Expr_Q)
             then
                Static_Components := False;
+
+               if Is_Private_Type (Etype (Expr_Q))
+                 and then Has_Discriminants (Etype (Expr_Q))
+               then
+                  return True;
+               end if;
             end if;
 
             Next (C);
@@ -5202,11 +5514,9 @@ package body Exp_Aggr is
       --  an atomic move for it.
 
       if Is_Atomic (Typ)
-        and then (Nkind (Parent (N)) = N_Object_Declaration
-                    or else Nkind (Parent (N)) = N_Assignment_Statement)
         and then Comes_From_Source (Parent (N))
+        and then Is_Atomic_Aggregate (N, Typ)
       then
-         Expand_Atomic_Aggregate (N, Typ);
          return;
 
       --  No special management required for aggregates used to initialize
@@ -5264,34 +5574,32 @@ package body Exp_Aggr is
          Convert_To_Assignments (N, Typ);
 
       --  If the tagged types covers interface types we need to initialize all
-      --  the hidden components containing the pointers to secondary dispatch
-      --  tables.
+      --  hidden components containing pointers to secondary dispatch tables.
 
-      elsif Is_Tagged_Type (Typ) and then Has_Abstract_Interfaces (Typ) then
+      elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
          Convert_To_Assignments (N, Typ);
 
       --  If some components are mutable, the size of the aggregate component
-      --  may be disctinct from the default size of the type component, so
+      --  may be distinct from the default size of the type component, so
       --  we need to expand to insure that the back-end copies the proper
       --  size of the data.
 
       elsif Has_Mutable_Components (Typ) then
          Convert_To_Assignments (N, Typ);
 
-      --  If the type involved has any non-bit aligned components, then
-      --  we are not sure that the back end can handle this case correctly.
+      --  If the type involved has any non-bit aligned components, then we are
+      --  not sure that the back end can handle this case correctly.
 
       elsif Type_May_Have_Bit_Aligned_Components (Typ) then
          Convert_To_Assignments (N, Typ);
 
-      --  In all other cases we generate a proper aggregate that
-      --  can be handled by gigi.
+      --  In all other cases, build a proper aggregate handlable by gigi
 
       else
          if Nkind (N) = N_Aggregate then
 
-            --  If the aggregate is static and can be handled by the
-            --  back-end, nothing left to do.
+            --  If the aggregate is static and can be handled by the back-end,
+            --  nothing left to do.
 
             if Static_Components then
                Set_Compile_Time_Known_Aggregate (N);
@@ -5321,8 +5629,8 @@ package body Exp_Aggr is
                Num_Gird     : Int := 0;
 
                procedure Prepend_Stored_Values (T : Entity_Id);
-               --  Scan the list of stored discriminants of the type, and
-               --  add their values to the aggregate being built.
+               --  Scan the list of stored discriminants of the type, and add
+               --  their values to the aggregate being built.
 
                ---------------------------
                -- Prepend_Stored_Values --
@@ -5358,8 +5666,7 @@ package body Exp_Aggr is
             --  Start of processing for Generate_Aggregate_For_Derived_Type
 
             begin
-               --  Remove the associations for the  discriminant of
-               --  the derived type.
+               --  Remove the associations for the discriminant of derived type
 
                First_Comp := First (Component_Associations (N));
                while Present (First_Comp) loop
@@ -5376,10 +5683,10 @@ package body Exp_Aggr is
 
                --  Insert stored discriminant associations in the correct
                --  order. If there are more stored discriminants than new
-               --  discriminants, there is at least one new discriminant
-               --  that constrains more than one of the stored discriminants.
-               --  In this case we need to construct a proper subtype of
-               --  the parent type, in order to supply values to all the
+               --  discriminants, there is at least one new discriminant that
+               --  constrains more than one of the stored discriminants. In
+               --  this case we need to construct a proper subtype of the
+               --  parent type, in order to supply values to all the
                --  components. Otherwise there is one-one correspondence
                --  between the constraints and the stored discriminants.
 
@@ -5395,9 +5702,9 @@ package body Exp_Aggr is
 
                if Num_Gird > Num_Disc then
 
-                  --  Create a proper subtype of the parent type, which is
-                  --  the proper implementation type for the aggregate, and
-                  --  convert it to the intended target type.
+                  --  Create a proper subtype of the parent type, which is the
+                  --  proper implementation type for the aggregate, and convert
+                  --  it to the intended target type.
 
                   Discriminant := First_Stored_Discriminant (Base_Type (Typ));
                   while Present (Discriminant) loop
@@ -5434,8 +5741,8 @@ package body Exp_Aggr is
                   Analyze (N);
 
                --  Case where we do not have fewer new discriminants than
-               --  stored discriminants, so in this case we can simply
-               --  use the stored discriminants of the subtype.
+               --  stored discriminants, so in this case we can simply use the
+               --  stored discriminants of the subtype.
 
                else
                   Prepend_Stored_Values (Typ);
@@ -5498,7 +5805,7 @@ package body Exp_Aggr is
 
             if Present (Orig_Tag) then
                Tag_Value := Orig_Tag;
-            elsif VM_Target /= No_VM then
+            elsif not Tagged_Type_Expansion then
                Tag_Value := Empty;
             else
                Tag_Value :=
@@ -5562,7 +5869,7 @@ package body Exp_Aggr is
             --  For a root type, the tag component is added (unless compiling
             --  for the VMs, where tags are implicit).
 
-            elsif VM_Target = No_VM then
+            elsif Tagged_Type_Expansion then
                declare
                   Tag_Name  : constant Node_Id :=
                                 New_Occurrence_Of
@@ -5593,8 +5900,7 @@ package body Exp_Aggr is
       C     : Node_Id;
       Expr  : Node_Id;
    begin
-      pragma Assert (Nkind (N) = N_Aggregate
-         or else Nkind (N) = N_Extension_Aggregate);
+      pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
 
       if No (Comps) then
          return False;
@@ -5622,8 +5928,8 @@ package body Exp_Aggr is
          Expr := Expression (C);
 
          if Present (Expr)
-           and then (Nkind (Expr) = N_Aggregate
-                     or else Nkind (Expr) = N_Extension_Aggregate)
+           and then
+             Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
            and then Has_Default_Init_Comps (Expr)
          then
             return True;
@@ -5665,7 +5971,7 @@ package body Exp_Aggr is
 
    begin
       return Static_Dispatch_Tables
-        and then VM_Target = No_VM
+        and then Tagged_Type_Expansion
         and then RTU_Loaded (Ada_Tags)
 
          --  Avoid circularity when rebuilding the compiler
@@ -5812,10 +6118,10 @@ package body Exp_Aggr is
          --  Values of bounds if compile time known
 
          function Get_Component_Val (N : Node_Id) return Uint;
-         --  Given a expression value N of the component type Ctyp, returns
-         --  A value of Csiz (component size) bits representing this value.
-         --  If the value is non-static or any other reason exists why the
-         --  value cannot be returned, then Not_Handled is raised.
+         --  Given a expression value N of the component type Ctyp, returns a
+         --  value of Csiz (component size) bits representing this value. If
+         --  the value is non-static or any other reason exists why the value
+         --  cannot be returned, then Not_Handled is raised.
 
          -----------------------
          -- Get_Component_Val --
@@ -5831,9 +6137,9 @@ package body Exp_Aggr is
 
             Analyze_And_Resolve (N, Ctyp);
 
-            --  Must have a compile time value. String literals have to
-            --  be converted into temporaries as well, because they cannot
-            --  easily be converted into their bit representation.
+            --  Must have a compile time value. String literals have to be
+            --  converted into temporaries as well, because they cannot easily
+            --  be converted into their bit representation.
 
             if not Compile_Time_Known_Value (N)
               or else Nkind (N) = N_String_Literal
@@ -5878,18 +6184,17 @@ package body Exp_Aggr is
             return False;
          end if;
 
-         --  At this stage we have a suitable aggregate for handling
-         --  at compile time (the only remaining checks, are that the
-         --  values of expressions in the aggregate are compile time
-         --  known (check performed by Get_Component_Val), and that
-         --  any subtypes or ranges are statically known.
+         --  At this stage we have a suitable aggregate for handling at compile
+         --  time (the only remaining checks are that the values of expressions
+         --  in the aggregate are compile time known (check is performed by
+         --  Get_Component_Val), and that any subtypes or ranges are statically
+         --  known.
 
-         --  If the aggregate is not fully positional at this stage,
-         --  then convert it to positional form. Either this will fail,
-         --  in which case we can do nothing, or it will succeed, in
-         --  which case we have succeeded in handling the aggregate,
-         --  or it will stay an aggregate, in which case we have failed
-         --  to handle this case.
+         --  If the aggregate is not fully positional at this stage, then
+         --  convert it to positional form. Either this will fail, in which
+         --  case we can do nothing, or it will succeed, in which case we have
+         --  succeeded in handling the aggregate, or it will stay an aggregate,
+         --  in which case we have failed to handle this case.
 
          if Present (Component_Associations (N)) then
             Convert_To_Positional
@@ -5907,13 +6212,12 @@ package body Exp_Aggr is
             --  The length of the array (number of elements)
 
             Aggregate_Val : Uint;
-            --  Value of aggregate. The value is set in the low order
-            --  bits of this value. For the little-endian case, the
-            --  values are stored from low-order to high-order and
-            --  for the big-endian case the values are stored from
-            --  high-order to low-order. Note that gigi will take care
-            --  of the conversions to left justify the value in the big
-            --  endian case (because of left justified modular type
+            --  Value of aggregate. The value is set in the low order bits of
+            --  this value. For the little-endian case, the values are stored
+            --  from low-order to high-order and for the big-endian case the
+            --  values are stored from high-order to low-order. Note that gigi
+            --  will take care of the conversions to left justify the value in
+            --  the big endian case (because of left justified modular type
             --  processing), so we do not have to worry about that here.
 
             Lit : Node_Id;
@@ -5929,10 +6233,9 @@ package body Exp_Aggr is
             --  Next expression from positional parameters of aggregate
 
          begin
-            --  For little endian, we fill up the low order bits of the
-            --  target value. For big endian we fill up the high order
-            --  bits of the target value (which is a left justified
-            --  modular value).
+            --  For little endian, we fill up the low order bits of the target
+            --  value. For big endian we fill up the high order bits of the
+            --  target value (which is a left justified modular value).
 
             if Bytes_Big_Endian xor Debug_Flag_8 then
                Shift := Csiz * (Len - 1);
@@ -6054,9 +6357,9 @@ package body Exp_Aggr is
    is
       L1, L2, H1, H2 : Node_Id;
    begin
-      --  No sliding if the type of the object is not established yet, if
-      --  it is an unconstrained type whose actual subtype comes from the
-      --  aggregate, or if the two types are identical.
+      --  No sliding if the type of the object is not established yet, if it is
+      --  an unconstrained type whose actual subtype comes from the aggregate,
+      --  or if the two types are identical.
 
       if not Is_Array_Type (Obj_Type) then
          return False;
@@ -6244,7 +6547,8 @@ package body Exp_Aggr is
             else
                --  The aggregate is static if all components are literals,
                --  or else all its components are static aggregates for the
-               --  component type.
+               --  component type. We also limit the size of a static aggregate
+               --  to prevent runaway static expressions.
 
                if Is_Array_Type (Comp_Type)
                  or else Is_Record_Type (Comp_Type)
@@ -6258,6 +6562,9 @@ package body Exp_Aggr is
 
                elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
                   return False;
+
+               elsif not Aggr_Size_OK (N, Typ) then
+                  return False;
                end if;
 
                --  Create a positional aggregate with the right number of
@@ -6269,7 +6576,13 @@ package body Exp_Aggr is
                loop
                   Append_To
                     (Expressions (Agg), New_Copy (Expression (Expr)));
-                  Set_Etype (Last (Expressions (Agg)), Component_Type (Typ));
+
+                  --  The copied expression must be analyzed and resolved.
+                  --  Besides setting the type, this ensures that static
+                  --  expressions are appropriately marked as such.
+
+                  Analyze_And_Resolve
+                    (Last (Expressions (Agg)), Component_Type (Typ));
                end loop;
 
                Set_Aggregate_Bounds (Agg, Bounds);
@@ -6286,4 +6599,5 @@ package body Exp_Aggr is
          return False;
       end if;
    end Static_Array_Aggregate;
+
 end Exp_Aggr;