Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | //===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===// |
---|---|
2 | // |
3 | // The LLVM Compiler Infrastructure |
4 | // |
5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | // |
10 | // This file defines the Decl and DeclContext interfaces. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_CLANG_AST_DECLBASE_H |
15 | #define LLVM_CLANG_AST_DECLBASE_H |
16 | |
17 | #include "clang/AST/AttrIterator.h" |
18 | #include "clang/AST/DeclarationName.h" |
19 | #include "clang/Basic/LLVM.h" |
20 | #include "clang/Basic/SourceLocation.h" |
21 | #include "clang/Basic/Specifiers.h" |
22 | #include "llvm/ADT/ArrayRef.h" |
23 | #include "llvm/ADT/PointerIntPair.h" |
24 | #include "llvm/ADT/PointerUnion.h" |
25 | #include "llvm/ADT/iterator.h" |
26 | #include "llvm/ADT/iterator_range.h" |
27 | #include "llvm/Support/Casting.h" |
28 | #include "llvm/Support/Compiler.h" |
29 | #include "llvm/Support/PrettyStackTrace.h" |
30 | #include "llvm/Support/VersionTuple.h" |
31 | #include <algorithm> |
32 | #include <cassert> |
33 | #include <cstddef> |
34 | #include <iterator> |
35 | #include <string> |
36 | #include <type_traits> |
37 | #include <utility> |
38 | |
39 | namespace clang { |
40 | |
41 | class ASTContext; |
42 | class ASTMutationListener; |
43 | class Attr; |
44 | class DeclContext; |
45 | class ExternalSourceSymbolAttr; |
46 | class FunctionDecl; |
47 | class FunctionType; |
48 | class IdentifierInfo; |
49 | enum Linkage : unsigned char; |
50 | class LinkageSpecDecl; |
51 | class Module; |
52 | class NamedDecl; |
53 | class ObjCCategoryDecl; |
54 | class ObjCCategoryImplDecl; |
55 | class ObjCContainerDecl; |
56 | class ObjCImplDecl; |
57 | class ObjCImplementationDecl; |
58 | class ObjCInterfaceDecl; |
59 | class ObjCMethodDecl; |
60 | class ObjCProtocolDecl; |
61 | struct PrintingPolicy; |
62 | class RecordDecl; |
63 | class SourceManager; |
64 | class Stmt; |
65 | class StoredDeclsMap; |
66 | class TemplateDecl; |
67 | class TranslationUnitDecl; |
68 | class UsingDirectiveDecl; |
69 | |
70 | /// Captures the result of checking the availability of a |
71 | /// declaration. |
72 | enum AvailabilityResult { |
73 | AR_Available = 0, |
74 | AR_NotYetIntroduced, |
75 | AR_Deprecated, |
76 | AR_Unavailable |
77 | }; |
78 | |
79 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
80 | /// typedef, function, struct, etc. |
81 | /// |
82 | /// Note: There are objects tacked on before the *beginning* of Decl |
83 | /// (and its subclasses) in its Decl::operator new(). Proper alignment |
84 | /// of all subclasses (not requiring more than the alignment of Decl) is |
85 | /// asserted in DeclBase.cpp. |
86 | class alignas(8) Decl { |
87 | public: |
88 | /// Lists the kind of concrete classes of Decl. |
89 | enum Kind { |
90 | #define DECL(DERIVED, BASE) DERIVED, |
91 | #define ABSTRACT_DECL(DECL) |
92 | #define DECL_RANGE(BASE, START, END) \ |
93 | first##BASE = START, last##BASE = END, |
94 | #define LAST_DECL_RANGE(BASE, START, END) \ |
95 | first##BASE = START, last##BASE = END |
96 | #include "clang/AST/DeclNodes.inc" |
97 | }; |
98 | |
99 | /// A placeholder type used to construct an empty shell of a |
100 | /// decl-derived type that will be filled in later (e.g., by some |
101 | /// deserialization method). |
102 | struct EmptyShell {}; |
103 | |
104 | /// IdentifierNamespace - The different namespaces in which |
105 | /// declarations may appear. According to C99 6.2.3, there are |
106 | /// four namespaces, labels, tags, members and ordinary |
107 | /// identifiers. C++ describes lookup completely differently: |
108 | /// certain lookups merely "ignore" certain kinds of declarations, |
109 | /// usually based on whether the declaration is of a type, etc. |
110 | /// |
111 | /// These are meant as bitmasks, so that searches in |
112 | /// C++ can look into the "tag" namespace during ordinary lookup. |
113 | /// |
114 | /// Decl currently provides 15 bits of IDNS bits. |
115 | enum IdentifierNamespace { |
116 | /// Labels, declared with 'x:' and referenced with 'goto x'. |
117 | IDNS_Label = 0x0001, |
118 | |
119 | /// Tags, declared with 'struct foo;' and referenced with |
120 | /// 'struct foo'. All tags are also types. This is what |
121 | /// elaborated-type-specifiers look for in C. |
122 | /// This also contains names that conflict with tags in the |
123 | /// same scope but that are otherwise ordinary names (non-type |
124 | /// template parameters and indirect field declarations). |
125 | IDNS_Tag = 0x0002, |
126 | |
127 | /// Types, declared with 'struct foo', typedefs, etc. |
128 | /// This is what elaborated-type-specifiers look for in C++, |
129 | /// but note that it's ill-formed to find a non-tag. |
130 | IDNS_Type = 0x0004, |
131 | |
132 | /// Members, declared with object declarations within tag |
133 | /// definitions. In C, these can only be found by "qualified" |
134 | /// lookup in member expressions. In C++, they're found by |
135 | /// normal lookup. |
136 | IDNS_Member = 0x0008, |
137 | |
138 | /// Namespaces, declared with 'namespace foo {}'. |
139 | /// Lookup for nested-name-specifiers find these. |
140 | IDNS_Namespace = 0x0010, |
141 | |
142 | /// Ordinary names. In C, everything that's not a label, tag, |
143 | /// member, or function-local extern ends up here. |
144 | IDNS_Ordinary = 0x0020, |
145 | |
146 | /// Objective C \@protocol. |
147 | IDNS_ObjCProtocol = 0x0040, |
148 | |
149 | /// This declaration is a friend function. A friend function |
150 | /// declaration is always in this namespace but may also be in |
151 | /// IDNS_Ordinary if it was previously declared. |
152 | IDNS_OrdinaryFriend = 0x0080, |
153 | |
154 | /// This declaration is a friend class. A friend class |
155 | /// declaration is always in this namespace but may also be in |
156 | /// IDNS_Tag|IDNS_Type if it was previously declared. |
157 | IDNS_TagFriend = 0x0100, |
158 | |
159 | /// This declaration is a using declaration. A using declaration |
160 | /// *introduces* a number of other declarations into the current |
161 | /// scope, and those declarations use the IDNS of their targets, |
162 | /// but the actual using declarations go in this namespace. |
163 | IDNS_Using = 0x0200, |
164 | |
165 | /// This declaration is a C++ operator declared in a non-class |
166 | /// context. All such operators are also in IDNS_Ordinary. |
167 | /// C++ lexical operator lookup looks for these. |
168 | IDNS_NonMemberOperator = 0x0400, |
169 | |
170 | /// This declaration is a function-local extern declaration of a |
171 | /// variable or function. This may also be IDNS_Ordinary if it |
172 | /// has been declared outside any function. These act mostly like |
173 | /// invisible friend declarations, but are also visible to unqualified |
174 | /// lookup within the scope of the declaring function. |
175 | IDNS_LocalExtern = 0x0800, |
176 | |
177 | /// This declaration is an OpenMP user defined reduction construction. |
178 | IDNS_OMPReduction = 0x1000 |
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 : 13; |
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 AccessDeclContextSanity() 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 | protected: |
367 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
368 | : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)), |
369 | DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false), |
370 | Implicit(false), Used(false), Referenced(false), |
371 | TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0), |
372 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
373 | CacheValidAndLinkage(0) { |
374 | if (StatisticsEnabled) add(DK); |
375 | } |
376 | |
377 | Decl(Kind DK, EmptyShell Empty) |
378 | : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false), |
379 | Used(false), Referenced(false), TopLevelDeclInObjCContainer(false), |
380 | Access(AS_none), FromASTFile(0), |
381 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
382 | CacheValidAndLinkage(0) { |
383 | if (StatisticsEnabled) add(DK); |
384 | } |
385 | |
386 | virtual ~Decl(); |
387 | |
388 | /// Update a potentially out-of-date declaration. |
389 | void updateOutOfDate(IdentifierInfo &II) const; |
390 | |
391 | Linkage getCachedLinkage() const { |
392 | return Linkage(CacheValidAndLinkage - 1); |
393 | } |
394 | |
395 | void setCachedLinkage(Linkage L) const { |
396 | CacheValidAndLinkage = L + 1; |
397 | } |
398 | |
399 | bool hasCachedLinkage() const { |
400 | return CacheValidAndLinkage; |
401 | } |
402 | |
403 | public: |
404 | /// Source range that this declaration covers. |
405 | virtual SourceRange getSourceRange() const LLVM_READONLY { |
406 | return SourceRange(getLocation(), getLocation()); |
407 | } |
408 | |
409 | SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); } |
410 | SourceLocation getBeginLoc() const LLVM_READONLY { |
411 | return getSourceRange().getBegin(); |
412 | } |
413 | |
414 | SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } |
415 | SourceLocation getEndLoc() const LLVM_READONLY { |
416 | return getSourceRange().getEnd(); |
417 | } |
418 | |
419 | SourceLocation getLocation() const { return Loc; } |
420 | void setLocation(SourceLocation L) { Loc = L; } |
421 | |
422 | Kind getKind() const { return static_cast<Kind>(DeclKind); } |
423 | const char *getDeclKindName() const; |
424 | |
425 | Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); } |
426 | const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();} |
427 | |
428 | DeclContext *getDeclContext() { |
429 | if (isInSemaDC()) |
430 | return getSemanticDC(); |
431 | return getMultipleDC()->SemanticDC; |
432 | } |
433 | const DeclContext *getDeclContext() const { |
434 | return const_cast<Decl*>(this)->getDeclContext(); |
435 | } |
436 | |
437 | /// Find the innermost non-closure ancestor of this declaration, |
438 | /// walking up through blocks, lambdas, etc. If that ancestor is |
439 | /// not a code context (!isFunctionOrMethod()), returns null. |
440 | /// |
441 | /// A declaration may be its own non-closure context. |
442 | Decl *getNonClosureContext(); |
443 | const Decl *getNonClosureContext() const { |
444 | return const_cast<Decl*>(this)->getNonClosureContext(); |
445 | } |
446 | |
447 | TranslationUnitDecl *getTranslationUnitDecl(); |
448 | const TranslationUnitDecl *getTranslationUnitDecl() const { |
449 | return const_cast<Decl*>(this)->getTranslationUnitDecl(); |
450 | } |
451 | |
452 | bool isInAnonymousNamespace() const; |
453 | |
454 | bool isInStdNamespace() const; |
455 | |
456 | ASTContext &getASTContext() const LLVM_READONLY; |
457 | |
458 | void setAccess(AccessSpecifier AS) { |
459 | Access = AS; |
460 | assert(AccessDeclContextSanity()); |
461 | } |
462 | |
463 | AccessSpecifier getAccess() const { |
464 | assert(AccessDeclContextSanity()); |
465 | return AccessSpecifier(Access); |
466 | } |
467 | |
468 | /// Retrieve the access specifier for this declaration, even though |
469 | /// it may not yet have been properly set. |
470 | AccessSpecifier getAccessUnsafe() const { |
471 | return AccessSpecifier(Access); |
472 | } |
473 | |
474 | bool hasAttrs() const { return HasAttrs; } |
475 | |
476 | void setAttrs(const AttrVec& Attrs) { |
477 | return setAttrsImpl(Attrs, getASTContext()); |
478 | } |
479 | |
480 | AttrVec &getAttrs() { |
481 | return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); |
482 | } |
483 | |
484 | const AttrVec &getAttrs() const; |
485 | void dropAttrs(); |
486 | |
487 | void addAttr(Attr *A) { |
488 | if (hasAttrs()) |
489 | getAttrs().push_back(A); |
490 | else |
491 | setAttrs(AttrVec(1, A)); |
492 | } |
493 | |
494 | using attr_iterator = AttrVec::const_iterator; |
495 | using attr_range = llvm::iterator_range<attr_iterator>; |
496 | |
497 | attr_range attrs() const { |
498 | return attr_range(attr_begin(), attr_end()); |
499 | } |
500 | |
501 | attr_iterator attr_begin() const { |
502 | return hasAttrs() ? getAttrs().begin() : nullptr; |
503 | } |
504 | attr_iterator attr_end() const { |
505 | return hasAttrs() ? getAttrs().end() : nullptr; |
506 | } |
507 | |
508 | template <typename T> |
509 | void dropAttr() { |
510 | if (!HasAttrs) return; |
511 | |
512 | AttrVec &Vec = getAttrs(); |
513 | Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end()); |
514 | |
515 | if (Vec.empty()) |
516 | HasAttrs = false; |
517 | } |
518 | |
519 | template <typename T> |
520 | llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const { |
521 | return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>()); |
522 | } |
523 | |
524 | template <typename T> |
525 | specific_attr_iterator<T> specific_attr_begin() const { |
526 | return specific_attr_iterator<T>(attr_begin()); |
527 | } |
528 | |
529 | template <typename T> |
530 | specific_attr_iterator<T> specific_attr_end() const { |
531 | return specific_attr_iterator<T>(attr_end()); |
532 | } |
533 | |
534 | template<typename T> T *getAttr() const { |
535 | return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr; |
536 | } |
537 | |
538 | template<typename T> bool hasAttr() const { |
539 | return hasAttrs() && hasSpecificAttr<T>(getAttrs()); |
540 | } |
541 | |
542 | /// getMaxAlignment - return the maximum alignment specified by attributes |
543 | /// on this decl, 0 if there are none. |
544 | unsigned getMaxAlignment() const; |
545 | |
546 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
547 | /// allows for graceful error recovery. |
548 | void setInvalidDecl(bool Invalid = true); |
549 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
550 | |
551 | /// isImplicit - Indicates whether the declaration was implicitly |
552 | /// generated by the implementation. If false, this declaration |
553 | /// was written explicitly in the source code. |
554 | bool isImplicit() const { return Implicit; } |
555 | void setImplicit(bool I = true) { Implicit = I; } |
556 | |
557 | /// Whether *any* (re-)declaration of the entity was used, meaning that |
558 | /// a definition is required. |
559 | /// |
560 | /// \param CheckUsedAttr When true, also consider the "used" attribute |
561 | /// (in addition to the "used" bit set by \c setUsed()) when determining |
562 | /// whether the function is used. |
563 | bool isUsed(bool CheckUsedAttr = true) const; |
564 | |
565 | /// Set whether the declaration is used, in the sense of odr-use. |
566 | /// |
567 | /// This should only be used immediately after creating a declaration. |
568 | /// It intentionally doesn't notify any listeners. |
569 | void setIsUsed() { getCanonicalDecl()->Used = true; } |
570 | |
571 | /// Mark the declaration used, in the sense of odr-use. |
572 | /// |
573 | /// This notifies any mutation listeners in addition to setting a bit |
574 | /// indicating the declaration is used. |
575 | void markUsed(ASTContext &C); |
576 | |
577 | /// Whether any declaration of this entity was referenced. |
578 | bool isReferenced() const; |
579 | |
580 | /// Whether this declaration was referenced. This should not be relied |
581 | /// upon for anything other than debugging. |
582 | bool isThisDeclarationReferenced() const { return Referenced; } |
583 | |
584 | void setReferenced(bool R = true) { Referenced = R; } |
585 | |
586 | /// Whether this declaration is a top-level declaration (function, |
587 | /// global variable, etc.) that is lexically inside an objc container |
588 | /// definition. |
589 | bool isTopLevelDeclInObjCContainer() const { |
590 | return TopLevelDeclInObjCContainer; |
591 | } |
592 | |
593 | void setTopLevelDeclInObjCContainer(bool V = true) { |
594 | TopLevelDeclInObjCContainer = V; |
595 | } |
596 | |
597 | /// Looks on this and related declarations for an applicable |
598 | /// external source symbol attribute. |
599 | ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const; |
600 | |
601 | /// Whether this declaration was marked as being private to the |
602 | /// module in which it was defined. |
603 | bool isModulePrivate() const { |
604 | return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate; |
605 | } |
606 | |
607 | /// Whether this declaration is exported (by virtue of being lexically |
608 | /// within an ExportDecl or by being a NamespaceDecl). |
609 | bool isExported() const; |
610 | |
611 | /// Return true if this declaration has an attribute which acts as |
612 | /// definition of the entity, such as 'alias' or 'ifunc'. |
613 | bool hasDefiningAttr() const; |
614 | |
615 | /// Return this declaration's defining attribute if it has one. |
616 | const Attr *getDefiningAttr() const; |
617 | |
618 | protected: |
619 | /// Specify that this declaration was marked as being private |
620 | /// to the module in which it was defined. |
621 | void setModulePrivate() { |
622 | // The module-private specifier has no effect on unowned declarations. |
623 | // FIXME: We should track this in some way for source fidelity. |
624 | if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned) |
625 | return; |
626 | setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate); |
627 | } |
628 | |
629 | /// Set the owning module ID. |
630 | void setOwningModuleID(unsigned ID) { |
631 | assert(isFromASTFile() && "Only works on a deserialized declaration"); |
632 | *((unsigned*)this - 2) = ID; |
633 | } |
634 | |
635 | public: |
636 | /// Determine the availability of the given declaration. |
637 | /// |
638 | /// This routine will determine the most restrictive availability of |
639 | /// the given declaration (e.g., preferring 'unavailable' to |
640 | /// 'deprecated'). |
641 | /// |
642 | /// \param Message If non-NULL and the result is not \c |
643 | /// AR_Available, will be set to a (possibly empty) message |
644 | /// describing why the declaration has not been introduced, is |
645 | /// deprecated, or is unavailable. |
646 | /// |
647 | /// \param EnclosingVersion The version to compare with. If empty, assume the |
648 | /// deployment target version. |
649 | /// |
650 | /// \param RealizedPlatform If non-NULL and the availability result is found |
651 | /// in an available attribute it will set to the platform which is written in |
652 | /// the available attribute. |
653 | AvailabilityResult |
654 | getAvailability(std::string *Message = nullptr, |
655 | VersionTuple EnclosingVersion = VersionTuple(), |
656 | StringRef *RealizedPlatform = nullptr) const; |
657 | |
658 | /// Retrieve the version of the target platform in which this |
659 | /// declaration was introduced. |
660 | /// |
661 | /// \returns An empty version tuple if this declaration has no 'introduced' |
662 | /// availability attributes, or the version tuple that's specified in the |
663 | /// attribute otherwise. |
664 | VersionTuple getVersionIntroduced() const; |
665 | |
666 | /// Determine whether this declaration is marked 'deprecated'. |
667 | /// |
668 | /// \param Message If non-NULL and the declaration is deprecated, |
669 | /// this will be set to the message describing why the declaration |
670 | /// was deprecated (which may be empty). |
671 | bool isDeprecated(std::string *Message = nullptr) const { |
672 | return getAvailability(Message) == AR_Deprecated; |
673 | } |
674 | |
675 | /// Determine whether this declaration is marked 'unavailable'. |
676 | /// |
677 | /// \param Message If non-NULL and the declaration is unavailable, |
678 | /// this will be set to the message describing why the declaration |
679 | /// was made unavailable (which may be empty). |
680 | bool isUnavailable(std::string *Message = nullptr) const { |
681 | return getAvailability(Message) == AR_Unavailable; |
682 | } |
683 | |
684 | /// Determine whether this is a weak-imported symbol. |
685 | /// |
686 | /// Weak-imported symbols are typically marked with the |
687 | /// 'weak_import' attribute, but may also be marked with an |
688 | /// 'availability' attribute where we're targing a platform prior to |
689 | /// the introduction of this feature. |
690 | bool isWeakImported() const; |
691 | |
692 | /// Determines whether this symbol can be weak-imported, |
693 | /// e.g., whether it would be well-formed to add the weak_import |
694 | /// attribute. |
695 | /// |
696 | /// \param IsDefinition Set to \c true to indicate that this |
697 | /// declaration cannot be weak-imported because it has a definition. |
698 | bool canBeWeakImported(bool &IsDefinition) const; |
699 | |
700 | /// Determine whether this declaration came from an AST file (such as |
701 | /// a precompiled header or module) rather than having been parsed. |
702 | bool isFromASTFile() const { return FromASTFile; } |
703 | |
704 | /// Retrieve the global declaration ID associated with this |
705 | /// declaration, which specifies where this Decl was loaded from. |
706 | unsigned getGlobalID() const { |
707 | if (isFromASTFile()) |
708 | return *((const unsigned*)this - 1); |
709 | return 0; |
710 | } |
711 | |
712 | /// Retrieve the global ID of the module that owns this particular |
713 | /// declaration. |
714 | unsigned getOwningModuleID() const { |
715 | if (isFromASTFile()) |
716 | return *((const unsigned*)this - 2); |
717 | return 0; |
718 | } |
719 | |
720 | private: |
721 | Module *getOwningModuleSlow() const; |
722 | |
723 | protected: |
724 | bool hasLocalOwningModuleStorage() const; |
725 | |
726 | public: |
727 | /// Get the imported owning module, if this decl is from an imported |
728 | /// (non-local) module. |
729 | Module *getImportedOwningModule() const { |
730 | if (!isFromASTFile() || !hasOwningModule()) |
731 | return nullptr; |
732 | |
733 | return getOwningModuleSlow(); |
734 | } |
735 | |
736 | /// Get the local owning module, if known. Returns nullptr if owner is |
737 | /// not yet known or declaration is not from a module. |
738 | Module *getLocalOwningModule() const { |
739 | if (isFromASTFile() || !hasOwningModule()) |
740 | return nullptr; |
741 | |
742 | assert(hasLocalOwningModuleStorage() && |
743 | "owned local decl but no local module storage"); |
744 | return reinterpret_cast<Module *const *>(this)[-1]; |
745 | } |
746 | void setLocalOwningModule(Module *M) { |
747 | assert(!isFromASTFile() && hasOwningModule() && |
748 | hasLocalOwningModuleStorage() && |
749 | "should not have a cached owning module"); |
750 | reinterpret_cast<Module **>(this)[-1] = M; |
751 | } |
752 | |
753 | /// Is this declaration owned by some module? |
754 | bool hasOwningModule() const { |
755 | return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned; |
756 | } |
757 | |
758 | /// Get the module that owns this declaration (for visibility purposes). |
759 | Module *getOwningModule() const { |
760 | return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule(); |
761 | } |
762 | |
763 | /// Get the module that owns this declaration for linkage purposes. |
764 | /// There only ever is such a module under the C++ Modules TS. |
765 | /// |
766 | /// \param IgnoreLinkage Ignore the linkage of the entity; assume that |
767 | /// all declarations in a global module fragment are unowned. |
768 | Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; |
769 | |
770 | /// Determine whether this declaration might be hidden from name |
771 | /// lookup. Note that the declaration might be visible even if this returns |
772 | /// \c false, if the owning module is visible within the query context. |
773 | // FIXME: Rename this to make it clearer what it does. |
774 | bool isHidden() const { |
775 | return (int)getModuleOwnershipKind() > (int)ModuleOwnershipKind::Visible; |
776 | } |
777 | |
778 | /// Set that this declaration is globally visible, even if it came from a |
779 | /// module that is not visible. |
780 | void setVisibleDespiteOwningModule() { |
781 | if (isHidden()) |
782 | setModuleOwnershipKind(ModuleOwnershipKind::Visible); |
783 | } |
784 | |
785 | /// Get the kind of module ownership for this declaration. |
786 | ModuleOwnershipKind getModuleOwnershipKind() const { |
787 | return NextInContextAndBits.getInt(); |
788 | } |
789 | |
790 | /// Set whether this declaration is hidden from name lookup. |
791 | void setModuleOwnershipKind(ModuleOwnershipKind MOK) { |
792 | assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && |
793 | MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && |
794 | !hasLocalOwningModuleStorage()) && |
795 | "no storage available for owning module for this declaration"); |
796 | NextInContextAndBits.setInt(MOK); |
797 | } |
798 | |
799 | unsigned getIdentifierNamespace() const { |
800 | return IdentifierNamespace; |
801 | } |
802 | |
803 | bool isInIdentifierNamespace(unsigned NS) const { |
804 | return getIdentifierNamespace() & NS; |
805 | } |
806 | |
807 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
808 | |
809 | bool hasTagIdentifierNamespace() const { |
810 | return isTagIdentifierNamespace(getIdentifierNamespace()); |
811 | } |
812 | |
813 | static bool isTagIdentifierNamespace(unsigned NS) { |
814 | // TagDecls have Tag and Type set and may also have TagFriend. |
815 | return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); |
816 | } |
817 | |
818 | /// getLexicalDeclContext - The declaration context where this Decl was |
819 | /// lexically declared (LexicalDC). May be different from |
820 | /// getDeclContext() (SemanticDC). |
821 | /// e.g.: |
822 | /// |
823 | /// namespace A { |
824 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
825 | /// } |
826 | /// void A::f(); // SemanticDC == namespace 'A' |
827 | /// // LexicalDC == global namespace |
828 | DeclContext *getLexicalDeclContext() { |
829 | if (isInSemaDC()) |
830 | return getSemanticDC(); |
831 | return getMultipleDC()->LexicalDC; |
832 | } |
833 | const DeclContext *getLexicalDeclContext() const { |
834 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
835 | } |
836 | |
837 | /// Determine whether this declaration is declared out of line (outside its |
838 | /// semantic context). |
839 | virtual bool isOutOfLine() const; |
840 | |
841 | /// setDeclContext - Set both the semantic and lexical DeclContext |
842 | /// to DC. |
843 | void setDeclContext(DeclContext *DC); |
844 | |
845 | void setLexicalDeclContext(DeclContext *DC); |
846 | |
847 | /// Determine whether this declaration is a templated entity (whether it is |
848 | // within the scope of a template parameter). |
849 | bool isTemplated() const; |
850 | |
851 | /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
852 | /// scoped decl is defined outside the current function or method. This is |
853 | /// roughly global variables and functions, but also handles enums (which |
854 | /// could be defined inside or outside a function etc). |
855 | bool isDefinedOutsideFunctionOrMethod() const { |
856 | return getParentFunctionOrMethod() == nullptr; |
857 | } |
858 | |
859 | /// Returns true if this declaration lexically is inside a function. |
860 | /// It recognizes non-defining declarations as well as members of local |
861 | /// classes: |
862 | /// \code |
863 | /// void foo() { void bar(); } |
864 | /// void foo2() { class ABC { void bar(); }; } |
865 | /// \endcode |
866 | bool isLexicallyWithinFunctionOrMethod() const; |
867 | |
868 | /// If this decl is defined inside a function/method/block it returns |
869 | /// the corresponding DeclContext, otherwise it returns null. |
870 | const DeclContext *getParentFunctionOrMethod() const; |
871 | DeclContext *getParentFunctionOrMethod() { |
872 | return const_cast<DeclContext*>( |
873 | const_cast<const Decl*>(this)->getParentFunctionOrMethod()); |
874 | } |
875 | |
876 | /// Retrieves the "canonical" declaration of the given declaration. |
877 | virtual Decl *getCanonicalDecl() { return this; } |
878 | const Decl *getCanonicalDecl() const { |
879 | return const_cast<Decl*>(this)->getCanonicalDecl(); |
880 | } |
881 | |
882 | /// Whether this particular Decl is a canonical one. |
883 | bool isCanonicalDecl() const { return getCanonicalDecl() == this; } |
884 | |
885 | protected: |
886 | /// Returns the next redeclaration or itself if this is the only decl. |
887 | /// |
888 | /// Decl subclasses that can be redeclared should override this method so that |
889 | /// Decl::redecl_iterator can iterate over them. |
890 | virtual Decl *getNextRedeclarationImpl() { return this; } |
891 | |
892 | /// Implementation of getPreviousDecl(), to be overridden by any |
893 | /// subclass that has a redeclaration chain. |
894 | virtual Decl *getPreviousDeclImpl() { return nullptr; } |
895 | |
896 | /// Implementation of getMostRecentDecl(), to be overridden by any |
897 | /// subclass that has a redeclaration chain. |
898 | virtual Decl *getMostRecentDeclImpl() { return this; } |
899 | |
900 | public: |
901 | /// Iterates through all the redeclarations of the same decl. |
902 | class redecl_iterator { |
903 | /// Current - The current declaration. |
904 | Decl *Current = nullptr; |
905 | Decl *Starter; |
906 | |
907 | public: |
908 | using value_type = Decl *; |
909 | using reference = const value_type &; |
910 | using pointer = const value_type *; |
911 | using iterator_category = std::forward_iterator_tag; |
912 | using difference_type = std::ptrdiff_t; |
913 | |
914 | redecl_iterator() = default; |
915 | explicit redecl_iterator(Decl *C) : Current(C), Starter(C) {} |
916 | |
917 | reference operator*() const { return Current; } |
918 | value_type operator->() const { return Current; } |
919 | |
920 | redecl_iterator& operator++() { |
921 | assert(Current && "Advancing while iterator has reached end"); |
922 | // Get either previous decl or latest decl. |
923 | Decl *Next = Current->getNextRedeclarationImpl(); |
924 | assert(Next && "Should return next redeclaration or itself, never null!"); |
925 | Current = (Next != Starter) ? Next : nullptr; |
926 | return *this; |
927 | } |
928 | |
929 | redecl_iterator operator++(int) { |
930 | redecl_iterator tmp(*this); |
931 | ++(*this); |
932 | return tmp; |
933 | } |
934 | |
935 | friend bool operator==(redecl_iterator x, redecl_iterator y) { |
936 | return x.Current == y.Current; |
937 | } |
938 | |
939 | friend bool operator!=(redecl_iterator x, redecl_iterator y) { |
940 | return x.Current != y.Current; |
941 | } |
942 | }; |
943 | |
944 | using redecl_range = llvm::iterator_range<redecl_iterator>; |
945 | |
946 | /// Returns an iterator range for all the redeclarations of the same |
947 | /// decl. It will iterate at least once (when this decl is the only one). |
948 | redecl_range redecls() const { |
949 | return redecl_range(redecls_begin(), redecls_end()); |
950 | } |
951 | |
952 | redecl_iterator redecls_begin() const { |
953 | return redecl_iterator(const_cast<Decl *>(this)); |
954 | } |
955 | |
956 | redecl_iterator redecls_end() const { return redecl_iterator(); } |
957 | |
958 | /// Retrieve the previous declaration that declares the same entity |
959 | /// as this declaration, or NULL if there is no previous declaration. |
960 | Decl *getPreviousDecl() { return getPreviousDeclImpl(); } |
961 | |
962 | /// Retrieve the most recent declaration that declares the same entity |
963 | /// as this declaration, or NULL if there is no previous declaration. |
964 | const Decl *getPreviousDecl() const { |
965 | return const_cast<Decl *>(this)->getPreviousDeclImpl(); |
966 | } |
967 | |
968 | /// True if this is the first declaration in its redeclaration chain. |
969 | bool isFirstDecl() const { |
970 | return getPreviousDecl() == nullptr; |
971 | } |
972 | |
973 | /// Retrieve the most recent declaration that declares the same entity |
974 | /// as this declaration (which may be this declaration). |
975 | Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); } |
976 | |
977 | /// Retrieve the most recent declaration that declares the same entity |
978 | /// as this declaration (which may be this declaration). |
979 | const Decl *getMostRecentDecl() const { |
980 | return const_cast<Decl *>(this)->getMostRecentDeclImpl(); |
981 | } |
982 | |
983 | /// getBody - If this Decl represents a declaration for a body of code, |
984 | /// such as a function or method definition, this method returns the |
985 | /// top-level Stmt* of that body. Otherwise this method returns null. |
986 | virtual Stmt* getBody() const { return nullptr; } |
987 | |
988 | /// Returns true if this \c Decl represents a declaration for a body of |
989 | /// code, such as a function or method definition. |
990 | /// Note that \c hasBody can also return true if any redeclaration of this |
991 | /// \c Decl represents a declaration for a body of code. |
992 | virtual bool hasBody() const { return getBody() != nullptr; } |
993 | |
994 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
995 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
996 | SourceLocation getBodyRBrace() const; |
997 | |
998 | // global temp stats (until we have a per-module visitor) |
999 | static void add(Kind k); |
1000 | static void EnableStatistics(); |
1001 | static void PrintStats(); |
1002 | |
1003 | /// isTemplateParameter - Determines whether this declaration is a |
1004 | /// template parameter. |
1005 | bool isTemplateParameter() const; |
1006 | |
1007 | /// isTemplateParameter - Determines whether this declaration is a |
1008 | /// template parameter pack. |
1009 | bool isTemplateParameterPack() const; |
1010 | |
1011 | /// Whether this declaration is a parameter pack. |
1012 | bool isParameterPack() const; |
1013 | |
1014 | /// returns true if this declaration is a template |
1015 | bool isTemplateDecl() const; |
1016 | |
1017 | /// Whether this declaration is a function or function template. |
1018 | bool isFunctionOrFunctionTemplate() const { |
1019 | return (DeclKind >= Decl::firstFunction && |
1020 | DeclKind <= Decl::lastFunction) || |
1021 | DeclKind == FunctionTemplate; |
1022 | } |
1023 | |
1024 | /// If this is a declaration that describes some template, this |
1025 | /// method returns that template declaration. |
1026 | TemplateDecl *getDescribedTemplate() const; |
1027 | |
1028 | /// Returns the function itself, or the templated function if this is a |
1029 | /// function template. |
1030 | FunctionDecl *getAsFunction() LLVM_READONLY; |
1031 | |
1032 | const FunctionDecl *getAsFunction() const { |
1033 | return const_cast<Decl *>(this)->getAsFunction(); |
1034 | } |
1035 | |
1036 | /// Changes the namespace of this declaration to reflect that it's |
1037 | /// a function-local extern declaration. |
1038 | /// |
1039 | /// These declarations appear in the lexical context of the extern |
1040 | /// declaration, but in the semantic context of the enclosing namespace |
1041 | /// scope. |
1042 | void setLocalExternDecl() { |
1043 | Decl *Prev = getPreviousDecl(); |
1044 | IdentifierNamespace &= ~IDNS_Ordinary; |
1045 | |
1046 | // It's OK for the declaration to still have the "invisible friend" flag or |
1047 | // the "conflicts with tag declarations in this scope" flag for the outer |
1048 | // scope. |
1049 | assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && |
1050 | "namespace is not ordinary"); |
1051 | |
1052 | IdentifierNamespace |= IDNS_LocalExtern; |
1053 | if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary) |
1054 | IdentifierNamespace |= IDNS_Ordinary; |
1055 | } |
1056 | |
1057 | /// Determine whether this is a block-scope declaration with linkage. |
1058 | /// This will either be a local variable declaration declared 'extern', or a |
1059 | /// local function declaration. |
1060 | bool isLocalExternDecl() { |
1061 | return IdentifierNamespace & IDNS_LocalExtern; |
1062 | } |
1063 | |
1064 | /// Changes the namespace of this declaration to reflect that it's |
1065 | /// the object of a friend declaration. |
1066 | /// |
1067 | /// These declarations appear in the lexical context of the friending |
1068 | /// class, but in the semantic context of the actual entity. This property |
1069 | /// applies only to a specific decl object; other redeclarations of the |
1070 | /// same entity may not (and probably don't) share this property. |
1071 | void setObjectOfFriendDecl(bool PerformFriendInjection = false) { |
1072 | unsigned OldNS = IdentifierNamespace; |
1073 | assert((OldNS & (IDNS_Tag | IDNS_Ordinary | |
1074 | IDNS_TagFriend | IDNS_OrdinaryFriend | |
1075 | IDNS_LocalExtern)) && |
1076 | "namespace includes neither ordinary nor tag"); |
1077 | assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | |
1078 | IDNS_TagFriend | IDNS_OrdinaryFriend | |
1079 | IDNS_LocalExtern)) && |
1080 | "namespace includes other than ordinary or tag"); |
1081 | |
1082 | Decl *Prev = getPreviousDecl(); |
1083 | IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type); |
1084 | |
1085 | if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { |
1086 | IdentifierNamespace |= IDNS_TagFriend; |
1087 | if (PerformFriendInjection || |
1088 | (Prev && Prev->getIdentifierNamespace() & IDNS_Tag)) |
1089 | IdentifierNamespace |= IDNS_Tag | IDNS_Type; |
1090 | } |
1091 | |
1092 | if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | IDNS_LocalExtern)) { |
1093 | IdentifierNamespace |= IDNS_OrdinaryFriend; |
1094 | if (PerformFriendInjection || |
1095 | (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)) |
1096 | IdentifierNamespace |= IDNS_Ordinary; |
1097 | } |
1098 | } |
1099 | |
1100 | enum FriendObjectKind { |
1101 | FOK_None, ///< Not a friend object. |
1102 | FOK_Declared, ///< A friend of a previously-declared entity. |
1103 | FOK_Undeclared ///< A friend of a previously-undeclared entity. |
1104 | }; |
1105 | |
1106 | /// Determines whether this declaration is the object of a |
1107 | /// friend declaration and, if so, what kind. |
1108 | /// |
1109 | /// There is currently no direct way to find the associated FriendDecl. |
1110 | FriendObjectKind getFriendObjectKind() const { |
1111 | unsigned mask = |
1112 | (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); |
1113 | if (!mask) return FOK_None; |
1114 | return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared |
1115 | : FOK_Undeclared); |
1116 | } |
1117 | |
1118 | /// Specifies that this declaration is a C++ overloaded non-member. |
1119 | void setNonMemberOperator() { |
1120 | assert(getKind() == Function || getKind() == FunctionTemplate); |
1121 | assert((IdentifierNamespace & IDNS_Ordinary) && |
1122 | "visible non-member operators should be in ordinary namespace"); |
1123 | IdentifierNamespace |= IDNS_NonMemberOperator; |
1124 | } |
1125 | |
1126 | static bool classofKind(Kind K) { return true; } |
1127 | static DeclContext *castToDeclContext(const Decl *); |
1128 | static Decl *castFromDeclContext(const DeclContext *); |
1129 | |
1130 | void print(raw_ostream &Out, unsigned Indentation = 0, |
1131 | bool PrintInstantiation = false) const; |
1132 | void print(raw_ostream &Out, const PrintingPolicy &Policy, |
1133 | unsigned Indentation = 0, bool PrintInstantiation = false) const; |
1134 | static void printGroup(Decl** Begin, unsigned NumDecls, |
1135 | raw_ostream &Out, const PrintingPolicy &Policy, |
1136 | unsigned Indentation = 0); |
1137 | |
1138 | // Debuggers don't usually respect default arguments. |
1139 | void dump() const; |
1140 | |
1141 | // Same as dump(), but forces color printing. |
1142 | void dumpColor() const; |
1143 | |
1144 | void dump(raw_ostream &Out, bool Deserialize = false) const; |
1145 | |
1146 | /// Looks through the Decl's underlying type to extract a FunctionType |
1147 | /// when possible. Will return null if the type underlying the Decl does not |
1148 | /// have a FunctionType. |
1149 | const FunctionType *getFunctionType(bool BlocksToo = true) const; |
1150 | |
1151 | private: |
1152 | void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx); |
1153 | void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
1154 | ASTContext &Ctx); |
1155 | |
1156 | protected: |
1157 | ASTMutationListener *getASTMutationListener() const; |
1158 | }; |
1159 | |
1160 | /// Determine whether two declarations declare the same entity. |
1161 | inline bool declaresSameEntity(const Decl *D1, const Decl *D2) { |
1162 | if (!D1 || !D2) |
1163 | return false; |
1164 | |
1165 | if (D1 == D2) |
1166 | return true; |
1167 | |
1168 | return D1->getCanonicalDecl() == D2->getCanonicalDecl(); |
1169 | } |
1170 | |
1171 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
1172 | /// doing something to a specific decl. |
1173 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
1174 | const Decl *TheDecl; |
1175 | SourceLocation Loc; |
1176 | SourceManager &SM; |
1177 | const char *Message; |
1178 | |
1179 | public: |
1180 | PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, |
1181 | SourceManager &sm, const char *Msg) |
1182 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
1183 | |
1184 | void print(raw_ostream &OS) const override; |
1185 | }; |
1186 | |
1187 | /// The results of name lookup within a DeclContext. This is either a |
1188 | /// single result (with no stable storage) or a collection of results (with |
1189 | /// stable storage provided by the lookup table). |
1190 | class DeclContextLookupResult { |
1191 | using ResultTy = ArrayRef<NamedDecl *>; |
1192 | |
1193 | ResultTy Result; |
1194 | |
1195 | // If there is only one lookup result, it would be invalidated by |
1196 | // reallocations of the name table, so store it separately. |
1197 | NamedDecl *Single = nullptr; |
1198 | |
1199 | static NamedDecl *const SingleElementDummyList; |
1200 | |
1201 | public: |
1202 | DeclContextLookupResult() = default; |
1203 | DeclContextLookupResult(ArrayRef<NamedDecl *> Result) |
1204 | : Result(Result) {} |
1205 | DeclContextLookupResult(NamedDecl *Single) |
1206 | : Result(SingleElementDummyList), Single(Single) {} |
1207 | |
1208 | class iterator; |
1209 | |
1210 | using IteratorBase = |
1211 | llvm::iterator_adaptor_base<iterator, ResultTy::iterator, |
1212 | std::random_access_iterator_tag, |
1213 | NamedDecl *const>; |
1214 | |
1215 | class iterator : public IteratorBase { |
1216 | value_type SingleElement; |
1217 | |
1218 | public: |
1219 | iterator() = default; |
1220 | explicit iterator(pointer Pos, value_type Single = nullptr) |
1221 | : IteratorBase(Pos), SingleElement(Single) {} |
1222 | |
1223 | reference operator*() const { |
1224 | return SingleElement ? SingleElement : IteratorBase::operator*(); |
1225 | } |
1226 | }; |
1227 | |
1228 | using const_iterator = iterator; |
1229 | using pointer = iterator::pointer; |
1230 | using reference = iterator::reference; |
1231 | |
1232 | iterator begin() const { return iterator(Result.begin(), Single); } |
1233 | iterator end() const { return iterator(Result.end(), Single); } |
1234 | |
1235 | bool empty() const { return Result.empty(); } |
1236 | pointer data() const { return Single ? &Single : Result.data(); } |
1237 | size_t size() const { return Single ? 1 : Result.size(); } |
1238 | reference front() const { return Single ? Single : Result.front(); } |
1239 | reference back() const { return Single ? Single : Result.back(); } |
1240 | reference operator[](size_t N) const { return Single ? Single : Result[N]; } |
1241 | |
1242 | // FIXME: Remove this from the interface |
1243 | DeclContextLookupResult slice(size_t N) const { |
1244 | DeclContextLookupResult Sliced = Result.slice(N); |
1245 | Sliced.Single = Single; |
1246 | return Sliced; |
1247 | } |
1248 | }; |
1249 | |
1250 | /// DeclContext - This is used only as base class of specific decl types that |
1251 | /// can act as declaration contexts. These decls are (only the top classes |
1252 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
1253 | /// |
1254 | /// TranslationUnitDecl |
1255 | /// NamespaceDecl |
1256 | /// FunctionDecl |
1257 | /// TagDecl |
1258 | /// ObjCMethodDecl |
1259 | /// ObjCContainerDecl |
1260 | /// LinkageSpecDecl |
1261 | /// ExportDecl |
1262 | /// BlockDecl |
1263 | /// OMPDeclareReductionDecl |
1264 | class DeclContext { |
1265 | /// DeclKind - This indicates which class this is. |
1266 | unsigned DeclKind : 8; |
1267 | |
1268 | /// Whether this declaration context also has some external |
1269 | /// storage that contains additional declarations that are lexically |
1270 | /// part of this context. |
1271 | mutable bool ExternalLexicalStorage : 1; |
1272 | |
1273 | /// Whether this declaration context also has some external |
1274 | /// storage that contains additional declarations that are visible |
1275 | /// in this context. |
1276 | mutable bool ExternalVisibleStorage : 1; |
1277 | |
1278 | /// Whether this declaration context has had external visible |
1279 | /// storage added since the last lookup. In this case, \c LookupPtr's |
1280 | /// invariant may not hold and needs to be fixed before we perform |
1281 | /// another lookup. |
1282 | mutable bool NeedToReconcileExternalVisibleStorage : 1; |
1283 | |
1284 | /// If \c true, this context may have local lexical declarations |
1285 | /// that are missing from the lookup table. |
1286 | mutable bool HasLazyLocalLexicalLookups : 1; |
1287 | |
1288 | /// If \c true, the external source may have lexical declarations |
1289 | /// that are missing from the lookup table. |
1290 | mutable bool HasLazyExternalLexicalLookups : 1; |
1291 | |
1292 | /// If \c true, lookups should only return identifier from |
1293 | /// DeclContext scope (for example TranslationUnit). Used in |
1294 | /// LookupQualifiedName() |
1295 | mutable bool UseQualifiedLookup : 1; |
1296 | |
1297 | /// Pointer to the data structure used to lookup declarations |
1298 | /// within this context (or a DependentStoredDeclsMap if this is a |
1299 | /// dependent context). We maintain the invariant that, if the map |
1300 | /// contains an entry for a DeclarationName (and we haven't lazily |
1301 | /// omitted anything), then it contains all relevant entries for that |
1302 | /// name (modulo the hasExternalDecls() flag). |
1303 | mutable StoredDeclsMap *LookupPtr = nullptr; |
1304 | |
1305 | protected: |
1306 | friend class ASTDeclReader; |
1307 | friend class ASTWriter; |
1308 | friend class ExternalASTSource; |
1309 | |
1310 | /// FirstDecl - The first declaration stored within this declaration |
1311 | /// context. |
1312 | mutable Decl *FirstDecl = nullptr; |
1313 | |
1314 | /// LastDecl - The last declaration stored within this declaration |
1315 | /// context. FIXME: We could probably cache this value somewhere |
1316 | /// outside of the DeclContext, to reduce the size of DeclContext by |
1317 | /// another pointer. |
1318 | mutable Decl *LastDecl = nullptr; |
1319 | |
1320 | /// Build up a chain of declarations. |
1321 | /// |
1322 | /// \returns the first/last pair of declarations. |
1323 | static std::pair<Decl *, Decl *> |
1324 | BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded); |
1325 | |
1326 | DeclContext(Decl::Kind K) |
1327 | : DeclKind(K), ExternalLexicalStorage(false), |
1328 | ExternalVisibleStorage(false), |
1329 | NeedToReconcileExternalVisibleStorage(false), |
1330 | HasLazyLocalLexicalLookups(false), HasLazyExternalLexicalLookups(false), |
1331 | UseQualifiedLookup(false) {} |
1332 | |
1333 | public: |
1334 | ~DeclContext(); |
1335 | |
1336 | Decl::Kind getDeclKind() const { |
1337 | return static_cast<Decl::Kind>(DeclKind); |
1338 | } |
1339 | |
1340 | const char *getDeclKindName() const; |
1341 | |
1342 | /// getParent - Returns the containing DeclContext. |
1343 | DeclContext *getParent() { |
1344 | return cast<Decl>(this)->getDeclContext(); |
1345 | } |
1346 | const DeclContext *getParent() const { |
1347 | return const_cast<DeclContext*>(this)->getParent(); |
1348 | } |
1349 | |
1350 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
1351 | /// different from getParent, e.g.: |
1352 | /// |
1353 | /// namespace A { |
1354 | /// struct S; |
1355 | /// } |
1356 | /// struct A::S {}; // getParent() == namespace 'A' |
1357 | /// // getLexicalParent() == translation unit |
1358 | /// |
1359 | DeclContext *getLexicalParent() { |
1360 | return cast<Decl>(this)->getLexicalDeclContext(); |
1361 | } |
1362 | const DeclContext *getLexicalParent() const { |
1363 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
1364 | } |
1365 | |
1366 | DeclContext *getLookupParent(); |
1367 | |
1368 | const DeclContext *getLookupParent() const { |
1369 | return const_cast<DeclContext*>(this)->getLookupParent(); |
1370 | } |
1371 | |
1372 | ASTContext &getParentASTContext() const { |
1373 | return cast<Decl>(this)->getASTContext(); |
1374 | } |
1375 | |
1376 | bool isClosure() const { |
1377 | return DeclKind == Decl::Block; |
1378 | } |
1379 | |
1380 | bool isObjCContainer() const { |
1381 | switch (DeclKind) { |
1382 | case Decl::ObjCCategory: |
1383 | case Decl::ObjCCategoryImpl: |
1384 | case Decl::ObjCImplementation: |
1385 | case Decl::ObjCInterface: |
1386 | case Decl::ObjCProtocol: |
1387 | return true; |
1388 | } |
1389 | return false; |
1390 | } |
1391 | |
1392 | bool isFunctionOrMethod() const { |
1393 | switch (DeclKind) { |
1394 | case Decl::Block: |
1395 | case Decl::Captured: |
1396 | case Decl::ObjCMethod: |
1397 | return true; |
1398 | default: |
1399 | return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction; |
1400 | } |
1401 | } |
1402 | |
1403 | /// Test whether the context supports looking up names. |
1404 | bool isLookupContext() const { |
1405 | return !isFunctionOrMethod() && DeclKind != Decl::LinkageSpec && |
1406 | DeclKind != Decl::Export; |
1407 | } |
1408 | |
1409 | bool isFileContext() const { |
1410 | return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; |
1411 | } |
1412 | |
1413 | bool isTranslationUnit() const { |
1414 | return DeclKind == Decl::TranslationUnit; |
1415 | } |
1416 | |
1417 | bool isRecord() const { |
1418 | return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord; |
1419 | } |
1420 | |
1421 | bool isNamespace() const { |
1422 | return DeclKind == Decl::Namespace; |
1423 | } |
1424 | |
1425 | bool isStdNamespace() const; |
1426 | |
1427 | bool isInlineNamespace() const; |
1428 | |
1429 | /// Determines whether this context is dependent on a |
1430 | /// template parameter. |
1431 | bool isDependentContext() const; |
1432 | |
1433 | /// isTransparentContext - Determines whether this context is a |
1434 | /// "transparent" context, meaning that the members declared in this |
1435 | /// context are semantically declared in the nearest enclosing |
1436 | /// non-transparent (opaque) context but are lexically declared in |
1437 | /// this context. For example, consider the enumerators of an |
1438 | /// enumeration type: |
1439 | /// @code |
1440 | /// enum E { |
1441 | /// Val1 |
1442 | /// }; |
1443 | /// @endcode |
1444 | /// Here, E is a transparent context, so its enumerator (Val1) will |
1445 | /// appear (semantically) that it is in the same context of E. |
1446 | /// Examples of transparent contexts include: enumerations (except for |
1447 | /// C++0x scoped enums), and C++ linkage specifications. |
1448 | bool isTransparentContext() const; |
1449 | |
1450 | /// Determines whether this context or some of its ancestors is a |
1451 | /// linkage specification context that specifies C linkage. |
1452 | bool isExternCContext() const; |
1453 | |
1454 | /// Retrieve the nearest enclosing C linkage specification context. |
1455 | const LinkageSpecDecl *getExternCContext() const; |
1456 | |
1457 | /// Determines whether this context or some of its ancestors is a |
1458 | /// linkage specification context that specifies C++ linkage. |
1459 | bool isExternCXXContext() const; |
1460 | |
1461 | /// Determine whether this declaration context is equivalent |
1462 | /// to the declaration context DC. |
1463 | bool Equals(const DeclContext *DC) const { |
1464 | return DC && this->getPrimaryContext() == DC->getPrimaryContext(); |
1465 | } |
1466 | |
1467 | /// Determine whether this declaration context encloses the |
1468 | /// declaration context DC. |
1469 | bool Encloses(const DeclContext *DC) const; |
1470 | |
1471 | /// Find the nearest non-closure ancestor of this context, |
1472 | /// i.e. the innermost semantic parent of this context which is not |
1473 | /// a closure. A context may be its own non-closure ancestor. |
1474 | Decl *getNonClosureAncestor(); |
1475 | const Decl *getNonClosureAncestor() const { |
1476 | return const_cast<DeclContext*>(this)->getNonClosureAncestor(); |
1477 | } |
1478 | |
1479 | /// getPrimaryContext - There may be many different |
1480 | /// declarations of the same entity (including forward declarations |
1481 | /// of classes, multiple definitions of namespaces, etc.), each with |
1482 | /// a different set of declarations. This routine returns the |
1483 | /// "primary" DeclContext structure, which will contain the |
1484 | /// information needed to perform name lookup into this context. |
1485 | DeclContext *getPrimaryContext(); |
1486 | const DeclContext *getPrimaryContext() const { |
1487 | return const_cast<DeclContext*>(this)->getPrimaryContext(); |
1488 | } |
1489 | |
1490 | /// getRedeclContext - Retrieve the context in which an entity conflicts with |
1491 | /// other entities of the same name, or where it is a redeclaration if the |
1492 | /// two entities are compatible. This skips through transparent contexts. |
1493 | DeclContext *getRedeclContext(); |
1494 | const DeclContext *getRedeclContext() const { |
1495 | return const_cast<DeclContext *>(this)->getRedeclContext(); |
1496 | } |
1497 | |
1498 | /// Retrieve the nearest enclosing namespace context. |
1499 | DeclContext *getEnclosingNamespaceContext(); |
1500 | const DeclContext *getEnclosingNamespaceContext() const { |
1501 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
1502 | } |
1503 | |
1504 | /// Retrieve the outermost lexically enclosing record context. |
1505 | RecordDecl *getOuterLexicalRecordContext(); |
1506 | const RecordDecl *getOuterLexicalRecordContext() const { |
1507 | return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext(); |
1508 | } |
1509 | |
1510 | /// Test if this context is part of the enclosing namespace set of |
1511 | /// the context NS, as defined in C++0x [namespace.def]p9. If either context |
1512 | /// isn't a namespace, this is equivalent to Equals(). |
1513 | /// |
1514 | /// The enclosing namespace set of a namespace is the namespace and, if it is |
1515 | /// inline, its enclosing namespace, recursively. |
1516 | bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; |
1517 | |
1518 | /// Collects all of the declaration contexts that are semantically |
1519 | /// connected to this declaration context. |
1520 | /// |
1521 | /// For declaration contexts that have multiple semantically connected but |
1522 | /// syntactically distinct contexts, such as C++ namespaces, this routine |
1523 | /// retrieves the complete set of such declaration contexts in source order. |
1524 | /// For example, given: |
1525 | /// |
1526 | /// \code |
1527 | /// namespace N { |
1528 | /// int x; |
1529 | /// } |
1530 | /// namespace N { |
1531 | /// int y; |
1532 | /// } |
1533 | /// \endcode |
1534 | /// |
1535 | /// The \c Contexts parameter will contain both definitions of N. |
1536 | /// |
1537 | /// \param Contexts Will be cleared and set to the set of declaration |
1538 | /// contexts that are semanticaly connected to this declaration context, |
1539 | /// in source order, including this context (which may be the only result, |
1540 | /// for non-namespace contexts). |
1541 | void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts); |
1542 | |
1543 | /// decl_iterator - Iterates through the declarations stored |
1544 | /// within this context. |
1545 | class decl_iterator { |
1546 | /// Current - The current declaration. |
1547 | Decl *Current = nullptr; |
1548 | |
1549 | public: |
1550 | using value_type = Decl *; |
1551 | using reference = const value_type &; |
1552 | using pointer = const value_type *; |
1553 | using iterator_category = std::forward_iterator_tag; |
1554 | using difference_type = std::ptrdiff_t; |
1555 | |
1556 | decl_iterator() = default; |
1557 | explicit decl_iterator(Decl *C) : Current(C) {} |
1558 | |
1559 | reference operator*() const { return Current; } |
1560 | |
1561 | // This doesn't meet the iterator requirements, but it's convenient |
1562 | value_type operator->() const { return Current; } |
1563 | |
1564 | decl_iterator& operator++() { |
1565 | Current = Current->getNextDeclInContext(); |
1566 | return *this; |
1567 | } |
1568 | |
1569 | decl_iterator operator++(int) { |
1570 | decl_iterator tmp(*this); |
1571 | ++(*this); |
1572 | return tmp; |
1573 | } |
1574 | |
1575 | friend bool operator==(decl_iterator x, decl_iterator y) { |
1576 | return x.Current == y.Current; |
1577 | } |
1578 | |
1579 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
1580 | return x.Current != y.Current; |
1581 | } |
1582 | }; |
1583 | |
1584 | using decl_range = llvm::iterator_range<decl_iterator>; |
1585 | |
1586 | /// decls_begin/decls_end - Iterate over the declarations stored in |
1587 | /// this context. |
1588 | decl_range decls() const { return decl_range(decls_begin(), decls_end()); } |
1589 | decl_iterator decls_begin() const; |
1590 | decl_iterator decls_end() const { return decl_iterator(); } |
1591 | bool decls_empty() const; |
1592 | |
1593 | /// noload_decls_begin/end - Iterate over the declarations stored in this |
1594 | /// context that are currently loaded; don't attempt to retrieve anything |
1595 | /// from an external source. |
1596 | decl_range noload_decls() const { |
1597 | return decl_range(noload_decls_begin(), noload_decls_end()); |
1598 | } |
1599 | decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); } |
1600 | decl_iterator noload_decls_end() const { return decl_iterator(); } |
1601 | |
1602 | /// specific_decl_iterator - Iterates over a subrange of |
1603 | /// declarations stored in a DeclContext, providing only those that |
1604 | /// are of type SpecificDecl (or a class derived from it). This |
1605 | /// iterator is used, for example, to provide iteration over just |
1606 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
1607 | template<typename SpecificDecl> |
1608 | class specific_decl_iterator { |
1609 | /// Current - The current, underlying declaration iterator, which |
1610 | /// will either be NULL or will point to a declaration of |
1611 | /// type SpecificDecl. |
1612 | DeclContext::decl_iterator Current; |
1613 | |
1614 | /// SkipToNextDecl - Advances the current position up to the next |
1615 | /// declaration of type SpecificDecl that also meets the criteria |
1616 | /// required by Acceptable. |
1617 | void SkipToNextDecl() { |
1618 | while (*Current && !isa<SpecificDecl>(*Current)) |
1619 | ++Current; |
1620 | } |
1621 | |
1622 | public: |
1623 | using value_type = SpecificDecl *; |
1624 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
1625 | // if we ever have a need for them. |
1626 | using reference = void; |
1627 | using pointer = void; |
1628 | using difference_type = |
1629 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
1630 | using iterator_category = std::forward_iterator_tag; |
1631 | |
1632 | specific_decl_iterator() = default; |
1633 | |
1634 | /// specific_decl_iterator - Construct a new iterator over a |
1635 | /// subset of the declarations the range [C, |
1636 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
1637 | /// member function of SpecificDecl that should return true for |
1638 | /// all of the SpecificDecl instances that will be in the subset |
1639 | /// of iterators. For example, if you want Objective-C instance |
1640 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
1641 | /// &ObjCMethodDecl::isInstanceMethod. |
1642 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
1643 | SkipToNextDecl(); |
1644 | } |
1645 | |
1646 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
1647 | |
1648 | // This doesn't meet the iterator requirements, but it's convenient |
1649 | value_type operator->() const { return **this; } |
1650 | |
1651 | specific_decl_iterator& operator++() { |
1652 | ++Current; |
1653 | SkipToNextDecl(); |
1654 | return *this; |
1655 | } |
1656 | |
1657 | specific_decl_iterator operator++(int) { |
1658 | specific_decl_iterator tmp(*this); |
1659 | ++(*this); |
1660 | return tmp; |
1661 | } |
1662 | |
1663 | friend bool operator==(const specific_decl_iterator& x, |
1664 | const specific_decl_iterator& y) { |
1665 | return x.Current == y.Current; |
1666 | } |
1667 | |
1668 | friend bool operator!=(const specific_decl_iterator& x, |
1669 | const specific_decl_iterator& y) { |
1670 | return x.Current != y.Current; |
1671 | } |
1672 | }; |
1673 | |
1674 | /// Iterates over a filtered subrange of declarations stored |
1675 | /// in a DeclContext. |
1676 | /// |
1677 | /// This iterator visits only those declarations that are of type |
1678 | /// SpecificDecl (or a class derived from it) and that meet some |
1679 | /// additional run-time criteria. This iterator is used, for |
1680 | /// example, to provide access to the instance methods within an |
1681 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
1682 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
1683 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
1684 | class filtered_decl_iterator { |
1685 | /// Current - The current, underlying declaration iterator, which |
1686 | /// will either be NULL or will point to a declaration of |
1687 | /// type SpecificDecl. |
1688 | DeclContext::decl_iterator Current; |
1689 | |
1690 | /// SkipToNextDecl - Advances the current position up to the next |
1691 | /// declaration of type SpecificDecl that also meets the criteria |
1692 | /// required by Acceptable. |
1693 | void SkipToNextDecl() { |
1694 | while (*Current && |
1695 | (!isa<SpecificDecl>(*Current) || |
1696 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
1697 | ++Current; |
1698 | } |
1699 | |
1700 | public: |
1701 | using value_type = SpecificDecl *; |
1702 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
1703 | // if we ever have a need for them. |
1704 | using reference = void; |
1705 | using pointer = void; |
1706 | using difference_type = |
1707 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
1708 | using iterator_category = std::forward_iterator_tag; |
1709 | |
1710 | filtered_decl_iterator() = default; |
1711 | |
1712 | /// filtered_decl_iterator - Construct a new iterator over a |
1713 | /// subset of the declarations the range [C, |
1714 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
1715 | /// member function of SpecificDecl that should return true for |
1716 | /// all of the SpecificDecl instances that will be in the subset |
1717 | /// of iterators. For example, if you want Objective-C instance |
1718 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
1719 | /// &ObjCMethodDecl::isInstanceMethod. |
1720 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
1721 | SkipToNextDecl(); |
1722 | } |
1723 | |
1724 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
1725 | value_type operator->() const { return cast<SpecificDecl>(*Current); } |
1726 | |
1727 | filtered_decl_iterator& operator++() { |
1728 | ++Current; |
1729 | SkipToNextDecl(); |
1730 | return *this; |
1731 | } |
1732 | |
1733 | filtered_decl_iterator operator++(int) { |
1734 | filtered_decl_iterator tmp(*this); |
1735 | ++(*this); |
1736 | return tmp; |
1737 | } |
1738 | |
1739 | friend bool operator==(const filtered_decl_iterator& x, |
1740 | const filtered_decl_iterator& y) { |
1741 | return x.Current == y.Current; |
1742 | } |
1743 | |
1744 | friend bool operator!=(const filtered_decl_iterator& x, |
1745 | const filtered_decl_iterator& y) { |
1746 | return x.Current != y.Current; |
1747 | } |
1748 | }; |
1749 | |
1750 | /// Add the declaration D into this context. |
1751 | /// |
1752 | /// This routine should be invoked when the declaration D has first |
1753 | /// been declared, to place D into the context where it was |
1754 | /// (lexically) defined. Every declaration must be added to one |
1755 | /// (and only one!) context, where it can be visited via |
1756 | /// [decls_begin(), decls_end()). Once a declaration has been added |
1757 | /// to its lexical context, the corresponding DeclContext owns the |
1758 | /// declaration. |
1759 | /// |
1760 | /// If D is also a NamedDecl, it will be made visible within its |
1761 | /// semantic context via makeDeclVisibleInContext. |
1762 | void addDecl(Decl *D); |
1763 | |
1764 | /// Add the declaration D into this context, but suppress |
1765 | /// searches for external declarations with the same name. |
1766 | /// |
1767 | /// Although analogous in function to addDecl, this removes an |
1768 | /// important check. This is only useful if the Decl is being |
1769 | /// added in response to an external search; in all other cases, |
1770 | /// addDecl() is the right function to use. |
1771 | /// See the ASTImporter for use cases. |
1772 | void addDeclInternal(Decl *D); |
1773 | |
1774 | /// Add the declaration D to this context without modifying |
1775 | /// any lookup tables. |
1776 | /// |
1777 | /// This is useful for some operations in dependent contexts where |
1778 | /// the semantic context might not be dependent; this basically |
1779 | /// only happens with friends. |
1780 | void addHiddenDecl(Decl *D); |
1781 | |
1782 | /// Removes a declaration from this context. |
1783 | void removeDecl(Decl *D); |
1784 | |
1785 | /// Checks whether a declaration is in this context. |
1786 | bool containsDecl(Decl *D) const; |
1787 | |
1788 | /// Checks whether a declaration is in this context. |
1789 | /// This also loads the Decls from the external source before the check. |
1790 | bool containsDeclAndLoad(Decl *D) const; |
1791 | |
1792 | using lookup_result = DeclContextLookupResult; |
1793 | using lookup_iterator = lookup_result::iterator; |
1794 | |
1795 | /// lookup - Find the declarations (if any) with the given Name in |
1796 | /// this context. Returns a range of iterators that contains all of |
1797 | /// the declarations with this name, with object, function, member, |
1798 | /// and enumerator names preceding any tag name. Note that this |
1799 | /// routine will not look into parent contexts. |
1800 | lookup_result lookup(DeclarationName Name) const; |
1801 | |
1802 | /// Find the declarations with the given name that are visible |
1803 | /// within this context; don't attempt to retrieve anything from an |
1804 | /// external source. |
1805 | lookup_result noload_lookup(DeclarationName Name); |
1806 | |
1807 | /// A simplistic name lookup mechanism that performs name lookup |
1808 | /// into this declaration context without consulting the external source. |
1809 | /// |
1810 | /// This function should almost never be used, because it subverts the |
1811 | /// usual relationship between a DeclContext and the external source. |
1812 | /// See the ASTImporter for the (few, but important) use cases. |
1813 | /// |
1814 | /// FIXME: This is very inefficient; replace uses of it with uses of |
1815 | /// noload_lookup. |
1816 | void localUncachedLookup(DeclarationName Name, |
1817 | SmallVectorImpl<NamedDecl *> &Results); |
1818 | |
1819 | /// Makes a declaration visible within this context. |
1820 | /// |
1821 | /// This routine makes the declaration D visible to name lookup |
1822 | /// within this context and, if this is a transparent context, |
1823 | /// within its parent contexts up to the first enclosing |
1824 | /// non-transparent context. Making a declaration visible within a |
1825 | /// context does not transfer ownership of a declaration, and a |
1826 | /// declaration can be visible in many contexts that aren't its |
1827 | /// lexical context. |
1828 | /// |
1829 | /// If D is a redeclaration of an existing declaration that is |
1830 | /// visible from this context, as determined by |
1831 | /// NamedDecl::declarationReplaces, the previous declaration will be |
1832 | /// replaced with D. |
1833 | void makeDeclVisibleInContext(NamedDecl *D); |
1834 | |
1835 | /// all_lookups_iterator - An iterator that provides a view over the results |
1836 | /// of looking up every possible name. |
1837 | class all_lookups_iterator; |
1838 | |
1839 | using lookups_range = llvm::iterator_range<all_lookups_iterator>; |
1840 | |
1841 | lookups_range lookups() const; |
1842 | // Like lookups(), but avoids loading external declarations. |
1843 | // If PreserveInternalState, avoids building lookup data structures too. |
1844 | lookups_range noload_lookups(bool PreserveInternalState) const; |
1845 | |
1846 | /// Iterators over all possible lookups within this context. |
1847 | all_lookups_iterator lookups_begin() const; |
1848 | all_lookups_iterator lookups_end() const; |
1849 | |
1850 | /// Iterators over all possible lookups within this context that are |
1851 | /// currently loaded; don't attempt to retrieve anything from an external |
1852 | /// source. |
1853 | all_lookups_iterator noload_lookups_begin() const; |
1854 | all_lookups_iterator noload_lookups_end() const; |
1855 | |
1856 | struct udir_iterator; |
1857 | |
1858 | using udir_iterator_base = |
1859 | llvm::iterator_adaptor_base<udir_iterator, lookup_iterator, |
1860 | std::random_access_iterator_tag, |
1861 | UsingDirectiveDecl *>; |
1862 | |
1863 | struct udir_iterator : udir_iterator_base { |
1864 | udir_iterator(lookup_iterator I) : udir_iterator_base(I) {} |
1865 | |
1866 | UsingDirectiveDecl *operator*() const; |
1867 | }; |
1868 | |
1869 | using udir_range = llvm::iterator_range<udir_iterator>; |
1870 | |
1871 | udir_range using_directives() const; |
1872 | |
1873 | // These are all defined in DependentDiagnostic.h. |
1874 | class ddiag_iterator; |
1875 | |
1876 | using ddiag_range = llvm::iterator_range<DeclContext::ddiag_iterator>; |
1877 | |
1878 | inline ddiag_range ddiags() const; |
1879 | |
1880 | // Low-level accessors |
1881 | |
1882 | /// Mark that there are external lexical declarations that we need |
1883 | /// to include in our lookup table (and that are not available as external |
1884 | /// visible lookups). These extra lookup results will be found by walking |
1885 | /// the lexical declarations of this context. This should be used only if |
1886 | /// setHasExternalLexicalStorage() has been called on any decl context for |
1887 | /// which this is the primary context. |
1888 | void setMustBuildLookupTable() { |
1889 | assert(this == getPrimaryContext() && |
1890 | "should only be called on primary context"); |
1891 | HasLazyExternalLexicalLookups = true; |
1892 | } |
1893 | |
1894 | /// Retrieve the internal representation of the lookup structure. |
1895 | /// This may omit some names if we are lazily building the structure. |
1896 | StoredDeclsMap *getLookupPtr() const { return LookupPtr; } |
1897 | |
1898 | /// Ensure the lookup structure is fully-built and return it. |
1899 | StoredDeclsMap *buildLookup(); |
1900 | |
1901 | /// Whether this DeclContext has external storage containing |
1902 | /// additional declarations that are lexically in this context. |
1903 | bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } |
1904 | |
1905 | /// State whether this DeclContext has external storage for |
1906 | /// declarations lexically in this context. |
1907 | void setHasExternalLexicalStorage(bool ES = true) { |
1908 | ExternalLexicalStorage = ES; |
1909 | } |
1910 | |
1911 | /// Whether this DeclContext has external storage containing |
1912 | /// additional declarations that are visible in this context. |
1913 | bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } |
1914 | |
1915 | /// State whether this DeclContext has external storage for |
1916 | /// declarations visible in this context. |
1917 | void setHasExternalVisibleStorage(bool ES = true) { |
1918 | ExternalVisibleStorage = ES; |
1919 | if (ES && LookupPtr) |
1920 | NeedToReconcileExternalVisibleStorage = true; |
1921 | } |
1922 | |
1923 | /// Determine whether the given declaration is stored in the list of |
1924 | /// declarations lexically within this context. |
1925 | bool isDeclInLexicalTraversal(const Decl *D) const { |
1926 | return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl || |
1927 | D == LastDecl); |
1928 | } |
1929 | |
1930 | bool setUseQualifiedLookup(bool use = true) { |
1931 | bool old_value = UseQualifiedLookup; |
1932 | UseQualifiedLookup = use; |
1933 | return old_value; |
1934 | } |
1935 | |
1936 | bool shouldUseQualifiedLookup() const { |
1937 | return UseQualifiedLookup; |
1938 | } |
1939 | |
1940 | static bool classof(const Decl *D); |
1941 | static bool classof(const DeclContext *D) { return true; } |
1942 | |
1943 | void dumpDeclContext() const; |
1944 | void dumpLookups() const; |
1945 | void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false, |
1946 | bool Deserialize = false) const; |
1947 | |
1948 | private: |
1949 | friend class DependentDiagnostic; |
1950 | |
1951 | void reconcileExternalVisibleStorage() const; |
1952 | bool LoadLexicalDeclsFromExternalStorage() const; |
1953 | |
1954 | /// Makes a declaration visible within this context, but |
1955 | /// suppresses searches for external declarations with the same |
1956 | /// name. |
1957 | /// |
1958 | /// Analogous to makeDeclVisibleInContext, but for the exclusive |
1959 | /// use of addDeclInternal(). |
1960 | void makeDeclVisibleInContextInternal(NamedDecl *D); |
1961 | |
1962 | StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; |
1963 | |
1964 | void loadLazyLocalLexicalLookups(); |
1965 | void buildLookupImpl(DeclContext *DCtx, bool Internal); |
1966 | void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
1967 | bool Rediscoverable); |
1968 | void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); |
1969 | }; |
1970 | |
1971 | inline bool Decl::isTemplateParameter() const { |
1972 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || |
1973 | getKind() == TemplateTemplateParm; |
1974 | } |
1975 | |
1976 | // Specialization selected when ToTy is not a known subclass of DeclContext. |
1977 | template <class ToTy, |
1978 | bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value> |
1979 | struct cast_convert_decl_context { |
1980 | static const ToTy *doit(const DeclContext *Val) { |
1981 | return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); |
1982 | } |
1983 | |
1984 | static ToTy *doit(DeclContext *Val) { |
1985 | return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); |
1986 | } |
1987 | }; |
1988 | |
1989 | // Specialization selected when ToTy is a known subclass of DeclContext. |
1990 | template <class ToTy> |
1991 | struct cast_convert_decl_context<ToTy, true> { |
1992 | static const ToTy *doit(const DeclContext *Val) { |
1993 | return static_cast<const ToTy*>(Val); |
1994 | } |
1995 | |
1996 | static ToTy *doit(DeclContext *Val) { |
1997 | return static_cast<ToTy*>(Val); |
1998 | } |
1999 | }; |
2000 | |
2001 | } // namespace clang |
2002 | |
2003 | namespace llvm { |
2004 | |
2005 | /// isa<T>(DeclContext*) |
2006 | template <typename To> |
2007 | struct isa_impl<To, ::clang::DeclContext> { |
2008 | static bool doit(const ::clang::DeclContext &Val) { |
2009 | return To::classofKind(Val.getDeclKind()); |
2010 | } |
2011 | }; |
2012 | |
2013 | /// cast<T>(DeclContext*) |
2014 | template<class ToTy> |
2015 | struct cast_convert_val<ToTy, |
2016 | const ::clang::DeclContext,const ::clang::DeclContext> { |
2017 | static const ToTy &doit(const ::clang::DeclContext &Val) { |
2018 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
2019 | } |
2020 | }; |
2021 | |
2022 | template<class ToTy> |
2023 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { |
2024 | static ToTy &doit(::clang::DeclContext &Val) { |
2025 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
2026 | } |
2027 | }; |
2028 | |
2029 | template<class ToTy> |
2030 | struct cast_convert_val<ToTy, |
2031 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
2032 | static const ToTy *doit(const ::clang::DeclContext *Val) { |
2033 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
2034 | } |
2035 | }; |
2036 | |
2037 | template<class ToTy> |
2038 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { |
2039 | static ToTy *doit(::clang::DeclContext *Val) { |
2040 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
2041 | } |
2042 | }; |
2043 | |
2044 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
2045 | template<class FromTy> |
2046 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
2047 | static ::clang::DeclContext &doit(const FromTy &Val) { |
2048 | return *FromTy::castToDeclContext(&Val); |
2049 | } |
2050 | }; |
2051 | |
2052 | template<class FromTy> |
2053 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
2054 | static ::clang::DeclContext *doit(const FromTy *Val) { |
2055 | return FromTy::castToDeclContext(Val); |
2056 | } |
2057 | }; |
2058 | |
2059 | template<class FromTy> |
2060 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
2061 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
2062 | return *FromTy::castToDeclContext(&Val); |
2063 | } |
2064 | }; |
2065 | |
2066 | template<class FromTy> |
2067 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
2068 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
2069 | return FromTy::castToDeclContext(Val); |
2070 | } |
2071 | }; |
2072 | |
2073 | } // namespace llvm |
2074 | |
2075 | #endif // LLVM_CLANG_AST_DECLBASE_H |
2076 |
Warning: That file was not part of the compilation database. It may have many parsing errors.