Fix class extends to use virtual destruction

This commit is contained in:
Wilson Snyder 2020-08-23 20:00:39 -04:00
parent 20206b1e2e
commit 3d073c9534
4 changed files with 13 additions and 2 deletions

View File

@ -1095,7 +1095,11 @@ void AstClass::repairCache() {
clearCache();
for (AstNode* itemp = membersp(); itemp; itemp = itemp->nextp()) { insertCache(itemp); }
}
void AstClass::dump(std::ostream& str) const { this->AstNode::dump(str); }
void AstClass::dump(std::ostream& str) const {
this->AstNode::dump(str);
if (isExtended()) str << " [EXT]";
if (isVirtual()) str << " [VIRT]";
}
AstClass* AstClassExtends::classp() const {
AstClassRefDType* refp = VN_CAST(dtypep(), ClassRefDType);
UASSERT_OBJ(refp, this, "class extends non-ref");

View File

@ -305,6 +305,7 @@ class AstClass : public AstNodeModule {
MemberNameMap m_members; // Members or method children
AstClassPackage* m_packagep = nullptr; // Class package this is under
bool m_virtual = false; // Virtual class
bool m_extended = false; // Is extension or extended by other classes
void insertCache(AstNode* nodep);
public:
@ -336,6 +337,8 @@ public:
const auto it = m_members.find(name);
return (it == m_members.end()) ? nullptr : it->second;
}
bool isExtended() const { return m_extended; }
void isExtended(bool flag) { m_extended = flag; }
bool isVirtual() const { return m_virtual; }
void isVirtual(bool flag) { m_virtual = flag; }
};

View File

@ -170,10 +170,12 @@ void V3CCtors::cctorsAll() {
}
}
}
if (VN_IS(modp, Class)) {
if (AstClass* classp = VN_CAST(modp, Class)) {
AstCFunc* funcp = new AstCFunc(modp->fileline(), "~", nullptr, "");
funcp->isDestructor(true);
funcp->isStatic(false);
// If can be referred to by base pointer, need virtual delete
funcp->isVirtual(classp->isExtended());
funcp->slow(false);
modp->addStmtp(funcp);
}

View File

@ -2709,6 +2709,8 @@ private:
AstClassRefDType* newp
= new AstClassRefDType{nodep->fileline(), classp};
cextp->childDTypep(newp);
classp->isExtended(true);
nodep->isExtended(true);
VL_DO_DANGLING(cpackagerefp->unlinkFrBack()->deleteTree(),
cpackagerefp);
ok = true;