Coverage Report

Created: 2021-01-19 06:58

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