OSDN Git Service

2005-03-29 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch12.adb
index 2e0534a..661ac76 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2004, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2005, 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- --
@@ -51,6 +51,7 @@ with Sem_Ch7;  use Sem_Ch7;
 with Sem_Ch8;  use Sem_Ch8;
 with Sem_Ch10; use Sem_Ch10;
 with Sem_Ch13; use Sem_Ch13;
+with Sem_Disp; use Sem_Disp;
 with Sem_Elab; use Sem_Elab;
 with Sem_Elim; use Sem_Elim;
 with Sem_Eval; use Sem_Eval;
@@ -254,14 +255,18 @@ package body Sem_Ch12 is
    --  in-out, because in the case of an anonymous type the entity is
    --  actually created in the procedure.
 
-   --  The following procedures treat other kinds of formal parameters.
+   --  The following procedures treat other kinds of formal parameters
 
    procedure Analyze_Formal_Derived_Type
      (N   : Node_Id;
       T   : Entity_Id;
       Def : Node_Id);
 
-   --  All the following need comments???
+   --  The following subprograms create abbreviated declarations for formal
+   --  scalar types. We introduce an anonymous base of the proper class for
+   --  each of them, and define the formals as constrained first subtypes of
+   --  their bases. The bounds are expressions that are non-static in the
+   --  generic.
 
    procedure Analyze_Formal_Decimal_Fixed_Point_Type
                                                 (T : Entity_Id; Def : Node_Id);
@@ -286,8 +291,7 @@ package body Sem_Ch12 is
    function Analyze_Associations
      (I_Node  : Node_Id;
       Formals : List_Id;
-      F_Copy  : List_Id)
-      return    List_Id;
+      F_Copy  : List_Id) return List_Id;
    --  At instantiation time, build the list of associations between formals
    --  and actuals. Each association becomes a renaming declaration for the
    --  formal entity. F_Copy is the analyzed list of formals in the generic
@@ -313,7 +317,7 @@ package body Sem_Ch12 is
    --  On return, the node N has been rewritten with the actual body.
 
    procedure Check_Formal_Packages (P_Id : Entity_Id);
-   --  Apply the following to all formal packages in generic associations.
+   --  Apply the following to all formal packages in generic associations
 
    procedure Check_Formal_Package_Instance
      (Formal_Pack : Entity_Id;
@@ -359,8 +363,7 @@ package body Sem_Ch12 is
    function Contains_Instance_Of
      (Inner : Entity_Id;
       Outer : Entity_Id;
-      N     : Node_Id)
-      return  Boolean;
+      N     : Node_Id) return Boolean;
    --  Inner is instantiated within the generic Outer. Check whether Inner
    --  directly or indirectly contains an instance of Outer or of one of its
    --  parents, in the case of a subunit. Each generic unit holds a list of
@@ -368,16 +371,20 @@ package body Sem_Ch12 is
    --  determines whether the set of such lists contains a cycle, i.e. an
    --  illegal circular instantiation.
 
-   function Denotes_Formal_Package (Pack : Entity_Id) return Boolean;
+   function Denotes_Formal_Package
+     (Pack    : Entity_Id;
+      On_Exit : Boolean := False) return Boolean;
    --  Returns True if E is a formal package of an enclosing generic, or
-   --  the actual for such a formal in an enclosing instantiation. Used in
-   --  Restore_Private_Views, to keep the formals of such a package visible
-   --  on exit from an inner instantiation.
+   --  the actual for such a formal in an enclosing instantiation. If such
+   --  a package is used as a formal in an nested generic, or as an actual
+   --  in a nested instantiation, the visibility of ITS formals should not
+   --  be modified. When called from within Restore_Private_Views, the flag
+   --  On_Exit is true, to indicate that the search for a possible enclosing
+   --  instance should ignore the current one.
 
    function Find_Actual_Type
      (Typ       : Entity_Id;
-      Gen_Scope : Entity_Id)
-      return      Entity_Id;
+      Gen_Scope : Entity_Id) return Entity_Id;
    --  When validating the actual types of a child instance, check whether
    --  the formal is a formal type of the parent unit, and retrieve the current
    --  actual for it. Typ is the entity in the analyzed formal type declaration
@@ -391,14 +398,18 @@ package body Sem_Ch12 is
 
    function In_Same_Declarative_Part
      (F_Node : Node_Id;
-      Inst   : Node_Id)
-      return   Boolean;
+      Inst   : Node_Id) return Boolean;
    --  True if the instantiation Inst and the given freeze_node F_Node appear
    --  within the same declarative part, ignoring subunits, but with no inter-
    --  vening suprograms or concurrent units. If true, the freeze node
    --  of the instance can be placed after the freeze node of the parent,
    --  which it itself an instance.
 
+   function In_Main_Context (E : Entity_Id) return Boolean;
+   --  Check whether an instantiation is in the context of the main unit.
+   --  Used to determine whether its body should be elaborated to allow
+   --  front-end inlining.
+
    procedure Set_Instance_Env
      (Gen_Unit : Entity_Id;
       Act_Unit : Entity_Id);
@@ -464,11 +475,11 @@ package body Sem_Ch12 is
    --  When compiling an instance of a child unit the parent (which is
    --  itself an instance) is an enclosing scope that must be made
    --  immediately visible. This procedure is also used to install the non-
-   --  generic parent of a generic child unit when compiling its body, so that
-   --  full views of types in the parent are made visible.
+   --  generic parent of a generic child unit when compiling its body, so
+   --  that full views of types in the parent are made visible.
 
    procedure Remove_Parent (In_Body : Boolean := False);
-   --  Reverse effect after instantiation of child is complete.
+   --  Reverse effect after instantiation of child is complete
 
    procedure Inline_Instance_Body
      (N        : Node_Id;
@@ -479,33 +490,32 @@ package body Sem_Ch12 is
    --  that successive instantiations succeed.
 
    --  The functions Instantiate_XXX perform various legality checks and build
-   --  the declarations for instantiated generic parameters.
-   --  Need to describe what the parameters are ???
+   --  the declarations for instantiated generic parameters. In all of these
+   --  Formal is the entity in the generic unit, Actual is the entity of
+   --  expression in the generic associations, and Analyzed_Formal is the
+   --  formal in the generic copy, which contains the semantic information to
+   --  be used to validate the actual.
 
    function Instantiate_Object
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            List_Id;
+      Analyzed_Formal : Node_Id) return List_Id;
 
    function Instantiate_Type
      (Formal          : Node_Id;
       Actual          : Node_Id;
       Analyzed_Formal : Node_Id;
-      Actual_Decls    : List_Id)
-      return            Node_Id;
+      Actual_Decls    : List_Id) return Node_Id;
 
    function Instantiate_Formal_Subprogram
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            Node_Id;
+      Analyzed_Formal : Node_Id) return Node_Id;
 
    function Instantiate_Formal_Package
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            List_Id;
+      Analyzed_Formal : Node_Id) return List_Id;
    --  If the formal package is declared with a box, special visibility rules
    --  apply to its formals: they are in the visible part of the package. This
    --  is true in the declarative region of the formal package, that is to say
@@ -639,7 +649,7 @@ package body Sem_Ch12 is
    procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
    function  Next_Assoc     (E : Assoc_Ptr) return Assoc_Ptr;
    function Get_Gen_Id      (E : Assoc_Ptr) return Entity_Id;
-   function Hash            (F : Entity_Id)   return HTable_Range;
+   function Hash            (F : Entity_Id) return HTable_Range;
 
    package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
       Header_Num => HTable_Range,
@@ -673,12 +683,19 @@ package body Sem_Ch12 is
    --  that the visibility data structures be properly initialized. Once the
    --  generic is unit is validated, Set_Instance_Env completes Save_Env.
 
+   Parent_Unit_Visible : Boolean := False;
+   --  Parent_Unit_Visible is used when the generic is a child unit, and
+   --  indicates whether the ultimate parent of the generic is visible in the
+   --  instantiation environment. It is used to reset the visiblity of the
+   --  parent at the end of the instantiation (see Remove_Parent).
+
    type Instance_Env is record
       Ada_Version         : Ada_Version_Type;
       Instantiated_Parent : Assoc;
       Exchanged_Views     : Elist_Id;
       Hidden_Entities     : Elist_Id;
       Current_Sem_Unit    : Unit_Number_Type;
+      Parent_Unit_Visible : Boolean := False;
    end record;
 
    package Instance_Envs is new Table.Table (
@@ -755,14 +772,12 @@ package body Sem_Ch12 is
    function Analyze_Associations
      (I_Node  : Node_Id;
       Formals : List_Id;
-      F_Copy  : List_Id)
-      return    List_Id
+      F_Copy  : List_Id) return List_Id
    is
-      Actual_Types    : constant Elist_Id  := New_Elmt_List;
-      Assoc           : constant List_Id   := New_List;
-      Defaults        : constant Elist_Id  := New_Elmt_List;
-      Gen_Unit        : constant Entity_Id := Defining_Entity
-                                                (Parent (F_Copy));
+      Actual_Types : constant Elist_Id  := New_Elmt_List;
+      Assoc        : constant List_Id   := New_List;
+      Defaults     : constant Elist_Id  := New_Elmt_List;
+      Gen_Unit     : constant Entity_Id := Defining_Entity (Parent (F_Copy));
       Actuals         : List_Id;
       Actual          : Node_Id;
       Formal          : Node_Id;
@@ -778,9 +793,8 @@ package body Sem_Ch12 is
       Num_Actuals     : Int := 0;
 
       function Matching_Actual
-        (F    : Entity_Id;
-         A_F  : Entity_Id)
-         return Node_Id;
+        (F   : Entity_Id;
+         A_F : Entity_Id) return Node_Id;
       --  Find actual that corresponds to a given a formal parameter. If the
       --  actuals are positional, return the next one, if any. If the actuals
       --  are named, scan the parameter associations to find the right one.
@@ -801,9 +815,8 @@ package body Sem_Ch12 is
       ---------------------
 
       function Matching_Actual
-        (F    : Entity_Id;
-         A_F  : Entity_Id)
-         return Node_Id
+        (F   : Entity_Id;
+         A_F : Entity_Id) return Node_Id
       is
          Found : Node_Id;
          Prev  : Node_Id;
@@ -881,7 +894,7 @@ package body Sem_Ch12 is
             case Nkind (Formal) is
 
                when N_Formal_Subprogram_Declaration =>
-                  exit when Kind = N_Formal_Subprogram_Declaration
+                  exit when Kind in N_Formal_Subprogram_Declaration
                     and then
                       Chars
                         (Defining_Unit_Name (Specification (Formal))) =
@@ -902,7 +915,7 @@ package body Sem_Ch12 is
                   --  unrecognized pragmas.
 
                   exit when
-                    Kind /= N_Formal_Subprogram_Declaration
+                    Kind not in N_Formal_Subprogram_Declaration
                       and then Kind /= N_Subprogram_Declaration
                       and then Kind /= N_Freeze_Entity
                       and then Kind /= N_Null_Statement
@@ -1040,7 +1053,7 @@ package body Sem_Ch12 is
                   then
                      Temp_Formal := First (Formals);
                      while Present (Temp_Formal) loop
-                        if Nkind (Temp_Formal) =
+                        if Nkind (Temp_Formal) in
                              N_Formal_Subprogram_Declaration
                           and then Temp_Formal /= Formal
                           and then
@@ -1214,14 +1227,19 @@ package body Sem_Ch12 is
       then
          Error_Msg_N ("premature usage of incomplete type", Def);
 
+      --  Check that range constraint is not allowed on the component type
+      --  of a generic formal array type (AARM 12.5.3(3))
+
       elsif Is_Internal (Component_Type (T))
+        and then Present (Subtype_Indication (Component_Definition (Def)))
         and then Nkind (Original_Node
                         (Subtype_Indication (Component_Definition (Def))))
-          /= N_Attribute_Reference
+          = N_Subtype_Indication
       then
          Error_Msg_N
-           ("only a subtype mark is allowed in a formal",
-              Subtype_Indication (Component_Definition (Def)));
+           ("in a formal, a subtype indication can only be "
+             & "a subtype mark ('R'M 12.5.3(3))",
+             Subtype_Indication (Component_Definition (Def)));
       end if;
 
    end Analyze_Formal_Array_Type;
@@ -1276,6 +1294,7 @@ package body Sem_Ch12 is
       Set_Delta_Value    (T, Delta_Val);
       Set_Small_Value    (T, Delta_Val);
       Set_Scalar_Range   (T, Scalar_Range (Base));
+      Set_Is_Constrained (T);
 
       Check_Restriction (No_Fixed_Point, Def);
    end Analyze_Formal_Decimal_Fixed_Point_Type;
@@ -1354,12 +1373,17 @@ package body Sem_Ch12 is
       Lo  : Node_Id;
       Hi  : Node_Id;
 
+      Base : constant Entity_Id :=
+               New_Internal_Entity
+                 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
    begin
-      Enter_Name     (T);
-      Set_Ekind      (T, E_Enumeration_Type);
-      Set_Etype      (T, T);
-      Init_Size      (T, 8);
-      Init_Alignment (T);
+      Enter_Name          (T);
+      Set_Ekind           (T, E_Enumeration_Subtype);
+      Set_Etype           (T, Base);
+      Init_Size           (T, 8);
+      Init_Alignment      (T);
+      Set_Is_Generic_Type (T);
+      Set_Is_Constrained  (T);
 
       --  For semantic analysis, the bounds of the type must be set to some
       --  non-static value. The simplest is to create attribute nodes for
@@ -1383,6 +1407,14 @@ package body Sem_Ch12 is
           Low_Bound => Lo,
           High_Bound => Hi));
 
+      Set_Ekind           (Base, E_Enumeration_Type);
+      Set_Etype           (Base, Base);
+      Init_Size           (Base, 8);
+      Init_Alignment      (Base);
+      Set_Is_Generic_Type (Base);
+      Set_Scalar_Range    (Base, Scalar_Range (T));
+      Set_Parent          (Base, Parent (Def));
+
    end Analyze_Formal_Discrete_Type;
 
    ----------------------------------
@@ -1401,12 +1433,13 @@ package body Sem_Ch12 is
       --  the generic itself.
 
       Enter_Name (T);
-      Set_Ekind        (T, E_Floating_Point_Subtype);
-      Set_Etype        (T, Base);
-      Set_Size_Info    (T,              (Standard_Float));
-      Set_RM_Size      (T, RM_Size      (Standard_Float));
-      Set_Digits_Value (T, Digits_Value (Standard_Float));
-      Set_Scalar_Range (T, Scalar_Range (Standard_Float));
+      Set_Ekind          (T, E_Floating_Point_Subtype);
+      Set_Etype          (T, Base);
+      Set_Size_Info      (T,              (Standard_Float));
+      Set_RM_Size        (T, RM_Size      (Standard_Float));
+      Set_Digits_Value   (T, Digits_Value (Standard_Float));
+      Set_Scalar_Range   (T, Scalar_Range (Standard_Float));
+      Set_Is_Constrained (T);
 
       Set_Is_Generic_Type (Base);
       Set_Etype           (Base, Base);
@@ -1489,7 +1522,7 @@ package body Sem_Ch12 is
          Set_Ekind (Id, K);
          Set_Etype (Id, T);
 
-      --  Case of generic IN OUT parameter.
+      --  Case of generic IN OUT parameter
 
       else
          --  If the formal has an unconstrained type, construct its
@@ -1559,6 +1592,7 @@ package body Sem_Ch12 is
         Make_Range (Loc,
           Low_Bound  => Make_Real_Literal (Loc, Ureal_1),
           High_Bound => Make_Real_Literal (Loc, Ureal_1)));
+      Set_Is_Constrained   (T);
 
       Set_Is_Generic_Type (Base);
       Set_Etype           (Base, Base);
@@ -1630,7 +1664,7 @@ package body Sem_Ch12 is
          end if;
       end if;
 
-      --  Check for a formal package that is a package renaming.
+      --  Check for a formal package that is a package renaming
 
       if Present (Renamed_Object (Gen_Unit)) then
          Gen_Unit := Renamed_Object (Gen_Unit);
@@ -1749,7 +1783,7 @@ package body Sem_Ch12 is
    begin
       New_Private_Type (N, T, Def);
 
-      --  Set the size to an arbitrary but legal value.
+      --  Set the size to an arbitrary but legal value
 
       Set_Size_Info (T, Standard_Integer);
       Set_RM_Size   (T, RM_Size (Standard_Integer));
@@ -1770,11 +1804,12 @@ package body Sem_Ch12 is
    begin
       Enter_Name (T);
 
-      Set_Ekind        (T, E_Signed_Integer_Subtype);
-      Set_Etype        (T, Base);
-      Set_Size_Info    (T, Standard_Integer);
-      Set_RM_Size      (T, RM_Size (Standard_Integer));
-      Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
+      Set_Ekind          (T, E_Signed_Integer_Subtype);
+      Set_Etype          (T, Base);
+      Set_Size_Info      (T, Standard_Integer);
+      Set_RM_Size        (T, RM_Size (Standard_Integer));
+      Set_Scalar_Range   (T, Scalar_Range (Standard_Integer));
+      Set_Is_Constrained (T);
 
       Set_Is_Generic_Type (Base);
       Set_Size_Info       (Base, Standard_Integer);
@@ -1808,6 +1843,25 @@ package body Sem_Ch12 is
       Set_Is_Formal_Subprogram (Nam);
       Set_Has_Completion (Nam);
 
+      if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
+         Set_Is_Abstract (Nam);
+         Set_Is_Dispatching_Operation (Nam);
+
+         declare
+            Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
+
+         begin
+            if not Present (Ctrl_Type) then
+               Error_Msg_N
+                 ("abstract formal subprogram must have a controlling type",
+                  N);
+
+            else
+               Check_Controlling_Formals (Ctrl_Type, Nam);
+            end if;
+         end;
+      end if;
+
       --  Default name is resolved at the point of instantiation
 
       if Box_Present (N) then
@@ -1952,7 +2006,7 @@ package body Sem_Ch12 is
             Defining_Identifier (First (Discriminant_Specifications (N))));
       end if;
 
-      --  Enter the new name, and branch to specific routine.
+      --  Enter the new name, and branch to specific routine
 
       case Nkind (Def) is
          when N_Formal_Private_Type_Definition         =>
@@ -2115,7 +2169,7 @@ package body Sem_Ch12 is
       Id := Defining_Entity (N);
       Generate_Definition (Id);
 
-      --  Expansion is not applied to generic units.
+      --  Expansion is not applied to generic units
 
       Start_Generic;
 
@@ -2319,9 +2373,7 @@ package body Sem_Ch12 is
 
          else
             E := First_Entity (Gen_Unit);
-
             while Present (E) loop
-
                if Is_Subprogram (E)
                  and then Is_Inlined (E)
                then
@@ -2343,7 +2395,7 @@ package body Sem_Ch12 is
 
       Text_IO_Kludge (Name (N));
 
-      --  Make node global for error reporting.
+      --  Make node global for error reporting
 
       Instantiation_Node := N;
 
@@ -2425,7 +2477,7 @@ package body Sem_Ch12 is
 
       Set_Entity (Gen_Id, Gen_Unit);
 
-      --  If generic is a renaming, get original generic unit.
+      --  If generic is a renaming, get original generic unit
 
       if Present (Renamed_Object (Gen_Unit))
         and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
@@ -2433,7 +2485,7 @@ package body Sem_Ch12 is
          Gen_Unit := Renamed_Object (Gen_Unit);
       end if;
 
-      --  Verify that there are no circular instantiations.
+      --  Verify that there are no circular instantiations
 
       if In_Open_Scopes (Gen_Unit) then
          Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
@@ -2463,7 +2515,7 @@ package body Sem_Ch12 is
 
          Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
 
-         --  Copy original generic tree, to produce text for instantiation.
+         --  Copy original generic tree, to produce text for instantiation
 
          Act_Tree :=
            Copy_Generic_Node
@@ -2572,7 +2624,8 @@ package body Sem_Ch12 is
               and then Expander_Active
               and then (not Is_Child_Unit (Gen_Unit)
                          or else not Is_Generic_Unit (Scope (Gen_Unit)))
-              and then Is_In_Main_Unit (N)
+              and then (Is_In_Main_Unit (N)
+                          or else In_Main_Context (Current_Scope))
               and then Nkind (Parent (N)) /= N_Compilation_Unit
               and then Might_Inline_Subp
               and then not Is_Actual_Pack
@@ -2596,8 +2649,9 @@ package body Sem_Ch12 is
             --  If front_end_inlining is enabled, do not instantiate a
             --  body if within a generic context.
 
-            if Front_End_Inlining
-              and then not Expander_Active
+            if (Front_End_Inlining
+                  and then not Expander_Active)
+              or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
             then
                Needs_Body := False;
             end if;
@@ -2768,13 +2822,15 @@ package body Sem_Ch12 is
 
                Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
 
-               --  If this is the main unit, replace the main entity as well.
+               --  If this is the main unit, replace the main entity as well
 
                if Current_Sem_Unit = Main_Unit then
                   Main_Unit_Entity := Act_Decl_Id;
                end if;
             end if;
 
+            --  There is a problem with inlining here.
+
             Set_Unit (Parent (N), Act_Decl);
             Set_Parent_Spec (Act_Decl, Parent_Spec (N));
             Analyze (Act_Decl);
@@ -2846,6 +2902,17 @@ package body Sem_Ch12 is
          Inline_Instance_Body (N, Gen_Unit, Act_Decl);
       end if;
 
+      --  The following is a tree patch for ASIS: ASIS needs separate nodes
+      --  to be used as defining identifiers for a formal package and for the
+      --  corresponding expanded package
+
+      if Nkind (N) = N_Formal_Package_Declaration then
+         Act_Decl_Id := New_Copy (Defining_Entity (N));
+         Set_Comes_From_Source (Act_Decl_Id, True);
+         Set_Is_Generic_Instance (Act_Decl_Id, False);
+         Set_Defining_Identifier (N, Act_Decl_Id);
+      end if;
+
    exception
       when Instantiation_Error =>
          if Parent_Installed then
@@ -3010,7 +3077,7 @@ package body Sem_Ch12 is
                New_Scope (Curr_Scope);
                Set_Is_Immediately_Visible (Curr_Scope);
 
-               --  Finally, restore inner scopes as well.
+               --  Finally, restore inner scopes as well
 
                for J in reverse 1 .. Num_Inner loop
                   New_Scope (Inner_Scopes (J));
@@ -3018,12 +3085,34 @@ package body Sem_Ch12 is
             end if;
 
             Restore_Scope_Stack (Handle_Use => False);
+
+            if Present (Curr_Scope)
+              and then
+                (In_Private_Part (Curr_Scope)
+                  or else In_Package_Body (Curr_Scope))
+            then
+               --  Install private declaration of ancestor units, which
+               --  are currently available. Restore_Scope_Stack and
+               --  Install_Context only install the visible part of parents.
+
+               declare
+                  Par : Entity_Id;
+               begin
+                  Par := Scope (Curr_Scope);
+                  while (Present (Par))
+                    and then Par /= Standard_Standard
+                  loop
+                     Install_Private_Declarations (Par);
+                     Par := Scope (Par);
+                  end loop;
+               end;
+            end if;
          end if;
 
-         --  Restore use clauses. For a child unit, use clauses in the
-         --  parents are restored when installing the context, so only
-         --  those in inner scopes (and those local to the child unit itself)
-         --  need to be installed explicitly.
+         --  Restore use clauses. For a child unit, use clauses in the parents
+         --  are restored when installing the context, so only those in inner
+         --  scopes (and those local to the child unit itself) need to be
+         --  installed explicitly.
 
          if Is_Child_Unit (Curr_Unit)
            and then Removed
@@ -3046,7 +3135,7 @@ package body Sem_Ch12 is
             Set_Is_Generic_Instance (Instances (J), True);
          end loop;
 
-      --  If generic unit is in current unit, current context is correct.
+      --  If generic unit is in current unit, current context is correct
 
       else
          Instantiate_Package_Body
@@ -3225,7 +3314,7 @@ package body Sem_Ch12 is
             Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
          end if;
 
-         --  The instance is not a freezing point for the new subprogram.
+         --  The instance is not a freezing point for the new subprogram
 
          Set_Is_Frozen (Act_Decl_Id, False);
 
@@ -3254,7 +3343,7 @@ package body Sem_Ch12 is
 
       Text_IO_Kludge (Gen_Id);
 
-      --  Make node global for error reporting.
+      --  Make node global for error reporting
 
       Instantiation_Node := N;
       Pre_Analyze_Actuals (N);
@@ -3345,14 +3434,14 @@ package body Sem_Ch12 is
 
          Set_Instance_Env (Gen_Unit, Empty);
 
-         --  Initialize renamings map, for error checking.
+         --  Initialize renamings map, for error checking
 
          Generic_Renamings.Set_Last (0);
          Generic_Renamings_HTable.Reset;
 
          Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
 
-         --  Copy original generic tree, to produce text for instantiation.
+         --  Copy original generic tree, to produce text for instantiation
 
          Act_Tree :=
            Copy_Generic_Node
@@ -3497,6 +3586,7 @@ package body Sem_Ch12 is
         or else Nkind (Assoc) = N_Extension_Aggregate
       then
          return Assoc;
+
       else
          --  If the node is part of an inner generic, it may itself have been
          --  remapped into a further generic copy. Associated_Node is otherwise
@@ -3570,7 +3660,7 @@ package body Sem_Ch12 is
       Set_Library_Unit  (Decl_Cunit, Body_Cunit);
       Set_Library_Unit  (Body_Cunit, Decl_Cunit);
 
-      --  Preserve the private nature of the package if needed.
+      --  Preserve the private nature of the package if needed
 
       Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
 
@@ -3711,7 +3801,7 @@ package body Sem_Ch12 is
         (E1, E2 : Entity_Id) return Boolean
       is
          function Original_Entity (E : Entity_Id) return Entity_Id;
-         --  Follow chain of renamings to the ultimate ancestor.
+         --  Follow chain of renamings to the ultimate ancestor
 
          ---------------------
          -- Original_Entity --
@@ -3949,6 +4039,37 @@ package body Sem_Ch12 is
       E      : Entity_Id;
       Astype : Entity_Id;
 
+      function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
+      --  For a formal that is an array type, the component type is often
+      --  a previous formal in the same unit. The privacy status of the
+      --  component type will have been examined earlier in the traversal
+      --  of the corresponding actuals, and this status should not be
+      --  modified for the array type itself.
+      --  To detect this case we have to rescan the list of formals, which
+      --  is usually short enough to ignore the resulting inefficiency.
+
+      function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
+         Prev : Entity_Id;
+      begin
+         Prev := First_Entity (Instance);
+         while Present (Prev) loop
+            if Is_Type (Prev)
+              and then Nkind (Parent (Prev)) = N_Subtype_Declaration
+              and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
+              and then Entity (Subtype_Indication (Parent (Prev))) = Typ
+            then
+               return True;
+            elsif Prev = E then
+               return False;
+            else
+               Next_Entity (Prev);
+            end if;
+         end loop;
+         return False;
+      end Denotes_Previous_Actual;
+
+   --  Start of processing for Check_Generic_Actuals
+
    begin
       E := First_Entity (Instance);
       while Present (E) loop
@@ -3957,9 +4078,17 @@ package body Sem_Ch12 is
            and then Scope (Etype (E)) /= Instance
            and then Is_Entity_Name (Subtype_Indication (Parent (E)))
          then
-            Check_Private_View (Subtype_Indication (Parent (E)));
+            if Is_Array_Type (E)
+              and then Denotes_Previous_Actual (Component_Type (E))
+            then
+               null;
+            else
+               Check_Private_View (Subtype_Indication (Parent (E)));
+            end if;
             Set_Is_Generic_Actual_Type (E, True);
             Set_Is_Hidden (E, False);
+            Set_Is_Potentially_Use_Visible (E,
+              In_Use (Instance));
 
             --  We constructed the generic actual type as a subtype of
             --  the supplied type. This means that it normally would not
@@ -4013,10 +4142,11 @@ package body Sem_Ch12 is
             elsif Denotes_Formal_Package (E) then
                null;
 
-            elsif Present (Associated_Formal_Package (E))
-              and then Box_Present (Parent (Associated_Formal_Package (E)))
-            then
-               Check_Generic_Actuals (Renamed_Object (E), True);
+            elsif Present (Associated_Formal_Package (E)) then
+               if Box_Present (Parent (Associated_Formal_Package (E))) then
+                  Check_Generic_Actuals (Renamed_Object (E), True);
+               end if;
+
                Set_Is_Hidden (E, False);
             end if;
 
@@ -4050,9 +4180,8 @@ package body Sem_Ch12 is
 
       function Find_Generic_Child
         (Scop : Entity_Id;
-         Id   : Node_Id)
-         return Entity_Id;
-      --  Search generic parent for possible child unit with the given name.
+         Id   : Node_Id) return Entity_Id;
+      --  Search generic parent for possible child unit with the given name
 
       function In_Enclosing_Instance return Boolean;
       --  Within an instance of the parent, the child unit may be denoted
@@ -4065,8 +4194,7 @@ package body Sem_Ch12 is
 
       function Find_Generic_Child
         (Scop : Entity_Id;
-         Id   : Node_Id)
-         return Entity_Id
+         Id   : Node_Id) return Entity_Id
       is
          E : Entity_Id;
 
@@ -4220,7 +4348,7 @@ package body Sem_Ch12 is
                Set_Entity (S, E);
                Set_Etype (S, Etype (E));
 
-               --  Indicate that this is a reference to the parent.
+               --  Indicate that this is a reference to the parent
 
                if In_Extended_Main_Source_Unit (Gen_Id) then
                   Set_Is_Instantiated (Inst_Par);
@@ -4486,8 +4614,7 @@ package body Sem_Ch12 is
    function Contains_Instance_Of
      (Inner : Entity_Id;
       Outer : Entity_Id;
-      N     : Node_Id)
-      return  Boolean
+      N     : Node_Id) return Boolean
    is
       Elmt : Elmt_Id;
       Scop : Entity_Id;
@@ -4537,7 +4664,7 @@ package body Sem_Ch12 is
                Next_Elmt (Elmt);
             end loop;
 
-            --  Indicate that Inner is being instantiated within  Scop.
+            --  Indicate that Inner is being instantiated within Scop
 
             Append_Elmt (Inner, Inner_Instances (Scop));
          end if;
@@ -4559,8 +4686,7 @@ package body Sem_Ch12 is
    function Copy_Generic_Node
      (N             : Node_Id;
       Parent_Id     : Node_Id;
-      Instantiating : Boolean)
-      return          Node_Id
+      Instantiating : Boolean) return Node_Id
    is
       Ent   : Entity_Id;
       New_N : Node_Id;
@@ -4572,16 +4698,15 @@ package body Sem_Ch12 is
       --  value (Sloc, Uint, Char) in which case it need not be copied.
 
       procedure Copy_Descendants;
-      --  Common utility for various nodes.
+      --  Common utility for various nodes
 
       function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
-      --  Make copy of element list.
+      --  Make copy of element list
 
       function Copy_Generic_List
         (L         : List_Id;
-         Parent_Id : Node_Id)
-         return      List_Id;
-      --  Apply Copy_Node recursively to the members of a node list.
+         Parent_Id : Node_Id) return List_Id;
+      --  Apply Copy_Node recursively to the members of a node list
 
       function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
       --  True if an identifier is part of the defining program unit name
@@ -4664,8 +4789,7 @@ package body Sem_Ch12 is
 
       function Copy_Generic_List
         (L         : List_Id;
-         Parent_Id : Node_Id)
-         return      List_Id
+         Parent_Id : Node_Id) return List_Id
       is
          N     : Node_Id;
          New_L : List_Id;
@@ -5074,7 +5198,7 @@ package body Sem_Ch12 is
 
          begin
             if Present (T) then
-               --  Retrieve the allocator node in the generic copy.
+               --  Retrieve the allocator node in the generic copy
 
                Acc_T := Etype (Parent (Parent (T)));
                if Present (Acc_T)
@@ -5163,12 +5287,23 @@ package body Sem_Ch12 is
    -- Denotes_Formal_Package --
    ----------------------------
 
-   function Denotes_Formal_Package (Pack : Entity_Id) return Boolean is
-      Par  : constant Entity_Id := Current_Instantiated_Parent.Act_Id;
+   function Denotes_Formal_Package
+     (Pack    : Entity_Id;
+      On_Exit : Boolean := False) return Boolean
+   is
+      Par  : Entity_Id;
       Scop : constant Entity_Id := Scope (Pack);
       E    : Entity_Id;
 
    begin
+      if On_Exit then
+         Par :=
+           Instance_Envs.Table
+             (Instance_Envs.Last).Instantiated_Parent.Act_Id;
+      else
+         Par  := Current_Instantiated_Parent.Act_Id;
+      end if;
+
       if Ekind (Scop) = E_Generic_Package
         or else Nkind (Unit_Declaration_Node (Scop)) =
                                          N_Generic_Subprogram_Declaration
@@ -5227,8 +5362,7 @@ package body Sem_Ch12 is
 
    function Find_Actual_Type
      (Typ       : Entity_Id;
-      Gen_Scope : Entity_Id)
-      return      Entity_Id
+      Gen_Scope : Entity_Id) return Entity_Id
    is
       T : Entity_Id;
 
@@ -5287,7 +5421,7 @@ package body Sem_Ch12 is
       --  node for it.
 
       function True_Parent (N : Node_Id) return Node_Id;
-      --  For a subunit, return parent of corresponding stub.
+      --  For a subunit, return parent of corresponding stub
 
       -------------
       -- Earlier --
@@ -5300,7 +5434,7 @@ package body Sem_Ch12 is
          P2 : Node_Id := N2;
 
          procedure Find_Depth (P : in out Node_Id; D : in out Integer);
-         --  Find distance from given node to enclosing compilation unit.
+         --  Find distance from given node to enclosing compilation unit
 
          ----------------
          -- Find_Depth --
@@ -5685,6 +5819,7 @@ package body Sem_Ch12 is
       Saved.Exchanged_Views     := Exchanged_Views;
       Saved.Hidden_Entities     := Hidden_Entities;
       Saved.Current_Sem_Unit    := Current_Sem_Unit;
+      Saved.Parent_Unit_Visible := Parent_Unit_Visible;
       Instance_Envs.Increment_Last;
       Instance_Envs.Table (Instance_Envs.Last) := Saved;
 
@@ -5704,8 +5839,7 @@ package body Sem_Ch12 is
 
    function In_Same_Declarative_Part
      (F_Node : Node_Id;
-      Inst   : Node_Id)
-      return   Boolean
+      Inst   : Node_Id) return Boolean
    is
       Decls : constant Node_Id := Parent (F_Node);
       Nod   : Node_Id := Parent (Inst);
@@ -5737,6 +5871,51 @@ package body Sem_Ch12 is
    end In_Same_Declarative_Part;
 
    ---------------------
+   -- In_Main_Context --
+   ---------------------
+
+   function In_Main_Context (E : Entity_Id) return Boolean is
+      Context : List_Id;
+      Clause  : Node_Id;
+      Nam     : Node_Id;
+
+   begin
+      if not Is_Compilation_Unit (E)
+        or else Ekind (E) /= E_Package
+        or else In_Private_Part (E)
+      then
+         return False;
+      end if;
+
+      Context := Context_Items (Cunit (Main_Unit));
+
+      Clause  := First (Context);
+      while Present (Clause) loop
+         if Nkind (Clause) = N_With_Clause then
+            Nam := Name (Clause);
+
+            --  If the current scope is part of the context of the main unit,
+            --  analysis of the corresponding with_clause is not complete, and
+            --  the entity is not set. We use the Chars field directly, which
+            --  might produce false positives in rare cases, but guarantees
+            --  that we produce all the instance bodies we will need.
+
+            if (Nkind (Nam) = N_Identifier
+                 and then Chars (Nam) = Chars (E))
+              or else (Nkind (Nam) = N_Selected_Component
+                        and then Chars (Selector_Name (Nam)) = Chars (E))
+            then
+               return True;
+            end if;
+         end if;
+
+         Next (Clause);
+      end loop;
+
+      return False;
+   end In_Main_Context;
+
+   ---------------------
    -- Inherit_Context --
    ---------------------
 
@@ -5836,7 +6015,7 @@ package body Sem_Ch12 is
       Must_Delay : Boolean;
 
       function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
-      --  Find subprogram (if any) that encloses instance and/or generic body.
+      --  Find subprogram (if any) that encloses instance and/or generic body
 
       function True_Sloc (N : Node_Id) return Source_Ptr;
       --  If the instance is nested inside a generic unit, the Sloc of the
@@ -5844,7 +6023,11 @@ package body Sem_Ch12 is
       --  point of the current enclosing instance. Pending a better usage of
       --  Slocs to indicate instantiation places, we determine the place of
       --  origin of a node by finding the maximum sloc of any ancestor node.
-      --  Why is this not equivalent fo Top_Level_Location ???
+      --  Why is this not equivalent to Top_Level_Location ???
+
+      --------------------
+      -- Enclosing_Subp --
+      --------------------
 
       function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
          Scop : Entity_Id := Scope (Id);
@@ -5859,6 +6042,10 @@ package body Sem_Ch12 is
          return Scop;
       end Enclosing_Subp;
 
+      ---------------
+      -- True_Sloc --
+      ---------------
+
       function True_Sloc (N : Node_Id) return Source_Ptr is
          Res : Source_Ptr;
          N1  : Node_Id;
@@ -5995,7 +6182,7 @@ package body Sem_Ch12 is
       --  for the unit itself.
 
       procedure Install_Noninstance_Specs (Par : Entity_Id);
-      --  Install the scopes of noninstance parent units ending with Par.
+      --  Install the scopes of noninstance parent units ending with Par
 
       procedure Install_Spec (Par : Entity_Id);
       --  The child unit is within the declarative part of the parent, so
@@ -6010,13 +6197,11 @@ package body Sem_Ch12 is
 
       begin
          E := First_Entity (Par);
-
          while Present (E) loop
-
             if Ekind (E) = E_Package
               and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
             then
-               --  If this is the renaming for the parent instance, done.
+               --  If this is the renaming for the parent instance, done
 
                if Renamed_Object (E) = Par then
                   exit;
@@ -6063,6 +6248,10 @@ package body Sem_Ch12 is
                   Specification (Unit_Declaration_Node (Par));
 
       begin
+         if not Is_Child_Unit (Par) then
+            Parent_Unit_Visible := Is_Immediately_Visible (Par);
+         end if;
+
          New_Scope (Par);
          Set_Is_Immediately_Visible   (Par);
          Install_Visible_Declarations (Par);
@@ -6169,8 +6358,7 @@ package body Sem_Ch12 is
    function Instantiate_Formal_Package
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            List_Id
+      Analyzed_Formal : Node_Id) return List_Id
    is
       Loc         : constant Source_Ptr := Sloc (Actual);
       Actual_Pack : Entity_Id;
@@ -6195,8 +6383,7 @@ package body Sem_Ch12 is
 
       function Formal_Entity
         (F       : Node_Id;
-         Act_Ent : Entity_Id)
-         return    Entity_Id;
+         Act_Ent : Entity_Id) return Entity_Id;
       --  Returns the entity associated with the given formal F. In the
       --  case where F is a formal package, this function will iterate
       --  through all of F's formals and enter map associations from the
@@ -6207,8 +6394,7 @@ package body Sem_Ch12 is
 
       function Is_Instance_Of
         (Act_Spec : Entity_Id;
-         Gen_Anc  : Entity_Id)
-         return     Boolean;
+         Gen_Anc  : Entity_Id) return Boolean;
       --  The actual can be an instantiation of a generic within another
       --  instance, in which case there is no direct link from it to the
       --  original generic ancestor. In that case, we recognize that the
@@ -6227,6 +6413,12 @@ package body Sem_Ch12 is
       --  that the entities in P2 are mapped into those of P3. The mapping of
       --  entities has to be done recursively for nested packages.
 
+      procedure Process_Nested_Formal (Formal : Entity_Id);
+      --  If the current formal is declared with a box, its own formals are
+      --  visible in the instance, as they were in the generic, and their
+      --  Hidden flag must be reset. If some of these formals are themselves
+      --  packages declared with a box, the processing must be recursive.
+
       --------------------------
       -- Find_Matching_Actual --
       --------------------------
@@ -6268,8 +6460,7 @@ package body Sem_Ch12 is
 
       function Formal_Entity
         (F       : Node_Id;
-         Act_Ent : Entity_Id)
-         return    Entity_Id
+         Act_Ent : Entity_Id) return Entity_Id
       is
          Orig_Node : Node_Id := F;
          Act_Pkg   : Entity_Id;
@@ -6327,7 +6518,7 @@ package body Sem_Ch12 is
 
                   while Present (Actual_Ent)
                     and then Present (Formal_Node)
-                    and then Actual_Ent /= First_Private_Entity (Act_Ent)
+                    and then Actual_Ent /= First_Private_Entity (Act_Pkg)
                   loop
                      --  ???  Are the following calls also needed here:
                      --
@@ -6371,8 +6562,7 @@ package body Sem_Ch12 is
 
       function Is_Instance_Of
         (Act_Spec : Entity_Id;
-         Gen_Anc  : Entity_Id)
-         return     Boolean
+         Gen_Anc  : Entity_Id) return Boolean
       is
          Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
 
@@ -6380,7 +6570,7 @@ package body Sem_Ch12 is
          if No (Gen_Par) then
             return False;
 
-         --  Simplest case: the generic parent of the actual is the formal.
+         --  Simplest case: the generic parent of the actual is the formal
 
          elsif Gen_Par = Gen_Anc then
             return True;
@@ -6455,6 +6645,33 @@ package body Sem_Ch12 is
          end loop;
       end Map_Entities;
 
+      ---------------------------
+      -- Process_Nested_Formal --
+      ---------------------------
+
+      procedure Process_Nested_Formal (Formal : Entity_Id) is
+         Ent : Entity_Id;
+
+      begin
+         if Present (Associated_Formal_Package (Formal))
+           and then Box_Present (Parent (Associated_Formal_Package (Formal)))
+         then
+            Ent := First_Entity (Formal);
+            while Present (Ent) loop
+               Set_Is_Hidden (Ent, False);
+               Set_Is_Potentially_Use_Visible
+                 (Ent, Is_Potentially_Use_Visible (Formal));
+
+               if Ekind (Ent) = E_Package then
+                  exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
+                  Process_Nested_Formal (Ent);
+               end if;
+
+               Next_Entity (Ent);
+            end loop;
+         end if;
+      end Process_Nested_Formal;
+
    --  Start of processing for Instantiate_Formal_Package
 
    begin
@@ -6563,6 +6780,10 @@ package body Sem_Ch12 is
                   Set_Is_Potentially_Use_Visible
                     (Actual_Ent, In_Use (Actual_Pack));
 
+                  if Ekind (Actual_Ent) = E_Package then
+                     Process_Nested_Formal (Actual_Ent);
+                  end if;
+
                   if Present (Formal_Node) then
                      Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
 
@@ -6618,8 +6839,7 @@ package body Sem_Ch12 is
    function Instantiate_Formal_Subprogram
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            Node_Id
+      Analyzed_Formal : Node_Id) return Node_Id
    is
       Loc        : Source_Ptr := Sloc (Instantiation_Node);
       Formal_Sub : constant Entity_Id :=
@@ -6631,16 +6851,16 @@ package body Sem_Ch12 is
       New_Spec   : Node_Id;
 
       function From_Parent_Scope (Subp : Entity_Id) return Boolean;
-      --  If the generic is a child unit, the parent has been installed
-      --  on the scope stack, but a default subprogram cannot resolve to
-      --  something on the parent because that parent is not really part
-      --  of the visible context (it is there to resolve explicit local
-      --  entities). If the default has resolved in this way, we remove
-      --  the entity from immediate visibility and analyze the node again
-      --  to emit an error message or find another visible candidate.
+      --  If the generic is a child unit, the parent has been installed on the
+      --  scope stack, but a default subprogram cannot resolve to something on
+      --  the parent because that parent is not really part of the visible
+      --  context (it is there to resolve explicit local entities). If the
+      --  default has resolved in this way, we remove the entity from
+      --  immediate visibility and analyze the node again to emit an error
+      --  message or find another visible candidate.
 
       procedure Valid_Actual_Subprogram (Act : Node_Id);
-      --  Perform legality check and raise exception on failure.
+      --  Perform legality check and raise exception on failure
 
       -----------------------
       -- From_Parent_Scope --
@@ -6700,7 +6920,7 @@ package body Sem_Ch12 is
    begin
       New_Spec := New_Copy_Tree (Specification (Formal));
 
-      --  Create new entity for the actual (New_Copy_Tree does not).
+      --  Create new entity for the actual (New_Copy_Tree does not)
 
       Set_Defining_Unit_Name
         (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
@@ -6824,10 +7044,12 @@ package body Sem_Ch12 is
 
       --  The generic instantiation freezes the actual. This can only be
       --  done once the actual is resolved, in the analysis of the renaming
-      --  declaration. To indicate that must be done, we set the corresponding
-      --  spec of the node to point to the formal subprogram entity.
+      --  declaration. To make the formal subprogram entity available, we set
+      --  Corresponding_Formal_Spec to point to the formal subprogram entity.
+      --  This is also needed in Analyze_Subprogram_Renaming for the processing
+      --  of formal abstract subprograms.
 
-      Set_Corresponding_Spec (Decl_Node, Analyzed_S);
+      Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
 
       --  We cannot analyze the renaming declaration, and thus find the
       --  actual, until the all the actuals are assembled in the instance.
@@ -6876,8 +7098,7 @@ package body Sem_Ch12 is
    function Instantiate_Object
      (Formal          : Node_Id;
       Actual          : Node_Id;
-      Analyzed_Formal : Node_Id)
-      return            List_Id
+      Analyzed_Formal : Node_Id) return List_Id
    is
       Formal_Id : constant Entity_Id  := Defining_Identifier (Formal);
       Type_Id   : constant Node_Id    := Subtype_Mark (Formal);
@@ -6891,7 +7112,8 @@ package body Sem_Ch12 is
       Subt_Decl : Node_Id := Empty;
 
    begin
-      --  Sloc for error message on missing actual.
+      --  Sloc for error message on missing actual
+
       Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
 
       if Get_Instance_Of (Formal_Id) /= Formal_Id then
@@ -6904,12 +7126,12 @@ package body Sem_Ch12 is
 
       if Out_Present (Formal) then
 
-         --  An IN OUT generic actual must be a name. The instantiation is
-         --  a renaming declaration. The actual is the name being renamed.
-         --  We use the actual directly, rather than a copy, because it is not
+         --  An IN OUT generic actual must be a name. The instantiation is a
+         --  renaming declaration. The actual is the name being renamed. We
+         --  use the actual directly, rather than a copy, because it is not
          --  used further in the list of actuals, and because a copy or a use
-         --  of relocate_node is incorrect if the instance is nested within
-         --  generic. In order to simplify ASIS searches, the Generic_Parent
+         --  of relocate_node is incorrect if the instance is nested within a
+         --  generic. In order to simplify ASIS searches, the Generic_Parent
          --  field links the declaration to the generic association.
 
          if No (Actual) then
@@ -6937,6 +7159,12 @@ package body Sem_Ch12 is
          Append (Decl_Node, List);
          Analyze (Actual);
 
+         --  Return if the analysis of the actual reported some error
+
+         if Etype (Actual) = Any_Type then
+            return List;
+         end if;
+
          --  This check is performed here because Analyze_Object_Renaming
          --  will not check it when Comes_From_Source is False. Note
          --  though that the check for the actual being the name of an
@@ -7046,6 +7274,12 @@ package body Sem_Ch12 is
 
             if Nkind (Actual) /= N_Allocator then
                Analyze (Actual);
+
+               --  Return if the analysis of the actual reported some error
+
+               if Etype (Actual) = Any_Type then
+                  return List;
+               end if;
             end if;
 
             declare
@@ -7056,9 +7290,9 @@ package body Sem_Ch12 is
             begin
                Freeze_Before (Instantiation_Node, Typ);
 
-               --  If the actual is an aggregate, perform name resolution
-               --  on its components (the analysis of an aggregate does not
-               --  do it) to capture local names that may be hidden if the
+               --  If the actual is an aggregate, perform name resolution on
+               --  its components (the analysis of an aggregate does not do
+               --  it) to capture local names that may be hidden if the
                --  generic is a child unit.
 
                if Nkind (Actual) = N_Aggregate then
@@ -7068,7 +7302,7 @@ package body Sem_Ch12 is
 
          elsif Present (Expression (Formal)) then
 
-            --  Use default to construct declaration.
+            --  Use default to construct declaration
 
             Decl_Node :=
               Make_Object_Declaration (Sloc (Formal),
@@ -7604,8 +7838,7 @@ package body Sem_Ch12 is
      (Formal          : Node_Id;
       Actual          : Node_Id;
       Analyzed_Formal : Node_Id;
-      Actual_Decls    : List_Id)
-      return            Node_Id
+      Actual_Decls    : List_Id) return Node_Id
    is
       Loc       : constant Source_Ptr := Sloc (Actual);
       Gen_T     : constant Entity_Id  := Defining_Identifier (Formal);
@@ -7754,6 +7987,10 @@ package body Sem_Ch12 is
          function Formal_Dimensions return Int;
          --  Count number of dimensions in array type formal
 
+         -----------------------
+         -- Formal_Dimensions --
+         -----------------------
+
          function Formal_Dimensions return Int is
             Num   : Int := 0;
             Index : Node_Id;
@@ -8115,19 +8352,20 @@ package body Sem_Ch12 is
 
                   Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
 
-                  --  access discriminants match if designated types do.
+                  --  Access discriminants match if designated types do
 
                   if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
-                    and then (Ekind (Base_Type (Etype (Actual_Discr))))
-                      = E_Anonymous_Access_Type
-                    and then Get_Instance_Of (
-                      Designated_Type (Base_Type (Formal_Subt)))
-                      = Designated_Type (Base_Type (Etype (Actual_Discr)))
+                    and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
+                                E_Anonymous_Access_Type
+                    and then
+                      Get_Instance_Of
+                        (Designated_Type (Base_Type (Formal_Subt))) =
+                           Designated_Type (Base_Type (Etype (Actual_Discr)))
                   then
                      null;
 
                   elsif Base_Type (Formal_Subt) /=
-                                       Base_Type (Etype (Actual_Discr))
+                          Base_Type (Etype (Actual_Discr))
                   then
                      Error_Msg_NE
                        ("types of actual discriminants must match formal",
@@ -8361,8 +8599,7 @@ package body Sem_Ch12 is
    ---------------------
 
    function Is_In_Main_Unit (N : Node_Id) return Boolean is
-      Unum : constant Unit_Number_Type := Get_Source_Unit (N);
-
+      Unum         : constant Unit_Number_Type := Get_Source_Unit (N);
       Current_Unit : Node_Id;
 
    begin
@@ -8772,21 +9009,25 @@ package body Sem_Ch12 is
                  and then P /= Current_Scope
                then
                   --  We are within an instance of some sibling. Retain
-                  --  visibility of parent, for proper subsequent cleanup.
+                  --  visibility of parent, for proper subsequent cleanup,
+                  --  and reinstall private declarations as well.
 
                   Set_In_Private_Part (P);
+                  Install_Private_Declarations (P);
                end if;
 
-            --  This looks incomplete: what about compilation units that
-            --  were made visible by Install_Parent but should not remain
-            --  visible??? Standard is on the scope stack.
+            --  If the ultimate parent is a compilation unit, reset its
+            --  visibility to what it was before instantiation.
 
-            elsif not In_Open_Scopes (Scope (P)) then
+            elsif not In_Open_Scopes (Scope (P))
+              or else
+               (not Is_Child_Unit (P) and then not Parent_Unit_Visible)
+            then
                Set_Is_Immediately_Visible (P, False);
             end if;
          end loop;
 
-         --  Reset visibility of entities in the enclosing scope.
+         --  Reset visibility of entities in the enclosing scope
 
          Set_Is_Hidden_Open_Scope (Current_Scope, False);
          Hidden := First_Elmt (Hidden_Entities);
@@ -8832,6 +9073,7 @@ package body Sem_Ch12 is
       Exchanged_Views              := Saved.Exchanged_Views;
       Hidden_Entities              := Saved.Hidden_Entities;
       Current_Sem_Unit             := Saved.Current_Sem_Unit;
+      Parent_Unit_Visible          := Saved.Parent_Unit_Visible;
 
       Instance_Envs.Decrement_Last;
    end Restore_Env;
@@ -8850,6 +9092,41 @@ package body Sem_Ch12 is
       Dep_Elmt : Elmt_Id;
       Dep_Typ  : Node_Id;
 
+      procedure Restore_Nested_Formal (Formal : Entity_Id);
+      --  Hide the generic formals of formal packages declared with box
+      --  which were reachable in the current instantiation.
+
+      procedure Restore_Nested_Formal (Formal : Entity_Id) is
+         Ent : Entity_Id;
+      begin
+         if Present (Renamed_Object (Formal))
+           and then Denotes_Formal_Package (Renamed_Object (Formal), True)
+         then
+            return;
+
+         elsif Present (Associated_Formal_Package (Formal))
+          and then Box_Present (Parent (Associated_Formal_Package (Formal)))
+         then
+            Ent := First_Entity (Formal);
+
+            while Present (Ent) loop
+               exit when Ekind (Ent) = E_Package
+                 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
+
+               Set_Is_Hidden (Ent);
+               Set_Is_Potentially_Use_Visible (Ent, False);
+
+               --  If package, then recurse
+
+               if Ekind (Ent) = E_Package then
+                  Restore_Nested_Formal (Ent);
+               end if;
+
+               Next_Entity (Ent);
+            end loop;
+         end if;
+      end Restore_Nested_Formal;
+
    begin
       M := First_Elmt (Exchanged_Views);
       while Present (M) loop
@@ -8930,7 +9207,7 @@ package body Sem_Ch12 is
 
             --  If the actual is itself a formal package for the enclosing
             --  generic, or the actual for such a formal package, it remains
-            --  visible after the current instance, and therefore nothing
+            --  visible on exit from the instance, and therefore nothing
             --  needs to be done either, except to keep it accessible.
 
             if Is_Package
@@ -8941,7 +9218,7 @@ package body Sem_Ch12 is
             elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
                null;
 
-            elsif Denotes_Formal_Package (Renamed_Object (E)) then
+            elsif Denotes_Formal_Package (Renamed_Object (E), True) then
                Set_Is_Hidden (E, False);
 
             else
@@ -8954,15 +9231,19 @@ package body Sem_Ch12 is
                   while Present (Id)
                     and then Id /= First_Private_Entity (Act_P)
                   loop
-                     Set_Is_Hidden (Id, True);
-                     Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
                      exit when Ekind (Id) = E_Package
                                  and then Renamed_Object (Id) = Act_P;
 
+                     Set_Is_Hidden (Id, True);
+                     Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
+
+                     if Ekind (Id) = E_Package then
+                        Restore_Nested_Formal (Id);
+                     end if;
+
                      Next_Entity (Id);
                   end loop;
                end;
-               null;
             end if;
          end if;
 
@@ -9054,6 +9335,10 @@ package body Sem_Ch12 is
          --  the current scope (e.g. when the instance appears within the body
          --  of an ancestor).
 
+         ----------------------
+         -- Is_Instance_Node --
+         ----------------------
+
          function Is_Instance_Node (Decl : Node_Id) return Boolean is
          begin
             return (Nkind (Decl) in N_Generic_Instantiation
@@ -9349,7 +9634,7 @@ package body Sem_Ch12 is
             Next (Act2);
          end loop;
 
-         --  Find the associations added for default suprograms.
+         --  Find the associations added for default suprograms
 
          if Present (Act2) then
             while Nkind (Act2) /= N_Generic_Association
@@ -9618,7 +9903,7 @@ package body Sem_Ch12 is
                   --  access to a composite type, or a parameterless function
                   --  call that returns an access type.
 
-                  --  Check whether corresponding entity in prefix is global.
+                  --  Check whether corresponding entity in prefix is global
 
                   if Is_Entity_Name (Prefix (N2))
                     and then Present (Entity (Prefix (N2)))