Improve the representation of template names in the AST. This
representation handles the various ways in which one can name a
template, including unqualified references ("vector"), qualified
references ("std::vector"), and dependent template names
("MetaFun::template apply").

One immediate effect of this change is that the representation of
nested-name-specifiers in type names for class template
specializations (e.g., std::vector<int>) is more accurate. Rather than
representing std::vector<int> as

  std::(vector<int>)

we represent it as

  (std::vector)<int>

which more closely follows the C++ grammar. 

Additionally, templates are no longer represented as declarations
(DeclPtrTy) in Parse-Sema interactions. Instead, I've introduced a new
OpaquePtr type (TemplateTy) that holds the representation of a
TemplateName. This will simplify the handling of dependent
template-names, once we get there.






git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68074 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/TemplateName.cpp b/lib/AST/TemplateName.cpp
new file mode 100644
index 0000000..4e54fe4
--- /dev/null
+++ b/lib/AST/TemplateName.cpp
@@ -0,0 +1,54 @@
+//===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the TemplateName interface and subclasses.
+//
+//===----------------------------------------------------------------------===//
+#include "clang/AST/TemplateName.h"
+#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/NestedNameSpecifier.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+
+TemplateDecl *TemplateName::getAsTemplateDecl() const {
+  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
+    return Template;
+  
+  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
+    return QTN->getTemplateDecl();
+
+  return 0;
+}
+
+bool TemplateName::isDependent() const {
+  if (TemplateDecl *Template = getAsTemplateDecl()) {
+    // FIXME: We don't yet have a notion of dependent
+    // declarations. When we do, check that. This hack won't last
+    // long!.
+    return isa<TemplateTemplateParmDecl>(Template);
+  }
+
+  return true;
+}
+
+void TemplateName::Print(llvm::raw_ostream &OS) const {
+  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
+    OS << Template->getIdentifier()->getName();
+  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
+    QTN->getQualifier()->Print(OS);
+    if (QTN->hasTemplateKeyword())
+      OS << "template ";
+    OS << QTN->getTemplateDecl()->getIdentifier()->getName();
+  } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
+    DTN->getQualifier()->Print(OS);
+    OS << "template ";
+    OS << DTN->getName()->getName();
+  }
+}