Coverage Report

Created: 2022-01-22 13:19

/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.4k
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
41
25.4k
  Profile(ID, Parameter, Replacement);
42
25.4k
}
43
44
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
45
                                           TemplateTemplateParmDecl *parameter,
46
52.8k
                                               TemplateName replacement) {
47
52.8k
  ID.AddPointer(parameter);
48
52.8k
  ID.AddPointer(replacement.getAsVoidPointer());
49
52.8k
}
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.37M
TemplateName::TemplateName(void *Ptr) {
65
3.37M
  Storage = StorageType::getFromOpaqueValue(Ptr);
66
3.37M
}
67
68
13.6M
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
69
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
70
106k
    : Storage(Storage) {}
71
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
72
863
    : Storage(Storage) {}
73
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
74
27.3k
    : Storage(Storage) {}
75
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
76
8
    : Storage(Storage) {}
77
220k
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
78
89.8k
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
79
80
4.03M
bool TemplateName::isNull() const { return Storage.isNull(); }
81
82
37.2M
TemplateName::NameKind TemplateName::getKind() const {
83
37.2M
  if (Storage.is<TemplateDecl *>())
84
36.7M
    return Template;
85
462k
  if (Storage.is<DependentTemplateName *>())
86
9.71k
    return DependentTemplate;
87
453k
  if (Storage.is<QualifiedTemplateName *>())
88
255k
    return QualifiedTemplate;
89
90
197k
  UncommonTemplateNameStorage *uncommon
91
197k
    = Storage.get<UncommonTemplateNameStorage*>();
92
197k
  if (uncommon->getAsOverloadedStorage())
93
109k
    return OverloadedTemplate;
94
87.8k
  if (uncommon->getAsAssumedTemplateName())
95
62
    return AssumedTemplate;
96
87.7k
  if (uncommon->getAsSubstTemplateTemplateParm())
97
87.7k
    return SubstTemplateTemplateParm;
98
22
  return SubstTemplateTemplateParmPack;
99
87.7k
}
100
101
64.3M
TemplateDecl *TemplateName::getAsTemplateDecl() const {
102
64.3M
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
103
63.6M
    return Template;
104
105
749k
  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
106
584k
    return QTN->getTemplateDecl();
107
108
164k
  if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
109
111k
    return sub->getReplacement().getAsTemplateDecl();
110
111
53.0k
  return nullptr;
112
164k
}
113
114
1.27M
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
115
1.27M
  if (UncommonTemplateNameStorage *Uncommon =
116
1.27M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
117
109k
    return Uncommon->getAsOverloadedStorage();
118
119
1.16M
  return nullptr;
120
1.27M
}
121
122
8.72M
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
123
8.72M
  if (UncommonTemplateNameStorage *Uncommon =
124
8.72M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
125
24.3k
    return Uncommon->getAsAssumedTemplateName();
126
127
8.69M
  return nullptr;
128
8.72M
}
129
130
SubstTemplateTemplateParmStorage *
131
6.50M
TemplateName::getAsSubstTemplateTemplateParm() const {
132
6.50M
  if (UncommonTemplateNameStorage *uncommon =
133
6.50M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
134
142k
    return uncommon->getAsSubstTemplateTemplateParm();
135
136
6.36M
  return nullptr;
137
6.50M
}
138
139
SubstTemplateTemplateParmPackStorage *
140
3.55M
TemplateName::getAsSubstTemplateTemplateParmPack() const {
141
3.55M
  if (UncommonTemplateNameStorage *Uncommon =
142
3.55M
          Storage.dyn_cast<UncommonTemplateNameStorage *>())
143
250
    return Uncommon->getAsSubstTemplateTemplateParmPack();
144
145
3.55M
  return nullptr;
146
3.55M
}
147
148
18.4M
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
149
18.4M
  return Storage.dyn_cast<QualifiedTemplateName *>();
150
18.4M
}
151
152
43.6M
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
153
43.6M
  return Storage.dyn_cast<DependentTemplateName *>();
154
43.6M
}
155
156
27.3k
TemplateName TemplateName::getNameToSubstitute() const {
157
27.3k
  TemplateDecl *Decl = getAsTemplateDecl();
158
159
  // Substituting a dependent template name: preserve it as written.
160
27.3k
  if (!Decl)
161
458
    return *this;
162
163
  // If we have a template declaration, use the most recent non-friend
164
  // declaration of that template.
165
26.9k
  Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
166
26.9k
  while (Decl->getFriendObjectKind()) {
167
0
    Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
168
0
    assert(Decl && "all declarations of template are friends");
169
0
  }
170
26.9k
  return TemplateName(Decl);
171
27.3k
}
172
173
16.4M
TemplateNameDependence TemplateName::getDependence() const {
174
16.4M
  auto D = TemplateNameDependence::None;
175
16.4M
  switch (getKind()) {
176
174k
  case TemplateName::NameKind::QualifiedTemplate:
177
174k
    D |= toTemplateNameDependence(
178
174k
        getAsQualifiedTemplateName()->getQualifier()->getDependence());
179
174k
    break;
180
1.01k
  case TemplateName::NameKind::DependentTemplate:
181
1.01k
    D |= toTemplateNameDependence(
182
1.01k
        getAsDependentTemplateName()->getQualifier()->getDependence());
183
1.01k
    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.3M
  default:
190
16.3M
    break;
191
16.4M
  }
192
16.4M
  if (TemplateDecl *Template = getAsTemplateDecl()) {
193
16.4M
    if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) {
194
121k
      D |= TemplateNameDependence::DependentInstantiation;
195
121k
      if (TTP->isParameterPack())
196
186
        D |= TemplateNameDependence::UnexpandedPack;
197
121k
    }
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.4M
    if (Template->getDeclContext() &&
203
16.4M
        Template->getDeclContext()->isDependentContext())
204
157k
      D |= TemplateNameDependence::DependentInstantiation;
205
16.4M
  } else {
206
1.91k
    D |= TemplateNameDependence::DependentInstantiation;
207
1.91k
  }
208
16.4M
  return D;
209
16.4M
}
210
211
5.66M
bool TemplateName::isDependent() const {
212
5.66M
  return getDependence() & TemplateNameDependence::Dependent;
213
5.66M
}
214
215
0
bool TemplateName::isInstantiationDependent() const {
216
0
  return getDependence() & TemplateNameDependence::Instantiation;
217
0
}
218
219
8.84k
bool TemplateName::containsUnexpandedParameterPack() const {
220
8.84k
  return getDependence() & TemplateNameDependence::UnexpandedPack;
221
8.84k
}
222
223
void TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
224
132k
                         Qualified Qual) const {
225
132k
  if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
226
132k
    if (Qual == Qualified::Fully &&
227
132k
        
getDependence() != TemplateNameDependenceScope::DependentInstantiation1.78k
)
228
1.71k
      Template->printQualifiedName(OS, Policy);
229
130k
    else
230
130k
      OS << *Template;
231
331
  else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
232
49
    if (Qual == Qualified::Fully &&
233
49
        getDependence() !=
234
1
            TemplateNameDependenceScope::DependentInstantiation) {
235
1
      QTN->getTemplateDecl()->printQualifiedName(OS, Policy);
236
1
      return;
237
1
    }
238
48
    if (Qual == Qualified::AsWritten)
239
48
      QTN->getQualifier()->print(OS, Policy);
240
48
    if (QTN->hasTemplateKeyword())
241
19
      OS << "template ";
242
48
    OS << *QTN->getDecl();
243
282
  } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
244
12
    if (Qual == Qualified::AsWritten && DTN->getQualifier())
245
12
      DTN->getQualifier()->print(OS, Policy);
246
12
    OS << "template ";
247
248
12
    if (DTN->isIdentifier())
249
0
      OS << DTN->getIdentifier()->getName();
250
12
    else
251
12
      OS << "operator " << getOperatorSpelling(DTN->getOperator());
252
270
  } else if (SubstTemplateTemplateParmStorage *subst
253
270
               = getAsSubstTemplateTemplateParm()) {
254
267
    subst->getReplacement().print(OS, Policy, Qual);
255
267
  } else 
if (SubstTemplateTemplateParmPackStorage *3
SubstPack3
256
3
                                        = getAsSubstTemplateTemplateParmPack())
257
0
    OS << *SubstPack->getParameterPack();
258
3
  else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
259
0
    Assumed->getDeclName().print(OS, Policy);
260
3
  } else {
261
3
    OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
262
3
    (*OTS->begin())->printName(OS);
263
3
  }
264
132k
}
265
266
const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
267
437
                                             TemplateName N) {
268
437
  std::string NameStr;
269
437
  llvm::raw_string_ostream OS(NameStr);
270
437
  LangOptions LO;
271
437
  LO.CPlusPlus = true;
272
437
  LO.Bool = true;
273
437
  OS << '\'';
274
437
  N.print(OS, PrintingPolicy(LO));
275
437
  OS << '\'';
276
437
  OS.flush();
277
437
  return DB << NameStr;
278
437
}
279
280
78
void TemplateName::dump(raw_ostream &OS) const {
281
78
  LangOptions LO;  // FIXME!
282
78
  LO.CPlusPlus = true;
283
78
  LO.Bool = true;
284
78
  print(OS, PrintingPolicy(LO));
285
78
}
286
287
0
LLVM_DUMP_METHOD void TemplateName::dump() const {
288
0
  dump(llvm::errs());
289
0
}