Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/TemplateName.cpp
Line
Count
Source (jump to first uncovered line)
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/Decl.h"
15
#include "clang/AST/DeclBase.h"
16
#include "clang/AST/DeclTemplate.h"
17
#include "clang/AST/DependenceFlags.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/PrettyPrinter.h"
20
#include "clang/AST/TemplateBase.h"
21
#include "clang/Basic/Diagnostic.h"
22
#include "clang/Basic/LLVM.h"
23
#include "clang/Basic/LangOptions.h"
24
#include "clang/Basic/OperatorKinds.h"
25
#include "llvm/ADT/ArrayRef.h"
26
#include "llvm/ADT/FoldingSet.h"
27
#include "llvm/Support/Casting.h"
28
#include "llvm/Support/Compiler.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <cassert>
31
#include <string>
32
33
using namespace clang;
34
35
TemplateArgument
36
9
SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
37
9
  return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
38
9
}
39
40
25.9k
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
41
25.9k
  Profile(ID, Parameter, Replacement);
42
25.9k
}
43
44
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
45
                                           TemplateTemplateParmDecl *parameter,
46
53.1k
                                               TemplateName replacement) {
47
53.1k
  ID.AddPointer(parameter);
48
53.1k
  ID.AddPointer(replacement.getAsVoidPointer());
49
53.1k
}
50
51
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
52
0
                                                   ASTContext &Context) {
53
0
  Profile(ID, Context, Parameter, getArgumentPack());
54
0
}
55
56
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
57
                                                   ASTContext &Context,
58
                                           TemplateTemplateParmDecl *Parameter,
59
8
                                             const TemplateArgument &ArgPack) {
60
8
  ID.AddPointer(Parameter);
61
8
  ArgPack.Profile(ID, Context);
62
8
}
63
64
3.45M
TemplateName::TemplateName(void *Ptr) {
65
3.45M
  Storage = StorageType::getFromOpaqueValue(Ptr);
66
3.45M
}
67
68
13.2M
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
69
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
70
105k
    : Storage(Storage) {}
71
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
72
872
    : Storage(Storage) {}
73
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
74
27.2k
    : Storage(Storage) {}
75
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
76
8
    : Storage(Storage) {}
77
148k
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
78
422k
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
79
80
4.20M
bool TemplateName::isNull() const { return Storage.isNull(); }
81
82
36.2M
TemplateName::NameKind TemplateName::getKind() const {
83
36.2M
  if (Storage.is<TemplateDecl *>())
84
35.9M
    return Template;
85
376k
  if (Storage.is<DependentTemplateName *>())
86
9.73k
    return DependentTemplate;
87
366k
  if (Storage.is<QualifiedTemplateName *>())
88
170k
    return QualifiedTemplate;
89
90
196k
  UncommonTemplateNameStorage *uncommon
91
196k
    = Storage.get<UncommonTemplateNameStorage*>();
92
196k
  if (uncommon->getAsOverloadedStorage())
93
109k
    return OverloadedTemplate;
94
87.4k
  if (uncommon->getAsAssumedTemplateName())
95
61
    return AssumedTemplate;
96
87.3k
  if (uncommon->getAsSubstTemplateTemplateParm())
97
87.3k
    return SubstTemplateTemplateParm;
98
22
  return SubstTemplateTemplateParmPack;
99
87.3k
}
100
101
63.1M
TemplateDecl *TemplateName::getAsTemplateDecl() const {
102
63.1M
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
103
62.5M
    return Template;
104
105
518k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
106
344k
    return QTN->getTemplateDecl();
107
108
174k
  if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
109
111k
    return sub->getReplacement().getAsTemplateDecl();
110
111
62.7k
  return nullptr;
112
174k
}
113
114
1.19M
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
115
1.19M
  if (UncommonTemplateNameStorage *Uncommon =
116
1.19M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
117
109k
    return Uncommon->getAsOverloadedStorage();
118
119
1.08M
  return nullptr;
120
1.19M
}
121
122
8.54M
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
123
8.54M
  if (UncommonTemplateNameStorage *Uncommon =
124
8.54M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
125
24.3k
    return Uncommon->getAsAssumedTemplateName();
126
127
8.51M
  return nullptr;
128
8.54M
}
129
130
SubstTemplateTemplateParmStorage *
131
6.43M
TemplateName::getAsSubstTemplateTemplateParm() const {
132
6.43M
  if (UncommonTemplateNameStorage *uncommon =
133
6.43M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
134
141k
    return uncommon->getAsSubstTemplateTemplateParm();
135
136
6.28M
  return nullptr;
137
6.43M
}
138
139
SubstTemplateTemplateParmPackStorage *
140
3.50M
TemplateName::getAsSubstTemplateTemplateParmPack() const {
141
3.50M
  if (UncommonTemplateNameStorage *Uncommon =
142
3.50M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
143
250
    return Uncommon->getAsSubstTemplateTemplateParmPack();
144
145
3.50M
  return nullptr;
146
3.50M
}
147
148
17.8M
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
149
17.8M
  return Storage.dyn_cast<QualifiedTemplateName *>();
150
17.8M
}
151
152
43.0M
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
153
43.0M
  return Storage.dyn_cast<DependentTemplateName *>();
154
43.0M
}
155
156
27.2k
TemplateName TemplateName::getNameToSubstitute() const {
157
27.2k
  TemplateDecl *Decl = getAsTemplateDecl();
158
159
  // Substituting a dependent template name: preserve it as written.
160
27.2k
  if (!Decl)
161
450
    return *this;
162
163
  // If we have a template declaration, use the most recent non-friend
164
  // declaration of that template.
165
26.8k
  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
166
26.8k
  while (Decl->getFriendObjectKind()) {
167
21
    Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
168
21
    assert(Decl && "all declarations of template are friends");
169
21
  }
170
26.8k
  return TemplateName(Decl);
171
27.2k
}
172
173
16.1M
TemplateNameDependence TemplateName::getDependence() const {
174
16.1M
  auto D = TemplateNameDependence::None;
175
16.1M
  switch (getKind()) {
176
95.5k
  case TemplateName::NameKind::QualifiedTemplate:
177
95.5k
    D |= toTemplateNameDependence(
178
95.5k
        getAsQualifiedTemplateName()->getQualifier()->getDependence());
179
95.5k
    break;
180
996
  case TemplateName::NameKind::DependentTemplate:
181
996
    D |= toTemplateNameDependence(
182
996
        getAsDependentTemplateName()->getQualifier()->getDependence());
183
996
    break;
184
8
  case TemplateName::NameKind::SubstTemplateTemplateParmPack:
185
8
    D |= TemplateNameDependence::UnexpandedPack;
186
8
    break;
187
0
  case TemplateName::NameKind::OverloadedTemplate:
188
0
    llvm_unreachable("overloaded templates shouldn't survive to here.");
189
16.0M
  default:
190
16.0M
    break;
191
16.1M
  }
192
16.1M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
193
16.1M
    if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) {
194
120k
      D |= TemplateNameDependence::DependentInstantiation;
195
120k
      if (TTP->isParameterPack())
196
185
        D |= TemplateNameDependence::UnexpandedPack;
197
120k
    }
198
    // FIXME: Hack, getDeclContext() can be null if Template is still
199
    // initializing due to PCH reading, so we check it before using it.
200
    // Should probably modify TemplateSpecializationType to allow constructing
201
    // it without the isDependent() checking.
202
16.1M
    if (Template->getDeclContext() &&
203
16.1M
        Template->getDeclContext()->isDependentContext())
204
169k
      D |= TemplateNameDependence::DependentInstantiation;
205
16.1M
  } else {
206
1.88k
    D |= TemplateNameDependence::DependentInstantiation;
207
1.88k
  }
208
16.1M
  return D;
209
16.1M
}
210
211
5.56M
bool TemplateName::isDependent() const {
212
5.56M
  return getDependence() & TemplateNameDependence::Dependent;
213
5.56M
}
214
215
0
bool TemplateName::isInstantiationDependent() const {
216
0
  return getDependence() & TemplateNameDependence::Instantiation;
217
0
}
218
219
8.67k
bool TemplateName::containsUnexpandedParameterPack() const {
220
8.67k
  return getDependence() & TemplateNameDependence::UnexpandedPack;
221
8.67k
}
222
223
void
224
TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
225
128k
                    bool SuppressNNS) const {
226
128k
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
227
128k
    OS << *Template;
228
143
  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
229
49
    if (!SuppressNNS)
230
49
      QTN->getQualifier()->print(OS, Policy);
231
49
    if (QTN->hasTemplateKeyword())
232
19
      OS << "template ";
233
49
    OS << *QTN->getDecl();
234
94
  } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
235
12
    if (!SuppressNNS && DTN->getQualifier())
236
12
      DTN->getQualifier()->print(OS, Policy);
237
12
    OS << "template ";
238
239
12
    if (DTN->isIdentifier())
240
0
      OS << DTN->getIdentifier()->getName();
241
12
    else
242
12
      OS << "operator " << getOperatorSpelling(DTN->getOperator());
243
82
  } else if (SubstTemplateTemplateParmStorage *subst
244
82
               = getAsSubstTemplateTemplateParm()) {
245
79
    subst->getReplacement().print(OS, Policy, SuppressNNS);
246
79
  } else 
if (SubstTemplateTemplateParmPackStorage *3
SubstPack3
247
3
                                        = getAsSubstTemplateTemplateParmPack())
248
0
    OS << *SubstPack->getParameterPack();
249
3
  else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
250
0
    Assumed->getDeclName().print(OS, Policy);
251
3
  } else {
252
3
    OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
253
3
    (*OTS->begin())->printName(OS);
254
3
  }
255
128k
}
256
257
const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
258
429
                                             TemplateName N) {
259
429
  std::string NameStr;
260
429
  llvm::raw_string_ostream OS(NameStr);
261
429
  LangOptions LO;
262
429
  LO.CPlusPlus = true;
263
429
  LO.Bool = true;
264
429
  OS << '\'';
265
429
  N.print(OS, PrintingPolicy(LO));
266
429
  OS << '\'';
267
429
  OS.flush();
268
429
  return DB << NameStr;
269
429
}
270
271
77
void TemplateName::dump(raw_ostream &OS) const {
272
77
  LangOptions LO;  // FIXME!
273
77
  LO.CPlusPlus = true;
274
77
  LO.Bool = true;
275
77
  print(OS, PrintingPolicy(LO));
276
77
}
277
278
0
LLVM_DUMP_METHOD void TemplateName::dump() const {
279
0
  dump(llvm::errs());
280
0
}