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