Overhaul of Stmt allocation:
- Made allocation of Stmt objects using vanilla new/delete a *compiler
error* by making this new/delete "protected" within class Stmt.
- Now the only way to allocate Stmt objects is by using the new
operator that takes ASTContext& as an argument. This ensures that
all Stmt nodes are allocated from the same (pool) allocator.
- Naturally, these two changes required that *all* creation sites for
AST nodes use new (ASTContext&). This is a large patch, but the
majority of the changes are just this mechanical adjustment.
- The above changes also mean that AST nodes can no longer be
deallocated using 'delete'. Instead, one most do
StmtObject->Destroy(ASTContext&) or do
ASTContextObject.Deallocate(StmtObject) (the latter not running the
'Destroy' method).
Along the way I also...
- Made CompoundStmt allocate its array of Stmt* using the allocator in
ASTContext (previously it used std::vector). There are a whole
bunch of other Stmt classes that need to be similarly changed to
ensure that all memory allocated for ASTs comes from the allocator
in ASTContext.
- Added a new smart pointer ExprOwningPtr to Sema.h. This replaces
the uses of llvm::OwningPtr within Sema, as llvm::OwningPtr used
'delete' to free memory instead of a Stmt's 'Destroy' method.
Big thanks to Doug Gregor for helping with the acrobatics of making
'new/delete' private and the new smart pointer ExprOwningPtr!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63997 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp
index cb65900..69bbf0e 100644
--- a/lib/Sema/SemaOverload.cpp
+++ b/lib/Sema/SemaOverload.cpp
@@ -3600,9 +3600,9 @@
// Extract the object argument.
Expr *ObjectArg = MemExpr->getBase();
if (MemExpr->isArrow())
- ObjectArg = new UnaryOperator(ObjectArg, UnaryOperator::Deref,
- ObjectArg->getType()->getAsPointerType()->getPointeeType(),
- SourceLocation());
+ ObjectArg = new (Context) UnaryOperator(ObjectArg, UnaryOperator::Deref,
+ ObjectArg->getType()->getAsPointerType()->getPointeeType(),
+ SourceLocation());
CXXMethodDecl *Method = 0;
if (OverloadedFunctionDecl *Ovl
= dyn_cast<OverloadedFunctionDecl>(MemExpr->getMemberDecl())) {
@@ -3647,8 +3647,8 @@
}
assert(Method && "Member call to something that isn't a method?");
- llvm::OwningPtr<CXXMemberCallExpr>
- TheCall(new CXXMemberCallExpr(MemExpr, Args, NumArgs,
+ ExprOwningPtr<CXXMemberCallExpr>
+ TheCall(this, new (Context) CXXMemberCallExpr(MemExpr, Args, NumArgs,
Method->getResultType().getNonReferenceType(),
RParenLoc));
@@ -3759,9 +3759,9 @@
if (Best == CandidateSet.end()) {
// We had an error; delete all of the subexpressions and return
// the error.
- delete Object;
+ Object->Destroy(Context);
for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
- delete Args[ArgIdx];
+ Args[ArgIdx]->Destroy(Context);
return true;
}
@@ -3807,22 +3807,23 @@
for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
MethodArgs[ArgIdx + 1] = Args[ArgIdx];
- Expr *NewFn = new DeclRefExpr(Method, Method->getType(),
- SourceLocation());
+ Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(),
+ SourceLocation());
UsualUnaryConversions(NewFn);
// Once we've built TheCall, all of the expressions are properly
// owned.
QualType ResultTy = Method->getResultType().getNonReferenceType();
- llvm::OwningPtr<CXXOperatorCallExpr>
- TheCall(new CXXOperatorCallExpr(NewFn, MethodArgs, NumArgs + 1,
- ResultTy, RParenLoc));
+ ExprOwningPtr<CXXOperatorCallExpr>
+ TheCall(this, new (Context) CXXOperatorCallExpr(NewFn, MethodArgs,
+ NumArgs + 1,
+ ResultTy, RParenLoc));
delete [] MethodArgs;
// We may have default arguments. If so, we need to allocate more
// slots in the call for them.
if (NumArgs < NumArgsInProto)
- TheCall->setNumArgs(NumArgsInProto + 1);
+ TheCall->setNumArgs(Context, NumArgsInProto + 1);
else if (NumArgs > NumArgsInProto)
NumArgsToCheck = NumArgsInProto;
@@ -3842,7 +3843,7 @@
if (PerformCopyInitialization(Arg, ProtoArgType, "passing"))
return true;
} else {
- Arg = new CXXDefaultArgExpr(Method->getParamDecl(i));
+ Arg = new (Context) CXXDefaultArgExpr(Method->getParamDecl(i));
}
TheCall->setArg(i + 1, Arg);
@@ -3888,7 +3889,7 @@
AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet,
/*SuppressUserConversions=*/false);
- llvm::OwningPtr<Expr> BasePtr(Base);
+ ExprOwningPtr<Expr> BasePtr(this, Base);
// Perform overload resolution.
OverloadCandidateSet::iterator Best;
@@ -3924,9 +3925,10 @@
BasePtr.take();
// Build the operator call.
- Expr *FnExpr = new DeclRefExpr(Method, Method->getType(), SourceLocation());
+ Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(),
+ SourceLocation());
UsualUnaryConversions(FnExpr);
- Base = new CXXOperatorCallExpr(FnExpr, &Base, 1,
+ Base = new (Context) CXXOperatorCallExpr(FnExpr, &Base, 1,
Method->getResultType().getNonReferenceType(),
OpLoc);
return ActOnMemberReferenceExpr(S, ExprArg(*this, Base), OpLoc, tok::arrow,