Coverage Report

Created: 2019-07-24 05:18

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