Overhaul of Stmt allocation:
- Made allocation of Stmt objects using vanilla new/delete a *compiler
  error* by making this new/delete "protected" within class Stmt.
- Now the only way to allocate Stmt objects is by using the new
  operator that takes ASTContext& as an argument.  This ensures that
  all Stmt nodes are allocated from the same (pool) allocator.
- Naturally, these two changes required that *all* creation sites for
  AST nodes use new (ASTContext&).  This is a large patch, but the
  majority of the changes are just this mechanical adjustment.
- The above changes also mean that AST nodes can no longer be
  deallocated using 'delete'.  Instead, one most do
  StmtObject->Destroy(ASTContext&) or do
  ASTContextObject.Deallocate(StmtObject) (the latter not running the
  'Destroy' method).

Along the way I also...
- Made CompoundStmt allocate its array of Stmt* using the allocator in
  ASTContext (previously it used std::vector).  There are a whole
  bunch of other Stmt classes that need to be similarly changed to
  ensure that all memory allocated for ASTs comes from the allocator
  in ASTContext.
- Added a new smart pointer ExprOwningPtr to Sema.h.  This replaces
  the uses of llvm::OwningPtr within Sema, as llvm::OwningPtr used
  'delete' to free memory instead of a Stmt's 'Destroy' method.

Big thanks to Doug Gregor for helping with the acrobatics of making
'new/delete' private and the new smart pointer ExprOwningPtr!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63997 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 7697dbd..18d9ad6 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::ActOnNullStmt(SourceLocation SemiLoc) {
-  return Owned(new NullStmt(SemiLoc));
+  return Owned(new (Context) NullStmt(SemiLoc));
 }
 
 Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
@@ -58,11 +59,11 @@
 
   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 @@
       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 @@
   // 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 @@
     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 @@
 
   // 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 @@
   }
 
   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 @@
     UsualUnaryConversions(Cond);
   }
 
-  SwitchStmt *SS = new SwitchStmt(Cond);
+  SwitchStmt *SS = new (Context) SwitchStmt(Cond);
   SwitchStack.push_back(SS);
   return Owned(SS);
 }
@@ -549,7 +551,8 @@
       << 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 @@
       << 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 @@
   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 @@
   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 @@
 
   // 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 @@
                             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 @@
     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 @@
     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 @@
   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 @@
     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 @@
           << RetValExp->getSourceRange();
       }
     }
-    return Owned(new ReturnStmt(ReturnLoc, RetValExp));
+    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
   }
 
   if (!RetValExp) {
@@ -808,7 +811,7 @@
       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 @@
     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 @@
   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 @@
                            SourceLocation RParen, StmtArg Parm,
                            StmtArg Body, StmtArg catchList) {
   Stmt *CatchList = static_cast<Stmt*>(catchList.release());
-  ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
+  ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
     static_cast<Stmt*>(Parm.release()), static_cast<Stmt*>(Body.release()),
     CatchList);
   return Owned(CatchList ? CatchList : CS);
@@ -963,27 +967,29 @@
 
 Action::OwningStmtResult
 Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
-  return Owned(new ObjCAtFinallyStmt(AtLoc,
-                                     static_cast<Stmt*>(Body.release())));
+  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
+                                           static_cast<Stmt*>(Body.release())));
 }
 
 Action::OwningStmtResult
 Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
                          StmtArg Try, StmtArg Catch, StmtArg Finally) {
-  return Owned(new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try.release()),
+  return Owned(new (Context) ObjCAtTryStmt(AtLoc,
+                                        static_cast<Stmt*>(Try.release()),
                                         static_cast<Stmt*>(Catch.release()),
                                         static_cast<Stmt*>(Finally.release())));
 }
 
 Action::OwningStmtResult
 Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw) {
-  return Owned(new ObjCAtThrowStmt(AtLoc, static_cast<Expr*>(Throw.release())));
+  return Owned(new (Context) ObjCAtThrowStmt(AtLoc,
+                                          static_cast<Expr*>(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<Stmt*>(SynchExpr.release()),
                      static_cast<Stmt*>(SynchBody.release())));
 }
@@ -994,8 +1000,9 @@
 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<VarDecl*>(ExDecl),
-                                static_cast<Stmt*>(HandlerBlock.release())));
+  return Owned(new (Context) CXXCatchStmt(CatchLoc,
+                                  static_cast<VarDecl*>(ExDecl),
+                                  static_cast<Stmt*>(HandlerBlock.release())));
 }
 
 /// ActOnCXXTryBlock - Takes a try compound-statement and a number of
@@ -1022,6 +1029,7 @@
   // and warns.
 
   RawHandlers.release();
-  return Owned(new CXXTryStmt(TryLoc, static_cast<Stmt*>(TryBlock.release()),
-                              Handlers, NumHandlers));
+  return Owned(new (Context) CXXTryStmt(TryLoc,
+                                        static_cast<Stmt*>(TryBlock.release()),
+                                        Handlers, NumHandlers));
 }