Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclBase.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
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 implements the Decl and DeclContext classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/DeclBase.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/ASTLambda.h"
16
#include "clang/AST/ASTMutationListener.h"
17
#include "clang/AST/Attr.h"
18
#include "clang/AST/AttrIterator.h"
19
#include "clang/AST/Decl.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclContextInternals.h"
22
#include "clang/AST/DeclFriend.h"
23
#include "clang/AST/DeclObjC.h"
24
#include "clang/AST/DeclOpenMP.h"
25
#include "clang/AST/DeclTemplate.h"
26
#include "clang/AST/DependentDiagnostic.h"
27
#include "clang/AST/ExternalASTSource.h"
28
#include "clang/AST/Stmt.h"
29
#include "clang/AST/Type.h"
30
#include "clang/Basic/IdentifierTable.h"
31
#include "clang/Basic/LLVM.h"
32
#include "clang/Basic/LangOptions.h"
33
#include "clang/Basic/ObjCRuntime.h"
34
#include "clang/Basic/PartialDiagnostic.h"
35
#include "clang/Basic/SourceLocation.h"
36
#include "clang/Basic/TargetInfo.h"
37
#include "llvm/ADT/ArrayRef.h"
38
#include "llvm/ADT/PointerIntPair.h"
39
#include "llvm/ADT/SmallVector.h"
40
#include "llvm/ADT/StringRef.h"
41
#include "llvm/Support/Casting.h"
42
#include "llvm/Support/ErrorHandling.h"
43
#include "llvm/Support/MathExtras.h"
44
#include "llvm/Support/VersionTuple.h"
45
#include "llvm/Support/raw_ostream.h"
46
#include <algorithm>
47
#include <cassert>
48
#include <cstddef>
49
#include <string>
50
#include <tuple>
51
#include <utility>
52
53
using namespace clang;
54
55
//===----------------------------------------------------------------------===//
56
//  Statistics
57
//===----------------------------------------------------------------------===//
58
59
#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
60
#define ABSTRACT_DECL(DECL)
61
#include "clang/AST/DeclNodes.inc"
62
63
0
void Decl::updateOutOfDate(IdentifierInfo &II) const {
64
0
  getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
65
0
}
66
67
#define DECL(DERIVED, BASE)                                                    \
68
  static_assert(alignof(Decl) >= alignof(DERIVED##Decl),                       \
69
                "Alignment sufficient after objects prepended to " #DERIVED);
70
#define ABSTRACT_DECL(DECL)
71
#include "clang/AST/DeclNodes.inc"
72
73
void *Decl::operator new(std::size_t Size, const ASTContext &Context,
74
4.67M
                         unsigned ID, std::size_t Extra) {
75
  // Allocate an extra 8 bytes worth of storage, which ensures that the
76
  // resulting pointer will still be 8-byte aligned.
77
4.67M
  static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78
4.67M
                "Decl won't be misaligned");
79
4.67M
  void *Start = Context.Allocate(Size + Extra + 8);
80
4.67M
  void *Result = (char*)Start + 8;
81
4.67M
82
4.67M
  unsigned *PrefixPtr = (unsigned *)Result - 2;
83
4.67M
84
  // Zero out the first 4 bytes; this is used to store the owning module ID.
85
4.67M
  PrefixPtr[0] = 0;
86
4.67M
87
  // Store the global declaration ID in the second 4 bytes.
88
4.67M
  PrefixPtr[1] = ID;
89
4.67M
90
4.67M
  return Result;
91
4.67M
}
92
93
void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
94
90.6M
                         DeclContext *Parent, std::size_t Extra) {
95
90.6M
  assert(!Parent || &Parent->getParentASTContext() == &Ctx);
96
  // With local visibility enabled, we track the owning module even for local
97
  // declarations. We create the TU decl early and may not yet know what the
98
  // LangOpts are, so conservatively allocate the storage.
99
90.6M
  if (Ctx.getLangOpts().trackLocalOwningModule() || 
!Parent87.2M
) {
100
    // Ensure required alignment of the resulting object by adding extra
101
    // padding at the start if required.
102
3.58M
    size_t ExtraAlign =
103
3.58M
        llvm::offsetToAlignment(sizeof(Module *), llvm::Align(alignof(Decl)));
104
3.58M
    auto *Buffer = reinterpret_cast<char *>(
105
3.58M
        ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
106
3.58M
    Buffer += ExtraAlign;
107
3.58M
    auto *ParentModule =
108
3.34M
        Parent ? cast<Decl>(Parent)->getOwningModule() : 
nullptr236k
;
109
3.58M
    return new (Buffer) Module*(ParentModule) + 1;
110
3.58M
  }
111
87.0M
  return ::operator new(Size + Extra, Ctx);
112
87.0M
}
113
114
970k
Module *Decl::getOwningModuleSlow() const {
115
970k
  assert(isFromASTFile() && "Not from AST file?");
116
970k
  return getASTContext().getExternalSource()->getModule(getOwningModuleID());
117
970k
}
118
119
7.97M
bool Decl::hasLocalOwningModuleStorage() const {
120
7.97M
  return getASTContext().getLangOpts().trackLocalOwningModule();
121
7.97M
}
122
123
28.6k
const char *Decl::getDeclKindName() const {
124
28.6k
  switch (DeclKind) {
125
0
  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
126
28.6k
#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
127
0
#define ABSTRACT_DECL(DECL)
128
0
#include "clang/AST/DeclNodes.inc"
129
28.6k
  }
130
28.6k
}
131
132
164k
void Decl::setInvalidDecl(bool Invalid) {
133
164k
  InvalidDecl = Invalid;
134
164k
  assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
135
164k
  if (!Invalid) {
136
125k
    return;
137
125k
  }
138
39.1k
139
39.1k
  if (!isa<ParmVarDecl>(this)) {
140
    // Defensive maneuver for ill-formed code: we're likely not to make it to
141
    // a point where we set the access specifier, so default it to "public"
142
    // to avoid triggering asserts elsewhere in the front end.
143
36.5k
    setAccess(AS_public);
144
36.5k
  }
145
39.1k
146
  // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
147
  // are invalid too.
148
39.1k
  if (auto *DD = dyn_cast<DecompositionDecl>(this)) {
149
100
    for (auto *Binding : DD->bindings()) {
150
100
      Binding->setInvalidDecl();
151
100
    }
152
69
  }
153
39.1k
}
154
155
120
const char *DeclContext::getDeclKindName() const {
156
120
  switch (getDeclKind()) {
157
120
#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
158
0
#define ABSTRACT_DECL(DECL)
159
0
#include "clang/AST/DeclNodes.inc"
160
120
  }
161
120
  
llvm_unreachable0
("Declaration context not in DeclNodes.inc!");
162
120
}
163
164
bool Decl::StatisticsEnabled = false;
165
3
void Decl::EnableStatistics() {
166
3
  StatisticsEnabled = true;
167
3
}
168
169
3
void Decl::PrintStats() {
170
3
  llvm::errs() << "\n*** Decl Stats:\n";
171
3
172
3
  int totalDecls = 0;
173
240
#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
174
3
#define ABSTRACT_DECL(DECL)
175
3
#include "clang/AST/DeclNodes.inc"
176
3
  llvm::errs() << "  " << totalDecls << " decls total.\n";
177
3
178
3
  int totalBytes = 0;
179
3
#define DECL(DERIVED, BASE)                                             \
180
240
  if (n##DERIVED##s > 0) {                                              \
181
21
    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
182
21
    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
183
21
                 << sizeof(DERIVED##Decl) << " each ("                  \
184
21
                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
185
21
                 << " bytes)\n";                                        \
186
21
  }
187
3
#define ABSTRACT_DECL(DECL)
188
3
#include "clang/AST/DeclNodes.inc"
189
3
190
3
  llvm::errs() << "Total bytes = " << totalBytes << "\n";
191
3
}
192
193
61
void Decl::add(Kind k) {
194
61
  switch (k) {
195
61
#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
196
0
#define ABSTRACT_DECL(DECL)
197
0
#include "clang/AST/DeclNodes.inc"
198
61
  }
199
61
}
200
201
36.0M
bool Decl::isTemplateParameterPack() const {
202
36.0M
  if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this))
203
22.6M
    return TTP->isParameterPack();
204
13.3M
  if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this))
205
5.20M
    return NTTP->isParameterPack();
206
8.13M
  if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this))
207
55.3k
    return TTP->isParameterPack();
208
8.07M
  return false;
209
8.07M
}
210
211
20.3M
bool Decl::isParameterPack() const {
212
20.3M
  if (const auto *Var = dyn_cast<VarDecl>(this))
213
15.3M
    return Var->isParameterPack();
214
4.94M
215
4.94M
  return isTemplateParameterPack();
216
4.94M
}
217
218
58.2M
FunctionDecl *Decl::getAsFunction() {
219
58.2M
  if (auto *FD = dyn_cast<FunctionDecl>(this))
220
53.6M
    return FD;
221
4.59M
  if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
222
3.23M
    return FTD->getTemplatedDecl();
223
1.35M
  return nullptr;
224
1.35M
}
225
226
4.30M
bool Decl::isTemplateDecl() const {
227
4.30M
  return isa<TemplateDecl>(this);
228
4.30M
}
229
230
4.59M
TemplateDecl *Decl::getDescribedTemplate() const {
231
4.59M
  if (auto *FD = dyn_cast<FunctionDecl>(this))
232
320k
    return FD->getDescribedFunctionTemplate();
233
4.27M
  else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
234
341k
    return RD->getDescribedClassTemplate();
235
3.93M
  else if (auto *VD = dyn_cast<VarDecl>(this))
236
1.77M
    return VD->getDescribedVarTemplate();
237
2.15M
  else if (auto *AD = dyn_cast<TypeAliasDecl>(this))
238
2.30k
    return AD->getDescribedAliasTemplate();
239
2.14M
240
2.14M
  return nullptr;
241
2.14M
}
242
243
4.15M
const TemplateParameterList *Decl::getDescribedTemplateParams() const {
244
4.15M
  if (auto *TD = getDescribedTemplate())
245
333k
    return TD->getTemplateParameters();
246
3.82M
  if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(this))
247
75.8k
    return CTPSD->getTemplateParameters();
248
3.74M
  if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(this))
249
13
    return VTPSD->getTemplateParameters();
250
3.74M
  return nullptr;
251
3.74M
}
252
253
17.0M
bool Decl::isTemplated() const {
254
  // A declaration is templated if it is a template or a template pattern, or
255
  // is within (lexcially for a friend, semantically otherwise) a dependent
256
  // context.
257
  // FIXME: Should local extern declarations be treated like friends?
258
17.0M
  if (auto *AsDC = dyn_cast<DeclContext>(this))
259
12.7M
    return AsDC->isDependentContext();
260
4.29M
  auto *DC = getFriendObjectKind() ? 
getLexicalDeclContext()0
: getDeclContext();
261
4.29M
  return DC->isDependentContext() || 
isTemplateDecl()4.22M
||
262
3.73M
         getDescribedTemplateParams();
263
4.29M
}
264
265
31.3k
unsigned Decl::getTemplateDepth() const {
266
31.3k
  if (auto *DC = dyn_cast<DeclContext>(this))
267
31.3k
    if (DC->isFileContext())
268
62
      return 0;
269
31.3k
270
31.3k
  if (auto *TPL = getDescribedTemplateParams())
271
31.2k
    return TPL->getDepth() + 1;
272
69
273
  // If this is a dependent lambda, there might be an enclosing variable
274
  // template. In this case, the next step is not the parent DeclContext (or
275
  // even a DeclContext at all).
276
69
  auto *RD = dyn_cast<CXXRecordDecl>(this);
277
69
  if (RD && 
RD->isDependentLambda()66
)
278
0
    if (Decl *Context = RD->getLambdaContextDecl())
279
0
      return Context->getTemplateDepth();
280
69
281
69
  const DeclContext *DC =
282
69
      getFriendObjectKind() ? 
getLexicalDeclContext()0
: getDeclContext();
283
69
  return cast<Decl>(DC)->getTemplateDepth();
284
69
}
285
286
1.66M
const DeclContext *Decl::getParentFunctionOrMethod() const {
287
1.66M
  for (const DeclContext *DC = getDeclContext();
288
1.71M
       DC && 
!DC->isTranslationUnit()1.70M
&&
!DC->isNamespace()1.63M
;
289
48.5k
       DC = DC->getParent())
290
89.1k
    if (DC->isFunctionOrMethod())
291
40.5k
      return DC;
292
1.66M
293
1.62M
  return nullptr;
294
1.66M
}
295
296
//===----------------------------------------------------------------------===//
297
// PrettyStackTraceDecl Implementation
298
//===----------------------------------------------------------------------===//
299
300
0
void PrettyStackTraceDecl::print(raw_ostream &OS) const {
301
0
  SourceLocation TheLoc = Loc;
302
0
  if (TheLoc.isInvalid() && TheDecl)
303
0
    TheLoc = TheDecl->getLocation();
304
0
305
0
  if (TheLoc.isValid()) {
306
0
    TheLoc.print(OS, SM);
307
0
    OS << ": ";
308
0
  }
309
0
310
0
  OS << Message;
311
0
312
0
  if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
313
0
    OS << " '";
314
0
    DN->printQualifiedName(OS);
315
0
    OS << '\'';
316
0
  }
317
0
  OS << '\n';
318
0
}
319
320
//===----------------------------------------------------------------------===//
321
// Decl Implementation
322
//===----------------------------------------------------------------------===//
323
324
// Out-of-line virtual method providing a home for Decl.
325
9.31k
Decl::~Decl() = default;
326
327
50.7M
void Decl::setDeclContext(DeclContext *DC) {
328
50.7M
  DeclCtx = DC;
329
50.7M
}
330
331
19.8M
void Decl::setLexicalDeclContext(DeclContext *DC) {
332
19.8M
  if (DC == getLexicalDeclContext())
333
19.4M
    return;
334
408k
335
408k
  if (isInSemaDC()) {
336
399k
    setDeclContextsImpl(getDeclContext(), DC, getASTContext());
337
9.26k
  } else {
338
9.26k
    getMultipleDC()->LexicalDC = DC;
339
9.26k
  }
340
408k
341
  // FIXME: We shouldn't be changing the lexical context of declarations
342
  // imported from AST files.
343
408k
  if (!isFromASTFile()) {
344
408k
    setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
345
408k
    if (hasOwningModule())
346
42.8k
      setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
347
408k
  }
348
408k
349
408k
  assert(
350
408k
      (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||
351
408k
       getOwningModule()) &&
352
408k
      "hidden declaration has no owning module");
353
408k
}
354
355
void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
356
4.91M
                               ASTContext &Ctx) {
357
4.91M
  if (SemaDC == LexicalDC) {
358
4.35M
    DeclCtx = SemaDC;
359
559k
  } else {
360
559k
    auto *MDC = new (Ctx) Decl::MultipleDC();
361
559k
    MDC->SemanticDC = SemaDC;
362
559k
    MDC->LexicalDC = LexicalDC;
363
559k
    DeclCtx = MDC;
364
559k
  }
365
4.91M
}
366
367
131k
bool Decl::isInLocalScopeForInstantiation() const {
368
131k
  const DeclContext *LDC = getLexicalDeclContext();
369
131k
  if (!LDC->isDependentContext())
370
7.54k
    return false;
371
248k
  
while (123k
true) {
372
248k
    if (LDC->isFunctionOrMethod())
373
25
      return true;
374
248k
    if (!isa<TagDecl>(LDC))
375
123k
      return false;
376
124k
    if (const auto *CRD = dyn_cast<CXXRecordDecl>(LDC))
377
124k
      if (CRD->isLambda())
378
22
        return true;
379
124k
    LDC = LDC->getLexicalParent();
380
124k
  }
381
0
  return false;
382
123k
}
383
384
7.70M
bool Decl::isInAnonymousNamespace() const {
385
19.7M
  for (const DeclContext *DC = getDeclContext(); DC; 
DC = DC->getParent()12.0M
) {
386
12.0M
    if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
387
3.26M
      if (ND->isAnonymousNamespace())
388
4.79k
        return true;
389
12.0M
  }
390
7.70M
391
7.69M
  return false;
392
7.70M
}
393
394
1.02M
bool Decl::isInStdNamespace() const {
395
1.02M
  const DeclContext *DC = getDeclContext();
396
1.02M
  return DC && DC->isStdNamespace();
397
1.02M
}
398
399
1.78G
TranslationUnitDecl *Decl::getTranslationUnitDecl() {
400
1.78G
  if (auto *TUD = dyn_cast<TranslationUnitDecl>(this))
401
118M
    return TUD;
402
1.66G
403
1.66G
  DeclContext *DC = getDeclContext();
404
1.66G
  assert(DC && "This decl is not contained in a translation unit!");
405
1.66G
406
2.79G
  while (!DC->isTranslationUnit()) {
407
1.13G
    DC = DC->getParent();
408
1.13G
    assert(DC && "This decl is not contained in a translation unit!");
409
1.13G
  }
410
1.66G
411
1.66G
  return cast<TranslationUnitDecl>(DC);
412
1.66G
}
413
414
1.78G
ASTContext &Decl::getASTContext() const {
415
1.78G
  return getTranslationUnitDecl()->getASTContext();
416
1.78G
}
417
418
/// Helper to get the language options from the ASTContext.
419
/// Defined out of line to avoid depending on ASTContext.h.
420
1.51M
const LangOptions &Decl::getLangOpts() const {
421
1.51M
  return getASTContext().getLangOpts();
422
1.51M
}
423
424
32.9M
ASTMutationListener *Decl::getASTMutationListener() const {
425
32.9M
  return getASTContext().getASTMutationListener();
426
32.9M
}
427
428
3.23M
unsigned Decl::getMaxAlignment() const {
429
3.23M
  if (!hasAttrs())
430
2.93M
    return 0;
431
302k
432
302k
  unsigned Align = 0;
433
302k
  const AttrVec &V = getAttrs();
434
302k
  ASTContext &Ctx = getASTContext();
435
302k
  specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
436
314k
  for (; I != E; 
++I12.4k
) {
437
12.4k
    if (!I->isAlignmentErrorDependent())
438
12.4k
      Align = std::max(Align, I->getAlignment(Ctx));
439
12.4k
  }
440
302k
  return Align;
441
302k
}
442
443
59.8M
bool Decl::isUsed(bool CheckUsedAttr) const {
444
59.8M
  const Decl *CanonD = getCanonicalDecl();
445
59.8M
  if (CanonD->Used)
446
8.52M
    return true;
447
51.2M
448
  // Check for used attribute.
449
  // Ask the most recent decl, since attributes accumulate in the redecl chain.
450
51.2M
  if (CheckUsedAttr && 
getMostRecentDecl()->hasAttr<UsedAttr>()35.0M
)
451
963
    return true;
452
51.2M
453
  // The information may have not been deserialized yet. Force deserialization
454
  // to complete the needed information.
455
51.2M
  return getMostRecentDecl()->getCanonicalDecl()->Used;
456
51.2M
}
457
458
11.1M
void Decl::markUsed(ASTContext &C) {
459
11.1M
  if (isUsed(false))
460
3.40M
    return;
461
7.73M
462
7.73M
  if (C.getASTMutationListener())
463
330k
    C.getASTMutationListener()->DeclarationMarkedUsed(this);
464
7.73M
465
7.73M
  setIsUsed();
466
7.73M
}
467
468
39.3M
bool Decl::isReferenced() const {
469
39.3M
  if (Referenced)
470
15.7M
    return true;
471
23.5M
472
  // Check redeclarations.
473
23.5M
  for (const auto *I : redecls())
474
24.6M
    if (I->Referenced)
475
6.94k
      return true;
476
23.5M
477
23.5M
  return false;
478
23.5M
}
479
480
30.4k
ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
481
30.4k
  const Decl *Definition = nullptr;
482
30.4k
  if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
483
1.48k
    Definition = ID->getDefinition();
484
28.9k
  } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) {
485
172
    Definition = PD->getDefinition();
486
28.7k
  } else if (auto *TD = dyn_cast<TagDecl>(this)) {
487
14.4k
    Definition = TD->getDefinition();
488
14.4k
  }
489
30.4k
  if (!Definition)
490
14.5k
    Definition = this;
491
30.4k
492
30.4k
  if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
493
71
    return attr;
494
30.3k
  if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
495
30.3k
    return dcd->getAttr<ExternalSourceSymbolAttr>();
496
30.3k
  }
497
0
498
0
  return nullptr;
499
0
}
500
501
21.5M
bool Decl::hasDefiningAttr() const {
502
21.5M
  return hasAttr<AliasAttr>() || 
hasAttr<IFuncAttr>()21.5M
||
503
21.5M
         hasAttr<LoaderUninitializedAttr>();
504
21.5M
}
505
506
116
const Attr *Decl::getDefiningAttr() const {
507
116
  if (auto *AA = getAttr<AliasAttr>())
508
106
    return AA;
509
10
  if (auto *IFA = getAttr<IFuncAttr>())
510
10
    return IFA;
511
0
  if (auto *NZA = getAttr<LoaderUninitializedAttr>())
512
0
    return NZA;
513
0
  return nullptr;
514
0
}
515
516
static StringRef getRealizedPlatform(const AvailabilityAttr *A,
517
5.71M
                                     const ASTContext &Context) {
518
  // Check if this is an App Extension "platform", and if so chop off
519
  // the suffix for matching with the actual platform.
520
5.71M
  StringRef RealizedPlatform = A->getPlatform()->getName();
521
5.71M
  if (!Context.getLangOpts().AppExt)
522
5.71M
    return RealizedPlatform;
523
48
  size_t suffix = RealizedPlatform.rfind("_app_extension");
524
48
  if (suffix != StringRef::npos)
525
28
    return RealizedPlatform.slice(0, suffix);
526
20
  return RealizedPlatform;
527
20
}
528
529
/// Determine the availability of the given declaration based on
530
/// the target platform.
531
///
532
/// When it returns an availability result other than \c AR_Available,
533
/// if the \p Message parameter is non-NULL, it will be set to a
534
/// string describing why the entity is unavailable.
535
///
536
/// FIXME: Make these strings localizable, since they end up in
537
/// diagnostics.
538
static AvailabilityResult CheckAvailability(ASTContext &Context,
539
                                            const AvailabilityAttr *A,
540
                                            std::string *Message,
541
5.71M
                                            VersionTuple EnclosingVersion) {
542
5.71M
  if (EnclosingVersion.empty())
543
5.71M
    EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
544
5.71M
545
5.71M
  if (EnclosingVersion.empty())
546
26
    return AR_Available;
547
5.71M
548
5.71M
  StringRef ActualPlatform = A->getPlatform()->getName();
549
5.71M
  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
550
5.71M
551
  // Match the platform name.
552
5.71M
  if (getRealizedPlatform(A, Context) != TargetPlatform)
553
3.73M
    return AR_Available;
554
1.97M
555
1.97M
  StringRef PrettyPlatformName
556
1.97M
    = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
557
1.97M
558
1.97M
  if (PrettyPlatformName.empty())
559
0
    PrettyPlatformName = ActualPlatform;
560
1.97M
561
1.97M
  std::string HintMessage;
562
1.97M
  if (!A->getMessage().empty()) {
563
391k
    HintMessage = " - ";
564
391k
    HintMessage += A->getMessage();
565
391k
  }
566
1.97M
567
  // Make sure that this declaration has not been marked 'unavailable'.
568
1.97M
  if (A->getUnavailable()) {
569
168k
    if (Message) {
570
409
      Message->clear();
571
409
      llvm::raw_string_ostream Out(*Message);
572
409
      Out << "not available on " << PrettyPlatformName
573
409
          << HintMessage;
574
409
    }
575
168k
576
168k
    return AR_Unavailable;
577
168k
  }
578
1.80M
579
  // Make sure that this declaration has already been introduced.
580
1.80M
  if (!A->getIntroduced().empty() &&
581
1.80M
      EnclosingVersion < A->getIntroduced()) {
582
1.27k
    if (Message) {
583
379
      Message->clear();
584
379
      llvm::raw_string_ostream Out(*Message);
585
379
      VersionTuple VTI(A->getIntroduced());
586
379
      Out << "introduced in " << PrettyPlatformName << ' '
587
379
          << VTI << HintMessage;
588
379
    }
589
1.27k
590
1.26k
    return A->getStrict() ? 
AR_Unavailable6
: AR_NotYetIntroduced;
591
1.27k
  }
592
1.80M
593
  // Make sure that this declaration hasn't been obsoleted.
594
1.80M
  if (!A->getObsoleted().empty() && 
EnclosingVersion >= A->getObsoleted()21
) {
595
21
    if (Message) {
596
10
      Message->clear();
597
10
      llvm::raw_string_ostream Out(*Message);
598
10
      VersionTuple VTO(A->getObsoleted());
599
10
      Out << "obsoleted in " << PrettyPlatformName << ' '
600
10
          << VTO << HintMessage;
601
10
    }
602
21
603
21
    return AR_Unavailable;
604
21
  }
605
1.80M
606
  // Make sure that this declaration hasn't been deprecated.
607
1.80M
  if (!A->getDeprecated().empty() && 
EnclosingVersion >= A->getDeprecated()1.02M
) {
608
1.01M
    if (Message) {
609
344k
      Message->clear();
610
344k
      llvm::raw_string_ostream Out(*Message);
611
344k
      VersionTuple VTD(A->getDeprecated());
612
344k
      Out << "first deprecated in " << PrettyPlatformName << ' '
613
344k
          << VTD << HintMessage;
614
344k
    }
615
1.01M
616
1.01M
    return AR_Deprecated;
617
1.01M
  }
618
791k
619
791k
  return AR_Available;
620
791k
}
621
622
AvailabilityResult Decl::getAvailability(std::string *Message,
623
                                         VersionTuple EnclosingVersion,
624
88.1M
                                         StringRef *RealizedPlatform) const {
625
88.1M
  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
626
47.1k
    return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
627
47.1k
                                                    RealizedPlatform);
628
88.1M
629
88.1M
  AvailabilityResult Result = AR_Available;
630
88.1M
  std::string ResultMessage;
631
88.1M
632
21.3M
  for (const auto *A : attrs()) {
633
21.3M
    if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
634
5.73k
      if (Result >= AR_Deprecated)
635
57
        continue;
636
5.67k
637
5.67k
      if (Message)
638
4.63k
        ResultMessage = std::string(Deprecated->getMessage());
639
5.67k
640
5.67k
      Result = AR_Deprecated;
641
5.67k
      continue;
642
5.67k
    }
643
21.3M
644
21.3M
    if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
645
101k
      if (Message)
646
9.78k
        *Message = std::string(Unavailable->getMessage());
647
101k
      return AR_Unavailable;
648
101k
    }
649
21.2M
650
21.2M
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
651
5.68M
      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
652
5.68M
                                                Message, EnclosingVersion);
653
5.68M
654
5.68M
      if (AR == AR_Unavailable) {
655
168k
        if (RealizedPlatform)
656
5
          *RealizedPlatform = Availability->getPlatform()->getName();
657
168k
        return AR_Unavailable;
658
168k
      }
659
5.51M
660
5.51M
      if (AR > Result) {
661
1.01M
        Result = AR;
662
1.01M
        if (Message)
663
345k
          ResultMessage.swap(*Message);
664
1.01M
      }
665
5.51M
      continue;
666
5.51M
    }
667
21.2M
  }
668
88.1M
669
87.8M
  if (Message)
670
84.6M
    Message->swap(ResultMessage);
671
87.8M
  return Result;
672
88.1M
}
673
674
4
VersionTuple Decl::getVersionIntroduced() const {
675
4
  const ASTContext &Context = getASTContext();
676
4
  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
677
4
  for (const auto *A : attrs()) {
678
4
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
679
4
      if (getRealizedPlatform(Availability, Context) != TargetPlatform)
680
0
        continue;
681
4
      if (!Availability->getIntroduced().empty())
682
4
        return Availability->getIntroduced();
683
4
    }
684
4
  }
685
0
  return {};
686
4
}
687
688
5.00M
bool Decl::canBeWeakImported(bool &IsDefinition) const {
689
5.00M
  IsDefinition = false;
690
5.00M
691
  // Variables, if they aren't definitions.
692
5.00M
  if (const auto *Var = dyn_cast<VarDecl>(this)) {
693
4.59M
    if (Var->isThisDeclarationADefinition()) {
694
353k
      IsDefinition = true;
695
353k
      return false;
696
353k
    }
697
4.24M
    return true;
698
4.24M
699
  // Functions, if they aren't definitions.
700
406k
  } else if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
701
376k
    if (FD->hasBody()) {
702
256k
      IsDefinition = true;
703
256k
      return false;
704
256k
    }
705
119k
    return true;
706
119k
707
  // Objective-C classes, if this is the non-fragile runtime.
708
30.1k
  } else if (isa<ObjCInterfaceDecl>(this) &&
709
22.7k
             getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
710
22.6k
    return true;
711
22.6k
712
  // Nothing else.
713
7.49k
  } else {
714
7.49k
    return false;
715
7.49k
  }
716
5.00M
}
717
718
5.00M
bool Decl::isWeakImported() const {
719
5.00M
  bool IsDefinition;
720
5.00M
  if (!canBeWeakImported(IsDefinition))
721
617k
    return false;
722
4.38M
723
4.38M
  for (const auto *A : getMostRecentDecl()->attrs()) {
724
74.6k
    if (isa<WeakImportAttr>(A))
725
155
      return true;
726
74.4k
727
74.4k
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
728
32.3k
      if (CheckAvailability(getASTContext(), Availability, nullptr,
729
32.3k
                            VersionTuple()) == AR_NotYetIntroduced)
730
39
        return true;
731
32.3k
    }
732
74.4k
  }
733
4.38M
734
4.38M
  return false;
735
4.38M
}
736
737
95.3M
unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
738
95.3M
  switch (DeclKind) {
739
73.2M
    case Function:
740
73.2M
    case CXXDeductionGuide:
741
73.2M
    case CXXMethod:
742
73.2M
    case CXXConstructor:
743
73.2M
    case ConstructorUsingShadow:
744
73.2M
    case CXXDestructor:
745
73.2M
    case CXXConversion:
746
73.2M
    case EnumConstant:
747
73.2M
    case Var:
748
73.2M
    case ImplicitParam:
749
73.2M
    case ParmVar:
750
73.2M
    case ObjCMethod:
751
73.2M
    case ObjCProperty:
752
73.2M
    case MSProperty:
753
73.2M
      return IDNS_Ordinary;
754
4.38k
    case Label:
755
4.38k
      return IDNS_Label;
756
7.78k
    case IndirectField:
757
7.78k
      return IDNS_Ordinary | IDNS_Member;
758
73.2M
759
392k
    case Binding:
760
392k
    case NonTypeTemplateParm:
761
392k
    case VarTemplate:
762
392k
    case Concept:
763
      // These (C++-only) declarations are found by redeclaration lookup for
764
      // tag types, so we include them in the tag namespace.
765
392k
      return IDNS_Ordinary | IDNS_Tag;
766
392k
767
354k
    case ObjCCompatibleAlias:
768
354k
    case ObjCInterface:
769
354k
      return IDNS_Ordinary | IDNS_Type;
770
354k
771
6.45M
    case Typedef:
772
6.45M
    case TypeAlias:
773
6.45M
    case TemplateTypeParm:
774
6.45M
    case ObjCTypeParam:
775
6.45M
      return IDNS_Ordinary | IDNS_Type;
776
6.45M
777
681
    case UnresolvedUsingTypename:
778
681
      return IDNS_Ordinary | IDNS_Type | IDNS_Using;
779
6.45M
780
158k
    case UsingShadow:
781
158k
      return 0; // we'll actually overwrite this later
782
6.45M
783
559
    case UnresolvedUsingValue:
784
559
      return IDNS_Ordinary | IDNS_Using;
785
6.45M
786
126k
    case Using:
787
126k
    case UsingPack:
788
126k
      return IDNS_Using;
789
126k
790
32.4k
    case ObjCProtocol:
791
32.4k
      return IDNS_ObjCProtocol;
792
126k
793
4.63M
    case Field:
794
4.63M
    case ObjCAtDefsField:
795
4.63M
    case ObjCIvar:
796
4.63M
      return IDNS_Member;
797
4.63M
798
4.81M
    case Record:
799
4.81M
    case CXXRecord:
800
4.81M
    case Enum:
801
4.81M
      return IDNS_Tag | IDNS_Type;
802
4.81M
803
265k
    case Namespace:
804
265k
    case NamespaceAlias:
805
265k
      return IDNS_Namespace;
806
265k
807
854k
    case FunctionTemplate:
808
854k
      return IDNS_Ordinary;
809
265k
810
465k
    case ClassTemplate:
811
465k
    case TemplateTemplateParm:
812
465k
    case TypeAliasTemplate:
813
465k
      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
814
465k
815
884
    case OMPDeclareReduction:
816
884
      return IDNS_OMPReduction;
817
465k
818
402
    case OMPDeclareMapper:
819
402
      return IDNS_OMPMapper;
820
465k
821
    // Never have names.
822
3.45M
    case Friend:
823
3.45M
    case FriendTemplate:
824
3.45M
    case AccessSpec:
825
3.45M
    case LinkageSpec:
826
3.45M
    case Export:
827
3.45M
    case FileScopeAsm:
828
3.45M
    case StaticAssert:
829
3.45M
    case ObjCPropertyImpl:
830
3.45M
    case PragmaComment:
831
3.45M
    case PragmaDetectMismatch:
832
3.45M
    case Block:
833
3.45M
    case Captured:
834
3.45M
    case TranslationUnit:
835
3.45M
    case ExternCContext:
836
3.45M
    case Decomposition:
837
3.45M
    case MSGuid:
838
3.45M
839
3.45M
    case UsingDirective:
840
3.45M
    case BuiltinTemplate:
841
3.45M
    case ClassTemplateSpecialization:
842
3.45M
    case ClassTemplatePartialSpecialization:
843
3.45M
    case ClassScopeFunctionSpecialization:
844
3.45M
    case VarTemplateSpecialization:
845
3.45M
    case VarTemplatePartialSpecialization:
846
3.45M
    case ObjCImplementation:
847
3.45M
    case ObjCCategory:
848
3.45M
    case ObjCCategoryImpl:
849
3.45M
    case Import:
850
3.45M
    case OMPThreadPrivate:
851
3.45M
    case OMPAllocate:
852
3.45M
    case OMPRequires:
853
3.45M
    case OMPCapturedExpr:
854
3.45M
    case Empty:
855
3.45M
    case LifetimeExtendedTemporary:
856
3.45M
    case RequiresExprBody:
857
      // Never looked up by name.
858
3.45M
      return 0;
859
0
  }
860
0
861
0
  llvm_unreachable("Invalid DeclKind!");
862
0
}
863
864
19.2M
void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
865
19.2M
  assert(!HasAttrs && "Decl already contains attrs.");
866
19.2M
867
19.2M
  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
868
19.2M
  assert(AttrBlank.empty() && "HasAttrs was wrong?");
869
19.2M
870
19.2M
  AttrBlank = attrs;
871
19.2M
  HasAttrs = true;
872
19.2M
}
873
874
5.92k
void Decl::dropAttrs() {
875
5.92k
  if (!HasAttrs) 
return0
;
876
5.92k
877
5.92k
  HasAttrs = false;
878
5.92k
  getASTContext().eraseDeclAttrs(this);
879
5.92k
}
880
881
58.5M
void Decl::addAttr(Attr *A) {
882
58.5M
  if (!hasAttrs()) {
883
18.5M
    setAttrs(AttrVec(1, A));
884
18.5M
    return;
885
18.5M
  }
886
40.0M
887
40.0M
  AttrVec &Attrs = getAttrs();
888
40.0M
  if (!A->isInherited()) {
889
39.7M
    Attrs.push_back(A);
890
39.7M
    return;
891
39.7M
  }
892
321k
893
  // Attribute inheritance is processed after attribute parsing. To keep the
894
  // order as in the source code, add inherited attributes before non-inherited
895
  // ones.
896
321k
  auto I = Attrs.begin(), E = Attrs.end();
897
449k
  for (; I != E; 
++I128k
) {
898
141k
    if (!(*I)->isInherited())
899
12.9k
      break;
900
141k
  }
901
321k
  Attrs.insert(I, A);
902
321k
}
903
904
1.12G
const AttrVec &Decl::getAttrs() const {
905
1.12G
  assert(HasAttrs && "No attrs to get!");
906
1.12G
  return getASTContext().getDeclAttrs(this);
907
1.12G
}
908
909
2.04G
Decl *Decl::castFromDeclContext (const DeclContext *D) {
910
2.04G
  Decl::Kind DK = D->getDeclKind();
911
2.04G
  switch(DK) {
912
0
#define DECL(NAME, BASE)
913
0
#define DECL_CONTEXT(NAME) \
914
1.32G
    case Decl::NAME:       \
915
1.32G
      return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
916
0
#define DECL_CONTEXT_BASE(NAME)
917
0
#include "clang/AST/DeclNodes.inc"
918
726M
    default:
919
726M
#define DECL(NAME, BASE)
920
726M
#define DECL_CONTEXT_BASE(NAME)                  \
921
1.30G
      if (DK >= first##NAME && 
DK <= last726M
##NAME) \
922
726M
        return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
923
726M
#include "clang/AST/DeclNodes.inc"
924
18.4E
      llvm_unreachable("a decl that inherits DeclContext isn't handled");
925
2.04G
  }
926
2.04G
}
927
928
37.7M
DeclContext *Decl::castToDeclContext(const Decl *D) {
929
37.7M
  Decl::Kind DK = D->getKind();
930
37.7M
  switch(DK) {
931
0
#define DECL(NAME, BASE)
932
0
#define DECL_CONTEXT(NAME) \
933
3.06M
    case Decl::NAME:       \
934
3.06M
      return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
935
0
#define DECL_CONTEXT_BASE(NAME)
936
0
#include "clang/AST/DeclNodes.inc"
937
34.7M
    default:
938
34.7M
#define DECL(NAME, BASE)
939
34.7M
#define DECL_CONTEXT_BASE(NAME)                                   \
940
62.1M
      if (DK >= first##NAME && 
DK <= last34.7M
##NAME) \
941
34.7M
        return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
942
34.7M
#include "clang/AST/DeclNodes.inc"
943
0
      llvm_unreachable("a decl that inherits DeclContext isn't handled");
944
37.7M
  }
945
37.7M
}
946
947
24.8k
SourceLocation Decl::getBodyRBrace() const {
948
  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
949
  // FunctionDecl stores EndRangeLoc for this purpose.
950
24.8k
  if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
951
150
    const FunctionDecl *Definition;
952
150
    if (FD->hasBody(Definition))
953
150
      return Definition->getSourceRange().getEnd();
954
0
    return {};
955
0
  }
956
24.7k
957
24.7k
  if (Stmt *Body = getBody())
958
24.7k
    return Body->getSourceRange().getEnd();
959
0
960
0
  return {};
961
0
}
962
963
210M
bool Decl::AccessDeclContextSanity() const {
964
210M
#ifndef NDEBUG
965
  // Suppress this check if any of the following hold:
966
  // 1. this is the translation unit (and thus has no parent)
967
  // 2. this is a template parameter (and thus doesn't belong to its context)
968
  // 3. this is a non-type template parameter
969
  // 4. the context is not a record
970
  // 5. it's invalid
971
  // 6. it's a C++0x static_assert.
972
  // 7. it's a block literal declaration
973
210M
  if (isa<TranslationUnitDecl>(this) ||
974
210M
      isa<TemplateTypeParmDecl>(this) ||
975
204M
      isa<NonTypeTemplateParmDecl>(this) ||
976
203M
      !getDeclContext() ||
977
203M
      !isa<CXXRecordDecl>(getDeclContext()) ||
978
37.3M
      isInvalidDecl() ||
979
37.3M
      isa<StaticAssertDecl>(this) ||
980
37.3M
      isa<BlockDecl>(this) ||
981
      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
982
      // as DeclContext (?).
983
37.3M
      isa<ParmVarDecl>(this) ||
984
      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
985
      // AS_none as access specifier.
986
37.3M
      isa<CXXRecordDecl>(this) ||
987
33.2M
      isa<ClassScopeFunctionSpecializationDecl>(this))
988
177M
    return true;
989
33.2M
990
33.2M
  assert(Access != AS_none &&
991
33.2M
         "Access specifier is AS_none inside a record decl");
992
33.2M
#endif
993
33.2M
  return true;
994
33.2M
}
995
996
280k
static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
997
84.1k
static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
998
999
0
int64_t Decl::getID() const {
1000
0
  return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(this);
1001
0
}
1002
1003
9.60M
const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
1004
9.60M
  QualType Ty;
1005
9.60M
  if (const auto *D = dyn_cast<ValueDecl>(this))
1006
9.58M
    Ty = D->getType();
1007
14.1k
  else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
1008
2.35k
    Ty = D->getUnderlyingType();
1009
11.7k
  else
1010
11.7k
    return nullptr;
1011
9.59M
1012
9.59M
  if (Ty->isFunctionPointerType())
1013
111
    Ty = Ty->castAs<PointerType>()->getPointeeType();
1014
9.59M
  else if (Ty->isFunctionReferenceType())
1015
4
    Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1016
9.59M
  else if (BlocksToo && 
Ty->isBlockPointerType()1.59M
)
1017
2.40k
    Ty = Ty->castAs<BlockPointerType>()->getPointeeType();
1018
9.59M
1019
9.59M
  return Ty->getAs<FunctionType>();
1020
9.59M
}
1021
1022
/// Starting at a given context (a Decl or DeclContext), look for a
1023
/// code context that is not a closure (a lambda, block, etc.).
1024
364k
template <class T> static Decl *getNonClosureContext(T *D) {
1025
364k
  if (getKind(D) == Decl::CXXMethod) {
1026
52.7k
    auto *MD = cast<CXXMethodDecl>(D);
1027
52.7k
    if (MD->getOverloadedOperator() == OO_Call &&
1028
4.85k
        MD->getParent()->isLambda())
1029
2.94k
      return getNonClosureContext(MD->getParent()->getParent());
1030
49.7k
    return MD;
1031
311k
  } else if (auto *FD = dyn_cast<FunctionDecl>(D))
1032
236k
    return FD;
1033
75.0k
  else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1034
6.37k
    return MD;
1035
68.6k
  else if (auto *BD = dyn_cast<BlockDecl>(D))
1036
1.48k
    return getNonClosureContext(BD->getParent());
1037
67.1k
  else if (auto *CD = dyn_cast<CapturedDecl>(D))
1038
60.7k
    return getNonClosureContext(CD->getParent());
1039
6.39k
  else
1040
6.39k
    return nullptr;
1041
364k
}
DeclBase.cpp:clang::Decl* getNonClosureContext<clang::Decl>(clang::Decl*)
Line
Count
Source
1024
280k
template <class T> static Decl *getNonClosureContext(T *D) {
1025
280k
  if (getKind(D) == Decl::CXXMethod) {
1026
48.0k
    auto *MD = cast<CXXMethodDecl>(D);
1027
48.0k
    if (MD->getOverloadedOperator() == OO_Call &&
1028
3.40k
        MD->getParent()->isLambda())
1029
1.48k
      return getNonClosureContext(MD->getParent()->getParent());
1030
46.5k
    return MD;
1031
232k
  } else if (auto *FD = dyn_cast<FunctionDecl>(D))
1032
198k
    return FD;
1033
33.3k
  else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1034
2.54k
    return MD;
1035
30.8k
  else if (auto *BD = dyn_cast<BlockDecl>(D))
1036
1.09k
    return getNonClosureContext(BD->getParent());
1037
29.7k
  else if (auto *CD = dyn_cast<CapturedDecl>(D))
1038
23.4k
    return getNonClosureContext(CD->getParent());
1039
6.23k
  else
1040
6.23k
    return nullptr;
1041
280k
}
DeclBase.cpp:clang::Decl* getNonClosureContext<clang::DeclContext>(clang::DeclContext*)
Line
Count
Source
1024
84.1k
template <class T> static Decl *getNonClosureContext(T *D) {
1025
84.1k
  if (getKind(D) == Decl::CXXMethod) {
1026
4.73k
    auto *MD = cast<CXXMethodDecl>(D);
1027
4.73k
    if (MD->getOverloadedOperator() == OO_Call &&
1028
1.45k
        MD->getParent()->isLambda())
1029
1.45k
      return getNonClosureContext(MD->getParent()->getParent());
1030
3.28k
    return MD;
1031
79.3k
  } else if (auto *FD = dyn_cast<FunctionDecl>(D))
1032
37.7k
    return FD;
1033
41.6k
  else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1034
3.83k
    return MD;
1035
37.8k
  else if (auto *BD = dyn_cast<BlockDecl>(D))
1036
387
    return getNonClosureContext(BD->getParent());
1037
37.4k
  else if (auto *CD = dyn_cast<CapturedDecl>(D))
1038
37.2k
    return getNonClosureContext(CD->getParent());
1039
159
  else
1040
159
    return nullptr;
1041
84.1k
}
1042
1043
280k
Decl *Decl::getNonClosureContext() {
1044
280k
  return ::getNonClosureContext(this);
1045
280k
}
1046
1047
18.9k
Decl *DeclContext::getNonClosureAncestor() {
1048
18.9k
  return ::getNonClosureContext(this);
1049
18.9k
}
1050
1051
//===----------------------------------------------------------------------===//
1052
// DeclContext Implementation
1053
//===----------------------------------------------------------------------===//
1054
1055
26.4M
DeclContext::DeclContext(Decl::Kind K) {
1056
26.4M
  DeclContextBits.DeclKind = K;
1057
26.4M
  setHasExternalLexicalStorage(false);
1058
26.4M
  setHasExternalVisibleStorage(false);
1059
26.4M
  setNeedToReconcileExternalVisibleStorage(false);
1060
26.4M
  setHasLazyLocalLexicalLookups(false);
1061
26.4M
  setHasLazyExternalLexicalLookups(false);
1062
26.4M
  setUseQualifiedLookup(false);
1063
26.4M
}
1064
1065
75.9M
bool DeclContext::classof(const Decl *D) {
1066
75.9M
  switch (D->getKind()) {
1067
0
#define DECL(NAME, BASE)
1068
63.2M
#define DECL_CONTEXT(NAME) case Decl::NAME:
1069
0
#define DECL_CONTEXT_BASE(NAME)
1070
5.75M
#include 
"clang/AST/DeclNodes.inc"0
1071
5.75M
      return true;
1072
70.2M
    default:
1073
70.2M
#define DECL(NAME, BASE)
1074
70.2M
#define DECL_CONTEXT_BASE(NAME)                 \
1075
134M
      if (D->getKind() >= Decl::first##NAME &&  \
1076
88.7M
          D->getKind() <= Decl::last##NAME)     \
1077
56.6M
        return true;
1078
70.2M
#include "clang/AST/DeclNodes.inc"
1079
13.5M
      return false;
1080
75.9M
  }
1081
75.9M
}
1082
1083
0
DeclContext::~DeclContext() = default;
1084
1085
/// Find the parent context of this context that will be
1086
/// used for unqualified name lookup.
1087
///
1088
/// Generally, the parent lookup context is the semantic context. However, for
1089
/// a friend function the parent lookup context is the lexical context, which
1090
/// is the class in which the friend is declared.
1091
29.6M
DeclContext *DeclContext::getLookupParent() {
1092
  // FIXME: Find a better way to identify friends.
1093
29.6M
  if (isa<FunctionDecl>(this))
1094
8.00M
    if (getParent()->getRedeclContext()->isFileContext() &&
1095
4.31M
        getLexicalParent()->getRedeclContext()->isRecord())
1096
17.9k
      return getLexicalParent();
1097
29.6M
1098
  // A lookup within the call operator of a lambda never looks in the lambda
1099
  // class; instead, skip to the context in which that closure type is
1100
  // declared.
1101
29.6M
  if (isLambdaCallOperator(this))
1102
16.3k
    return getParent()->getParent();
1103
29.6M
1104
29.6M
  return getParent();
1105
29.6M
}
1106
1107
263
const BlockDecl *DeclContext::getInnermostBlockDecl() const {
1108
263
  const DeclContext *Ctx = this;
1109
263
1110
743
  do {
1111
743
    if (Ctx->isClosure())
1112
25
      return cast<BlockDecl>(Ctx);
1113
718
    Ctx = Ctx->getParent();
1114
718
  } while (Ctx);
1115
263
1116
238
  return nullptr;
1117
263
}
1118
1119
39.2M
bool DeclContext::isInlineNamespace() const {
1120
39.2M
  return isNamespace() &&
1121
2.56M
         cast<NamespaceDecl>(this)->isInline();
1122
39.2M
}
1123
1124
1.46M
bool DeclContext::isStdNamespace() const {
1125
1.46M
  if (!isNamespace())
1126
584k
    return false;
1127
880k
1128
880k
  const auto *ND = cast<NamespaceDecl>(this);
1129
880k
  if (ND->isInline()) {
1130
384k
    return ND->getParent()->isStdNamespace();
1131
384k
  }
1132
495k
1133
495k
  if (!getParent()->getRedeclContext()->isTranslationUnit())
1134
31.2k
    return false;
1135
464k
1136
464k
  const IdentifierInfo *II = ND->getIdentifier();
1137
464k
  return II && 
II->isStr("std")463k
;
1138
464k
}
1139
1140
269M
bool DeclContext::isDependentContext() const {
1141
269M
  if (isFileContext())
1142
143M
    return false;
1143
126M
1144
126M
  if (isa<ClassTemplatePartialSpecializationDecl>(this))
1145
5.32M
    return true;
1146
121M
1147
121M
  if (const auto *Record = dyn_cast<CXXRecordDecl>(this)) {
1148
57.2M
    if (Record->getDescribedClassTemplate())
1149
17.9M
      return true;
1150
39.2M
1151
39.2M
    if (Record->isDependentLambda())
1152
17.7k
      return true;
1153
103M
  }
1154
103M
1155
103M
  if (const auto *Function = dyn_cast<FunctionDecl>(this)) {
1156
39.7M
    if (Function->getDescribedFunctionTemplate())
1157
5.05M
      return true;
1158
34.6M
1159
    // Friend function declarations are dependent if their *lexical*
1160
    // context is dependent.
1161
34.6M
    if (cast<Decl>(this)->getFriendObjectKind())
1162
67.1k
      return getLexicalParent()->isDependentContext();
1163
98.0M
  }
1164
98.0M
1165
  // FIXME: A variable template is a dependent context, but is not a
1166
  // DeclContext. A context within it (such as a lambda-expression)
1167
  // should be considered dependent.
1168
98.0M
1169
98.0M
  return getParent() && getParent()->isDependentContext();
1170
98.0M
}
1171
1172
837M
bool DeclContext::isTransparentContext() const {
1173
837M
  if (getDeclKind() == Decl::Enum)
1174
10.9M
    return !cast<EnumDecl>(this)->isScoped();
1175
826M
  else if (getDeclKind() == Decl::LinkageSpec || 
getDeclKind() == Decl::Export806M
)
1176
20.5M
    return true;
1177
806M
1178
806M
  return false;
1179
806M
}
1180
1181
static bool isLinkageSpecContext(const DeclContext *DC,
1182
17.4M
                                 LinkageSpecDecl::LanguageIDs ID) {
1183
41.9M
  while (DC->getDeclKind() != Decl::TranslationUnit) {
1184
29.9M
    if (DC->getDeclKind() == Decl::LinkageSpec)
1185
5.42M
      return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1186
24.5M
    DC = DC->getLexicalParent();
1187
24.5M
  }
1188
11.9M
  return false;
1189
17.4M
}
1190
1191
17.4M
bool DeclContext::isExternCContext() const {
1192
17.4M
  return isLinkageSpecContext(this, LinkageSpecDecl::lang_c);
1193
17.4M
}
1194
1195
34
const LinkageSpecDecl *DeclContext::getExternCContext() const {
1196
34
  const DeclContext *DC = this;
1197
38
  while (DC->getDeclKind() != Decl::TranslationUnit) {
1198
38
    if (DC->getDeclKind() == Decl::LinkageSpec &&
1199
34
        cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c)
1200
34
      return cast<LinkageSpecDecl>(DC);
1201
4
    DC = DC->getLexicalParent();
1202
4
  }
1203
0
  return nullptr;
1204
34
}
1205
1206
13.5k
bool DeclContext::isExternCXXContext() const {
1207
13.5k
  return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx);
1208
13.5k
}
1209
1210
25.6M
bool DeclContext::Encloses(const DeclContext *DC) const {
1211
25.6M
  if (getPrimaryContext() != this)
1212
8.61M
    return getPrimaryContext()->Encloses(DC);
1213
16.9M
1214
41.9M
  
for (; 16.9M
DC;
DC = DC->getParent()24.9M
)
1215
33.6M
    if (DC->getPrimaryContext() == this)
1216
8.69M
      return true;
1217
8.30M
  return false;
1218
16.9M
}
1219
1220
971M
DeclContext *DeclContext::getPrimaryContext() {
1221
971M
  switch (getDeclKind()) {
1222
420M
  case Decl::TranslationUnit:
1223
420M
  case Decl::ExternCContext:
1224
420M
  case Decl::LinkageSpec:
1225
420M
  case Decl::Export:
1226
420M
  case Decl::Block:
1227
420M
  case Decl::Captured:
1228
420M
  case Decl::OMPDeclareReduction:
1229
420M
  case Decl::OMPDeclareMapper:
1230
420M
  case Decl::RequiresExprBody:
1231
    // There is only one DeclContext for these entities.
1232
420M
    return this;
1233
420M
1234
273M
  case Decl::Namespace:
1235
    // The original namespace is our primary context.
1236
273M
    return static_cast<NamespaceDecl *>(this)->getOriginalNamespace();
1237
420M
1238
434k
  case Decl::ObjCMethod:
1239
434k
    return this;
1240
420M
1241
9.32M
  case Decl::ObjCInterface:
1242
9.32M
    if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this))
1243
9.32M
      if (auto *Def = OID->getDefinition())
1244
9.32M
        return Def;
1245
1.18k
    return this;
1246
1.18k
1247
3.46M
  case Decl::ObjCProtocol:
1248
3.46M
    if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this))
1249
3.46M
      if (auto *Def = OPD->getDefinition())
1250
3.46M
        return Def;
1251
138
    return this;
1252
138
1253
12.3M
  case Decl::ObjCCategory:
1254
12.3M
    return this;
1255
138
1256
121k
  case Decl::ObjCImplementation:
1257
121k
  case Decl::ObjCCategoryImpl:
1258
121k
    return this;
1259
121k
1260
252M
  default:
1261
252M
    if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
1262
      // If this is a tag type that has a definition or is currently
1263
      // being defined, that definition is our primary context.
1264
215M
      auto *Tag = cast<TagDecl>(this);
1265
215M
1266
215M
      if (TagDecl *Def = Tag->getDefinition())
1267
199M
        return Def;
1268
16.5M
1269
16.5M
      if (const auto *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1270
        // Note, TagType::getDecl returns the (partial) definition one exists.
1271
16.4M
        TagDecl *PossiblePartialDef = TagTy->getDecl();
1272
16.4M
        if (PossiblePartialDef->isBeingDefined())
1273
16.2M
          return PossiblePartialDef;
1274
60.8k
      } else {
1275
60.8k
        assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
1276
60.8k
      }
1277
16.5M
1278
237k
      return Tag;
1279
36.4M
    }
1280
36.4M
1281
36.4M
    assert(getDeclKind() >= Decl::firstFunction &&
1282
36.4M
           getDeclKind() <= Decl::lastFunction &&
1283
36.4M
          "Unknown DeclContext kind");
1284
36.4M
    return this;
1285
971M
  }
1286
971M
}
1287
1288
void
1289
1.67M
DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
1290
1.67M
  Contexts.clear();
1291
1.67M
1292
1.67M
  if (getDeclKind() != Decl::Namespace) {
1293
1.64M
    Contexts.push_back(this);
1294
1.64M
    return;
1295
1.64M
  }
1296
28.1k
1297
28.1k
  auto *Self = static_cast<NamespaceDecl *>(this);
1298
58.3k
  for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1299
30.1k
       N = N->getPreviousDecl())
1300
30.1k
    Contexts.push_back(N);
1301
28.1k
1302
28.1k
  std::reverse(Contexts.begin(), Contexts.end());
1303
28.1k
}
1304
1305
std::pair<Decl *, Decl *>
1306
DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls,
1307
46.2k
                            bool FieldsAlreadyLoaded) {
1308
  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1309
46.2k
  Decl *FirstNewDecl = nullptr;
1310
46.2k
  Decl *PrevDecl = nullptr;
1311
315k
  for (auto *D : Decls) {
1312
315k
    if (FieldsAlreadyLoaded && 
isa<FieldDecl>(D)39.5k
)
1313
0
      continue;
1314
315k
1315
315k
    if (PrevDecl)
1316
269k
      PrevDecl->NextInContextAndBits.setPointer(D);
1317
46.2k
    else
1318
46.2k
      FirstNewDecl = D;
1319
315k
1320
315k
    PrevDecl = D;
1321
315k
  }
1322
46.2k
1323
46.2k
  return std::make_pair(FirstNewDecl, PrevDecl);
1324
46.2k
}
1325
1326
/// We have just acquired external visible storage, and we already have
1327
/// built a lookup map. For every name in the map, pull in the new names from
1328
/// the external storage.
1329
18.8k
void DeclContext::reconcileExternalVisibleStorage() const {
1330
18.8k
  assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr);
1331
18.8k
  setNeedToReconcileExternalVisibleStorage(false);
1332
18.8k
1333
18.8k
  for (auto &Lookup : *LookupPtr)
1334
568k
    Lookup.second.setHasExternalDecls();
1335
18.8k
}
1336
1337
/// Load the declarations within this lexical storage from an
1338
/// external source.
1339
/// \return \c true if any declarations were added.
1340
bool
1341
51.4k
DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1342
51.4k
  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1343
51.4k
  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1344
51.4k
1345
  // Notify that we have a DeclContext that is initializing.
1346
51.4k
  ExternalASTSource::Deserializing ADeclContext(Source);
1347
51.4k
1348
  // Load the external declarations, if any.
1349
51.4k
  SmallVector<Decl*, 64> Decls;
1350
51.4k
  setHasExternalLexicalStorage(false);
1351
51.4k
  Source->FindExternalLexicalDecls(this, Decls);
1352
51.4k
1353
51.4k
  if (Decls.empty())
1354
14.8k
    return false;
1355
36.5k
1356
  // We may have already loaded just the fields of this record, in which case
1357
  // we need to ignore them.
1358
36.5k
  bool FieldsAlreadyLoaded = false;
1359
36.5k
  if (const auto *RD = dyn_cast<RecordDecl>(this))
1360
32.8k
    FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage();
1361
36.5k
1362
  // Splice the newly-read declarations into the beginning of the list
1363
  // of declarations.
1364
36.5k
  Decl *ExternalFirst, *ExternalLast;
1365
36.5k
  std::tie(ExternalFirst, ExternalLast) =
1366
36.5k
      BuildDeclChain(Decls, FieldsAlreadyLoaded);
1367
36.5k
  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1368
36.5k
  FirstDecl = ExternalFirst;
1369
36.5k
  if (!LastDecl)
1370
34.9k
    LastDecl = ExternalLast;
1371
36.5k
  return true;
1372
36.5k
}
1373
1374
DeclContext::lookup_result
1375
ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1376
312k
                                                    DeclarationName Name) {
1377
312k
  ASTContext &Context = DC->getParentASTContext();
1378
312k
  StoredDeclsMap *Map;
1379
312k
  if (!(Map = DC->LookupPtr))
1380
0
    Map = DC->CreateStoredDeclsMap(Context);
1381
312k
  if (DC->hasNeedToReconcileExternalVisibleStorage())
1382
0
    DC->reconcileExternalVisibleStorage();
1383
312k
1384
312k
  (*Map)[Name].removeExternalDecls();
1385
312k
1386
312k
  return DeclContext::lookup_result();
1387
312k
}
1388
1389
DeclContext::lookup_result
1390
ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1391
                                                  DeclarationName Name,
1392
1.03M
                                                  ArrayRef<NamedDecl*> Decls) {
1393
1.03M
  ASTContext &Context = DC->getParentASTContext();
1394
1.03M
  StoredDeclsMap *Map;
1395
1.03M
  if (!(Map = DC->LookupPtr))
1396
16
    Map = DC->CreateStoredDeclsMap(Context);
1397
1.03M
  if (DC->hasNeedToReconcileExternalVisibleStorage())
1398
11.9k
    DC->reconcileExternalVisibleStorage();
1399
1.03M
1400
1.03M
  StoredDeclsList &List = (*Map)[Name];
1401
1.03M
1402
  // Clear out any old external visible declarations, to avoid quadratic
1403
  // performance in the redeclaration checks below.
1404
1.03M
  List.removeExternalDecls();
1405
1.03M
1406
1.03M
  if (!List.isNull()) {
1407
    // We have both existing declarations and new declarations for this name.
1408
    // Some of the declarations may simply replace existing ones. Handle those
1409
    // first.
1410
227k
    llvm::SmallVector<unsigned, 8> Skip;
1411
485k
    for (unsigned I = 0, N = Decls.size(); I != N; 
++I258k
)
1412
258k
      if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
1413
11.0k
        Skip.push_back(I);
1414
227k
    Skip.push_back(Decls.size());
1415
227k
1416
    // Add in any new declarations.
1417
227k
    unsigned SkipPos = 0;
1418
485k
    for (unsigned I = 0, N = Decls.size(); I != N; 
++I258k
) {
1419
258k
      if (I == Skip[SkipPos])
1420
11.0k
        ++SkipPos;
1421
247k
      else
1422
247k
        List.AddSubsequentDecl(Decls[I]);
1423
258k
    }
1424
806k
  } else {
1425
    // Convert the array to a StoredDeclsList.
1426
65.4k
    for (auto *D : Decls) {
1427
65.4k
      if (List.isNull())
1428
47.5k
        List.setOnlyValue(D);
1429
17.9k
      else
1430
17.9k
        List.AddSubsequentDecl(D);
1431
65.4k
    }
1432
806k
  }
1433
1.03M
1434
1.03M
  return List.getLookupResult();
1435
1.03M
}
1436
1437
13.6M
DeclContext::decl_iterator DeclContext::decls_begin() const {
1438
13.6M
  if (hasExternalLexicalStorage())
1439
39.6k
    LoadLexicalDeclsFromExternalStorage();
1440
13.6M
  return decl_iterator(FirstDecl);
1441
13.6M
}
1442
1443
871k
bool DeclContext::decls_empty() const {
1444
871k
  if (hasExternalLexicalStorage())
1445
7
    LoadLexicalDeclsFromExternalStorage();
1446
871k
1447
871k
  return !FirstDecl;
1448
871k
}
1449
1450
19.1k
bool DeclContext::containsDecl(Decl *D) const {
1451
19.1k
  return (D->getLexicalDeclContext() == this &&
1452
18.5k
          (D->NextInContextAndBits.getPointer() || 
D == LastDecl6.51k
));
1453
19.1k
}
1454
1455
6.03k
bool DeclContext::containsDeclAndLoad(Decl *D) const {
1456
6.03k
  if (hasExternalLexicalStorage())
1457
7
    LoadLexicalDeclsFromExternalStorage();
1458
6.03k
  return containsDecl(D);
1459
6.03k
}
1460
1461
/// shouldBeHidden - Determine whether a declaration which was declared
1462
/// within its semantic context should be invisible to qualified name lookup.
1463
46.2M
static bool shouldBeHidden(NamedDecl *D) {
1464
  // Skip unnamed declarations.
1465
46.2M
  if (!D->getDeclName())
1466
1.20M
    return true;
1467
45.0M
1468
  // Skip entities that can't be found by name lookup into a particular
1469
  // context.
1470
45.0M
  if ((D->getIdentifierNamespace() == 0 && 
!isa<UsingDirectiveDecl>(D)286k
) ||
1471
44.7M
      D->isTemplateParameter())
1472
282k
    return true;
1473
44.7M
1474
  // Skip friends and local extern declarations unless they're the first
1475
  // declaration of the entity.
1476
44.7M
  if ((D->isLocalExternDecl() || 
D->getFriendObjectKind()44.7M
) &&
1477
51.7k
      D != D->getCanonicalDecl())
1478
22.6k
    return true;
1479
44.7M
1480
  // Skip template specializations.
1481
  // FIXME: This feels like a hack. Should DeclarationName support
1482
  // template-ids, or is there a better way to keep specializations
1483
  // from being visible?
1484
44.7M
  if (isa<ClassTemplateSpecializationDecl>(D))
1485
123
    return true;
1486
44.7M
  if (auto *FD = dyn_cast<FunctionDecl>(D))
1487
16.0M
    if (FD->isFunctionTemplateSpecialization())
1488
3.41k
      return true;
1489
44.7M
1490
  // Hide destructors that are invalid. There should always be one destructor,
1491
  // but if it is an invalid decl, another one is created. We need to hide the
1492
  // invalid one from places that expect exactly one destructor, like the
1493
  // serialization code.
1494
44.7M
  if (isa<CXXDestructorDecl>(D) && 
D->isInvalidDecl()139k
)
1495
37
    return true;
1496
44.7M
1497
44.7M
  return false;
1498
44.7M
}
1499
1500
11.2k
void DeclContext::removeDecl(Decl *D) {
1501
11.2k
  assert(D->getLexicalDeclContext() == this &&
1502
11.2k
         "decl being removed from non-lexical context");
1503
11.2k
  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
1504
11.2k
         "decl is not in decls list");
1505
11.2k
1506
  // Remove D from the decl chain.  This is O(n) but hopefully rare.
1507
11.2k
  if (D == FirstDecl) {
1508
5.97k
    if (D == LastDecl)
1509
502
      FirstDecl = LastDecl = nullptr;
1510
5.47k
    else
1511
5.47k
      FirstDecl = D->NextInContextAndBits.getPointer();
1512
5.32k
  } else {
1513
1.46M
    for (Decl *I = FirstDecl; true; 
I = I->NextInContextAndBits.getPointer()1.46M
) {
1514
1.46M
      assert(I && "decl not found in linked list");
1515
1.46M
      if (I->NextInContextAndBits.getPointer() == D) {
1516
5.32k
        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1517
5.32k
        if (D == LastDecl) 
LastDecl = I1.52k
;
1518
5.32k
        break;
1519
5.32k
      }
1520
1.46M
    }
1521
5.32k
  }
1522
11.2k
1523
  // Mark that D is no longer in the decl chain.
1524
11.2k
  D->NextInContextAndBits.setPointer(nullptr);
1525
11.2k
1526
  // Remove D from the lookup table if necessary.
1527
11.2k
  if (isa<NamedDecl>(D)) {
1528
10.1k
    auto *ND = cast<NamedDecl>(D);
1529
10.1k
1530
    // Do not try to remove the declaration if that is invisible to qualified
1531
    // lookup.  E.g. template specializations are skipped.
1532
10.1k
    if (shouldBeHidden(ND))
1533
251
      return;
1534
9.87k
1535
    // Remove only decls that have a name
1536
9.87k
    if (!ND->getDeclName())
1537
0
      return;
1538
9.87k
1539
9.87k
    auto *DC = D->getDeclContext();
1540
9.87k
    do {
1541
9.87k
      StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1542
9.87k
      if (Map) {
1543
7.60k
        StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1544
7.60k
        assert(Pos != Map->end() && "no lookup entry for decl");
1545
        // Remove the decl only if it is contained.
1546
7.60k
        StoredDeclsList::DeclsTy *Vec = Pos->second.getAsVector();
1547
7.60k
        if ((Vec && 
is_contained(*Vec, ND)6.48k
) ||
Pos->second.getAsDecl() == ND1.11k
)
1548
7.60k
          Pos->second.remove(ND);
1549
7.60k
      }
1550
9.87k
    } while (DC->isTransparentContext() && 
(DC = DC->getParent())0
);
1551
9.87k
  }
1552
11.2k
}
1553
1554
47.8M
void DeclContext::addHiddenDecl(Decl *D) {
1555
47.8M
  assert(D->getLexicalDeclContext() == this &&
1556
47.8M
         "Decl inserted into wrong lexical context");
1557
47.8M
  assert(!D->getNextDeclInContext() && D != LastDecl &&
1558
47.8M
         "Decl already inserted into a DeclContext");
1559
47.8M
1560
47.8M
  if (FirstDecl) {
1561
40.9M
    LastDecl->NextInContextAndBits.setPointer(D);
1562
40.9M
    LastDecl = D;
1563
6.84M
  } else {
1564
6.84M
    FirstDecl = LastDecl = D;
1565
6.84M
  }
1566
47.8M
1567
  // Notify a C++ record declaration that we've added a member, so it can
1568
  // update its class-specific state.
1569
47.8M
  if (auto *Record = dyn_cast<CXXRecordDecl>(this))
1570
7.30M
    Record->addedMember(D);
1571
47.8M
1572
  // If this is a newly-created (not de-serialized) import declaration, wire
1573
  // it in to the list of local import declarations.
1574
47.8M
  if (!D->isFromASTFile()) {
1575
47.7M
    if (auto *Import = dyn_cast<ImportDecl>(D))
1576
81.1k
      D->getASTContext().addedLocalImportDecl(Import);
1577
47.7M
  }
1578
47.8M
}
1579
1580
46.8M
void DeclContext::addDecl(Decl *D) {
1581
46.8M
  addHiddenDecl(D);
1582
46.8M
1583
46.8M
  if (auto *ND = dyn_cast<NamedDecl>(D))
1584
45.7M
    ND->getDeclContext()->getPrimaryContext()->
1585
45.7M
        makeDeclVisibleInContextWithFlags(ND, false, true);
1586
46.8M
}
1587
1588
262k
void DeclContext::addDeclInternal(Decl *D) {
1589
262k
  addHiddenDecl(D);
1590
262k
1591
262k
  if (auto *ND = dyn_cast<NamedDecl>(D))
1592
257k
    ND->getDeclContext()->getPrimaryContext()->
1593
257k
        makeDeclVisibleInContextWithFlags(ND, true, true);
1594
262k
}
1595
1596
/// buildLookup - Build the lookup data structure with all of the
1597
/// declarations in this DeclContext (and any other contexts linked
1598
/// to it or transparent contexts nested within it) and return it.
1599
///
1600
/// Note that the produced map may miss out declarations from an
1601
/// external source. If it does, those entries will be marked with
1602
/// the 'hasExternalDecls' flag.
1603
13.5M
StoredDeclsMap *DeclContext::buildLookup() {
1604
13.5M
  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1605
13.5M
1606
13.5M
  if (!hasLazyLocalLexicalLookups() &&
1607
11.9M
      !hasLazyExternalLexicalLookups())
1608
11.9M
    return LookupPtr;
1609
1.66M
1610
1.66M
  SmallVector<DeclContext *, 2> Contexts;
1611
1.66M
  collectAllContexts(Contexts);
1612
1.66M
1613
1.66M
  if (hasLazyExternalLexicalLookups()) {
1614
11.9k
    setHasLazyExternalLexicalLookups(false);
1615
11.9k
    for (auto *DC : Contexts) {
1616
11.9k
      if (DC->hasExternalLexicalStorage()) {
1617
11.7k
        bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage();
1618
11.7k
        setHasLazyLocalLexicalLookups(
1619
11.7k
            hasLazyLocalLexicalLookups() | LoadedDecls );
1620
11.7k
      }
1621
11.9k
    }
1622
11.9k
1623
11.9k
    if (!hasLazyLocalLexicalLookups())
1624
10.6k
      return LookupPtr;
1625
1.65M
  }
1626
1.65M
1627
1.65M
  for (auto *DC : Contexts)
1628
1.65M
    buildLookupImpl(DC, hasExternalVisibleStorage());
1629
1.65M
1630
  // We no longer have any lazy decls.
1631
1.65M
  setHasLazyLocalLexicalLookups(false);
1632
1.65M
  return LookupPtr;
1633
1.65M
}
1634
1635
/// buildLookupImpl - Build part of the lookup data structure for the
1636
/// declarations contained within DCtx, which will either be this
1637
/// DeclContext, a DeclContext linked to it, or a transparent context
1638
/// nested within it.
1639
1.68M
void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1640
3.22M
  for (auto *D : DCtx->noload_decls()) {
1641
    // Insert this declaration into the lookup structure, but only if
1642
    // it's semantically within its decl context. Any other decls which
1643
    // should be found in this context are added eagerly.
1644
    //
1645
    // If it's from an AST file, don't add it now. It'll get handled by
1646
    // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1647
    // in C++, we do not track external visible decls for the TU, so in
1648
    // that case we need to collect them all here.
1649
3.22M
    if (auto *ND = dyn_cast<NamedDecl>(D))
1650
3.03M
      if (ND->getDeclContext() == DCtx && 
!shouldBeHidden(ND)3.03M
&&
1651
2.99M
          (!ND->isFromASTFile() ||
1652
182k
           (isTranslationUnit() &&
1653
2.69k
            !getParentASTContext().getLangOpts().CPlusPlus)))
1654
2.81M
        makeDeclVisibleInContextImpl(ND, Internal);
1655
3.22M
1656
    // If this declaration is itself a transparent declaration context
1657
    // or inline namespace, add the members of this declaration of that
1658
    // context (recursively).
1659
3.22M
    if (auto *InnerCtx = dyn_cast<DeclContext>(D))
1660
1.62M
      if (InnerCtx->isTransparentContext() || 
InnerCtx->isInlineNamespace()1.61M
)
1661
13.3k
        buildLookupImpl(InnerCtx, Internal);
1662
3.22M
  }
1663
1.68M
}
1664
1665
NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1666
1667
DeclContext::lookup_result
1668
188M
DeclContext::lookup(DeclarationName Name) const {
1669
188M
  assert(getDeclKind() != Decl::LinkageSpec &&
1670
188M
         getDeclKind() != Decl::Export &&
1671
188M
         "should not perform lookups into transparent contexts");
1672
188M
1673
188M
  const DeclContext *PrimaryContext = getPrimaryContext();
1674
188M
  if (PrimaryContext != this)
1675
39.0M
    return PrimaryContext->lookup(Name);
1676
149M
1677
  // If we have an external source, ensure that any later redeclarations of this
1678
  // context have been loaded, since they may add names to the result of this
1679
  // lookup (or add external visible storage).
1680
149M
  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1681
149M
  if (Source)
1682
15.6M
    (void)cast<Decl>(this)->getMostRecentDecl();
1683
149M
1684
149M
  if (hasExternalVisibleStorage()) {
1685
6.51M
    assert(Source && "external visible storage but no external source?");
1686
6.51M
1687
6.51M
    if (hasNeedToReconcileExternalVisibleStorage())
1688
6.88k
      reconcileExternalVisibleStorage();
1689
6.51M
1690
6.51M
    StoredDeclsMap *Map = LookupPtr;
1691
6.51M
1692
6.51M
    if (hasLazyLocalLexicalLookups() ||
1693
6.51M
        hasLazyExternalLexicalLookups())
1694
      // FIXME: Make buildLookup const?
1695
516
      Map = const_cast<DeclContext*>(this)->buildLookup();
1696
6.51M
1697
6.51M
    if (!Map)
1698
29.2k
      Map = CreateStoredDeclsMap(getParentASTContext());
1699
6.51M
1700
    // If we have a lookup result with no external decls, we are done.
1701
6.51M
    std::pair<StoredDeclsMap::iterator, bool> R =
1702
6.51M
        Map->insert(std::make_pair(Name, StoredDeclsList()));
1703
6.51M
    if (!R.second && 
!R.first->second.hasExternalDecls()5.52M
)
1704
5.32M
      return R.first->second.getLookupResult();
1705
1.18M
1706
1.18M
    if (Source->FindExternalVisibleDeclsByName(this, Name) || 
!R.second1.01M
) {
1707
272k
      if (StoredDeclsMap *Map = LookupPtr) {
1708
272k
        StoredDeclsMap::iterator I = Map->find(Name);
1709
272k
        if (I != Map->end())
1710
272k
          return I->second.getLookupResult();
1711
914k
      }
1712
272k
    }
1713
914k
1714
914k
    return {};
1715
914k
  }
1716
142M
1717
142M
  StoredDeclsMap *Map = LookupPtr;
1718
142M
  if (hasLazyLocalLexicalLookups() ||
1719
141M
      hasLazyExternalLexicalLookups())
1720
1.61M
    Map = const_cast<DeclContext*>(this)->buildLookup();
1721
142M
1722
142M
  if (!Map)
1723
13.4M
    return {};
1724
129M
1725
129M
  StoredDeclsMap::iterator I = Map->find(Name);
1726
129M
  if (I == Map->end())
1727
91.7M
    return {};
1728
37.3M
1729
37.3M
  return I->second.getLookupResult();
1730
37.3M
}
1731
1732
DeclContext::lookup_result
1733
1.77M
DeclContext::noload_lookup(DeclarationName Name) {
1734
1.77M
  assert(getDeclKind() != Decl::LinkageSpec &&
1735
1.77M
         getDeclKind() != Decl::Export &&
1736
1.77M
         "should not perform lookups into transparent contexts");
1737
1.77M
1738
1.77M
  DeclContext *PrimaryContext = getPrimaryContext();
1739
1.77M
  if (PrimaryContext != this)
1740
148
    return PrimaryContext->noload_lookup(Name);
1741
1.77M
1742
1.77M
  loadLazyLocalLexicalLookups();
1743
1.77M
  StoredDeclsMap *Map = LookupPtr;
1744
1.77M
  if (!Map)
1745
88.3k
    return {};
1746
1.68M
1747
1.68M
  StoredDeclsMap::iterator I = Map->find(Name);
1748
1.32M
  return I != Map->end() ? I->second.getLookupResult()
1749
358k
                         : lookup_result();
1750
1.68M
}
1751
1752
// If we have any lazy lexical declarations not in our lookup map, add them
1753
// now. Don't import any external declarations, not even if we know we have
1754
// some missing from the external visible lookups.
1755
1.77M
void DeclContext::loadLazyLocalLexicalLookups() {
1756
1.77M
  if (hasLazyLocalLexicalLookups()) {
1757
15.8k
    SmallVector<DeclContext *, 2> Contexts;
1758
15.8k
    collectAllContexts(Contexts);
1759
15.8k
    for (auto *Context : Contexts)
1760
15.8k
      buildLookupImpl(Context, hasExternalVisibleStorage());
1761
15.8k
    setHasLazyLocalLexicalLookups(false);
1762
15.8k
  }
1763
1.77M
}
1764
1765
void DeclContext::localUncachedLookup(DeclarationName Name,
1766
128k
                                      SmallVectorImpl<NamedDecl *> &Results) {
1767
128k
  Results.clear();
1768
128k
1769
  // If there's no external storage, just perform a normal lookup and copy
1770
  // the results.
1771
128k
  if (!hasExternalVisibleStorage() && 
!hasExternalLexicalStorage()98.5k
&&
Name79.0k
) {
1772
78.7k
    lookup_result LookupResults = lookup(Name);
1773
78.7k
    Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1774
78.7k
    return;
1775
78.7k
  }
1776
49.8k
1777
  // If we have a lookup table, check there first. Maybe we'll get lucky.
1778
  // FIXME: Should we be checking these flags on the primary context?
1779
49.8k
  if (Name && 
!hasLazyLocalLexicalLookups()49.3k
&&
1780
49.3k
      !hasLazyExternalLexicalLookups()) {
1781
32.8k
    if (StoredDeclsMap *Map = LookupPtr) {
1782
30.8k
      StoredDeclsMap::iterator Pos = Map->find(Name);
1783
30.8k
      if (Pos != Map->end()) {
1784
923
        Results.insert(Results.end(),
1785
923
                       Pos->second.getLookupResult().begin(),
1786
923
                       Pos->second.getLookupResult().end());
1787
923
        return;
1788
923
      }
1789
48.9k
    }
1790
32.8k
  }
1791
48.9k
1792
  // Slow case: grovel through the declarations in our chain looking for
1793
  // matches.
1794
  // FIXME: If we have lazy external declarations, this will not find them!
1795
  // FIXME: Should we CollectAllContexts and walk them all here?
1796
4.31M
  
for (Decl *D = FirstDecl; 48.9k
D;
D = D->getNextDeclInContext()4.27M
) {
1797
4.27M
    if (auto *ND = dyn_cast<NamedDecl>(D))
1798
4.24M
      if (ND->getDeclName() == Name)
1799
3.88k
        Results.push_back(ND);
1800
4.27M
  }
1801
48.9k
}
1802
1803
660M
DeclContext *DeclContext::getRedeclContext() {
1804
660M
  DeclContext *Ctx = this;
1805
660M
1806
  // In C, a record type is the redeclaration context for its fields only. If
1807
  // we arrive at a record context after skipping anything else, we should skip
1808
  // the record as well. Currently, this means skipping enumerations because
1809
  // they're the only transparent context that can exist within a struct or
1810
  // union.
1811
660M
  bool SkipRecords = getDeclKind() == Decl::Kind::Enum &&
1812
946k
                     !getParentASTContext().getLangOpts().CPlusPlus;
1813
660M
1814
  // Skip through contexts to get to the redeclaration context. Transparent
1815
  // contexts are always skipped.
1816
673M
  while ((SkipRecords && 
Ctx->isRecord()1.58M
) ||
Ctx->isTransparentContext()673M
)
1817
13.0M
    Ctx = Ctx->getParent();
1818
660M
  return Ctx;
1819
660M
}
1820
1821
77.8k
DeclContext *DeclContext::getEnclosingNamespaceContext() {
1822
77.8k
  DeclContext *Ctx = this;
1823
  // Skip through non-namespace, non-translation-unit contexts.
1824
123k
  while (!Ctx->isFileContext())
1825
45.3k
    Ctx = Ctx->getParent();
1826
77.8k
  return Ctx->getPrimaryContext();
1827
77.8k
}
1828
1829
16
RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1830
  // Loop until we find a non-record context.
1831
16
  RecordDecl *OutermostRD = nullptr;
1832
16
  DeclContext *DC = this;
1833
41
  while (DC->isRecord()) {
1834
25
    OutermostRD = cast<RecordDecl>(DC);
1835
25
    DC = DC->getLexicalParent();
1836
25
  }
1837
16
  return OutermostRD;
1838
16
}
1839
1840
938k
bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1841
  // For non-file contexts, this is equivalent to Equals.
1842
938k
  if (!isFileContext())
1843
875k
    return O->Equals(this);
1844
62.6k
1845
62.6k
  
do 62.6k
{
1846
62.6k
    if (O->Equals(this))
1847
62.5k
      return true;
1848
120
1849
120
    const auto *NS = dyn_cast<NamespaceDecl>(O);
1850
120
    if (!NS || 
!NS->isInline()105
)
1851
53
      break;
1852
67
    O = NS->getParent();
1853
67
  } while (O);
1854
62.6k
1855
53
  return false;
1856
62.6k
}
1857
1858
1.15M
void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1859
1.15M
  DeclContext *PrimaryDC = this->getPrimaryContext();
1860
1.15M
  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1861
  // If the decl is being added outside of its semantic decl context, we
1862
  // need to ensure that we eagerly build the lookup information for it.
1863
1.15M
  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1864
1.15M
}
1865
1866
void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1867
54.7M
                                                    bool Recoverable) {
1868
54.7M
  assert(this == getPrimaryContext() && "expected a primary DC");
1869
54.7M
1870
54.7M
  if (!isLookupContext()) {
1871
11.6M
    if (isTransparentContext())
1872
1.77M
      getParent()->getPrimaryContext()
1873
1.77M
        ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1874
11.6M
    return;
1875
11.6M
  }
1876
43.1M
1877
  // Skip declarations which should be invisible to name lookup.
1878
43.1M
  if (shouldBeHidden(D))
1879
1.47M
    return;
1880
41.7M
1881
  // If we already have a lookup data structure, perform the insertion into
1882
  // it. If we might have externally-stored decls with this name, look them
1883
  // up and perform the insertion. If this decl was declared outside its
1884
  // semantic context, buildLookup won't add it, so add it now.
1885
  //
1886
  // FIXME: As a performance hack, don't add such decls into the translation
1887
  // unit unless we're in C++, since qualified lookup into the TU is never
1888
  // performed.
1889
41.7M
  if (LookupPtr || 
hasExternalVisibleStorage()30.1M
||
1890
30.1M
      ((!Recoverable || 
D->getDeclContext() != D->getLexicalDeclContext()30.0M
) &&
1891
21.3k
       (getParentASTContext().getLangOpts().CPlusPlus ||
1892
11.6M
        
!isTranslationUnit()6.62k
))) {
1893
    // If we have lazily omitted any decls, they might have the same name as
1894
    // the decl which we are adding, so build a full lookup table before adding
1895
    // this decl.
1896
11.6M
    buildLookup();
1897
11.6M
    makeDeclVisibleInContextImpl(D, Internal);
1898
30.0M
  } else {
1899
30.0M
    setHasLazyLocalLexicalLookups(true);
1900
30.0M
  }
1901
41.7M
1902
  // If we are a transparent context or inline namespace, insert into our
1903
  // parent context, too. This operation is recursive.
1904
41.7M
  if (isTransparentContext() || 
isInlineNamespace()36.7M
)
1905
5.80M
    getParent()->getPrimaryContext()->
1906
5.80M
        makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1907
41.7M
1908
41.7M
  auto *DCAsDecl = cast<Decl>(this);
1909
  // Notify that a decl was made visible unless we are a Tag being defined.
1910
41.7M
  if (!(isa<TagDecl>(DCAsDecl) && 
cast<TagDecl>(DCAsDecl)->isBeingDefined()14.1M
))
1911
28.2M
    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1912
940k
      L->AddedVisibleDecl(this, D);
1913
41.7M
}
1914
1915
14.8M
void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1916
  // Find or create the stored declaration map.
1917
14.8M
  StoredDeclsMap *Map = LookupPtr;
1918
14.8M
  if (!Map) {
1919
1.75M
    ASTContext *C = &getParentASTContext();
1920
1.75M
    Map = CreateStoredDeclsMap(*C);
1921
1.75M
  }
1922
14.8M
1923
  // If there is an external AST source, load any declarations it knows about
1924
  // with this declaration's name.
1925
  // If the lookup table contains an entry about this name it means that we
1926
  // have already checked the external source.
1927
14.8M
  if (!Internal)
1928
14.2M
    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1929
2.04M
      if (hasExternalVisibleStorage() &&
1930
622k
          Map->find(D->getDeclName()) == Map->end())
1931
139k
        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1932
14.8M
1933
  // Insert this declaration into the map.
1934
14.8M
  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1935
14.8M
1936
14.8M
  if (Internal) {
1937
    // If this is being added as part of loading an external declaration,
1938
    // this may not be the only external declaration with this name.
1939
    // In this case, we never try to replace an existing declaration; we'll
1940
    // handle that when we finalize the list of declarations for this name.
1941
615k
    DeclNameEntries.setHasExternalDecls();
1942
615k
    DeclNameEntries.AddSubsequentDecl(D);
1943
615k
    return;
1944
615k
  }
1945
14.2M
1946
14.2M
  if (DeclNameEntries.isNull()) {
1947
12.2M
    DeclNameEntries.setOnlyValue(D);
1948
12.2M
    return;
1949
12.2M
  }
1950
2.07M
1951
2.07M
  if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
1952
    // This declaration has replaced an existing one for which
1953
    // declarationReplaces returns true.
1954
397k
    return;
1955
397k
  }
1956
1.67M
1957
  // Put this declaration into the appropriate slot.
1958
1.67M
  DeclNameEntries.AddSubsequentDecl(D);
1959
1.67M
}
1960
1961
16.2M
UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1962
16.2M
  return cast<UsingDirectiveDecl>(*I);
1963
16.2M
}
1964
1965
/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1966
/// this context.
1967
53.2M
DeclContext::udir_range DeclContext::using_directives() const {
1968
  // FIXME: Use something more efficient than normal lookup for using
1969
  // directives. In C++, using directives are looked up more than anything else.
1970
53.2M
  lookup_result Result = lookup(UsingDirectiveDecl::getName());
1971
53.2M
  return udir_range(Result.begin(), Result.end());
1972
53.2M
}
1973
1974
//===----------------------------------------------------------------------===//
1975
// Creation and Destruction of StoredDeclsMaps.                               //
1976
//===----------------------------------------------------------------------===//
1977
1978
1.78M
StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1979
1.78M
  assert(!LookupPtr && "context already has a decls map");
1980
1.78M
  assert(getPrimaryContext() == this &&
1981
1.78M
         "creating decls map on non-primary context");
1982
1.78M
1983
1.78M
  StoredDeclsMap *M;
1984
1.78M
  bool Dependent = isDependentContext();
1985
1.78M
  if (Dependent)
1986
367k
    M = new DependentStoredDeclsMap();
1987
1.41M
  else
1988
1.41M
    M = new StoredDeclsMap();
1989
1.78M
  M->Previous = C.LastSDM;
1990
1.78M
  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1991
1.78M
  LookupPtr = M;
1992
1.78M
  return M;
1993
1.78M
}
1994
1995
78.7k
void ASTContext::ReleaseDeclContextMaps() {
1996
  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1997
  // pointer because the subclass doesn't add anything that needs to
1998
  // be deleted.
1999
78.7k
  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
2000
78.7k
}
2001
2002
78.7k
void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
2003
582k
  while (Map) {
2004
    // Advance the iteration before we invalidate memory.
2005
503k
    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
2006
503k
2007
503k
    if (Dependent)
2008
57.8k
      delete static_cast<DependentStoredDeclsMap*>(Map);
2009
445k
    else
2010
445k
      delete Map;
2011
503k
2012
503k
    Map = Next.getPointer();
2013
503k
    Dependent = Next.getInt();
2014
503k
  }
2015
78.7k
}
2016
2017
DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
2018
                                                 DeclContext *Parent,
2019
97
                                           const PartialDiagnostic &PDiag) {
2020
97
  assert(Parent->isDependentContext()
2021
97
         && "cannot iterate dependent diagnostics of non-dependent context");
2022
97
  Parent = Parent->getPrimaryContext();
2023
97
  if (!Parent->LookupPtr)
2024
87
    Parent->CreateStoredDeclsMap(C);
2025
97
2026
97
  auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
2027
97
2028
  // Allocate the copy of the PartialDiagnostic via the ASTContext's
2029
  // BumpPtrAllocator, rather than the ASTContext itself.
2030
97
  PartialDiagnostic::Storage *DiagStorage = nullptr;
2031
97
  if (PDiag.hasStorage())
2032
2
    DiagStorage = new (C) PartialDiagnostic::Storage;
2033
97
2034
97
  auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
2035
97
2036
  // TODO: Maybe we shouldn't reverse the order during insertion.
2037
97
  DD->NextDiagnostic = Map->FirstDiagnostic;
2038
97
  Map->FirstDiagnostic = DD;
2039
97
2040
97
  return DD;
2041
97
}