From 668bf91d31265b6ea8c3eb854ba450857701f269 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 9 Feb 2009 20:51:47 +0000 Subject: [PATCH] CallExpr now uses ASTContext's allocate to allocate/delete its array of subexpressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64162 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/RewriteObjC.cpp | 30 ++++++++++++++++++------------ include/clang/AST/Expr.h | 18 ++++++++++++++---- include/clang/AST/ExprCXX.h | 12 ++++++------ lib/AST/Expr.cpp | 21 ++++++++++++++++----- lib/AST/StmtSerialization.cpp | 1 - lib/Sema/SemaExpr.cpp | 18 ++++++++++-------- lib/Sema/SemaOverload.cpp | 13 +++++++++---- 7 files changed, 73 insertions(+), 40 deletions(-) diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index 187314608f..0674f8f116 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -1778,7 +1778,8 @@ CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( const FunctionType *FT = msgSendType->getAsFunctionType(); - return new (Context) CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); + return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), + SourceLocation()); } static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, @@ -2311,8 +2312,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // Simulate a contructor call... DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, superType, SourceLocation()); - SuperRep = new (Context) CallExpr(DRE, &InitExprs[0], InitExprs.size(), - superType, SourceLocation()); + SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], + InitExprs.size(), + superType, SourceLocation()); // The code for super is a little tricky to prevent collision with // the structure definition in the header. The rewriter has it's own // internal definition (__rw_objc_super) that is uses. This is why @@ -2394,8 +2396,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // Simulate a contructor call... DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, superType, SourceLocation()); - SuperRep = new (Context) CallExpr(DRE, &InitExprs[0], InitExprs.size(), - superType, SourceLocation()); + SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], + InitExprs.size(), + superType, SourceLocation()); // The code for super is a little tricky to prevent collision with // the structure definition in the header. The rewriter has it's own // internal definition (__rw_objc_super) that is uses. This is why @@ -2521,8 +2524,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); const FunctionType *FT = msgSendType->getAsFunctionType(); - CallExpr *CE = new (Context) CallExpr(PE, &MsgExprs[0], MsgExprs.size(), - FT->getResultType(), SourceLocation()); + CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], + MsgExprs.size(), + FT->getResultType(), SourceLocation()); Stmt *ReplacingStmt = CE; if (MsgSendStretFlavor) { // We have the method which returns a struct/union. Must also generate @@ -2548,8 +2552,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); FT = msgSendType->getAsFunctionType(); - CallExpr *STCE = new (Context) CallExpr(PE, &MsgExprs[0], MsgExprs.size(), - FT->getResultType(), SourceLocation()); + CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], + MsgExprs.size(), + FT->getResultType(), SourceLocation()); // Build sizeof(returnType) SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, true, @@ -3864,7 +3869,8 @@ Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { E = Exp->arg_end(); I != E; ++I) { BlkExprs.push_back(*I); } - CallExpr *CE = new (Context) CallExpr(PE, &BlkExprs[0], BlkExprs.size(), + CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], + BlkExprs.size(), Exp->getType(), SourceLocation()); return CE; } @@ -4157,8 +4163,8 @@ Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { InitExprs.push_back(Exp); } } - NewRep = new (Context) CallExpr(DRE, &InitExprs[0], InitExprs.size(), - FType, SourceLocation()); + NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), + FType, SourceLocation()); NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, Context->getPointerType(NewRep->getType()), SourceLocation()); diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 55b31b4369..408c660a48 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -770,13 +770,16 @@ class CallExpr : public Expr { protected: // This version of the constructor is for derived classes. - CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t, - SourceLocation rparenloc); + CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, + QualType t, SourceLocation rparenloc); public: - CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, + CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t, SourceLocation rparenloc); - ~CallExpr() { delete [] SubExprs; } + + ~CallExpr() {} + + void Destroy(ASTContext& C); const Expr *getCallee() const { return cast(SubExprs[FN]); } Expr *getCallee() { return cast(SubExprs[FN]); } @@ -795,12 +798,19 @@ public: assert(Arg < NumArgs && "Arg access out of range!"); return cast(SubExprs[Arg+ARGS_START]); } + + // FIXME: Why is this needed? Why not just create the CallExpr with the + // corect number of arguments? It makes the ASTs less brittle. /// setArg - Set the specified argument. void setArg(unsigned Arg, Expr *ArgExpr) { assert(Arg < NumArgs && "Arg access out of range!"); SubExprs[Arg+ARGS_START] = ArgExpr; } + // FIXME: It would be great to just get rid of this. There is only one + // callee of this method, and it probably could be refactored to not use + // this method and instead just create a CallExpr with the right number of + // arguments. /// setNumArgs - This changes the number of arguments present in this call. /// Any orphaned expressions are deleted by this, and any new operands are set /// to null. diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index 26f3b6c161..22e852dbd1 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -32,9 +32,9 @@ namespace clang { /// better information about the syntactic representation of the call. class CXXOperatorCallExpr : public CallExpr { public: - CXXOperatorCallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, - SourceLocation operatorloc) - : CallExpr(CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc) { } + CXXOperatorCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, + QualType t, SourceLocation operatorloc) + : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc){} /// getOperator - Returns the kind of overloaded operator that this /// expression refers to. @@ -65,9 +65,9 @@ public: /// the object argument). class CXXMemberCallExpr : public CallExpr { public: - CXXMemberCallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, - SourceLocation rparenloc) - : CallExpr(CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) { } + CXXMemberCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, + QualType t, SourceLocation rparenloc) + : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {} /// getImplicitObjectArgument - Retrieves the implicit object /// argument for the member call. For example, in "x.f(5)", this diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 27ae2b2266..95a6a349f3 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -105,32 +105,43 @@ const char *UnaryOperator::getOpcodeStr(Opcode Op) { // Postfix Operators. //===----------------------------------------------------------------------===// -CallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args, +CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t, SourceLocation rparenloc) : Expr(SC, t, fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), NumArgs(numargs) { - SubExprs = new Stmt*[numargs+1]; + + SubExprs = new (C) Stmt*[numargs+1]; SubExprs[FN] = fn; for (unsigned i = 0; i != numargs; ++i) SubExprs[i+ARGS_START] = args[i]; + RParenLoc = rparenloc; } -CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, - SourceLocation rparenloc) +CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, + QualType t, SourceLocation rparenloc) : Expr(CallExprClass, t, fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), NumArgs(numargs) { - SubExprs = new Stmt*[numargs+1]; + + SubExprs = new (C) Stmt*[numargs+1]; SubExprs[FN] = fn; for (unsigned i = 0; i != numargs; ++i) SubExprs[i+ARGS_START] = args[i]; + RParenLoc = rparenloc; } +void CallExpr::Destroy(ASTContext& C) { + DestroyChildren(C); + if (SubExprs) C.Deallocate(SubExprs); + this->~CallExpr(); + C.Deallocate(this); +} + /// setNumArgs - This changes the number of arguments present in this call. /// Any orphaned expressions are deleted by this, and any new operands are set /// to null. diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp index 4d92245f75..5a6e49cb3e 100644 --- a/lib/AST/StmtSerialization.cpp +++ b/lib/AST/StmtSerialization.cpp @@ -402,7 +402,6 @@ CallExpr* CallExpr::CreateImpl(Deserializer& D, ASTContext& C, StmtClass SC) { unsigned NumArgs = D.ReadInt(); Stmt** SubExprs = new (C, llvm::alignof()) Stmt*[NumArgs+1]; D.BatchReadOwnedPtrs(NumArgs+1, SubExprs, C); - return new (C, llvm::alignof()) CallExpr(SC, SubExprs,NumArgs,t,L); } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f881e81bf4..af417963b1 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1226,8 +1226,8 @@ Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, UsualUnaryConversions(FnExpr); Input.release(); - return Owned(new (Context)CXXOperatorCallExpr(FnExpr, Args, 2, ResultTy, - OpLoc)); + return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, + ResultTy, OpLoc)); } else { // We matched a built-in operator. Convert the arguments, then // break out so that we will build the appropriate built-in @@ -1326,7 +1326,7 @@ Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, Base.release(); Idx.release(); - return Owned(new (Context) CXXOperatorCallExpr(FnExpr, Args, 2, + return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, ResultTy, LLoc)); } else { // We matched a built-in operator. Convert the arguments, then @@ -1850,7 +1850,7 @@ Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, Dependent = true; if (Dependent) - return Owned(new (Context) CallExpr(Fn, Args, NumArgs, + return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, Context.DependentTy, RParenLoc)); // Determine whether this is a call to an object (C++ [over.call.object]). @@ -1943,8 +1943,10 @@ Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, // of arguments and function on error. // FIXME: Except that llvm::OwningPtr uses delete, when it really must be // Destroy(), or nothing gets cleaned up. - ExprOwningPtr TheCall(this, new (Context) CallExpr(Fn, Args,NumArgs, - Context.BoolTy, RParenLoc)); + ExprOwningPtr TheCall(this, new (Context) CallExpr(Context, Fn, + Args, NumArgs, + Context.BoolTy, + RParenLoc)); const FunctionType *FuncT; if (!Fn->getType()->isBlockPointerType()) { @@ -3795,7 +3797,7 @@ Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, SourceLocation()); UsualUnaryConversions(FnExpr); - return Owned(new (Context) CXXOperatorCallExpr(FnExpr, Args, 2, + return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, ResultTy, TokLoc)); } else { // We matched a built-in operator. Convert the arguments, then @@ -3897,7 +3899,7 @@ Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, UsualUnaryConversions(FnExpr); input.release(); - return Owned(new (Context) CXXOperatorCallExpr(FnExpr, &Input, 1, + return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, &Input, 1, ResultTy, OpLoc)); } else { // We matched a built-in operator. Convert the arguments, then diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 69bbf0e05a..c91b507dc8 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -2123,7 +2123,11 @@ Sema::AddConversionCandidate(CXXConversionDecl *Conversion, SourceLocation()); ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), &ConversionRef, false); - CallExpr Call(&ConversionFn, 0, 0, + + // Note that it is safe to allocate CallExpr on the stack here because + // there are 0 arguments (i.e., nothing is allocated using ASTContext's + // allocator). + CallExpr Call(Context, &ConversionFn, 0, 0, Conversion->getConversionType().getNonReferenceType(), SourceLocation()); ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); @@ -3648,7 +3652,8 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, assert(Method && "Member call to something that isn't a method?"); ExprOwningPtr - TheCall(this, new (Context) CXXMemberCallExpr(MemExpr, Args, NumArgs, + TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, + NumArgs, Method->getResultType().getNonReferenceType(), RParenLoc)); @@ -3815,7 +3820,7 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, // owned. QualType ResultTy = Method->getResultType().getNonReferenceType(); ExprOwningPtr - TheCall(this, new (Context) CXXOperatorCallExpr(NewFn, MethodArgs, + TheCall(this, new (Context) CXXOperatorCallExpr(Context, NewFn, MethodArgs, NumArgs + 1, ResultTy, RParenLoc)); delete [] MethodArgs; @@ -3928,7 +3933,7 @@ Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), SourceLocation()); UsualUnaryConversions(FnExpr); - Base = new (Context) CXXOperatorCallExpr(FnExpr, &Base, 1, + Base = new (Context) CXXOperatorCallExpr(Context, FnExpr, &Base, 1, Method->getResultType().getNonReferenceType(), OpLoc); return ActOnMemberReferenceExpr(S, ExprArg(*this, Base), OpLoc, tok::arrow, -- 2.40.0