blob: 14722f70398c5f39c879cde176e9f46bb38c535b [file] [log] [blame]
Douglas Gregor7532dc62009-03-30 22:58:21 +00001//===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the TemplateName interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerc960ee32009-04-02 06:07:12 +000013
Douglas Gregor7532dc62009-03-30 22:58:21 +000014#include "clang/AST/TemplateName.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000017#include "clang/AST/PrettyPrinter.h"
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnere4f21422009-06-30 01:26:17 +000019#include "clang/Basic/LangOptions.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000020#include "llvm/Support/raw_ostream.h"
Douglas Gregor7532dc62009-03-30 22:58:21 +000021using namespace clang;
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000022using namespace llvm;
Douglas Gregor7532dc62009-03-30 22:58:21 +000023
24TemplateDecl *TemplateName::getAsTemplateDecl() const {
25 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
26 return Template;
Mike Stump1eb44332009-09-09 15:08:12 +000027
Douglas Gregor7532dc62009-03-30 22:58:21 +000028 if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
29 return QTN->getTemplateDecl();
30
31 return 0;
32}
33
34bool TemplateName::isDependent() const {
35 if (TemplateDecl *Template = getAsTemplateDecl()) {
Mike Stump1eb44332009-09-09 15:08:12 +000036 return isa<TemplateTemplateParmDecl>(Template) ||
Douglas Gregord99cbe62009-07-29 18:26:50 +000037 Template->getDeclContext()->isDependentContext();
Douglas Gregor7532dc62009-03-30 22:58:21 +000038 }
39
John McCall0bd6feb2009-12-02 08:04:21 +000040 assert(!getAsOverloadedTemplate() &&
41 "overloaded templates shouldn't survive to here");
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregor7532dc62009-03-30 22:58:21 +000043 return true;
44}
45
Mike Stump1eb44332009-09-09 15:08:12 +000046void
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000047TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
48 bool SuppressNNS) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +000049 if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
Benjamin Kramer900fc632010-04-17 09:33:03 +000050 OS << Template;
Douglas Gregor7532dc62009-03-30 22:58:21 +000051 else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
Douglas Gregor17343172009-04-01 00:28:59 +000052 if (!SuppressNNS)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000053 QTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000054 if (QTN->hasTemplateKeyword())
55 OS << "template ";
Benjamin Kramer900fc632010-04-17 09:33:03 +000056 OS << QTN->getDecl();
Douglas Gregor7532dc62009-03-30 22:58:21 +000057 } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000058 if (!SuppressNNS && DTN->getQualifier())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000059 DTN->getQualifier()->print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +000060 OS << "template ";
Douglas Gregorca1bdd72009-11-04 00:56:37 +000061
62 if (DTN->isIdentifier())
63 OS << DTN->getIdentifier()->getName();
64 else
65 OS << "operator " << getOperatorSpelling(DTN->getOperator());
Douglas Gregor7532dc62009-03-30 22:58:21 +000066 }
67}
Douglas Gregorde650ae2009-03-31 18:38:02 +000068
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +000069const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
70 TemplateName N) {
71 std::string NameStr;
72 raw_string_ostream OS(NameStr);
73 LangOptions LO;
74 LO.CPlusPlus = true;
75 LO.Bool = true;
76 N.print(OS, PrintingPolicy(LO));
77 OS.flush();
78 return DB << NameStr;
79}
80
Douglas Gregor9bde7732009-03-31 20:22:05 +000081void TemplateName::dump() const {
Chris Lattnere4f21422009-06-30 01:26:17 +000082 LangOptions LO; // FIXME!
83 LO.CPlusPlus = true;
84 LO.Bool = true;
85 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregorde650ae2009-03-31 18:38:02 +000086}