Parsing, semantic analysis, and template instantiation for typename
specifiers that terminate in a simple-template-id, e.g.,

  typename MetaFun::template apply<T1, T2>

Also, implement template instantiation for dependent
nested-name-specifiers that involve unresolved identifiers, e.g.,

  typename T::type::type





git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68166 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 422988d..cede0e5 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -1454,6 +1454,39 @@
   return QualType(T, 0);  
 }
 
+QualType 
+ASTContext::getTypenameType(NestedNameSpecifier *NNS, 
+                            const TemplateSpecializationType *TemplateId,
+                            QualType Canon) {
+  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
+
+  if (Canon.isNull()) {
+    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
+    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
+    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
+      const TemplateSpecializationType *CanonTemplateId
+        = CanonType->getAsTemplateSpecializationType();
+      assert(CanonTemplateId &&
+             "Canonical type must also be a template specialization type");
+      Canon = getTypenameType(CanonNNS, CanonTemplateId);
+    }
+  }
+
+  llvm::FoldingSetNodeID ID;
+  TypenameType::Profile(ID, NNS, TemplateId);
+
+  void *InsertPos = 0;
+  TypenameType *T 
+    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
+  if (T)
+    return QualType(T, 0);
+
+  T = new (*this) TypenameType(NNS, TemplateId, Canon);
+  Types.push_back(T);
+  TypenameTypes.InsertNode(T, InsertPos);
+  return QualType(T, 0);    
+}
+
 /// CmpProtocolNames - Comparison predicate for sorting protocols
 /// alphabetically.
 static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
diff --git a/lib/AST/NestedNameSpecifier.cpp b/lib/AST/NestedNameSpecifier.cpp
index 2db8c76..c94a4da 100644
--- a/lib/AST/NestedNameSpecifier.cpp
+++ b/lib/AST/NestedNameSpecifier.cpp
@@ -30,7 +30,7 @@
   NestedNameSpecifier *NNS 
     = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
   if (!NNS) {
-    NNS = new (Context) NestedNameSpecifier(Mockup);
+    NNS = new (Context, 4) NestedNameSpecifier(Mockup);
     Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
   }
 
@@ -44,9 +44,9 @@
   assert(Prefix && Prefix->isDependent() && "Prefix must be dependent");
 
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(II);
-  Mockup.Specifier.setInt(Identifier);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Identifier);
+  Mockup.Specifier = II;
   return FindOrInsert(Context, Mockup);
 }
 
@@ -58,9 +58,9 @@
           (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
          "Broken nested name specifier");
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(NS);
-  Mockup.Specifier.setInt(Namespace);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Namespace);
+  Mockup.Specifier = NS;
   return FindOrInsert(Context, Mockup);
 }
 
@@ -69,15 +69,15 @@
                             bool Template, Type *T) {
   assert(T && "Type cannot be NULL");
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(T);
-  Mockup.Specifier.setInt(Template? TypeSpecWithTemplate : TypeSpec);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
+  Mockup.Specifier = T;
   return FindOrInsert(Context, Mockup);
 }
   
 NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
   if (!Context.GlobalNestedNameSpecifier)
-    Context.GlobalNestedNameSpecifier = new (Context) NestedNameSpecifier();
+    Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
   return Context.GlobalNestedNameSpecifier;
 }
 
@@ -105,8 +105,8 @@
 /// \brief Print this nested name specifier to the given output
 /// stream.
 void NestedNameSpecifier::print(llvm::raw_ostream &OS) const {
-  if (Prefix)
-    Prefix->print(OS);
+  if (getPrefix())
+    getPrefix()->print(OS);
 
   switch (getKind()) {
   case Identifier:
diff --git a/lib/AST/TemplateName.cpp b/lib/AST/TemplateName.cpp
index 659796d..16b96a0 100644
--- a/lib/AST/TemplateName.cpp
+++ b/lib/AST/TemplateName.cpp
@@ -38,16 +38,18 @@
   return true;
 }
 
-void TemplateName::print(llvm::raw_ostream &OS) const {
+void TemplateName::print(llvm::raw_ostream &OS, bool SuppressNNS) const {
   if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
     OS << Template->getIdentifier()->getName();
   else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
-    QTN->getQualifier()->print(OS);
+    if (!SuppressNNS)
+      QTN->getQualifier()->print(OS);
     if (QTN->hasTemplateKeyword())
       OS << "template ";
     OS << QTN->getTemplateDecl()->getIdentifier()->getName();
   } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
-    DTN->getQualifier()->print(OS);
+    if (!SuppressNNS)
+      DTN->getQualifier()->print(OS);
     OS << "template ";
     OS << DTN->getName()->getName();
   }
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index 669eb7c..b9bd0ba 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -1450,7 +1450,14 @@
     llvm::raw_string_ostream OS(MyString);
     OS << "typename ";
     NNS->print(OS);
-    OS << Name->getName();
+
+    if (const IdentifierInfo *Ident = getIdentifier())
+      OS << Ident->getName();
+    else if (const TemplateSpecializationType *Spec = getTemplateId()) {
+      Spec->getTemplateName().print(OS, true);
+      OS << TemplateSpecializationType::PrintTemplateArgumentList(
+                                         Spec->getArgs(), Spec->getNumArgs());
+    }
   }
   
   if (InnerString.empty())