Internals: V3Class cleanup. No functional change.

This commit is contained in:
Wilson Snyder 2022-01-02 09:43:37 -05:00
parent 028737cde8
commit 2342549caf
1 changed files with 32 additions and 23 deletions

View File

@ -31,16 +31,17 @@
class ClassVisitor final : public AstNVisitor { class ClassVisitor final : public AstNVisitor {
private: private:
// MEMBERS // NODE STATE
// AstClass::user1() -> bool. True if iterated already
// AstVar::user1p() -> AstVarScope* Scope used with this var
const AstUser1InUse m_inuser1; const AstUser1InUse m_inuser1;
// MEMBERS
string m_prefix; // String prefix to add to name based on hier string m_prefix; // String prefix to add to name based on hier
const AstScope* m_classScopep = nullptr; // Package moving scopes into const AstScope* m_classScopep = nullptr; // Package moving scopes into
AstScope* m_packageScopep = nullptr; // Class package scope AstScope* m_packageScopep = nullptr; // Class package scope
const AstNodeFTask* m_ftaskp = nullptr; // Current task const AstNodeFTask* m_ftaskp = nullptr; // Current task
std::vector<std::pair<AstNode*, AstScope*>> m_moves; std::vector<std::pair<AstNode*, AstScope*>> m_toScopeMoves;
// NODE STATE
// AstClass::user1() -> bool. True if iterated already
// METHODS // METHODS
VL_DEBUG_FUNC; // Declare debug() VL_DEBUG_FUNC; // Declare debug()
@ -54,15 +55,15 @@ private:
// Make containing package // Make containing package
// Note origName is the same as the class origName so errors look correct // Note origName is the same as the class origName so errors look correct
AstClassPackage* const packagep AstClassPackage* const packagep
= new AstClassPackage(nodep->fileline(), nodep->origName()); = new AstClassPackage{nodep->fileline(), nodep->origName()};
packagep->name(nodep->name() + "__Vclpkg"); packagep->name(nodep->name() + "__Vclpkg");
nodep->classOrPackagep(packagep); nodep->classOrPackagep(packagep);
packagep->classp(nodep); packagep->classp(nodep);
v3Global.rootp()->addModulep(packagep); v3Global.rootp()->addModulep(packagep);
// Add package to hierarchy // Add package to hierarchy
AstCell* const cellp AstCell* const cellp
= new AstCell(packagep->fileline(), packagep->fileline(), packagep->name(), = new AstCell{packagep->fileline(), packagep->fileline(), packagep->name(),
packagep->name(), nullptr, nullptr, nullptr); packagep->name(), nullptr, nullptr, nullptr};
cellp->modp(packagep); cellp->modp(packagep);
v3Global.rootp()->topModulep()->addStmtp(cellp); v3Global.rootp()->topModulep()->addStmtp(cellp);
// Find class's scope // Find class's scope
@ -75,8 +76,8 @@ private:
// Add scope // Add scope
AstScope* const scopep AstScope* const scopep
= new AstScope(nodep->fileline(), packagep, classScopep->name(), = new AstScope{nodep->fileline(), packagep, classScopep->name(),
classScopep->aboveScopep(), classScopep->aboveCellp()); classScopep->aboveScopep(), classScopep->aboveCellp()};
packagep->addStmtp(scopep); packagep->addStmtp(scopep);
// Iterate // Iterate
VL_RESTORER(m_prefix); VL_RESTORER(m_prefix);
@ -100,10 +101,13 @@ private:
virtual void visit(AstVar* nodep) override { virtual void visit(AstVar* nodep) override {
iterateChildren(nodep); iterateChildren(nodep);
// Don't move now, or wouldn't keep interating the class if (m_packageScopep) {
// TODO move class statics too if (m_ftaskp && m_ftaskp->lifetime().isStatic()) {
if (m_packageScopep && m_ftaskp && m_ftaskp->lifetime().isStatic()) { // Move later, or we wouldn't keep interating the class
m_moves.push_back(std::make_pair(nodep, m_packageScopep)); // We're really moving the VarScope but we might not
// have a pointer to it yet
m_toScopeMoves.push_back(std::make_pair(nodep, m_packageScopep));
}
} }
} }
@ -118,7 +122,7 @@ private:
m_ftaskp = nodep; m_ftaskp = nodep;
iterateChildren(nodep); iterateChildren(nodep);
if (m_packageScopep && nodep->lifetime().isStatic()) { if (m_packageScopep && nodep->lifetime().isStatic()) {
m_moves.push_back(std::make_pair(nodep, m_packageScopep)); m_toScopeMoves.push_back(std::make_pair(nodep, m_packageScopep));
} }
} }
} }
@ -128,7 +132,7 @@ private:
// Don't move now, or wouldn't keep interating the class // Don't move now, or wouldn't keep interating the class
// TODO move function statics only // TODO move function statics only
// if (m_classScopep) { // if (m_classScopep) {
// m_moves.push_back(std::make_pair(nodep, m_classScopep)); // m_toScopeMoves.push_back(std::make_pair(nodep, m_classScopep));
//} //}
} }
@ -140,13 +144,18 @@ public:
// CONSTRUCTORS // CONSTRUCTORS
explicit ClassVisitor(AstNetlist* nodep) { iterate(nodep); } explicit ClassVisitor(AstNetlist* nodep) { iterate(nodep); }
virtual ~ClassVisitor() override { virtual ~ClassVisitor() override {
for (auto moved : m_moves) { for (auto moved : m_toScopeMoves) {
if (VN_IS(moved.first, NodeFTask)) { AstNode* const nodep = moved.first;
moved.second->addActivep(moved.first->unlinkFrBack()); AstScope* const scopep = moved.second;
} else if (VN_IS(moved.first, Var)) { UINFO(9, "moving " << nodep << " to " << scopep << endl);
AstVarScope* const scopep = VN_AS(moved.first->user1p(), VarScope); if (VN_IS(nodep, NodeFTask)) {
scopep->unlinkFrBack(); scopep->addActivep(nodep->unlinkFrBack());
moved.second->addVarp(scopep); } else if (VN_IS(nodep, Var)) {
AstVarScope* const vscp = VN_AS(nodep->user1p(), VarScope);
vscp->unlinkFrBack();
scopep->addVarp(vscp);
} else {
nodep->v3fatalSrc("Bad case");
} }
} }
} }