diff --git a/src/V3AstNodes.cpp b/src/V3AstNodes.cpp index 241d91078..076d94669 100644 --- a/src/V3AstNodes.cpp +++ b/src/V3AstNodes.cpp @@ -1171,6 +1171,12 @@ void AstClass::repairCache() { clearCache(); for (AstNode* itemp = membersp(); itemp; itemp = itemp->nextp()) { insertCache(itemp); } } +bool AstClass::isClassExtendedFrom(const AstClass* refClassp, const AstClass* baseClassp) { + // TAIL RECURSIVE + if (!refClassp || !baseClassp) return false; + if (refClassp == baseClassp) return true; + return isClassExtendedFrom(refClassp->extendsp()->classp(), baseClassp); +} void AstClass::dump(std::ostream& str) const { this->AstNode::dump(str); if (isExtended()) str << " [EXT]"; diff --git a/src/V3AstNodes.h b/src/V3AstNodes.h index 38fa9d26d..63f91c1a5 100644 --- a/src/V3AstNodes.h +++ b/src/V3AstNodes.h @@ -341,6 +341,9 @@ public: void isExtended(bool flag) { m_extended = flag; } bool isVirtual() const { return m_virtual; } void isVirtual(bool flag) { m_virtual = flag; } + // Return true if this class is an extension of base class (SLOW) + // Accepts nullptrs + static bool isClassExtendedFrom(const AstClass* refClassp, const AstClass* baseClassp); }; class AstClassExtends final : public AstNode { diff --git a/src/V3WidthCommit.h b/src/V3WidthCommit.h index 52344c197..b84b5a110 100644 --- a/src/V3WidthCommit.h +++ b/src/V3WidthCommit.h @@ -133,7 +133,7 @@ private: const char* how = nullptr; if (local && defClassp && refClassp != defClassp) { how = "'local'"; - } else if (prot && defClassp && !classExtendedRecurse(refClassp, defClassp)) { + } else if (prot && defClassp && !AstClass::isClassExtendedFrom(refClassp, defClassp)) { how = "'protected'"; } if (how) { @@ -149,12 +149,6 @@ private: } } } - static bool classExtendedRecurse(const AstClass* refClassp, const AstClass* defClassp) { - // Return true if refClassp is an extends class of defClassp - if (!refClassp || !defClassp) return false; - if (refClassp == defClassp) return true; - return classExtendedRecurse(refClassp->extendsp()->classp(), defClassp); - } // VISITORS virtual void visit(AstNodeModule* nodep) override {