Introduce reference counting for statements and expressions, using it
to allow sharing of nodes. Simplifies some aspects of template
instantiation, and fixes both PR3444 and <rdar://problem/6757457>.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78450 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp
index 668f7ef..a0ef5a6 100644
--- a/lib/AST/Stmt.cpp
+++ b/lib/AST/Stmt.cpp
@@ -43,7 +43,7 @@
 }
 
 const char *Stmt::getStmtClassName() const {
-  return getStmtInfoTableEntry(sClass).Name;
+  return getStmtInfoTableEntry((StmtClass)sClass).Name;
 }
 
 void Stmt::DestroyChildren(ASTContext &C) {
@@ -104,6 +104,20 @@
   return new (C) BreakStmt(BreakLoc);
 }
 
+void SwitchStmt::DoDestroy(ASTContext &Ctx) {
+  // Destroy the SwitchCase statements in this switch. In the normal
+  // case, this loop will merely decrement the reference counts from 
+  // the Retain() calls in addSwitchCase();
+  SwitchCase *SC = FirstCase;
+  while (SC) {
+    SwitchCase *Next = SC->getNextSwitchCase();
+    SC->Destroy(Ctx);
+    SC = Next;
+  }
+  
+  Stmt::DoDestroy(Ctx);
+}
+
 void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
   if (this->Body)
     C.Deallocate(Body);