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" |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 15 | #include "clang/AST/TemplateBase.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
| 17 | #include "clang/AST/NestedNameSpecifier.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 22 | using namespace clang; |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 25 | TemplateArgument |
| 26 | SubstTemplateTemplateParmPackStorage::getArgumentPack() const { |
| 27 | return TemplateArgument(Arguments, size()); |
| 28 | } |
| 29 | |
| 30 | void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID) { |
| 31 | Profile(ID, Context, Parameter, TemplateArgument(Arguments, size())); |
| 32 | } |
| 33 | |
| 34 | void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID, |
| 35 | ASTContext &Context, |
| 36 | TemplateTemplateParmDecl *Parameter, |
| 37 | const TemplateArgument &ArgPack) { |
| 38 | ID.AddPointer(Parameter); |
| 39 | ArgPack.Profile(ID, Context); |
| 40 | } |
| 41 | |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 42 | TemplateName::NameKind TemplateName::getKind() const { |
| 43 | if (Storage.is<TemplateDecl *>()) |
| 44 | return Template; |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 45 | if (Storage.is<DependentTemplateName *>()) |
| 46 | return DependentTemplate; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 47 | if (Storage.is<QualifiedTemplateName *>()) |
| 48 | return QualifiedTemplate; |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 49 | |
| 50 | return getAsOverloadedTemplate()? OverloadedTemplate |
| 51 | : SubstTemplateTemplateParmPack; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 54 | TemplateDecl *TemplateName::getAsTemplateDecl() const { |
| 55 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
| 56 | return Template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 58 | if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) |
| 59 | return QTN->getTemplateDecl(); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | bool TemplateName::isDependent() const { |
| 65 | if (TemplateDecl *Template = getAsTemplateDecl()) { |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 66 | if (isa<TemplateTemplateParmDecl>(Template)) |
| 67 | return true; |
| 68 | // FIXME: Hack, getDeclContext() can be null if Template is still |
| 69 | // initializing due to PCH reading, so we check it before using it. |
| 70 | // Should probably modify TemplateSpecializationType to allow constructing |
| 71 | // it without the isDependent() checking. |
| 72 | return Template->getDeclContext() && |
| 73 | Template->getDeclContext()->isDependentContext(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 76 | assert(!getAsOverloadedTemplate() && |
| 77 | "overloaded templates shouldn't survive to here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 82 | bool TemplateName::containsUnexpandedParameterPack() const { |
| 83 | if (TemplateDecl *Template = getAsTemplateDecl()) { |
| 84 | if (TemplateTemplateParmDecl *TTP |
| 85 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 86 | return TTP->isParameterPack(); |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (DependentTemplateName *DTN = getAsDependentTemplateName()) |
| 92 | return DTN->getQualifier() && |
| 93 | DTN->getQualifier()->containsUnexpandedParameterPack(); |
| 94 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 95 | return getAsSubstTemplateTemplateParmPack() != 0; |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | void |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 99 | TemplateName::print(llvm::raw_ostream &OS, const PrintingPolicy &Policy, |
| 100 | bool SuppressNNS) const { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 101 | if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>()) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 102 | OS << Template; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 103 | else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 104 | if (!SuppressNNS) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 105 | QTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 106 | if (QTN->hasTemplateKeyword()) |
| 107 | OS << "template "; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 108 | OS << QTN->getDecl(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 109 | } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) { |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 110 | if (!SuppressNNS && DTN->getQualifier()) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 111 | DTN->getQualifier()->print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 112 | OS << "template "; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 113 | |
| 114 | if (DTN->isIdentifier()) |
| 115 | OS << DTN->getIdentifier()->getName(); |
| 116 | else |
| 117 | OS << "operator " << getOperatorSpelling(DTN->getOperator()); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame^] | 118 | } else if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 119 | = getAsSubstTemplateTemplateParmPack()) |
| 120 | OS << SubstPack->getParameterPack()->getNameAsString(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 122 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 123 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 124 | TemplateName N) { |
| 125 | std::string NameStr; |
| 126 | raw_string_ostream OS(NameStr); |
| 127 | LangOptions LO; |
| 128 | LO.CPlusPlus = true; |
| 129 | LO.Bool = true; |
| 130 | N.print(OS, PrintingPolicy(LO)); |
| 131 | OS.flush(); |
| 132 | return DB << NameStr; |
| 133 | } |
| 134 | |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 135 | void TemplateName::dump() const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 136 | LangOptions LO; // FIXME! |
| 137 | LO.CPlusPlus = true; |
| 138 | LO.Bool = true; |
| 139 | print(llvm::errs(), PrintingPolicy(LO)); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 140 | } |