Coverage Report

Created: 2020-02-25 14:32

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