Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | c960ee3 | 2009-04-02 06:07:12 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 14 | #include "clang/AST/TemplateName.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/NestedNameSpecifier.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyPrinter.h" |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 21 | using namespace clang; |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 23 | |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 24 | TemplateName::NameKind TemplateName::getKind() const { |
| 25 | if (Storage.is<TemplateDecl *>()) |
| 26 | return Template; |
| 27 | if (Storage.is<OverloadedTemplateStorage *>()) |
| 28 | return OverloadedTemplate; |
| 29 | if (Storage.is<QualifiedTemplateName *>()) |
| 30 | return QualifiedTemplate; |
| 31 | assert(Storage.is<DependentTemplateName *>() && "There's a case unhandled!"); |
| 32 | return DependentTemplate; |
| 33 | } |
| 34 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 35 | TemplateDecl *TemplateName::getAsTemplateDecl() const { |
| 36 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
| 37 | return Template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 39 | if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) |
| 40 | return QTN->getTemplateDecl(); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | bool TemplateName::isDependent() const { |
| 46 | if (TemplateDecl *Template = getAsTemplateDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | return isa<TemplateTemplateParmDecl>(Template) || |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 48 | Template->getDeclContext()->isDependentContext(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 49 | } |
| 50 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 51 | assert(!getAsOverloadedTemplate() && |
| 52 | "overloaded templates shouldn't survive to here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | void |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 58 | TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy, |
| 59 | bool SuppressNNS) const { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 60 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 61 | OS << Template; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 62 | else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 63 | if (!SuppressNNS) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 64 | QTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 65 | if (QTN->hasTemplateKeyword()) |
| 66 | OS << "template "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 67 | OS << QTN->getDecl(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 68 | } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) { |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 69 | if (!SuppressNNS && DTN->getQualifier()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 70 | DTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 71 | OS << "template "; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 72 | |
| 73 | if (DTN->isIdentifier()) |
| 74 | OS << DTN->getIdentifier()->getName(); |
| 75 | else |
| 76 | OS << "operator " << getOperatorSpelling(DTN->getOperator()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 79 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 80 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 81 | TemplateName N) { |
| 82 | std::string NameStr; |
| 83 | raw_string_ostream OS(NameStr); |
| 84 | LangOptions LO; |
| 85 | LO.CPlusPlus = true; |
| 86 | LO.Bool = true; |
| 87 | N.print(OS, PrintingPolicy(LO)); |
| 88 | OS.flush(); |
| 89 | return DB << NameStr; |
| 90 | } |
| 91 | |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 92 | void TemplateName::dump() const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 93 | LangOptions LO; // FIXME! |
| 94 | LO.CPlusPlus = true; |
| 95 | LO.Bool = true; |
| 96 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 97 | } |