From: Douglas Gregor Date: Fri, 7 Aug 2009 06:08:38 +0000 (+0000) Subject: Separate Stmt::Destroy into the entrypoint for destroying a statement X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=42602bb40aefcc2751d4078ba88aacf4d965c9bd;p=clang Separate Stmt::Destroy into the entrypoint for destroying a statement or expression (Destroy) from the virtual function used to actually destroy a given expression (DoDestroy). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78375 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 3cec4e8aaf..e65a842d3f 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -564,6 +564,10 @@ class StringLiteral : public Expr { SourceLocation TokLocs[1]; StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {} + +protected: + virtual void DoDestroy(ASTContext &C); + public: /// This is the "fully general" constructor that allows representation of /// strings formed from multiple concatenated tokens. @@ -582,7 +586,6 @@ public: static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs); StringLiteral* Clone(ASTContext &C) const; - void Destroy(ASTContext &C); const char *getStrData() const { return StrData; } unsigned getByteLength() const { return ByteLength; } @@ -780,6 +783,10 @@ class SizeOfAlignOfExpr : public Expr { Stmt *Ex; } Argument; SourceLocation OpLoc, RParenLoc; + +protected: + virtual void DoDestroy(ASTContext& C); + public: SizeOfAlignOfExpr(bool issizeof, QualType T, QualType resultType, SourceLocation op, @@ -807,8 +814,6 @@ public: explicit SizeOfAlignOfExpr(EmptyShell Empty) : Expr(SizeOfAlignOfExprClass, Empty) { } - virtual void Destroy(ASTContext& C); - bool isSizeOf() const { return isSizeof; } void setSizeof(bool S) { isSizeof = S; } @@ -950,6 +955,8 @@ protected: // This version of the constructor is for derived classes. CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t, SourceLocation rparenloc); + + virtual void DoDestroy(ASTContext& C); public: CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, @@ -960,8 +967,6 @@ public: ~CallExpr() {} - void Destroy(ASTContext& C); - const Expr *getCallee() const { return cast(SubExprs[FN]); } Expr *getCallee() { return cast(SubExprs[FN]); } void setCallee(Expr *F) { SubExprs[FN] = F; } @@ -2113,6 +2118,9 @@ private: : Expr(DesignatedInitExprClass, EmptyShell()), NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { } +protected: + virtual void DoDestroy(ASTContext &C); + public: /// A field designator, e.g., ".x". struct FieldDesignator { @@ -2332,8 +2340,6 @@ public: virtual SourceRange getSourceRange() const; - virtual void Destroy(ASTContext &C); - static bool classof(const Stmt *T) { return T->getStmtClass() == DesignatedInitExprClass; } diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index bee9b29896..008e6af2b7 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -431,7 +431,8 @@ class CXXTemporary { public: static CXXTemporary *Create(ASTContext &C, const CXXDestructorDecl *Destructor); - void Destroy(ASTContext &C); + + void Destroy(ASTContext &Ctx); const CXXDestructorDecl *getDestructor() const { return Destructor; } }; @@ -448,10 +449,12 @@ class CXXBindTemporaryExpr : public Expr { subexpr->getType()), Temp(temp), SubExpr(subexpr) { } ~CXXBindTemporaryExpr() { } +protected: + virtual void DoDestroy(ASTContext &C); + public: static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp, Expr* SubExpr); - void Destroy(ASTContext &C); CXXTemporary *getTemporary() { return Temp; } const CXXTemporary *getTemporary() const { return Temp; } @@ -489,12 +492,13 @@ protected: Expr **args, unsigned numargs); ~CXXConstructExpr() { } + virtual void DoDestroy(ASTContext &C); + public: static CXXConstructExpr *Create(ASTContext &C, QualType T, CXXConstructorDecl *D, bool Elidable, Expr **Args, unsigned NumArgs); - void Destroy(ASTContext &C); CXXConstructorDecl* getConstructor() const { return Constructor; } @@ -653,8 +657,6 @@ public: /*FIXME:integral constant?*/ var->getType()->isDependentType()) {} - virtual void Destroy(ASTContext& Ctx); - SourceLocation getStartLoc() const { return getLocation(); } VarDecl *getVarDecl() { return cast(getDecl()); } @@ -1072,6 +1074,8 @@ class TemplateIdRefExpr : public Expr { unsigned NumTemplateArgs, SourceLocation RAngleLoc); + virtual void DoDestroy(ASTContext &Context); + public: static TemplateIdRefExpr * Create(ASTContext &Context, QualType T, @@ -1080,8 +1084,6 @@ public: SourceLocation LAngleLoc, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs, SourceLocation RAngleLoc); - void Destroy(ASTContext &Context); - /// \brief Retrieve the nested name specifier used to qualify the name of /// this template-id, e.g., the "std::sort" in @c std::sort, or NULL /// if this template-id was an unqualified-id. @@ -1143,12 +1145,14 @@ class CXXExprWithTemporaries : public Expr { CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps, unsigned NumTemps, bool ShouldDestroyTemps); ~CXXExprWithTemporaries(); - + +protected: + virtual void DoDestroy(ASTContext &C); + public: static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr, CXXTemporary **Temps, unsigned NumTemps, bool ShouldDestroyTemporaries); - void Destroy(ASTContext &C); unsigned getNumTemporaries() const { return NumTemps; } CXXTemporary *getTemporary(unsigned i) { diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index e841f25d76..239c6b5f43 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -155,13 +155,21 @@ protected: if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); } + /// \brief Virtual method that performs the actual destruction of + /// this statement. + /// + /// Subclasses should override this method (not Destroy()) to + /// provide class-specific destruction. + virtual void DoDestroy(ASTContext &Ctx); + public: Stmt(StmtClass SC) : sClass(SC) { if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); } virtual ~Stmt() {} - virtual void Destroy(ASTContext &Ctx); + /// \brief Destroy the current statement and its children. + void Destroy(ASTContext &Ctx) { DoDestroy(Ctx); } StmtClass getStmtClass() const { return sClass; } const char *getStmtClassName() const; @@ -256,6 +264,7 @@ public: class DeclStmt : public Stmt { DeclGroupRef DG; SourceLocation StartLoc, EndLoc; + public: DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg), @@ -264,8 +273,6 @@ public: /// \brief Build an empty declaration statement. explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { } - virtual void Destroy(ASTContext& Ctx); - /// isSingleDecl - This method returns true if this DeclStmt refers /// to a single Decl. bool isSingleDecl() const { diff --git a/include/clang/AST/StmtCXX.h b/include/clang/AST/StmtCXX.h index 2338f1457a..28fe348c45 100644 --- a/include/clang/AST/StmtCXX.h +++ b/include/clang/AST/StmtCXX.h @@ -29,13 +29,14 @@ class CXXCatchStmt : public Stmt { /// The handler block. Stmt *HandlerBlock; +protected: + virtual void DoDestroy(ASTContext& Ctx); + public: CXXCatchStmt(SourceLocation catchLoc, VarDecl *exDecl, Stmt *handlerBlock) : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl), HandlerBlock(handlerBlock) {} - virtual void Destroy(ASTContext& Ctx); - virtual SourceRange getSourceRange() const { return SourceRange(CatchLoc, HandlerBlock->getLocEnd()); } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 842caa8d00..0f3cbe18d9 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -111,10 +111,9 @@ StringLiteral* StringLiteral::Clone(ASTContext &C) const { TokLocs, NumConcatenated); } -void StringLiteral::Destroy(ASTContext &C) { +void StringLiteral::DoDestroy(ASTContext &C) { C.Deallocate(const_cast(StrData)); - this->~StringLiteral(); - C.Deallocate(this); + Expr::DoDestroy(C); } void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) { @@ -218,7 +217,7 @@ CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty) SubExprs = new (C) Stmt*[1]; } -void CallExpr::Destroy(ASTContext& C) { +void CallExpr::DoDestroy(ASTContext& C) { DestroyChildren(C); if (SubExprs) C.Deallocate(SubExprs); this->~CallExpr(); @@ -1657,7 +1656,7 @@ void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) { memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs); } -void SizeOfAlignOfExpr::Destroy(ASTContext& C) { +void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) { // Override default behavior of traversing children. If this has a type // operand and the type is a variable-length array, the child iteration // will iterate over the size expression. However, this expression belongs @@ -1668,7 +1667,7 @@ void SizeOfAlignOfExpr::Destroy(ASTContext& C) { C.Deallocate(this); } else - Expr::Destroy(C); + Expr::DoDestroy(C); } //===----------------------------------------------------------------------===// @@ -1831,9 +1830,9 @@ void DesignatedInitExpr::ExpandDesignator(unsigned Idx, NumDesignators = NumDesignators - 1 + NumNewDesignators; } -void DesignatedInitExpr::Destroy(ASTContext &C) { +void DesignatedInitExpr::DoDestroy(ASTContext &C) { delete [] Designators; - Expr::Destroy(C); + Expr::DoDestroy(C); } ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const { diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index d8e069d000..67d62a94ec 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -17,14 +17,6 @@ #include "clang/AST/ExprCXX.h" using namespace clang; -void CXXConditionDeclExpr::Destroy(ASTContext& C) { - // FIXME: Cannot destroy the decl here, because it is linked into the - // DeclContext's chain. - //getVarDecl()->Destroy(C); - this->~CXXConditionDeclExpr(); - C.Deallocate(this); -} - //===----------------------------------------------------------------------===// // Child Iterators for iterating over subexpressions/substatements //===----------------------------------------------------------------------===// @@ -196,11 +188,13 @@ TemplateIdRefExpr::Create(ASTContext &Context, QualType T, NumTemplateArgs, RAngleLoc); } -void TemplateIdRefExpr::Destroy(ASTContext &Context) { +void TemplateIdRefExpr::DoDestroy(ASTContext &Context) { const TemplateArgument *TemplateArgs = getTemplateArgs(); for (unsigned I = 0; I != NumTemplateArgs; ++I) if (Expr *E = TemplateArgs[I].getAsExpr()) E->Destroy(Context); + this->~TemplateIdRefExpr(); + Context.Deallocate(this); } Stmt::child_iterator TemplateIdRefExpr::child_begin() { @@ -348,9 +342,9 @@ CXXTemporary *CXXTemporary::Create(ASTContext &C, return new (C) CXXTemporary(Destructor); } -void CXXTemporary::Destroy(ASTContext &C) { +void CXXTemporary::Destroy(ASTContext &Ctx) { this->~CXXTemporary(); - C.Deallocate(this); + Ctx.Deallocate(this); } CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C, @@ -362,7 +356,7 @@ CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C, return new (C) CXXBindTemporaryExpr(Temp, SubExpr); } -void CXXBindTemporaryExpr::Destroy(ASTContext &C) { +void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) { Temp->Destroy(C); this->~CXXBindTemporaryExpr(); C.Deallocate(this); @@ -406,7 +400,7 @@ CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, } } -void CXXConstructExpr::Destroy(ASTContext &C) { +void CXXConstructExpr::DoDestroy(ASTContext &C) { DestroyChildren(C); if (Args) C.Deallocate(Args); @@ -438,7 +432,7 @@ CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C, ShouldDestroyTemps); } -void CXXExprWithTemporaries::Destroy(ASTContext &C) { +void CXXExprWithTemporaries::DoDestroy(ASTContext &C) { DestroyChildren(C); this->~CXXExprWithTemporaries(); C.Deallocate(this); diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 17577910d2..668f7ef57f 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -51,19 +51,12 @@ void Stmt::DestroyChildren(ASTContext &C) { if (Stmt* Child = *I++) Child->Destroy(C); } -void Stmt::Destroy(ASTContext &C) { +void Stmt::DoDestroy(ASTContext &C) { DestroyChildren(C); - // FIXME: Eventually all Stmts should be allocated with the allocator - // in ASTContext, just like with Decls. this->~Stmt(); C.Deallocate((void *)this); } -void DeclStmt::Destroy(ASTContext &C) { - this->~DeclStmt(); - C.Deallocate((void *)this); -} - void Stmt::PrintStats() { // Ensure the table is primed. getStmtInfoTableEntry(Stmt::NullStmtClass); @@ -569,10 +562,10 @@ QualType CXXCatchStmt::getCaughtType() { return QualType(); } -void CXXCatchStmt::Destroy(ASTContext& C) { +void CXXCatchStmt::DoDestroy(ASTContext& C) { if (ExceptionDecl) ExceptionDecl->Destroy(C); - Stmt::Destroy(C); + Stmt::DoDestroy(C); } // CXXTryStmt