Bug Summary

File:tools/clang/lib/AST/DeclBase.cpp
Warning:line 1891, column 22
Access to field 'NextDiagnostic' results in a dereference of a null pointer (loaded from variable 'DD')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DeclBase.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329611/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn329611/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329611/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329611/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329611/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329611/build-llvm/tools/clang/lib/AST -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-10-031729-19628-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp
1//===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
15#include "clang/AST/ASTContext.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 "clang/Basic/VersionTuple.h"
38#include "llvm/ADT/ArrayRef.h"
39#include "llvm/ADT/PointerIntPair.h"
40#include "llvm/ADT/SmallVector.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/MathExtras.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
53using 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
63void Decl::updateOutOfDate(IdentifierInfo &II) const {
64 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
65}
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
73void *Decl::operator new(std::size_t Size, const ASTContext &Context,
74 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 static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78 "Decl won't be misaligned");
79 void *Start = Context.Allocate(Size + Extra + 8);
80 void *Result = (char*)Start + 8;
81
82 unsigned *PrefixPtr = (unsigned *)Result - 2;
83
84 // Zero out the first 4 bytes; this is used to store the owning module ID.
85 PrefixPtr[0] = 0;
86
87 // Store the global declaration ID in the second 4 bytes.
88 PrefixPtr[1] = ID;
89
90 return Result;
91}
92
93void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
94 DeclContext *Parent, std::size_t Extra) {
95 assert(!Parent || &Parent->getParentASTContext() == &Ctx)(static_cast <bool> (!Parent || &Parent->getParentASTContext
() == &Ctx) ? void (0) : __assert_fail ("!Parent || &Parent->getParentASTContext() == &Ctx"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 95, __extension__ __PRETTY_FUNCTION__))
;
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 if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) {
100 // Ensure required alignment of the resulting object by adding extra
101 // padding at the start if required.
102 size_t ExtraAlign =
103 llvm::OffsetToAlignment(sizeof(Module *), alignof(Decl));
104 char *Buffer = reinterpret_cast<char *>(
105 ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
106 Buffer += ExtraAlign;
107 auto *ParentModule =
108 Parent ? cast<Decl>(Parent)->getOwningModule() : nullptr;
109 return new (Buffer) Module*(ParentModule) + 1;
110 }
111 return ::operator new(Size + Extra, Ctx);
112}
113
114Module *Decl::getOwningModuleSlow() const {
115 assert(isFromASTFile() && "Not from AST file?")(static_cast <bool> (isFromASTFile() && "Not from AST file?"
) ? void (0) : __assert_fail ("isFromASTFile() && \"Not from AST file?\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 115, __extension__ __PRETTY_FUNCTION__))
;
116 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
117}
118
119bool Decl::hasLocalOwningModuleStorage() const {
120 return getASTContext().getLangOpts().trackLocalOwningModule();
121}
122
123const char *Decl::getDeclKindName() const {
124 switch (DeclKind) {
125 default: llvm_unreachable("Declaration not in DeclNodes.inc!")::llvm::llvm_unreachable_internal("Declaration not in DeclNodes.inc!"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 125)
;
126#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
127#define ABSTRACT_DECL(DECL)
128#include "clang/AST/DeclNodes.inc"
129 }
130}
131
132void Decl::setInvalidDecl(bool Invalid) {
133 InvalidDecl = Invalid;
134 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition())(static_cast <bool> (!isa<TagDecl>(this) || !cast
<TagDecl>(this)->isCompleteDefinition()) ? void (0) :
__assert_fail ("!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition()"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 134, __extension__ __PRETTY_FUNCTION__))
;
135 if (!Invalid) {
136 return;
137 }
138
139 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 setAccess(AS_public);
144 }
145
146 // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
147 // are invalid too.
148 if (DecompositionDecl *DD = dyn_cast<DecompositionDecl>(this)) {
149 for (BindingDecl *Binding : DD->bindings()) {
150 Binding->setInvalidDecl();
151 }
152 }
153}
154
155const char *DeclContext::getDeclKindName() const {
156 switch (DeclKind) {
157 default: llvm_unreachable("Declaration context not in DeclNodes.inc!")::llvm::llvm_unreachable_internal("Declaration context not in DeclNodes.inc!"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 157)
;
158#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
159#define ABSTRACT_DECL(DECL)
160#include "clang/AST/DeclNodes.inc"
161 }
162}
163
164bool Decl::StatisticsEnabled = false;
165void Decl::EnableStatistics() {
166 StatisticsEnabled = true;
167}
168
169void Decl::PrintStats() {
170 llvm::errs() << "\n*** Decl Stats:\n";
171
172 int totalDecls = 0;
173#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
174#define ABSTRACT_DECL(DECL)
175#include "clang/AST/DeclNodes.inc"
176 llvm::errs() << " " << totalDecls << " decls total.\n";
177
178 int totalBytes = 0;
179#define DECL(DERIVED, BASE) \
180 if (n##DERIVED##s > 0) { \
181 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
182 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
183 << sizeof(DERIVED##Decl) << " each (" \
184 << n##DERIVED##s * sizeof(DERIVED##Decl) \
185 << " bytes)\n"; \
186 }
187#define ABSTRACT_DECL(DECL)
188#include "clang/AST/DeclNodes.inc"
189
190 llvm::errs() << "Total bytes = " << totalBytes << "\n";
191}
192
193void Decl::add(Kind k) {
194 switch (k) {
195#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
196#define ABSTRACT_DECL(DECL)
197#include "clang/AST/DeclNodes.inc"
198 }
199}
200
201bool Decl::isTemplateParameterPack() const {
202 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
203 return TTP->isParameterPack();
204 if (const NonTypeTemplateParmDecl *NTTP
205 = dyn_cast<NonTypeTemplateParmDecl>(this))
206 return NTTP->isParameterPack();
207 if (const TemplateTemplateParmDecl *TTP
208 = dyn_cast<TemplateTemplateParmDecl>(this))
209 return TTP->isParameterPack();
210 return false;
211}
212
213bool Decl::isParameterPack() const {
214 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
215 return Parm->isParameterPack();
216
217 return isTemplateParameterPack();
218}
219
220FunctionDecl *Decl::getAsFunction() {
221 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
222 return FD;
223 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
224 return FTD->getTemplatedDecl();
225 return nullptr;
226}
227
228bool Decl::isTemplateDecl() const {
229 return isa<TemplateDecl>(this);
230}
231
232TemplateDecl *Decl::getDescribedTemplate() const {
233 if (auto *FD = dyn_cast<FunctionDecl>(this))
234 return FD->getDescribedFunctionTemplate();
235 else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
236 return RD->getDescribedClassTemplate();
237 else if (auto *VD = dyn_cast<VarDecl>(this))
238 return VD->getDescribedVarTemplate();
239 else if (auto *AD = dyn_cast<TypeAliasDecl>(this))
240 return AD->getDescribedAliasTemplate();
241
242 return nullptr;
243}
244
245bool Decl::isTemplated() const {
246 // A declaration is dependent if it is a template or a template pattern, or
247 // is within (lexcially for a friend, semantically otherwise) a dependent
248 // context.
249 // FIXME: Should local extern declarations be treated like friends?
250 if (auto *AsDC = dyn_cast<DeclContext>(this))
251 return AsDC->isDependentContext();
252 auto *DC = getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
253 return DC->isDependentContext() || isTemplateDecl() || getDescribedTemplate();
254}
255
256const DeclContext *Decl::getParentFunctionOrMethod() const {
257 for (const DeclContext *DC = getDeclContext();
258 DC && !DC->isTranslationUnit() && !DC->isNamespace();
259 DC = DC->getParent())
260 if (DC->isFunctionOrMethod())
261 return DC;
262
263 return nullptr;
264}
265
266//===----------------------------------------------------------------------===//
267// PrettyStackTraceDecl Implementation
268//===----------------------------------------------------------------------===//
269
270void PrettyStackTraceDecl::print(raw_ostream &OS) const {
271 SourceLocation TheLoc = Loc;
272 if (TheLoc.isInvalid() && TheDecl)
273 TheLoc = TheDecl->getLocation();
274
275 if (TheLoc.isValid()) {
276 TheLoc.print(OS, SM);
277 OS << ": ";
278 }
279
280 OS << Message;
281
282 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
283 OS << " '";
284 DN->printQualifiedName(OS);
285 OS << '\'';
286 }
287 OS << '\n';
288}
289
290//===----------------------------------------------------------------------===//
291// Decl Implementation
292//===----------------------------------------------------------------------===//
293
294// Out-of-line virtual method providing a home for Decl.
295Decl::~Decl() = default;
296
297void Decl::setDeclContext(DeclContext *DC) {
298 DeclCtx = DC;
299}
300
301void Decl::setLexicalDeclContext(DeclContext *DC) {
302 if (DC == getLexicalDeclContext())
303 return;
304
305 if (isInSemaDC()) {
306 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
307 } else {
308 getMultipleDC()->LexicalDC = DC;
309 }
310
311 // FIXME: We shouldn't be changing the lexical context of declarations
312 // imported from AST files.
313 if (!isFromASTFile()) {
314 setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
315 if (hasOwningModule())
316 setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
317 }
318
319 assert((static_cast <bool> ((getModuleOwnershipKind() != ModuleOwnershipKind
::VisibleWhenImported || getOwningModule()) && "hidden declaration has no owning module"
) ? void (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 322, __extension__ __PRETTY_FUNCTION__))
320 (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||(static_cast <bool> ((getModuleOwnershipKind() != ModuleOwnershipKind
::VisibleWhenImported || getOwningModule()) && "hidden declaration has no owning module"
) ? void (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 322, __extension__ __PRETTY_FUNCTION__))
321 getOwningModule()) &&(static_cast <bool> ((getModuleOwnershipKind() != ModuleOwnershipKind
::VisibleWhenImported || getOwningModule()) && "hidden declaration has no owning module"
) ? void (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 322, __extension__ __PRETTY_FUNCTION__))
322 "hidden declaration has no owning module")(static_cast <bool> ((getModuleOwnershipKind() != ModuleOwnershipKind
::VisibleWhenImported || getOwningModule()) && "hidden declaration has no owning module"
) ? void (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 322, __extension__ __PRETTY_FUNCTION__))
;
323}
324
325void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
326 ASTContext &Ctx) {
327 if (SemaDC == LexicalDC) {
328 DeclCtx = SemaDC;
329 } else {
330 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
331 MDC->SemanticDC = SemaDC;
332 MDC->LexicalDC = LexicalDC;
333 DeclCtx = MDC;
334 }
335}
336
337bool Decl::isLexicallyWithinFunctionOrMethod() const {
338 const DeclContext *LDC = getLexicalDeclContext();
339 while (true) {
340 if (LDC->isFunctionOrMethod())
341 return true;
342 if (!isa<TagDecl>(LDC))
343 return false;
344 LDC = LDC->getLexicalParent();
345 }
346 return false;
347}
348
349bool Decl::isInAnonymousNamespace() const {
350 for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) {
351 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
352 if (ND->isAnonymousNamespace())
353 return true;
354 }
355
356 return false;
357}
358
359bool Decl::isInStdNamespace() const {
360 return getDeclContext()->isStdNamespace();
361}
362
363TranslationUnitDecl *Decl::getTranslationUnitDecl() {
364 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
365 return TUD;
366
367 DeclContext *DC = getDeclContext();
368 assert(DC && "This decl is not contained in a translation unit!")(static_cast <bool> (DC && "This decl is not contained in a translation unit!"
) ? void (0) : __assert_fail ("DC && \"This decl is not contained in a translation unit!\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 368, __extension__ __PRETTY_FUNCTION__))
;
369
370 while (!DC->isTranslationUnit()) {
371 DC = DC->getParent();
372 assert(DC && "This decl is not contained in a translation unit!")(static_cast <bool> (DC && "This decl is not contained in a translation unit!"
) ? void (0) : __assert_fail ("DC && \"This decl is not contained in a translation unit!\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 372, __extension__ __PRETTY_FUNCTION__))
;
373 }
374
375 return cast<TranslationUnitDecl>(DC);
376}
377
378ASTContext &Decl::getASTContext() const {
379 return getTranslationUnitDecl()->getASTContext();
380}
381
382ASTMutationListener *Decl::getASTMutationListener() const {
383 return getASTContext().getASTMutationListener();
384}
385
386unsigned Decl::getMaxAlignment() const {
387 if (!hasAttrs())
388 return 0;
389
390 unsigned Align = 0;
391 const AttrVec &V = getAttrs();
392 ASTContext &Ctx = getASTContext();
393 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
394 for (; I != E; ++I)
395 Align = std::max(Align, I->getAlignment(Ctx));
396 return Align;
397}
398
399bool Decl::isUsed(bool CheckUsedAttr) const {
400 const Decl *CanonD = getCanonicalDecl();
401 if (CanonD->Used)
402 return true;
403
404 // Check for used attribute.
405 // Ask the most recent decl, since attributes accumulate in the redecl chain.
406 if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
407 return true;
408
409 // The information may have not been deserialized yet. Force deserialization
410 // to complete the needed information.
411 return getMostRecentDecl()->getCanonicalDecl()->Used;
412}
413
414void Decl::markUsed(ASTContext &C) {
415 if (isUsed(false))
416 return;
417
418 if (C.getASTMutationListener())
419 C.getASTMutationListener()->DeclarationMarkedUsed(this);
420
421 setIsUsed();
422}
423
424bool Decl::isReferenced() const {
425 if (Referenced)
426 return true;
427
428 // Check redeclarations.
429 for (auto I : redecls())
430 if (I->Referenced)
431 return true;
432
433 return false;
434}
435
436bool Decl::isExported() const {
437 if (isModulePrivate())
438 return false;
439 // Namespaces are always exported.
440 if (isa<TranslationUnitDecl>(this) || isa<NamespaceDecl>(this))
441 return true;
442 // Otherwise, this is a strictly lexical check.
443 for (auto *DC = getLexicalDeclContext(); DC; DC = DC->getLexicalParent()) {
444 if (cast<Decl>(DC)->isModulePrivate())
445 return false;
446 if (isa<ExportDecl>(DC))
447 return true;
448 }
449 return false;
450}
451
452ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
453 const Decl *Definition = nullptr;
454 if (auto ID = dyn_cast<ObjCInterfaceDecl>(this)) {
455 Definition = ID->getDefinition();
456 } else if (auto PD = dyn_cast<ObjCProtocolDecl>(this)) {
457 Definition = PD->getDefinition();
458 } else if (auto TD = dyn_cast<TagDecl>(this)) {
459 Definition = TD->getDefinition();
460 }
461 if (!Definition)
462 Definition = this;
463
464 if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
465 return attr;
466 if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
467 return dcd->getAttr<ExternalSourceSymbolAttr>();
468 }
469
470 return nullptr;
471}
472
473bool Decl::hasDefiningAttr() const {
474 return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
475}
476
477const Attr *Decl::getDefiningAttr() const {
478 if (AliasAttr *AA = getAttr<AliasAttr>())
479 return AA;
480 if (IFuncAttr *IFA = getAttr<IFuncAttr>())
481 return IFA;
482 return nullptr;
483}
484
485static StringRef getRealizedPlatform(const AvailabilityAttr *A,
486 const ASTContext &Context) {
487 // Check if this is an App Extension "platform", and if so chop off
488 // the suffix for matching with the actual platform.
489 StringRef RealizedPlatform = A->getPlatform()->getName();
490 if (!Context.getLangOpts().AppExt)
491 return RealizedPlatform;
492 size_t suffix = RealizedPlatform.rfind("_app_extension");
493 if (suffix != StringRef::npos)
494 return RealizedPlatform.slice(0, suffix);
495 return RealizedPlatform;
496}
497
498/// \brief Determine the availability of the given declaration based on
499/// the target platform.
500///
501/// When it returns an availability result other than \c AR_Available,
502/// if the \p Message parameter is non-NULL, it will be set to a
503/// string describing why the entity is unavailable.
504///
505/// FIXME: Make these strings localizable, since they end up in
506/// diagnostics.
507static AvailabilityResult CheckAvailability(ASTContext &Context,
508 const AvailabilityAttr *A,
509 std::string *Message,
510 VersionTuple EnclosingVersion) {
511 if (EnclosingVersion.empty())
512 EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
513
514 if (EnclosingVersion.empty())
515 return AR_Available;
516
517 StringRef ActualPlatform = A->getPlatform()->getName();
518 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
519
520 // Match the platform name.
521 if (getRealizedPlatform(A, Context) != TargetPlatform)
522 return AR_Available;
523
524 StringRef PrettyPlatformName
525 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
526
527 if (PrettyPlatformName.empty())
528 PrettyPlatformName = ActualPlatform;
529
530 std::string HintMessage;
531 if (!A->getMessage().empty()) {
532 HintMessage = " - ";
533 HintMessage += A->getMessage();
534 }
535
536 // Make sure that this declaration has not been marked 'unavailable'.
537 if (A->getUnavailable()) {
538 if (Message) {
539 Message->clear();
540 llvm::raw_string_ostream Out(*Message);
541 Out << "not available on " << PrettyPlatformName
542 << HintMessage;
543 }
544
545 return AR_Unavailable;
546 }
547
548 // Make sure that this declaration has already been introduced.
549 if (!A->getIntroduced().empty() &&
550 EnclosingVersion < A->getIntroduced()) {
551 if (Message) {
552 Message->clear();
553 llvm::raw_string_ostream Out(*Message);
554 VersionTuple VTI(A->getIntroduced());
555 VTI.UseDotAsSeparator();
556 Out << "introduced in " << PrettyPlatformName << ' '
557 << VTI << HintMessage;
558 }
559
560 return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
561 }
562
563 // Make sure that this declaration hasn't been obsoleted.
564 if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
565 if (Message) {
566 Message->clear();
567 llvm::raw_string_ostream Out(*Message);
568 VersionTuple VTO(A->getObsoleted());
569 VTO.UseDotAsSeparator();
570 Out << "obsoleted in " << PrettyPlatformName << ' '
571 << VTO << HintMessage;
572 }
573
574 return AR_Unavailable;
575 }
576
577 // Make sure that this declaration hasn't been deprecated.
578 if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
579 if (Message) {
580 Message->clear();
581 llvm::raw_string_ostream Out(*Message);
582 VersionTuple VTD(A->getDeprecated());
583 VTD.UseDotAsSeparator();
584 Out << "first deprecated in " << PrettyPlatformName << ' '
585 << VTD << HintMessage;
586 }
587
588 return AR_Deprecated;
589 }
590
591 return AR_Available;
592}
593
594AvailabilityResult Decl::getAvailability(std::string *Message,
595 VersionTuple EnclosingVersion) const {
596 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
597 return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion);
598
599 AvailabilityResult Result = AR_Available;
600 std::string ResultMessage;
601
602 for (const auto *A : attrs()) {
603 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
604 if (Result >= AR_Deprecated)
605 continue;
606
607 if (Message)
608 ResultMessage = Deprecated->getMessage();
609
610 Result = AR_Deprecated;
611 continue;
612 }
613
614 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
615 if (Message)
616 *Message = Unavailable->getMessage();
617 return AR_Unavailable;
618 }
619
620 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
621 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
622 Message, EnclosingVersion);
623
624 if (AR == AR_Unavailable)
625 return AR_Unavailable;
626
627 if (AR > Result) {
628 Result = AR;
629 if (Message)
630 ResultMessage.swap(*Message);
631 }
632 continue;
633 }
634 }
635
636 if (Message)
637 Message->swap(ResultMessage);
638 return Result;
639}
640
641VersionTuple Decl::getVersionIntroduced() const {
642 const ASTContext &Context = getASTContext();
643 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
644 for (const auto *A : attrs()) {
645 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
646 if (getRealizedPlatform(Availability, Context) != TargetPlatform)
647 continue;
648 if (!Availability->getIntroduced().empty())
649 return Availability->getIntroduced();
650 }
651 }
652 return VersionTuple();
653}
654
655bool Decl::canBeWeakImported(bool &IsDefinition) const {
656 IsDefinition = false;
657
658 // Variables, if they aren't definitions.
659 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
660 if (Var->isThisDeclarationADefinition()) {
661 IsDefinition = true;
662 return false;
663 }
664 return true;
665
666 // Functions, if they aren't definitions.
667 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
668 if (FD->hasBody()) {
669 IsDefinition = true;
670 return false;
671 }
672 return true;
673
674 // Objective-C classes, if this is the non-fragile runtime.
675 } else if (isa<ObjCInterfaceDecl>(this) &&
676 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
677 return true;
678
679 // Nothing else.
680 } else {
681 return false;
682 }
683}
684
685bool Decl::isWeakImported() const {
686 bool IsDefinition;
687 if (!canBeWeakImported(IsDefinition))
688 return false;
689
690 for (const auto *A : attrs()) {
691 if (isa<WeakImportAttr>(A))
692 return true;
693
694 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
695 if (CheckAvailability(getASTContext(), Availability, nullptr,
696 VersionTuple()) == AR_NotYetIntroduced)
697 return true;
698 }
699 }
700
701 return false;
702}
703
704unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
705 switch (DeclKind) {
706 case Function:
707 case CXXDeductionGuide:
708 case CXXMethod:
709 case CXXConstructor:
710 case ConstructorUsingShadow:
711 case CXXDestructor:
712 case CXXConversion:
713 case EnumConstant:
714 case Var:
715 case ImplicitParam:
716 case ParmVar:
717 case ObjCMethod:
718 case ObjCProperty:
719 case MSProperty:
720 return IDNS_Ordinary;
721 case Label:
722 return IDNS_Label;
723 case IndirectField:
724 return IDNS_Ordinary | IDNS_Member;
725
726 case Binding:
727 case NonTypeTemplateParm:
728 case VarTemplate:
729 // These (C++-only) declarations are found by redeclaration lookup for
730 // tag types, so we include them in the tag namespace.
731 return IDNS_Ordinary | IDNS_Tag;
732
733 case ObjCCompatibleAlias:
734 case ObjCInterface:
735 return IDNS_Ordinary | IDNS_Type;
736
737 case Typedef:
738 case TypeAlias:
739 case TemplateTypeParm:
740 case ObjCTypeParam:
741 return IDNS_Ordinary | IDNS_Type;
742
743 case UnresolvedUsingTypename:
744 return IDNS_Ordinary | IDNS_Type | IDNS_Using;
745
746 case UsingShadow:
747 return 0; // we'll actually overwrite this later
748
749 case UnresolvedUsingValue:
750 return IDNS_Ordinary | IDNS_Using;
751
752 case Using:
753 case UsingPack:
754 return IDNS_Using;
755
756 case ObjCProtocol:
757 return IDNS_ObjCProtocol;
758
759 case Field:
760 case ObjCAtDefsField:
761 case ObjCIvar:
762 return IDNS_Member;
763
764 case Record:
765 case CXXRecord:
766 case Enum:
767 return IDNS_Tag | IDNS_Type;
768
769 case Namespace:
770 case NamespaceAlias:
771 return IDNS_Namespace;
772
773 case FunctionTemplate:
774 return IDNS_Ordinary;
775
776 case ClassTemplate:
777 case TemplateTemplateParm:
778 case TypeAliasTemplate:
779 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
780
781 case OMPDeclareReduction:
782 return IDNS_OMPReduction;
783
784 // Never have names.
785 case Friend:
786 case FriendTemplate:
787 case AccessSpec:
788 case LinkageSpec:
789 case Export:
790 case FileScopeAsm:
791 case StaticAssert:
792 case ObjCPropertyImpl:
793 case PragmaComment:
794 case PragmaDetectMismatch:
795 case Block:
796 case Captured:
797 case TranslationUnit:
798 case ExternCContext:
799 case Decomposition:
800
801 case UsingDirective:
802 case BuiltinTemplate:
803 case ClassTemplateSpecialization:
804 case ClassTemplatePartialSpecialization:
805 case ClassScopeFunctionSpecialization:
806 case VarTemplateSpecialization:
807 case VarTemplatePartialSpecialization:
808 case ObjCImplementation:
809 case ObjCCategory:
810 case ObjCCategoryImpl:
811 case Import:
812 case OMPThreadPrivate:
813 case OMPCapturedExpr:
814 case Empty:
815 // Never looked up by name.
816 return 0;
817 }
818
819 llvm_unreachable("Invalid DeclKind!")::llvm::llvm_unreachable_internal("Invalid DeclKind!", "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 819)
;
820}
821
822void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
823 assert(!HasAttrs && "Decl already contains attrs.")(static_cast <bool> (!HasAttrs && "Decl already contains attrs."
) ? void (0) : __assert_fail ("!HasAttrs && \"Decl already contains attrs.\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 823, __extension__ __PRETTY_FUNCTION__))
;
824
825 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
826 assert(AttrBlank.empty() && "HasAttrs was wrong?")(static_cast <bool> (AttrBlank.empty() && "HasAttrs was wrong?"
) ? void (0) : __assert_fail ("AttrBlank.empty() && \"HasAttrs was wrong?\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 826, __extension__ __PRETTY_FUNCTION__))
;
827
828 AttrBlank = attrs;
829 HasAttrs = true;
830}
831
832void Decl::dropAttrs() {
833 if (!HasAttrs) return;
834
835 HasAttrs = false;
836 getASTContext().eraseDeclAttrs(this);
837}
838
839const AttrVec &Decl::getAttrs() const {
840 assert(HasAttrs && "No attrs to get!")(static_cast <bool> (HasAttrs && "No attrs to get!"
) ? void (0) : __assert_fail ("HasAttrs && \"No attrs to get!\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 840, __extension__ __PRETTY_FUNCTION__))
;
841 return getASTContext().getDeclAttrs(this);
842}
843
844Decl *Decl::castFromDeclContext (const DeclContext *D) {
845 Decl::Kind DK = D->getDeclKind();
846 switch(DK) {
847#define DECL(NAME, BASE)
848#define DECL_CONTEXT(NAME) \
849 case Decl::NAME: \
850 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
851#define DECL_CONTEXT_BASE(NAME)
852#include "clang/AST/DeclNodes.inc"
853 default:
854#define DECL(NAME, BASE)
855#define DECL_CONTEXT_BASE(NAME) \
856 if (DK >= first##NAME && DK <= last##NAME) \
857 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
858#include "clang/AST/DeclNodes.inc"
859 llvm_unreachable("a decl that inherits DeclContext isn't handled")::llvm::llvm_unreachable_internal("a decl that inherits DeclContext isn't handled"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 859)
;
860 }
861}
862
863DeclContext *Decl::castToDeclContext(const Decl *D) {
864 Decl::Kind DK = D->getKind();
865 switch(DK) {
866#define DECL(NAME, BASE)
867#define DECL_CONTEXT(NAME) \
868 case Decl::NAME: \
869 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
870#define DECL_CONTEXT_BASE(NAME)
871#include "clang/AST/DeclNodes.inc"
872 default:
873#define DECL(NAME, BASE)
874#define DECL_CONTEXT_BASE(NAME) \
875 if (DK >= first##NAME && DK <= last##NAME) \
876 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
877#include "clang/AST/DeclNodes.inc"
878 llvm_unreachable("a decl that inherits DeclContext isn't handled")::llvm::llvm_unreachable_internal("a decl that inherits DeclContext isn't handled"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 878)
;
879 }
880}
881
882SourceLocation Decl::getBodyRBrace() const {
883 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
884 // FunctionDecl stores EndRangeLoc for this purpose.
885 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
886 const FunctionDecl *Definition;
887 if (FD->hasBody(Definition))
888 return Definition->getSourceRange().getEnd();
889 return SourceLocation();
890 }
891
892 if (Stmt *Body = getBody())
893 return Body->getSourceRange().getEnd();
894
895 return SourceLocation();
896}
897
898bool Decl::AccessDeclContextSanity() const {
899#ifndef NDEBUG
900 // Suppress this check if any of the following hold:
901 // 1. this is the translation unit (and thus has no parent)
902 // 2. this is a template parameter (and thus doesn't belong to its context)
903 // 3. this is a non-type template parameter
904 // 4. the context is not a record
905 // 5. it's invalid
906 // 6. it's a C++0x static_assert.
907 // 7. it's a block literal declaration
908 if (isa<TranslationUnitDecl>(this) ||
909 isa<TemplateTypeParmDecl>(this) ||
910 isa<NonTypeTemplateParmDecl>(this) ||
911 !isa<CXXRecordDecl>(getDeclContext()) ||
912 isInvalidDecl() ||
913 isa<StaticAssertDecl>(this) ||
914 isa<BlockDecl>(this) ||
915 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
916 // as DeclContext (?).
917 isa<ParmVarDecl>(this) ||
918 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
919 // AS_none as access specifier.
920 isa<CXXRecordDecl>(this) ||
921 isa<ClassScopeFunctionSpecializationDecl>(this))
922 return true;
923
924 assert(Access != AS_none &&(static_cast <bool> (Access != AS_none && "Access specifier is AS_none inside a record decl"
) ? void (0) : __assert_fail ("Access != AS_none && \"Access specifier is AS_none inside a record decl\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 925, __extension__ __PRETTY_FUNCTION__))
925 "Access specifier is AS_none inside a record decl")(static_cast <bool> (Access != AS_none && "Access specifier is AS_none inside a record decl"
) ? void (0) : __assert_fail ("Access != AS_none && \"Access specifier is AS_none inside a record decl\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 925, __extension__ __PRETTY_FUNCTION__))
;
926#endif
927 return true;
928}
929
930static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
931static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
932
933const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
934 QualType Ty;
935 if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
936 Ty = D->getType();
937 else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
938 Ty = D->getUnderlyingType();
939 else
940 return nullptr;
941
942 if (Ty->isFunctionPointerType())
943 Ty = Ty->getAs<PointerType>()->getPointeeType();
944 else if (BlocksToo && Ty->isBlockPointerType())
945 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
946
947 return Ty->getAs<FunctionType>();
948}
949
950/// Starting at a given context (a Decl or DeclContext), look for a
951/// code context that is not a closure (a lambda, block, etc.).
952template <class T> static Decl *getNonClosureContext(T *D) {
953 if (getKind(D) == Decl::CXXMethod) {
954 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
955 if (MD->getOverloadedOperator() == OO_Call &&
956 MD->getParent()->isLambda())
957 return getNonClosureContext(MD->getParent()->getParent());
958 return MD;
959 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
960 return FD;
961 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
962 return MD;
963 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
964 return getNonClosureContext(BD->getParent());
965 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
966 return getNonClosureContext(CD->getParent());
967 } else {
968 return nullptr;
969 }
970}
971
972Decl *Decl::getNonClosureContext() {
973 return ::getNonClosureContext(this);
974}
975
976Decl *DeclContext::getNonClosureAncestor() {
977 return ::getNonClosureContext(this);
978}
979
980//===----------------------------------------------------------------------===//
981// DeclContext Implementation
982//===----------------------------------------------------------------------===//
983
984bool DeclContext::classof(const Decl *D) {
985 switch (D->getKind()) {
986#define DECL(NAME, BASE)
987#define DECL_CONTEXT(NAME) case Decl::NAME:
988#define DECL_CONTEXT_BASE(NAME)
989#include "clang/AST/DeclNodes.inc"
990 return true;
991 default:
992#define DECL(NAME, BASE)
993#define DECL_CONTEXT_BASE(NAME) \
994 if (D->getKind() >= Decl::first##NAME && \
995 D->getKind() <= Decl::last##NAME) \
996 return true;
997#include "clang/AST/DeclNodes.inc"
998 return false;
999 }
1000}
1001
1002DeclContext::~DeclContext() = default;
1003
1004/// \brief Find the parent context of this context that will be
1005/// used for unqualified name lookup.
1006///
1007/// Generally, the parent lookup context is the semantic context. However, for
1008/// a friend function the parent lookup context is the lexical context, which
1009/// is the class in which the friend is declared.
1010DeclContext *DeclContext::getLookupParent() {
1011 // FIXME: Find a better way to identify friends
1012 if (isa<FunctionDecl>(this))
1013 if (getParent()->getRedeclContext()->isFileContext() &&
1014 getLexicalParent()->getRedeclContext()->isRecord())
1015 return getLexicalParent();
1016
1017 return getParent();
1018}
1019
1020bool DeclContext::isInlineNamespace() const {
1021 return isNamespace() &&
1022 cast<NamespaceDecl>(this)->isInline();
1023}
1024
1025bool DeclContext::isStdNamespace() const {
1026 if (!isNamespace())
1027 return false;
1028
1029 const NamespaceDecl *ND = cast<NamespaceDecl>(this);
1030 if (ND->isInline()) {
1031 return ND->getParent()->isStdNamespace();
1032 }
1033
1034 if (!getParent()->getRedeclContext()->isTranslationUnit())
1035 return false;
1036
1037 const IdentifierInfo *II = ND->getIdentifier();
1038 return II && II->isStr("std");
1039}
1040
1041bool DeclContext::isDependentContext() const {
1042 if (isFileContext())
1043 return false;
1044
1045 if (isa<ClassTemplatePartialSpecializationDecl>(this))
1046 return true;
1047
1048 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
1049 if (Record->getDescribedClassTemplate())
1050 return true;
1051
1052 if (Record->isDependentLambda())
1053 return true;
1054 }
1055
1056 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
1057 if (Function->getDescribedFunctionTemplate())
1058 return true;
1059
1060 // Friend function declarations are dependent if their *lexical*
1061 // context is dependent.
1062 if (cast<Decl>(this)->getFriendObjectKind())
1063 return getLexicalParent()->isDependentContext();
1064 }
1065
1066 // FIXME: A variable template is a dependent context, but is not a
1067 // DeclContext. A context within it (such as a lambda-expression)
1068 // should be considered dependent.
1069
1070 return getParent() && getParent()->isDependentContext();
1071}
1072
1073bool DeclContext::isTransparentContext() const {
1074 if (DeclKind == Decl::Enum)
1075 return !cast<EnumDecl>(this)->isScoped();
1076 else if (DeclKind == Decl::LinkageSpec || DeclKind == Decl::Export)
1077 return true;
1078
1079 return false;
1080}
1081
1082static bool isLinkageSpecContext(const DeclContext *DC,
1083 LinkageSpecDecl::LanguageIDs ID) {
1084 while (DC->getDeclKind() != Decl::TranslationUnit) {
1085 if (DC->getDeclKind() == Decl::LinkageSpec)
1086 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1087 DC = DC->getLexicalParent();
1088 }
1089 return false;
1090}
1091
1092bool DeclContext::isExternCContext() const {
1093 return isLinkageSpecContext(this, LinkageSpecDecl::lang_c);
1094}
1095
1096const LinkageSpecDecl *DeclContext::getExternCContext() const {
1097 const DeclContext *DC = this;
1098 while (DC->getDeclKind() != Decl::TranslationUnit) {
1099 if (DC->getDeclKind() == Decl::LinkageSpec &&
1100 cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c)
1101 return cast<LinkageSpecDecl>(DC);
1102 DC = DC->getLexicalParent();
1103 }
1104 return nullptr;
1105}
1106
1107bool DeclContext::isExternCXXContext() const {
1108 return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx);
1109}
1110
1111bool DeclContext::Encloses(const DeclContext *DC) const {
1112 if (getPrimaryContext() != this)
1113 return getPrimaryContext()->Encloses(DC);
1114
1115 for (; DC; DC = DC->getParent())
1116 if (DC->getPrimaryContext() == this)
1117 return true;
1118 return false;
1119}
1120
1121DeclContext *DeclContext::getPrimaryContext() {
1122 switch (DeclKind) {
1123 case Decl::TranslationUnit:
1124 case Decl::ExternCContext:
1125 case Decl::LinkageSpec:
1126 case Decl::Export:
1127 case Decl::Block:
1128 case Decl::Captured:
1129 case Decl::OMPDeclareReduction:
1130 // There is only one DeclContext for these entities.
1131 return this;
1132
1133 case Decl::Namespace:
1134 // The original namespace is our primary context.
1135 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
1136
1137 case Decl::ObjCMethod:
1138 return this;
1139
1140 case Decl::ObjCInterface:
1141 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
1142 return Def;
1143 return this;
1144
1145 case Decl::ObjCProtocol:
1146 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
1147 return Def;
1148 return this;
1149
1150 case Decl::ObjCCategory:
1151 return this;
1152
1153 case Decl::ObjCImplementation:
1154 case Decl::ObjCCategoryImpl:
1155 return this;
1156
1157 default:
1158 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
1159 // If this is a tag type that has a definition or is currently
1160 // being defined, that definition is our primary context.
1161 TagDecl *Tag = cast<TagDecl>(this);
1162
1163 if (TagDecl *Def = Tag->getDefinition())
1164 return Def;
1165
1166 if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1167 // Note, TagType::getDecl returns the (partial) definition one exists.
1168 TagDecl *PossiblePartialDef = TagTy->getDecl();
1169 if (PossiblePartialDef->isBeingDefined())
1170 return PossiblePartialDef;
1171 } else {
1172 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()))(static_cast <bool> (isa<InjectedClassNameType>(Tag
->getTypeForDecl())) ? void (0) : __assert_fail ("isa<InjectedClassNameType>(Tag->getTypeForDecl())"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1172, __extension__ __PRETTY_FUNCTION__))
;
1173 }
1174
1175 return Tag;
1176 }
1177
1178 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&(static_cast <bool> (DeclKind >= Decl::firstFunction
&& DeclKind <= Decl::lastFunction && "Unknown DeclContext kind"
) ? void (0) : __assert_fail ("DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && \"Unknown DeclContext kind\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1179, __extension__ __PRETTY_FUNCTION__))
1179 "Unknown DeclContext kind")(static_cast <bool> (DeclKind >= Decl::firstFunction
&& DeclKind <= Decl::lastFunction && "Unknown DeclContext kind"
) ? void (0) : __assert_fail ("DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && \"Unknown DeclContext kind\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1179, __extension__ __PRETTY_FUNCTION__))
;
1180 return this;
1181 }
1182}
1183
1184void
1185DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
1186 Contexts.clear();
1187
1188 if (DeclKind != Decl::Namespace) {
1189 Contexts.push_back(this);
1190 return;
1191 }
1192
1193 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
1194 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1195 N = N->getPreviousDecl())
1196 Contexts.push_back(N);
1197
1198 std::reverse(Contexts.begin(), Contexts.end());
1199}
1200
1201std::pair<Decl *, Decl *>
1202DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
1203 bool FieldsAlreadyLoaded) {
1204 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1205 Decl *FirstNewDecl = nullptr;
1206 Decl *PrevDecl = nullptr;
1207 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1208 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1209 continue;
1210
1211 Decl *D = Decls[I];
1212 if (PrevDecl)
1213 PrevDecl->NextInContextAndBits.setPointer(D);
1214 else
1215 FirstNewDecl = D;
1216
1217 PrevDecl = D;
1218 }
1219
1220 return std::make_pair(FirstNewDecl, PrevDecl);
1221}
1222
1223/// \brief We have just acquired external visible storage, and we already have
1224/// built a lookup map. For every name in the map, pull in the new names from
1225/// the external storage.
1226void DeclContext::reconcileExternalVisibleStorage() const {
1227 assert(NeedToReconcileExternalVisibleStorage && LookupPtr)(static_cast <bool> (NeedToReconcileExternalVisibleStorage
&& LookupPtr) ? void (0) : __assert_fail ("NeedToReconcileExternalVisibleStorage && LookupPtr"
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1227, __extension__ __PRETTY_FUNCTION__))
;
1228 NeedToReconcileExternalVisibleStorage = false;
1229
1230 for (auto &Lookup : *LookupPtr)
1231 Lookup.second.setHasExternalDecls();
1232}
1233
1234/// \brief Load the declarations within this lexical storage from an
1235/// external source.
1236/// \return \c true if any declarations were added.
1237bool
1238DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1239 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1240 assert(hasExternalLexicalStorage() && Source && "No external storage?")(static_cast <bool> (hasExternalLexicalStorage() &&
Source && "No external storage?") ? void (0) : __assert_fail
("hasExternalLexicalStorage() && Source && \"No external storage?\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1240, __extension__ __PRETTY_FUNCTION__))
;
1241
1242 // Notify that we have a DeclContext that is initializing.
1243 ExternalASTSource::Deserializing ADeclContext(Source);
1244
1245 // Load the external declarations, if any.
1246 SmallVector<Decl*, 64> Decls;
1247 ExternalLexicalStorage = false;
1248 Source->FindExternalLexicalDecls(this, Decls);
1249
1250 if (Decls.empty())
1251 return false;
1252
1253 // We may have already loaded just the fields of this record, in which case
1254 // we need to ignore them.
1255 bool FieldsAlreadyLoaded = false;
1256 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1257 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1258
1259 // Splice the newly-read declarations into the beginning of the list
1260 // of declarations.
1261 Decl *ExternalFirst, *ExternalLast;
1262 std::tie(ExternalFirst, ExternalLast) =
1263 BuildDeclChain(Decls, FieldsAlreadyLoaded);
1264 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1265 FirstDecl = ExternalFirst;
1266 if (!LastDecl)
1267 LastDecl = ExternalLast;
1268 return true;
1269}
1270
1271DeclContext::lookup_result
1272ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1273 DeclarationName Name) {
1274 ASTContext &Context = DC->getParentASTContext();
1275 StoredDeclsMap *Map;
1276 if (!(Map = DC->LookupPtr))
1277 Map = DC->CreateStoredDeclsMap(Context);
1278 if (DC->NeedToReconcileExternalVisibleStorage)
1279 DC->reconcileExternalVisibleStorage();
1280
1281 (*Map)[Name].removeExternalDecls();
1282
1283 return DeclContext::lookup_result();
1284}
1285
1286DeclContext::lookup_result
1287ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1288 DeclarationName Name,
1289 ArrayRef<NamedDecl*> Decls) {
1290 ASTContext &Context = DC->getParentASTContext();
1291 StoredDeclsMap *Map;
1292 if (!(Map = DC->LookupPtr))
1293 Map = DC->CreateStoredDeclsMap(Context);
1294 if (DC->NeedToReconcileExternalVisibleStorage)
1295 DC->reconcileExternalVisibleStorage();
1296
1297 StoredDeclsList &List = (*Map)[Name];
1298
1299 // Clear out any old external visible declarations, to avoid quadratic
1300 // performance in the redeclaration checks below.
1301 List.removeExternalDecls();
1302
1303 if (!List.isNull()) {
1304 // We have both existing declarations and new declarations for this name.
1305 // Some of the declarations may simply replace existing ones. Handle those
1306 // first.
1307 llvm::SmallVector<unsigned, 8> Skip;
1308 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1309 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
1310 Skip.push_back(I);
1311 Skip.push_back(Decls.size());
1312
1313 // Add in any new declarations.
1314 unsigned SkipPos = 0;
1315 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1316 if (I == Skip[SkipPos])
1317 ++SkipPos;
1318 else
1319 List.AddSubsequentDecl(Decls[I]);
1320 }
1321 } else {
1322 // Convert the array to a StoredDeclsList.
1323 for (ArrayRef<NamedDecl*>::iterator
1324 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1325 if (List.isNull())
1326 List.setOnlyValue(*I);
1327 else
1328 List.AddSubsequentDecl(*I);
1329 }
1330 }
1331
1332 return List.getLookupResult();
1333}
1334
1335DeclContext::decl_iterator DeclContext::decls_begin() const {
1336 if (hasExternalLexicalStorage())
1337 LoadLexicalDeclsFromExternalStorage();
1338 return decl_iterator(FirstDecl);
1339}
1340
1341bool DeclContext::decls_empty() const {
1342 if (hasExternalLexicalStorage())
1343 LoadLexicalDeclsFromExternalStorage();
1344
1345 return !FirstDecl;
1346}
1347
1348bool DeclContext::containsDecl(Decl *D) const {
1349 return (D->getLexicalDeclContext() == this &&
1350 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1351}
1352
1353void DeclContext::removeDecl(Decl *D) {
1354 assert(D->getLexicalDeclContext() == this &&(static_cast <bool> (D->getLexicalDeclContext() == this
&& "decl being removed from non-lexical context") ? void
(0) : __assert_fail ("D->getLexicalDeclContext() == this && \"decl being removed from non-lexical context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1355, __extension__ __PRETTY_FUNCTION__))
1355 "decl being removed from non-lexical context")(static_cast <bool> (D->getLexicalDeclContext() == this
&& "decl being removed from non-lexical context") ? void
(0) : __assert_fail ("D->getLexicalDeclContext() == this && \"decl being removed from non-lexical context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1355, __extension__ __PRETTY_FUNCTION__))
;
1356 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&(static_cast <bool> ((D->NextInContextAndBits.getPointer
() || D == LastDecl) && "decl is not in decls list") ?
void (0) : __assert_fail ("(D->NextInContextAndBits.getPointer() || D == LastDecl) && \"decl is not in decls list\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1357, __extension__ __PRETTY_FUNCTION__))
1357 "decl is not in decls list")(static_cast <bool> ((D->NextInContextAndBits.getPointer
() || D == LastDecl) && "decl is not in decls list") ?
void (0) : __assert_fail ("(D->NextInContextAndBits.getPointer() || D == LastDecl) && \"decl is not in decls list\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1357, __extension__ __PRETTY_FUNCTION__))
;
1358
1359 // Remove D from the decl chain. This is O(n) but hopefully rare.
1360 if (D == FirstDecl) {
1361 if (D == LastDecl)
1362 FirstDecl = LastDecl = nullptr;
1363 else
1364 FirstDecl = D->NextInContextAndBits.getPointer();
1365 } else {
1366 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
1367 assert(I && "decl not found in linked list")(static_cast <bool> (I && "decl not found in linked list"
) ? void (0) : __assert_fail ("I && \"decl not found in linked list\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1367, __extension__ __PRETTY_FUNCTION__))
;
1368 if (I->NextInContextAndBits.getPointer() == D) {
1369 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1370 if (D == LastDecl) LastDecl = I;
1371 break;
1372 }
1373 }
1374 }
1375
1376 // Mark that D is no longer in the decl chain.
1377 D->NextInContextAndBits.setPointer(nullptr);
1378
1379 // Remove D from the lookup table if necessary.
1380 if (isa<NamedDecl>(D)) {
1381 NamedDecl *ND = cast<NamedDecl>(D);
1382
1383 // Remove only decls that have a name
1384 if (!ND->getDeclName()) return;
1385
1386 auto *DC = D->getDeclContext();
1387 do {
1388 StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1389 if (Map) {
1390 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1391 assert(Pos != Map->end() && "no lookup entry for decl")(static_cast <bool> (Pos != Map->end() && "no lookup entry for decl"
) ? void (0) : __assert_fail ("Pos != Map->end() && \"no lookup entry for decl\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1391, __extension__ __PRETTY_FUNCTION__))
;
1392 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1393 Pos->second.remove(ND);
1394 }
1395 } while (DC->isTransparentContext() && (DC = DC->getParent()));
1396 }
1397}
1398
1399void DeclContext::addHiddenDecl(Decl *D) {
1400 assert(D->getLexicalDeclContext() == this &&(static_cast <bool> (D->getLexicalDeclContext() == this
&& "Decl inserted into wrong lexical context") ? void
(0) : __assert_fail ("D->getLexicalDeclContext() == this && \"Decl inserted into wrong lexical context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1401, __extension__ __PRETTY_FUNCTION__))
1401 "Decl inserted into wrong lexical context")(static_cast <bool> (D->getLexicalDeclContext() == this
&& "Decl inserted into wrong lexical context") ? void
(0) : __assert_fail ("D->getLexicalDeclContext() == this && \"Decl inserted into wrong lexical context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1401, __extension__ __PRETTY_FUNCTION__))
;
1402 assert(!D->getNextDeclInContext() && D != LastDecl &&(static_cast <bool> (!D->getNextDeclInContext() &&
D != LastDecl && "Decl already inserted into a DeclContext"
) ? void (0) : __assert_fail ("!D->getNextDeclInContext() && D != LastDecl && \"Decl already inserted into a DeclContext\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1403, __extension__ __PRETTY_FUNCTION__))
1403 "Decl already inserted into a DeclContext")(static_cast <bool> (!D->getNextDeclInContext() &&
D != LastDecl && "Decl already inserted into a DeclContext"
) ? void (0) : __assert_fail ("!D->getNextDeclInContext() && D != LastDecl && \"Decl already inserted into a DeclContext\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1403, __extension__ __PRETTY_FUNCTION__))
;
1404
1405 if (FirstDecl) {
1406 LastDecl->NextInContextAndBits.setPointer(D);
1407 LastDecl = D;
1408 } else {
1409 FirstDecl = LastDecl = D;
1410 }
1411
1412 // Notify a C++ record declaration that we've added a member, so it can
1413 // update its class-specific state.
1414 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1415 Record->addedMember(D);
1416
1417 // If this is a newly-created (not de-serialized) import declaration, wire
1418 // it in to the list of local import declarations.
1419 if (!D->isFromASTFile()) {
1420 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1421 D->getASTContext().addedLocalImportDecl(Import);
1422 }
1423}
1424
1425void DeclContext::addDecl(Decl *D) {
1426 addHiddenDecl(D);
1427
1428 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1429 ND->getDeclContext()->getPrimaryContext()->
1430 makeDeclVisibleInContextWithFlags(ND, false, true);
1431}
1432
1433void DeclContext::addDeclInternal(Decl *D) {
1434 addHiddenDecl(D);
1435
1436 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1437 ND->getDeclContext()->getPrimaryContext()->
1438 makeDeclVisibleInContextWithFlags(ND, true, true);
1439}
1440
1441/// shouldBeHidden - Determine whether a declaration which was declared
1442/// within its semantic context should be invisible to qualified name lookup.
1443static bool shouldBeHidden(NamedDecl *D) {
1444 // Skip unnamed declarations.
1445 if (!D->getDeclName())
1446 return true;
1447
1448 // Skip entities that can't be found by name lookup into a particular
1449 // context.
1450 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1451 D->isTemplateParameter())
1452 return true;
1453
1454 // Skip template specializations.
1455 // FIXME: This feels like a hack. Should DeclarationName support
1456 // template-ids, or is there a better way to keep specializations
1457 // from being visible?
1458 if (isa<ClassTemplateSpecializationDecl>(D))
1459 return true;
1460 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1461 if (FD->isFunctionTemplateSpecialization())
1462 return true;
1463
1464 return false;
1465}
1466
1467/// buildLookup - Build the lookup data structure with all of the
1468/// declarations in this DeclContext (and any other contexts linked
1469/// to it or transparent contexts nested within it) and return it.
1470///
1471/// Note that the produced map may miss out declarations from an
1472/// external source. If it does, those entries will be marked with
1473/// the 'hasExternalDecls' flag.
1474StoredDeclsMap *DeclContext::buildLookup() {
1475 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC")(static_cast <bool> (this == getPrimaryContext() &&
"buildLookup called on non-primary DC") ? void (0) : __assert_fail
("this == getPrimaryContext() && \"buildLookup called on non-primary DC\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1475, __extension__ __PRETTY_FUNCTION__))
;
1476
1477 if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1478 return LookupPtr;
1479
1480 SmallVector<DeclContext *, 2> Contexts;
1481 collectAllContexts(Contexts);
1482
1483 if (HasLazyExternalLexicalLookups) {
1484 HasLazyExternalLexicalLookups = false;
1485 for (auto *DC : Contexts) {
1486 if (DC->hasExternalLexicalStorage())
1487 HasLazyLocalLexicalLookups |=
1488 DC->LoadLexicalDeclsFromExternalStorage();
1489 }
1490
1491 if (!HasLazyLocalLexicalLookups)
1492 return LookupPtr;
1493 }
1494
1495 for (auto *DC : Contexts)
1496 buildLookupImpl(DC, hasExternalVisibleStorage());
1497
1498 // We no longer have any lazy decls.
1499 HasLazyLocalLexicalLookups = false;
1500 return LookupPtr;
1501}
1502
1503/// buildLookupImpl - Build part of the lookup data structure for the
1504/// declarations contained within DCtx, which will either be this
1505/// DeclContext, a DeclContext linked to it, or a transparent context
1506/// nested within it.
1507void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1508 for (Decl *D : DCtx->noload_decls()) {
1509 // Insert this declaration into the lookup structure, but only if
1510 // it's semantically within its decl context. Any other decls which
1511 // should be found in this context are added eagerly.
1512 //
1513 // If it's from an AST file, don't add it now. It'll get handled by
1514 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1515 // in C++, we do not track external visible decls for the TU, so in
1516 // that case we need to collect them all here.
1517 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1518 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1519 (!ND->isFromASTFile() ||
1520 (isTranslationUnit() &&
1521 !getParentASTContext().getLangOpts().CPlusPlus)))
1522 makeDeclVisibleInContextImpl(ND, Internal);
1523
1524 // If this declaration is itself a transparent declaration context
1525 // or inline namespace, add the members of this declaration of that
1526 // context (recursively).
1527 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1528 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1529 buildLookupImpl(InnerCtx, Internal);
1530 }
1531}
1532
1533NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1534
1535DeclContext::lookup_result
1536DeclContext::lookup(DeclarationName Name) const {
1537 assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&(static_cast <bool> (DeclKind != Decl::LinkageSpec &&
DeclKind != Decl::Export && "should not perform lookups into transparent contexts"
) ? void (0) : __assert_fail ("DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1538, __extension__ __PRETTY_FUNCTION__))
1538 "should not perform lookups into transparent contexts")(static_cast <bool> (DeclKind != Decl::LinkageSpec &&
DeclKind != Decl::Export && "should not perform lookups into transparent contexts"
) ? void (0) : __assert_fail ("DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1538, __extension__ __PRETTY_FUNCTION__))
;
1539
1540 const DeclContext *PrimaryContext = getPrimaryContext();
1541 if (PrimaryContext != this)
1542 return PrimaryContext->lookup(Name);
1543
1544 // If we have an external source, ensure that any later redeclarations of this
1545 // context have been loaded, since they may add names to the result of this
1546 // lookup (or add external visible storage).
1547 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1548 if (Source)
1549 (void)cast<Decl>(this)->getMostRecentDecl();
1550
1551 if (hasExternalVisibleStorage()) {
1552 assert(Source && "external visible storage but no external source?")(static_cast <bool> (Source && "external visible storage but no external source?"
) ? void (0) : __assert_fail ("Source && \"external visible storage but no external source?\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1552, __extension__ __PRETTY_FUNCTION__))
;
1553
1554 if (NeedToReconcileExternalVisibleStorage)
1555 reconcileExternalVisibleStorage();
1556
1557 StoredDeclsMap *Map = LookupPtr;
1558
1559 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1560 // FIXME: Make buildLookup const?
1561 Map = const_cast<DeclContext*>(this)->buildLookup();
1562
1563 if (!Map)
1564 Map = CreateStoredDeclsMap(getParentASTContext());
1565
1566 // If we have a lookup result with no external decls, we are done.
1567 std::pair<StoredDeclsMap::iterator, bool> R =
1568 Map->insert(std::make_pair(Name, StoredDeclsList()));
1569 if (!R.second && !R.first->second.hasExternalDecls())
1570 return R.first->second.getLookupResult();
1571
1572 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
1573 if (StoredDeclsMap *Map = LookupPtr) {
1574 StoredDeclsMap::iterator I = Map->find(Name);
1575 if (I != Map->end())
1576 return I->second.getLookupResult();
1577 }
1578 }
1579
1580 return lookup_result();
1581 }
1582
1583 StoredDeclsMap *Map = LookupPtr;
1584 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1585 Map = const_cast<DeclContext*>(this)->buildLookup();
1586
1587 if (!Map)
1588 return lookup_result();
1589
1590 StoredDeclsMap::iterator I = Map->find(Name);
1591 if (I == Map->end())
1592 return lookup_result();
1593
1594 return I->second.getLookupResult();
1595}
1596
1597DeclContext::lookup_result
1598DeclContext::noload_lookup(DeclarationName Name) {
1599 assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&(static_cast <bool> (DeclKind != Decl::LinkageSpec &&
DeclKind != Decl::Export && "should not perform lookups into transparent contexts"
) ? void (0) : __assert_fail ("DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1600, __extension__ __PRETTY_FUNCTION__))
1600 "should not perform lookups into transparent contexts")(static_cast <bool> (DeclKind != Decl::LinkageSpec &&
DeclKind != Decl::Export && "should not perform lookups into transparent contexts"
) ? void (0) : __assert_fail ("DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1600, __extension__ __PRETTY_FUNCTION__))
;
1601
1602 DeclContext *PrimaryContext = getPrimaryContext();
1603 if (PrimaryContext != this)
1604 return PrimaryContext->noload_lookup(Name);
1605
1606 loadLazyLocalLexicalLookups();
1607 StoredDeclsMap *Map = LookupPtr;
1608 if (!Map)
1609 return lookup_result();
1610
1611 StoredDeclsMap::iterator I = Map->find(Name);
1612 return I != Map->end() ? I->second.getLookupResult()
1613 : lookup_result();
1614}
1615
1616// If we have any lazy lexical declarations not in our lookup map, add them
1617// now. Don't import any external declarations, not even if we know we have
1618// some missing from the external visible lookups.
1619void DeclContext::loadLazyLocalLexicalLookups() {
1620 if (HasLazyLocalLexicalLookups) {
1621 SmallVector<DeclContext *, 2> Contexts;
1622 collectAllContexts(Contexts);
1623 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1624 buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1625 HasLazyLocalLexicalLookups = false;
1626 }
1627}
1628
1629void DeclContext::localUncachedLookup(DeclarationName Name,
1630 SmallVectorImpl<NamedDecl *> &Results) {
1631 Results.clear();
1632
1633 // If there's no external storage, just perform a normal lookup and copy
1634 // the results.
1635 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1636 lookup_result LookupResults = lookup(Name);
1637 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1638 return;
1639 }
1640
1641 // If we have a lookup table, check there first. Maybe we'll get lucky.
1642 // FIXME: Should we be checking these flags on the primary context?
1643 if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1644 if (StoredDeclsMap *Map = LookupPtr) {
1645 StoredDeclsMap::iterator Pos = Map->find(Name);
1646 if (Pos != Map->end()) {
1647 Results.insert(Results.end(),
1648 Pos->second.getLookupResult().begin(),
1649 Pos->second.getLookupResult().end());
1650 return;
1651 }
1652 }
1653 }
1654
1655 // Slow case: grovel through the declarations in our chain looking for
1656 // matches.
1657 // FIXME: If we have lazy external declarations, this will not find them!
1658 // FIXME: Should we CollectAllContexts and walk them all here?
1659 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1660 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1661 if (ND->getDeclName() == Name)
1662 Results.push_back(ND);
1663 }
1664}
1665
1666DeclContext *DeclContext::getRedeclContext() {
1667 DeclContext *Ctx = this;
1668 // Skip through transparent contexts.
1669 while (Ctx->isTransparentContext())
1670 Ctx = Ctx->getParent();
1671 return Ctx;
1672}
1673
1674DeclContext *DeclContext::getEnclosingNamespaceContext() {
1675 DeclContext *Ctx = this;
1676 // Skip through non-namespace, non-translation-unit contexts.
1677 while (!Ctx->isFileContext())
1678 Ctx = Ctx->getParent();
1679 return Ctx->getPrimaryContext();
1680}
1681
1682RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1683 // Loop until we find a non-record context.
1684 RecordDecl *OutermostRD = nullptr;
1685 DeclContext *DC = this;
1686 while (DC->isRecord()) {
1687 OutermostRD = cast<RecordDecl>(DC);
1688 DC = DC->getLexicalParent();
1689 }
1690 return OutermostRD;
1691}
1692
1693bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1694 // For non-file contexts, this is equivalent to Equals.
1695 if (!isFileContext())
1696 return O->Equals(this);
1697
1698 do {
1699 if (O->Equals(this))
1700 return true;
1701
1702 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1703 if (!NS || !NS->isInline())
1704 break;
1705 O = NS->getParent();
1706 } while (O);
1707
1708 return false;
1709}
1710
1711void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1712 DeclContext *PrimaryDC = this->getPrimaryContext();
1713 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1714 // If the decl is being added outside of its semantic decl context, we
1715 // need to ensure that we eagerly build the lookup information for it.
1716 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1717}
1718
1719void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1720 bool Recoverable) {
1721 assert(this == getPrimaryContext() && "expected a primary DC")(static_cast <bool> (this == getPrimaryContext() &&
"expected a primary DC") ? void (0) : __assert_fail ("this == getPrimaryContext() && \"expected a primary DC\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1721, __extension__ __PRETTY_FUNCTION__))
;
1722
1723 if (!isLookupContext()) {
1724 if (isTransparentContext())
1725 getParent()->getPrimaryContext()
1726 ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1727 return;
1728 }
1729
1730 // Skip declarations which should be invisible to name lookup.
1731 if (shouldBeHidden(D))
1732 return;
1733
1734 // If we already have a lookup data structure, perform the insertion into
1735 // it. If we might have externally-stored decls with this name, look them
1736 // up and perform the insertion. If this decl was declared outside its
1737 // semantic context, buildLookup won't add it, so add it now.
1738 //
1739 // FIXME: As a performance hack, don't add such decls into the translation
1740 // unit unless we're in C++, since qualified lookup into the TU is never
1741 // performed.
1742 if (LookupPtr || hasExternalVisibleStorage() ||
1743 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1744 (getParentASTContext().getLangOpts().CPlusPlus ||
1745 !isTranslationUnit()))) {
1746 // If we have lazily omitted any decls, they might have the same name as
1747 // the decl which we are adding, so build a full lookup table before adding
1748 // this decl.
1749 buildLookup();
1750 makeDeclVisibleInContextImpl(D, Internal);
1751 } else {
1752 HasLazyLocalLexicalLookups = true;
1753 }
1754
1755 // If we are a transparent context or inline namespace, insert into our
1756 // parent context, too. This operation is recursive.
1757 if (isTransparentContext() || isInlineNamespace())
1758 getParent()->getPrimaryContext()->
1759 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1760
1761 Decl *DCAsDecl = cast<Decl>(this);
1762 // Notify that a decl was made visible unless we are a Tag being defined.
1763 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1764 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1765 L->AddedVisibleDecl(this, D);
1766}
1767
1768void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1769 // Find or create the stored declaration map.
1770 StoredDeclsMap *Map = LookupPtr;
1771 if (!Map) {
1772 ASTContext *C = &getParentASTContext();
1773 Map = CreateStoredDeclsMap(*C);
1774 }
1775
1776 // If there is an external AST source, load any declarations it knows about
1777 // with this declaration's name.
1778 // If the lookup table contains an entry about this name it means that we
1779 // have already checked the external source.
1780 if (!Internal)
1781 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1782 if (hasExternalVisibleStorage() &&
1783 Map->find(D->getDeclName()) == Map->end())
1784 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1785
1786 // Insert this declaration into the map.
1787 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1788
1789 if (Internal) {
1790 // If this is being added as part of loading an external declaration,
1791 // this may not be the only external declaration with this name.
1792 // In this case, we never try to replace an existing declaration; we'll
1793 // handle that when we finalize the list of declarations for this name.
1794 DeclNameEntries.setHasExternalDecls();
1795 DeclNameEntries.AddSubsequentDecl(D);
1796 return;
1797 }
1798
1799 if (DeclNameEntries.isNull()) {
1800 DeclNameEntries.setOnlyValue(D);
1801 return;
1802 }
1803
1804 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
1805 // This declaration has replaced an existing one for which
1806 // declarationReplaces returns true.
1807 return;
1808 }
1809
1810 // Put this declaration into the appropriate slot.
1811 DeclNameEntries.AddSubsequentDecl(D);
1812}
1813
1814UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1815 return cast<UsingDirectiveDecl>(*I);
1816}
1817
1818/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1819/// this context.
1820DeclContext::udir_range DeclContext::using_directives() const {
1821 // FIXME: Use something more efficient than normal lookup for using
1822 // directives. In C++, using directives are looked up more than anything else.
1823 lookup_result Result = lookup(UsingDirectiveDecl::getName());
1824 return udir_range(Result.begin(), Result.end());
1825}
1826
1827//===----------------------------------------------------------------------===//
1828// Creation and Destruction of StoredDeclsMaps. //
1829//===----------------------------------------------------------------------===//
1830
1831StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1832 assert(!LookupPtr && "context already has a decls map")(static_cast <bool> (!LookupPtr && "context already has a decls map"
) ? void (0) : __assert_fail ("!LookupPtr && \"context already has a decls map\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1832, __extension__ __PRETTY_FUNCTION__))
;
1833 assert(getPrimaryContext() == this &&(static_cast <bool> (getPrimaryContext() == this &&
"creating decls map on non-primary context") ? void (0) : __assert_fail
("getPrimaryContext() == this && \"creating decls map on non-primary context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1834, __extension__ __PRETTY_FUNCTION__))
1834 "creating decls map on non-primary context")(static_cast <bool> (getPrimaryContext() == this &&
"creating decls map on non-primary context") ? void (0) : __assert_fail
("getPrimaryContext() == this && \"creating decls map on non-primary context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1834, __extension__ __PRETTY_FUNCTION__))
;
1835
1836 StoredDeclsMap *M;
1837 bool Dependent = isDependentContext();
1838 if (Dependent)
1839 M = new DependentStoredDeclsMap();
1840 else
1841 M = new StoredDeclsMap();
1842 M->Previous = C.LastSDM;
1843 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1844 LookupPtr = M;
1845 return M;
1846}
1847
1848void ASTContext::ReleaseDeclContextMaps() {
1849 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1850 // pointer because the subclass doesn't add anything that needs to
1851 // be deleted.
1852 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1853}
1854
1855void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1856 while (Map) {
1857 // Advance the iteration before we invalidate memory.
1858 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1859
1860 if (Dependent)
1861 delete static_cast<DependentStoredDeclsMap*>(Map);
1862 else
1863 delete Map;
1864
1865 Map = Next.getPointer();
1866 Dependent = Next.getInt();
1867 }
1868}
1869
1870DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1871 DeclContext *Parent,
1872 const PartialDiagnostic &PDiag) {
1873 assert(Parent->isDependentContext()(static_cast <bool> (Parent->isDependentContext() &&
"cannot iterate dependent diagnostics of non-dependent context"
) ? void (0) : __assert_fail ("Parent->isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1874, __extension__ __PRETTY_FUNCTION__))
1874 && "cannot iterate dependent diagnostics of non-dependent context")(static_cast <bool> (Parent->isDependentContext() &&
"cannot iterate dependent diagnostics of non-dependent context"
) ? void (0) : __assert_fail ("Parent->isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-7~svn329611/tools/clang/lib/AST/DeclBase.cpp"
, 1874, __extension__ __PRETTY_FUNCTION__))
;
1875 Parent = Parent->getPrimaryContext();
1876 if (!Parent->LookupPtr)
1
Assuming the condition is false
2
Taking false branch
1877 Parent->CreateStoredDeclsMap(C);
1878
1879 DependentStoredDeclsMap *Map =
1880 static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
1881
1882 // Allocate the copy of the PartialDiagnostic via the ASTContext's
1883 // BumpPtrAllocator, rather than the ASTContext itself.
1884 PartialDiagnostic::Storage *DiagStorage = nullptr;
1885 if (PDiag.hasStorage())
3
Taking false branch
1886 DiagStorage = new (C) PartialDiagnostic::Storage;
1887
1888 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
4
'DD' initialized to a null pointer value
1889
1890 // TODO: Maybe we shouldn't reverse the order during insertion.
1891 DD->NextDiagnostic = Map->FirstDiagnostic;
5
Access to field 'NextDiagnostic' results in a dereference of a null pointer (loaded from variable 'DD')
1892 Map->FirstDiagnostic = DD;
1893
1894 return DD;
1895}