File: | clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp |
Warning: | line 24, column 31 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- CalleeNamespaceCheck.cpp ------------------------------------------===// | |||
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 | #include "CalleeNamespaceCheck.h" | |||
10 | #include "clang/AST/ASTContext.h" | |||
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" | |||
12 | ||||
13 | #include "llvm/ADT/StringSet.h" | |||
14 | ||||
15 | using namespace clang::ast_matchers; | |||
16 | ||||
17 | namespace clang { | |||
18 | namespace tidy { | |||
19 | namespace llvm_libc { | |||
20 | ||||
21 | // Gets the outermost namespace of a DeclContext, right under the Translation | |||
22 | // Unit. | |||
23 | const DeclContext *getOutermostNamespace(const DeclContext *Decl) { | |||
24 | const DeclContext *Parent = Decl->getParent(); | |||
| ||||
25 | if (Parent && Parent->isTranslationUnit()) | |||
26 | return Decl; | |||
27 | return getOutermostNamespace(Parent); | |||
28 | } | |||
29 | ||||
30 | void CalleeNamespaceCheck::registerMatchers(MatchFinder *Finder) { | |||
31 | Finder->addMatcher( | |||
32 | declRefExpr(to(functionDecl().bind("func"))).bind("use-site"), this); | |||
33 | } | |||
34 | ||||
35 | // A list of functions that are exempted from this check. The __errno_location | |||
36 | // function is for setting errno, which is allowed in libc, and the other | |||
37 | // functions are specifically allowed to be external so that they can be | |||
38 | // intercepted. | |||
39 | static const llvm::StringSet<> IgnoredFunctions = { | |||
40 | "__errno_location", "malloc", "calloc", "realloc", "free", "aligned_alloc"}; | |||
41 | ||||
42 | void CalleeNamespaceCheck::check(const MatchFinder::MatchResult &Result) { | |||
43 | const auto *UsageSiteExpr = Result.Nodes.getNodeAs<DeclRefExpr>("use-site"); | |||
44 | const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func"); | |||
45 | ||||
46 | // Ignore compiler builtin functions. | |||
47 | if (FuncDecl->getBuiltinID() != 0) | |||
| ||||
48 | return; | |||
49 | ||||
50 | // If the outermost namespace of the function is __llvm_libc, we're good. | |||
51 | const auto *NS = dyn_cast<NamespaceDecl>(getOutermostNamespace(FuncDecl)); | |||
52 | if (NS && NS->getName() == "__llvm_libc") | |||
53 | return; | |||
54 | ||||
55 | if (IgnoredFunctions.contains(FuncDecl->getName())) | |||
56 | return; | |||
57 | ||||
58 | diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared " | |||
59 | "within the '__llvm_libc' namespace") | |||
60 | << FuncDecl; | |||
61 | ||||
62 | diag(FuncDecl->getLocation(), "resolves to this declaration", | |||
63 | clang::DiagnosticIDs::Note); | |||
64 | } | |||
65 | ||||
66 | } // namespace llvm_libc | |||
67 | } // namespace tidy | |||
68 | } // namespace clang |
1 | //===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===// |
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 defines the Decl and DeclContext interfaces. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECLBASE_H |
14 | #define LLVM_CLANG_AST_DECLBASE_H |
15 | |
16 | #include "clang/AST/ASTDumperUtils.h" |
17 | #include "clang/AST/AttrIterator.h" |
18 | #include "clang/AST/DeclarationName.h" |
19 | #include "clang/Basic/IdentifierTable.h" |
20 | #include "clang/Basic/LLVM.h" |
21 | #include "clang/Basic/SourceLocation.h" |
22 | #include "clang/Basic/Specifiers.h" |
23 | #include "llvm/ADT/ArrayRef.h" |
24 | #include "llvm/ADT/PointerIntPair.h" |
25 | #include "llvm/ADT/PointerUnion.h" |
26 | #include "llvm/ADT/iterator.h" |
27 | #include "llvm/ADT/iterator_range.h" |
28 | #include "llvm/Support/Casting.h" |
29 | #include "llvm/Support/Compiler.h" |
30 | #include "llvm/Support/PrettyStackTrace.h" |
31 | #include "llvm/Support/VersionTuple.h" |
32 | #include <algorithm> |
33 | #include <cassert> |
34 | #include <cstddef> |
35 | #include <iterator> |
36 | #include <string> |
37 | #include <type_traits> |
38 | #include <utility> |
39 | |
40 | namespace clang { |
41 | |
42 | class ASTContext; |
43 | class ASTMutationListener; |
44 | class Attr; |
45 | class BlockDecl; |
46 | class DeclContext; |
47 | class ExternalSourceSymbolAttr; |
48 | class FunctionDecl; |
49 | class FunctionType; |
50 | class IdentifierInfo; |
51 | enum Linkage : unsigned char; |
52 | class LinkageSpecDecl; |
53 | class Module; |
54 | class NamedDecl; |
55 | class ObjCContainerDecl; |
56 | class ObjCMethodDecl; |
57 | struct PrintingPolicy; |
58 | class RecordDecl; |
59 | class SourceManager; |
60 | class Stmt; |
61 | class StoredDeclsMap; |
62 | class TemplateDecl; |
63 | class TemplateParameterList; |
64 | class TranslationUnitDecl; |
65 | class UsingDirectiveDecl; |
66 | |
67 | /// Captures the result of checking the availability of a |
68 | /// declaration. |
69 | enum AvailabilityResult { |
70 | AR_Available = 0, |
71 | AR_NotYetIntroduced, |
72 | AR_Deprecated, |
73 | AR_Unavailable |
74 | }; |
75 | |
76 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
77 | /// typedef, function, struct, etc. |
78 | /// |
79 | /// Note: There are objects tacked on before the *beginning* of Decl |
80 | /// (and its subclasses) in its Decl::operator new(). Proper alignment |
81 | /// of all subclasses (not requiring more than the alignment of Decl) is |
82 | /// asserted in DeclBase.cpp. |
83 | class alignas(8) Decl { |
84 | public: |
85 | /// Lists the kind of concrete classes of Decl. |
86 | enum Kind { |
87 | #define DECL(DERIVED, BASE) DERIVED, |
88 | #define ABSTRACT_DECL(DECL) |
89 | #define DECL_RANGE(BASE, START, END) \ |
90 | first##BASE = START, last##BASE = END, |
91 | #define LAST_DECL_RANGE(BASE, START, END) \ |
92 | first##BASE = START, last##BASE = END |
93 | #include "clang/AST/DeclNodes.inc" |
94 | }; |
95 | |
96 | /// A placeholder type used to construct an empty shell of a |
97 | /// decl-derived type that will be filled in later (e.g., by some |
98 | /// deserialization method). |
99 | struct EmptyShell {}; |
100 | |
101 | /// IdentifierNamespace - The different namespaces in which |
102 | /// declarations may appear. According to C99 6.2.3, there are |
103 | /// four namespaces, labels, tags, members and ordinary |
104 | /// identifiers. C++ describes lookup completely differently: |
105 | /// certain lookups merely "ignore" certain kinds of declarations, |
106 | /// usually based on whether the declaration is of a type, etc. |
107 | /// |
108 | /// These are meant as bitmasks, so that searches in |
109 | /// C++ can look into the "tag" namespace during ordinary lookup. |
110 | /// |
111 | /// Decl currently provides 15 bits of IDNS bits. |
112 | enum IdentifierNamespace { |
113 | /// Labels, declared with 'x:' and referenced with 'goto x'. |
114 | IDNS_Label = 0x0001, |
115 | |
116 | /// Tags, declared with 'struct foo;' and referenced with |
117 | /// 'struct foo'. All tags are also types. This is what |
118 | /// elaborated-type-specifiers look for in C. |
119 | /// This also contains names that conflict with tags in the |
120 | /// same scope but that are otherwise ordinary names (non-type |
121 | /// template parameters and indirect field declarations). |
122 | IDNS_Tag = 0x0002, |
123 | |
124 | /// Types, declared with 'struct foo', typedefs, etc. |
125 | /// This is what elaborated-type-specifiers look for in C++, |
126 | /// but note that it's ill-formed to find a non-tag. |
127 | IDNS_Type = 0x0004, |
128 | |
129 | /// Members, declared with object declarations within tag |
130 | /// definitions. In C, these can only be found by "qualified" |
131 | /// lookup in member expressions. In C++, they're found by |
132 | /// normal lookup. |
133 | IDNS_Member = 0x0008, |
134 | |
135 | /// Namespaces, declared with 'namespace foo {}'. |
136 | /// Lookup for nested-name-specifiers find these. |
137 | IDNS_Namespace = 0x0010, |
138 | |
139 | /// Ordinary names. In C, everything that's not a label, tag, |
140 | /// member, or function-local extern ends up here. |
141 | IDNS_Ordinary = 0x0020, |
142 | |
143 | /// Objective C \@protocol. |
144 | IDNS_ObjCProtocol = 0x0040, |
145 | |
146 | /// This declaration is a friend function. A friend function |
147 | /// declaration is always in this namespace but may also be in |
148 | /// IDNS_Ordinary if it was previously declared. |
149 | IDNS_OrdinaryFriend = 0x0080, |
150 | |
151 | /// This declaration is a friend class. A friend class |
152 | /// declaration is always in this namespace but may also be in |
153 | /// IDNS_Tag|IDNS_Type if it was previously declared. |
154 | IDNS_TagFriend = 0x0100, |
155 | |
156 | /// This declaration is a using declaration. A using declaration |
157 | /// *introduces* a number of other declarations into the current |
158 | /// scope, and those declarations use the IDNS of their targets, |
159 | /// but the actual using declarations go in this namespace. |
160 | IDNS_Using = 0x0200, |
161 | |
162 | /// This declaration is a C++ operator declared in a non-class |
163 | /// context. All such operators are also in IDNS_Ordinary. |
164 | /// C++ lexical operator lookup looks for these. |
165 | IDNS_NonMemberOperator = 0x0400, |
166 | |
167 | /// This declaration is a function-local extern declaration of a |
168 | /// variable or function. This may also be IDNS_Ordinary if it |
169 | /// has been declared outside any function. These act mostly like |
170 | /// invisible friend declarations, but are also visible to unqualified |
171 | /// lookup within the scope of the declaring function. |
172 | IDNS_LocalExtern = 0x0800, |
173 | |
174 | /// This declaration is an OpenMP user defined reduction construction. |
175 | IDNS_OMPReduction = 0x1000, |
176 | |
177 | /// This declaration is an OpenMP user defined mapper. |
178 | IDNS_OMPMapper = 0x2000, |
179 | }; |
180 | |
181 | /// ObjCDeclQualifier - 'Qualifiers' written next to the return and |
182 | /// parameter types in method declarations. Other than remembering |
183 | /// them and mangling them into the method's signature string, these |
184 | /// are ignored by the compiler; they are consumed by certain |
185 | /// remote-messaging frameworks. |
186 | /// |
187 | /// in, inout, and out are mutually exclusive and apply only to |
188 | /// method parameters. bycopy and byref are mutually exclusive and |
189 | /// apply only to method parameters (?). oneway applies only to |
190 | /// results. All of these expect their corresponding parameter to |
191 | /// have a particular type. None of this is currently enforced by |
192 | /// clang. |
193 | /// |
194 | /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier. |
195 | enum ObjCDeclQualifier { |
196 | OBJC_TQ_None = 0x0, |
197 | OBJC_TQ_In = 0x1, |
198 | OBJC_TQ_Inout = 0x2, |
199 | OBJC_TQ_Out = 0x4, |
200 | OBJC_TQ_Bycopy = 0x8, |
201 | OBJC_TQ_Byref = 0x10, |
202 | OBJC_TQ_Oneway = 0x20, |
203 | |
204 | /// The nullability qualifier is set when the nullability of the |
205 | /// result or parameter was expressed via a context-sensitive |
206 | /// keyword. |
207 | OBJC_TQ_CSNullability = 0x40 |
208 | }; |
209 | |
210 | /// The kind of ownership a declaration has, for visibility purposes. |
211 | /// This enumeration is designed such that higher values represent higher |
212 | /// levels of name hiding. |
213 | enum class ModuleOwnershipKind : unsigned { |
214 | /// This declaration is not owned by a module. |
215 | Unowned, |
216 | |
217 | /// This declaration has an owning module, but is globally visible |
218 | /// (typically because its owning module is visible and we know that |
219 | /// modules cannot later become hidden in this compilation). |
220 | /// After serialization and deserialization, this will be converted |
221 | /// to VisibleWhenImported. |
222 | Visible, |
223 | |
224 | /// This declaration has an owning module, and is visible when that |
225 | /// module is imported. |
226 | VisibleWhenImported, |
227 | |
228 | /// This declaration has an owning module, but is only visible to |
229 | /// lookups that occur within that module. |
230 | ModulePrivate |
231 | }; |
232 | |
233 | protected: |
234 | /// The next declaration within the same lexical |
235 | /// DeclContext. These pointers form the linked list that is |
236 | /// traversed via DeclContext's decls_begin()/decls_end(). |
237 | /// |
238 | /// The extra two bits are used for the ModuleOwnershipKind. |
239 | llvm::PointerIntPair<Decl *, 2, ModuleOwnershipKind> NextInContextAndBits; |
240 | |
241 | private: |
242 | friend class DeclContext; |
243 | |
244 | struct MultipleDC { |
245 | DeclContext *SemanticDC; |
246 | DeclContext *LexicalDC; |
247 | }; |
248 | |
249 | /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. |
250 | /// For declarations that don't contain C++ scope specifiers, it contains |
251 | /// the DeclContext where the Decl was declared. |
252 | /// For declarations with C++ scope specifiers, it contains a MultipleDC* |
253 | /// with the context where it semantically belongs (SemanticDC) and the |
254 | /// context where it was lexically declared (LexicalDC). |
255 | /// e.g.: |
256 | /// |
257 | /// namespace A { |
258 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
259 | /// } |
260 | /// void A::f(); // SemanticDC == namespace 'A' |
261 | /// // LexicalDC == global namespace |
262 | llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; |
263 | |
264 | bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } |
265 | bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } |
266 | |
267 | MultipleDC *getMultipleDC() const { |
268 | return DeclCtx.get<MultipleDC*>(); |
269 | } |
270 | |
271 | DeclContext *getSemanticDC() const { |
272 | return DeclCtx.get<DeclContext*>(); |
273 | } |
274 | |
275 | /// Loc - The location of this decl. |
276 | SourceLocation Loc; |
277 | |
278 | /// DeclKind - This indicates which class this is. |
279 | unsigned DeclKind : 7; |
280 | |
281 | /// InvalidDecl - This indicates a semantic error occurred. |
282 | unsigned InvalidDecl : 1; |
283 | |
284 | /// HasAttrs - This indicates whether the decl has attributes or not. |
285 | unsigned HasAttrs : 1; |
286 | |
287 | /// Implicit - Whether this declaration was implicitly generated by |
288 | /// the implementation rather than explicitly written by the user. |
289 | unsigned Implicit : 1; |
290 | |
291 | /// Whether this declaration was "used", meaning that a definition is |
292 | /// required. |
293 | unsigned Used : 1; |
294 | |
295 | /// Whether this declaration was "referenced". |
296 | /// The difference with 'Used' is whether the reference appears in a |
297 | /// evaluated context or not, e.g. functions used in uninstantiated templates |
298 | /// are regarded as "referenced" but not "used". |
299 | unsigned Referenced : 1; |
300 | |
301 | /// Whether this declaration is a top-level declaration (function, |
302 | /// global variable, etc.) that is lexically inside an objc container |
303 | /// definition. |
304 | unsigned TopLevelDeclInObjCContainer : 1; |
305 | |
306 | /// Whether statistic collection is enabled. |
307 | static bool StatisticsEnabled; |
308 | |
309 | protected: |
310 | friend class ASTDeclReader; |
311 | friend class ASTDeclWriter; |
312 | friend class ASTNodeImporter; |
313 | friend class ASTReader; |
314 | friend class CXXClassMemberWrapper; |
315 | friend class LinkageComputer; |
316 | template<typename decl_type> friend class Redeclarable; |
317 | |
318 | /// Access - Used by C++ decls for the access specifier. |
319 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
320 | unsigned Access : 2; |
321 | |
322 | /// Whether this declaration was loaded from an AST file. |
323 | unsigned FromASTFile : 1; |
324 | |
325 | /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. |
326 | unsigned IdentifierNamespace : 14; |
327 | |
328 | /// If 0, we have not computed the linkage of this declaration. |
329 | /// Otherwise, it is the linkage + 1. |
330 | mutable unsigned CacheValidAndLinkage : 3; |
331 | |
332 | /// Allocate memory for a deserialized declaration. |
333 | /// |
334 | /// This routine must be used to allocate memory for any declaration that is |
335 | /// deserialized from a module file. |
336 | /// |
337 | /// \param Size The size of the allocated object. |
338 | /// \param Ctx The context in which we will allocate memory. |
339 | /// \param ID The global ID of the deserialized declaration. |
340 | /// \param Extra The amount of extra space to allocate after the object. |
341 | void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID, |
342 | std::size_t Extra = 0); |
343 | |
344 | /// Allocate memory for a non-deserialized declaration. |
345 | void *operator new(std::size_t Size, const ASTContext &Ctx, |
346 | DeclContext *Parent, std::size_t Extra = 0); |
347 | |
348 | private: |
349 | bool AccessDeclContextCheck() const; |
350 | |
351 | /// Get the module ownership kind to use for a local lexical child of \p DC, |
352 | /// which may be either a local or (rarely) an imported declaration. |
353 | static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) { |
354 | if (DC) { |
355 | auto *D = cast<Decl>(DC); |
356 | auto MOK = D->getModuleOwnershipKind(); |
357 | if (MOK != ModuleOwnershipKind::Unowned && |
358 | (!D->isFromASTFile() || D->hasLocalOwningModuleStorage())) |
359 | return MOK; |
360 | // If D is not local and we have no local module storage, then we don't |
361 | // need to track module ownership at all. |
362 | } |
363 | return ModuleOwnershipKind::Unowned; |
364 | } |
365 | |
366 | public: |
367 | Decl() = delete; |
368 | Decl(const Decl&) = delete; |
369 | Decl(Decl &&) = delete; |
370 | Decl &operator=(const Decl&) = delete; |
371 | Decl &operator=(Decl&&) = delete; |
372 | |
373 | protected: |
374 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
375 | : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)), |
376 | DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false), |
377 | Implicit(false), Used(false), Referenced(false), |
378 | TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0), |
379 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
380 | CacheValidAndLinkage(0) { |
381 | if (StatisticsEnabled) add(DK); |
382 | } |
383 | |
384 | Decl(Kind DK, EmptyShell Empty) |
385 | : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false), |
386 | Used(false), Referenced(false), TopLevelDeclInObjCContainer(false), |
387 | Access(AS_none), FromASTFile(0), |
388 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
389 | CacheValidAndLinkage(0) { |
390 | if (StatisticsEnabled) add(DK); |
391 | } |
392 | |
393 | virtual ~Decl(); |
394 | |
395 | /// Update a potentially out-of-date declaration. |
396 | void updateOutOfDate(IdentifierInfo &II) const; |
397 | |
398 | Linkage getCachedLinkage() const { |
399 | return Linkage(CacheValidAndLinkage - 1); |
400 | } |
401 | |
402 | void setCachedLinkage(Linkage L) const { |
403 | CacheValidAndLinkage = L + 1; |
404 | } |
405 | |
406 | bool hasCachedLinkage() const { |
407 | return CacheValidAndLinkage; |
408 | } |
409 | |
410 | public: |
411 | /// Source range that this declaration covers. |
412 | virtual SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) { |
413 | return SourceRange(getLocation(), getLocation()); |
414 | } |
415 | |
416 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { |
417 | return getSourceRange().getBegin(); |
418 | } |
419 | |
420 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { |
421 | return getSourceRange().getEnd(); |
422 | } |
423 | |
424 | SourceLocation getLocation() const { return Loc; } |
425 | void setLocation(SourceLocation L) { Loc = L; } |
426 | |
427 | Kind getKind() const { return static_cast<Kind>(DeclKind); } |
428 | const char *getDeclKindName() const; |
429 | |
430 | Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); } |
431 | const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();} |
432 | |
433 | DeclContext *getDeclContext() { |
434 | if (isInSemaDC()) |
435 | return getSemanticDC(); |
436 | return getMultipleDC()->SemanticDC; |
437 | } |
438 | const DeclContext *getDeclContext() const { |
439 | return const_cast<Decl*>(this)->getDeclContext(); |
440 | } |
441 | |
442 | /// Find the innermost non-closure ancestor of this declaration, |
443 | /// walking up through blocks, lambdas, etc. If that ancestor is |
444 | /// not a code context (!isFunctionOrMethod()), returns null. |
445 | /// |
446 | /// A declaration may be its own non-closure context. |
447 | Decl *getNonClosureContext(); |
448 | const Decl *getNonClosureContext() const { |
449 | return const_cast<Decl*>(this)->getNonClosureContext(); |
450 | } |
451 | |
452 | TranslationUnitDecl *getTranslationUnitDecl(); |
453 | const TranslationUnitDecl *getTranslationUnitDecl() const { |
454 | return const_cast<Decl*>(this)->getTranslationUnitDecl(); |
455 | } |
456 | |
457 | bool isInAnonymousNamespace() const; |
458 | |
459 | bool isInStdNamespace() const; |
460 | |
461 | ASTContext &getASTContext() const LLVM_READONLY__attribute__((__pure__)); |
462 | |
463 | /// Helper to get the language options from the ASTContext. |
464 | /// Defined out of line to avoid depending on ASTContext.h. |
465 | const LangOptions &getLangOpts() const LLVM_READONLY__attribute__((__pure__)); |
466 | |
467 | void setAccess(AccessSpecifier AS) { |
468 | Access = AS; |
469 | assert(AccessDeclContextCheck())(static_cast <bool> (AccessDeclContextCheck()) ? void ( 0) : __assert_fail ("AccessDeclContextCheck()", "clang/include/clang/AST/DeclBase.h" , 469, __extension__ __PRETTY_FUNCTION__)); |
470 | } |
471 | |
472 | AccessSpecifier getAccess() const { |
473 | assert(AccessDeclContextCheck())(static_cast <bool> (AccessDeclContextCheck()) ? void ( 0) : __assert_fail ("AccessDeclContextCheck()", "clang/include/clang/AST/DeclBase.h" , 473, __extension__ __PRETTY_FUNCTION__)); |
474 | return AccessSpecifier(Access); |
475 | } |
476 | |
477 | /// Retrieve the access specifier for this declaration, even though |
478 | /// it may not yet have been properly set. |
479 | AccessSpecifier getAccessUnsafe() const { |
480 | return AccessSpecifier(Access); |
481 | } |
482 | |
483 | bool hasAttrs() const { return HasAttrs; } |
484 | |
485 | void setAttrs(const AttrVec& Attrs) { |
486 | return setAttrsImpl(Attrs, getASTContext()); |
487 | } |
488 | |
489 | AttrVec &getAttrs() { |
490 | return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); |
491 | } |
492 | |
493 | const AttrVec &getAttrs() const; |
494 | void dropAttrs(); |
495 | void addAttr(Attr *A); |
496 | |
497 | using attr_iterator = AttrVec::const_iterator; |
498 | using attr_range = llvm::iterator_range<attr_iterator>; |
499 | |
500 | attr_range attrs() const { |
501 | return attr_range(attr_begin(), attr_end()); |
502 | } |
503 | |
504 | attr_iterator attr_begin() const { |
505 | return hasAttrs() ? getAttrs().begin() : nullptr; |
506 | } |
507 | attr_iterator attr_end() const { |
508 | return hasAttrs() ? getAttrs().end() : nullptr; |
509 | } |
510 | |
511 | template <typename T> |
512 | void dropAttr() { |
513 | if (!HasAttrs) return; |
514 | |
515 | AttrVec &Vec = getAttrs(); |
516 | llvm::erase_if(Vec, [](Attr *A) { return isa<T>(A); }); |
517 | |
518 | if (Vec.empty()) |
519 | HasAttrs = false; |
520 | } |
521 | |
522 | template <typename T> |
523 | llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const { |
524 | return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>()); |
525 | } |
526 | |
527 | template <typename T> |
528 | specific_attr_iterator<T> specific_attr_begin() const { |
529 | return specific_attr_iterator<T>(attr_begin()); |
530 | } |
531 | |
532 | template <typename T> |
533 | specific_attr_iterator<T> specific_attr_end() const { |
534 | return specific_attr_iterator<T>(attr_end()); |
535 | } |
536 | |
537 | template<typename T> T *getAttr() const { |
538 | return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr; |
539 | } |
540 | |
541 | template<typename T> bool hasAttr() const { |
542 | return hasAttrs() && hasSpecificAttr<T>(getAttrs()); |
543 | } |
544 | |
545 | /// getMaxAlignment - return the maximum alignment specified by attributes |
546 | /// on this decl, 0 if there are none. |
547 | unsigned getMaxAlignment() const; |
548 | |
549 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
550 | /// allows for graceful error recovery. |
551 | void setInvalidDecl(bool Invalid = true); |
552 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
553 | |
554 | /// isImplicit - Indicates whether the declaration was implicitly |
555 | /// generated by the implementation. If false, this declaration |
556 | /// was written explicitly in the source code. |
557 | bool isImplicit() const { return Implicit; } |
558 | void setImplicit(bool I = true) { Implicit = I; } |
559 | |
560 | /// Whether *any* (re-)declaration of the entity was used, meaning that |
561 | /// a definition is required. |
562 | /// |
563 | /// \param CheckUsedAttr When true, also consider the "used" attribute |
564 | /// (in addition to the "used" bit set by \c setUsed()) when determining |
565 | /// whether the function is used. |
566 | bool isUsed(bool CheckUsedAttr = true) const; |
567 | |
568 | /// Set whether the declaration is used, in the sense of odr-use. |
569 | /// |
570 | /// This should only be used immediately after creating a declaration. |
571 | /// It intentionally doesn't notify any listeners. |
572 | void setIsUsed() { getCanonicalDecl()->Used = true; } |
573 | |
574 | /// Mark the declaration used, in the sense of odr-use. |
575 | /// |
576 | /// This notifies any mutation listeners in addition to setting a bit |
577 | /// indicating the declaration is used. |
578 | void markUsed(ASTContext &C); |
579 | |
580 | /// Whether any declaration of this entity was referenced. |
581 | bool isReferenced() const; |
582 | |
583 | /// Whether this declaration was referenced. This should not be relied |
584 | /// upon for anything other than debugging. |
585 | bool isThisDeclarationReferenced() const { return Referenced; } |
586 | |
587 | void setReferenced(bool R = true) { Referenced = R; } |
588 | |
589 | /// Whether this declaration is a top-level declaration (function, |
590 | /// global variable, etc.) that is lexically inside an objc container |
591 | /// definition. |
592 | bool isTopLevelDeclInObjCContainer() const { |
593 | return TopLevelDeclInObjCContainer; |
594 | } |
595 | |
596 | void setTopLevelDeclInObjCContainer(bool V = true) { |
597 | TopLevelDeclInObjCContainer = V; |
598 | } |
599 | |
600 | /// Looks on this and related declarations for an applicable |
601 | /// external source symbol attribute. |
602 | ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const; |
603 | |
604 | /// Whether this declaration was marked as being private to the |
605 | /// module in which it was defined. |
606 | bool isModulePrivate() const { |
607 | return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate; |
608 | } |
609 | |
610 | /// Whether this declaration was exported in a lexical context. |
611 | /// e.g.: |
612 | /// |
613 | /// export namespace A { |
614 | /// void f1(); // isInExportDeclContext() == true |
615 | /// } |
616 | /// void A::f1(); // isInExportDeclContext() == false |
617 | /// |
618 | /// namespace B { |
619 | /// void f2(); // isInExportDeclContext() == false |
620 | /// } |
621 | /// export void B::f2(); // isInExportDeclContext() == true |
622 | bool isInExportDeclContext() const; |
623 | |
624 | /// Return true if this declaration has an attribute which acts as |
625 | /// definition of the entity, such as 'alias' or 'ifunc'. |
626 | bool hasDefiningAttr() const; |
627 | |
628 | /// Return this declaration's defining attribute if it has one. |
629 | const Attr *getDefiningAttr() const; |
630 | |
631 | protected: |
632 | /// Specify that this declaration was marked as being private |
633 | /// to the module in which it was defined. |
634 | void setModulePrivate() { |
635 | // The module-private specifier has no effect on unowned declarations. |
636 | // FIXME: We should track this in some way for source fidelity. |
637 | if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned) |
638 | return; |
639 | setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate); |
640 | } |
641 | |
642 | public: |
643 | /// Set the FromASTFile flag. This indicates that this declaration |
644 | /// was deserialized and not parsed from source code and enables |
645 | /// features such as module ownership information. |
646 | void setFromASTFile() { |
647 | FromASTFile = true; |
648 | } |
649 | |
650 | /// Set the owning module ID. This may only be called for |
651 | /// deserialized Decls. |
652 | void setOwningModuleID(unsigned ID) { |
653 | assert(isFromASTFile() && "Only works on a deserialized declaration")(static_cast <bool> (isFromASTFile() && "Only works on a deserialized declaration" ) ? void (0) : __assert_fail ("isFromASTFile() && \"Only works on a deserialized declaration\"" , "clang/include/clang/AST/DeclBase.h", 653, __extension__ __PRETTY_FUNCTION__ )); |
654 | *((unsigned*)this - 2) = ID; |
655 | } |
656 | |
657 | public: |
658 | /// Determine the availability of the given declaration. |
659 | /// |
660 | /// This routine will determine the most restrictive availability of |
661 | /// the given declaration (e.g., preferring 'unavailable' to |
662 | /// 'deprecated'). |
663 | /// |
664 | /// \param Message If non-NULL and the result is not \c |
665 | /// AR_Available, will be set to a (possibly empty) message |
666 | /// describing why the declaration has not been introduced, is |
667 | /// deprecated, or is unavailable. |
668 | /// |
669 | /// \param EnclosingVersion The version to compare with. If empty, assume the |
670 | /// deployment target version. |
671 | /// |
672 | /// \param RealizedPlatform If non-NULL and the availability result is found |
673 | /// in an available attribute it will set to the platform which is written in |
674 | /// the available attribute. |
675 | AvailabilityResult |
676 | getAvailability(std::string *Message = nullptr, |
677 | VersionTuple EnclosingVersion = VersionTuple(), |
678 | StringRef *RealizedPlatform = nullptr) const; |
679 | |
680 | /// Retrieve the version of the target platform in which this |
681 | /// declaration was introduced. |
682 | /// |
683 | /// \returns An empty version tuple if this declaration has no 'introduced' |
684 | /// availability attributes, or the version tuple that's specified in the |
685 | /// attribute otherwise. |
686 | VersionTuple getVersionIntroduced() const; |
687 | |
688 | /// Determine whether this declaration is marked 'deprecated'. |
689 | /// |
690 | /// \param Message If non-NULL and the declaration is deprecated, |
691 | /// this will be set to the message describing why the declaration |
692 | /// was deprecated (which may be empty). |
693 | bool isDeprecated(std::string *Message = nullptr) const { |
694 | return getAvailability(Message) == AR_Deprecated; |
695 | } |
696 | |
697 | /// Determine whether this declaration is marked 'unavailable'. |
698 | /// |
699 | /// \param Message If non-NULL and the declaration is unavailable, |
700 | /// this will be set to the message describing why the declaration |
701 | /// was made unavailable (which may be empty). |
702 | bool isUnavailable(std::string *Message = nullptr) const { |
703 | return getAvailability(Message) == AR_Unavailable; |
704 | } |
705 | |
706 | /// Determine whether this is a weak-imported symbol. |
707 | /// |
708 | /// Weak-imported symbols are typically marked with the |
709 | /// 'weak_import' attribute, but may also be marked with an |
710 | /// 'availability' attribute where we're targing a platform prior to |
711 | /// the introduction of this feature. |
712 | bool isWeakImported() const; |
713 | |
714 | /// Determines whether this symbol can be weak-imported, |
715 | /// e.g., whether it would be well-formed to add the weak_import |
716 | /// attribute. |
717 | /// |
718 | /// \param IsDefinition Set to \c true to indicate that this |
719 | /// declaration cannot be weak-imported because it has a definition. |
720 | bool canBeWeakImported(bool &IsDefinition) const; |
721 | |
722 | /// Determine whether this declaration came from an AST file (such as |
723 | /// a precompiled header or module) rather than having been parsed. |
724 | bool isFromASTFile() const { return FromASTFile; } |
725 | |
726 | /// Retrieve the global declaration ID associated with this |
727 | /// declaration, which specifies where this Decl was loaded from. |
728 | unsigned getGlobalID() const { |
729 | if (isFromASTFile()) |
730 | return *((const unsigned*)this - 1); |
731 | return 0; |
732 | } |
733 | |
734 | /// Retrieve the global ID of the module that owns this particular |
735 | /// declaration. |
736 | unsigned getOwningModuleID() const { |
737 | if (isFromASTFile()) |
738 | return *((const unsigned*)this - 2); |
739 | return 0; |
740 | } |
741 | |
742 | private: |
743 | Module *getOwningModuleSlow() const; |
744 | |
745 | protected: |
746 | bool hasLocalOwningModuleStorage() const; |
747 | |
748 | public: |
749 | /// Get the imported owning module, if this decl is from an imported |
750 | /// (non-local) module. |
751 | Module *getImportedOwningModule() const { |
752 | if (!isFromASTFile() || !hasOwningModule()) |
753 | return nullptr; |
754 | |
755 | return getOwningModuleSlow(); |
756 | } |
757 | |
758 | /// Get the local owning module, if known. Returns nullptr if owner is |
759 | /// not yet known or declaration is not from a module. |
760 | Module *getLocalOwningModule() const { |
761 | if (isFromASTFile() || !hasOwningModule()) |
762 | return nullptr; |
763 | |
764 | assert(hasLocalOwningModuleStorage() &&(static_cast <bool> (hasLocalOwningModuleStorage() && "owned local decl but no local module storage") ? void (0) : __assert_fail ("hasLocalOwningModuleStorage() && \"owned local decl but no local module storage\"" , "clang/include/clang/AST/DeclBase.h", 765, __extension__ __PRETTY_FUNCTION__ )) |
765 | "owned local decl but no local module storage")(static_cast <bool> (hasLocalOwningModuleStorage() && "owned local decl but no local module storage") ? void (0) : __assert_fail ("hasLocalOwningModuleStorage() && \"owned local decl but no local module storage\"" , "clang/include/clang/AST/DeclBase.h", 765, __extension__ __PRETTY_FUNCTION__ )); |
766 | return reinterpret_cast<Module *const *>(this)[-1]; |
767 | } |
768 | void setLocalOwningModule(Module *M) { |
769 | assert(!isFromASTFile() && hasOwningModule() &&(static_cast <bool> (!isFromASTFile() && hasOwningModule () && hasLocalOwningModuleStorage() && "should not have a cached owning module" ) ? void (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\"" , "clang/include/clang/AST/DeclBase.h", 771, __extension__ __PRETTY_FUNCTION__ )) |
770 | hasLocalOwningModuleStorage() &&(static_cast <bool> (!isFromASTFile() && hasOwningModule () && hasLocalOwningModuleStorage() && "should not have a cached owning module" ) ? void (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\"" , "clang/include/clang/AST/DeclBase.h", 771, __extension__ __PRETTY_FUNCTION__ )) |
771 | "should not have a cached owning module")(static_cast <bool> (!isFromASTFile() && hasOwningModule () && hasLocalOwningModuleStorage() && "should not have a cached owning module" ) ? void (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\"" , "clang/include/clang/AST/DeclBase.h", 771, __extension__ __PRETTY_FUNCTION__ )); |
772 | reinterpret_cast<Module **>(this)[-1] = M; |
773 | } |
774 | |
775 | /// Is this declaration owned by some module? |
776 | bool hasOwningModule() const { |
777 | return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned; |
778 | } |
779 | |
780 | /// Get the module that owns this declaration (for visibility purposes). |
781 | Module *getOwningModule() const { |
782 | return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule(); |
783 | } |
784 | |
785 | /// Get the module that owns this declaration for linkage purposes. |
786 | /// There only ever is such a module under the C++ Modules TS. |
787 | /// |
788 | /// \param IgnoreLinkage Ignore the linkage of the entity; assume that |
789 | /// all declarations in a global module fragment are unowned. |
790 | Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; |
791 | |
792 | /// Determine whether this declaration is definitely visible to name lookup, |
793 | /// independent of whether the owning module is visible. |
794 | /// Note: The declaration may be visible even if this returns \c false if the |
795 | /// owning module is visible within the query context. This is a low-level |
796 | /// helper function; most code should be calling Sema::isVisible() instead. |
797 | bool isUnconditionallyVisible() const { |
798 | return (int)getModuleOwnershipKind() <= (int)ModuleOwnershipKind::Visible; |
799 | } |
800 | |
801 | /// Set that this declaration is globally visible, even if it came from a |
802 | /// module that is not visible. |
803 | void setVisibleDespiteOwningModule() { |
804 | if (!isUnconditionallyVisible()) |
805 | setModuleOwnershipKind(ModuleOwnershipKind::Visible); |
806 | } |
807 | |
808 | /// Get the kind of module ownership for this declaration. |
809 | ModuleOwnershipKind getModuleOwnershipKind() const { |
810 | return NextInContextAndBits.getInt(); |
811 | } |
812 | |
813 | /// Set whether this declaration is hidden from name lookup. |
814 | void setModuleOwnershipKind(ModuleOwnershipKind MOK) { |
815 | assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&(static_cast <bool> (!(getModuleOwnershipKind() == ModuleOwnershipKind ::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration" ) ? void (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\"" , "clang/include/clang/AST/DeclBase.h", 818, __extension__ __PRETTY_FUNCTION__ )) |
816 | MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() &&(static_cast <bool> (!(getModuleOwnershipKind() == ModuleOwnershipKind ::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration" ) ? void (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\"" , "clang/include/clang/AST/DeclBase.h", 818, __extension__ __PRETTY_FUNCTION__ )) |
817 | !hasLocalOwningModuleStorage()) &&(static_cast <bool> (!(getModuleOwnershipKind() == ModuleOwnershipKind ::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration" ) ? void (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\"" , "clang/include/clang/AST/DeclBase.h", 818, __extension__ __PRETTY_FUNCTION__ )) |
818 | "no storage available for owning module for this declaration")(static_cast <bool> (!(getModuleOwnershipKind() == ModuleOwnershipKind ::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration" ) ? void (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\"" , "clang/include/clang/AST/DeclBase.h", 818, __extension__ __PRETTY_FUNCTION__ )); |
819 | NextInContextAndBits.setInt(MOK); |
820 | } |
821 | |
822 | unsigned getIdentifierNamespace() const { |
823 | return IdentifierNamespace; |
824 | } |
825 | |
826 | bool isInIdentifierNamespace(unsigned NS) const { |
827 | return getIdentifierNamespace() & NS; |
828 | } |
829 | |
830 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
831 | |
832 | bool hasTagIdentifierNamespace() const { |
833 | return isTagIdentifierNamespace(getIdentifierNamespace()); |
834 | } |
835 | |
836 | static bool isTagIdentifierNamespace(unsigned NS) { |
837 | // TagDecls have Tag and Type set and may also have TagFriend. |
838 | return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); |
839 | } |
840 | |
841 | /// getLexicalDeclContext - The declaration context where this Decl was |
842 | /// lexically declared (LexicalDC). May be different from |
843 | /// getDeclContext() (SemanticDC). |
844 | /// e.g.: |
845 | /// |
846 | /// namespace A { |
847 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
848 | /// } |
849 | /// void A::f(); // SemanticDC == namespace 'A' |
850 | /// // LexicalDC == global namespace |
851 | DeclContext *getLexicalDeclContext() { |
852 | if (isInSemaDC()) |
853 | return getSemanticDC(); |
854 | return getMultipleDC()->LexicalDC; |
855 | } |
856 | const DeclContext *getLexicalDeclContext() const { |
857 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
858 | } |
859 | |
860 | /// Determine whether this declaration is declared out of line (outside its |
861 | /// semantic context). |
862 | virtual bool isOutOfLine() const; |
863 | |
864 | /// setDeclContext - Set both the semantic and lexical DeclContext |
865 | /// to DC. |
866 | void setDeclContext(DeclContext *DC); |
867 | |
868 | void setLexicalDeclContext(DeclContext *DC); |
869 | |
870 | /// Determine whether this declaration is a templated entity (whether it is |
871 | // within the scope of a template parameter). |
872 | bool isTemplated() const; |
873 | |
874 | /// Determine the number of levels of template parameter surrounding this |
875 | /// declaration. |
876 | unsigned getTemplateDepth() const; |
877 | |
878 | /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
879 | /// scoped decl is defined outside the current function or method. This is |
880 | /// roughly global variables and functions, but also handles enums (which |
881 | /// could be defined inside or outside a function etc). |
882 | bool isDefinedOutsideFunctionOrMethod() const { |
883 | return getParentFunctionOrMethod() == nullptr; |
884 | } |
885 | |
886 | /// Determine whether a substitution into this declaration would occur as |
887 | /// part of a substitution into a dependent local scope. Such a substitution |
888 | /// transitively substitutes into all constructs nested within this |
889 | /// declaration. |
890 | /// |
891 | /// This recognizes non-defining declarations as well as members of local |
892 | /// classes and lambdas: |
893 | /// \code |
894 | /// template<typename T> void foo() { void bar(); } |
895 | /// template<typename T> void foo2() { class ABC { void bar(); }; } |
896 | /// template<typename T> inline int x = [](){ return 0; }(); |
897 | /// \endcode |
898 | bool isInLocalScopeForInstantiation() const; |
899 | |
900 | /// If this decl is defined inside a function/method/block it returns |
901 | /// the corresponding DeclContext, otherwise it returns null. |
902 | const DeclContext *getParentFunctionOrMethod() const; |
903 | DeclContext *getParentFunctionOrMethod() { |
904 | return const_cast<DeclContext*>( |
905 | const_cast<const Decl*>(this)->getParentFunctionOrMethod()); |
906 | } |
907 | |
908 | /// Retrieves the "canonical" declaration of the given declaration. |
909 | virtual Decl *getCanonicalDecl() { return this; } |
910 | const Decl *getCanonicalDecl() const { |
911 | return const_cast<Decl*>(this)->getCanonicalDecl(); |
912 | } |
913 | |
914 | /// Whether this particular Decl is a canonical one. |
915 | bool isCanonicalDecl() const { return getCanonicalDecl() == this; } |
916 | |
917 | protected: |
918 | /// Returns the next redeclaration or itself if this is the only decl. |
919 | /// |
920 | /// Decl subclasses that can be redeclared should override this method so that |
921 | /// Decl::redecl_iterator can iterate over them. |
922 | virtual Decl *getNextRedeclarationImpl() { return this; } |
923 | |
924 | /// Implementation of getPreviousDecl(), to be overridden by any |
925 | /// subclass that has a redeclaration chain. |
926 | virtual Decl *getPreviousDeclImpl() { return nullptr; } |
927 | |
928 | /// Implementation of getMostRecentDecl(), to be overridden by any |
929 | /// subclass that has a redeclaration chain. |
930 | virtual Decl *getMostRecentDeclImpl() { return this; } |
931 | |
932 | public: |
933 | /// Iterates through all the redeclarations of the same decl. |
934 | class redecl_iterator { |
935 | /// Current - The current declaration. |
936 | Decl *Current = nullptr; |
937 | Decl *Starter; |
938 | |
939 | public: |
940 | using value_type = Decl *; |
941 | using reference = const value_type &; |
942 | using pointer = const value_type *; |
943 | using iterator_category = std::forward_iterator_tag; |
944 | using difference_type = std::ptrdiff_t; |
945 | |
946 | redecl_iterator() = default; |
947 | explicit redecl_iterator(Decl *C) : Current(C), Starter(C) {} |
948 | |
949 | reference operator*() const { return Current; } |
950 | value_type operator->() const { return Current; } |
951 | |
952 | redecl_iterator& operator++() { |
953 | assert(Current && "Advancing while iterator has reached end")(static_cast <bool> (Current && "Advancing while iterator has reached end" ) ? void (0) : __assert_fail ("Current && \"Advancing while iterator has reached end\"" , "clang/include/clang/AST/DeclBase.h", 953, __extension__ __PRETTY_FUNCTION__ )); |
954 | // Get either previous decl or latest decl. |
955 | Decl *Next = Current->getNextRedeclarationImpl(); |
956 | assert(Next && "Should return next redeclaration or itself, never null!")(static_cast <bool> (Next && "Should return next redeclaration or itself, never null!" ) ? void (0) : __assert_fail ("Next && \"Should return next redeclaration or itself, never null!\"" , "clang/include/clang/AST/DeclBase.h", 956, __extension__ __PRETTY_FUNCTION__ )); |
957 | Current = (Next != Starter) ? Next : nullptr; |
958 | return *this; |
959 | } |
960 | |
961 | redecl_iterator operator++(int) { |
962 | redecl_iterator tmp(*this); |
963 | ++(*this); |
964 | return tmp; |
965 | } |
966 | |
967 | friend bool operator==(redecl_iterator x, redecl_iterator y) { |
968 | return x.Current == y.Current; |
969 | } |
970 | |
971 | friend bool operator!=(redecl_iterator x, redecl_iterator y) { |
972 | return x.Current != y.Current; |
973 | } |
974 | }; |
975 | |
976 | using redecl_range = llvm::iterator_range<redecl_iterator>; |
977 | |
978 | /// Returns an iterator range for all the redeclarations of the same |
979 | /// decl. It will iterate at least once (when this decl is the only one). |
980 | redecl_range redecls() const { |
981 | return redecl_range(redecls_begin(), redecls_end()); |
982 | } |
983 | |
984 | redecl_iterator redecls_begin() const { |
985 | return redecl_iterator(const_cast<Decl *>(this)); |
986 | } |
987 | |
988 | redecl_iterator redecls_end() const { return redecl_iterator(); } |
989 | |
990 | /// Retrieve the previous declaration that declares the same entity |
991 | /// as this declaration, or NULL if there is no previous declaration. |
992 | Decl *getPreviousDecl() { return getPreviousDeclImpl(); } |
993 | |
994 | /// Retrieve the previous declaration that declares the same entity |
995 | /// as this declaration, or NULL if there is no previous declaration. |
996 | const Decl *getPreviousDecl() const { |
997 | return const_cast<Decl *>(this)->getPreviousDeclImpl(); |
998 | } |
999 | |
1000 | /// True if this is the first declaration in its redeclaration chain. |
1001 | bool isFirstDecl() const { |
1002 | return getPreviousDecl() == nullptr; |
1003 | } |
1004 | |
1005 | /// Retrieve the most recent declaration that declares the same entity |
1006 | /// as this declaration (which may be this declaration). |
1007 | Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); } |
1008 | |
1009 | /// Retrieve the most recent declaration that declares the same entity |
1010 | /// as this declaration (which may be this declaration). |
1011 | const Decl *getMostRecentDecl() const { |
1012 | return const_cast<Decl *>(this)->getMostRecentDeclImpl(); |
1013 | } |
1014 | |
1015 | /// getBody - If this Decl represents a declaration for a body of code, |
1016 | /// such as a function or method definition, this method returns the |
1017 | /// top-level Stmt* of that body. Otherwise this method returns null. |
1018 | virtual Stmt* getBody() const { return nullptr; } |
1019 | |
1020 | /// Returns true if this \c Decl represents a declaration for a body of |
1021 | /// code, such as a function or method definition. |
1022 | /// Note that \c hasBody can also return true if any redeclaration of this |
1023 | /// \c Decl represents a declaration for a body of code. |
1024 | virtual bool hasBody() const { return getBody() != nullptr; } |
1025 | |
1026 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
1027 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
1028 | SourceLocation getBodyRBrace() const; |
1029 | |
1030 | // global temp stats (until we have a per-module visitor) |
1031 | static void add(Kind k); |
1032 | static void EnableStatistics(); |
1033 | static void PrintStats(); |
1034 | |
1035 | /// isTemplateParameter - Determines whether this declaration is a |
1036 | /// template parameter. |
1037 | bool isTemplateParameter() const; |
1038 | |
1039 | /// isTemplateParameter - Determines whether this declaration is a |
1040 | /// template parameter pack. |
1041 | bool isTemplateParameterPack() const; |
1042 | |
1043 | /// Whether this declaration is a parameter pack. |
1044 | bool isParameterPack() const; |
1045 | |
1046 | /// returns true if this declaration is a template |
1047 | bool isTemplateDecl() const; |
1048 | |
1049 | /// Whether this declaration is a function or function template. |
1050 | bool isFunctionOrFunctionTemplate() const { |
1051 | return (DeclKind >= Decl::firstFunction && |
1052 | DeclKind <= Decl::lastFunction) || |
1053 | DeclKind == FunctionTemplate; |
1054 | } |
1055 | |
1056 | /// If this is a declaration that describes some template, this |
1057 | /// method returns that template declaration. |
1058 | /// |
1059 | /// Note that this returns nullptr for partial specializations, because they |
1060 | /// are not modeled as TemplateDecls. Use getDescribedTemplateParams to handle |
1061 | /// those cases. |
1062 | TemplateDecl *getDescribedTemplate() const; |
1063 | |
1064 | /// If this is a declaration that describes some template or partial |
1065 | /// specialization, this returns the corresponding template parameter list. |
1066 | const TemplateParameterList *getDescribedTemplateParams() const; |
1067 | |
1068 | /// Returns the function itself, or the templated function if this is a |
1069 | /// function template. |
1070 | FunctionDecl *getAsFunction() LLVM_READONLY__attribute__((__pure__)); |
1071 | |
1072 | const FunctionDecl *getAsFunction() const { |
1073 | return const_cast<Decl *>(this)->getAsFunction(); |
1074 | } |
1075 | |
1076 | /// Changes the namespace of this declaration to reflect that it's |
1077 | /// a function-local extern declaration. |
1078 | /// |
1079 | /// These declarations appear in the lexical context of the extern |
1080 | /// declaration, but in the semantic context of the enclosing namespace |
1081 | /// scope. |
1082 | void setLocalExternDecl() { |
1083 | Decl *Prev = getPreviousDecl(); |
1084 | IdentifierNamespace &= ~IDNS_Ordinary; |
1085 | |
1086 | // It's OK for the declaration to still have the "invisible friend" flag or |
1087 | // the "conflicts with tag declarations in this scope" flag for the outer |
1088 | // scope. |
1089 | assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 &&(static_cast <bool> ((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && "namespace is not ordinary") ? void (0) : __assert_fail ("(IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && \"namespace is not ordinary\"" , "clang/include/clang/AST/DeclBase.h", 1090, __extension__ __PRETTY_FUNCTION__ )) |
1090 | "namespace is not ordinary")(static_cast <bool> ((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && "namespace is not ordinary") ? void (0) : __assert_fail ("(IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && \"namespace is not ordinary\"" , "clang/include/clang/AST/DeclBase.h", 1090, __extension__ __PRETTY_FUNCTION__ )); |
1091 | |
1092 | IdentifierNamespace |= IDNS_LocalExtern; |
1093 | if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary) |
1094 | IdentifierNamespace |= IDNS_Ordinary; |
1095 | } |
1096 | |
1097 | /// Determine whether this is a block-scope declaration with linkage. |
1098 | /// This will either be a local variable declaration declared 'extern', or a |
1099 | /// local function declaration. |
1100 | bool isLocalExternDecl() { |
1101 | return IdentifierNamespace & IDNS_LocalExtern; |
1102 | } |
1103 | |
1104 | /// Changes the namespace of this declaration to reflect that it's |
1105 | /// the object of a friend declaration. |
1106 | /// |
1107 | /// These declarations appear in the lexical context of the friending |
1108 | /// class, but in the semantic context of the actual entity. This property |
1109 | /// applies only to a specific decl object; other redeclarations of the |
1110 | /// same entity may not (and probably don't) share this property. |
1111 | void setObjectOfFriendDecl(bool PerformFriendInjection = false) { |
1112 | unsigned OldNS = IdentifierNamespace; |
1113 | assert((OldNS & (IDNS_Tag | IDNS_Ordinary |(static_cast <bool> ((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator )) && "namespace includes neither ordinary nor tag") ? void (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\"" , "clang/include/clang/AST/DeclBase.h", 1116, __extension__ __PRETTY_FUNCTION__ )) |
1114 | IDNS_TagFriend | IDNS_OrdinaryFriend |(static_cast <bool> ((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator )) && "namespace includes neither ordinary nor tag") ? void (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\"" , "clang/include/clang/AST/DeclBase.h", 1116, __extension__ __PRETTY_FUNCTION__ )) |
1115 | IDNS_LocalExtern | IDNS_NonMemberOperator)) &&(static_cast <bool> ((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator )) && "namespace includes neither ordinary nor tag") ? void (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\"" , "clang/include/clang/AST/DeclBase.h", 1116, __extension__ __PRETTY_FUNCTION__ )) |
1116 | "namespace includes neither ordinary nor tag")(static_cast <bool> ((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator )) && "namespace includes neither ordinary nor tag") ? void (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\"" , "clang/include/clang/AST/DeclBase.h", 1116, __extension__ __PRETTY_FUNCTION__ )); |
1117 | assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |(static_cast <bool> (!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes other than ordinary or tag" ) ? void (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\"" , "clang/include/clang/AST/DeclBase.h", 1120, __extension__ __PRETTY_FUNCTION__ )) |
1118 | IDNS_TagFriend | IDNS_OrdinaryFriend |(static_cast <bool> (!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes other than ordinary or tag" ) ? void (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\"" , "clang/include/clang/AST/DeclBase.h", 1120, __extension__ __PRETTY_FUNCTION__ )) |
1119 | IDNS_LocalExtern | IDNS_NonMemberOperator)) &&(static_cast <bool> (!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes other than ordinary or tag" ) ? void (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\"" , "clang/include/clang/AST/DeclBase.h", 1120, __extension__ __PRETTY_FUNCTION__ )) |
1120 | "namespace includes other than ordinary or tag")(static_cast <bool> (!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes other than ordinary or tag" ) ? void (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\"" , "clang/include/clang/AST/DeclBase.h", 1120, __extension__ __PRETTY_FUNCTION__ )); |
1121 | |
1122 | Decl *Prev = getPreviousDecl(); |
1123 | IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type); |
1124 | |
1125 | if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { |
1126 | IdentifierNamespace |= IDNS_TagFriend; |
1127 | if (PerformFriendInjection || |
1128 | (Prev && Prev->getIdentifierNamespace() & IDNS_Tag)) |
1129 | IdentifierNamespace |= IDNS_Tag | IDNS_Type; |
1130 | } |
1131 | |
1132 | if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | |
1133 | IDNS_LocalExtern | IDNS_NonMemberOperator)) { |
1134 | IdentifierNamespace |= IDNS_OrdinaryFriend; |
1135 | if (PerformFriendInjection || |
1136 | (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)) |
1137 | IdentifierNamespace |= IDNS_Ordinary; |
1138 | } |
1139 | } |
1140 | |
1141 | enum FriendObjectKind { |
1142 | FOK_None, ///< Not a friend object. |
1143 | FOK_Declared, ///< A friend of a previously-declared entity. |
1144 | FOK_Undeclared ///< A friend of a previously-undeclared entity. |
1145 | }; |
1146 | |
1147 | /// Determines whether this declaration is the object of a |
1148 | /// friend declaration and, if so, what kind. |
1149 | /// |
1150 | /// There is currently no direct way to find the associated FriendDecl. |
1151 | FriendObjectKind getFriendObjectKind() const { |
1152 | unsigned mask = |
1153 | (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); |
1154 | if (!mask) return FOK_None; |
1155 | return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared |
1156 | : FOK_Undeclared); |
1157 | } |
1158 | |
1159 | /// Specifies that this declaration is a C++ overloaded non-member. |
1160 | void setNonMemberOperator() { |
1161 | assert(getKind() == Function || getKind() == FunctionTemplate)(static_cast <bool> (getKind() == Function || getKind() == FunctionTemplate) ? void (0) : __assert_fail ("getKind() == Function || getKind() == FunctionTemplate" , "clang/include/clang/AST/DeclBase.h", 1161, __extension__ __PRETTY_FUNCTION__ )); |
1162 | assert((IdentifierNamespace & IDNS_Ordinary) &&(static_cast <bool> ((IdentifierNamespace & IDNS_Ordinary ) && "visible non-member operators should be in ordinary namespace" ) ? void (0) : __assert_fail ("(IdentifierNamespace & IDNS_Ordinary) && \"visible non-member operators should be in ordinary namespace\"" , "clang/include/clang/AST/DeclBase.h", 1163, __extension__ __PRETTY_FUNCTION__ )) |
1163 | "visible non-member operators should be in ordinary namespace")(static_cast <bool> ((IdentifierNamespace & IDNS_Ordinary ) && "visible non-member operators should be in ordinary namespace" ) ? void (0) : __assert_fail ("(IdentifierNamespace & IDNS_Ordinary) && \"visible non-member operators should be in ordinary namespace\"" , "clang/include/clang/AST/DeclBase.h", 1163, __extension__ __PRETTY_FUNCTION__ )); |
1164 | IdentifierNamespace |= IDNS_NonMemberOperator; |
1165 | } |
1166 | |
1167 | static bool classofKind(Kind K) { return true; } |
1168 | static DeclContext *castToDeclContext(const Decl *); |
1169 | static Decl *castFromDeclContext(const DeclContext *); |
1170 | |
1171 | void print(raw_ostream &Out, unsigned Indentation = 0, |
1172 | bool PrintInstantiation = false) const; |
1173 | void print(raw_ostream &Out, const PrintingPolicy &Policy, |
1174 | unsigned Indentation = 0, bool PrintInstantiation = false) const; |
1175 | static void printGroup(Decl** Begin, unsigned NumDecls, |
1176 | raw_ostream &Out, const PrintingPolicy &Policy, |
1177 | unsigned Indentation = 0); |
1178 | |
1179 | // Debuggers don't usually respect default arguments. |
1180 | void dump() const; |
1181 | |
1182 | // Same as dump(), but forces color printing. |
1183 | void dumpColor() const; |
1184 | |
1185 | void dump(raw_ostream &Out, bool Deserialize = false, |
1186 | ASTDumpOutputFormat OutputFormat = ADOF_Default) const; |
1187 | |
1188 | /// \return Unique reproducible object identifier |
1189 | int64_t getID() const; |
1190 | |
1191 | /// Looks through the Decl's underlying type to extract a FunctionType |
1192 | /// when possible. Will return null if the type underlying the Decl does not |
1193 | /// have a FunctionType. |
1194 | const FunctionType *getFunctionType(bool BlocksToo = true) const; |
1195 | |
1196 | private: |
1197 | void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx); |
1198 | void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
1199 | ASTContext &Ctx); |
1200 | |
1201 | protected: |
1202 | ASTMutationListener *getASTMutationListener() const; |
1203 | }; |
1204 | |
1205 | /// Determine whether two declarations declare the same entity. |
1206 | inline bool declaresSameEntity(const Decl *D1, const Decl *D2) { |
1207 | if (!D1 || !D2) |
1208 | return false; |
1209 | |
1210 | if (D1 == D2) |
1211 | return true; |
1212 | |
1213 | return D1->getCanonicalDecl() == D2->getCanonicalDecl(); |
1214 | } |
1215 | |
1216 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
1217 | /// doing something to a specific decl. |
1218 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
1219 | const Decl *TheDecl; |
1220 | SourceLocation Loc; |
1221 | SourceManager &SM; |
1222 | const char *Message; |
1223 | |
1224 | public: |
1225 | PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, |
1226 | SourceManager &sm, const char *Msg) |
1227 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
1228 | |
1229 | void print(raw_ostream &OS) const override; |
1230 | }; |
1231 | } // namespace clang |
1232 | |
1233 | // Required to determine the layout of the PointerUnion<NamedDecl*> before |
1234 | // seeing the NamedDecl definition being first used in DeclListNode::operator*. |
1235 | namespace llvm { |
1236 | template <> struct PointerLikeTypeTraits<::clang::NamedDecl *> { |
1237 | static inline void *getAsVoidPointer(::clang::NamedDecl *P) { return P; } |
1238 | static inline ::clang::NamedDecl *getFromVoidPointer(void *P) { |
1239 | return static_cast<::clang::NamedDecl *>(P); |
1240 | } |
1241 | static constexpr int NumLowBitsAvailable = 3; |
1242 | }; |
1243 | } |
1244 | |
1245 | namespace clang { |
1246 | /// A list storing NamedDecls in the lookup tables. |
1247 | class DeclListNode { |
1248 | friend class ASTContext; // allocate, deallocate nodes. |
1249 | friend class StoredDeclsList; |
1250 | public: |
1251 | using Decls = llvm::PointerUnion<NamedDecl*, DeclListNode*>; |
1252 | class iterator { |
1253 | friend class DeclContextLookupResult; |
1254 | friend class StoredDeclsList; |
1255 | |
1256 | Decls Ptr; |
1257 | iterator(Decls Node) : Ptr(Node) { } |
1258 | public: |
1259 | using difference_type = ptrdiff_t; |
1260 | using value_type = NamedDecl*; |
1261 | using pointer = void; |
1262 | using reference = value_type; |
1263 | using iterator_category = std::forward_iterator_tag; |
1264 | |
1265 | iterator() = default; |
1266 | |
1267 | reference operator*() const { |
1268 | assert(Ptr && "dereferencing end() iterator")(static_cast <bool> (Ptr && "dereferencing end() iterator" ) ? void (0) : __assert_fail ("Ptr && \"dereferencing end() iterator\"" , "clang/include/clang/AST/DeclBase.h", 1268, __extension__ __PRETTY_FUNCTION__ )); |
1269 | if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>()) |
1270 | return CurNode->D; |
1271 | return Ptr.get<NamedDecl*>(); |
1272 | } |
1273 | void operator->() const { } // Unsupported. |
1274 | bool operator==(const iterator &X) const { return Ptr == X.Ptr; } |
1275 | bool operator!=(const iterator &X) const { return Ptr != X.Ptr; } |
1276 | inline iterator &operator++() { // ++It |
1277 | assert(!Ptr.isNull() && "Advancing empty iterator")(static_cast <bool> (!Ptr.isNull() && "Advancing empty iterator" ) ? void (0) : __assert_fail ("!Ptr.isNull() && \"Advancing empty iterator\"" , "clang/include/clang/AST/DeclBase.h", 1277, __extension__ __PRETTY_FUNCTION__ )); |
1278 | |
1279 | if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>()) |
1280 | Ptr = CurNode->Rest; |
1281 | else |
1282 | Ptr = nullptr; |
1283 | return *this; |
1284 | } |
1285 | iterator operator++(int) { // It++ |
1286 | iterator temp = *this; |
1287 | ++(*this); |
1288 | return temp; |
1289 | } |
1290 | // Enables the pattern for (iterator I =..., E = I.end(); I != E; ++I) |
1291 | iterator end() { return iterator(); } |
1292 | }; |
1293 | private: |
1294 | NamedDecl *D = nullptr; |
1295 | Decls Rest = nullptr; |
1296 | DeclListNode(NamedDecl *ND) : D(ND) {} |
1297 | }; |
1298 | |
1299 | /// The results of name lookup within a DeclContext. |
1300 | class DeclContextLookupResult { |
1301 | using Decls = DeclListNode::Decls; |
1302 | |
1303 | /// When in collection form, this is what the Data pointer points to. |
1304 | Decls Result; |
1305 | |
1306 | public: |
1307 | DeclContextLookupResult() = default; |
1308 | DeclContextLookupResult(Decls Result) : Result(Result) {} |
1309 | |
1310 | using iterator = DeclListNode::iterator; |
1311 | using const_iterator = iterator; |
1312 | using reference = iterator::reference; |
1313 | |
1314 | iterator begin() { return iterator(Result); } |
1315 | iterator end() { return iterator(); } |
1316 | const_iterator begin() const { |
1317 | return const_cast<DeclContextLookupResult*>(this)->begin(); |
1318 | } |
1319 | const_iterator end() const { return iterator(); } |
1320 | |
1321 | bool empty() const { return Result.isNull(); } |
1322 | bool isSingleResult() const { return Result.dyn_cast<NamedDecl*>(); } |
1323 | reference front() const { return *begin(); } |
1324 | |
1325 | // Find the first declaration of the given type in the list. Note that this |
1326 | // is not in general the earliest-declared declaration, and should only be |
1327 | // used when it's not possible for there to be more than one match or where |
1328 | // it doesn't matter which one is found. |
1329 | template<class T> T *find_first() const { |
1330 | for (auto *D : *this) |
1331 | if (T *Decl = dyn_cast<T>(D)) |
1332 | return Decl; |
1333 | |
1334 | return nullptr; |
1335 | } |
1336 | }; |
1337 | |
1338 | /// DeclContext - This is used only as base class of specific decl types that |
1339 | /// can act as declaration contexts. These decls are (only the top classes |
1340 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
1341 | /// |
1342 | /// TranslationUnitDecl |
1343 | /// ExternCContext |
1344 | /// NamespaceDecl |
1345 | /// TagDecl |
1346 | /// OMPDeclareReductionDecl |
1347 | /// OMPDeclareMapperDecl |
1348 | /// FunctionDecl |
1349 | /// ObjCMethodDecl |
1350 | /// ObjCContainerDecl |
1351 | /// LinkageSpecDecl |
1352 | /// ExportDecl |
1353 | /// BlockDecl |
1354 | /// CapturedDecl |
1355 | class DeclContext { |
1356 | /// For makeDeclVisibleInContextImpl |
1357 | friend class ASTDeclReader; |
1358 | /// For reconcileExternalVisibleStorage, CreateStoredDeclsMap, |
1359 | /// hasNeedToReconcileExternalVisibleStorage |
1360 | friend class ExternalASTSource; |
1361 | /// For CreateStoredDeclsMap |
1362 | friend class DependentDiagnostic; |
1363 | /// For hasNeedToReconcileExternalVisibleStorage, |
1364 | /// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups |
1365 | friend class ASTWriter; |
1366 | |
1367 | // We use uint64_t in the bit-fields below since some bit-fields |
1368 | // cross the unsigned boundary and this breaks the packing. |
1369 | |
1370 | /// Stores the bits used by DeclContext. |
1371 | /// If modified NumDeclContextBit, the ctor of DeclContext and the accessor |
1372 | /// methods in DeclContext should be updated appropriately. |
1373 | class DeclContextBitfields { |
1374 | friend class DeclContext; |
1375 | /// DeclKind - This indicates which class this is. |
1376 | uint64_t DeclKind : 7; |
1377 | |
1378 | /// Whether this declaration context also has some external |
1379 | /// storage that contains additional declarations that are lexically |
1380 | /// part of this context. |
1381 | mutable uint64_t ExternalLexicalStorage : 1; |
1382 | |
1383 | /// Whether this declaration context also has some external |
1384 | /// storage that contains additional declarations that are visible |
1385 | /// in this context. |
1386 | mutable uint64_t ExternalVisibleStorage : 1; |
1387 | |
1388 | /// Whether this declaration context has had externally visible |
1389 | /// storage added since the last lookup. In this case, \c LookupPtr's |
1390 | /// invariant may not hold and needs to be fixed before we perform |
1391 | /// another lookup. |
1392 | mutable uint64_t NeedToReconcileExternalVisibleStorage : 1; |
1393 | |
1394 | /// If \c true, this context may have local lexical declarations |
1395 | /// that are missing from the lookup table. |
1396 | mutable uint64_t HasLazyLocalLexicalLookups : 1; |
1397 | |
1398 | /// If \c true, the external source may have lexical declarations |
1399 | /// that are missing from the lookup table. |
1400 | mutable uint64_t HasLazyExternalLexicalLookups : 1; |
1401 | |
1402 | /// If \c true, lookups should only return identifier from |
1403 | /// DeclContext scope (for example TranslationUnit). Used in |
1404 | /// LookupQualifiedName() |
1405 | mutable uint64_t UseQualifiedLookup : 1; |
1406 | }; |
1407 | |
1408 | /// Number of bits in DeclContextBitfields. |
1409 | enum { NumDeclContextBits = 13 }; |
1410 | |
1411 | /// Stores the bits used by TagDecl. |
1412 | /// If modified NumTagDeclBits and the accessor |
1413 | /// methods in TagDecl should be updated appropriately. |
1414 | class TagDeclBitfields { |
1415 | friend class TagDecl; |
1416 | /// For the bits in DeclContextBitfields |
1417 | uint64_t : NumDeclContextBits; |
1418 | |
1419 | /// The TagKind enum. |
1420 | uint64_t TagDeclKind : 3; |
1421 | |
1422 | /// True if this is a definition ("struct foo {};"), false if it is a |
1423 | /// declaration ("struct foo;"). It is not considered a definition |
1424 | /// until the definition has been fully processed. |
1425 | uint64_t IsCompleteDefinition : 1; |
1426 | |
1427 | /// True if this is currently being defined. |
1428 | uint64_t IsBeingDefined : 1; |
1429 | |
1430 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
1431 | /// for the very first time) in the syntax of a declarator. |
1432 | uint64_t IsEmbeddedInDeclarator : 1; |
1433 | |
1434 | /// True if this tag is free standing, e.g. "struct foo;". |
1435 | uint64_t IsFreeStanding : 1; |
1436 | |
1437 | /// Indicates whether it is possible for declarations of this kind |
1438 | /// to have an out-of-date definition. |
1439 | /// |
1440 | /// This option is only enabled when modules are enabled. |
1441 | uint64_t MayHaveOutOfDateDef : 1; |
1442 | |
1443 | /// Has the full definition of this type been required by a use somewhere in |
1444 | /// the TU. |
1445 | uint64_t IsCompleteDefinitionRequired : 1; |
1446 | }; |
1447 | |
1448 | /// Number of non-inherited bits in TagDeclBitfields. |
1449 | enum { NumTagDeclBits = 9 }; |
1450 | |
1451 | /// Stores the bits used by EnumDecl. |
1452 | /// If modified NumEnumDeclBit and the accessor |
1453 | /// methods in EnumDecl should be updated appropriately. |
1454 | class EnumDeclBitfields { |
1455 | friend class EnumDecl; |
1456 | /// For the bits in DeclContextBitfields. |
1457 | uint64_t : NumDeclContextBits; |
1458 | /// For the bits in TagDeclBitfields. |
1459 | uint64_t : NumTagDeclBits; |
1460 | |
1461 | /// Width in bits required to store all the non-negative |
1462 | /// enumerators of this enum. |
1463 | uint64_t NumPositiveBits : 8; |
1464 | |
1465 | /// Width in bits required to store all the negative |
1466 | /// enumerators of this enum. |
1467 | uint64_t NumNegativeBits : 8; |
1468 | |
1469 | /// True if this tag declaration is a scoped enumeration. Only |
1470 | /// possible in C++11 mode. |
1471 | uint64_t IsScoped : 1; |
1472 | |
1473 | /// If this tag declaration is a scoped enum, |
1474 | /// then this is true if the scoped enum was declared using the class |
1475 | /// tag, false if it was declared with the struct tag. No meaning is |
1476 | /// associated if this tag declaration is not a scoped enum. |
1477 | uint64_t IsScopedUsingClassTag : 1; |
1478 | |
1479 | /// True if this is an enumeration with fixed underlying type. Only |
1480 | /// possible in C++11, Microsoft extensions, or Objective C mode. |
1481 | uint64_t IsFixed : 1; |
1482 | |
1483 | /// True if a valid hash is stored in ODRHash. |
1484 | uint64_t HasODRHash : 1; |
1485 | }; |
1486 | |
1487 | /// Number of non-inherited bits in EnumDeclBitfields. |
1488 | enum { NumEnumDeclBits = 20 }; |
1489 | |
1490 | /// Stores the bits used by RecordDecl. |
1491 | /// If modified NumRecordDeclBits and the accessor |
1492 | /// methods in RecordDecl should be updated appropriately. |
1493 | class RecordDeclBitfields { |
1494 | friend class RecordDecl; |
1495 | /// For the bits in DeclContextBitfields. |
1496 | uint64_t : NumDeclContextBits; |
1497 | /// For the bits in TagDeclBitfields. |
1498 | uint64_t : NumTagDeclBits; |
1499 | |
1500 | /// This is true if this struct ends with a flexible |
1501 | /// array member (e.g. int X[]) or if this union contains a struct that does. |
1502 | /// If so, this cannot be contained in arrays or other structs as a member. |
1503 | uint64_t HasFlexibleArrayMember : 1; |
1504 | |
1505 | /// Whether this is the type of an anonymous struct or union. |
1506 | uint64_t AnonymousStructOrUnion : 1; |
1507 | |
1508 | /// This is true if this struct has at least one member |
1509 | /// containing an Objective-C object pointer type. |
1510 | uint64_t HasObjectMember : 1; |
1511 | |
1512 | /// This is true if struct has at least one member of |
1513 | /// 'volatile' type. |
1514 | uint64_t HasVolatileMember : 1; |
1515 | |
1516 | /// Whether the field declarations of this record have been loaded |
1517 | /// from external storage. To avoid unnecessary deserialization of |
1518 | /// methods/nested types we allow deserialization of just the fields |
1519 | /// when needed. |
1520 | mutable uint64_t LoadedFieldsFromExternalStorage : 1; |
1521 | |
1522 | /// Basic properties of non-trivial C structs. |
1523 | uint64_t NonTrivialToPrimitiveDefaultInitialize : 1; |
1524 | uint64_t NonTrivialToPrimitiveCopy : 1; |
1525 | uint64_t NonTrivialToPrimitiveDestroy : 1; |
1526 | |
1527 | /// The following bits indicate whether this is or contains a C union that |
1528 | /// is non-trivial to default-initialize, destruct, or copy. These bits |
1529 | /// imply the associated basic non-triviality predicates declared above. |
1530 | uint64_t HasNonTrivialToPrimitiveDefaultInitializeCUnion : 1; |
1531 | uint64_t HasNonTrivialToPrimitiveDestructCUnion : 1; |
1532 | uint64_t HasNonTrivialToPrimitiveCopyCUnion : 1; |
1533 | |
1534 | /// Indicates whether this struct is destroyed in the callee. |
1535 | uint64_t ParamDestroyedInCallee : 1; |
1536 | |
1537 | /// Represents the way this type is passed to a function. |
1538 | uint64_t ArgPassingRestrictions : 2; |
1539 | }; |
1540 | |
1541 | /// Number of non-inherited bits in RecordDeclBitfields. |
1542 | enum { NumRecordDeclBits = 14 }; |
1543 | |
1544 | /// Stores the bits used by OMPDeclareReductionDecl. |
1545 | /// If modified NumOMPDeclareReductionDeclBits and the accessor |
1546 | /// methods in OMPDeclareReductionDecl should be updated appropriately. |
1547 | class OMPDeclareReductionDeclBitfields { |
1548 | friend class OMPDeclareReductionDecl; |
1549 | /// For the bits in DeclContextBitfields |
1550 | uint64_t : NumDeclContextBits; |
1551 | |
1552 | /// Kind of initializer, |
1553 | /// function call or omp_priv<init_expr> initializtion. |
1554 | uint64_t InitializerKind : 2; |
1555 | }; |
1556 | |
1557 | /// Number of non-inherited bits in OMPDeclareReductionDeclBitfields. |
1558 | enum { NumOMPDeclareReductionDeclBits = 2 }; |
1559 | |
1560 | /// Stores the bits used by FunctionDecl. |
1561 | /// If modified NumFunctionDeclBits and the accessor |
1562 | /// methods in FunctionDecl and CXXDeductionGuideDecl |
1563 | /// (for IsCopyDeductionCandidate) should be updated appropriately. |
1564 | class FunctionDeclBitfields { |
1565 | friend class FunctionDecl; |
1566 | /// For IsCopyDeductionCandidate |
1567 | friend class CXXDeductionGuideDecl; |
1568 | /// For the bits in DeclContextBitfields. |
1569 | uint64_t : NumDeclContextBits; |
1570 | |
1571 | uint64_t SClass : 3; |
1572 | uint64_t IsInline : 1; |
1573 | uint64_t IsInlineSpecified : 1; |
1574 | |
1575 | uint64_t IsVirtualAsWritten : 1; |
1576 | uint64_t IsPure : 1; |
1577 | uint64_t HasInheritedPrototype : 1; |
1578 | uint64_t HasWrittenPrototype : 1; |
1579 | uint64_t IsDeleted : 1; |
1580 | /// Used by CXXMethodDecl |
1581 | uint64_t IsTrivial : 1; |
1582 | |
1583 | /// This flag indicates whether this function is trivial for the purpose of |
1584 | /// calls. This is meaningful only when this function is a copy/move |
1585 | /// constructor or a destructor. |
1586 | uint64_t IsTrivialForCall : 1; |
1587 | |
1588 | uint64_t IsDefaulted : 1; |
1589 | uint64_t IsExplicitlyDefaulted : 1; |
1590 | uint64_t HasDefaultedFunctionInfo : 1; |
1591 | uint64_t HasImplicitReturnZero : 1; |
1592 | uint64_t IsLateTemplateParsed : 1; |
1593 | |
1594 | /// Kind of contexpr specifier as defined by ConstexprSpecKind. |
1595 | uint64_t ConstexprKind : 2; |
1596 | uint64_t InstantiationIsPending : 1; |
1597 | |
1598 | /// Indicates if the function uses __try. |
1599 | uint64_t UsesSEHTry : 1; |
1600 | |
1601 | /// Indicates if the function was a definition |
1602 | /// but its body was skipped. |
1603 | uint64_t HasSkippedBody : 1; |
1604 | |
1605 | /// Indicates if the function declaration will |
1606 | /// have a body, once we're done parsing it. |
1607 | uint64_t WillHaveBody : 1; |
1608 | |
1609 | /// Indicates that this function is a multiversioned |
1610 | /// function using attribute 'target'. |
1611 | uint64_t IsMultiVersion : 1; |
1612 | |
1613 | /// [C++17] Only used by CXXDeductionGuideDecl. Indicates that |
1614 | /// the Deduction Guide is the implicitly generated 'copy |
1615 | /// deduction candidate' (is used during overload resolution). |
1616 | uint64_t IsCopyDeductionCandidate : 1; |
1617 | |
1618 | /// Store the ODRHash after first calculation. |
1619 | uint64_t HasODRHash : 1; |
1620 | |
1621 | /// Indicates if the function uses Floating Point Constrained Intrinsics |
1622 | uint64_t UsesFPIntrin : 1; |
1623 | }; |
1624 | |
1625 | /// Number of non-inherited bits in FunctionDeclBitfields. |
1626 | enum { NumFunctionDeclBits = 27 }; |
1627 | |
1628 | /// Stores the bits used by CXXConstructorDecl. If modified |
1629 | /// NumCXXConstructorDeclBits and the accessor |
1630 | /// methods in CXXConstructorDecl should be updated appropriately. |
1631 | class CXXConstructorDeclBitfields { |
1632 | friend class CXXConstructorDecl; |
1633 | /// For the bits in DeclContextBitfields. |
1634 | uint64_t : NumDeclContextBits; |
1635 | /// For the bits in FunctionDeclBitfields. |
1636 | uint64_t : NumFunctionDeclBits; |
1637 | |
1638 | /// 24 bits to fit in the remaining available space. |
1639 | /// Note that this makes CXXConstructorDeclBitfields take |
1640 | /// exactly 64 bits and thus the width of NumCtorInitializers |
1641 | /// will need to be shrunk if some bit is added to NumDeclContextBitfields, |
1642 | /// NumFunctionDeclBitfields or CXXConstructorDeclBitfields. |
1643 | uint64_t NumCtorInitializers : 21; |
1644 | uint64_t IsInheritingConstructor : 1; |
1645 | |
1646 | /// Whether this constructor has a trail-allocated explicit specifier. |
1647 | uint64_t HasTrailingExplicitSpecifier : 1; |
1648 | /// If this constructor does't have a trail-allocated explicit specifier. |
1649 | /// Whether this constructor is explicit specified. |
1650 | uint64_t IsSimpleExplicit : 1; |
1651 | }; |
1652 | |
1653 | /// Number of non-inherited bits in CXXConstructorDeclBitfields. |
1654 | enum { |
1655 | NumCXXConstructorDeclBits = 64 - NumDeclContextBits - NumFunctionDeclBits |
1656 | }; |
1657 | |
1658 | /// Stores the bits used by ObjCMethodDecl. |
1659 | /// If modified NumObjCMethodDeclBits and the accessor |
1660 | /// methods in ObjCMethodDecl should be updated appropriately. |
1661 | class ObjCMethodDeclBitfields { |
1662 | friend class ObjCMethodDecl; |
1663 | |
1664 | /// For the bits in DeclContextBitfields. |
1665 | uint64_t : NumDeclContextBits; |
1666 | |
1667 | /// The conventional meaning of this method; an ObjCMethodFamily. |
1668 | /// This is not serialized; instead, it is computed on demand and |
1669 | /// cached. |
1670 | mutable uint64_t Family : ObjCMethodFamilyBitWidth; |
1671 | |
1672 | /// instance (true) or class (false) method. |
1673 | uint64_t IsInstance : 1; |
1674 | uint64_t IsVariadic : 1; |
1675 | |
1676 | /// True if this method is the getter or setter for an explicit property. |
1677 | uint64_t IsPropertyAccessor : 1; |
1678 | |
1679 | /// True if this method is a synthesized property accessor stub. |
1680 | uint64_t IsSynthesizedAccessorStub : 1; |
1681 | |
1682 | /// Method has a definition. |
1683 | uint64_t IsDefined : 1; |
1684 | |
1685 | /// Method redeclaration in the same interface. |
1686 | uint64_t IsRedeclaration : 1; |
1687 | |
1688 | /// Is redeclared in the same interface. |
1689 | mutable uint64_t HasRedeclaration : 1; |
1690 | |
1691 | /// \@required/\@optional |
1692 | uint64_t DeclImplementation : 2; |
1693 | |
1694 | /// in, inout, etc. |
1695 | uint64_t objcDeclQualifier : 7; |
1696 | |
1697 | /// Indicates whether this method has a related result type. |
1698 | uint64_t RelatedResultType : 1; |
1699 | |
1700 | /// Whether the locations of the selector identifiers are in a |
1701 | /// "standard" position, a enum SelectorLocationsKind. |
1702 | uint64_t SelLocsKind : 2; |
1703 | |
1704 | /// Whether this method overrides any other in the class hierarchy. |
1705 | /// |
1706 | /// A method is said to override any method in the class's |
1707 | /// base classes, its protocols, or its categories' protocols, that has |
1708 | /// the same selector and is of the same kind (class or instance). |
1709 | /// A method in an implementation is not considered as overriding the same |
1710 | /// method in the interface or its categories. |
1711 | uint64_t IsOverriding : 1; |
1712 | |
1713 | /// Indicates if the method was a definition but its body was skipped. |
1714 | uint64_t HasSkippedBody : 1; |
1715 | }; |
1716 | |
1717 | /// Number of non-inherited bits in ObjCMethodDeclBitfields. |
1718 | enum { NumObjCMethodDeclBits = 24 }; |
1719 | |
1720 | /// Stores the bits used by ObjCContainerDecl. |
1721 | /// If modified NumObjCContainerDeclBits and the accessor |
1722 | /// methods in ObjCContainerDecl should be updated appropriately. |
1723 | class ObjCContainerDeclBitfields { |
1724 | friend class ObjCContainerDecl; |
1725 | /// For the bits in DeclContextBitfields |
1726 | uint32_t : NumDeclContextBits; |
1727 | |
1728 | // Not a bitfield but this saves space. |
1729 | // Note that ObjCContainerDeclBitfields is full. |
1730 | SourceLocation AtStart; |
1731 | }; |
1732 | |
1733 | /// Number of non-inherited bits in ObjCContainerDeclBitfields. |
1734 | /// Note that here we rely on the fact that SourceLocation is 32 bits |
1735 | /// wide. We check this with the static_assert in the ctor of DeclContext. |
1736 | enum { NumObjCContainerDeclBits = 64 - NumDeclContextBits }; |
1737 | |
1738 | /// Stores the bits used by LinkageSpecDecl. |
1739 | /// If modified NumLinkageSpecDeclBits and the accessor |
1740 | /// methods in LinkageSpecDecl should be updated appropriately. |
1741 | class LinkageSpecDeclBitfields { |
1742 | friend class LinkageSpecDecl; |
1743 | /// For the bits in DeclContextBitfields. |
1744 | uint64_t : NumDeclContextBits; |
1745 | |
1746 | /// The language for this linkage specification with values |
1747 | /// in the enum LinkageSpecDecl::LanguageIDs. |
1748 | uint64_t Language : 3; |
1749 | |
1750 | /// True if this linkage spec has braces. |
1751 | /// This is needed so that hasBraces() returns the correct result while the |
1752 | /// linkage spec body is being parsed. Once RBraceLoc has been set this is |
1753 | /// not used, so it doesn't need to be serialized. |
1754 | uint64_t HasBraces : 1; |
1755 | }; |
1756 | |
1757 | /// Number of non-inherited bits in LinkageSpecDeclBitfields. |
1758 | enum { NumLinkageSpecDeclBits = 4 }; |
1759 | |
1760 | /// Stores the bits used by BlockDecl. |
1761 | /// If modified NumBlockDeclBits and the accessor |
1762 | /// methods in BlockDecl should be updated appropriately. |
1763 | class BlockDeclBitfields { |
1764 | friend class BlockDecl; |
1765 | /// For the bits in DeclContextBitfields. |
1766 | uint64_t : NumDeclContextBits; |
1767 | |
1768 | uint64_t IsVariadic : 1; |
1769 | uint64_t CapturesCXXThis : 1; |
1770 | uint64_t BlockMissingReturnType : 1; |
1771 | uint64_t IsConversionFromLambda : 1; |
1772 | |
1773 | /// A bit that indicates this block is passed directly to a function as a |
1774 | /// non-escaping parameter. |
1775 | uint64_t DoesNotEscape : 1; |
1776 | |
1777 | /// A bit that indicates whether it's possible to avoid coying this block to |
1778 | /// the heap when it initializes or is assigned to a local variable with |
1779 | /// automatic storage. |
1780 | uint64_t CanAvoidCopyToHeap : 1; |
1781 | }; |
1782 | |
1783 | /// Number of non-inherited bits in BlockDeclBitfields. |
1784 | enum { NumBlockDeclBits = 5 }; |
1785 | |
1786 | /// Pointer to the data structure used to lookup declarations |
1787 | /// within this context (or a DependentStoredDeclsMap if this is a |
1788 | /// dependent context). We maintain the invariant that, if the map |
1789 | /// contains an entry for a DeclarationName (and we haven't lazily |
1790 | /// omitted anything), then it contains all relevant entries for that |
1791 | /// name (modulo the hasExternalDecls() flag). |
1792 | mutable StoredDeclsMap *LookupPtr = nullptr; |
1793 | |
1794 | protected: |
1795 | /// This anonymous union stores the bits belonging to DeclContext and classes |
1796 | /// deriving from it. The goal is to use otherwise wasted |
1797 | /// space in DeclContext to store data belonging to derived classes. |
1798 | /// The space saved is especially significient when pointers are aligned |
1799 | /// to 8 bytes. In this case due to alignment requirements we have a |
1800 | /// little less than 8 bytes free in DeclContext which we can use. |
1801 | /// We check that none of the classes in this union is larger than |
1802 | /// 8 bytes with static_asserts in the ctor of DeclContext. |
1803 | union { |
1804 | DeclContextBitfields DeclContextBits; |
1805 | TagDeclBitfields TagDeclBits; |
1806 | EnumDeclBitfields EnumDeclBits; |
1807 | RecordDeclBitfields RecordDeclBits; |
1808 | OMPDeclareReductionDeclBitfields OMPDeclareReductionDeclBits; |
1809 | FunctionDeclBitfields FunctionDeclBits; |
1810 | CXXConstructorDeclBitfields CXXConstructorDeclBits; |
1811 | ObjCMethodDeclBitfields ObjCMethodDeclBits; |
1812 | ObjCContainerDeclBitfields ObjCContainerDeclBits; |
1813 | LinkageSpecDeclBitfields LinkageSpecDeclBits; |
1814 | BlockDeclBitfields BlockDeclBits; |
1815 | |
1816 | static_assert(sizeof(DeclContextBitfields) <= 8, |
1817 | "DeclContextBitfields is larger than 8 bytes!"); |
1818 | static_assert(sizeof(TagDeclBitfields) <= 8, |
1819 | "TagDeclBitfields is larger than 8 bytes!"); |
1820 | static_assert(sizeof(EnumDeclBitfields) <= 8, |
1821 | "EnumDeclBitfields is larger than 8 bytes!"); |
1822 | static_assert(sizeof(RecordDeclBitfields) <= 8, |
1823 | "RecordDeclBitfields is larger than 8 bytes!"); |
1824 | static_assert(sizeof(OMPDeclareReductionDeclBitfields) <= 8, |
1825 | "OMPDeclareReductionDeclBitfields is larger than 8 bytes!"); |
1826 | static_assert(sizeof(FunctionDeclBitfields) <= 8, |
1827 | "FunctionDeclBitfields is larger than 8 bytes!"); |
1828 | static_assert(sizeof(CXXConstructorDeclBitfields) <= 8, |
1829 | "CXXConstructorDeclBitfields is larger than 8 bytes!"); |
1830 | static_assert(sizeof(ObjCMethodDeclBitfields) <= 8, |
1831 | "ObjCMethodDeclBitfields is larger than 8 bytes!"); |
1832 | static_assert(sizeof(ObjCContainerDeclBitfields) <= 8, |
1833 | "ObjCContainerDeclBitfields is larger than 8 bytes!"); |
1834 | static_assert(sizeof(LinkageSpecDeclBitfields) <= 8, |
1835 | "LinkageSpecDeclBitfields is larger than 8 bytes!"); |
1836 | static_assert(sizeof(BlockDeclBitfields) <= 8, |
1837 | "BlockDeclBitfields is larger than 8 bytes!"); |
1838 | }; |
1839 | |
1840 | /// FirstDecl - The first declaration stored within this declaration |
1841 | /// context. |
1842 | mutable Decl *FirstDecl = nullptr; |
1843 | |
1844 | /// LastDecl - The last declaration stored within this declaration |
1845 | /// context. FIXME: We could probably cache this value somewhere |
1846 | /// outside of the DeclContext, to reduce the size of DeclContext by |
1847 | /// another pointer. |
1848 | mutable Decl *LastDecl = nullptr; |
1849 | |
1850 | /// Build up a chain of declarations. |
1851 | /// |
1852 | /// \returns the first/last pair of declarations. |
1853 | static std::pair<Decl *, Decl *> |
1854 | BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded); |
1855 | |
1856 | DeclContext(Decl::Kind K); |
1857 | |
1858 | public: |
1859 | ~DeclContext(); |
1860 | |
1861 | Decl::Kind getDeclKind() const { |
1862 | return static_cast<Decl::Kind>(DeclContextBits.DeclKind); |
1863 | } |
1864 | |
1865 | const char *getDeclKindName() const; |
1866 | |
1867 | /// getParent - Returns the containing DeclContext. |
1868 | DeclContext *getParent() { |
1869 | return cast<Decl>(this)->getDeclContext(); |
1870 | } |
1871 | const DeclContext *getParent() const { |
1872 | return const_cast<DeclContext*>(this)->getParent(); |
1873 | } |
1874 | |
1875 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
1876 | /// different from getParent, e.g.: |
1877 | /// |
1878 | /// namespace A { |
1879 | /// struct S; |
1880 | /// } |
1881 | /// struct A::S {}; // getParent() == namespace 'A' |
1882 | /// // getLexicalParent() == translation unit |
1883 | /// |
1884 | DeclContext *getLexicalParent() { |
1885 | return cast<Decl>(this)->getLexicalDeclContext(); |
1886 | } |
1887 | const DeclContext *getLexicalParent() const { |
1888 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
1889 | } |
1890 | |
1891 | DeclContext *getLookupParent(); |
1892 | |
1893 | const DeclContext *getLookupParent() const { |
1894 | return const_cast<DeclContext*>(this)->getLookupParent(); |
1895 | } |
1896 | |
1897 | ASTContext &getParentASTContext() const { |
1898 | return cast<Decl>(this)->getASTContext(); |
1899 | } |
1900 | |
1901 | bool isClosure() const { return getDeclKind() == Decl::Block; } |
1902 | |
1903 | /// Return this DeclContext if it is a BlockDecl. Otherwise, return the |
1904 | /// innermost enclosing BlockDecl or null if there are no enclosing blocks. |
1905 | const BlockDecl *getInnermostBlockDecl() const; |
1906 | |
1907 | bool isObjCContainer() const { |
1908 | switch (getDeclKind()) { |
1909 | case Decl::ObjCCategory: |
1910 | case Decl::ObjCCategoryImpl: |
1911 | case Decl::ObjCImplementation: |
1912 | case Decl::ObjCInterface: |
1913 | case Decl::ObjCProtocol: |
1914 | return true; |
1915 | default: |
1916 | return false; |
1917 | } |
1918 | } |
1919 | |
1920 | bool isFunctionOrMethod() const { |
1921 | switch (getDeclKind()) { |
1922 | case Decl::Block: |
1923 | case Decl::Captured: |
1924 | case Decl::ObjCMethod: |
1925 | return true; |
1926 | default: |
1927 | return getDeclKind() >= Decl::firstFunction && |
1928 | getDeclKind() <= Decl::lastFunction; |
1929 | } |
1930 | } |
1931 | |
1932 | /// Test whether the context supports looking up names. |
1933 | bool isLookupContext() const { |
1934 | return !isFunctionOrMethod() && getDeclKind() != Decl::LinkageSpec && |
1935 | getDeclKind() != Decl::Export; |
1936 | } |
1937 | |
1938 | bool isFileContext() const { |
1939 | return getDeclKind() == Decl::TranslationUnit || |
1940 | getDeclKind() == Decl::Namespace; |
1941 | } |
1942 | |
1943 | bool isTranslationUnit() const { |
1944 | return getDeclKind() == Decl::TranslationUnit; |
1945 | } |
1946 | |
1947 | bool isRecord() const { |
1948 | return getDeclKind() >= Decl::firstRecord && |
1949 | getDeclKind() <= Decl::lastRecord; |
1950 | } |
1951 | |
1952 | bool isNamespace() const { return getDeclKind() == Decl::Namespace; } |
1953 | |
1954 | bool isStdNamespace() const; |
1955 | |
1956 | bool isInlineNamespace() const; |
1957 | |
1958 | /// Determines whether this context is dependent on a |
1959 | /// template parameter. |
1960 | bool isDependentContext() const; |
1961 | |
1962 | /// isTransparentContext - Determines whether this context is a |
1963 | /// "transparent" context, meaning that the members declared in this |
1964 | /// context are semantically declared in the nearest enclosing |
1965 | /// non-transparent (opaque) context but are lexically declared in |
1966 | /// this context. For example, consider the enumerators of an |
1967 | /// enumeration type: |
1968 | /// @code |
1969 | /// enum E { |
1970 | /// Val1 |
1971 | /// }; |
1972 | /// @endcode |
1973 | /// Here, E is a transparent context, so its enumerator (Val1) will |
1974 | /// appear (semantically) that it is in the same context of E. |
1975 | /// Examples of transparent contexts include: enumerations (except for |
1976 | /// C++0x scoped enums), and C++ linkage specifications. |
1977 | bool isTransparentContext() const; |
1978 | |
1979 | /// Determines whether this context or some of its ancestors is a |
1980 | /// linkage specification context that specifies C linkage. |
1981 | bool isExternCContext() const; |
1982 | |
1983 | /// Retrieve the nearest enclosing C linkage specification context. |
1984 | const LinkageSpecDecl *getExternCContext() const; |
1985 | |
1986 | /// Determines whether this context or some of its ancestors is a |
1987 | /// linkage specification context that specifies C++ linkage. |
1988 | bool isExternCXXContext() const; |
1989 | |
1990 | /// Determine whether this declaration context is equivalent |
1991 | /// to the declaration context DC. |
1992 | bool Equals(const DeclContext *DC) const { |
1993 | return DC && this->getPrimaryContext() == DC->getPrimaryContext(); |
1994 | } |
1995 | |
1996 | /// Determine whether this declaration context encloses the |
1997 | /// declaration context DC. |
1998 | bool Encloses(const DeclContext *DC) const; |
1999 | |
2000 | /// Find the nearest non-closure ancestor of this context, |
2001 | /// i.e. the innermost semantic parent of this context which is not |
2002 | /// a closure. A context may be its own non-closure ancestor. |
2003 | Decl *getNonClosureAncestor(); |
2004 | const Decl *getNonClosureAncestor() const { |
2005 | return const_cast<DeclContext*>(this)->getNonClosureAncestor(); |
2006 | } |
2007 | |
2008 | // Retrieve the nearest context that is not a transparent context. |
2009 | DeclContext *getNonTransparentContext(); |
2010 | const DeclContext *getNonTransparentContext() const { |
2011 | return const_cast<DeclContext *>(this)->getNonTransparentContext(); |
2012 | } |
2013 | |
2014 | /// getPrimaryContext - There may be many different |
2015 | /// declarations of the same entity (including forward declarations |
2016 | /// of classes, multiple definitions of namespaces, etc.), each with |
2017 | /// a different set of declarations. This routine returns the |
2018 | /// "primary" DeclContext structure, which will contain the |
2019 | /// information needed to perform name lookup into this context. |
2020 | DeclContext *getPrimaryContext(); |
2021 | const DeclContext *getPrimaryContext() const { |
2022 | return const_cast<DeclContext*>(this)->getPrimaryContext(); |
2023 | } |
2024 | |
2025 | /// getRedeclContext - Retrieve the context in which an entity conflicts with |
2026 | /// other entities of the same name, or where it is a redeclaration if the |
2027 | /// two entities are compatible. This skips through transparent contexts. |
2028 | DeclContext *getRedeclContext(); |
2029 | const DeclContext *getRedeclContext() const { |
2030 | return const_cast<DeclContext *>(this)->getRedeclContext(); |
2031 | } |
2032 | |
2033 | /// Retrieve the nearest enclosing namespace context. |
2034 | DeclContext *getEnclosingNamespaceContext(); |
2035 | const DeclContext *getEnclosingNamespaceContext() const { |
2036 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
2037 | } |
2038 | |
2039 | /// Retrieve the outermost lexically enclosing record context. |
2040 | RecordDecl *getOuterLexicalRecordContext(); |
2041 | const RecordDecl *getOuterLexicalRecordContext() const { |
2042 | return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext(); |
2043 | } |
2044 | |
2045 | /// Test if this context is part of the enclosing namespace set of |
2046 | /// the context NS, as defined in C++0x [namespace.def]p9. If either context |
2047 | /// isn't a namespace, this is equivalent to Equals(). |
2048 | /// |
2049 | /// The enclosing namespace set of a namespace is the namespace and, if it is |
2050 | /// inline, its enclosing namespace, recursively. |
2051 | bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; |
2052 | |
2053 | /// Collects all of the declaration contexts that are semantically |
2054 | /// connected to this declaration context. |
2055 | /// |
2056 | /// For declaration contexts that have multiple semantically connected but |
2057 | /// syntactically distinct contexts, such as C++ namespaces, this routine |
2058 | /// retrieves the complete set of such declaration contexts in source order. |
2059 | /// For example, given: |
2060 | /// |
2061 | /// \code |
2062 | /// namespace N { |
2063 | /// int x; |
2064 | /// } |
2065 | /// namespace N { |
2066 | /// int y; |
2067 | /// } |
2068 | /// \endcode |
2069 | /// |
2070 | /// The \c Contexts parameter will contain both definitions of N. |
2071 | /// |
2072 | /// \param Contexts Will be cleared and set to the set of declaration |
2073 | /// contexts that are semanticaly connected to this declaration context, |
2074 | /// in source order, including this context (which may be the only result, |
2075 | /// for non-namespace contexts). |
2076 | void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts); |
2077 | |
2078 | /// decl_iterator - Iterates through the declarations stored |
2079 | /// within this context. |
2080 | class decl_iterator { |
2081 | /// Current - The current declaration. |
2082 | Decl *Current = nullptr; |
2083 | |
2084 | public: |
2085 | using value_type = Decl *; |
2086 | using reference = const value_type &; |
2087 | using pointer = const value_type *; |
2088 | using iterator_category = std::forward_iterator_tag; |
2089 | using difference_type = std::ptrdiff_t; |
2090 | |
2091 | decl_iterator() = default; |
2092 | explicit decl_iterator(Decl *C) : Current(C) {} |
2093 | |
2094 | reference operator*() const { return Current; } |
2095 | |
2096 | // This doesn't meet the iterator requirements, but it's convenient |
2097 | value_type operator->() const { return Current; } |
2098 | |
2099 | decl_iterator& operator++() { |
2100 | Current = Current->getNextDeclInContext(); |
2101 | return *this; |
2102 | } |
2103 | |
2104 | decl_iterator operator++(int) { |
2105 | decl_iterator tmp(*this); |
2106 | ++(*this); |
2107 | return tmp; |
2108 | } |
2109 | |
2110 | friend bool operator==(decl_iterator x, decl_iterator y) { |
2111 | return x.Current == y.Current; |
2112 | } |
2113 | |
2114 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
2115 | return x.Current != y.Current; |
2116 | } |
2117 | }; |
2118 | |
2119 | using decl_range = llvm::iterator_range<decl_iterator>; |
2120 | |
2121 | /// decls_begin/decls_end - Iterate over the declarations stored in |
2122 | /// this context. |
2123 | decl_range decls() const { return decl_range(decls_begin(), decls_end()); } |
2124 | decl_iterator decls_begin() const; |
2125 | decl_iterator decls_end() const { return decl_iterator(); } |
2126 | bool decls_empty() const; |
2127 | |
2128 | /// noload_decls_begin/end - Iterate over the declarations stored in this |
2129 | /// context that are currently loaded; don't attempt to retrieve anything |
2130 | /// from an external source. |
2131 | decl_range noload_decls() const { |
2132 | return decl_range(noload_decls_begin(), noload_decls_end()); |
2133 | } |
2134 | decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); } |
2135 | decl_iterator noload_decls_end() const { return decl_iterator(); } |
2136 | |
2137 | /// specific_decl_iterator - Iterates over a subrange of |
2138 | /// declarations stored in a DeclContext, providing only those that |
2139 | /// are of type SpecificDecl (or a class derived from it). This |
2140 | /// iterator is used, for example, to provide iteration over just |
2141 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
2142 | template<typename SpecificDecl> |
2143 | class specific_decl_iterator { |
2144 | /// Current - The current, underlying declaration iterator, which |
2145 | /// will either be NULL or will point to a declaration of |
2146 | /// type SpecificDecl. |
2147 | DeclContext::decl_iterator Current; |
2148 | |
2149 | /// SkipToNextDecl - Advances the current position up to the next |
2150 | /// declaration of type SpecificDecl that also meets the criteria |
2151 | /// required by Acceptable. |
2152 | void SkipToNextDecl() { |
2153 | while (*Current && !isa<SpecificDecl>(*Current)) |
2154 | ++Current; |
2155 | } |
2156 | |
2157 | public: |
2158 | using value_type = SpecificDecl *; |
2159 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
2160 | // if we ever have a need for them. |
2161 | using reference = void; |
2162 | using pointer = void; |
2163 | using difference_type = |
2164 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
2165 | using iterator_category = std::forward_iterator_tag; |
2166 | |
2167 | specific_decl_iterator() = default; |
2168 | |
2169 | /// specific_decl_iterator - Construct a new iterator over a |
2170 | /// subset of the declarations the range [C, |
2171 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
2172 | /// member function of SpecificDecl that should return true for |
2173 | /// all of the SpecificDecl instances that will be in the subset |
2174 | /// of iterators. For example, if you want Objective-C instance |
2175 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
2176 | /// &ObjCMethodDecl::isInstanceMethod. |
2177 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
2178 | SkipToNextDecl(); |
2179 | } |
2180 | |
2181 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
2182 | |
2183 | // This doesn't meet the iterator requirements, but it's convenient |
2184 | value_type operator->() const { return **this; } |
2185 | |
2186 | specific_decl_iterator& operator++() { |
2187 | ++Current; |
2188 | SkipToNextDecl(); |
2189 | return *this; |
2190 | } |
2191 | |
2192 | specific_decl_iterator operator++(int) { |
2193 | specific_decl_iterator tmp(*this); |
2194 | ++(*this); |
2195 | return tmp; |
2196 | } |
2197 | |
2198 | friend bool operator==(const specific_decl_iterator& x, |
2199 | const specific_decl_iterator& y) { |
2200 | return x.Current == y.Current; |
2201 | } |
2202 | |
2203 | friend bool operator!=(const specific_decl_iterator& x, |
2204 | const specific_decl_iterator& y) { |
2205 | return x.Current != y.Current; |
2206 | } |
2207 | }; |
2208 | |
2209 | /// Iterates over a filtered subrange of declarations stored |
2210 | /// in a DeclContext. |
2211 | /// |
2212 | /// This iterator visits only those declarations that are of type |
2213 | /// SpecificDecl (or a class derived from it) and that meet some |
2214 | /// additional run-time criteria. This iterator is used, for |
2215 | /// example, to provide access to the instance methods within an |
2216 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
2217 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
2218 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
2219 | class filtered_decl_iterator { |
2220 | /// Current - The current, underlying declaration iterator, which |
2221 | /// will either be NULL or will point to a declaration of |
2222 | /// type SpecificDecl. |
2223 | DeclContext::decl_iterator Current; |
2224 | |
2225 | /// SkipToNextDecl - Advances the current position up to the next |
2226 | /// declaration of type SpecificDecl that also meets the criteria |
2227 | /// required by Acceptable. |
2228 | void SkipToNextDecl() { |
2229 | while (*Current && |
2230 | (!isa<SpecificDecl>(*Current) || |
2231 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
2232 | ++Current; |
2233 | } |
2234 | |
2235 | public: |
2236 | using value_type = SpecificDecl *; |
2237 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
2238 | // if we ever have a need for them. |
2239 | using reference = void; |
2240 | using pointer = void; |
2241 | using difference_type = |
2242 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
2243 | using iterator_category = std::forward_iterator_tag; |
2244 | |
2245 | filtered_decl_iterator() = default; |
2246 | |
2247 | /// filtered_decl_iterator - Construct a new iterator over a |
2248 | /// subset of the declarations the range [C, |
2249 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
2250 | /// member function of SpecificDecl that should return true for |
2251 | /// all of the SpecificDecl instances that will be in the subset |
2252 | /// of iterators. For example, if you want Objective-C instance |
2253 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
2254 | /// &ObjCMethodDecl::isInstanceMethod. |
2255 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
2256 | SkipToNextDecl(); |
2257 | } |
2258 | |
2259 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
2260 | value_type operator->() const { return cast<SpecificDecl>(*Current); } |
2261 | |
2262 | filtered_decl_iterator& operator++() { |
2263 | ++Current; |
2264 | SkipToNextDecl(); |
2265 | return *this; |
2266 | } |
2267 | |
2268 | filtered_decl_iterator operator++(int) { |
2269 | filtered_decl_iterator tmp(*this); |
2270 | ++(*this); |
2271 | return tmp; |
2272 | } |
2273 | |
2274 | friend bool operator==(const filtered_decl_iterator& x, |
2275 | const filtered_decl_iterator& y) { |
2276 | return x.Current == y.Current; |
2277 | } |
2278 | |
2279 | friend bool operator!=(const filtered_decl_iterator& x, |
2280 | const filtered_decl_iterator& y) { |
2281 | return x.Current != y.Current; |
2282 | } |
2283 | }; |
2284 | |
2285 | /// Add the declaration D into this context. |
2286 | /// |
2287 | /// This routine should be invoked when the declaration D has first |
2288 | /// been declared, to place D into the context where it was |
2289 | /// (lexically) defined. Every declaration must be added to one |
2290 | /// (and only one!) context, where it can be visited via |
2291 | /// [decls_begin(), decls_end()). Once a declaration has been added |
2292 | /// to its lexical context, the corresponding DeclContext owns the |
2293 | /// declaration. |
2294 | /// |
2295 | /// If D is also a NamedDecl, it will be made visible within its |
2296 | /// semantic context via makeDeclVisibleInContext. |
2297 | void addDecl(Decl *D); |
2298 | |
2299 | /// Add the declaration D into this context, but suppress |
2300 | /// searches for external declarations with the same name. |
2301 | /// |
2302 | /// Although analogous in function to addDecl, this removes an |
2303 | /// important check. This is only useful if the Decl is being |
2304 | /// added in response to an external search; in all other cases, |
2305 | /// addDecl() is the right function to use. |
2306 | /// See the ASTImporter for use cases. |
2307 | void addDeclInternal(Decl *D); |
2308 | |
2309 | /// Add the declaration D to this context without modifying |
2310 | /// any lookup tables. |
2311 | /// |
2312 | /// This is useful for some operations in dependent contexts where |
2313 | /// the semantic context might not be dependent; this basically |
2314 | /// only happens with friends. |
2315 | void addHiddenDecl(Decl *D); |
2316 | |
2317 | /// Removes a declaration from this context. |
2318 | void removeDecl(Decl *D); |
2319 | |
2320 | /// Checks whether a declaration is in this context. |
2321 | bool containsDecl(Decl *D) const; |
2322 | |
2323 | /// Checks whether a declaration is in this context. |
2324 | /// This also loads the Decls from the external source before the check. |
2325 | bool containsDeclAndLoad(Decl *D) const; |
2326 | |
2327 | using lookup_result = DeclContextLookupResult; |
2328 | using lookup_iterator = lookup_result::iterator; |
2329 | |
2330 | /// lookup - Find the declarations (if any) with the given Name in |
2331 | /// this context. Returns a range of iterators that contains all of |
2332 | /// the declarations with this name, with object, function, member, |
2333 | /// and enumerator names preceding any tag name. Note that this |
2334 | /// routine will not look into parent contexts. |
2335 | lookup_result lookup(DeclarationName Name) const; |
2336 | |
2337 | /// Find the declarations with the given name that are visible |
2338 | /// within this context; don't attempt to retrieve anything from an |
2339 | /// external source. |
2340 | lookup_result noload_lookup(DeclarationName Name); |
2341 | |
2342 | /// A simplistic name lookup mechanism that performs name lookup |
2343 | /// into this declaration context without consulting the external source. |
2344 | /// |
2345 | /// This function should almost never be used, because it subverts the |
2346 | /// usual relationship between a DeclContext and the external source. |
2347 | /// See the ASTImporter for the (few, but important) use cases. |
2348 | /// |
2349 | /// FIXME: This is very inefficient; replace uses of it with uses of |
2350 | /// noload_lookup. |
2351 | void localUncachedLookup(DeclarationName Name, |
2352 | SmallVectorImpl<NamedDecl *> &Results); |
2353 | |
2354 | /// Makes a declaration visible within this context. |
2355 | /// |
2356 | /// This routine makes the declaration D visible to name lookup |
2357 | /// within this context and, if this is a transparent context, |
2358 | /// within its parent contexts up to the first enclosing |
2359 | /// non-transparent context. Making a declaration visible within a |
2360 | /// context does not transfer ownership of a declaration, and a |
2361 | /// declaration can be visible in many contexts that aren't its |
2362 | /// lexical context. |
2363 | /// |
2364 | /// If D is a redeclaration of an existing declaration that is |
2365 | /// visible from this context, as determined by |
2366 | /// NamedDecl::declarationReplaces, the previous declaration will be |
2367 | /// replaced with D. |
2368 | void makeDeclVisibleInContext(NamedDecl *D); |
2369 | |
2370 | /// all_lookups_iterator - An iterator that provides a view over the results |
2371 | /// of looking up every possible name. |
2372 | class all_lookups_iterator; |
2373 | |
2374 | using lookups_range = llvm::iterator_range<all_lookups_iterator>; |
2375 | |
2376 | lookups_range lookups() const; |
2377 | // Like lookups(), but avoids loading external declarations. |
2378 | // If PreserveInternalState, avoids building lookup data structures too. |
2379 | lookups_range noload_lookups(bool PreserveInternalState) const; |
2380 | |
2381 | /// Iterators over all possible lookups within this context. |
2382 | all_lookups_iterator lookups_begin() const; |
2383 | all_lookups_iterator lookups_end() const; |
2384 | |
2385 | /// Iterators over all possible lookups within this context that are |
2386 | /// currently loaded; don't attempt to retrieve anything from an external |
2387 | /// source. |
2388 | all_lookups_iterator noload_lookups_begin() const; |
2389 | all_lookups_iterator noload_lookups_end() const; |
2390 | |
2391 | struct udir_iterator; |
2392 | |
2393 | using udir_iterator_base = |
2394 | llvm::iterator_adaptor_base<udir_iterator, lookup_iterator, |
2395 | typename lookup_iterator::iterator_category, |
2396 | UsingDirectiveDecl *>; |
2397 | |
2398 | struct udir_iterator : udir_iterator_base { |
2399 | udir_iterator(lookup_iterator I) : udir_iterator_base(I) {} |
2400 | |
2401 | UsingDirectiveDecl *operator*() const; |
2402 | }; |
2403 | |
2404 | using udir_range = llvm::iterator_range<udir_iterator>; |
2405 | |
2406 | udir_range using_directives() const; |
2407 | |
2408 | // These are all defined in DependentDiagnostic.h. |
2409 | class ddiag_iterator; |
2410 | |
2411 | using ddiag_range = llvm::iterator_range<DeclContext::ddiag_iterator>; |
2412 | |
2413 | inline ddiag_range ddiags() const; |
2414 | |
2415 | // Low-level accessors |
2416 | |
2417 | /// Mark that there are external lexical declarations that we need |
2418 | /// to include in our lookup table (and that are not available as external |
2419 | /// visible lookups). These extra lookup results will be found by walking |
2420 | /// the lexical declarations of this context. This should be used only if |
2421 | /// setHasExternalLexicalStorage() has been called on any decl context for |
2422 | /// which this is the primary context. |
2423 | void setMustBuildLookupTable() { |
2424 | assert(this == getPrimaryContext() &&(static_cast <bool> (this == getPrimaryContext() && "should only be called on primary context") ? void (0) : __assert_fail ("this == getPrimaryContext() && \"should only be called on primary context\"" , "clang/include/clang/AST/DeclBase.h", 2425, __extension__ __PRETTY_FUNCTION__ )) |
2425 | "should only be called on primary context")(static_cast <bool> (this == getPrimaryContext() && "should only be called on primary context") ? void (0) : __assert_fail ("this == getPrimaryContext() && \"should only be called on primary context\"" , "clang/include/clang/AST/DeclBase.h", 2425, __extension__ __PRETTY_FUNCTION__ )); |
2426 | DeclContextBits.HasLazyExternalLexicalLookups = true; |
2427 | } |
2428 | |
2429 | /// Retrieve the internal representation of the lookup structure. |
2430 | /// This may omit some names if we are lazily building the structure. |
2431 | StoredDeclsMap *getLookupPtr() const { return LookupPtr; } |
2432 | |
2433 | /// Ensure the lookup structure is fully-built and return it. |
2434 | StoredDeclsMap *buildLookup(); |
2435 | |
2436 | /// Whether this DeclContext has external storage containing |
2437 | /// additional declarations that are lexically in this context. |
2438 | bool hasExternalLexicalStorage() const { |
2439 | return DeclContextBits.ExternalLexicalStorage; |
2440 | } |
2441 | |
2442 | /// State whether this DeclContext has external storage for |
2443 | /// declarations lexically in this context. |
2444 | void setHasExternalLexicalStorage(bool ES = true) const { |
2445 | DeclContextBits.ExternalLexicalStorage = ES; |
2446 | } |
2447 | |
2448 | /// Whether this DeclContext has external storage containing |
2449 | /// additional declarations that are visible in this context. |
2450 | bool hasExternalVisibleStorage() const { |
2451 | return DeclContextBits.ExternalVisibleStorage; |
2452 | } |
2453 | |
2454 | /// State whether this DeclContext has external storage for |
2455 | /// declarations visible in this context. |
2456 | void setHasExternalVisibleStorage(bool ES = true) const { |
2457 | DeclContextBits.ExternalVisibleStorage = ES; |
2458 | if (ES && LookupPtr) |
2459 | DeclContextBits.NeedToReconcileExternalVisibleStorage = true; |
2460 | } |
2461 | |
2462 | /// Determine whether the given declaration is stored in the list of |
2463 | /// declarations lexically within this context. |
2464 | bool isDeclInLexicalTraversal(const Decl *D) const { |
2465 | return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl || |
2466 | D == LastDecl); |
2467 | } |
2468 | |
2469 | bool setUseQualifiedLookup(bool use = true) const { |
2470 | bool old_value = DeclContextBits.UseQualifiedLookup; |
2471 | DeclContextBits.UseQualifiedLookup = use; |
2472 | return old_value; |
2473 | } |
2474 | |
2475 | bool shouldUseQualifiedLookup() const { |
2476 | return DeclContextBits.UseQualifiedLookup; |
2477 | } |
2478 | |
2479 | static bool classof(const Decl *D); |
2480 | static bool classof(const DeclContext *D) { return true; } |
2481 | |
2482 | void dumpDeclContext() const; |
2483 | void dumpLookups() const; |
2484 | void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false, |
2485 | bool Deserialize = false) const; |
2486 | |
2487 | private: |
2488 | /// Whether this declaration context has had externally visible |
2489 | /// storage added since the last lookup. In this case, \c LookupPtr's |
2490 | /// invariant may not hold and needs to be fixed before we perform |
2491 | /// another lookup. |
2492 | bool hasNeedToReconcileExternalVisibleStorage() const { |
2493 | return DeclContextBits.NeedToReconcileExternalVisibleStorage; |
2494 | } |
2495 | |
2496 | /// State that this declaration context has had externally visible |
2497 | /// storage added since the last lookup. In this case, \c LookupPtr's |
2498 | /// invariant may not hold and needs to be fixed before we perform |
2499 | /// another lookup. |
2500 | void setNeedToReconcileExternalVisibleStorage(bool Need = true) const { |
2501 | DeclContextBits.NeedToReconcileExternalVisibleStorage = Need; |
2502 | } |
2503 | |
2504 | /// If \c true, this context may have local lexical declarations |
2505 | /// that are missing from the lookup table. |
2506 | bool hasLazyLocalLexicalLookups() const { |
2507 | return DeclContextBits.HasLazyLocalLexicalLookups; |
2508 | } |
2509 | |
2510 | /// If \c true, this context may have local lexical declarations |
2511 | /// that are missing from the lookup table. |
2512 | void setHasLazyLocalLexicalLookups(bool HasLLLL = true) const { |
2513 | DeclContextBits.HasLazyLocalLexicalLookups = HasLLLL; |
2514 | } |
2515 | |
2516 | /// If \c true, the external source may have lexical declarations |
2517 | /// that are missing from the lookup table. |
2518 | bool hasLazyExternalLexicalLookups() const { |
2519 | return DeclContextBits.HasLazyExternalLexicalLookups; |
2520 | } |
2521 | |
2522 | /// If \c true, the external source may have lexical declarations |
2523 | /// that are missing from the lookup table. |
2524 | void setHasLazyExternalLexicalLookups(bool HasLELL = true) const { |
2525 | DeclContextBits.HasLazyExternalLexicalLookups = HasLELL; |
2526 | } |
2527 | |
2528 | void reconcileExternalVisibleStorage() const; |
2529 | bool LoadLexicalDeclsFromExternalStorage() const; |
2530 | |
2531 | /// Makes a declaration visible within this context, but |
2532 | /// suppresses searches for external declarations with the same |
2533 | /// name. |
2534 | /// |
2535 | /// Analogous to makeDeclVisibleInContext, but for the exclusive |
2536 | /// use of addDeclInternal(). |
2537 | void makeDeclVisibleInContextInternal(NamedDecl *D); |
2538 | |
2539 | StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; |
2540 | |
2541 | void loadLazyLocalLexicalLookups(); |
2542 | void buildLookupImpl(DeclContext *DCtx, bool Internal); |
2543 | void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
2544 | bool Rediscoverable); |
2545 | void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); |
2546 | }; |
2547 | |
2548 | inline bool Decl::isTemplateParameter() const { |
2549 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || |
2550 | getKind() == TemplateTemplateParm; |
2551 | } |
2552 | |
2553 | // Specialization selected when ToTy is not a known subclass of DeclContext. |
2554 | template <class ToTy, |
2555 | bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value> |
2556 | struct cast_convert_decl_context { |
2557 | static const ToTy *doit(const DeclContext *Val) { |
2558 | return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); |
2559 | } |
2560 | |
2561 | static ToTy *doit(DeclContext *Val) { |
2562 | return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); |
2563 | } |
2564 | }; |
2565 | |
2566 | // Specialization selected when ToTy is a known subclass of DeclContext. |
2567 | template <class ToTy> |
2568 | struct cast_convert_decl_context<ToTy, true> { |
2569 | static const ToTy *doit(const DeclContext *Val) { |
2570 | return static_cast<const ToTy*>(Val); |
2571 | } |
2572 | |
2573 | static ToTy *doit(DeclContext *Val) { |
2574 | return static_cast<ToTy*>(Val); |
2575 | } |
2576 | }; |
2577 | |
2578 | } // namespace clang |
2579 | |
2580 | namespace llvm { |
2581 | |
2582 | /// isa<T>(DeclContext*) |
2583 | template <typename To> |
2584 | struct isa_impl<To, ::clang::DeclContext> { |
2585 | static bool doit(const ::clang::DeclContext &Val) { |
2586 | return To::classofKind(Val.getDeclKind()); |
2587 | } |
2588 | }; |
2589 | |
2590 | /// cast<T>(DeclContext*) |
2591 | template<class ToTy> |
2592 | struct cast_convert_val<ToTy, |
2593 | const ::clang::DeclContext,const ::clang::DeclContext> { |
2594 | static const ToTy &doit(const ::clang::DeclContext &Val) { |
2595 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
2596 | } |
2597 | }; |
2598 | |
2599 | template<class ToTy> |
2600 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { |
2601 | static ToTy &doit(::clang::DeclContext &Val) { |
2602 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
2603 | } |
2604 | }; |
2605 | |
2606 | template<class ToTy> |
2607 | struct cast_convert_val<ToTy, |
2608 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
2609 | static const ToTy *doit(const ::clang::DeclContext *Val) { |
2610 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
2611 | } |
2612 | }; |
2613 | |
2614 | template<class ToTy> |
2615 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { |
2616 | static ToTy *doit(::clang::DeclContext *Val) { |
2617 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
2618 | } |
2619 | }; |
2620 | |
2621 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
2622 | template<class FromTy> |
2623 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
2624 | static ::clang::DeclContext &doit(const FromTy &Val) { |
2625 | return *FromTy::castToDeclContext(&Val); |
2626 | } |
2627 | }; |
2628 | |
2629 | template<class FromTy> |
2630 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
2631 | static ::clang::DeclContext *doit(const FromTy *Val) { |
2632 | return FromTy::castToDeclContext(Val); |
2633 | } |
2634 | }; |
2635 | |
2636 | template<class FromTy> |
2637 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
2638 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
2639 | return *FromTy::castToDeclContext(&Val); |
2640 | } |
2641 | }; |
2642 | |
2643 | template<class FromTy> |
2644 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
2645 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
2646 | return FromTy::castToDeclContext(Val); |
2647 | } |
2648 | }; |
2649 | |
2650 | } // namespace llvm |
2651 | |
2652 | #endif // LLVM_CLANG_AST_DECLBASE_H |
1 | //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===// |
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 | /// \file |
10 | /// This file defines the PointerUnion class, which is a discriminated union of |
11 | /// pointer types. |
12 | /// |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_ADT_POINTERUNION_H |
16 | #define LLVM_ADT_POINTERUNION_H |
17 | |
18 | #include "llvm/ADT/DenseMapInfo.h" |
19 | #include "llvm/ADT/PointerIntPair.h" |
20 | #include "llvm/ADT/STLExtras.h" |
21 | #include "llvm/Support/PointerLikeTypeTraits.h" |
22 | #include <algorithm> |
23 | #include <cassert> |
24 | #include <cstddef> |
25 | #include <cstdint> |
26 | |
27 | namespace llvm { |
28 | |
29 | namespace pointer_union_detail { |
30 | /// Determine the number of bits required to store integers with values < n. |
31 | /// This is ceil(log2(n)). |
32 | constexpr int bitsRequired(unsigned n) { |
33 | return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0; |
34 | } |
35 | |
36 | template <typename... Ts> constexpr int lowBitsAvailable() { |
37 | return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...}); |
38 | } |
39 | |
40 | /// Find the first type in a list of types. |
41 | template <typename T, typename...> struct GetFirstType { |
42 | using type = T; |
43 | }; |
44 | |
45 | /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion |
46 | /// for the template arguments. |
47 | template <typename ...PTs> class PointerUnionUIntTraits { |
48 | public: |
49 | static inline void *getAsVoidPointer(void *P) { return P; } |
50 | static inline void *getFromVoidPointer(void *P) { return P; } |
51 | static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>(); |
52 | }; |
53 | |
54 | template <typename Derived, typename ValTy, int I, typename ...Types> |
55 | class PointerUnionMembers; |
56 | |
57 | template <typename Derived, typename ValTy, int I> |
58 | class PointerUnionMembers<Derived, ValTy, I> { |
59 | protected: |
60 | ValTy Val; |
61 | PointerUnionMembers() = default; |
62 | PointerUnionMembers(ValTy Val) : Val(Val) {} |
63 | |
64 | friend struct PointerLikeTypeTraits<Derived>; |
65 | }; |
66 | |
67 | template <typename Derived, typename ValTy, int I, typename Type, |
68 | typename ...Types> |
69 | class PointerUnionMembers<Derived, ValTy, I, Type, Types...> |
70 | : public PointerUnionMembers<Derived, ValTy, I + 1, Types...> { |
71 | using Base = PointerUnionMembers<Derived, ValTy, I + 1, Types...>; |
72 | public: |
73 | using Base::Base; |
74 | PointerUnionMembers() = default; |
75 | PointerUnionMembers(Type V) |
76 | : Base(ValTy(const_cast<void *>( |
77 | PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
78 | I)) {} |
79 | |
80 | using Base::operator=; |
81 | Derived &operator=(Type V) { |
82 | this->Val = ValTy( |
83 | const_cast<void *>(PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
84 | I); |
85 | return static_cast<Derived &>(*this); |
86 | }; |
87 | }; |
88 | } |
89 | |
90 | /// A discriminated union of two or more pointer types, with the discriminator |
91 | /// in the low bit of the pointer. |
92 | /// |
93 | /// This implementation is extremely efficient in space due to leveraging the |
94 | /// low bits of the pointer, while exposing a natural and type-safe API. |
95 | /// |
96 | /// Common use patterns would be something like this: |
97 | /// PointerUnion<int*, float*> P; |
98 | /// P = (int*)0; |
99 | /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0" |
100 | /// X = P.get<int*>(); // ok. |
101 | /// Y = P.get<float*>(); // runtime assertion failure. |
102 | /// Z = P.get<double*>(); // compile time failure. |
103 | /// P = (float*)0; |
104 | /// Y = P.get<float*>(); // ok. |
105 | /// X = P.get<int*>(); // runtime assertion failure. |
106 | /// PointerUnion<int*, int*> Q; // compile time failure. |
107 | template <typename... PTs> |
108 | class PointerUnion |
109 | : public pointer_union_detail::PointerUnionMembers< |
110 | PointerUnion<PTs...>, |
111 | PointerIntPair< |
112 | void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int, |
113 | pointer_union_detail::PointerUnionUIntTraits<PTs...>>, |
114 | 0, PTs...> { |
115 | static_assert(TypesAreDistinct<PTs...>::value, |
116 | "PointerUnion alternative types cannot be repeated"); |
117 | // The first type is special because we want to directly cast a pointer to a |
118 | // default-initialized union to a pointer to the first type. But we don't |
119 | // want PointerUnion to be a 'template <typename First, typename ...Rest>' |
120 | // because it's much more convenient to have a name for the whole pack. So |
121 | // split off the first type here. |
122 | using First = TypeAtIndex<0, PTs...>; |
123 | using Base = typename PointerUnion::PointerUnionMembers; |
124 | |
125 | public: |
126 | PointerUnion() = default; |
127 | |
128 | PointerUnion(std::nullptr_t) : PointerUnion() {} |
129 | using Base::Base; |
130 | |
131 | /// Test if the pointer held in the union is null, regardless of |
132 | /// which type it is. |
133 | bool isNull() const { return !this->Val.getPointer(); } |
134 | |
135 | explicit operator bool() const { return !isNull(); } |
136 | |
137 | /// Test if the Union currently holds the type matching T. |
138 | template <typename T> bool is() const { |
139 | return this->Val.getInt() == FirstIndexOfType<T, PTs...>::value; |
140 | } |
141 | |
142 | /// Returns the value of the specified pointer type. |
143 | /// |
144 | /// If the specified pointer type is incorrect, assert. |
145 | template <typename T> T get() const { |
146 | assert(is<T>() && "Invalid accessor called")(static_cast <bool> (is<T>() && "Invalid accessor called" ) ? void (0) : __assert_fail ("is<T>() && \"Invalid accessor called\"" , "llvm/include/llvm/ADT/PointerUnion.h", 146, __extension__ __PRETTY_FUNCTION__ )); |
147 | return PointerLikeTypeTraits<T>::getFromVoidPointer(this->Val.getPointer()); |
148 | } |
149 | |
150 | /// Returns the current pointer if it is of the specified pointer type, |
151 | /// otherwise returns null. |
152 | template <typename T> T dyn_cast() const { |
153 | if (is<T>()) |
154 | return get<T>(); |
155 | return T(); |
156 | } |
157 | |
158 | /// If the union is set to the first pointer type get an address pointing to |
159 | /// it. |
160 | First const *getAddrOfPtr1() const { |
161 | return const_cast<PointerUnion *>(this)->getAddrOfPtr1(); |
162 | } |
163 | |
164 | /// If the union is set to the first pointer type get an address pointing to |
165 | /// it. |
166 | First *getAddrOfPtr1() { |
167 | assert(is<First>() && "Val is not the first pointer")(static_cast <bool> (is<First>() && "Val is not the first pointer" ) ? void (0) : __assert_fail ("is<First>() && \"Val is not the first pointer\"" , "llvm/include/llvm/ADT/PointerUnion.h", 167, __extension__ __PRETTY_FUNCTION__ )); |
168 | assert((static_cast <bool> (PointerLikeTypeTraits<First> ::getAsVoidPointer(get<First>()) == this->Val.getPointer () && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? void (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "llvm/include/llvm/ADT/PointerUnion.h", 171, __extension__ __PRETTY_FUNCTION__ )) |
169 | PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==(static_cast <bool> (PointerLikeTypeTraits<First> ::getAsVoidPointer(get<First>()) == this->Val.getPointer () && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? void (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "llvm/include/llvm/ADT/PointerUnion.h", 171, __extension__ __PRETTY_FUNCTION__ )) |
170 | this->Val.getPointer() &&(static_cast <bool> (PointerLikeTypeTraits<First> ::getAsVoidPointer(get<First>()) == this->Val.getPointer () && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? void (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "llvm/include/llvm/ADT/PointerUnion.h", 171, __extension__ __PRETTY_FUNCTION__ )) |
171 | "Can't get the address because PointerLikeTypeTraits changes the ptr")(static_cast <bool> (PointerLikeTypeTraits<First> ::getAsVoidPointer(get<First>()) == this->Val.getPointer () && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? void (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "llvm/include/llvm/ADT/PointerUnion.h", 171, __extension__ __PRETTY_FUNCTION__ )); |
172 | return const_cast<First *>( |
173 | reinterpret_cast<const First *>(this->Val.getAddrOfPointer())); |
174 | } |
175 | |
176 | /// Assignment from nullptr which just clears the union. |
177 | const PointerUnion &operator=(std::nullptr_t) { |
178 | this->Val.initWithPointer(nullptr); |
179 | return *this; |
180 | } |
181 | |
182 | /// Assignment from elements of the union. |
183 | using Base::operator=; |
184 | |
185 | void *getOpaqueValue() const { return this->Val.getOpaqueValue(); } |
186 | static inline PointerUnion getFromOpaqueValue(void *VP) { |
187 | PointerUnion V; |
188 | V.Val = decltype(V.Val)::getFromOpaqueValue(VP); |
189 | return V; |
190 | } |
191 | }; |
192 | |
193 | template <typename ...PTs> |
194 | bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
195 | return lhs.getOpaqueValue() == rhs.getOpaqueValue(); |
196 | } |
197 | |
198 | template <typename ...PTs> |
199 | bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
200 | return lhs.getOpaqueValue() != rhs.getOpaqueValue(); |
201 | } |
202 | |
203 | template <typename ...PTs> |
204 | bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
205 | return lhs.getOpaqueValue() < rhs.getOpaqueValue(); |
206 | } |
207 | |
208 | // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has |
209 | // # low bits available = min(PT1bits,PT2bits)-1. |
210 | template <typename ...PTs> |
211 | struct PointerLikeTypeTraits<PointerUnion<PTs...>> { |
212 | static inline void *getAsVoidPointer(const PointerUnion<PTs...> &P) { |
213 | return P.getOpaqueValue(); |
214 | } |
215 | |
216 | static inline PointerUnion<PTs...> getFromVoidPointer(void *P) { |
217 | return PointerUnion<PTs...>::getFromOpaqueValue(P); |
218 | } |
219 | |
220 | // The number of bits available are the min of the pointer types minus the |
221 | // bits needed for the discriminator. |
222 | static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<decltype( |
223 | PointerUnion<PTs...>::Val)>::NumLowBitsAvailable; |
224 | }; |
225 | |
226 | // Teach DenseMap how to use PointerUnions as keys. |
227 | template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> { |
228 | using Union = PointerUnion<PTs...>; |
229 | using FirstInfo = |
230 | DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>; |
231 | |
232 | static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); } |
233 | |
234 | static inline Union getTombstoneKey() { |
235 | return Union(FirstInfo::getTombstoneKey()); |
236 | } |
237 | |
238 | static unsigned getHashValue(const Union &UnionVal) { |
239 | intptr_t key = (intptr_t)UnionVal.getOpaqueValue(); |
240 | return DenseMapInfo<intptr_t>::getHashValue(key); |
241 | } |
242 | |
243 | static bool isEqual(const Union &LHS, const Union &RHS) { |
244 | return LHS == RHS; |
245 | } |
246 | }; |
247 | |
248 | } // end namespace llvm |
249 | |
250 | #endif // LLVM_ADT_POINTERUNION_H |