clang  3.9.0
TemplateName.cpp
Go to the documentation of this file.
1 //===--- TemplateName.cpp - C++ Template Name Representation---------------===//
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 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/TemplateName.h"
15 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "clang/Basic/Diagnostic.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
23 using namespace llvm;
24 
27  return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
28 }
29 
30 void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
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 
43  Profile(ID, Context, Parameter, getArgumentPack());
44 }
45 
46 void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
49  const TemplateArgument &ArgPack) {
50  ID.AddPointer(Parameter);
51  ArgPack.Profile(ID, Context);
52 }
53 
54 TemplateName::TemplateName(void *Ptr) {
55  Storage = StorageType::getFromOpaqueValue(Ptr);
56 }
57 
58 TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
60  : Storage(Storage) {}
62  : Storage(Storage) {}
64  : Storage(Storage) {}
67 
68 bool TemplateName::isNull() const { return Storage.isNull(); }
69 
71  if (Storage.is<TemplateDecl *>())
72  return Template;
73  if (Storage.is<DependentTemplateName *>())
74  return DependentTemplate;
75  if (Storage.is<QualifiedTemplateName *>())
76  return QualifiedTemplate;
77 
79  = Storage.get<UncommonTemplateNameStorage*>();
80  if (uncommon->getAsOverloadedStorage())
81  return OverloadedTemplate;
82  if (uncommon->getAsSubstTemplateTemplateParm())
85 }
86 
88  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
89  return Template;
90 
92  return QTN->getTemplateDecl();
93 
95  return sub->getReplacement().getAsTemplateDecl();
96 
97  return nullptr;
98 }
99 
101  if (UncommonTemplateNameStorage *Uncommon =
102  Storage.dyn_cast<UncommonTemplateNameStorage *>())
103  return Uncommon->getAsOverloadedStorage();
104 
105  return nullptr;
106 }
107 
110  if (UncommonTemplateNameStorage *uncommon =
111  Storage.dyn_cast<UncommonTemplateNameStorage *>())
112  return uncommon->getAsSubstTemplateTemplateParm();
113 
114  return nullptr;
115 }
116 
119  if (UncommonTemplateNameStorage *Uncommon =
120  Storage.dyn_cast<UncommonTemplateNameStorage *>())
121  return Uncommon->getAsSubstTemplateTemplateParmPack();
122 
123  return nullptr;
124 }
125 
127  return Storage.dyn_cast<QualifiedTemplateName *>();
128 }
129 
131  return Storage.dyn_cast<DependentTemplateName *>();
132 }
133 
136  if (isa<TemplateTemplateParmDecl>(Template))
137  return true;
138  // FIXME: Hack, getDeclContext() can be null if Template is still
139  // initializing due to PCH reading, so we check it before using it.
140  // Should probably modify TemplateSpecializationType to allow constructing
141  // it without the isDependent() checking.
142  return Template->getDeclContext() &&
143  Template->getDeclContext()->isDependentContext();
144  }
145 
146  assert(!getAsOverloadedTemplate() &&
147  "overloaded templates shouldn't survive to here");
148 
149  return true;
150 }
151 
154  if (QTN->getQualifier()->isInstantiationDependent())
155  return true;
156  }
157 
158  return isDependent();
159 }
160 
163  if (TemplateTemplateParmDecl *TTP
164  = dyn_cast<TemplateTemplateParmDecl>(Template))
165  return TTP->isParameterPack();
166 
167  return false;
168  }
169 
171  return DTN->getQualifier() &&
172  DTN->getQualifier()->containsUnexpandedParameterPack();
173 
174  return getAsSubstTemplateTemplateParmPack() != nullptr;
175 }
176 
177 void
178 TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
179  bool SuppressNNS) const {
180  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
181  OS << *Template;
183  if (!SuppressNNS)
184  QTN->getQualifier()->print(OS, Policy);
185  if (QTN->hasTemplateKeyword())
186  OS << "template ";
187  OS << *QTN->getDecl();
189  if (!SuppressNNS && DTN->getQualifier())
190  DTN->getQualifier()->print(OS, Policy);
191  OS << "template ";
192 
193  if (DTN->isIdentifier())
194  OS << DTN->getIdentifier()->getName();
195  else
196  OS << "operator " << getOperatorSpelling(DTN->getOperator());
197  } else if (SubstTemplateTemplateParmStorage *subst
199  subst->getReplacement().print(OS, Policy, SuppressNNS);
200  } else if (SubstTemplateTemplateParmPackStorage *SubstPack
202  OS << *SubstPack->getParameterPack();
203  else {
205  (*OTS->begin())->printName(OS);
206  }
207 }
208 
210  TemplateName N) {
211  std::string NameStr;
212  raw_string_ostream OS(NameStr);
213  LangOptions LO;
214  LO.CPlusPlus = true;
215  LO.Bool = true;
216  OS << '\'';
217  N.print(OS, PrintingPolicy(LO));
218  OS << '\'';
219  OS.flush();
220  return DB << NameStr;
221 }
222 
223 void TemplateName::dump(raw_ostream &OS) const {
224  LangOptions LO; // FIXME!
225  LO.CPlusPlus = true;
226  LO.Bool = true;
227  print(OS, PrintingPolicy(LO));
228 }
229 
230 LLVM_DUMP_METHOD void TemplateName::dump() const {
231  dump(llvm::errs());
232 }
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool SuppressNNS=false) const
Print the template name.
TemplateDecl * getAsTemplateDecl() const
Retrieve the underlying template declaration that this template name refers to, if known...
Defines the C++ template declaration subclasses.
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:198
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:201
void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context)
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
OverloadedTemplateStorage * getAsOverloadedTemplate() const
Retrieve the underlying, overloaded function template.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:412
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm()
Definition: TemplateName.h:78
NameKind getKind() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
A qualified template name, where the qualification is kept to describe the source code as written...
Definition: TemplateName.h:195
TemplateArgument getArgumentPack() const
Retrieve the template template argument pack with which this parameter was substituted.
QualifiedTemplateName * getAsQualifiedTemplateName() const
Retrieve the underlying qualified template name structure, if any.
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack()
Definition: TemplateName.h:84
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:873
bool containsUnexpandedParameterPack() const
Determines whether this template name contains an unexpanded parameter pack (for C++0x variadic templ...
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:198
ASTContext * Context
Defines the clang::LangOptions interface.
bool isInstantiationDependent() const
Determines whether this is a template name that somehow depends on a template parameter.
void Profile(llvm::FoldingSetNodeID &ID)
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:314
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack() const
Retrieve the substituted template template parameter pack, if known.
Represents a C++ template name within the type system.
Definition: TemplateName.h:176
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
A template template parameter pack that has been substituted for a template template argument pack...
Definition: TemplateName.h:205
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:72
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:119
const std::string ID
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx)
Definition: Type.h:4262
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) const
Used to insert TemplateArguments into FoldingSets.
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm() const
Retrieve the substituted template template parameter, if known.
Represents a template argument.
Definition: TemplateBase.h:40
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:355
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:299
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:330
bool isDependent() const
Determines whether this is a dependent template name.
Defines the Diagnostic-related interfaces.
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword...
bool isNull() const
Determine whether this template name is NULL.
const Expr * Replacement
Definition: AttributeList.h:58
DependentTemplateName * getAsDependentTemplateName() const
Retrieve the underlying dependent template name structure, if any.
The parameter type of a method or function.
A structure for storing the information associated with an overloaded template name.
Definition: TemplateName.h:93
A set of overloaded template declarations.
Definition: TemplateName.h:192
void dump() const
Debugging aid that dumps the template name to standard error.
Implementation class used to describe either a set of overloaded template names or an already-substit...
Definition: TemplateName.h:42
A single template declaration.
Definition: TemplateName.h:190