Skip to content

Commit 28a10da

Browse files
author
Eric Botcazou
committed
Ada: Fix instantiation failure with qualified name of child generic unit
This is again an issue with multiple levels of nested instances, and it arises because the qualified name of the problematic child generic unit is used (this works fine with the direct name), exposing the rather questionable processing implemented for instances in Find_Expanded_Name. The patch replaces this processing with the straightforward decoding of the renaming scheme used in Sem_Ch12. gcc/ada/ PR ada/16214 * sem_ch8.adb (Find_Expanded_Name): Consolidate and streamline the processing required for references to instances within themselves. gcc/testsuite/ * gnat.dg/specs/generic_inst6.ads: New test. * gnat.dg/specs/generic_inst6_pkg1-child.ads: New helper. * gnat.dg/specs/generic_inst6_pkg1-child-grand1.ads: Likewise. * gnat.dg/specs/generic_inst6_pkg1-child-grand2.ads: Likewise. * gnat.dg/specs/generic_inst6_pkg1.ads: Likewise. * gnat.dg/specs/generic_inst6_pkg2.ads: Likewise. * gnat.dg/specs/generic_inst6_pkg3.ads: Likewise.
1 parent 1bb016e commit 28a10da

File tree

8 files changed

+77
-66
lines changed

8 files changed

+77
-66
lines changed

gcc/ada/sem_ch8.adb

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7225,6 +7225,8 @@ package body Sem_Ch8 is
72257225

72267226
begin
72277227
while Present (Id) loop
7228+
-- The immediate case is when Id is an entity of the prefix
7229+
72287230
if Scope (Id) = P_Name then
72297231
Candidate := Id;
72307232
Is_New_Candidate := True;
@@ -7250,6 +7252,53 @@ package body Sem_Ch8 is
72507252
end if;
72517253
end if;
72527254

7255+
-- If the name of a generic child unit appears within an instance
7256+
-- of itself, then it is resolved to the renaming of the name of
7257+
-- the instance built in Sem_Ch12, so we get to the generic parent
7258+
-- through the renaming.
7259+
7260+
elsif Ekind (Id) in E_Function | E_Package | E_Procedure
7261+
and then Present (Renamed_Entity (Id))
7262+
and then Is_Generic_Instance (Renamed_Entity (Id))
7263+
and then In_Open_Scopes (Renamed_Entity (Id))
7264+
then
7265+
declare
7266+
Gen_Inst : constant Entity_Id := Renamed_Entity (Id);
7267+
Gen_Par : constant Entity_Id :=
7268+
Generic_Parent
7269+
(Specification (Unit_Declaration_Node (Gen_Inst)));
7270+
7271+
begin
7272+
-- The easy case is when Gen_Par is an entity of the prefix
7273+
7274+
if Scope (Gen_Par) = P_Name then
7275+
Is_New_Candidate := True;
7276+
7277+
-- Now the prefix may also be within an instance of itself,
7278+
-- but we do not need to go through the renaming for it, as
7279+
-- this was done on entry to the procedure.
7280+
7281+
elsif Is_Generic_Instance (P_Name)
7282+
and then In_Open_Scopes (P_Name)
7283+
then
7284+
declare
7285+
Gen_Par_P : constant Entity_Id :=
7286+
Generic_Parent
7287+
(Specification (Unit_Declaration_Node (P_Name)));
7288+
7289+
begin
7290+
if Scope (Gen_Par) = Gen_Par_P then
7291+
Is_New_Candidate := True;
7292+
else
7293+
Is_New_Candidate := False;
7294+
end if;
7295+
end;
7296+
7297+
else
7298+
Is_New_Candidate := False;
7299+
end if;
7300+
end;
7301+
72537302
-- Ada 2005 (AI-217): Handle shadow entities associated with
72547303
-- types declared in limited-withed nested packages. We don't need
72557304
-- to handle E_Incomplete_Subtype entities because the entities
@@ -7284,22 +7333,6 @@ package body Sem_Ch8 is
72847333
Candidate := Get_Full_View (Id);
72857334
Is_New_Candidate := True;
72867335

7287-
-- An unusual case arises with a fully qualified name for an
7288-
-- entity local to a generic child unit package, within an
7289-
-- instantiation of that package. The name of the unit now
7290-
-- denotes the renaming created within the instance. This is
7291-
-- only relevant in an instance body, see below.
7292-
7293-
elsif Is_Generic_Instance (Scope (Id))
7294-
and then In_Open_Scopes (Scope (Id))
7295-
and then In_Instance_Body
7296-
and then Ekind (Scope (Id)) = E_Package
7297-
and then Ekind (Id) = E_Package
7298-
and then Renamed_Entity (Id) = Scope (Id)
7299-
and then Is_Immediately_Visible (P_Name)
7300-
then
7301-
Is_New_Candidate := True;
7302-
73037336
else
73047337
Is_New_Candidate := False;
73057338
end if;
@@ -7434,55 +7467,6 @@ package body Sem_Ch8 is
74347467
end if;
74357468

74367469
else
7437-
-- Within the instantiation of a child unit, the prefix may
7438-
-- denote the parent instance, but the selector has the name
7439-
-- of the original child. That is to say, when A.B appears
7440-
-- within an instantiation of generic child unit B, the scope
7441-
-- stack includes an instance of A (P_Name) and an instance
7442-
-- of B under some other name. We scan the scope to find this
7443-
-- child instance, which is the desired entity.
7444-
-- Note that the parent may itself be a child instance, if
7445-
-- the reference is of the form A.B.C, in which case A.B has
7446-
-- already been rewritten with the proper entity.
7447-
7448-
if In_Open_Scopes (P_Name)
7449-
and then Is_Generic_Instance (P_Name)
7450-
then
7451-
declare
7452-
Gen_Par : constant Entity_Id :=
7453-
Generic_Parent (Specification
7454-
(Unit_Declaration_Node (P_Name)));
7455-
S : Entity_Id := Current_Scope;
7456-
P : Entity_Id;
7457-
7458-
begin
7459-
for J in reverse 0 .. Scope_Stack.Last loop
7460-
S := Scope_Stack.Table (J).Entity;
7461-
7462-
exit when S = Standard_Standard;
7463-
7464-
if Ekind (S) in E_Function | E_Package | E_Procedure
7465-
then
7466-
P :=
7467-
Generic_Parent (Specification
7468-
(Unit_Declaration_Node (S)));
7469-
7470-
-- Check that P is a generic child of the generic
7471-
-- parent of the prefix.
7472-
7473-
if Present (P)
7474-
and then Chars (P) = Chars (Selector)
7475-
and then Scope (P) = Gen_Par
7476-
then
7477-
Id := S;
7478-
goto Found;
7479-
end if;
7480-
end if;
7481-
7482-
end loop;
7483-
end;
7484-
end if;
7485-
74867470
-- If this is a selection from Ada, System or Interfaces, then
74877471
-- we assume a missing with for the corresponding package.
74887472

@@ -7589,7 +7573,6 @@ package body Sem_Ch8 is
75897573
end if;
75907574
end if;
75917575

7592-
<<Found>>
75937576
if Comes_From_Source (N)
75947577
and then Is_Remote_Access_To_Subprogram_Type (Id)
75957578
and then Ekind (Id) = E_Access_Subprogram_Type
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- { dg-do compile }
2+
3+
with Generic_Inst6_Pkg1.Child.Grand2;
4+
with Generic_Inst6_Pkg3;
5+
6+
package Generic_Inst6 is new Generic_Inst6_Pkg3.Grand2;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generic
2+
package Generic_Inst6_Pkg1.Child.Grand1 is
3+
end Generic_Inst6_Pkg1.Child.Grand1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with Generic_Inst6_Pkg1.Child.Grand1;
2+
3+
generic
4+
package Generic_Inst6_Pkg1.Child.Grand2 is
5+
package My_Grand1 is new Generic_Inst6_Pkg1.Child.Grand1;
6+
end Generic_Inst6_Pkg1.Child.Grand2;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generic
2+
package Generic_Inst6_Pkg1.Child is
3+
end Generic_Inst6_Pkg1.Child;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generic
2+
package Generic_Inst6_Pkg1 is
3+
end Generic_Inst6_Pkg1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with Generic_Inst6_Pkg1;
2+
3+
package Generic_Inst6_Pkg2 is new Generic_Inst6_Pkg1;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
with Generic_Inst6_Pkg1.Child;
2+
with Generic_Inst6_Pkg2;
3+
4+
package Generic_Inst6_Pkg3 is new Generic_Inst6_Pkg2.Child;

0 commit comments

Comments
 (0)