Coverage Report

Created: 2021-08-24 07:12

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