clang  9.0.0svn
TemplateName.cpp
Go to the documentation of this file.
1 //===- TemplateName.cpp - C++ Template Name Representation ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the TemplateName interface and subclasses.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/TemplateName.h"
14 #include "clang/AST/DeclBase.h"
15 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <cassert>
29 #include <string>
30 
31 using namespace clang;
32 
35  return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
36 }
37 
38 void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
39  Profile(ID, Parameter, Replacement);
40 }
41 
42 void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
43  TemplateTemplateParmDecl *parameter,
44  TemplateName replacement) {
45  ID.AddPointer(parameter);
46  ID.AddPointer(replacement.getAsVoidPointer());
47 }
48 
50  ASTContext &Context) {
51  Profile(ID, Context, Parameter, getArgumentPack());
52 }
53 
55  ASTContext &Context,
56  TemplateTemplateParmDecl *Parameter,
57  const TemplateArgument &ArgPack) {
58  ID.AddPointer(Parameter);
59  ArgPack.Profile(ID, Context);
60 }
61 
62 TemplateName::TemplateName(void *Ptr) {
63  Storage = StorageType::getFromOpaqueValue(Ptr);
64 }
65 
66 TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
68  : Storage(Storage) {}
70  : Storage(Storage) {}
72  : Storage(Storage) {}
75 
76 bool TemplateName::isNull() const { return Storage.isNull(); }
77 
79  if (Storage.is<TemplateDecl *>())
80  return Template;
81  if (Storage.is<DependentTemplateName *>())
82  return DependentTemplate;
83  if (Storage.is<QualifiedTemplateName *>())
84  return QualifiedTemplate;
85 
87  = Storage.get<UncommonTemplateNameStorage*>();
88  if (uncommon->getAsOverloadedStorage())
89  return OverloadedTemplate;
90  if (uncommon->getAsSubstTemplateTemplateParm())
93 }
94 
96  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
97  return Template;
98 
100  return QTN->getTemplateDecl();
101 
103  return sub->getReplacement().getAsTemplateDecl();
104 
105  return nullptr;
106 }
107 
109  if (UncommonTemplateNameStorage *Uncommon =
110  Storage.dyn_cast<UncommonTemplateNameStorage *>())
111  return Uncommon->getAsOverloadedStorage();
112 
113  return nullptr;
114 }
115 
118  if (UncommonTemplateNameStorage *uncommon =
119  Storage.dyn_cast<UncommonTemplateNameStorage *>())
120  return uncommon->getAsSubstTemplateTemplateParm();
121 
122  return nullptr;
123 }
124 
127  if (UncommonTemplateNameStorage *Uncommon =
128  Storage.dyn_cast<UncommonTemplateNameStorage *>())
129  return Uncommon->getAsSubstTemplateTemplateParmPack();
130 
131  return nullptr;
132 }
133 
135  return Storage.dyn_cast<QualifiedTemplateName *>();
136 }
137 
139  return Storage.dyn_cast<DependentTemplateName *>();
140 }
141 
144 
145  // Substituting a dependent template name: preserve it as written.
146  if (!Decl)
147  return *this;
148 
149  // If we have a template declaration, use the most recent non-friend
150  // declaration of that template.
151  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
152  while (Decl->getFriendObjectKind()) {
153  Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
154  assert(Decl && "all declarations of template are friends");
155  }
156  return TemplateName(Decl);
157 }
158 
161  if (isa<TemplateTemplateParmDecl>(Template))
162  return true;
163  // FIXME: Hack, getDeclContext() can be null if Template is still
164  // initializing due to PCH reading, so we check it before using it.
165  // Should probably modify TemplateSpecializationType to allow constructing
166  // it without the isDependent() checking.
167  return Template->getDeclContext() &&
168  Template->getDeclContext()->isDependentContext();
169  }
170 
171  assert(!getAsOverloadedTemplate() &&
172  "overloaded templates shouldn't survive to here");
173 
174  return true;
175 }
176 
179  if (QTN->getQualifier()->isInstantiationDependent())
180  return true;
181  }
182 
183  return isDependent();
184 }
185 
188  if (QTN->getQualifier()->containsUnexpandedParameterPack())
189  return true;
190  }
191 
193  if (TemplateTemplateParmDecl *TTP
194  = dyn_cast<TemplateTemplateParmDecl>(Template))
195  return TTP->isParameterPack();
196 
197  return false;
198  }
199 
201  return DTN->getQualifier() &&
202  DTN->getQualifier()->containsUnexpandedParameterPack();
203 
204  return getAsSubstTemplateTemplateParmPack() != nullptr;
205 }
206 
207 void
208 TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
209  bool SuppressNNS) const {
210  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
211  OS << *Template;
213  if (!SuppressNNS)
214  QTN->getQualifier()->print(OS, Policy);
215  if (QTN->hasTemplateKeyword())
216  OS << "template ";
217  OS << *QTN->getDecl();
219  if (!SuppressNNS && DTN->getQualifier())
220  DTN->getQualifier()->print(OS, Policy);
221  OS << "template ";
222 
223  if (DTN->isIdentifier())
224  OS << DTN->getIdentifier()->getName();
225  else
226  OS << "operator " << getOperatorSpelling(DTN->getOperator());
227  } else if (SubstTemplateTemplateParmStorage *subst
229  subst->getReplacement().print(OS, Policy, SuppressNNS);
230  } else if (SubstTemplateTemplateParmPackStorage *SubstPack
232  OS << *SubstPack->getParameterPack();
233  else {
235  (*OTS->begin())->printName(OS);
236  }
237 }
238 
240  TemplateName N) {
241  std::string NameStr;
242  llvm::raw_string_ostream OS(NameStr);
243  LangOptions LO;
244  LO.CPlusPlus = true;
245  LO.Bool = true;
246  OS << '\'';
247  N.print(OS, PrintingPolicy(LO));
248  OS << '\'';
249  OS.flush();
250  return DB << NameStr;
251 }
252 
253 void TemplateName::dump(raw_ostream &OS) const {
254  LangOptions LO; // FIXME!
255  LO.CPlusPlus = true;
256  LO.Bool = true;
257  print(OS, PrintingPolicy(LO));
258 }
259 
260 LLVM_DUMP_METHOD void TemplateName::dump() const {
261  dump(llvm::errs());
262 }
void dump() const
Debugging aid that dumps the template name to standard error.
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:310
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
Defines the C++ template declaration subclasses.
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration, or NULL if there is no previous declaration.
Definition: DeclBase.h:952
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:335
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:206
void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context)
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) const
Used to insert TemplateArguments into FoldingSets.
QualifiedTemplateName * getAsQualifiedTemplateName() const
Retrieve the underlying qualified template name structure, if any.
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm() const
Retrieve the substituted template template parameter, if known.
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool SuppressNNS=false) const
Print the template name.
TemplateName getNameToSubstitute() const
Get the template name to substitute when this template name is used as a template template argument...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:422
TemplateDecl * getAsTemplateDecl() const
Retrieve the underlying template declaration that this template name refers to, if known...
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack() const
Retrieve the substituted template template parameter pack, if known.
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm()
Definition: TemplateName.h:80
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
A qualified template name, where the qualification is kept to describe the source code as written...
Definition: TemplateName.h:198
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Defines the Diagnostic-related interfaces.
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack()
Definition: TemplateName.h:86
bool containsUnexpandedParameterPack() const
Determines whether this template name contains an unexpanded parameter pack (for C++0x variadic templ...
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1044
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:202
Defines the clang::LangOptions interface.
Defines an enumeration for C++ overloaded operators.
void Profile(llvm::FoldingSetNodeID &ID)
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:325
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
TemplateName()=default
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:1103
A template template parameter pack that has been substituted for a template template argument pack...
Definition: TemplateName.h:211
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:74
OverloadedTemplateStorage * getAsOverloadedTemplate() const
Retrieve the underlying, overloaded function template.
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:121
DependentTemplateName * getAsDependentTemplateName() const
Retrieve the underlying dependent template name structure, if any.
Represents a template argument.
Definition: TemplateBase.h:50
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:366
bool isNull() const
Determine whether this template name is NULL.
Dataflow Directional Tag Classes.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:398
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword...
A structure for storing the information associated with an overloaded template name.
Definition: TemplateName.h:95
NameKind getKind() const
NamedDecl * getMostRecentDecl()
Definition: Decl.h:445
TemplateArgument getArgumentPack() const
Retrieve the template template argument pack with which this parameter was substituted.
A set of overloaded template declarations.
Definition: TemplateName.h:194
Implementation class used to describe either a set of overloaded template names or an already-substit...
Definition: TemplateName.h:44
bool isDependent() const
Determines whether this is a dependent template name.
A single template declaration.
Definition: TemplateName.h:191
bool isInstantiationDependent() const
Determines whether this is a template name that somehow depends on a template parameter.