(Clause1 : Entity_Id;
Clause2 : Entity_Id) return Entity_Id
is
+ function Determine_Package_Scope (Clause : Node_Id) return Entity_Id;
+ -- Given a use clause, determine which package it belongs to
+
+ -----------------------------
+ -- Determine_Package_Scope --
+ -----------------------------
+
+ function Determine_Package_Scope (Clause : Node_Id) return Entity_Id is
+ begin
+ -- Check if the clause appears in the context area
+
+ -- Note we cannot employ Enclosing_Packge for use clauses within
+ -- context clauses since they are not actually "enclosed."
+
+ if Nkind (Parent (Clause)) = N_Compilation_Unit then
+ return Entity_Of_Unit (Unit (Parent (Clause)));
+ end if;
+
+ -- Otherwise, obtain the enclosing package normally
+
+ return Enclosing_Package (Clause);
+ end Determine_Package_Scope;
+
Scope1 : Entity_Id;
Scope2 : Entity_Id;
+ -- Start of processing for Most_Descendant_Use_Clause
+
begin
if Clause1 = Clause2 then
return Clause1;
-- We determine which one is the most descendant by the scope distance
-- to the ultimate parent unit.
- Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
- Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
+ Scope1 := Determine_Package_Scope (Clause1);
+ Scope2 := Determine_Package_Scope (Clause2);
while Scope1 /= Standard_Standard
and then Scope2 /= Standard_Standard
loop
-- Enclosing_Package --
-----------------------
- function Enclosing_Package (E : Entity_Id) return Entity_Id is
- Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
+ function Enclosing_Package (N : Node_Or_Entity_Id) return Entity_Id is
+ Dynamic_Scope : Entity_Id;
begin
+ -- Obtain the enclosing scope when N is a Node_Id - taking care to
+ -- handle the case when the enclosing scope is already a package.
+
+ if Nkind (N) not in N_Entity then
+ declare
+ Encl_Scop : constant Entity_Id := Find_Enclosing_Scope (N);
+ begin
+ if No (Encl_Scop) then
+ return Empty;
+ elsif Ekind (Encl_Scop) in
+ E_Generic_Package | E_Package | E_Package_Body
+ then
+ return Encl_Scop;
+ end if;
+
+ return Enclosing_Package (Encl_Scop);
+ end;
+ end if;
+
+ -- When N is already an Entity_Id proceed
+
+ Dynamic_Scope := Enclosing_Dynamic_Scope (N);
if Dynamic_Scope = Standard_Standard then
return Standard_Standard;
-- Enclosing_Comp_Unit_Node returns a subunit, then the corresponding
-- library unit. If no such item is found, returns Empty.
- function Enclosing_Package (E : Entity_Id) return Entity_Id;
+ function Enclosing_Package (N : Node_Or_Entity_Id) return Entity_Id;
-- Utility function to return the Ada entity of the package enclosing
- -- the entity E, if any. Returns Empty if no enclosing package.
+ -- the entity or node N, if any. Returns Empty if no enclosing package.
function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id;
-- Returns the entity of the package or subprogram enclosing E, if any.