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 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
15 #define LLVM_CLANG_AST_TEMPLATENAME_H
16
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "clang/Basic/OperatorKinds.h"
21
22 namespace clang {
23
24 class ASTContext;
25 class DependentTemplateName;
26 class DiagnosticBuilder;
27 class IdentifierInfo;
28 class NestedNameSpecifier;
29 class OverloadedTemplateStorage;
30 struct PrintingPolicy;
31 class QualifiedTemplateName;
32 class NamedDecl;
33 class SubstTemplateTemplateParmStorage;
34 class SubstTemplateTemplateParmPackStorage;
35 class TemplateArgument;
36 class TemplateDecl;
37 class TemplateTemplateParmDecl;
38
39 /// \brief Implementation class used to describe either a set of overloaded
40 /// template names or an already-substituted template template parameter pack.
41 class UncommonTemplateNameStorage {
42 protected:
43 enum Kind {
44 Overloaded,
45 SubstTemplateTemplateParm,
46 SubstTemplateTemplateParmPack
47 };
48
49 union {
50 struct {
51 /// \brief A Kind.
52 unsigned Kind : 2;
53
54 /// \brief The number of stored templates or template arguments,
55 /// depending on which subclass we have.
56 unsigned Size : 30;
57 } Bits;
58
59 void *PointerAlignment;
60 };
61
UncommonTemplateNameStorage(Kind kind,unsigned size)62 UncommonTemplateNameStorage(Kind kind, unsigned size) {
63 Bits.Kind = kind;
64 Bits.Size = size;
65 }
66
67 public:
size()68 unsigned size() const { return Bits.Size; }
69
getAsOverloadedStorage()70 OverloadedTemplateStorage *getAsOverloadedStorage() {
71 return Bits.Kind == Overloaded
72 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
73 : 0;
74 }
75
getAsSubstTemplateTemplateParm()76 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
77 return Bits.Kind == SubstTemplateTemplateParm
78 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
79 : 0;
80 }
81
getAsSubstTemplateTemplateParmPack()82 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
83 return Bits.Kind == SubstTemplateTemplateParmPack
84 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
85 : 0;
86 }
87 };
88
89 /// \brief A structure for storing the information associated with an
90 /// overloaded template name.
91 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
92 friend class ASTContext;
93
OverloadedTemplateStorage(unsigned size)94 OverloadedTemplateStorage(unsigned size)
95 : UncommonTemplateNameStorage(Overloaded, size) { }
96
getStorage()97 NamedDecl **getStorage() {
98 return reinterpret_cast<NamedDecl **>(this + 1);
99 }
getStorage()100 NamedDecl * const *getStorage() const {
101 return reinterpret_cast<NamedDecl *const *>(this + 1);
102 }
103
104 public:
105 typedef NamedDecl *const *iterator;
106
begin()107 iterator begin() const { return getStorage(); }
end()108 iterator end() const { return getStorage() + size(); }
109 };
110
111 /// \brief A structure for storing an already-substituted template template
112 /// parameter pack.
113 ///
114 /// This kind of template names occurs when the parameter pack has been
115 /// provided with a template template argument pack in a context where its
116 /// enclosing pack expansion could not be fully expanded.
117 class SubstTemplateTemplateParmPackStorage
118 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
119 {
120 TemplateTemplateParmDecl *Parameter;
121 const TemplateArgument *Arguments;
122
123 public:
SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl * Parameter,unsigned Size,const TemplateArgument * Arguments)124 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
125 unsigned Size,
126 const TemplateArgument *Arguments)
127 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
128 Parameter(Parameter), Arguments(Arguments) { }
129
130 /// \brief Retrieve the template template parameter pack being substituted.
getParameterPack()131 TemplateTemplateParmDecl *getParameterPack() const {
132 return Parameter;
133 }
134
135 /// \brief Retrieve the template template argument pack with which this
136 /// parameter was substituted.
137 TemplateArgument getArgumentPack() const;
138
139 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
140
141 static void Profile(llvm::FoldingSetNodeID &ID,
142 ASTContext &Context,
143 TemplateTemplateParmDecl *Parameter,
144 const TemplateArgument &ArgPack);
145 };
146
147 /// \brief Represents a C++ template name within the type system.
148 ///
149 /// A C++ template name refers to a template within the C++ type
150 /// system. In most cases, a template name is simply a reference to a
151 /// class template, e.g.
152 ///
153 /// \code
154 /// template<typename T> class X { };
155 ///
156 /// X<int> xi;
157 /// \endcode
158 ///
159 /// Here, the 'X' in \c X<int> is a template name that refers to the
160 /// declaration of the class template X, above. Template names can
161 /// also refer to function templates, C++0x template aliases, etc.
162 ///
163 /// Some template names are dependent. For example, consider:
164 ///
165 /// \code
166 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
167 /// typedef typename MetaFun::template apply<T1, T2>::type type;
168 /// };
169 /// \endcode
170 ///
171 /// Here, "apply" is treated as a template name within the typename
172 /// specifier in the typedef. "apply" is a nested template, and can
173 /// only be understood in the context of
174 class TemplateName {
175 typedef llvm::PointerUnion4<TemplateDecl *,
176 UncommonTemplateNameStorage *,
177 QualifiedTemplateName *,
178 DependentTemplateName *> StorageType;
179
180 StorageType Storage;
181
TemplateName(void * Ptr)182 explicit TemplateName(void *Ptr) {
183 Storage = StorageType::getFromOpaqueValue(Ptr);
184 }
185
186 public:
187 // \brief Kind of name that is actually stored.
188 enum NameKind {
189 /// \brief A single template declaration.
190 Template,
191 /// \brief A set of overloaded template declarations.
192 OverloadedTemplate,
193 /// \brief A qualified template name, where the qualification is kept
194 /// to describe the source code as written.
195 QualifiedTemplate,
196 /// \brief A dependent template name that has not been resolved to a
197 /// template (or set of templates).
198 DependentTemplate,
199 /// \brief A template template parameter that has been substituted
200 /// for some other template name.
201 SubstTemplateTemplateParm,
202 /// \brief A template template parameter pack that has been substituted for
203 /// a template template argument pack, but has not yet been expanded into
204 /// individual arguments.
205 SubstTemplateTemplateParmPack
206 };
207
TemplateName()208 TemplateName() : Storage() { }
TemplateName(TemplateDecl * Template)209 explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
TemplateName(OverloadedTemplateStorage * Storage)210 explicit TemplateName(OverloadedTemplateStorage *Storage)
211 : Storage(Storage) { }
212 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
TemplateName(SubstTemplateTemplateParmPackStorage * Storage)213 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
214 : Storage(Storage) { }
TemplateName(QualifiedTemplateName * Qual)215 explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
TemplateName(DependentTemplateName * Dep)216 explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
217
218 /// \brief Determine whether this template name is NULL.
isNull()219 bool isNull() const { return Storage.isNull(); }
220
221 // \brief Get the kind of name that is actually stored.
222 NameKind getKind() const;
223
224 /// \brief Retrieve the underlying template declaration that
225 /// this template name refers to, if known.
226 ///
227 /// \returns The template declaration that this template name refers
228 /// to, if any. If the template name does not refer to a specific
229 /// declaration because it is a dependent name, or if it refers to a
230 /// set of function templates, returns NULL.
231 TemplateDecl *getAsTemplateDecl() const;
232
233 /// \brief Retrieve the underlying, overloaded function template
234 // declarations that this template name refers to, if known.
235 ///
236 /// \returns The set of overloaded function templates that this template
237 /// name refers to, if known. If the template name does not refer to a
238 /// specific set of function templates because it is a dependent name or
239 /// refers to a single template, returns NULL.
getAsOverloadedTemplate()240 OverloadedTemplateStorage *getAsOverloadedTemplate() const {
241 if (UncommonTemplateNameStorage *Uncommon =
242 Storage.dyn_cast<UncommonTemplateNameStorage *>())
243 return Uncommon->getAsOverloadedStorage();
244
245 return 0;
246 }
247
248 /// \brief Retrieve the substituted template template parameter, if
249 /// known.
250 ///
251 /// \returns The storage for the substituted template template parameter,
252 /// if known. Otherwise, returns NULL.
getAsSubstTemplateTemplateParm()253 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const {
254 if (UncommonTemplateNameStorage *uncommon =
255 Storage.dyn_cast<UncommonTemplateNameStorage *>())
256 return uncommon->getAsSubstTemplateTemplateParm();
257
258 return 0;
259 }
260
261 /// \brief Retrieve the substituted template template parameter pack, if
262 /// known.
263 ///
264 /// \returns The storage for the substituted template template parameter pack,
265 /// if known. Otherwise, returns NULL.
266 SubstTemplateTemplateParmPackStorage *
getAsSubstTemplateTemplateParmPack()267 getAsSubstTemplateTemplateParmPack() const {
268 if (UncommonTemplateNameStorage *Uncommon =
269 Storage.dyn_cast<UncommonTemplateNameStorage *>())
270 return Uncommon->getAsSubstTemplateTemplateParmPack();
271
272 return 0;
273 }
274
275 /// \brief Retrieve the underlying qualified template name
276 /// structure, if any.
getAsQualifiedTemplateName()277 QualifiedTemplateName *getAsQualifiedTemplateName() const {
278 return Storage.dyn_cast<QualifiedTemplateName *>();
279 }
280
281 /// \brief Retrieve the underlying dependent template name
282 /// structure, if any.
getAsDependentTemplateName()283 DependentTemplateName *getAsDependentTemplateName() const {
284 return Storage.dyn_cast<DependentTemplateName *>();
285 }
286
287 TemplateName getUnderlying() const;
288
289 /// \brief Determines whether this is a dependent template name.
290 bool isDependent() const;
291
292 /// \brief Determines whether this is a template name that somehow
293 /// depends on a template parameter.
294 bool isInstantiationDependent() const;
295
296 /// \brief Determines whether this template name contains an
297 /// unexpanded parameter pack (for C++0x variadic templates).
298 bool containsUnexpandedParameterPack() const;
299
300 /// \brief Print the template name.
301 ///
302 /// \param OS the output stream to which the template name will be
303 /// printed.
304 ///
305 /// \param SuppressNNS if true, don't print the
306 /// nested-name-specifier that precedes the template name (if it has
307 /// one).
308 void print(raw_ostream &OS, const PrintingPolicy &Policy,
309 bool SuppressNNS = false) const;
310
311 /// \brief Debugging aid that dumps the template name to standard
312 /// error.
313 void dump() const;
314
Profile(llvm::FoldingSetNodeID & ID)315 void Profile(llvm::FoldingSetNodeID &ID) {
316 ID.AddPointer(Storage.getOpaqueValue());
317 }
318
319 /// \brief Retrieve the template name as a void pointer.
getAsVoidPointer()320 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
321
322 /// \brief Build a template name from a void pointer.
getFromVoidPointer(void * Ptr)323 static TemplateName getFromVoidPointer(void *Ptr) {
324 return TemplateName(Ptr);
325 }
326 };
327
328 /// Insertion operator for diagnostics. This allows sending TemplateName's
329 /// into a diagnostic with <<.
330 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
331 TemplateName N);
332
333 /// \brief A structure for storing the information associated with a
334 /// substituted template template parameter.
335 class SubstTemplateTemplateParmStorage
336 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
337 friend class ASTContext;
338
339 TemplateTemplateParmDecl *Parameter;
340 TemplateName Replacement;
341
SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl * parameter,TemplateName replacement)342 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
343 TemplateName replacement)
344 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
345 Parameter(parameter), Replacement(replacement) {}
346
347 public:
getParameter()348 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
getReplacement()349 TemplateName getReplacement() const { return Replacement; }
350
351 void Profile(llvm::FoldingSetNodeID &ID);
352
353 static void Profile(llvm::FoldingSetNodeID &ID,
354 TemplateTemplateParmDecl *parameter,
355 TemplateName replacement);
356 };
357
TemplateName(SubstTemplateTemplateParmStorage * Storage)358 inline TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
359 : Storage(Storage) { }
360
getUnderlying()361 inline TemplateName TemplateName::getUnderlying() const {
362 if (SubstTemplateTemplateParmStorage *subst
363 = getAsSubstTemplateTemplateParm())
364 return subst->getReplacement().getUnderlying();
365 return *this;
366 }
367
368 /// \brief Represents a template name that was expressed as a
369 /// qualified name.
370 ///
371 /// This kind of template name refers to a template name that was
372 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
373 /// the nested name specifier is "std::" and the template name is the
374 /// declaration for "vector". The QualifiedTemplateName class is only
375 /// used to provide "sugar" for template names that were expressed
376 /// with a qualified name, and has no semantic meaning. In this
377 /// manner, it is to TemplateName what ElaboratedType is to Type,
378 /// providing extra syntactic sugar for downstream clients.
379 class QualifiedTemplateName : public llvm::FoldingSetNode {
380 /// \brief The nested name specifier that qualifies the template name.
381 ///
382 /// The bit is used to indicate whether the "template" keyword was
383 /// present before the template name itself. Note that the
384 /// "template" keyword is always redundant in this case (otherwise,
385 /// the template name would be a dependent name and we would express
386 /// this name with DependentTemplateName).
387 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
388
389 /// \brief The template declaration or set of overloaded function templates
390 /// that this qualified name refers to.
391 TemplateDecl *Template;
392
393 friend class ASTContext;
394
QualifiedTemplateName(NestedNameSpecifier * NNS,bool TemplateKeyword,TemplateDecl * Template)395 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
396 TemplateDecl *Template)
397 : Qualifier(NNS, TemplateKeyword? 1 : 0),
398 Template(Template) { }
399
400 public:
401 /// \brief Return the nested name specifier that qualifies this name.
getQualifier()402 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
403
404 /// \brief Whether the template name was prefixed by the "template"
405 /// keyword.
hasTemplateKeyword()406 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
407
408 /// \brief The template declaration that this qualified name refers
409 /// to.
getDecl()410 TemplateDecl *getDecl() const { return Template; }
411
412 /// \brief The template declaration to which this qualified name
413 /// refers.
getTemplateDecl()414 TemplateDecl *getTemplateDecl() const { return Template; }
415
Profile(llvm::FoldingSetNodeID & ID)416 void Profile(llvm::FoldingSetNodeID &ID) {
417 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
418 }
419
Profile(llvm::FoldingSetNodeID & ID,NestedNameSpecifier * NNS,bool TemplateKeyword,TemplateDecl * Template)420 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
421 bool TemplateKeyword, TemplateDecl *Template) {
422 ID.AddPointer(NNS);
423 ID.AddBoolean(TemplateKeyword);
424 ID.AddPointer(Template);
425 }
426 };
427
428 /// \brief Represents a dependent template name that cannot be
429 /// resolved prior to template instantiation.
430 ///
431 /// This kind of template name refers to a dependent template name,
432 /// including its nested name specifier (if any). For example,
433 /// DependentTemplateName can refer to "MetaFun::template apply",
434 /// where "MetaFun::" is the nested name specifier and "apply" is the
435 /// template name referenced. The "template" keyword is implied.
436 class DependentTemplateName : public llvm::FoldingSetNode {
437 /// \brief The nested name specifier that qualifies the template
438 /// name.
439 ///
440 /// The bit stored in this qualifier describes whether the \c Name field
441 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
442 /// overloaded operator kind (when set).
443 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
444
445 /// \brief The dependent template name.
446 union {
447 /// \brief The identifier template name.
448 ///
449 /// Only valid when the bit on \c Qualifier is clear.
450 const IdentifierInfo *Identifier;
451
452 /// \brief The overloaded operator name.
453 ///
454 /// Only valid when the bit on \c Qualifier is set.
455 OverloadedOperatorKind Operator;
456 };
457
458 /// \brief The canonical template name to which this dependent
459 /// template name refers.
460 ///
461 /// The canonical template name for a dependent template name is
462 /// another dependent template name whose nested name specifier is
463 /// canonical.
464 TemplateName CanonicalTemplateName;
465
466 friend class ASTContext;
467
DependentTemplateName(NestedNameSpecifier * Qualifier,const IdentifierInfo * Identifier)468 DependentTemplateName(NestedNameSpecifier *Qualifier,
469 const IdentifierInfo *Identifier)
470 : Qualifier(Qualifier, false), Identifier(Identifier),
471 CanonicalTemplateName(this) { }
472
DependentTemplateName(NestedNameSpecifier * Qualifier,const IdentifierInfo * Identifier,TemplateName Canon)473 DependentTemplateName(NestedNameSpecifier *Qualifier,
474 const IdentifierInfo *Identifier,
475 TemplateName Canon)
476 : Qualifier(Qualifier, false), Identifier(Identifier),
477 CanonicalTemplateName(Canon) { }
478
DependentTemplateName(NestedNameSpecifier * Qualifier,OverloadedOperatorKind Operator)479 DependentTemplateName(NestedNameSpecifier *Qualifier,
480 OverloadedOperatorKind Operator)
481 : Qualifier(Qualifier, true), Operator(Operator),
482 CanonicalTemplateName(this) { }
483
DependentTemplateName(NestedNameSpecifier * Qualifier,OverloadedOperatorKind Operator,TemplateName Canon)484 DependentTemplateName(NestedNameSpecifier *Qualifier,
485 OverloadedOperatorKind Operator,
486 TemplateName Canon)
487 : Qualifier(Qualifier, true), Operator(Operator),
488 CanonicalTemplateName(Canon) { }
489
490 public:
491 /// \brief Return the nested name specifier that qualifies this name.
getQualifier()492 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
493
494 /// \brief Determine whether this template name refers to an identifier.
isIdentifier()495 bool isIdentifier() const { return !Qualifier.getInt(); }
496
497 /// \brief Returns the identifier to which this template name refers.
getIdentifier()498 const IdentifierInfo *getIdentifier() const {
499 assert(isIdentifier() && "Template name isn't an identifier?");
500 return Identifier;
501 }
502
503 /// \brief Determine whether this template name refers to an overloaded
504 /// operator.
isOverloadedOperator()505 bool isOverloadedOperator() const { return Qualifier.getInt(); }
506
507 /// \brief Return the overloaded operator to which this template name refers.
getOperator()508 OverloadedOperatorKind getOperator() const {
509 assert(isOverloadedOperator() &&
510 "Template name isn't an overloaded operator?");
511 return Operator;
512 }
513
Profile(llvm::FoldingSetNodeID & ID)514 void Profile(llvm::FoldingSetNodeID &ID) {
515 if (isIdentifier())
516 Profile(ID, getQualifier(), getIdentifier());
517 else
518 Profile(ID, getQualifier(), getOperator());
519 }
520
Profile(llvm::FoldingSetNodeID & ID,NestedNameSpecifier * NNS,const IdentifierInfo * Identifier)521 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
522 const IdentifierInfo *Identifier) {
523 ID.AddPointer(NNS);
524 ID.AddBoolean(false);
525 ID.AddPointer(Identifier);
526 }
527
Profile(llvm::FoldingSetNodeID & ID,NestedNameSpecifier * NNS,OverloadedOperatorKind Operator)528 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
529 OverloadedOperatorKind Operator) {
530 ID.AddPointer(NNS);
531 ID.AddBoolean(true);
532 ID.AddInteger(Operator);
533 }
534 };
535
536 } // end namespace clang.
537
538 namespace llvm {
539
540 /// \brief The clang::TemplateName class is effectively a pointer.
541 template<>
542 class PointerLikeTypeTraits<clang::TemplateName> {
543 public:
getAsVoidPointer(clang::TemplateName TN)544 static inline void *getAsVoidPointer(clang::TemplateName TN) {
545 return TN.getAsVoidPointer();
546 }
547
getFromVoidPointer(void * Ptr)548 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
549 return clang::TemplateName::getFromVoidPointer(Ptr);
550 }
551
552 // No bits are available!
553 enum { NumLowBitsAvailable = 0 };
554 };
555
556 } // end namespace llvm.
557
558 #endif
559