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 | |
| 24 | TemplateDecl *TemplateName::getAsTemplateDecl() const { |
| 25 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
| 26 | return Template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 28 | if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) |
| 29 | return QTN->getTemplateDecl(); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | bool TemplateName::isDependent() const { |
| 35 | if (TemplateDecl *Template = getAsTemplateDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | return isa<TemplateTemplateParmDecl>(Template) || |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 37 | Template->getDeclContext()->isDependentContext(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 38 | } |
| 39 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 40 | assert(!getAsOverloadedTemplate() && |
| 41 | "overloaded templates shouldn't survive to here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | void |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 47 | TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy, |
| 48 | bool SuppressNNS) const { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 49 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame^] | 50 | OS << Template; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 51 | else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 52 | if (!SuppressNNS) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 53 | QTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 54 | if (QTN->hasTemplateKeyword()) |
| 55 | OS << "template "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame^] | 56 | OS << QTN->getDecl(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 57 | } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) { |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 58 | if (!SuppressNNS && DTN->getQualifier()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 59 | DTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 60 | OS << "template "; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 61 | |
| 62 | if (DTN->isIdentifier()) |
| 63 | OS << DTN->getIdentifier()->getName(); |
| 64 | else |
| 65 | OS << "operator " << getOperatorSpelling(DTN->getOperator()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 68 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 69 | const 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 Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 81 | void TemplateName::dump() const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 82 | LangOptions LO; // FIXME! |
| 83 | LO.CPlusPlus = true; |
| 84 | LO.Bool = true; |
| 85 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 86 | } |