Coverage Report

Created: 2018-09-23 03:40

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