Coverage Report

Created: 2020-12-01 06:44

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