Combine ClassTemplateDecl's PreviousDeclaration with CommonPtr, as in FunctionTemplateDecl.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@106412 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp
index 1fcf923..a318de6 100644
--- a/lib/AST/DeclTemplate.cpp
+++ b/lib/AST/DeclTemplate.cpp
@@ -155,21 +155,6 @@
   return Template;
 }
 
-void ClassTemplateDecl::initPreviousDeclaration(ASTContext &C,
-                                                ClassTemplateDecl *PrevDecl) {
-  assert(PreviousDeclaration == 0 && "PreviousDeclaration already set!");
-  assert(CommonPtr == 0 && "initPreviousDeclaration already called!");
-
-  PreviousDeclaration = PrevDecl;
-
-  if (PrevDecl)
-    CommonPtr = PrevDecl->CommonPtr;
-  else {
-    CommonPtr = new (C) Common;
-    C.AddDeallocation(DeallocateCommon, CommonPtr);
-  }
-}
-
 ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
                                              DeclContext *DC,
                                              SourceLocation L,
@@ -178,29 +163,18 @@
                                              NamedDecl *Decl,
                                              ClassTemplateDecl *PrevDecl) {
   ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
-  New->initPreviousDeclaration(C, PrevDecl);
+  New->setPreviousDeclaration(PrevDecl);
   return New;
 }
 
-ClassTemplateDecl::~ClassTemplateDecl() {
-  assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed");
-}
-
 void ClassTemplateDecl::Destroy(ASTContext& C) {
-  if (!PreviousDeclaration) {
-    CommonPtr->~Common();
-    C.Deallocate((void*)CommonPtr);
-  }
-  CommonPtr = 0;
-
-  this->~ClassTemplateDecl();
-  C.Deallocate((void*)this);
+  Decl::Destroy(C);
 }
 
 void ClassTemplateDecl::getPartialSpecializations(
           llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
   llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
-    = CommonPtr->PartialSpecializations;
+    = getPartialSpecializations();
   PS.clear();
   PS.resize(PartialSpecs.size());
   for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
@@ -228,6 +202,7 @@
 
 QualType
 ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) {
+  Common *CommonPtr = getCommonPtr();
   if (!CommonPtr->InjectedClassNameType.isNull())
     return CommonPtr->InjectedClassNameType;
 
@@ -264,6 +239,20 @@
   return CommonPtr->InjectedClassNameType;
 }
 
+ClassTemplateDecl::Common *ClassTemplateDecl::getCommonPtr() {
+  // Find the first declaration of this function template.
+  ClassTemplateDecl *First = this;
+  while (First->getPreviousDeclaration())
+    First = First->getPreviousDeclaration();
+
+  if (First->CommonOrPrev.isNull()) {
+    Common *CommonPtr = new (getASTContext()) Common;
+    getASTContext().AddDeallocation(DeallocateCommon, CommonPtr);
+    First->CommonOrPrev = CommonPtr;
+  }
+  return First->CommonOrPrev.get<Common*>();
+}
+
 //===----------------------------------------------------------------------===//
 // TemplateTypeParm Allocation/Deallocation Method Implementations
 //===----------------------------------------------------------------------===//