From: Ted Kremenek Date: Sat, 7 Feb 2009 01:47:29 +0000 (+0000) Subject: Overhaul of Stmt allocation: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8189cde56b4f6f938cd65f53c932fe1860d0204c;p=clang 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/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index 4e262963a9..187314608f 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -339,7 +339,9 @@ namespace { // We avoid calling Type::isBlockPointerType(), since it operates on the // canonical type. We only care if the top-level type is a closure pointer. - bool isTopLevelBlockPointerType(QualType T) { return isa(T); } + bool isTopLevelBlockPointerType(QualType T) { + return isa(T); + } // FIXME: This predicate seems like it would be useful to add to ASTContext. bool isObjCType(QualType T) { @@ -360,7 +362,8 @@ namespace { return false; } bool PointerTypeTakesAnyBlockArguments(QualType QT); - void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen); + void GetExtentOfArgList(const char *Name, const char *&LParen, + const char *&RParen); void RewriteCastExpr(CStyleCastExpr *CE); FunctionDecl *SynthBlockInitFunctionDecl(const char *name); @@ -410,7 +413,8 @@ RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, "rewriting sub-expression within a macro (may not be correct)"); TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, - "rewriter doesn't support user-specified control flow semantics for @try/@finally (code may not execute properly)"); + "rewriter doesn't support user-specified control flow semantics " + "for @try/@finally (code may not execute properly)"); } ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, @@ -465,7 +469,8 @@ void RewriteObjC::Initialize(ASTContext &context) { Preamble += "struct objc_object *superClass; "; if (LangOpts.Microsoft) { // Add a constructor for creating temporary objects. - Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) : "; + Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " + ": "; Preamble += "object(o), superClass(s) {} "; } Preamble += "};\n"; @@ -672,7 +677,8 @@ void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, assert((*startBuf == '@') && "bogus @synthesize location"); const char *semiBuf = strchr(startBuf, ';'); assert((*semiBuf == ';') && "@synthesize: can't find ';'"); - SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); + SourceLocation onePastSemiLoc = + startLoc.getFileLocWithOffset(semiBuf-startBuf+1); if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) return; // FIXME: is this correct? @@ -1066,7 +1072,7 @@ Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, // This allows us to handle chain/nested property getters. Receiver = PropGetters[PRE]; } - MsgExpr = new ObjCMessageExpr(dyn_cast(Receiver), + MsgExpr = new (Context) ObjCMessageExpr(dyn_cast(Receiver), PDecl->getSetterName(), PDecl->getType(), PDecl->getSetterMethodDecl(), SourceLocation(), SourceLocation(), @@ -1076,7 +1082,9 @@ Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, // Now do the actual rewrite. ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); //delete BinOp; - delete MsgExpr; + // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references + // to things that stay around. + Context->Deallocate(MsgExpr); return ReplacingStmt; } @@ -1093,7 +1101,7 @@ Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { // This allows us to handle chain/nested property getters. Receiver = PropGetters[PRE]; } - MsgExpr = new ObjCMessageExpr(dyn_cast(Receiver), + MsgExpr = new (Context) ObjCMessageExpr(dyn_cast(Receiver), PDecl->getGetterName(), PDecl->getType(), PDecl->getGetterMethodDecl(), SourceLocation(), SourceLocation(), @@ -1109,12 +1117,16 @@ Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { // We stash away the ReplacingStmt since actually doing the // replacement/rewrite won't work for nested getters (e.g. obj.p.i) PropGetters[PropRefExpr] = ReplacingStmt; - delete MsgExpr; + // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references + // to things that stay around. + Context->Deallocate(MsgExpr); return PropRefExpr; // return the original... } else { ReplaceStmt(PropRefExpr, ReplacingStmt); - // delete PropRefExpr; elsewhere... - delete MsgExpr; + // delete PropRefExpr; elsewhere... + // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references + // to things that stay around. + Context->Deallocate(MsgExpr); return ReplacingStmt; } } @@ -1124,7 +1136,8 @@ Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, ObjCIvarDecl *D = IV->getDecl(); if (CurMethodDef) { if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { - ObjCInterfaceType *iFaceDecl = dyn_cast(pType->getPointeeType()); + ObjCInterfaceType *iFaceDecl = + dyn_cast(pType->getPointeeType()); // lookup which class implements the instance variable. ObjCInterfaceDecl *clsDeclared = 0; iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); @@ -1138,16 +1151,18 @@ Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation(), II); assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); - CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, - SourceLocation(), SourceLocation()); + CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), + castT,SourceLocation(), + SourceLocation()); // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), - IV->getBase()->getLocEnd(), - castExpr); + ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), + IV->getBase()->getLocEnd(), + castExpr); if (IV->isFreeIvar() && CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { - MemberExpr *ME = new MemberExpr(PE, true, D, IV->getLocation(), - D->getType()); + MemberExpr *ME = new (Context) MemberExpr(PE, true, D, + IV->getLocation(), + D->getType()); ReplaceStmt(IV, ME); // delete IV; leak for now, see RewritePropertySetter() usage for more info. return ME; @@ -1180,10 +1195,11 @@ Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation(), II); assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); - CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, - SourceLocation(), SourceLocation()); + CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), + castT, SourceLocation(), + SourceLocation()); // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), + ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), IV->getBase()->getLocEnd(), castExpr); ReplaceStmt(IV->getBase(), PE); // Cannot delete IV->getBase(), since PE points to it. @@ -1475,10 +1491,11 @@ Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { buf += " _rethrow = objc_exception_extract(&_stack);\n"; buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; buf += " objc_sync_exit("; - Expr *syncExpr = new CStyleCastExpr(Context->getObjCIdType(), - S->getSynchExpr(), - Context->getObjCIdType(), - SourceLocation(), SourceLocation()); + Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), + S->getSynchExpr(), + Context->getObjCIdType(), + SourceLocation(), + SourceLocation()); std::string syncExprBufS; llvm::raw_string_ostream syncExprBuf(syncExprBufS); syncExpr->printPretty(syncExprBuf); @@ -1717,7 +1734,7 @@ Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { QualType StrType = Context->getPointerType(Context->CharTy); std::string StrEncoding; Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); - Expr *Replacement = new (*Context) StringLiteral(*Context,StrEncoding.c_str(), + Expr *Replacement = new (Context) StringLiteral(*Context,StrEncoding.c_str(), StrEncoding.length(), false, StrType, SourceLocation(), SourceLocation()); ReplaceStmt(Exp, Replacement); @@ -1734,7 +1751,7 @@ Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { // Create a call to sel_registerName("selName"). llvm::SmallVector SelExprs; QualType argType = Context->getPointerType(Context->CharTy); - SelExprs.push_back(new (*Context) StringLiteral((*Context), + SelExprs.push_back(new (Context) StringLiteral((*Context), Exp->getSelector().getAsString().c_str(), Exp->getSelector().getAsString().size(), false, argType, SourceLocation(), @@ -1752,16 +1769,16 @@ CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( QualType msgSendType = FD->getType(); // Create a reference to the objc_msgSend() declaration. - DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); + DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); // Now, we cast the reference to a pointer to the objc_msgSend type. QualType pToFunc = Context->getPointerType(msgSendType); - ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE, + ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, DRE, /*isLvalue=*/false); const FunctionType *FT = msgSendType->getAsFunctionType(); - return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); + return new (Context) CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); } static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, @@ -2134,12 +2151,12 @@ Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), &Context->Idents.get(S.c_str()), strType, VarDecl::Static); - DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); - Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, + DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); + Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, Context->getPointerType(DRE->getType()), SourceLocation()); // cast to NSConstantString * - CastExpr *cast = new CStyleCastExpr(Exp->getType(), Unop, + CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), Unop, Exp->getType(), SourceLocation(), SourceLocation()); ReplaceStmt(Exp, cast); // delete Exp; leak for now, see RewritePropertySetter() usage for more info. @@ -2266,13 +2283,13 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { llvm::SmallVector InitExprs; // set the receiver to self, the first argument to all methods. - InitExprs.push_back(new DeclRefExpr( + InitExprs.push_back(new (Context) DeclRefExpr( CurMethodDef->getSelfDecl(), Context->getObjCIdType(), SourceLocation())); llvm::SmallVector ClsExprs; QualType argType = Context->getPointerType(Context->CharTy); - ClsExprs.push_back(new (*Context) StringLiteral(*Context, + ClsExprs.push_back(new (Context) StringLiteral(*Context, SuperDecl->getIdentifier()->getName(), SuperDecl->getIdentifier()->getLength(), false, argType, SourceLocation(), @@ -2282,7 +2299,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { ClsExprs.size()); // To turn off a warning, type-cast to 'id' InitExprs.push_back( // set 'super class', using objc_getClass(). - new CStyleCastExpr(Context->getObjCIdType(), + new (Context) CStyleCastExpr(Context->getObjCIdType(), Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); // struct objc_super @@ -2292,9 +2309,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { if (LangOpts.Microsoft) { SynthSuperContructorFunctionDecl(); // Simulate a contructor call... - DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, + DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, superType, SourceLocation()); - SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), + SuperRep = new (Context) CallExpr(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 @@ -2302,21 +2319,21 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // we need the cast below. For example: // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) // - SuperRep = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, + SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, Context->getPointerType(SuperRep->getType()), SourceLocation()); - SuperRep = new CStyleCastExpr(Context->getPointerType(superType), + SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), SuperRep, Context->getPointerType(superType), SourceLocation(), SourceLocation()); } else { // (struct objc_super) { } - InitListExpr *ILE = new InitListExpr(SourceLocation(), + InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), &InitExprs[0], InitExprs.size(), SourceLocation()); - SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, + SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); // struct objc_super * - SuperRep = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, + SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, Context->getPointerType(SuperRep->getType()), SourceLocation()); } @@ -2324,7 +2341,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { } else { llvm::SmallVector ClsExprs; QualType argType = Context->getPointerType(Context->CharTy); - ClsExprs.push_back(new (*Context) StringLiteral(*Context, + ClsExprs.push_back(new (Context) StringLiteral(*Context, clsName->getName(), clsName->getLength(), false, argType, SourceLocation(), @@ -2346,8 +2363,8 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { llvm::SmallVector InitExprs; InitExprs.push_back( - new CStyleCastExpr(Context->getObjCIdType(), - new DeclRefExpr(CurMethodDef->getSelfDecl(), + new (Context) CStyleCastExpr(Context->getObjCIdType(), + new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), Context->getObjCIdType(), SourceLocation()), Context->getObjCIdType(), @@ -2355,7 +2372,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { llvm::SmallVector ClsExprs; QualType argType = Context->getPointerType(Context->CharTy); - ClsExprs.push_back(new (*Context) StringLiteral(*Context, + ClsExprs.push_back(new (Context) StringLiteral(*Context, SuperDecl->getIdentifier()->getName(), SuperDecl->getIdentifier()->getLength(), false, argType, SourceLocation(), @@ -2366,7 +2383,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // To turn off a warning, type-cast to 'id' InitExprs.push_back( // set 'super class', using objc_getClass(). - new CStyleCastExpr(Context->getObjCIdType(), + new (Context) CStyleCastExpr(Context->getObjCIdType(), Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); // struct objc_super QualType superType = getSuperStructType(); @@ -2375,9 +2392,9 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { if (LangOpts.Microsoft) { SynthSuperContructorFunctionDecl(); // Simulate a contructor call... - DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, + DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, superType, SourceLocation()); - SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), + SuperRep = new (Context) CallExpr(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 @@ -2385,18 +2402,18 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // we need the cast below. For example: // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) // - SuperRep = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, + SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, Context->getPointerType(SuperRep->getType()), SourceLocation()); - SuperRep = new CStyleCastExpr(Context->getPointerType(superType), + SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), SuperRep, Context->getPointerType(superType), SourceLocation(), SourceLocation()); } else { // (struct objc_super) { } - InitListExpr *ILE = new InitListExpr(SourceLocation(), + InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), &InitExprs[0], InitExprs.size(), SourceLocation()); - SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); + SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); } MsgExprs.push_back(SuperRep); } else { @@ -2404,7 +2421,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // Foo *. while (CStyleCastExpr *CE = dyn_cast(recExpr)) recExpr = CE->getSubExpr(); - recExpr = new CStyleCastExpr(Context->getObjCIdType(), recExpr, + recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), recExpr, Context->getObjCIdType(), SourceLocation(), SourceLocation()); MsgExprs.push_back(recExpr); @@ -2413,7 +2430,7 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // Create a call to sel_registerName("selName"), it will be the 2nd argument. llvm::SmallVector SelExprs; QualType argType = Context->getPointerType(Context->CharTy); - SelExprs.push_back(new (*Context) StringLiteral(*Context, + SelExprs.push_back(new (Context) StringLiteral(*Context, Exp->getSelector().getAsString().c_str(), Exp->getSelector().getAsString().size(), false, argType, SourceLocation(), @@ -2431,14 +2448,14 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { QualType type = ICE->getType()->isObjCQualifiedIdType() ? Context->getObjCIdType() : ICE->getType(); - userExpr = new CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); + userExpr = new (Context) CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); } // Make id cast into an 'id' cast. else if (CStyleCastExpr *CE = dyn_cast(userExpr)) { if (CE->getType()->isObjCQualifiedIdType()) { while ((CE = dyn_cast(userExpr))) userExpr = CE->getSubExpr(); - userExpr = new CStyleCastExpr(Context->getObjCIdType(), + userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), userExpr, Context->getObjCIdType(), SourceLocation(), SourceLocation()); } @@ -2481,14 +2498,14 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { QualType msgSendType = MsgSendFlavor->getType(); // Create a reference to the objc_msgSend() declaration. - DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, + DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, SourceLocation()); // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). // If we don't do this cast, we get the following bizarre warning/note: // xx.m:13: warning: function called through a non-compatible type // xx.m:13: note: if this code is reached, the program will abort - cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, + cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, Context->getPointerType(Context->VoidTy), SourceLocation(), SourceLocation()); @@ -2498,13 +2515,13 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // If we don't have a method decl, force a variadic cast. Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); castType = Context->getPointerType(castType); - cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); + cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); + ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); const FunctionType *FT = msgSendType->getAsFunctionType(); - CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), + CallExpr *CE = new (Context) CallExpr(PE, &MsgExprs[0], MsgExprs.size(), FT->getResultType(), SourceLocation()); Stmt *ReplacingStmt = CE; if (MsgSendStretFlavor) { @@ -2514,10 +2531,10 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // method's return type. // Create a reference to the objc_msgSend_stret() declaration. - DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, + DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, SourceLocation()); // Need to cast objc_msgSend_stret to "void *" (see above comment). - cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, + cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, Context->getPointerType(Context->VoidTy), SourceLocation(), SourceLocation()); // Now do the "normal" pointer to function cast. @@ -2525,17 +2542,17 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { &ArgTypes[0], ArgTypes.size(), Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); castType = Context->getPointerType(castType); - cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); + cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); // Don't forget the parens to enforce the proper binding. - PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); + PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); FT = msgSendType->getAsFunctionType(); - CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), + CallExpr *STCE = new (Context) CallExpr(PE, &MsgExprs[0], MsgExprs.size(), FT->getResultType(), SourceLocation()); // Build sizeof(returnType) - SizeOfAlignOfExpr *sizeofExpr = new SizeOfAlignOfExpr(true, true, + SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, true, returnType.getAsOpaquePtr(), Context->getSizeType(), SourceLocation(), SourceLocation()); @@ -2545,17 +2562,17 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { // is needed to decide what to do. unsigned IntSize = static_cast(Context->getTypeSize(Context->IntTy)); - IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), + IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), Context->IntTy, SourceLocation()); - BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, + BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, BinaryOperator::LE, Context->IntTy, SourceLocation()); // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) ConditionalOperator *CondExpr = - new ConditionalOperator(lessThanExpr, CE, STCE, returnType); - ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); + new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); + ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); } return ReplacingStmt; } @@ -2579,7 +2596,7 @@ Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { // Create a call to objc_getProtocol("ProtocolName"). llvm::SmallVector ProtoExprs; QualType argType = Context->getPointerType(Context->CharTy); - ProtoExprs.push_back(new (*Context) + ProtoExprs.push_back(new (Context) StringLiteral(*Context, Exp->getProtocol()->getNameAsCString(), strlen(Exp->getProtocol()->getNameAsCString()), @@ -3819,18 +3836,25 @@ Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); - CastExpr *BlkCast = new CStyleCastExpr(PtrBlock, Exp->getCallee(), PtrBlock, SourceLocation(), SourceLocation()); + CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, Exp->getCallee(), + PtrBlock, SourceLocation(), + SourceLocation()); // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), BlkCast); + ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), + BlkCast); //PE->dump(); FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, /*BitWidth=*/0, /*Mutable=*/true); - MemberExpr *ME = new MemberExpr(PE, true, FD, SourceLocation(), FD->getType()); + MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), + FD->getType()); - CastExpr *FunkCast = new CStyleCastExpr(PtrToFuncCastType, ME, PtrToFuncCastType, SourceLocation(), SourceLocation()); - PE = new ParenExpr(SourceLocation(), SourceLocation(), FunkCast); + CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, ME, + PtrToFuncCastType, + SourceLocation(), + SourceLocation()); + PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); llvm::SmallVector BlkExprs; // Add the implicit argument. @@ -3840,7 +3864,8 @@ Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { E = Exp->arg_end(); I != E; ++I) { BlkExprs.push_back(*I); } - CallExpr *CE = new CallExpr(PE, &BlkExprs[0], BlkExprs.size(), Exp->getType(), SourceLocation()); + CallExpr *CE = new (Context) CallExpr(PE, &BlkExprs[0], BlkExprs.size(), + Exp->getType(), SourceLocation()); return CE; } @@ -3851,11 +3876,11 @@ void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { // FIXME: Add more elaborate code generation required by the ABI. - Expr *DerefExpr = new UnaryOperator(BDRE, UnaryOperator::Deref, + Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, Context->getPointerType(BDRE->getType()), SourceLocation()); // Need parens to enforce precedence. - ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); + ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); ReplaceStmt(BDRE, PE); } @@ -3942,8 +3967,8 @@ bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { return false; } -void RewriteObjC::GetExtentOfArgList(const char *Name, - const char *&LParen, const char *&RParen) { +void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, + const char *&RParen) { const char *argPtr = strchr(Name, '('); assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); @@ -4069,30 +4094,34 @@ Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { // Simulate a contructor call... FD = SynthBlockInitFunctionDecl(Tag.c_str()); - DeclRefExpr *DRE = new DeclRefExpr(FD, FType, SourceLocation()); + DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); llvm::SmallVector InitExprs; // Initialize the block function. FD = SynthBlockInitFunctionDecl(Func.c_str()); - DeclRefExpr *Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); - CastExpr *castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, - Context->VoidPtrTy, SourceLocation(), SourceLocation()); + DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), + SourceLocation()); + CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, + Context->VoidPtrTy, SourceLocation(), + SourceLocation()); InitExprs.push_back(castExpr); if (ImportedBlockDecls.size()) { std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; FD = SynthBlockInitFunctionDecl(Buf.c_str()); - Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); - castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, - Context->VoidPtrTy, SourceLocation(), SourceLocation()); + Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); + castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, + Context->VoidPtrTy, SourceLocation(), + SourceLocation()); InitExprs.push_back(castExpr); Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; FD = SynthBlockInitFunctionDecl(Buf.c_str()); - Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); - castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, - Context->VoidPtrTy, SourceLocation(), SourceLocation()); + Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); + castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, + Context->VoidPtrTy, SourceLocation(), + SourceLocation()); InitExprs.push_back(castExpr); } // Add initializers for any closure decl refs. @@ -4104,15 +4133,16 @@ Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { if (isObjCType((*I)->getType())) { // FIXME: Conform to ABI ([[obj retain] autorelease]). FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); + Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); } else if (isTopLevelBlockPointerType((*I)->getType())) { FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); - Exp = new CStyleCastExpr(Context->VoidPtrTy, Arg, - Context->VoidPtrTy, SourceLocation(), SourceLocation()); + Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); + Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, + Context->VoidPtrTy, SourceLocation(), + SourceLocation()); } else { FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); + Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); } InitExprs.push_back(Exp); } @@ -4120,19 +4150,20 @@ Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { for (llvm::SmallPtrSet::iterator I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; ++I) { FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); - Exp = new UnaryOperator(Exp, UnaryOperator::AddrOf, + Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); + Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, Context->getPointerType(Exp->getType()), SourceLocation()); InitExprs.push_back(Exp); } } - NewRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), + NewRep = new (Context) CallExpr(DRE, &InitExprs[0], InitExprs.size(), FType, SourceLocation()); - NewRep = new UnaryOperator(NewRep, UnaryOperator::AddrOf, + NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, Context->getPointerType(NewRep->getType()), SourceLocation()); - NewRep = new CStyleCastExpr(FType, NewRep, FType, SourceLocation(), SourceLocation()); + NewRep = new (Context) CStyleCastExpr(FType, NewRep, FType, SourceLocation(), + SourceLocation()); BlockDeclRefs.clear(); BlockByRefDecls.clear(); BlockByCopyDecls.clear(); @@ -4363,7 +4394,7 @@ Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { } #if 0 if (ImplicitCastExpr *ICE = dyn_cast(S)) { - CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); + CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); // Get the new text. std::string SStr; llvm::raw_string_ostream Buf(SStr); diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 8cd27c30da..4b17929e11 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -776,9 +776,7 @@ protected: public: CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, SourceLocation rparenloc); - ~CallExpr() { - delete [] SubExprs; - } + ~CallExpr() { delete [] SubExprs; } const Expr *getCallee() const { return cast(SubExprs[FN]); } Expr *getCallee() { return cast(SubExprs[FN]); } @@ -806,7 +804,7 @@ public: /// 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. - void setNumArgs(unsigned NumArgs); + void setNumArgs(ASTContext& C, unsigned NumArgs); typedef ExprIterator arg_iterator; typedef ConstExprIterator const_arg_iterator; diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index fbd252b3bd..61f8b9c9ad 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -22,6 +22,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/iterator.h" #include "llvm/Bitcode/SerializationFwd.h" +#include "clang/AST/ASTContext.h" #include using llvm::dyn_cast_or_null; @@ -102,6 +103,33 @@ public: }; private: const StmtClass sClass; + + // Make vanilla 'new' and 'delete' illegal for Stmts. +protected: + void* operator new(size_t bytes) throw() { + assert(0 && "Stmts cannot be allocated with regular 'new'."); + return 0; + } + void operator delete(void* data) throw() { + assert(0 && "Stmts cannot be released with regular 'delete'."); + } + +public: + // Only allow allocation of Stmts using the allocator in ASTContext + // or by doing a placement new. + void* operator new(size_t bytes, ASTContext& C, + unsigned alignment = 16) throw() { + return ::operator new(bytes, C, alignment); + } + + void* operator new(size_t bytes, ASTContext* C, + unsigned alignment = 16) throw() { + return ::operator new(bytes, *C, alignment); + } + + void* operator new(size_t bytes, void* mem) throw() { + return mem; + } protected: /// DestroyChildren - Invoked by destructors of subclasses of Stmt to @@ -305,37 +333,52 @@ public: /// CompoundStmt - This represents a group of statements like { stmt stmt }. /// class CompoundStmt : public Stmt { - llvm::SmallVector Body; + Stmt** Body; + unsigned NumStmts; SourceLocation LBracLoc, RBracLoc; public: - CompoundStmt(Stmt **StmtStart, unsigned NumStmts, - SourceLocation LB, SourceLocation RB) - : Stmt(CompoundStmtClass), Body(StmtStart, StmtStart+NumStmts), - LBracLoc(LB), RBracLoc(RB) {} - - bool body_empty() const { return Body.empty(); } - - typedef llvm::SmallVector::iterator body_iterator; - body_iterator body_begin() { return Body.begin(); } - body_iterator body_end() { return Body.end(); } - Stmt *body_back() { return Body.back(); } - - typedef llvm::SmallVector::const_iterator const_body_iterator; - const_body_iterator body_begin() const { return Body.begin(); } - const_body_iterator body_end() const { return Body.end(); } - const Stmt *body_back() const { return Body.back(); } - - typedef llvm::SmallVector::reverse_iterator reverse_body_iterator; - reverse_body_iterator body_rbegin() { return Body.rbegin(); } - reverse_body_iterator body_rend() { return Body.rend(); } - - typedef llvm::SmallVector::const_reverse_iterator - const_reverse_body_iterator; - const_reverse_body_iterator body_rbegin() const { return Body.rbegin(); } - const_reverse_body_iterator body_rend() const { return Body.rend(); } - - void push_back(Stmt *S) { Body.push_back(S); } + CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts, + SourceLocation LB, SourceLocation RB) + : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) { + if (NumStmts) { + Body = new (C) Stmt*[NumStmts]; + memcpy(Body, StmtStart, numStmts * sizeof(*Body)); + } + else + Body = 0; + } + + bool body_empty() const { return NumStmts == 0; } + + typedef Stmt** body_iterator; + body_iterator body_begin() { return Body; } + body_iterator body_end() { return Body + NumStmts; } + Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; } + + typedef Stmt* const * const_body_iterator; + const_body_iterator body_begin() const { return Body; } + const_body_iterator body_end() const { return Body + NumStmts; } + const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; } + + typedef std::reverse_iterator reverse_body_iterator; + reverse_body_iterator body_rbegin() { + return reverse_body_iterator(body_end()); + } + reverse_body_iterator body_rend() { + return reverse_body_iterator(body_begin()); + } + + typedef std::reverse_iterator + const_reverse_body_iterator; + + const_reverse_body_iterator body_rbegin() const { + return const_reverse_body_iterator(body_end()); + } + const_reverse_body_iterator body_rend() const { + return const_reverse_body_iterator(body_begin()); + } + virtual SourceRange getSourceRange() const { return SourceRange(LBracLoc, RBracLoc); } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 89fdbe31f3..d686167cd0 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -54,8 +54,8 @@ StringLiteral::StringLiteral(ASTContext& C, const char *strData, } void StringLiteral::Destroy(ASTContext &C) { - C.Deallocate(const_cast(StrData)); this->~StringLiteral(); + C.Deallocate(const_cast(StrData)); } bool UnaryOperator::isPostfix(Opcode Op) { @@ -104,8 +104,8 @@ const char *UnaryOperator::getOpcodeStr(Opcode Op) { // Postfix Operators. //===----------------------------------------------------------------------===// -CallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, - QualType t, SourceLocation rparenloc) +CallExpr::CallExpr(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)), @@ -133,14 +133,14 @@ CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, /// 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. -void CallExpr::setNumArgs(unsigned NumArgs) { +void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { // No change, just return. if (NumArgs == getNumArgs()) return; // If shrinking # arguments, just delete the extras and forgot them. if (NumArgs < getNumArgs()) { for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) - delete getArg(i); + getArg(i)->Destroy(C); this->NumArgs = NumArgs; return; } @@ -154,7 +154,7 @@ void CallExpr::setNumArgs(unsigned NumArgs) { for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) NewSubExprs[i] = 0; - delete[] SubExprs; + delete [] SubExprs; SubExprs = NewSubExprs; this->NumArgs = NumArgs; } @@ -1391,10 +1391,10 @@ void SizeOfAlignOfExpr::Destroy(ASTContext& C) { // will iterate over the size expression. However, this expression belongs // to the type, not to this, so we don't want to delete it. // We still want to delete this expression. - // FIXME: Same as in Stmt::Destroy - will be eventually in ASTContext's - // pool allocator. - if (isArgumentType()) - delete this; + if (isArgumentType()) { + this->~SizeOfAlignOfExpr(); + C.Deallocate(this); + } else Expr::Destroy(C); } diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index b328c1ef3d..801a0f22e2 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -20,10 +20,10 @@ void CXXConditionDeclExpr::Destroy(ASTContext& C) { // FIXME: Cannot destroy the decl here, because it is linked into the // DeclContext's chain. //getVarDecl()->Destroy(C); - delete this; + this->~CXXConditionDeclExpr(); + C.Deallocate(this); } - //===----------------------------------------------------------------------===// // Child Iterators for iterating over subexpressions/substatements //===----------------------------------------------------------------------===// diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 541bb0401c..49c8c80509 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -219,7 +219,7 @@ Stmt::child_iterator NullStmt::child_end() { return child_iterator(); } // CompoundStmt Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; } -Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); } +Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; } // CaseStmt Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; } diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp index 8d6636d6a6..92713cc633 100644 --- a/lib/AST/StmtSerialization.cpp +++ b/lib/AST/StmtSerialization.cpp @@ -483,7 +483,8 @@ void CompoundLiteralExpr::EmitImpl(Serializer& S) const { S.EmitOwnedPtr(Init); } -CompoundLiteralExpr* CompoundLiteralExpr::CreateImpl(Deserializer& D, ASTContext& C) { +CompoundLiteralExpr* CompoundLiteralExpr::CreateImpl(Deserializer& D, + ASTContext& C) { QualType Q = QualType::ReadVal(D); SourceLocation L = SourceLocation::ReadVal(D); bool fileScope = D.ReadBool(); @@ -495,10 +496,8 @@ CompoundLiteralExpr* CompoundLiteralExpr::CreateImpl(Deserializer& D, ASTContext void CompoundStmt::EmitImpl(Serializer& S) const { S.Emit(LBracLoc); S.Emit(RBracLoc); - S.Emit(Body.size()); - - for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I) - S.EmitOwnedPtr(*I); + S.Emit(NumStmts); + if (NumStmts) S.BatchEmitOwnedPtrs(NumStmts, &Body[0]); } CompoundStmt* CompoundStmt::CreateImpl(Deserializer& D, ASTContext& C) { @@ -507,13 +506,15 @@ CompoundStmt* CompoundStmt::CreateImpl(Deserializer& D, ASTContext& C) { unsigned size = D.ReadInt(); CompoundStmt* stmt = new (C, llvm::alignof()) - CompoundStmt(NULL, 0, LB, RB); - - stmt->Body.reserve(size); - - for (unsigned i = 0; i < size; ++i) - stmt->Body.push_back(D.ReadOwnedPtr(C)); + CompoundStmt(C, NULL, 0, LB, RB); + + stmt->NumStmts = size; + if (size) { + stmt->Body = new (C) Stmt*[size]; + D.BatchReadOwnedPtrs(size, &stmt->Body[0], C); + } + return stmt; } @@ -1306,7 +1307,7 @@ ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C) { Expr *B = D.ReadOwnedPtr(C); IdentifierInfo *A = D.ReadPtr(); SourceLocation AL = SourceLocation::ReadVal(D); - return new ExtVectorElementExpr(T, B, *A, AL); + return new (C) ExtVectorElementExpr(T, B, *A, AL); } void BlockExpr::EmitImpl(Serializer& S) const { diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 028c9ebf4f..8667ee28e2 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -183,7 +183,7 @@ void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { ImpCast->setType(Ty); ImpCast->setLvalueCast(isLvalue); } else - Expr = new ImplicitCastExpr(Ty, Expr, isLvalue); + Expr = new (Context) ImplicitCastExpr(Ty, Expr, isLvalue); } void Sema::DeleteExpr(ExprTy *E) { diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 4878403333..88d7a0f6f4 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1970,7 +1970,7 @@ struct BlockSemaInfo { BlockDecl *TheDecl; - /// TheScope - This is the scope for the block itself, which contains + /// TheScope - This is the scope for the block itself, which containsfile://localhost/Volumes/Data/Users/kremenek/llvm/tools/clang /// arguments etc. Scope *TheScope; @@ -1982,8 +1982,23 @@ struct BlockSemaInfo { /// to the outer block. BlockSemaInfo *PrevBlockInfo; }; - - + +//===--------------------------------------------------------------------===// +// Typed version of Parser::ExprArg (smart pointer for wrapping Expr pointers). +template +class ExprOwningPtr : public Action::ExprArg { +public: + ExprOwningPtr(Sema *S, T *expr) : Action::ExprArg(*S, expr) {}; + + void reset(T* p) { Action::ExprArg::operator=(p); } + T* get() const { return static_cast(Action::ExprArg::get()); } + T* take() { return static_cast(Action::ExprArg::take()); } + T* release() { return take(); } + + T& operator*() const { return *get(); } + T* operator->() const { return get(); } +}; + } // end namespace clang #endif diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 92cad1029b..e52c730442 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -306,9 +306,10 @@ Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { TheCall->setArg(i, 0); } - return Owned(new ShuffleVectorExpr(exprs.begin(), numElements+2, FAType, - TheCall->getCallee()->getLocStart(), - TheCall->getRParenLoc())); + return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), numElements+2, + FAType, + TheCall->getCallee()->getLocStart(), + TheCall->getRParenLoc())); } /// SemaBuiltinPrefetch - Handle __builtin_prefetch. diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index bf16e1aeee..cdb6930536 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -2260,7 +2260,7 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init, bool DirectInit) { // If there is no declaration, there was an error parsing it. Just ignore // the initializer. if (RealDecl == 0) { - delete Init; + Init->Destroy(Context); return; } @@ -2697,8 +2697,10 @@ Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtArg BodyArg) { assert(FD == getCurFunctionDecl() && "Function parsing confused"); } else if (ObjCMethodDecl *MD = dyn_cast_or_null(dcl)) { MD->setBody((Stmt*)Body); - } else + } else { + Body->Destroy(Context); return 0; + } PopDeclContext(); // Verify and clean out per-function state. @@ -2717,11 +2719,17 @@ Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtArg BodyArg) { // the function body so that they aren't leaked and that the AST is well // formed. if (Body) { - L->setSubStmt(new NullStmt(L->getIdentLoc())); - cast(Body)->push_back(L); +#if 0 + // FIXME: Why do this? Having a 'push_back' in CompoundStmt is ugly, + // and the AST is malformed anyway. We should just blow away 'L'. + L->setSubStmt(new (Context) NullStmt(L->getIdentLoc())); + cast(Body)->push_back(L); +#else + L->Destroy(Context); +#endif } else { // The whole function wasn't parsed correctly, just delete this. - delete L; + L->Destroy(Context); } } } @@ -3516,7 +3524,7 @@ Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, else Diag(IdLoc, diag::err_redefinition) << Id; Diag(PrevDecl->getLocation(), diag::note_previous_definition); - delete Val; + Val->Destroy(Context); return 0; } } @@ -3530,7 +3538,7 @@ Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, // C99 6.7.2.2p2: Make sure we have an integer constant expression. SourceLocation ExpLoc; if (VerifyIntegerConstantExpression(Val, &EnumVal)) { - delete Val; + Val->Destroy(Context); Val = 0; // Just forget about it. } else { EltTy = Val->getType(); @@ -3721,8 +3729,8 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, // Adjust the Expr initializer and type. if (ECD->getInitExpr()) - ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr(), - /*isLvalue=*/false)); + ECD->setInitExpr(new (Context) ImplicitCastExpr(NewTy, ECD->getInitExpr(), + /*isLvalue=*/false)); if (getLangOptions().CPlusPlus) // C++ [dcl.enum]p4: Following the closing brace of an // enum-specifier, each enumerator has the type of its @@ -3756,7 +3764,7 @@ void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, !Val.isPowerOf2() || Val.getZExtValue() > 16) { Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); - delete Alignment; + Alignment->Destroy(Context); return; // Ignore } diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 35050e517b..c4c526eedb 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -107,7 +107,7 @@ void Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, ExprTy *defarg) { ParmVarDecl *Param = (ParmVarDecl *)param; - llvm::OwningPtr DefaultArg((Expr *)defarg); + ExprOwningPtr DefaultArg(this, (Expr *)defarg); QualType ParamType = Param->getType(); // Default arguments are only permitted in C++ @@ -1503,7 +1503,7 @@ void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, // the initializer. if (RealDecl == 0) { for (unsigned i = 0; i != NumExprs; ++i) - delete static_cast(ExprTys[i]); + static_cast(ExprTys[i])->Destroy(Context); return; } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 201a0f99e3..8ce8bfdd8b 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -454,8 +454,7 @@ Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, if (BaseObject) { // BaseObject is an anonymous struct/union variable (and is, // therefore, not part of another non-anonymous record). - delete BaseObjectExpr; - + if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), SourceLocation()); ExtraQuals @@ -1770,7 +1769,7 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); // Use default arguments for missing arguments NumArgsToCheck = NumArgsInProto; - Call->setNumArgs(NumArgsInProto); + Call->setNumArgs(Context, NumArgsInProto); } // If too many are passed and not variadic, error on the extras and drop @@ -1783,7 +1782,7 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, << SourceRange(Args[NumArgsInProto]->getLocStart(), Args[NumArgs-1]->getLocEnd()); // This deletes the extra arguments. - Call->setNumArgs(NumArgsInProto); + Call->setNumArgs(Context, NumArgsInProto); Invalid = true; } NumArgsToCheck = NumArgsInProto; @@ -1945,7 +1944,7 @@ 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. - llvm::OwningPtr TheCall(new (Context) CallExpr(Fn, Args, NumArgs, + ExprOwningPtr TheCall(this, new (Context) CallExpr(Fn, Args,NumArgs, Context.BoolTy, RParenLoc)); const FunctionType *FuncT; @@ -4154,7 +4153,7 @@ Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, // Offset of an array sub-field. TODO: Should we allow vector elements? const ArrayType *AT = Context.getAsArrayType(Res->getType()); if (!AT) { - delete Res; + Res->Destroy(Context); return Diag(OC.LocEnd, diag::err_offsetof_array_type) << Res->getType(); } @@ -4173,7 +4172,7 @@ Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, const RecordType *RC = Res->getType()->getAsRecordType(); if (!RC) { - delete Res; + Res->Destroy(Context); return Diag(OC.LocEnd, diag::err_offsetof_record_type) << Res->getType(); } @@ -4336,7 +4335,7 @@ Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, Scope *CurScope) { // Ensure that CurBlock is deleted. llvm::OwningPtr BSI(CurBlock); - llvm::OwningPtr Body(static_cast(body)); + ExprOwningPtr Body(this, static_cast(body)); PopDeclContext(); diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 2a7c9a85f0..748eddc5f7 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -70,8 +70,8 @@ Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); - return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(), - SourceRange(OpLoc, RParenLoc)); + return new (Context) CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(), + SourceRange(OpLoc, RParenLoc)); } /// ActOnCXXBoolLiteral - Parse {true,false} literals. @@ -79,13 +79,13 @@ Action::ExprResult Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { assert((Kind == tok::kw_true || Kind == tok::kw_false) && "Unknown C++ Boolean value!"); - return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); + return new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); } /// ActOnCXXThrow - Parse throw expressions. Action::ExprResult Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { - return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); + return new (Context) CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); } Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { @@ -100,7 +100,7 @@ Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { if (CXXMethodDecl *MD = dyn_cast(CurContext)) if (MD->isInstance()) - return new CXXThisExpr(ThisLoc, MD->getThisType(Context)); + return new (Context) CXXThisExpr(ThisLoc, MD->getThisType(Context)); return Diag(ThisLoc, diag::err_invalid_this_use); } @@ -129,8 +129,8 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, if (NumExprs == 1) { if (CheckCastTypes(TypeRange, Ty, Exprs[0])) return true; - return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, - Exprs[0], RParenLoc); + return new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, + TyBeginLoc, Exprs[0], RParenLoc); } if (const RecordType *RT = Ty->getAsRecordType()) { @@ -148,7 +148,7 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, if (!Constructor) return true; - return new CXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc, + return new (Context) CXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc, Exprs, NumExprs, RParenLoc); } @@ -178,7 +178,7 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, diag::err_invalid_incomplete_type_use, FullRange)) return true; - return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); + return new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); } @@ -312,8 +312,8 @@ Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) - return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs, - ParenTypeId, ArraySize, Constructor, Init, + return new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, + NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, ConsArgs, NumConsArgs, OperatorDelete, ResultType, StartLoc, Init ? ConstructorRParen : SourceLocation()); } @@ -384,7 +384,7 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, bool UseGlobal, // We don't care about the actual value of this argument. // FIXME: Should the Sema create the expression and embed it in the syntax // tree? Or should the consumer just recalculate the value? - AllocArgs[0] = new IntegerLiteral(llvm::APInt::getNullValue( + AllocArgs[0] = new (Context) IntegerLiteral(llvm::APInt::getNullValue( Context.Target.getPointerWidth(0)), Context.getSizeType(), SourceLocation()); @@ -533,7 +533,7 @@ void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, // FIXME: Do we need to check for default arguments here? FunctionDecl *Func = cast(*Alloc); if (Func->getNumParams() == 1 && - Context.getCanonicalType(Func->getParamDecl(0)->getType()) == Argument) + Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) return; } } @@ -594,8 +594,8 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, // along. // FIXME: Check access and ambiguity of operator delete and destructor. - return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex, - StartLoc); + return new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, + Ex, StartLoc); } @@ -648,7 +648,7 @@ Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, if (VarDecl *VD = dyn_cast((Decl *)Dcl)) VD->setDeclaredInCondition(true); - return new CXXConditionDeclExpr(StartLoc, EqualLoc, + return new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, cast(static_cast(Dcl))); } @@ -879,7 +879,7 @@ Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, // There is no point in eagerly computing the value. The traits are designed // to be used from type trait templates, so Ty will be a template parameter // 99% of the time. - return Owned(new UnaryTypeTraitExpr(KWLoc, OTT, + return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, QualType::getFromOpaquePtr(Ty), RParen, Context.BoolTy)); } diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 003e121f37..32ff7ff163 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -38,7 +38,7 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, isWide = true; memcpy(p, S->getStrData(), S->getByteLength()); p += S->getByteLength(); - delete S; + S->Destroy(Context); } S = new (Context, 8) StringLiteral(Context, strBuf, Length, isWide, Context.getPointerType(Context.CharTy), @@ -65,7 +65,7 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, } else { t = Context.getPointerType(t); } - return new ObjCStringLiteral(S, t, AtLoc); + return new (Context) ObjCStringLiteral(S, t, AtLoc); } Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, @@ -76,7 +76,7 @@ Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, QualType EncodedType = QualType::getFromOpaquePtr(Ty); QualType t = Context.getPointerType(Context.CharTy); - return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); + return new (Context) ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); } Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, @@ -85,7 +85,7 @@ Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, SourceLocation LParenLoc, SourceLocation RParenLoc) { QualType t = Context.getObjCSelType(); - return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); + return new (Context) ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); } Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, @@ -103,7 +103,7 @@ Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, if (t.isNull()) return true; t = Context.getPointerType(t); - return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); + return new (Context) ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); } bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, @@ -199,7 +199,8 @@ Sema::ExprResult Sema::ActOnClassMessage( if (getCurMethodDecl()->isInstanceMethod()) { QualType superTy = Context.getObjCInterfaceType(ClassDecl); superTy = Context.getPointerType(superTy); - ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy); + ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(), + superTy); // We are really in an instance method, redirect. return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac, Args, NumArgs); @@ -212,8 +213,8 @@ Sema::ExprResult Sema::ActOnClassMessage( NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName); ValueDecl *VD = dyn_cast_or_null(SuperDecl); if (VD) { - ExprResult ReceiverExpr = new DeclRefExpr(VD, VD->getType(), - receiverLoc); + ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(), + receiverLoc); // We are really in an instance method, redirect. return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac, Args, NumArgs); @@ -277,11 +278,11 @@ Sema::ExprResult Sema::ActOnClassMessage( // For now, we simply pass the "super" identifier through (which isn't // consistent with instance methods. if (isSuper) - return new ObjCMessageExpr(receiverName, Sel, returnType, Method, - lbrac, rbrac, ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method, + lbrac, rbrac, ArgExprs, NumArgs); else - return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method, - lbrac, rbrac, ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method, + lbrac, rbrac, ArgExprs, NumArgs); } // ActOnInstanceMessage - used for both unary and keyword messages. @@ -312,8 +313,8 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, lbrac, rbrac, returnType)) return true; - return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, - ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, + rbrac, ArgExprs, NumArgs); } // Handle messages to id. @@ -326,8 +327,8 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, lbrac, rbrac, returnType)) return true; - return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, - ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, + rbrac, ArgExprs, NumArgs); } // Handle messages to Class. @@ -348,8 +349,8 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, lbrac, rbrac, returnType)) return true; - return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, - ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, + rbrac, ArgExprs, NumArgs); } ObjCMethodDecl *Method = 0; @@ -411,8 +412,8 @@ Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, lbrac, rbrac, returnType)) return true; - return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, - ArgExprs, NumArgs); + return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, + rbrac, ArgExprs, NumArgs); } //===----------------------------------------------------------------------===// diff --git a/lib/Sema/SemaNamedCast.cpp b/lib/Sema/SemaNamedCast.cpp index e2137b3001..c5bee98a37 100644 --- a/lib/Sema/SemaNamedCast.cpp +++ b/lib/Sema/SemaNamedCast.cpp @@ -76,26 +76,26 @@ Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, case tok::kw_const_cast: if (!TypeDependent) CheckConstCast(*this, Ex, DestType, OpRange, DestRange); - return new CXXConstCastExpr(DestType.getNonReferenceType(), Ex, - DestType, OpLoc); + return new (Context) CXXConstCastExpr(DestType.getNonReferenceType(), Ex, + DestType, OpLoc); case tok::kw_dynamic_cast: if (!TypeDependent) CheckDynamicCast(*this, Ex, DestType, OpRange, DestRange); - return new CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex, - DestType, OpLoc); + return new (Context)CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex, + DestType, OpLoc); case tok::kw_reinterpret_cast: if (!TypeDependent) CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange); - return new CXXReinterpretCastExpr(DestType.getNonReferenceType(), Ex, - DestType, OpLoc); + return new (Context) CXXReinterpretCastExpr(DestType.getNonReferenceType(), + Ex, DestType, OpLoc); case tok::kw_static_cast: if (!TypeDependent) CheckStaticCast(*this, Ex, DestType, OpRange); - return new CXXStaticCastExpr(DestType.getNonReferenceType(), Ex, - DestType, OpLoc); + return new (Context) CXXStaticCastExpr(DestType.getNonReferenceType(), Ex, + DestType, OpLoc); } return true; diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index cb659002b3..69bbf0e05a 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -3600,9 +3600,9 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, // 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(MemExpr->getMemberDecl())) { @@ -3647,8 +3647,8 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, } assert(Method && "Member call to something that isn't a method?"); - llvm::OwningPtr - TheCall(new CXXMemberCallExpr(MemExpr, Args, NumArgs, + ExprOwningPtr + TheCall(this, new (Context) CXXMemberCallExpr(MemExpr, Args, NumArgs, Method->getResultType().getNonReferenceType(), RParenLoc)); @@ -3759,9 +3759,9 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, 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 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, 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 - TheCall(new CXXOperatorCallExpr(NewFn, MethodArgs, NumArgs + 1, - ResultTy, RParenLoc)); + ExprOwningPtr + 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 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, 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 @@ Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, AddMethodCandidate(cast(*Oper), Base, 0, 0, CandidateSet, /*SuppressUserConversions=*/false); - llvm::OwningPtr BasePtr(Base); + ExprOwningPtr BasePtr(this, Base); // Perform overload resolution. OverloadCandidateSet::iterator Best; @@ -3924,9 +3925,10 @@ Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, 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, diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 7697dbda00..18d9ad6f8b 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -16,6 +16,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/Expr.h" +#include "clang/AST/ASTContext.h" #include "clang/Basic/TargetInfo.h" using namespace clang; @@ -33,7 +34,7 @@ Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) { Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { - return Owned(new NullStmt(SemiLoc)); + return Owned(new (Context) NullStmt(SemiLoc)); } Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl, @@ -58,11 +59,11 @@ Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl, if (decls.size() == 1) { DeclGroupOwningRef DG(*decls.begin()); - return Owned(new DeclStmt(DG, StartLoc, EndLoc)); + return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); } else { DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0])); - return Owned(new DeclStmt(DG, StartLoc, EndLoc)); + return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); } } @@ -115,7 +116,7 @@ Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange(); } - return Owned(new CompoundStmt(Elts, NumElts, L, R)); + return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); } Action::OwningStmtResult @@ -148,7 +149,7 @@ Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval, // Only now release the smart pointers. lhsval.release(); rhsval.release(); - CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); + CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); SwitchStack.back()->addSwitchCase(CS); return Owned(CS); } @@ -163,7 +164,7 @@ Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, return Owned(SubStmt); } - DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); + DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt); SwitchStack.back()->addSwitchCase(DS); return Owned(DS); } @@ -177,7 +178,7 @@ Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, // If not forward referenced or defined already, just create a new LabelStmt. if (LabelDecl == 0) - return Owned(LabelDecl = new LabelStmt(IdentLoc, II, SubStmt)); + return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt)); assert(LabelDecl->getID() == II && "Label mismatch!"); @@ -228,7 +229,8 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal, } CondVal.release(); - return Owned(new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal.release())); + return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt, + (Stmt*)ElseVal.release())); } Action::OwningStmtResult @@ -260,7 +262,7 @@ Sema::ActOnStartOfSwitchStmt(ExprArg cond) { UsualUnaryConversions(Cond); } - SwitchStmt *SS = new SwitchStmt(Cond); + SwitchStmt *SS = new (Context) SwitchStmt(Cond); SwitchStack.push_back(SS); return Owned(SS); } @@ -549,7 +551,8 @@ Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) { << condType << condExpr->getSourceRange()); Cond.release(); - return Owned(new WhileStmt(condExpr, (Stmt*)Body.release(), WhileLoc)); + return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(), + WhileLoc)); } Action::OwningStmtResult @@ -570,7 +573,7 @@ Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, << condType << condExpr->getSourceRange()); Cond.release(); - return Owned(new DoStmt((Stmt*)Body.release(), condExpr, DoLoc)); + return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc)); } Action::OwningStmtResult @@ -614,7 +617,7 @@ Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, second.release(); third.release(); body.release(); - return Owned(new ForStmt(First, Second, Third, Body, ForLoc)); + return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc)); } Action::OwningStmtResult @@ -665,8 +668,8 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, first.release(); second.release(); body.release(); - return Owned(new ObjCForCollectionStmt(First, Second, Body, - ForLoc, RParenLoc)); + return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, + ForLoc, RParenLoc)); } Action::OwningStmtResult @@ -681,9 +684,9 @@ Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, // If we haven't seen this label yet, create a forward reference. if (LabelDecl == 0) - LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); + LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0); - return Owned(new GotoStmt(LabelDecl, GotoLoc, LabelLoc)); + return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc)); } Action::OwningStmtResult @@ -691,7 +694,7 @@ Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, ExprArg DestExp) { // FIXME: Verify that the operand is convertible to void*. - return Owned(new IndirectGotoStmt((Expr*)DestExp.release())); + return Owned(new (Context) IndirectGotoStmt((Expr*)DestExp.release())); } Action::OwningStmtResult @@ -702,7 +705,7 @@ Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); } - return Owned(new ContinueStmt(ContinueLoc)); + return Owned(new (Context) ContinueStmt(ContinueLoc)); } Action::OwningStmtResult @@ -713,7 +716,7 @@ Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); } - return Owned(new BreakStmt(BreakLoc)); + return Owned(new (Context) BreakStmt(BreakLoc)); } /// ActOnBlockReturnStmt - Utility routine to figure out block's return type. @@ -740,10 +743,10 @@ Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { if (CurBlock->ReturnType->isVoidType()) { if (RetValExp) { Diag(ReturnLoc, diag::err_return_block_has_expr); - delete RetValExp; + RetValExp->Destroy(Context); RetValExp = 0; } - return Owned(new ReturnStmt(ReturnLoc, RetValExp)); + return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); } if (!RetValExp) @@ -766,7 +769,7 @@ Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); } - return Owned(new ReturnStmt(ReturnLoc, RetValExp)); + return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); } Action::OwningStmtResult @@ -796,7 +799,7 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { << RetValExp->getSourceRange(); } } - return Owned(new ReturnStmt(ReturnLoc, RetValExp)); + return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); } if (!RetValExp) { @@ -808,7 +811,7 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; else Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; - return Owned(new ReturnStmt(ReturnLoc, (Expr*)0)); + return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0)); } if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { @@ -828,7 +831,7 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); } - return Owned(new ReturnStmt(ReturnLoc, RetValExp)); + return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); } Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, @@ -945,9 +948,10 @@ Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, exprs.release(); asmString.release(); clobbers.release(); - return Owned(new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, - Names, Constraints, Exprs, AsmString, NumClobbers, - Clobbers, RParenLoc)); + return Owned(new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, + NumInputs, Names, Constraints, Exprs, + AsmString, NumClobbers, + Clobbers, RParenLoc)); } Action::OwningStmtResult @@ -955,7 +959,7 @@ Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, SourceLocation RParen, StmtArg Parm, StmtArg Body, StmtArg catchList) { Stmt *CatchList = static_cast(catchList.release()); - ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen, + ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen, static_cast(Parm.release()), static_cast(Body.release()), CatchList); return Owned(CatchList ? CatchList : CS); @@ -963,27 +967,29 @@ Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, Action::OwningStmtResult Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) { - return Owned(new ObjCAtFinallyStmt(AtLoc, - static_cast(Body.release()))); + return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, + static_cast(Body.release()))); } Action::OwningStmtResult Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, StmtArg Try, StmtArg Catch, StmtArg Finally) { - return Owned(new ObjCAtTryStmt(AtLoc, static_cast(Try.release()), + return Owned(new (Context) ObjCAtTryStmt(AtLoc, + static_cast(Try.release()), static_cast(Catch.release()), static_cast(Finally.release()))); } Action::OwningStmtResult Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw) { - return Owned(new ObjCAtThrowStmt(AtLoc, static_cast(Throw.release()))); + return Owned(new (Context) ObjCAtThrowStmt(AtLoc, + static_cast(Throw.release()))); } Action::OwningStmtResult Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr, StmtArg SynchBody) { - return Owned(new ObjCAtSynchronizedStmt(AtLoc, + return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, static_cast(SynchExpr.release()), static_cast(SynchBody.release()))); } @@ -994,8 +1000,9 @@ Action::OwningStmtResult Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl, StmtArg HandlerBlock) { // There's nothing to test that ActOnExceptionDecl didn't already test. - return Owned(new CXXCatchStmt(CatchLoc, static_cast(ExDecl), - static_cast(HandlerBlock.release()))); + return Owned(new (Context) CXXCatchStmt(CatchLoc, + static_cast(ExDecl), + static_cast(HandlerBlock.release()))); } /// ActOnCXXTryBlock - Takes a try compound-statement and a number of @@ -1022,6 +1029,7 @@ Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock, // and warns. RawHandlers.release(); - return Owned(new CXXTryStmt(TryLoc, static_cast(TryBlock.release()), - Handlers, NumHandlers)); + return Owned(new (Context) CXXTryStmt(TryLoc, + static_cast(TryBlock.release()), + Handlers, NumHandlers)); } diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index e6d18338a4..e206c40c3b 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -424,7 +424,10 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) { Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) << ArraySize->getType() << ArraySize->getSourceRange(); D.setInvalidType(true); - delete ArraySize; + + Context.Deallocate(ArraySize); + ArraySize->~Expr(); + ATI.NumElts = ArraySize = 0; } llvm::APSInt ConstVal(32);