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