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