Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as

  Fibonacci<N - 1>::value

where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.

Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.

We can now process a simple Fibonacci computation described as a
template metaprogram.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp
index ac76c25..7389b83 100644
--- a/lib/AST/DeclTemplate.cpp
+++ b/lib/AST/DeclTemplate.cpp
@@ -93,8 +93,31 @@
                                              SourceLocation L,
                                              DeclarationName Name,
                                              TemplateParameterList *Params,
-                                             NamedDecl *Decl) {
-  return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
+                                             NamedDecl *Decl,
+                                             ClassTemplateDecl *PrevDecl) {
+  Common *CommonPtr;
+  if (PrevDecl)
+    CommonPtr = PrevDecl->CommonPtr;
+  else
+    CommonPtr = new (C) Common;
+
+  return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl, 
+                                   CommonPtr);
+}
+
+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);
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp
index f18a288..53be84b 100644
--- a/lib/AST/ExprCXX.cpp
+++ b/lib/AST/ExprCXX.cpp
@@ -49,6 +49,29 @@
                                         NumComponents);
 }
 
+UnresolvedDeclRefExpr::UnresolvedDeclRefExpr(DeclarationName N, QualType T,
+                                             SourceLocation L, SourceRange R,
+                                       const NestedNameSpecifier *Components,
+                                             unsigned NumComponents)
+  : Expr(UnresolvedDeclRefExprClass, T, true, true), 
+    Name(N), Loc(L), QualifierRange(R), NumComponents(NumComponents) {
+  NestedNameSpecifier *Data 
+    = reinterpret_cast<NestedNameSpecifier *>(this + 1);
+  for (unsigned I = 0; I < NumComponents; ++I)
+    Data[I] = Components[I];
+}
+
+UnresolvedDeclRefExpr *
+UnresolvedDeclRefExpr::Create(ASTContext &Context, DeclarationName N,
+                              SourceLocation L, SourceRange R,
+                              const NestedNameSpecifier *Components,
+                              unsigned NumComponents) {
+  void *Mem = Context.Allocate((sizeof(UnresolvedDeclRefExpr) +
+                                sizeof(NestedNameSpecifier) * NumComponents));
+  return new (Mem) UnresolvedDeclRefExpr(N, Context.DependentTy, L, R, 
+                                         Components, NumComponents);
+}
+
 //===----------------------------------------------------------------------===//
 //  Child Iterators for iterating over subexpressions/substatements
 //===----------------------------------------------------------------------===//
@@ -164,6 +187,15 @@
   return child_iterator();
 }
 
+// UnresolvedDeclRefExpr
+StmtIterator UnresolvedDeclRefExpr::child_begin() {
+  return child_iterator();
+}
+
+StmtIterator UnresolvedDeclRefExpr::child_end() {
+  return child_iterator();
+}
+
 bool UnaryTypeTraitExpr::EvaluateTrait() const {
   switch(UTT) {
   default: assert(false && "Unknown type trait or not implemented");
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 3a6a01c..60300ba 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -537,6 +537,11 @@
   OS << D->getNameAsString();
 }
 
+void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {  
+  NestedNameSpecifier::Print(OS, Node->begin(), Node->end());
+  OS << Node->getDeclName().getAsString();
+}
+
 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
   if (Node->getBase()) {
     PrintExpr(Node->getBase());