Fix parameterized class typedef as interface type parameter (#7000)

Enhance similarDTypeNode for ClassRefDType to compare type parameters,
not just class pointers. Add resolveDotToTypedef in V3Param to handle
ParamTypeDType whose value is a parameterized class needing
specialization. This fixes patterns like:
  interface outer #(parameter type C = class_with_typedef#(T));
    inner #(.P(C::typedef_name)) i();
  endinterface
This commit is contained in:
Leela Pakanati 2026-02-05 22:06:23 -06:00
parent b82f6beffb
commit 388fb9db2d
6 changed files with 178 additions and 5 deletions

View File

@ -585,11 +585,7 @@ public:
const AstClassRefDType* const asamep = VN_DBG_AS(samep, ClassRefDType);
return (m_classp == asamep->m_classp && m_classOrPackagep == asamep->m_classOrPackagep);
}
bool similarDTypeNode(const AstNodeDType* samep) const override {
// Doesn't need to compare m_classOrPackagep
const AstClassRefDType* const asamep = VN_DBG_AS(samep, ClassRefDType);
return m_classp == asamep->m_classp;
}
bool similarDTypeNode(const AstNodeDType* samep) const override;
void dump(std::ostream& str = std::cout) const override;
void dumpJson(std::ostream& str = std::cout) const override;
void dumpSmall(std::ostream& str) const override;

View File

@ -1894,6 +1894,21 @@ void AstClassRefDType::dumpSmall(std::ostream& str) const {
}
string AstClassRefDType::prettyDTypeName(bool) const { return "class{}"s + prettyName(); }
string AstClassRefDType::name() const { return classp() ? classp()->name() : "<unlinked>"; }
bool AstClassRefDType::similarDTypeNode(const AstNodeDType* samep) const {
// Doesn't need to compare m_classOrPackagep
const AstClassRefDType* const asamep = VN_DBG_AS(samep, ClassRefDType);
if (m_classp != asamep->m_classp) return false;
// Also compare type parameters - C#(int) != C#(string)
const AstPin* lp = paramsp();
const AstPin* rp = asamep->paramsp();
while (lp && rp) {
if (!lp->exprp() != !rp->exprp()) return false;
if (lp->exprp() && !lp->exprp()->sameTree(rp->exprp())) return false;
lp = VN_CAST(lp->nextp(), Pin);
rp = VN_CAST(rp->nextp(), Pin);
}
return !lp && !rp;
}
void AstNodeCoverOrAssert::dump(std::ostream& str) const {
this->AstNodeStmt::dump(str);
str << " ["s + this->type().ascii() + "]";

View File

@ -948,6 +948,39 @@ class ParamProcessor final {
if (!parseRefp) return;
const AstClass* lhsClassp = VN_CAST(classRefp->classOrPackageSkipp(), Class);
// If the ClassOrPackageRef points to a type parameter (ParamTypeDType), we need
// to check if the parameter's value contains a parameterized class that needs
// specialization. This handles patterns like:
// interface outer #(parameter type C = class_with_type_param#(T));
// inner #(.P(C::typedef_name)) i(); // C is a type parameter
// endinterface
AstParamTypeDType* const paramTypep
= VN_CAST(classRefp->classOrPackageNodep(), ParamTypeDType);
if (paramTypep) {
// Traverse through the type parameter to find if there's a ClassRefDType
// with parameters that needs specialization
AstNodeDType* const dtypep = paramTypep->subDTypep();
AstClassRefDType* const classRefDTypep
= dtypep ? VN_CAST(dtypep->skipRefp(), ClassRefDType) : nullptr;
if (classRefDTypep) {
AstClass* const srcClassp = classRefDTypep->classp();
if (srcClassp && srcClassp->hasGParam() && classRefDTypep->paramsp()) {
// The type parameter's value is a parameterized class - specialize it
if (lhsClassp == srcClassp || !lhsClassp) {
UINFO(9, "resolveDotToTypedef: specializing type param class "
<< srcClassp->name() << endl);
classRefDeparam(classRefDTypep, srcClassp);
lhsClassp = classRefDTypep->classp();
} else {
UINFO(9, "resolveDotToTypedef: type param class "
<< srcClassp->name()
<< " already specialized to " << lhsClassp->name() << endl);
}
}
}
}
if (classRefp->paramsp()) {
// ClassOrPackageRef has parameters - may need to specialize the class
AstClass* const srcClassp = VN_CAST(classRefp->classOrPackageNodep(), Class);
@ -957,6 +990,10 @@ class ParamProcessor final {
UINFO(9, "resolveDotToTypedef: specializing " << srcClassp->name() << endl);
classRefDeparam(classRefp, srcClassp);
lhsClassp = VN_CAST(classRefp->classOrPackageSkipp(), Class);
} else {
UINFO(9, "resolveDotToTypedef: class " << srcClassp->name()
<< " already specialized to "
<< lhsClassp->name() << endl);
}
}
}

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of either the GNU Lesser General Public License Version 3
# or the Perl Artistic License Version 2.0.
# SPDX-FileCopyrightText: 2026 Wilson Snyder
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('simulator')
test.compile(verilator_flags2=["--binary"])
test.execute()
test.passes()

View File

@ -0,0 +1,87 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2026 Leela Pakanati
// SPDX-License-Identifier: CC0-1.0
// Test that parameterized class typedefs work as interface type parameters
// when the class itself has type parameters.
// See issue #7000
// Class with single type parameter
class C #(parameter type T = logic);
typedef struct packed { T data; } td_t;
endclass
// Class with multiple type parameters and multiple typedefs
class multi_param #(parameter type ADDR_T = logic, parameter type DATA_T = logic);
typedef struct packed { ADDR_T addr; } addr_td_t;
typedef struct packed { DATA_T data; } data_td_t;
endclass
// verilog_format: off
`define stop $stop
`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
// verilog_format: on
// Leaf interface: holds a value of parameterized type
interface l0 #(type P = logic);
P p;
endinterface
// 1-level nesting: wraps l0 with a class typedef parameter
interface l1 #(parameter type X = C#(logic));
l0 #(.P(X::td_t)) sub();
endinterface
// 2-level nesting: forwards type param through l1 to l0
interface l2 #(parameter type X = C#(logic));
l1 #(.X(X)) sub();
endinterface
// Multi-param leaf: holds two values of different parameterized types
interface multi_l0 #(type P = logic, type Q = logic);
P p;
Q q;
endinterface
// Multi-param nesting: accesses different typedefs from same class
interface multi_l1 #(
parameter type CFG = multi_param#(logic, logic)
);
multi_l0 #(.P(CFG::addr_td_t), .Q(CFG::data_td_t)) sub();
endinterface
module t;
// Test 1-level nesting with different parameterizations
l1 #(.X(C#(logic[7:0]))) l1_i1();
l1 #(.X(C#(logic[15:0]))) l1_i2();
// Test default type parameter (C#(logic) -> td_t is struct packed { logic data; })
l1 l1_default();
// Test 2-level nesting - type parameter passed through multiple levels
l2 #(.X(C#(logic[31:0]))) l2_i();
// Test multiple type params - different parameterizations accessing multiple typedefs
multi_l1 #(.CFG(multi_param#(logic[7:0], logic[31:0]))) ml1_i1();
multi_l1 #(.CFG(multi_param#(logic[15:0], logic[63:0]))) ml1_i2();
initial begin
// 1-level nesting
`checkd($bits(l1_i1.sub.p), 8);
`checkd($bits(l1_i2.sub.p), 16);
`checkd($bits(l1_default.sub.p), 1); // default C#(logic) -> 1-bit
// 2-level nesting
`checkd($bits(l2_i.sub.sub.p), 32);
// Multiple type params passed to sub-interface - two different typedefs
`checkd($bits(ml1_i1.sub.p), 8); // addr_td_t from ADDR_T=logic[7:0]
`checkd($bits(ml1_i1.sub.q), 32); // data_td_t from DATA_T=logic[31:0]
`checkd($bits(ml1_i2.sub.p), 16); // addr_td_t from ADDR_T=logic[15:0]
`checkd($bits(ml1_i2.sub.q), 64); // data_td_t from DATA_T=logic[63:0]
$write("*-* All Finished *-*\n");
$finish;
end
endmodule

View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of either the GNU Lesser General Public License Version 3
# or the Perl Artistic License Version 2.0.
# SPDX-FileCopyrightText: 2026 Wilson Snyder
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('simulator')
test.top_filename = "t/t_iface_param_class_type_param.v"
test.compile(verilator_flags2=['--binary', '-fno-inline'])
test.execute()
test.passes()