+ return;
+
+ elsif In_Open_Scopes (Gen_Unit) then
+ if Is_Compilation_Unit (Gen_Unit)
+ and then Is_Child_Unit (Current_Scope)
+ then
+ -- Special-case the error when the formal is a parent, and
+ -- continue analysis to minimize cascaded errors.
+
+ Error_Msg_N
+ ("generic parent cannot be used as formal package "
+ & "of a child unit",
+ Gen_Id);
+
+ else
+ Error_Msg_N
+ ("generic package cannot be used as a formal package "
+ & "within itself",
+ Gen_Id);
+ Restore_Env;
+ return;
+ end if;
+ end if;
+
+ if Box_Present (N)
+ or else No (Generic_Associations (N))
+ or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
+ then
+ No_Associations := True;
+ end if;
+
+ -- If there are no generic associations, the generic parameters appear
+ -- as local entities and are instantiated like them. We copy the generic
+ -- package declaration as if it were an instantiation, and analyze it
+ -- like a regular package, except that we treat the formals as
+ -- additional visible components.
+
+ Gen_Decl := Unit_Declaration_Node (Gen_Unit);
+
+ if In_Extended_Main_Source_Unit (N) then
+ Set_Is_Instantiated (Gen_Unit);
+ Generate_Reference (Gen_Unit, N);
+ end if;
+
+ Formal := New_Copy (Pack_Id);
+ Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
+
+ -- Make local generic without formals. The formals will be replaced with
+ -- internal declarations.
+
+ New_N := Build_Local_Package;
+ Rewrite (N, New_N);
+ Set_Defining_Unit_Name (Specification (New_N), Formal);
+ Set_Generic_Parent (Specification (N), Gen_Unit);
+ Set_Instance_Env (Gen_Unit, Formal);
+ Set_Is_Generic_Instance (Formal);
+
+ Enter_Name (Formal);
+ Set_Ekind (Formal, E_Package);
+ Set_Etype (Formal, Standard_Void_Type);
+ Set_Inner_Instances (Formal, New_Elmt_List);
+ Push_Scope (Formal);
+
+ if Is_Child_Unit (Gen_Unit)
+ and then Parent_Installed
+ then
+ -- Similarly, we have to make the name of the formal visible in the
+ -- parent instance, to resolve properly fully qualified names that
+ -- may appear in the generic unit. The parent instance has been
+ -- placed on the scope stack ahead of the current scope.
+
+ Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
+
+ Renaming_In_Par :=
+ Make_Defining_Identifier (Loc, Chars (Gen_Unit));
+ Set_Ekind (Renaming_In_Par, E_Package);
+ Set_Etype (Renaming_In_Par, Standard_Void_Type);
+ Set_Scope (Renaming_In_Par, Parent_Instance);
+ Set_Parent (Renaming_In_Par, Parent (Formal));
+ Set_Renamed_Object (Renaming_In_Par, Formal);
+ Append_Entity (Renaming_In_Par, Parent_Instance);
+ end if;
+
+ Analyze (Specification (N));
+
+ -- The formals for which associations are provided are not visible
+ -- outside of the formal package. The others are still declared by a
+ -- formal parameter declaration.
+
+ if not No_Associations then
+ declare
+ E : Entity_Id;
+
+ begin
+ E := First_Entity (Formal);
+ while Present (E) loop
+ exit when Ekind (E) = E_Package
+ and then Renamed_Entity (E) = Formal;
+
+ if not Is_Generic_Formal (E) then
+ Set_Is_Hidden (E);
+ end if;
+
+ Next_Entity (E);
+ end loop;
+ end;
+ end if;