Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1 | //===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===// |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 10 | // This file defines the Decl and DeclContext interfaces. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_AST_DECLBASE_H |
| 15 | #define LLVM_CLANG_AST_DECLBASE_H |
| 16 | |
| 17 | #include "clang/AST/Attr.h" |
| 18 | #include "clang/AST/Type.h" |
John McCall | ad2b804 | 2010-01-18 23:21:37 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Specifiers.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 24 | class DeclContext; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 25 | class TranslationUnitDecl; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 26 | class NamespaceDecl; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 27 | class UsingDirectiveDecl; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 28 | class NamedDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 29 | class FunctionDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 30 | class CXXRecordDecl; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 31 | class EnumDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 32 | class ObjCMethodDecl; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 33 | class ObjCContainerDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 34 | class ObjCInterfaceDecl; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 35 | class ObjCCategoryDecl; |
| 36 | class ObjCProtocolDecl; |
| 37 | class ObjCImplementationDecl; |
| 38 | class ObjCCategoryImplDecl; |
Argyrios Kyrtzidis | bfb498d | 2009-07-17 01:19:49 +0000 | [diff] [blame] | 39 | class ObjCImplDecl; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 40 | class LinkageSpecDecl; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 41 | class BlockDecl; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 42 | class DeclarationName; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 43 | class CompoundStmt; |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 44 | class StoredDeclsMap; |
| 45 | class DependentDiagnostic; |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 46 | class ASTMutationListener; |
Chris Lattner | 0eda3b3 | 2009-03-29 04:32:54 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | namespace llvm { |
| 50 | // DeclContext* is only 4-byte aligned on 32-bit systems. |
| 51 | template<> |
| 52 | class PointerLikeTypeTraits<clang::DeclContext*> { |
| 53 | typedef clang::DeclContext* PT; |
| 54 | public: |
| 55 | static inline void *getAsVoidPointer(PT P) { return P; } |
| 56 | static inline PT getFromVoidPointer(void *P) { |
| 57 | return static_cast<PT>(P); |
| 58 | } |
| 59 | enum { NumLowBitsAvailable = 2 }; |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | namespace clang { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 65 | /// \brief Captures the result of checking the availability of a |
| 66 | /// declaration. |
| 67 | enum AvailabilityResult { |
| 68 | AR_Available = 0, |
| 69 | AR_NotYetIntroduced, |
| 70 | AR_Deprecated, |
| 71 | AR_Unavailable |
| 72 | }; |
| 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
| 75 | /// typedef, function, struct, etc. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 76 | /// |
| 77 | class Decl { |
| 78 | public: |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 79 | /// \brief Lists the kind of concrete classes of Decl. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 80 | enum Kind { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 81 | #define DECL(DERIVED, BASE) DERIVED, |
| 82 | #define ABSTRACT_DECL(DECL) |
| 83 | #define DECL_RANGE(BASE, START, END) \ |
| 84 | first##BASE = START, last##BASE = END, |
| 85 | #define LAST_DECL_RANGE(BASE, START, END) \ |
| 86 | first##BASE = START, last##BASE = END |
| 87 | #include "clang/AST/DeclNodes.inc" |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 90 | /// \brief A placeholder type used to construct an empty shell of a |
| 91 | /// decl-derived type that will be filled in later (e.g., by some |
| 92 | /// deserialization method). |
| 93 | struct EmptyShell { }; |
| 94 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 95 | /// IdentifierNamespace - The different namespaces in which |
| 96 | /// declarations may appear. According to C99 6.2.3, there are |
| 97 | /// four namespaces, labels, tags, members and ordinary |
| 98 | /// identifiers. C++ describes lookup completely differently: |
| 99 | /// certain lookups merely "ignore" certain kinds of declarations, |
| 100 | /// usually based on whether the declaration is of a type, etc. |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 101 | /// |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 102 | /// These are meant as bitmasks, so that searches in |
| 103 | /// C++ can look into the "tag" namespace during ordinary lookup. |
| 104 | /// |
Sebastian Redl | 07a353c | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 105 | /// Decl currently provides 15 bits of IDNS bits. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 106 | enum IdentifierNamespace { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 107 | /// Labels, declared with 'x:' and referenced with 'goto x'. |
| 108 | IDNS_Label = 0x0001, |
| 109 | |
| 110 | /// Tags, declared with 'struct foo;' and referenced with |
| 111 | /// 'struct foo'. All tags are also types. This is what |
| 112 | /// elaborated-type-specifiers look for in C. |
| 113 | IDNS_Tag = 0x0002, |
| 114 | |
| 115 | /// Types, declared with 'struct foo', typedefs, etc. |
| 116 | /// This is what elaborated-type-specifiers look for in C++, |
| 117 | /// but note that it's ill-formed to find a non-tag. |
| 118 | IDNS_Type = 0x0004, |
| 119 | |
| 120 | /// Members, declared with object declarations within tag |
| 121 | /// definitions. In C, these can only be found by "qualified" |
| 122 | /// lookup in member expressions. In C++, they're found by |
| 123 | /// normal lookup. |
| 124 | IDNS_Member = 0x0008, |
| 125 | |
| 126 | /// Namespaces, declared with 'namespace foo {}'. |
| 127 | /// Lookup for nested-name-specifiers find these. |
| 128 | IDNS_Namespace = 0x0010, |
| 129 | |
| 130 | /// Ordinary names. In C, everything that's not a label, tag, |
| 131 | /// or member ends up here. |
| 132 | IDNS_Ordinary = 0x0020, |
| 133 | |
| 134 | /// Objective C @protocol. |
| 135 | IDNS_ObjCProtocol = 0x0040, |
| 136 | |
| 137 | /// This declaration is a friend function. A friend function |
| 138 | /// declaration is always in this namespace but may also be in |
| 139 | /// IDNS_Ordinary if it was previously declared. |
| 140 | IDNS_OrdinaryFriend = 0x0080, |
| 141 | |
| 142 | /// This declaration is a friend class. A friend class |
| 143 | /// declaration is always in this namespace but may also be in |
| 144 | /// IDNS_Tag|IDNS_Type if it was previously declared. |
| 145 | IDNS_TagFriend = 0x0100, |
| 146 | |
| 147 | /// This declaration is a using declaration. A using declaration |
| 148 | /// *introduces* a number of other declarations into the current |
| 149 | /// scope, and those declarations use the IDNS of their targets, |
| 150 | /// but the actual using declarations go in this namespace. |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 151 | IDNS_Using = 0x0200, |
| 152 | |
| 153 | /// This declaration is a C++ operator declared in a non-class |
| 154 | /// context. All such operators are also in IDNS_Ordinary. |
| 155 | /// C++ lexical operator lookup looks for these. |
| 156 | IDNS_NonMemberOperator = 0x0400 |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 157 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
John McCall | 09e2c52 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 159 | /// ObjCDeclQualifier - 'Qualifiers' written next to the return and |
| 160 | /// parameter types in method declarations. Other than remembering |
| 161 | /// them and mangling them into the method's signature string, these |
| 162 | /// are ignored by the compiler; they are consumed by certain |
| 163 | /// remote-messaging frameworks. |
| 164 | /// |
| 165 | /// in, inout, and out are mutually exclusive and apply only to |
| 166 | /// method parameters. bycopy and byref are mutually exclusive and |
| 167 | /// apply only to method parameters (?). oneway applies only to |
| 168 | /// results. All of these expect their corresponding parameter to |
| 169 | /// have a particular type. None of this is currently enforced by |
| 170 | /// clang. |
| 171 | /// |
| 172 | /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 173 | enum ObjCDeclQualifier { |
| 174 | OBJC_TQ_None = 0x0, |
| 175 | OBJC_TQ_In = 0x1, |
| 176 | OBJC_TQ_Inout = 0x2, |
| 177 | OBJC_TQ_Out = 0x4, |
| 178 | OBJC_TQ_Bycopy = 0x8, |
| 179 | OBJC_TQ_Byref = 0x10, |
| 180 | OBJC_TQ_Oneway = 0x20 |
| 181 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 183 | protected: |
| 184 | // Enumeration values used in the bits stored in NextInContextAndBits. |
| 185 | enum { |
| 186 | /// \brief Whether this declaration is a top-level declaration (function, |
| 187 | /// global variable, etc.) that is lexically inside an objc container |
| 188 | /// definition. |
| 189 | TopLevelDeclInObjCContainerFlag = 0x01, |
| 190 | |
| 191 | /// \brief Whether this declaration is private to the module in which it was |
| 192 | /// defined. |
| 193 | ModulePrivateFlag = 0x02 |
| 194 | }; |
| 195 | |
| 196 | /// \brief The next declaration within the same lexical |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 197 | /// DeclContext. These pointers form the linked list that is |
| 198 | /// traversed via DeclContext's decls_begin()/decls_end(). |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 199 | /// |
| 200 | /// The extra two bits are used for the TopLevelDeclInObjCContainer and |
| 201 | /// ModulePrivate bits. |
| 202 | llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 204 | private: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 205 | friend class DeclContext; |
| 206 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 207 | struct MultipleDC { |
| 208 | DeclContext *SemanticDC; |
| 209 | DeclContext *LexicalDC; |
| 210 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
| 212 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 213 | /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. |
| 214 | /// For declarations that don't contain C++ scope specifiers, it contains |
| 215 | /// the DeclContext where the Decl was declared. |
| 216 | /// For declarations with C++ scope specifiers, it contains a MultipleDC* |
| 217 | /// with the context where it semantically belongs (SemanticDC) and the |
| 218 | /// context where it was lexically declared (LexicalDC). |
| 219 | /// e.g.: |
| 220 | /// |
| 221 | /// namespace A { |
| 222 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 223 | /// } |
| 224 | /// void A::f(); // SemanticDC == namespace 'A' |
| 225 | /// // LexicalDC == global namespace |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 226 | llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 227 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 228 | inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } |
| 229 | inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 230 | inline MultipleDC *getMultipleDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 231 | return DeclCtx.get<MultipleDC*>(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 233 | inline DeclContext *getSemanticDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 234 | return DeclCtx.get<DeclContext*>(); |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 237 | /// Loc - The location of this decl. |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 238 | SourceLocation Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 240 | /// DeclKind - This indicates which class this is. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 241 | unsigned DeclKind : 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 243 | /// InvalidDecl - This indicates a semantic error occurred. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 244 | unsigned InvalidDecl : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 246 | /// HasAttrs - This indicates whether the decl has attributes or not. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 247 | unsigned HasAttrs : 1; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 249 | /// Implicit - Whether this declaration was implicitly generated by |
| 250 | /// the implementation rather than explicitly written by the user. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 251 | unsigned Implicit : 1; |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 253 | /// \brief Whether this declaration was "used", meaning that a definition is |
| 254 | /// required. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 255 | unsigned Used : 1; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 256 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 257 | /// \brief Whether this declaration was "referenced". |
| 258 | /// The difference with 'Used' is whether the reference appears in a |
| 259 | /// evaluated context or not, e.g. functions used in uninstantiated templates |
| 260 | /// are regarded as "referenced" but not "used". |
| 261 | unsigned Referenced : 1; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 263 | protected: |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 264 | /// Access - Used by C++ decls for the access specifier. |
| 265 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
| 266 | unsigned Access : 2; |
| 267 | friend class CXXClassMemberWrapper; |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | 08e0bc1 | 2011-09-10 00:09:20 +0000 | [diff] [blame] | 269 | /// \brief Whether this declaration was loaded from an AST file. |
| 270 | unsigned FromASTFile : 1; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | f143ffc | 2012-01-06 16:22:39 +0000 | [diff] [blame] | 272 | /// \brief Whether this declaration is hidden from normal name lookup, e.g., |
| 273 | /// because it is was loaded from an AST file is either module-private or |
| 274 | /// because its submodule has not been made visible. |
| 275 | unsigned Hidden : 1; |
| 276 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 277 | /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 278 | unsigned IdentifierNamespace : 12; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 280 | /// \brief Whether the \c CachedLinkage field is active. |
| 281 | /// |
| 282 | /// This field is only valid for NamedDecls subclasses. |
| 283 | mutable unsigned HasCachedLinkage : 1; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 285 | /// \brief If \c HasCachedLinkage, the linkage of this declaration. |
| 286 | /// |
| 287 | /// This field is only valid for NamedDecls subclasses. |
| 288 | mutable unsigned CachedLinkage : 2; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 289 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 290 | friend class ASTDeclWriter; |
| 291 | friend class ASTDeclReader; |
Douglas Gregor | ecc2c09 | 2011-12-01 22:20:10 +0000 | [diff] [blame] | 292 | friend class ASTReader; |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 293 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 294 | private: |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 295 | void CheckAccessDeclContext() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 297 | protected: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 298 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 300 | : NextInContextAndBits(), DeclCtx(DC), |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 301 | Loc(L), DeclKind(DK), InvalidDecl(0), |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 302 | HasAttrs(false), Implicit(false), Used(false), Referenced(false), |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 303 | Access(AS_none), FromASTFile(0), Hidden(0), |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 304 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 305 | HasCachedLinkage(0) |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 306 | { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 307 | if (Decl::CollectingStats()) add(DK); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 308 | } |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 309 | |
Argyrios Kyrtzidis | 6764334 | 2010-06-29 22:47:00 +0000 | [diff] [blame] | 310 | Decl(Kind DK, EmptyShell Empty) |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 311 | : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0), |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 312 | HasAttrs(false), Implicit(false), Used(false), Referenced(false), |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 313 | Access(AS_none), FromASTFile(0), Hidden(0), |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 314 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
| 315 | HasCachedLinkage(0) |
| 316 | { |
Argyrios Kyrtzidis | 6764334 | 2010-06-29 22:47:00 +0000 | [diff] [blame] | 317 | if (Decl::CollectingStats()) add(DK); |
| 318 | } |
| 319 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 320 | virtual ~Decl(); |
| 321 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 322 | /// \brief Allocate memory for a deserialized declaration. |
| 323 | /// |
| 324 | /// This routine must be used to allocate memory for any declaration that is |
| 325 | /// deserialized from a module file. |
| 326 | /// |
| 327 | /// \param Context The context in which we will allocate memory. |
| 328 | /// \param ID The global ID of the deserialized declaration. |
| 329 | /// \param Size The size of the allocated object. |
| 330 | static void *AllocateDeserializedDecl(const ASTContext &Context, |
| 331 | unsigned ID, |
| 332 | unsigned Size); |
| 333 | |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 334 | public: |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 335 | |
| 336 | /// \brief Source range that this declaration covers. |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 337 | virtual SourceRange getSourceRange() const { |
| 338 | return SourceRange(getLocation(), getLocation()); |
| 339 | } |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 340 | SourceLocation getLocStart() const { return getSourceRange().getBegin(); } |
| 341 | SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } |
| 342 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 343 | SourceLocation getLocation() const { return Loc; } |
| 344 | void setLocation(SourceLocation L) { Loc = L; } |
| 345 | |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 346 | Kind getKind() const { return static_cast<Kind>(DeclKind); } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 347 | const char *getDeclKindName() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 349 | Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); } |
| 350 | const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();} |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 352 | DeclContext *getDeclContext() { |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 353 | if (isInSemaDC()) |
| 354 | return getSemanticDC(); |
| 355 | return getMultipleDC()->SemanticDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 357 | const DeclContext *getDeclContext() const { |
| 358 | return const_cast<Decl*>(this)->getDeclContext(); |
| 359 | } |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 360 | |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 361 | /// Finds the innermost non-closure context of this declaration. |
| 362 | /// That is, walk out the DeclContext chain, skipping any blocks. |
| 363 | DeclContext *getNonClosureContext(); |
| 364 | const DeclContext *getNonClosureContext() const { |
| 365 | return const_cast<Decl*>(this)->getNonClosureContext(); |
| 366 | } |
| 367 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 368 | TranslationUnitDecl *getTranslationUnitDecl(); |
| 369 | const TranslationUnitDecl *getTranslationUnitDecl() const { |
| 370 | return const_cast<Decl*>(this)->getTranslationUnitDecl(); |
| 371 | } |
| 372 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 373 | bool isInAnonymousNamespace() const; |
| 374 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 375 | ASTContext &getASTContext() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 377 | void setAccess(AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | Access = AS; |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 379 | #ifndef NDEBUG |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 380 | CheckAccessDeclContext(); |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 381 | #endif |
Anders Carlsson | b8547e8 | 2009-03-25 20:19:57 +0000 | [diff] [blame] | 382 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
| 384 | AccessSpecifier getAccess() const { |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 385 | #ifndef NDEBUG |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 386 | CheckAccessDeclContext(); |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 387 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | return AccessSpecifier(Access); |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 389 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 391 | bool hasAttrs() const { return HasAttrs; } |
Argyrios Kyrtzidis | 4bbb850 | 2012-02-09 02:44:08 +0000 | [diff] [blame] | 392 | void setAttrs(const AttrVec& Attrs) { |
| 393 | return setAttrsImpl(Attrs, getASTContext()); |
| 394 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 395 | AttrVec &getAttrs() { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 396 | return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 397 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 398 | const AttrVec &getAttrs() const; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 399 | void swapAttrs(Decl *D); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 400 | void dropAttrs(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | b5f35ba | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 402 | void addAttr(Attr *A) { |
| 403 | if (hasAttrs()) |
| 404 | getAttrs().push_back(A); |
| 405 | else |
| 406 | setAttrs(AttrVec(1, A)); |
| 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 409 | typedef AttrVec::const_iterator attr_iterator; |
| 410 | |
| 411 | // FIXME: Do not rely on iterators having comparable singular values. |
| 412 | // Note that this should error out if they do not. |
| 413 | attr_iterator attr_begin() const { |
| 414 | return hasAttrs() ? getAttrs().begin() : 0; |
| 415 | } |
| 416 | attr_iterator attr_end() const { |
| 417 | return hasAttrs() ? getAttrs().end() : 0; |
| 418 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | c3ca14d | 2011-06-23 17:50:10 +0000 | [diff] [blame] | 420 | template <typename T> |
Fariborz Jahanian | 8cf0f52 | 2011-06-23 20:24:38 +0000 | [diff] [blame] | 421 | void dropAttr() { |
| 422 | if (!HasAttrs) return; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 423 | |
Fariborz Jahanian | 8cf0f52 | 2011-06-23 20:24:38 +0000 | [diff] [blame] | 424 | AttrVec &Attrs = getAttrs(); |
| 425 | for (unsigned i = 0, e = Attrs.size(); i != e; /* in loop */) { |
| 426 | if (isa<T>(Attrs[i])) { |
| 427 | Attrs.erase(Attrs.begin() + i); |
| 428 | --e; |
| 429 | } |
| 430 | else |
| 431 | ++i; |
| 432 | } |
| 433 | if (Attrs.empty()) |
| 434 | HasAttrs = false; |
| 435 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 436 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 437 | template <typename T> |
| 438 | specific_attr_iterator<T> specific_attr_begin() const { |
| 439 | return specific_attr_iterator<T>(attr_begin()); |
| 440 | } |
| 441 | template <typename T> |
| 442 | specific_attr_iterator<T> specific_attr_end() const { |
| 443 | return specific_attr_iterator<T>(attr_end()); |
| 444 | } |
| 445 | |
| 446 | template<typename T> T *getAttr() const { |
| 447 | return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0; |
| 448 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 449 | template<typename T> bool hasAttr() const { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 450 | return hasAttrs() && hasSpecificAttr<T>(getAttrs()); |
| 451 | } |
| 452 | |
| 453 | /// getMaxAlignment - return the maximum alignment specified by attributes |
| 454 | /// on this decl, 0 if there are none. |
| 455 | unsigned getMaxAlignment() const { |
| 456 | return hasAttrs() ? getMaxAttrAlignment(getAttrs(), getASTContext()) : 0; |
Chris Lattner | 115cafc | 2009-04-12 20:07:59 +0000 | [diff] [blame] | 457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 459 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
| 460 | /// allows for graceful error recovery. |
Douglas Gregor | 4273857 | 2010-03-05 00:26:45 +0000 | [diff] [blame] | 461 | void setInvalidDecl(bool Invalid = true); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 462 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 463 | |
| 464 | /// isImplicit - Indicates whether the declaration was implicitly |
| 465 | /// generated by the implementation. If false, this declaration |
| 466 | /// was written explicitly in the source code. |
| 467 | bool isImplicit() const { return Implicit; } |
| 468 | void setImplicit(bool I = true) { Implicit = I; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 470 | /// \brief Whether this declaration was used, meaning that a definition |
| 471 | /// is required. |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 472 | /// |
| 473 | /// \param CheckUsedAttr When true, also consider the "used" attribute |
| 474 | /// (in addition to the "used" bit set by \c setUsed()) when determining |
| 475 | /// whether the function is used. |
| 476 | bool isUsed(bool CheckUsedAttr = true) const; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 478 | void setUsed(bool U = true) { Used = U; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 480 | /// \brief Whether this declaration was referenced. |
| 481 | bool isReferenced() const; |
| 482 | |
| 483 | void setReferenced(bool R = true) { Referenced = R; } |
| 484 | |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 485 | /// \brief Whether this declaration is a top-level declaration (function, |
| 486 | /// global variable, etc.) that is lexically inside an objc container |
| 487 | /// definition. |
| 488 | bool isTopLevelDeclInObjCContainer() const { |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 489 | return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag; |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void setTopLevelDeclInObjCContainer(bool V = true) { |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 493 | unsigned Bits = NextInContextAndBits.getInt(); |
| 494 | if (V) |
| 495 | Bits |= TopLevelDeclInObjCContainerFlag; |
| 496 | else |
| 497 | Bits &= ~TopLevelDeclInObjCContainerFlag; |
| 498 | NextInContextAndBits.setInt(Bits); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 501 | protected: |
| 502 | /// \brief Whether this declaration was marked as being private to the |
| 503 | /// module in which it was defined. |
| 504 | bool isModulePrivate() const { |
| 505 | return NextInContextAndBits.getInt() & ModulePrivateFlag; |
| 506 | } |
| 507 | |
| 508 | /// \brief Specify whether this declaration was marked as being private |
| 509 | /// to the module in which it was defined. |
| 510 | void setModulePrivate(bool MP = true) { |
| 511 | unsigned Bits = NextInContextAndBits.getInt(); |
| 512 | if (MP) |
| 513 | Bits |= ModulePrivateFlag; |
| 514 | else |
| 515 | Bits &= ~ModulePrivateFlag; |
| 516 | NextInContextAndBits.setInt(Bits); |
| 517 | } |
| 518 | |
Douglas Gregor | c6c8e0e | 2012-01-09 17:30:44 +0000 | [diff] [blame] | 519 | /// \brief Set the owning module ID. |
| 520 | void setOwningModuleID(unsigned ID) { |
| 521 | assert(isFromASTFile() && "Only works on a deserialized declaration"); |
| 522 | *((unsigned*)this - 2) = ID; |
| 523 | } |
| 524 | |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 525 | public: |
| 526 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 527 | /// \brief Determine the availability of the given declaration. |
| 528 | /// |
| 529 | /// This routine will determine the most restrictive availability of |
| 530 | /// the given declaration (e.g., preferring 'unavailable' to |
| 531 | /// 'deprecated'). |
| 532 | /// |
| 533 | /// \param Message If non-NULL and the result is not \c |
| 534 | /// AR_Available, will be set to a (possibly empty) message |
| 535 | /// describing why the declaration has not been introduced, is |
| 536 | /// deprecated, or is unavailable. |
| 537 | AvailabilityResult getAvailability(std::string *Message = 0) const; |
| 538 | |
| 539 | /// \brief Determine whether this declaration is marked 'deprecated'. |
| 540 | /// |
| 541 | /// \param Message If non-NULL and the declaration is deprecated, |
| 542 | /// this will be set to the message describing why the declaration |
| 543 | /// was deprecated (which may be empty). |
| 544 | bool isDeprecated(std::string *Message = 0) const { |
| 545 | return getAvailability(Message) == AR_Deprecated; |
| 546 | } |
| 547 | |
| 548 | /// \brief Determine whether this declaration is marked 'unavailable'. |
| 549 | /// |
| 550 | /// \param Message If non-NULL and the declaration is unavailable, |
| 551 | /// this will be set to the message describing why the declaration |
| 552 | /// was made unavailable (which may be empty). |
| 553 | bool isUnavailable(std::string *Message = 0) const { |
| 554 | return getAvailability(Message) == AR_Unavailable; |
| 555 | } |
| 556 | |
| 557 | /// \brief Determine whether this is a weak-imported symbol. |
| 558 | /// |
| 559 | /// Weak-imported symbols are typically marked with the |
Eli Friedman | 7f3ad23 | 2011-03-28 02:00:21 +0000 | [diff] [blame] | 560 | /// 'weak_import' attribute, but may also be marked with an |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 561 | /// 'availability' attribute where we're targing a platform prior to |
| 562 | /// the introduction of this feature. |
| 563 | bool isWeakImported() const; |
| 564 | |
| 565 | /// \brief Determines whether this symbol can be weak-imported, |
| 566 | /// e.g., whether it would be well-formed to add the weak_import |
| 567 | /// attribute. |
| 568 | /// |
| 569 | /// \param IsDefinition Set to \c true to indicate that this |
| 570 | /// declaration cannot be weak-imported because it has a definition. |
| 571 | bool canBeWeakImported(bool &IsDefinition) const; |
| 572 | |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 573 | /// \brief Determine whether this declaration came from an AST file (such as |
| 574 | /// a precompiled header or module) rather than having been parsed. |
Douglas Gregor | 08e0bc1 | 2011-09-10 00:09:20 +0000 | [diff] [blame] | 575 | bool isFromASTFile() const { return FromASTFile; } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | b6b60c1 | 2012-01-05 22:27:05 +0000 | [diff] [blame] | 577 | /// \brief Retrieve the global declaration ID associated with this |
| 578 | /// declaration, which specifies where in the |
| 579 | unsigned getGlobalID() const { |
| 580 | if (isFromASTFile()) |
| 581 | return *((const unsigned*)this - 1); |
| 582 | return 0; |
| 583 | } |
Douglas Gregor | c6c8e0e | 2012-01-09 17:30:44 +0000 | [diff] [blame] | 584 | |
| 585 | /// \brief Retrieve the global ID of the module that owns this particular |
| 586 | /// declaration. |
| 587 | unsigned getOwningModuleID() const { |
| 588 | if (isFromASTFile()) |
| 589 | return *((const unsigned*)this - 2); |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 594 | unsigned getIdentifierNamespace() const { |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 595 | return IdentifierNamespace; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | d62fdc4 | 2009-01-06 07:16:40 +0000 | [diff] [blame] | 597 | bool isInIdentifierNamespace(unsigned NS) const { |
| 598 | return getIdentifierNamespace() & NS; |
| 599 | } |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 600 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
| 601 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 602 | bool hasTagIdentifierNamespace() const { |
| 603 | return isTagIdentifierNamespace(getIdentifierNamespace()); |
| 604 | } |
| 605 | static bool isTagIdentifierNamespace(unsigned NS) { |
| 606 | // TagDecls have Tag and Type set and may also have TagFriend. |
| 607 | return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); |
| 608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 610 | /// getLexicalDeclContext - The declaration context where this Decl was |
| 611 | /// lexically declared (LexicalDC). May be different from |
| 612 | /// getDeclContext() (SemanticDC). |
| 613 | /// e.g.: |
| 614 | /// |
| 615 | /// namespace A { |
| 616 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 617 | /// } |
| 618 | /// void A::f(); // SemanticDC == namespace 'A' |
| 619 | /// // LexicalDC == global namespace |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 620 | DeclContext *getLexicalDeclContext() { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 621 | if (isInSemaDC()) |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 622 | return getSemanticDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 623 | return getMultipleDC()->LexicalDC; |
| 624 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 625 | const DeclContext *getLexicalDeclContext() const { |
| 626 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 627 | } |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 628 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 629 | virtual bool isOutOfLine() const { |
| 630 | return getLexicalDeclContext() != getDeclContext(); |
| 631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 633 | /// setDeclContext - Set both the semantic and lexical DeclContext |
| 634 | /// to DC. |
| 635 | void setDeclContext(DeclContext *DC); |
| 636 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 637 | void setLexicalDeclContext(DeclContext *DC); |
| 638 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 639 | /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
| 640 | /// scoped decl is defined outside the current function or method. This is |
| 641 | /// roughly global variables and functions, but also handles enums (which |
| 642 | /// could be defined inside or outside a function etc). |
Argyrios Kyrtzidis | c8680f4 | 2011-09-28 02:45:33 +0000 | [diff] [blame] | 643 | bool isDefinedOutsideFunctionOrMethod() const { |
| 644 | return getParentFunctionOrMethod() == 0; |
| 645 | } |
| 646 | |
| 647 | /// \brief If this decl is defined inside a function/method/block it returns |
| 648 | /// the corresponding DeclContext, otherwise it returns null. |
| 649 | const DeclContext *getParentFunctionOrMethod() const; |
Argyrios Kyrtzidis | fcc1e50 | 2011-09-28 18:14:24 +0000 | [diff] [blame] | 650 | DeclContext *getParentFunctionOrMethod() { |
| 651 | return const_cast<DeclContext*>( |
| 652 | const_cast<const Decl*>(this)->getParentFunctionOrMethod()); |
| 653 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 654 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 655 | /// \brief Retrieves the "canonical" declaration of the given declaration. |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 656 | virtual Decl *getCanonicalDecl() { return this; } |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 657 | const Decl *getCanonicalDecl() const { |
| 658 | return const_cast<Decl*>(this)->getCanonicalDecl(); |
| 659 | } |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 660 | |
Argyrios Kyrtzidis | ac4e379 | 2009-07-18 08:50:48 +0000 | [diff] [blame] | 661 | /// \brief Whether this particular Decl is a canonical one. |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 662 | bool isCanonicalDecl() const { return getCanonicalDecl() == this; } |
Douglas Gregor | c896ad0 | 2011-12-14 21:44:45 +0000 | [diff] [blame] | 663 | |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 664 | protected: |
| 665 | /// \brief Returns the next redeclaration or itself if this is the only decl. |
| 666 | /// |
| 667 | /// Decl subclasses that can be redeclared should override this method so that |
| 668 | /// Decl::redecl_iterator can iterate over them. |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 669 | virtual Decl *getNextRedeclaration() { return this; } |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 671 | /// \brief Implementation of getPreviousDecl(), to be overridden by any |
| 672 | /// subclass that has a redeclaration chain. |
| 673 | virtual Decl *getPreviousDeclImpl() { return 0; } |
| 674 | |
| 675 | /// \brief Implementation of getMostRecentDecl(), to be overridden by any |
| 676 | /// subclass that has a redeclaration chain. |
| 677 | virtual Decl *getMostRecentDeclImpl() { return this; } |
| 678 | |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 679 | public: |
| 680 | /// \brief Iterates through all the redeclarations of the same decl. |
| 681 | class redecl_iterator { |
| 682 | /// Current - The current declaration. |
| 683 | Decl *Current; |
| 684 | Decl *Starter; |
| 685 | |
| 686 | public: |
| 687 | typedef Decl* value_type; |
| 688 | typedef Decl* reference; |
| 689 | typedef Decl* pointer; |
| 690 | typedef std::forward_iterator_tag iterator_category; |
| 691 | typedef std::ptrdiff_t difference_type; |
| 692 | |
| 693 | redecl_iterator() : Current(0) { } |
| 694 | explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } |
| 695 | |
| 696 | reference operator*() const { return Current; } |
| 697 | pointer operator->() const { return Current; } |
| 698 | |
| 699 | redecl_iterator& operator++() { |
| 700 | assert(Current && "Advancing while iterator has reached end"); |
| 701 | // Get either previous decl or latest decl. |
| 702 | Decl *Next = Current->getNextRedeclaration(); |
Argyrios Kyrtzidis | 22cbd2b | 2009-07-21 00:06:27 +0000 | [diff] [blame] | 703 | assert(Next && "Should return next redeclaration or itself, never null!"); |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 704 | Current = (Next != Starter ? Next : 0); |
| 705 | return *this; |
| 706 | } |
| 707 | |
| 708 | redecl_iterator operator++(int) { |
| 709 | redecl_iterator tmp(*this); |
| 710 | ++(*this); |
| 711 | return tmp; |
| 712 | } |
| 713 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | friend bool operator==(redecl_iterator x, redecl_iterator y) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 715 | return x.Current == y.Current; |
| 716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | friend bool operator!=(redecl_iterator x, redecl_iterator y) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 718 | return x.Current != y.Current; |
| 719 | } |
| 720 | }; |
| 721 | |
| 722 | /// \brief Returns iterator for all the redeclarations of the same decl. |
| 723 | /// It will iterate at least once (when this decl is the only one). |
| 724 | redecl_iterator redecls_begin() const { |
| 725 | return redecl_iterator(const_cast<Decl*>(this)); |
| 726 | } |
| 727 | redecl_iterator redecls_end() const { return redecl_iterator(); } |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 729 | /// \brief Retrieve the previous declaration that declares the same entity |
| 730 | /// as this declaration, or NULL if there is no previous declaration. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 731 | Decl *getPreviousDecl() { return getPreviousDeclImpl(); } |
| 732 | |
| 733 | /// \brief Retrieve the most recent declaration that declares the same entity |
| 734 | /// as this declaration, or NULL if there is no previous declaration. |
| 735 | const Decl *getPreviousDecl() const { |
| 736 | return const_cast<Decl *>(this)->getPreviousDeclImpl(); |
| 737 | } |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 738 | |
| 739 | /// \brief Retrieve the most recent declaration that declares the same entity |
| 740 | /// as this declaration (which may be this declaration). |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 741 | Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); } |
| 742 | |
| 743 | /// \brief Retrieve the most recent declaration that declares the same entity |
| 744 | /// as this declaration (which may be this declaration). |
| 745 | const Decl *getMostRecentDecl() const { |
| 746 | return const_cast<Decl *>(this)->getMostRecentDeclImpl(); |
| 747 | } |
| 748 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 749 | /// getBody - If this Decl represents a declaration for a body of code, |
| 750 | /// such as a function or method definition, this method returns the |
| 751 | /// top-level Stmt* of that body. Otherwise this method returns null. |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 752 | virtual Stmt* getBody() const { return 0; } |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 753 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 754 | /// \brief Returns true if this Decl represents a declaration for a body of |
| 755 | /// code, such as a function or method definition. |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 756 | virtual bool hasBody() const { return getBody() != 0; } |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 757 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 758 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
| 759 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 760 | SourceLocation getBodyRBrace() const; |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 762 | // global temp stats (until we have a per-module visitor) |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 763 | static void add(Kind k); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 764 | static bool CollectingStats(bool Enable = false); |
| 765 | static void PrintStats(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 767 | /// isTemplateParameter - Determines whether this declaration is a |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 768 | /// template parameter. |
| 769 | bool isTemplateParameter() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 771 | /// isTemplateParameter - Determines whether this declaration is a |
| 772 | /// template parameter pack. |
| 773 | bool isTemplateParameterPack() const; |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 775 | /// \brief Whether this declaration is a parameter pack. |
| 776 | bool isParameterPack() const; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 777 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 778 | /// \brief returns true if this declaration is a template |
| 779 | bool isTemplateDecl() const; |
| 780 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 781 | /// \brief Whether this declaration is a function or function template. |
| 782 | bool isFunctionOrFunctionTemplate() const; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 783 | |
| 784 | /// \brief Changes the namespace of this declaration to reflect that it's |
| 785 | /// the object of a friend declaration. |
| 786 | /// |
| 787 | /// These declarations appear in the lexical context of the friending |
| 788 | /// class, but in the semantic context of the actual entity. This property |
| 789 | /// applies only to a specific decl object; other redeclarations of the |
| 790 | /// same entity may not (and probably don't) share this property. |
| 791 | void setObjectOfFriendDecl(bool PreviouslyDeclared) { |
| 792 | unsigned OldNS = IdentifierNamespace; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 793 | assert((OldNS & (IDNS_Tag | IDNS_Ordinary | |
| 794 | IDNS_TagFriend | IDNS_OrdinaryFriend)) && |
| 795 | "namespace includes neither ordinary nor tag"); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 796 | assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 797 | IDNS_TagFriend | IDNS_OrdinaryFriend)) && |
| 798 | "namespace includes other than ordinary or tag"); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 799 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 800 | IdentifierNamespace = 0; |
| 801 | if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 802 | IdentifierNamespace |= IDNS_TagFriend; |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 803 | if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 807 | IdentifierNamespace |= IDNS_OrdinaryFriend; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 808 | if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary; |
| 809 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | enum FriendObjectKind { |
| 813 | FOK_None, // not a friend object |
| 814 | FOK_Declared, // a friend of a previously-declared entity |
| 815 | FOK_Undeclared // a friend of a previously-undeclared entity |
| 816 | }; |
| 817 | |
| 818 | /// \brief Determines whether this declaration is the object of a |
| 819 | /// friend declaration and, if so, what kind. |
| 820 | /// |
| 821 | /// There is currently no direct way to find the associated FriendDecl. |
| 822 | FriendObjectKind getFriendObjectKind() const { |
| 823 | unsigned mask |
| 824 | = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); |
| 825 | if (!mask) return FOK_None; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 826 | return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? |
Anders Carlsson | 255d641 | 2009-09-13 23:59:13 +0000 | [diff] [blame] | 827 | FOK_Declared : FOK_Undeclared); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 830 | /// Specifies that this declaration is a C++ overloaded non-member. |
| 831 | void setNonMemberOperator() { |
| 832 | assert(getKind() == Function || getKind() == FunctionTemplate); |
| 833 | assert((IdentifierNamespace & IDNS_Ordinary) && |
| 834 | "visible non-member operators should be in ordinary namespace"); |
| 835 | IdentifierNamespace |= IDNS_NonMemberOperator; |
| 836 | } |
| 837 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 838 | // Implement isa/cast/dyncast/etc. |
| 839 | static bool classof(const Decl *) { return true; } |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 840 | static bool classofKind(Kind K) { return true; } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 841 | static DeclContext *castToDeclContext(const Decl *); |
| 842 | static Decl *castFromDeclContext(const DeclContext *); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 844 | void print(raw_ostream &Out, unsigned Indentation = 0, |
| 845 | bool PrintInstantiation = false) const; |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 846 | void print(raw_ostream &Out, const PrintingPolicy &Policy, |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 847 | unsigned Indentation = 0, bool PrintInstantiation = false) const; |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 848 | static void printGroup(Decl** Begin, unsigned NumDecls, |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 849 | raw_ostream &Out, const PrintingPolicy &Policy, |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 850 | unsigned Indentation = 0); |
Argyrios Kyrtzidis | 0ee7d94 | 2012-02-21 05:04:44 +0000 | [diff] [blame^] | 851 | LLVM_ATTRIBUTE_USED void dump() const; |
| 852 | LLVM_ATTRIBUTE_USED void dumpXML() const; |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 853 | void dumpXML(raw_ostream &OS) const; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 855 | private: |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 856 | const Attr *getAttrsImpl() const; |
Argyrios Kyrtzidis | 4bbb850 | 2012-02-09 02:44:08 +0000 | [diff] [blame] | 857 | void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx); |
| 858 | void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
| 859 | ASTContext &Ctx); |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 860 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 861 | protected: |
| 862 | ASTMutationListener *getASTMutationListener() const; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 863 | }; |
| 864 | |
Douglas Gregor | eaa9511 | 2011-12-14 23:59:32 +0000 | [diff] [blame] | 865 | /// \brief Determine whether two declarations declare the same entity. |
| 866 | inline bool declaresSameEntity(const Decl *D1, const Decl *D2) { |
Douglas Gregor | eaa9511 | 2011-12-14 23:59:32 +0000 | [diff] [blame] | 867 | if (!D1 || !D2) |
| 868 | return false; |
| 869 | |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 870 | if (D1 == D2) |
| 871 | return true; |
| 872 | |
Douglas Gregor | eaa9511 | 2011-12-14 23:59:32 +0000 | [diff] [blame] | 873 | return D1->getCanonicalDecl() == D2->getCanonicalDecl(); |
| 874 | } |
| 875 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 876 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
| 877 | /// doing something to a specific decl. |
| 878 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 879 | const Decl *TheDecl; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 880 | SourceLocation Loc; |
| 881 | SourceManager &SM; |
| 882 | const char *Message; |
| 883 | public: |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 884 | PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 885 | SourceManager &sm, const char *Msg) |
| 886 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 888 | virtual void print(raw_ostream &OS) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | }; |
| 890 | |
John McCall | 6d9496d | 2010-08-26 09:52:08 +0000 | [diff] [blame] | 891 | class DeclContextLookupResult |
| 892 | : public std::pair<NamedDecl**,NamedDecl**> { |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 893 | public: |
John McCall | 6d9496d | 2010-08-26 09:52:08 +0000 | [diff] [blame] | 894 | DeclContextLookupResult(NamedDecl **I, NamedDecl **E) |
| 895 | : std::pair<NamedDecl**,NamedDecl**>(I, E) {} |
| 896 | DeclContextLookupResult() |
| 897 | : std::pair<NamedDecl**,NamedDecl**>() {} |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 898 | |
John McCall | f29815a | 2010-08-26 10:20:09 +0000 | [diff] [blame] | 899 | using std::pair<NamedDecl**,NamedDecl**>::operator=; |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 900 | }; |
| 901 | |
| 902 | class DeclContextLookupConstResult |
| 903 | : public std::pair<NamedDecl*const*, NamedDecl*const*> { |
| 904 | public: |
| 905 | DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R) |
John McCall | 6d9496d | 2010-08-26 09:52:08 +0000 | [diff] [blame] | 906 | : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {} |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 907 | DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E) |
John McCall | 6d9496d | 2010-08-26 09:52:08 +0000 | [diff] [blame] | 908 | : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {} |
| 909 | DeclContextLookupConstResult() |
| 910 | : std::pair<NamedDecl*const*, NamedDecl*const*>() {} |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 911 | |
John McCall | f29815a | 2010-08-26 10:20:09 +0000 | [diff] [blame] | 912 | using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=; |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 913 | }; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 914 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 915 | /// DeclContext - This is used only as base class of specific decl types that |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 916 | /// can act as declaration contexts. These decls are (only the top classes |
| 917 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 918 | /// |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 919 | /// TranslationUnitDecl |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 920 | /// NamespaceDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 921 | /// FunctionDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 922 | /// TagDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 923 | /// ObjCMethodDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 924 | /// ObjCContainerDecl |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 925 | /// LinkageSpecDecl |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 926 | /// BlockDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 927 | /// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 928 | class DeclContext { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 929 | /// DeclKind - This indicates which class this is. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 930 | unsigned DeclKind : 8; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 932 | /// \brief Whether this declaration context also has some external |
| 933 | /// storage that contains additional declarations that are lexically |
| 934 | /// part of this context. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 935 | mutable unsigned ExternalLexicalStorage : 1; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 936 | |
| 937 | /// \brief Whether this declaration context also has some external |
| 938 | /// storage that contains additional declarations that are visible |
| 939 | /// in this context. |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 940 | mutable unsigned ExternalVisibleStorage : 1; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 942 | /// \brief Pointer to the data structure used to lookup declarations |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 943 | /// within this context (or a DependentStoredDeclsMap if this is a |
| 944 | /// dependent context). |
| 945 | mutable StoredDeclsMap *LookupPtr; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 946 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 947 | protected: |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 948 | /// FirstDecl - The first declaration stored within this declaration |
| 949 | /// context. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 950 | mutable Decl *FirstDecl; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 951 | |
| 952 | /// LastDecl - The last declaration stored within this declaration |
| 953 | /// context. FIXME: We could probably cache this value somewhere |
| 954 | /// outside of the DeclContext, to reduce the size of DeclContext by |
| 955 | /// another pointer. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 956 | mutable Decl *LastDecl; |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 957 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 958 | friend class ExternalASTSource; |
| 959 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 960 | /// \brief Build up a chain of declarations. |
| 961 | /// |
| 962 | /// \returns the first/last pair of declarations. |
| 963 | static std::pair<Decl *, Decl *> |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 964 | BuildDeclChain(const SmallVectorImpl<Decl*> &Decls, bool FieldsAlreadyLoaded); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 965 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | DeclContext(Decl::Kind K) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 967 | : DeclKind(K), ExternalLexicalStorage(false), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 969 | LastDecl(0) { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 971 | public: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 972 | ~DeclContext(); |
| 973 | |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 974 | Decl::Kind getDeclKind() const { |
John McCall | 35043e5 | 2010-10-19 05:43:52 +0000 | [diff] [blame] | 975 | return static_cast<Decl::Kind>(DeclKind); |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 976 | } |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 977 | const char *getDeclKindName() const; |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 978 | |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 979 | /// getParent - Returns the containing DeclContext. |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 980 | DeclContext *getParent() { |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 981 | return cast<Decl>(this)->getDeclContext(); |
| 982 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 983 | const DeclContext *getParent() const { |
| 984 | return const_cast<DeclContext*>(this)->getParent(); |
Argyrios Kyrtzidis | d2595ec | 2008-10-12 18:40:01 +0000 | [diff] [blame] | 985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 987 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 988 | /// different from getParent, e.g.: |
| 989 | /// |
| 990 | /// namespace A { |
| 991 | /// struct S; |
| 992 | /// } |
| 993 | /// struct A::S {}; // getParent() == namespace 'A' |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 994 | /// // getLexicalParent() == translation unit |
| 995 | /// |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 996 | DeclContext *getLexicalParent() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 997 | return cast<Decl>(this)->getLexicalDeclContext(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 998 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 999 | const DeclContext *getLexicalParent() const { |
| 1000 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | } |
Argyrios Kyrtzidis | 048f30a | 2009-06-30 02:35:38 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 1003 | DeclContext *getLookupParent(); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 1005 | const DeclContext *getLookupParent() const { |
| 1006 | return const_cast<DeclContext*>(this)->getLookupParent(); |
| 1007 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1008 | |
Argyrios Kyrtzidis | 048f30a | 2009-06-30 02:35:38 +0000 | [diff] [blame] | 1009 | ASTContext &getParentASTContext() const { |
| 1010 | return cast<Decl>(this)->getASTContext(); |
| 1011 | } |
| 1012 | |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 1013 | bool isClosure() const { |
| 1014 | return DeclKind == Decl::Block; |
| 1015 | } |
| 1016 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1017 | bool isObjCContainer() const { |
Fariborz Jahanian | 80f7753 | 2011-08-22 17:13:51 +0000 | [diff] [blame] | 1018 | switch (DeclKind) { |
| 1019 | case Decl::ObjCCategory: |
| 1020 | case Decl::ObjCCategoryImpl: |
| 1021 | case Decl::ObjCImplementation: |
| 1022 | case Decl::ObjCInterface: |
| 1023 | case Decl::ObjCProtocol: |
| 1024 | return true; |
| 1025 | } |
| 1026 | return false; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1029 | bool isFunctionOrMethod() const { |
| 1030 | switch (DeclKind) { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 1031 | case Decl::Block: |
| 1032 | case Decl::ObjCMethod: |
| 1033 | return true; |
| 1034 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 1035 | return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1039 | bool isFileContext() const { |
| 1040 | return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; |
| 1041 | } |
| 1042 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1043 | bool isTranslationUnit() const { |
| 1044 | return DeclKind == Decl::TranslationUnit; |
| 1045 | } |
| 1046 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1047 | bool isRecord() const { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 1048 | return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord; |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1051 | bool isNamespace() const { |
| 1052 | return DeclKind == Decl::Namespace; |
| 1053 | } |
| 1054 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1055 | bool isInlineNamespace() const; |
| 1056 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 1057 | /// \brief Determines whether this context is dependent on a |
| 1058 | /// template parameter. |
| 1059 | bool isDependentContext() const; |
| 1060 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1061 | /// isTransparentContext - Determines whether this context is a |
| 1062 | /// "transparent" context, meaning that the members declared in this |
| 1063 | /// context are semantically declared in the nearest enclosing |
| 1064 | /// non-transparent (opaque) context but are lexically declared in |
| 1065 | /// this context. For example, consider the enumerators of an |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | /// enumeration type: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1067 | /// @code |
| 1068 | /// enum E { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | /// Val1 |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1070 | /// }; |
| 1071 | /// @endcode |
| 1072 | /// Here, E is a transparent context, so its enumerator (Val1) will |
| 1073 | /// appear (semantically) that it is in the same context of E. |
| 1074 | /// Examples of transparent contexts include: enumerations (except for |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1075 | /// C++0x scoped enums), and C++ linkage specifications. |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1076 | bool isTransparentContext() const; |
| 1077 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 1078 | /// \brief Determines whether this context is, or is nested within, |
| 1079 | /// a C++ extern "C" linkage spec. |
| 1080 | bool isExternCContext() const; |
| 1081 | |
Douglas Gregor | 61481da | 2009-09-01 17:22:34 +0000 | [diff] [blame] | 1082 | /// \brief Determine whether this declaration context is equivalent |
| 1083 | /// to the declaration context DC. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1084 | bool Equals(const DeclContext *DC) const { |
Douglas Gregor | dbdf5e7 | 2010-03-15 15:26:48 +0000 | [diff] [blame] | 1085 | return DC && this->getPrimaryContext() == DC->getPrimaryContext(); |
Douglas Gregor | 61481da | 2009-09-01 17:22:34 +0000 | [diff] [blame] | 1086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 1088 | /// \brief Determine whether this declaration context encloses the |
| 1089 | /// declaration context DC. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1090 | bool Encloses(const DeclContext *DC) const; |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1091 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1092 | /// \brief Find the nearest non-closure ancestor of this context, |
| 1093 | /// i.e. the innermost semantic parent of this context which is not |
| 1094 | /// a closure. A context may be its own non-closure ancestor. |
| 1095 | DeclContext *getNonClosureAncestor(); |
| 1096 | const DeclContext *getNonClosureAncestor() const { |
| 1097 | return const_cast<DeclContext*>(this)->getNonClosureAncestor(); |
| 1098 | } |
| 1099 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1100 | /// getPrimaryContext - There may be many different |
| 1101 | /// declarations of the same entity (including forward declarations |
| 1102 | /// of classes, multiple definitions of namespaces, etc.), each with |
| 1103 | /// a different set of declarations. This routine returns the |
| 1104 | /// "primary" DeclContext structure, which will contain the |
| 1105 | /// information needed to perform name lookup into this context. |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1106 | DeclContext *getPrimaryContext(); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1107 | const DeclContext *getPrimaryContext() const { |
| 1108 | return const_cast<DeclContext*>(this)->getPrimaryContext(); |
| 1109 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1110 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1111 | /// getRedeclContext - Retrieve the context in which an entity conflicts with |
| 1112 | /// other entities of the same name, or where it is a redeclaration if the |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1113 | /// two entities are compatible. This skips through transparent contexts. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1114 | DeclContext *getRedeclContext(); |
| 1115 | const DeclContext *getRedeclContext() const { |
| 1116 | return const_cast<DeclContext *>(this)->getRedeclContext(); |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 1117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1119 | /// \brief Retrieve the nearest enclosing namespace context. |
| 1120 | DeclContext *getEnclosingNamespaceContext(); |
| 1121 | const DeclContext *getEnclosingNamespaceContext() const { |
| 1122 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
| 1123 | } |
| 1124 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1125 | /// \brief Test if this context is part of the enclosing namespace set of |
| 1126 | /// the context NS, as defined in C++0x [namespace.def]p9. If either context |
| 1127 | /// isn't a namespace, this is equivalent to Equals(). |
| 1128 | /// |
| 1129 | /// The enclosing namespace set of a namespace is the namespace and, if it is |
| 1130 | /// inline, its enclosing namespace, recursively. |
| 1131 | bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; |
| 1132 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 1133 | /// \\brief Collects all of the declaration contexts that are semantically |
| 1134 | /// connected to this declaration context. |
| 1135 | /// |
| 1136 | /// For declaration contexts that have multiple semantically connected but |
| 1137 | /// syntactically distinct contexts, such as C++ namespaces, this routine |
| 1138 | /// retrieves the complete set of such declaration contexts in source order. |
| 1139 | /// For example, given: |
| 1140 | /// |
| 1141 | /// \code |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1142 | /// namespace N { |
| 1143 | /// int x; |
| 1144 | /// } |
| 1145 | /// namespace N { |
| 1146 | /// int y; |
| 1147 | /// } |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 1148 | /// \endcode |
| 1149 | /// |
| 1150 | /// The \c Contexts parameter will contain both definitions of N. |
| 1151 | /// |
| 1152 | /// \param Contexts Will be cleared and set to the set of declaration |
| 1153 | /// contexts that are semanticaly connected to this declaration context, |
| 1154 | /// in source order, including this context (which may be the only result, |
| 1155 | /// for non-namespace contexts). |
| 1156 | void collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1157 | |
| 1158 | /// decl_iterator - Iterates through the declarations stored |
| 1159 | /// within this context. |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1160 | class decl_iterator { |
| 1161 | /// Current - The current declaration. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1162 | Decl *Current; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1163 | |
| 1164 | public: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1165 | typedef Decl* value_type; |
| 1166 | typedef Decl* reference; |
| 1167 | typedef Decl* pointer; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1168 | typedef std::forward_iterator_tag iterator_category; |
| 1169 | typedef std::ptrdiff_t difference_type; |
| 1170 | |
| 1171 | decl_iterator() : Current(0) { } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1172 | explicit decl_iterator(Decl *C) : Current(C) { } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1173 | |
| 1174 | reference operator*() const { return Current; } |
| 1175 | pointer operator->() const { return Current; } |
| 1176 | |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 1177 | decl_iterator& operator++() { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 1178 | Current = Current->getNextDeclInContext(); |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 1179 | return *this; |
| 1180 | } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1181 | |
| 1182 | decl_iterator operator++(int) { |
| 1183 | decl_iterator tmp(*this); |
| 1184 | ++(*this); |
| 1185 | return tmp; |
| 1186 | } |
| 1187 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | friend bool operator==(decl_iterator x, decl_iterator y) { |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1189 | return x.Current == y.Current; |
| 1190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1192 | return x.Current != y.Current; |
| 1193 | } |
| 1194 | }; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1196 | /// decls_begin/decls_end - Iterate over the declarations stored in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | /// this context. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1198 | decl_iterator decls_begin() const; |
| 1199 | decl_iterator decls_end() const; |
| 1200 | bool decls_empty() const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1201 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1202 | /// noload_decls_begin/end - Iterate over the declarations stored in this |
| 1203 | /// context that are currently loaded; don't attempt to retrieve anything |
| 1204 | /// from an external source. |
| 1205 | decl_iterator noload_decls_begin() const; |
| 1206 | decl_iterator noload_decls_end() const; |
| 1207 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1208 | /// specific_decl_iterator - Iterates over a subrange of |
| 1209 | /// declarations stored in a DeclContext, providing only those that |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1210 | /// are of type SpecificDecl (or a class derived from it). This |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1211 | /// iterator is used, for example, to provide iteration over just |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1212 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1213 | template<typename SpecificDecl> |
| 1214 | class specific_decl_iterator { |
| 1215 | /// Current - The current, underlying declaration iterator, which |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 1216 | /// will either be NULL or will point to a declaration of |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1217 | /// type SpecificDecl. |
| 1218 | DeclContext::decl_iterator Current; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1220 | /// SkipToNextDecl - Advances the current position up to the next |
| 1221 | /// declaration of type SpecificDecl that also meets the criteria |
| 1222 | /// required by Acceptable. |
| 1223 | void SkipToNextDecl() { |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1224 | while (*Current && !isa<SpecificDecl>(*Current)) |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1225 | ++Current; |
| 1226 | } |
| 1227 | |
| 1228 | public: |
| 1229 | typedef SpecificDecl* value_type; |
| 1230 | typedef SpecificDecl* reference; |
| 1231 | typedef SpecificDecl* pointer; |
| 1232 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 1233 | difference_type; |
| 1234 | typedef std::forward_iterator_tag iterator_category; |
| 1235 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1236 | specific_decl_iterator() : Current() { } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1237 | |
| 1238 | /// specific_decl_iterator - Construct a new iterator over a |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 1239 | /// subset of the declarations the range [C, |
| 1240 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 1241 | /// member function of SpecificDecl that should return true for |
| 1242 | /// all of the SpecificDecl instances that will be in the subset |
| 1243 | /// of iterators. For example, if you want Objective-C instance |
| 1244 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 1245 | /// &ObjCMethodDecl::isInstanceMethod. |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1246 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1247 | SkipToNextDecl(); |
| 1248 | } |
| 1249 | |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1250 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 1251 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1252 | |
| 1253 | specific_decl_iterator& operator++() { |
| 1254 | ++Current; |
| 1255 | SkipToNextDecl(); |
| 1256 | return *this; |
| 1257 | } |
| 1258 | |
| 1259 | specific_decl_iterator operator++(int) { |
| 1260 | specific_decl_iterator tmp(*this); |
| 1261 | ++(*this); |
| 1262 | return tmp; |
| 1263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1265 | friend bool operator==(const specific_decl_iterator& x, |
| 1266 | const specific_decl_iterator& y) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1267 | return x.Current == y.Current; |
| 1268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1270 | friend bool operator!=(const specific_decl_iterator& x, |
| 1271 | const specific_decl_iterator& y) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1272 | return x.Current != y.Current; |
| 1273 | } |
| 1274 | }; |
| 1275 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1276 | /// \brief Iterates over a filtered subrange of declarations stored |
| 1277 | /// in a DeclContext. |
| 1278 | /// |
| 1279 | /// This iterator visits only those declarations that are of type |
| 1280 | /// SpecificDecl (or a class derived from it) and that meet some |
| 1281 | /// additional run-time criteria. This iterator is used, for |
| 1282 | /// example, to provide access to the instance methods within an |
| 1283 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
| 1284 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
| 1285 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
| 1286 | class filtered_decl_iterator { |
| 1287 | /// Current - The current, underlying declaration iterator, which |
| 1288 | /// will either be NULL or will point to a declaration of |
| 1289 | /// type SpecificDecl. |
| 1290 | DeclContext::decl_iterator Current; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1292 | /// SkipToNextDecl - Advances the current position up to the next |
| 1293 | /// declaration of type SpecificDecl that also meets the criteria |
| 1294 | /// required by Acceptable. |
| 1295 | void SkipToNextDecl() { |
| 1296 | while (*Current && |
| 1297 | (!isa<SpecificDecl>(*Current) || |
| 1298 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
| 1299 | ++Current; |
| 1300 | } |
| 1301 | |
| 1302 | public: |
| 1303 | typedef SpecificDecl* value_type; |
| 1304 | typedef SpecificDecl* reference; |
| 1305 | typedef SpecificDecl* pointer; |
| 1306 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 1307 | difference_type; |
| 1308 | typedef std::forward_iterator_tag iterator_category; |
| 1309 | |
| 1310 | filtered_decl_iterator() : Current() { } |
| 1311 | |
| 1312 | /// specific_decl_iterator - Construct a new iterator over a |
| 1313 | /// subset of the declarations the range [C, |
| 1314 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 1315 | /// member function of SpecificDecl that should return true for |
| 1316 | /// all of the SpecificDecl instances that will be in the subset |
| 1317 | /// of iterators. For example, if you want Objective-C instance |
| 1318 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 1319 | /// &ObjCMethodDecl::isInstanceMethod. |
| 1320 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
| 1321 | SkipToNextDecl(); |
| 1322 | } |
| 1323 | |
| 1324 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 1325 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
| 1326 | |
| 1327 | filtered_decl_iterator& operator++() { |
| 1328 | ++Current; |
| 1329 | SkipToNextDecl(); |
| 1330 | return *this; |
| 1331 | } |
| 1332 | |
| 1333 | filtered_decl_iterator operator++(int) { |
| 1334 | filtered_decl_iterator tmp(*this); |
| 1335 | ++(*this); |
| 1336 | return tmp; |
| 1337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1339 | friend bool operator==(const filtered_decl_iterator& x, |
| 1340 | const filtered_decl_iterator& y) { |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1341 | return x.Current == y.Current; |
| 1342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1344 | friend bool operator!=(const filtered_decl_iterator& x, |
| 1345 | const filtered_decl_iterator& y) { |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 1346 | return x.Current != y.Current; |
| 1347 | } |
| 1348 | }; |
| 1349 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 1350 | /// @brief Add the declaration D into this context. |
| 1351 | /// |
| 1352 | /// This routine should be invoked when the declaration D has first |
| 1353 | /// been declared, to place D into the context where it was |
| 1354 | /// (lexically) defined. Every declaration must be added to one |
| 1355 | /// (and only one!) context, where it can be visited via |
| 1356 | /// [decls_begin(), decls_end()). Once a declaration has been added |
| 1357 | /// to its lexical context, the corresponding DeclContext owns the |
| 1358 | /// declaration. |
| 1359 | /// |
| 1360 | /// If D is also a NamedDecl, it will be made visible within its |
| 1361 | /// semantic context via makeDeclVisibleInContext. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1362 | void addDecl(Decl *D); |
Sean Callanan | 381509e | 2011-10-21 16:15:18 +0000 | [diff] [blame] | 1363 | |
| 1364 | /// @brief Add the declaration D into this context, but suppress |
| 1365 | /// searches for external declarations with the same name. |
| 1366 | /// |
| 1367 | /// Although analogous in function to addDecl, this removes an |
| 1368 | /// important check. This is only useful if the Decl is being |
| 1369 | /// added in response to an external search; in all other cases, |
| 1370 | /// addDecl() is the right function to use. |
| 1371 | /// See the ASTImporter for use cases. |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1372 | void addDeclInternal(Decl *D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1373 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1374 | /// @brief Add the declaration D to this context without modifying |
| 1375 | /// any lookup tables. |
| 1376 | /// |
| 1377 | /// This is useful for some operations in dependent contexts where |
| 1378 | /// the semantic context might not be dependent; this basically |
| 1379 | /// only happens with friends. |
| 1380 | void addHiddenDecl(Decl *D); |
| 1381 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1382 | /// @brief Removes a declaration from this context. |
| 1383 | void removeDecl(Decl *D); |
| 1384 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1385 | /// lookup_iterator - An iterator that provides access to the results |
| 1386 | /// of looking up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1387 | typedef NamedDecl **lookup_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1388 | |
| 1389 | /// lookup_const_iterator - An iterator that provides non-mutable |
| 1390 | /// access to the results of lookup up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1391 | typedef NamedDecl * const * lookup_const_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1392 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 1393 | typedef DeclContextLookupResult lookup_result; |
| 1394 | typedef DeclContextLookupConstResult lookup_const_result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1395 | |
| 1396 | /// lookup - Find the declarations (if any) with the given Name in |
| 1397 | /// this context. Returns a range of iterators that contains all of |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 1398 | /// the declarations with this name, with object, function, member, |
| 1399 | /// and enumerator names preceding any tag name. Note that this |
| 1400 | /// routine will not look into parent contexts. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1401 | lookup_result lookup(DeclarationName Name); |
| 1402 | lookup_const_result lookup(DeclarationName Name) const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | b75a345 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 1404 | /// \brief A simplistic name lookup mechanism that performs name lookup |
| 1405 | /// into this declaration context without consulting the external source. |
| 1406 | /// |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1407 | /// This function should almost never be used, because it subverts the |
Douglas Gregor | b75a345 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 1408 | /// usual relationship between a DeclContext and the external source. |
| 1409 | /// See the ASTImporter for the (few, but important) use cases. |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1410 | void localUncachedLookup(DeclarationName Name, |
Douglas Gregor | b75a345 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 1411 | llvm::SmallVectorImpl<NamedDecl *> &Results); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 1413 | /// @brief Makes a declaration visible within this context. |
| 1414 | /// |
| 1415 | /// This routine makes the declaration D visible to name lookup |
| 1416 | /// within this context and, if this is a transparent context, |
| 1417 | /// within its parent contexts up to the first enclosing |
| 1418 | /// non-transparent context. Making a declaration visible within a |
| 1419 | /// context does not transfer ownership of a declaration, and a |
| 1420 | /// declaration can be visible in many contexts that aren't its |
| 1421 | /// lexical context. |
| 1422 | /// |
| 1423 | /// If D is a redeclaration of an existing declaration that is |
| 1424 | /// visible from this context, as determined by |
| 1425 | /// NamedDecl::declarationReplaces, the previous declaration will be |
| 1426 | /// replaced with D. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1427 | /// |
| 1428 | /// @param Recoverable true if it's okay to not add this decl to |
| 1429 | /// the lookup tables because it can be easily recovered by walking |
| 1430 | /// the declaration chains. |
| 1431 | void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1432 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1433 | /// udir_iterator - Iterates through the using-directives stored |
| 1434 | /// within this context. |
| 1435 | typedef UsingDirectiveDecl * const * udir_iterator; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1437 | typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; |
| 1438 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1439 | udir_iterator_range getUsingDirectives() const; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1440 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1441 | udir_iterator using_directives_begin() const { |
| 1442 | return getUsingDirectives().first; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1445 | udir_iterator using_directives_end() const { |
| 1446 | return getUsingDirectives().second; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1449 | // These are all defined in DependentDiagnostic.h. |
| 1450 | class ddiag_iterator; |
| 1451 | inline ddiag_iterator ddiag_begin() const; |
| 1452 | inline ddiag_iterator ddiag_end() const; |
| 1453 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 1454 | // Low-level accessors |
| 1455 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 1456 | /// \brief Retrieve the internal representation of the lookup structure. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1457 | StoredDeclsMap* getLookupPtr() const { return LookupPtr; } |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1459 | /// \brief Whether this DeclContext has external storage containing |
| 1460 | /// additional declarations that are lexically in this context. |
| 1461 | bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } |
| 1462 | |
| 1463 | /// \brief State whether this DeclContext has external storage for |
| 1464 | /// declarations lexically in this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | void setHasExternalLexicalStorage(bool ES = true) { |
| 1466 | ExternalLexicalStorage = ES; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /// \brief Whether this DeclContext has external storage containing |
| 1470 | /// additional declarations that are visible in this context. |
| 1471 | bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } |
| 1472 | |
| 1473 | /// \brief State whether this DeclContext has external storage for |
| 1474 | /// declarations visible in this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | void setHasExternalVisibleStorage(bool ES = true) { |
| 1476 | ExternalVisibleStorage = ES; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Douglas Gregor | 2ea054f | 2011-08-26 22:04:51 +0000 | [diff] [blame] | 1479 | /// \brief Determine whether the given declaration is stored in the list of |
| 1480 | /// declarations lexically within this context. |
| 1481 | bool isDeclInLexicalTraversal(const Decl *D) const { |
Douglas Gregor | 46cd218 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 1482 | return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl || |
| 1483 | D == LastDecl); |
Douglas Gregor | 2ea054f | 2011-08-26 22:04:51 +0000 | [diff] [blame] | 1484 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1485 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 1486 | static bool classof(const Decl *D); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1487 | static bool classof(const DeclContext *D) { return true; } |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 1488 | #define DECL(NAME, BASE) |
| 1489 | #define DECL_CONTEXT(NAME) \ |
| 1490 | static bool classof(const NAME##Decl *D) { return true; } |
| 1491 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1492 | |
Argyrios Kyrtzidis | 0ee7d94 | 2012-02-21 05:04:44 +0000 | [diff] [blame^] | 1493 | LLVM_ATTRIBUTE_USED void dumpDeclContext() const; |
Anders Carlsson | 2b7d8dd | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 1494 | |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1495 | private: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1496 | void LoadLexicalDeclsFromExternalStorage() const; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1497 | |
| 1498 | /// @brief Makes a declaration visible within this context, but |
Sean Callanan | 381509e | 2011-10-21 16:15:18 +0000 | [diff] [blame] | 1499 | /// suppresses searches for external declarations with the same |
| 1500 | /// name. |
| 1501 | /// |
| 1502 | /// Analogous to makeDeclVisibleInContext, but for the exclusive |
| 1503 | /// use of addDeclInternal(). |
| 1504 | void makeDeclVisibleInContextInternal(NamedDecl *D, |
| 1505 | bool Recoverable = true); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1506 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1507 | friend class DependentDiagnostic; |
| 1508 | StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; |
| 1509 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1510 | void buildLookup(DeclContext *DCtx); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1511 | void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
| 1512 | bool Recoverable); |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1513 | void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1514 | }; |
| 1515 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1516 | inline bool Decl::isTemplateParameter() const { |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1517 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || |
| 1518 | getKind() == TemplateTemplateParm; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1521 | // Specialization selected when ToTy is not a known subclass of DeclContext. |
| 1522 | template <class ToTy, |
| 1523 | bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value> |
| 1524 | struct cast_convert_decl_context { |
| 1525 | static const ToTy *doit(const DeclContext *Val) { |
| 1526 | return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); |
| 1527 | } |
| 1528 | |
| 1529 | static ToTy *doit(DeclContext *Val) { |
| 1530 | return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); |
| 1531 | } |
| 1532 | }; |
| 1533 | |
| 1534 | // Specialization selected when ToTy is a known subclass of DeclContext. |
| 1535 | template <class ToTy> |
| 1536 | struct cast_convert_decl_context<ToTy, true> { |
| 1537 | static const ToTy *doit(const DeclContext *Val) { |
| 1538 | return static_cast<const ToTy*>(Val); |
| 1539 | } |
| 1540 | |
| 1541 | static ToTy *doit(DeclContext *Val) { |
| 1542 | return static_cast<ToTy*>(Val); |
| 1543 | } |
| 1544 | }; |
| 1545 | |
| 1546 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1547 | } // end clang. |
| 1548 | |
| 1549 | namespace llvm { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1550 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1551 | /// isa<T>(DeclContext*) |
Eli Friedman | 4d50934 | 2011-05-21 19:15:39 +0000 | [diff] [blame] | 1552 | template <typename To> |
| 1553 | struct isa_impl<To, ::clang::DeclContext> { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1554 | static bool doit(const ::clang::DeclContext &Val) { |
Eli Friedman | 4d50934 | 2011-05-21 19:15:39 +0000 | [diff] [blame] | 1555 | return To::classofKind(Val.getDeclKind()); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1556 | } |
| 1557 | }; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1558 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1559 | /// cast<T>(DeclContext*) |
| 1560 | template<class ToTy> |
| 1561 | struct cast_convert_val<ToTy, |
| 1562 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 1563 | static const ToTy &doit(const ::clang::DeclContext &Val) { |
| 1564 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 1565 | } |
| 1566 | }; |
| 1567 | template<class ToTy> |
| 1568 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { |
| 1569 | static ToTy &doit(::clang::DeclContext &Val) { |
| 1570 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 1571 | } |
| 1572 | }; |
| 1573 | template<class ToTy> |
| 1574 | struct cast_convert_val<ToTy, |
| 1575 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
| 1576 | static const ToTy *doit(const ::clang::DeclContext *Val) { |
| 1577 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 1578 | } |
| 1579 | }; |
| 1580 | template<class ToTy> |
| 1581 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { |
| 1582 | static ToTy *doit(::clang::DeclContext *Val) { |
| 1583 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 1584 | } |
| 1585 | }; |
| 1586 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1587 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1588 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1589 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1590 | static ::clang::DeclContext &doit(const FromTy &Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1591 | return *FromTy::castToDeclContext(&Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1592 | } |
| 1593 | }; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1594 | |
| 1595 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1596 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1597 | static ::clang::DeclContext *doit(const FromTy *Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1598 | return FromTy::castToDeclContext(Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1599 | } |
| 1600 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1602 | template<class FromTy> |
| 1603 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
| 1604 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
| 1605 | return *FromTy::castToDeclContext(&Val); |
| 1606 | } |
| 1607 | }; |
| 1608 | |
| 1609 | template<class FromTy> |
| 1610 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
| 1611 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
| 1612 | return FromTy::castToDeclContext(Val); |
| 1613 | } |
| 1614 | }; |
| 1615 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1616 | } // end namespace llvm |
| 1617 | |
| 1618 | #endif |