Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 1 | //===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl and DeclContext classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclBase.h" |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | 2951e14 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 24 | #include "clang/AST/Type.h" |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 27 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | c25d8a7 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 32 | #include <cstdio> |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Statistics |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 39 | #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0; |
| 40 | #define ABSTRACT_DECL(DECL) |
| 41 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 42 | |
| 43 | static bool StatSwitch = false; |
| 44 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 45 | const char *Decl::getDeclKindName() const { |
| 46 | switch (DeclKind) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 47 | default: assert(0 && "Declaration not in DeclNodes.inc!"); |
| 48 | #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED; |
| 49 | #define ABSTRACT_DECL(DECL) |
| 50 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Douglas Gregor | 90d4717 | 2010-03-05 00:26:45 +0000 | [diff] [blame] | 54 | void Decl::setInvalidDecl(bool Invalid) { |
| 55 | InvalidDecl = Invalid; |
| 56 | if (Invalid) { |
| 57 | // Defensive maneuver for ill-formed code: we're likely not to make it to |
| 58 | // a point where we set the access specifier, so default it to "public" |
| 59 | // to avoid triggering asserts elsewhere in the front end. |
| 60 | setAccess(AS_public); |
| 61 | } |
| 62 | } |
| 63 | |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 64 | const char *DeclContext::getDeclKindName() const { |
| 65 | switch (DeclKind) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 66 | default: assert(0 && "Declaration context not in DeclNodes.inc!"); |
| 67 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED; |
| 68 | #define ABSTRACT_DECL(DECL) |
| 69 | #include "clang/AST/DeclNodes.inc" |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 73 | bool Decl::CollectingStats(bool Enable) { |
Kovarththanan Rajaratnam | 130f7f9 | 2009-11-29 14:54:35 +0000 | [diff] [blame] | 74 | if (Enable) StatSwitch = true; |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 75 | return StatSwitch; |
| 76 | } |
| 77 | |
| 78 | void Decl::PrintStats() { |
| 79 | fprintf(stderr, "*** Decl Stats:\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 81 | int totalDecls = 0; |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 82 | #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s; |
| 83 | #define ABSTRACT_DECL(DECL) |
| 84 | #include "clang/AST/DeclNodes.inc" |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 85 | fprintf(stderr, " %d decls total.\n", totalDecls); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 87 | int totalBytes = 0; |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 88 | #define DECL(DERIVED, BASE) \ |
| 89 | if (n##DERIVED##s > 0) { \ |
| 90 | totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \ |
| 91 | fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \ |
| 92 | n##DERIVED##s, (int)sizeof(DERIVED##Decl), \ |
| 93 | (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \ |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 94 | } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 95 | #define ABSTRACT_DECL(DECL) |
| 96 | #include "clang/AST/DeclNodes.inc" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 8bd3c2e | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 98 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 101 | void Decl::add(Kind k) { |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 102 | switch (k) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 103 | default: assert(0 && "Declaration not in DeclNodes.inc!"); |
| 104 | #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break; |
| 105 | #define ABSTRACT_DECL(DECL) |
| 106 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Anders Carlsson | aa73b91 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 110 | bool Decl::isTemplateParameterPack() const { |
| 111 | if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) |
| 112 | return TTP->isParameterPack(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 113 | if (const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 114 | = dyn_cast<NonTypeTemplateParmDecl>(this)) |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 115 | return NTTP->isParameterPack(); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 116 | if (const TemplateTemplateParmDecl *TTP |
| 117 | = dyn_cast<TemplateTemplateParmDecl>(this)) |
| 118 | return TTP->isParameterPack(); |
Anders Carlsson | aa73b91 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Douglas Gregor | 3c6bd2a | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 122 | bool Decl::isParameterPack() const { |
| 123 | if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this)) |
| 124 | return Parm->isParameterPack(); |
| 125 | |
| 126 | return isTemplateParameterPack(); |
| 127 | } |
| 128 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 129 | bool Decl::isFunctionOrFunctionTemplate() const { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 130 | if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this)) |
Anders Carlsson | f057cb2 | 2009-06-26 05:26:50 +0000 | [diff] [blame] | 131 | return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 133 | return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); |
| 134 | } |
| 135 | |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 136 | bool Decl::isDefinedOutsideFunctionOrMethod() const { |
| 137 | for (const DeclContext *DC = getDeclContext(); |
| 138 | DC && !DC->isTranslationUnit(); |
| 139 | DC = DC->getParent()) |
| 140 | if (DC->isFunctionOrMethod()) |
| 141 | return false; |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Douglas Gregor | 133eddd | 2011-02-17 08:47:29 +0000 | [diff] [blame] | 146 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 148 | // PrettyStackTraceDecl Implementation |
| 149 | //===----------------------------------------------------------------------===// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 151 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 152 | SourceLocation TheLoc = Loc; |
| 153 | if (TheLoc.isInvalid() && TheDecl) |
| 154 | TheLoc = TheDecl->getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 156 | if (TheLoc.isValid()) { |
| 157 | TheLoc.print(OS, SM); |
| 158 | OS << ": "; |
| 159 | } |
| 160 | |
| 161 | OS << Message; |
| 162 | |
Daniel Dunbar | 4f1054e | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 163 | if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 164 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 165 | OS << '\n'; |
| 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 169 | // Decl Implementation |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 172 | // Out-of-line virtual method providing a home for Decl. |
| 173 | Decl::~Decl() { } |
Douglas Gregor | a43942a | 2011-02-17 07:02:32 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 175 | void Decl::setDeclContext(DeclContext *DC) { |
Chris Lattner | b81eb05 | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 176 | DeclCtx = DC; |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 180 | if (DC == getLexicalDeclContext()) |
| 181 | return; |
| 182 | |
| 183 | if (isInSemaDC()) { |
Ted Kremenek | f8c12a3 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 184 | MultipleDC *MDC = new (getASTContext()) MultipleDC(); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 185 | MDC->SemanticDC = getDeclContext(); |
| 186 | MDC->LexicalDC = DC; |
Chris Lattner | b81eb05 | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 187 | DeclCtx = MDC; |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 188 | } else { |
| 189 | getMultipleDC()->LexicalDC = DC; |
| 190 | } |
| 191 | } |
| 192 | |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 193 | bool Decl::isInAnonymousNamespace() const { |
| 194 | const DeclContext *DC = getDeclContext(); |
| 195 | do { |
| 196 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) |
| 197 | if (ND->isAnonymousNamespace()) |
| 198 | return true; |
| 199 | } while ((DC = DC->getParent())); |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 204 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argyrios Kyrtzidis | 4e1a72b | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 205 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 206 | return TUD; |
| 207 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 208 | DeclContext *DC = getDeclContext(); |
| 209 | assert(DC && "This decl is not contained in a translation unit!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 211 | while (!DC->isTranslationUnit()) { |
| 212 | DC = DC->getParent(); |
| 213 | assert(DC && "This decl is not contained in a translation unit!"); |
| 214 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 216 | return cast<TranslationUnitDecl>(DC); |
| 217 | } |
| 218 | |
| 219 | ASTContext &Decl::getASTContext() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | return getTranslationUnitDecl()->getASTContext(); |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 223 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 224 | return getASTContext().getASTMutationListener(); |
| 225 | } |
| 226 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 227 | bool Decl::isUsed(bool CheckUsedAttr) const { |
Tanya Lattner | 8aefcbe | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 228 | if (Used) |
| 229 | return true; |
| 230 | |
| 231 | // Check for used attribute. |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 232 | if (CheckUsedAttr && hasAttr<UsedAttr>()) |
Tanya Lattner | 8aefcbe | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 233 | return true; |
| 234 | |
| 235 | // Check redeclarations for used attribute. |
| 236 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 237 | if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used) |
Tanya Lattner | 8aefcbe | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 244 | bool Decl::isReferenced() const { |
| 245 | if (Referenced) |
| 246 | return true; |
| 247 | |
| 248 | // Check redeclarations. |
| 249 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 250 | if (I->Referenced) |
| 251 | return true; |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 256 | /// \brief Determine the availability of the given declaration based on |
| 257 | /// the target platform. |
| 258 | /// |
| 259 | /// When it returns an availability result other than \c AR_Available, |
| 260 | /// if the \p Message parameter is non-NULL, it will be set to a |
| 261 | /// string describing why the entity is unavailable. |
| 262 | /// |
| 263 | /// FIXME: Make these strings localizable, since they end up in |
| 264 | /// diagnostics. |
| 265 | static AvailabilityResult CheckAvailability(ASTContext &Context, |
| 266 | const AvailabilityAttr *A, |
| 267 | std::string *Message) { |
| 268 | llvm::StringRef TargetPlatform = Context.Target.getPlatformName(); |
| 269 | llvm::StringRef PrettyPlatformName |
| 270 | = AvailabilityAttr::getPrettyPlatformName(TargetPlatform); |
| 271 | if (PrettyPlatformName.empty()) |
| 272 | PrettyPlatformName = TargetPlatform; |
| 273 | |
| 274 | VersionTuple TargetMinVersion = Context.Target.getPlatformMinVersion(); |
| 275 | if (TargetMinVersion.empty()) |
| 276 | return AR_Available; |
| 277 | |
| 278 | // Match the platform name. |
| 279 | if (A->getPlatform()->getName() != TargetPlatform) |
| 280 | return AR_Available; |
| 281 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 282 | // Make sure that this declaration has not been marked 'unavailable'. |
| 283 | if (A->getUnavailable()) { |
| 284 | if (Message) { |
| 285 | Message->clear(); |
| 286 | llvm::raw_string_ostream Out(*Message); |
| 287 | Out << "not available on " << PrettyPlatformName; |
| 288 | } |
| 289 | |
| 290 | return AR_Unavailable; |
| 291 | } |
| 292 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 293 | // Make sure that this declaration has already been introduced. |
| 294 | if (!A->getIntroduced().empty() && |
| 295 | TargetMinVersion < A->getIntroduced()) { |
| 296 | if (Message) { |
| 297 | Message->clear(); |
| 298 | llvm::raw_string_ostream Out(*Message); |
| 299 | Out << "introduced in " << PrettyPlatformName << ' ' |
| 300 | << A->getIntroduced(); |
| 301 | } |
| 302 | |
| 303 | return AR_NotYetIntroduced; |
| 304 | } |
| 305 | |
| 306 | // Make sure that this declaration hasn't been obsoleted. |
| 307 | if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) { |
| 308 | if (Message) { |
| 309 | Message->clear(); |
| 310 | llvm::raw_string_ostream Out(*Message); |
| 311 | Out << "obsoleted in " << PrettyPlatformName << ' ' |
| 312 | << A->getObsoleted(); |
| 313 | } |
| 314 | |
| 315 | return AR_Unavailable; |
| 316 | } |
| 317 | |
| 318 | // Make sure that this declaration hasn't been deprecated. |
| 319 | if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) { |
| 320 | if (Message) { |
| 321 | Message->clear(); |
| 322 | llvm::raw_string_ostream Out(*Message); |
| 323 | Out << "first deprecated in " << PrettyPlatformName << ' ' |
| 324 | << A->getDeprecated(); |
| 325 | } |
| 326 | |
| 327 | return AR_Deprecated; |
| 328 | } |
| 329 | |
| 330 | return AR_Available; |
| 331 | } |
| 332 | |
| 333 | AvailabilityResult Decl::getAvailability(std::string *Message) const { |
| 334 | AvailabilityResult Result = AR_Available; |
| 335 | std::string ResultMessage; |
| 336 | |
| 337 | for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) { |
| 338 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) { |
| 339 | if (Result >= AR_Deprecated) |
| 340 | continue; |
| 341 | |
| 342 | if (Message) |
| 343 | ResultMessage = Deprecated->getMessage(); |
| 344 | |
| 345 | Result = AR_Deprecated; |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) { |
| 350 | if (Message) |
| 351 | *Message = Unavailable->getMessage(); |
| 352 | return AR_Unavailable; |
| 353 | } |
| 354 | |
| 355 | if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) { |
| 356 | AvailabilityResult AR = CheckAvailability(getASTContext(), Availability, |
| 357 | Message); |
| 358 | |
| 359 | if (AR == AR_Unavailable) |
| 360 | return AR_Unavailable; |
| 361 | |
| 362 | if (AR > Result) { |
| 363 | Result = AR; |
| 364 | if (Message) |
| 365 | ResultMessage.swap(*Message); |
| 366 | } |
| 367 | continue; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (Message) |
| 372 | Message->swap(ResultMessage); |
| 373 | return Result; |
| 374 | } |
| 375 | |
| 376 | bool Decl::canBeWeakImported(bool &IsDefinition) const { |
| 377 | IsDefinition = false; |
| 378 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) { |
| 379 | if (!Var->hasExternalStorage() || Var->getInit()) { |
| 380 | IsDefinition = true; |
| 381 | return false; |
| 382 | } |
| 383 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 384 | if (FD->hasBody()) { |
| 385 | IsDefinition = true; |
| 386 | return false; |
| 387 | } |
| 388 | } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this)) |
| 389 | return false; |
| 390 | else if (!(getASTContext().getLangOptions().ObjCNonFragileABI && |
| 391 | isa<ObjCInterfaceDecl>(this))) |
| 392 | return false; |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | bool Decl::isWeakImported() const { |
| 398 | bool IsDefinition; |
| 399 | if (!canBeWeakImported(IsDefinition)) |
| 400 | return false; |
| 401 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 402 | for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) { |
| 403 | if (isa<WeakImportAttr>(*A)) |
| 404 | return true; |
| 405 | |
| 406 | if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) { |
| 407 | if (CheckAvailability(getASTContext(), Availability, 0) |
| 408 | == AR_NotYetIntroduced) |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return false; |
| 414 | } |
Tanya Lattner | 8aefcbe | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 416 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 417 | switch (DeclKind) { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 418 | case Function: |
| 419 | case CXXMethod: |
| 420 | case CXXConstructor: |
| 421 | case CXXDestructor: |
| 422 | case CXXConversion: |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 423 | case EnumConstant: |
| 424 | case Var: |
| 425 | case ImplicitParam: |
| 426 | case ParmVar: |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 427 | case NonTypeTemplateParm: |
| 428 | case ObjCMethod: |
Daniel Dunbar | 45b2d8a | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 429 | case ObjCProperty: |
Daniel Dunbar | 45b2d8a | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 430 | return IDNS_Ordinary; |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 431 | case Label: |
| 432 | return IDNS_Label; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 433 | case IndirectField: |
| 434 | return IDNS_Ordinary | IDNS_Member; |
| 435 | |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 436 | case ObjCCompatibleAlias: |
| 437 | case ObjCInterface: |
| 438 | return IDNS_Ordinary | IDNS_Type; |
| 439 | |
| 440 | case Typedef: |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 441 | case TypeAlias: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 442 | case UnresolvedUsingTypename: |
| 443 | case TemplateTypeParm: |
| 444 | return IDNS_Ordinary | IDNS_Type; |
| 445 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 446 | case UsingShadow: |
| 447 | return 0; // we'll actually overwrite this later |
| 448 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 449 | case UnresolvedUsingValue: |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 450 | return IDNS_Ordinary | IDNS_Using; |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 451 | |
| 452 | case Using: |
| 453 | return IDNS_Using; |
| 454 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 455 | case ObjCProtocol: |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 456 | return IDNS_ObjCProtocol; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 458 | case Field: |
| 459 | case ObjCAtDefsField: |
| 460 | case ObjCIvar: |
| 461 | return IDNS_Member; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 463 | case Record: |
| 464 | case CXXRecord: |
| 465 | case Enum: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 466 | return IDNS_Tag | IDNS_Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 468 | case Namespace: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 469 | case NamespaceAlias: |
| 470 | return IDNS_Namespace; |
| 471 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 472 | case FunctionTemplate: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 473 | return IDNS_Ordinary; |
| 474 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 475 | case ClassTemplate: |
| 476 | case TemplateTemplateParm: |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 477 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 479 | // Never have names. |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 480 | case Friend: |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 481 | case FriendTemplate: |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 482 | case AccessSpec: |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 483 | case LinkageSpec: |
| 484 | case FileScopeAsm: |
| 485 | case StaticAssert: |
| 486 | case ObjCClass: |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 487 | case ObjCPropertyImpl: |
| 488 | case ObjCForwardProtocol: |
| 489 | case Block: |
| 490 | case TranslationUnit: |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 492 | case UsingDirective: |
| 493 | case ClassTemplateSpecialization: |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 494 | case ClassTemplatePartialSpecialization: |
Douglas Gregor | e93525e | 2010-04-22 23:19:50 +0000 | [diff] [blame] | 495 | case ObjCImplementation: |
| 496 | case ObjCCategory: |
| 497 | case ObjCCategoryImpl: |
| 498 | // Never looked up by name. |
Chris Lattner | 8e09719 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 501 | |
| 502 | return 0; |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 505 | void Decl::setAttrs(const AttrVec &attrs) { |
Argyrios Kyrtzidis | 9116717 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 506 | assert(!HasAttrs && "Decl already contains attrs."); |
| 507 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 508 | AttrVec &AttrBlank = getASTContext().getDeclAttrs(this); |
| 509 | assert(AttrBlank.empty() && "HasAttrs was wrong?"); |
Argyrios Kyrtzidis | 9116717 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 510 | |
| 511 | AttrBlank = attrs; |
| 512 | HasAttrs = true; |
| 513 | } |
| 514 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 515 | void Decl::dropAttrs() { |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 516 | if (!HasAttrs) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 518 | HasAttrs = false; |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 519 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 522 | const AttrVec &Decl::getAttrs() const { |
| 523 | assert(HasAttrs && "No attrs to get!"); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 524 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 527 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 528 | bool HasLHSAttr = this->HasAttrs; |
| 529 | bool HasRHSAttr = RHS->HasAttrs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 531 | // Usually, neither decl has attrs, nothing to do. |
| 532 | if (!HasLHSAttr && !HasRHSAttr) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 534 | // If 'this' has no attrs, swap the other way. |
| 535 | if (!HasLHSAttr) |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 536 | return RHS->swapAttrs(this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 538 | ASTContext &Context = getASTContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 540 | // Handle the case when both decls have attrs. |
| 541 | if (HasRHSAttr) { |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 542 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 546 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 547 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 548 | Context.eraseDeclAttrs(this); |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 549 | this->HasAttrs = false; |
| 550 | RHS->HasAttrs = true; |
| 551 | } |
| 552 | |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 553 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 554 | Decl::Kind DK = D->getDeclKind(); |
| 555 | switch(DK) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 556 | #define DECL(NAME, BASE) |
| 557 | #define DECL_CONTEXT(NAME) \ |
| 558 | case Decl::NAME: \ |
| 559 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 560 | #define DECL_CONTEXT_BASE(NAME) |
| 561 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 562 | default: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 563 | #define DECL(NAME, BASE) |
| 564 | #define DECL_CONTEXT_BASE(NAME) \ |
| 565 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 566 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 567 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 568 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 569 | return 0; |
| 570 | } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 574 | Decl::Kind DK = D->getKind(); |
| 575 | switch(DK) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 576 | #define DECL(NAME, BASE) |
| 577 | #define DECL_CONTEXT(NAME) \ |
| 578 | case Decl::NAME: \ |
| 579 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 580 | #define DECL_CONTEXT_BASE(NAME) |
| 581 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 582 | default: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 583 | #define DECL(NAME, BASE) |
| 584 | #define DECL_CONTEXT_BASE(NAME) \ |
| 585 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 586 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 587 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 588 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 589 | return 0; |
| 590 | } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 593 | SourceLocation Decl::getBodyRBrace() const { |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 594 | // Special handling of FunctionDecl to avoid de-serializing the body from PCH. |
| 595 | // FunctionDecl stores EndRangeLoc for this purpose. |
| 596 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 597 | const FunctionDecl *Definition; |
| 598 | if (FD->hasBody(Definition)) |
| 599 | return Definition->getSourceRange().getEnd(); |
| 600 | return SourceLocation(); |
| 601 | } |
| 602 | |
Argyrios Kyrtzidis | 6fbc8fa | 2010-07-07 11:31:27 +0000 | [diff] [blame] | 603 | if (Stmt *Body = getBody()) |
| 604 | return Body->getSourceRange().getEnd(); |
| 605 | |
| 606 | return SourceLocation(); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Anders Carlsson | a28908d | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 609 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | 4b00d3b | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 610 | #ifndef NDEBUG |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 611 | // Suppress this check if any of the following hold: |
| 612 | // 1. this is the translation unit (and thus has no parent) |
| 613 | // 2. this is a template parameter (and thus doesn't belong to its context) |
Argyrios Kyrtzidis | e177863 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 614 | // 3. this is a non-type template parameter |
| 615 | // 4. the context is not a record |
| 616 | // 5. it's invalid |
| 617 | // 6. it's a C++0x static_assert. |
Anders Carlsson | adf36b2 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 618 | if (isa<TranslationUnitDecl>(this) || |
Argyrios Kyrtzidis | a45855f | 2010-07-02 11:55:44 +0000 | [diff] [blame] | 619 | isa<TemplateTypeParmDecl>(this) || |
Argyrios Kyrtzidis | e177863 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 620 | isa<NonTypeTemplateParmDecl>(this) || |
Douglas Gregor | 2b76dd9 | 2010-02-22 17:53:38 +0000 | [diff] [blame] | 621 | !isa<CXXRecordDecl>(getDeclContext()) || |
Argyrios Kyrtzidis | 260b4a8 | 2010-09-08 21:32:35 +0000 | [diff] [blame] | 622 | isInvalidDecl() || |
| 623 | isa<StaticAssertDecl>(this) || |
| 624 | // FIXME: a ParmVarDecl can have ClassTemplateSpecialization |
| 625 | // as DeclContext (?). |
Argyrios Kyrtzidis | e177863 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 626 | isa<ParmVarDecl>(this) || |
| 627 | // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have |
| 628 | // AS_none as access specifier. |
| 629 | isa<CXXRecordDecl>(this)) |
Anders Carlsson | adf36b2 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 630 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
| 632 | assert(Access != AS_none && |
Anders Carlsson | a28908d | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 633 | "Access specifier is AS_none inside a record decl"); |
Douglas Gregor | 4b00d3b | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 634 | #endif |
Anders Carlsson | a28908d | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 635 | } |
| 636 | |
John McCall | b67608f | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 637 | DeclContext *Decl::getNonClosureContext() { |
| 638 | DeclContext *DC = getDeclContext(); |
| 639 | |
| 640 | // This is basically "while (DC->isClosure()) DC = DC->getParent();" |
| 641 | // except that it's significantly more efficient to cast to a known |
| 642 | // decl type and call getDeclContext() than to call getParent(). |
| 643 | do { |
| 644 | if (isa<BlockDecl>(DC)) { |
| 645 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 646 | continue; |
| 647 | } |
| 648 | } while (false); |
| 649 | |
| 650 | assert(!DC->isClosure()); |
| 651 | return DC; |
| 652 | } |
Anders Carlsson | a28908d | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 653 | |
Eli Friedman | 7dbab8a | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 654 | //===----------------------------------------------------------------------===// |
| 655 | // DeclContext Implementation |
| 656 | //===----------------------------------------------------------------------===// |
| 657 | |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 658 | bool DeclContext::classof(const Decl *D) { |
| 659 | switch (D->getKind()) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 660 | #define DECL(NAME, BASE) |
| 661 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 662 | #define DECL_CONTEXT_BASE(NAME) |
| 663 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 664 | return true; |
| 665 | default: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 666 | #define DECL(NAME, BASE) |
| 667 | #define DECL_CONTEXT_BASE(NAME) \ |
| 668 | if (D->getKind() >= Decl::first##NAME && \ |
| 669 | D->getKind() <= Decl::last##NAME) \ |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 670 | return true; |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 671 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | afe24c8 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | |
Douglas Gregor | 9c832f7 | 2010-07-25 18:38:02 +0000 | [diff] [blame] | 676 | DeclContext::~DeclContext() { } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 7f737c0 | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 678 | /// \brief Find the parent context of this context that will be |
| 679 | /// used for unqualified name lookup. |
| 680 | /// |
| 681 | /// Generally, the parent lookup context is the semantic context. However, for |
| 682 | /// a friend function the parent lookup context is the lexical context, which |
| 683 | /// is the class in which the friend is declared. |
| 684 | DeclContext *DeclContext::getLookupParent() { |
| 685 | // FIXME: Find a better way to identify friends |
| 686 | if (isa<FunctionDecl>(this)) |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 687 | if (getParent()->getRedeclContext()->isFileContext() && |
| 688 | getLexicalParent()->getRedeclContext()->isRecord()) |
Douglas Gregor | 7f737c0 | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 689 | return getLexicalParent(); |
| 690 | |
| 691 | return getParent(); |
| 692 | } |
| 693 | |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 694 | bool DeclContext::isInlineNamespace() const { |
| 695 | return isNamespace() && |
| 696 | cast<NamespaceDecl>(this)->isInline(); |
| 697 | } |
| 698 | |
Douglas Gregor | 9e927ab | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 699 | bool DeclContext::isDependentContext() const { |
| 700 | if (isFileContext()) |
| 701 | return false; |
| 702 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 703 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 704 | return true; |
| 705 | |
Douglas Gregor | 9e927ab | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 706 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 707 | if (Record->getDescribedClassTemplate()) |
| 708 | return true; |
| 709 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 710 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | 9e927ab | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 711 | if (Function->getDescribedFunctionTemplate()) |
| 712 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 714 | // Friend function declarations are dependent if their *lexical* |
| 715 | // context is dependent. |
| 716 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 717 | return getLexicalParent()->isDependentContext(); |
| 718 | } |
| 719 | |
Douglas Gregor | 9e927ab | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 720 | return getParent() && getParent()->isDependentContext(); |
| 721 | } |
| 722 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 723 | bool DeclContext::isTransparentContext() const { |
| 724 | if (DeclKind == Decl::Enum) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 725 | return !cast<EnumDecl>(this)->isScoped(); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 726 | else if (DeclKind == Decl::LinkageSpec) |
| 727 | return true; |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 728 | |
| 729 | return false; |
| 730 | } |
| 731 | |
John McCall | 5fe8412 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 732 | bool DeclContext::isExternCContext() const { |
| 733 | const DeclContext *DC = this; |
| 734 | while (DC->DeclKind != Decl::TranslationUnit) { |
| 735 | if (DC->DeclKind == Decl::LinkageSpec) |
| 736 | return cast<LinkageSpecDecl>(DC)->getLanguage() |
| 737 | == LinkageSpecDecl::lang_c; |
| 738 | DC = DC->getParent(); |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 743 | bool DeclContext::Encloses(const DeclContext *DC) const { |
Douglas Gregor | e985a3b | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 744 | if (getPrimaryContext() != this) |
| 745 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | e985a3b | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 747 | for (; DC; DC = DC->getParent()) |
| 748 | if (DC->getPrimaryContext() == this) |
| 749 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | return false; |
Douglas Gregor | e985a3b | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 753 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 754 | switch (DeclKind) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 755 | case Decl::TranslationUnit: |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 756 | case Decl::LinkageSpec: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | case Decl::Block: |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 758 | // There is only one DeclContext for these entities. |
| 759 | return this; |
| 760 | |
| 761 | case Decl::Namespace: |
| 762 | // The original namespace is our primary context. |
| 763 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 764 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 765 | case Decl::ObjCMethod: |
| 766 | return this; |
| 767 | |
| 768 | case Decl::ObjCInterface: |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 769 | case Decl::ObjCProtocol: |
| 770 | case Decl::ObjCCategory: |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 771 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 772 | return this; |
| 773 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 774 | case Decl::ObjCImplementation: |
| 775 | case Decl::ObjCCategoryImpl: |
| 776 | return this; |
| 777 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 778 | default: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 779 | if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 780 | // If this is a tag type that has a definition or is currently |
| 781 | // being defined, that definition is our primary context. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 782 | TagDecl *Tag = cast<TagDecl>(this); |
| 783 | assert(isa<TagType>(Tag->TypeForDecl) || |
| 784 | isa<InjectedClassNameType>(Tag->TypeForDecl)); |
| 785 | |
| 786 | if (TagDecl *Def = Tag->getDefinition()) |
| 787 | return Def; |
| 788 | |
| 789 | if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) { |
| 790 | const TagType *TagTy = cast<TagType>(Tag->TypeForDecl); |
| 791 | if (TagTy->isBeingDefined()) |
| 792 | // FIXME: is it necessarily being defined in the decl |
| 793 | // that owns the type? |
| 794 | return TagTy->getDecl(); |
| 795 | } |
| 796 | |
| 797 | return Tag; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 800 | assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 801 | "Unknown DeclContext kind"); |
| 802 | return this; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | DeclContext *DeclContext::getNextContext() { |
| 807 | switch (DeclKind) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 808 | case Decl::Namespace: |
| 809 | // Return the next namespace |
| 810 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 811 | |
| 812 | default: |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | } |
| 816 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 817 | std::pair<Decl *, Decl *> |
| 818 | DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) { |
| 819 | // Build up a chain of declarations via the Decl::NextDeclInContext field. |
| 820 | Decl *FirstNewDecl = 0; |
| 821 | Decl *PrevDecl = 0; |
| 822 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 823 | Decl *D = Decls[I]; |
| 824 | if (PrevDecl) |
| 825 | PrevDecl->NextDeclInContext = D; |
| 826 | else |
| 827 | FirstNewDecl = D; |
| 828 | |
| 829 | PrevDecl = D; |
| 830 | } |
| 831 | |
| 832 | return std::make_pair(FirstNewDecl, PrevDecl); |
| 833 | } |
| 834 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 835 | /// \brief Load the declarations within this lexical storage from an |
| 836 | /// external source. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | void |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 838 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 839 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 840 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 841 | |
Argyrios Kyrtzidis | 98d045e | 2010-07-30 10:03:23 +0000 | [diff] [blame] | 842 | // Notify that we have a DeclContext that is initializing. |
| 843 | ExternalASTSource::Deserializing ADeclContext(Source); |
| 844 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 845 | llvm::SmallVector<Decl*, 64> Decls; |
| 846 | if (Source->FindExternalLexicalDecls(this, Decls)) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 847 | return; |
| 848 | |
| 849 | // There is no longer any lexical storage in this context |
| 850 | ExternalLexicalStorage = false; |
| 851 | |
| 852 | if (Decls.empty()) |
| 853 | return; |
| 854 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 855 | // We may have already loaded just the fields of this record, in which case |
| 856 | // don't add the decls, just replace the FirstDecl/LastDecl chain. |
| 857 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(this)) |
| 858 | if (RD->LoadedFieldsFromExternalStorage) { |
| 859 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 860 | return; |
| 861 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 862 | |
| 863 | // Splice the newly-read declarations into the beginning of the list |
| 864 | // of declarations. |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 865 | Decl *ExternalFirst, *ExternalLast; |
| 866 | llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls); |
| 867 | ExternalLast->NextDeclInContext = FirstDecl; |
| 868 | FirstDecl = ExternalFirst; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 869 | if (!LastDecl) |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 870 | LastDecl = ExternalLast; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 871 | } |
| 872 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 873 | DeclContext::lookup_result |
| 874 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 875 | DeclarationName Name) { |
| 876 | ASTContext &Context = DC->getParentASTContext(); |
| 877 | StoredDeclsMap *Map; |
| 878 | if (!(Map = DC->LookupPtr)) |
| 879 | Map = DC->CreateStoredDeclsMap(Context); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 880 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 881 | StoredDeclsList &List = (*Map)[Name]; |
| 882 | assert(List.isNull()); |
| 883 | (void) List; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 884 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 885 | return DeclContext::lookup_result(); |
| 886 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 887 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 888 | DeclContext::lookup_result |
| 889 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 890 | DeclarationName Name, |
| 891 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 892 | ASTContext &Context = DC->getParentASTContext();; |
| 893 | |
| 894 | StoredDeclsMap *Map; |
| 895 | if (!(Map = DC->LookupPtr)) |
| 896 | Map = DC->CreateStoredDeclsMap(Context); |
| 897 | |
| 898 | StoredDeclsList &List = (*Map)[Name]; |
| 899 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 900 | if (List.isNull()) |
| 901 | List.setOnlyValue(Decls[I]); |
| 902 | else |
| 903 | List.AddSubsequentDecl(Decls[I]); |
| 904 | } |
| 905 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 906 | return List.getLookupResult(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Argyrios Kyrtzidis | d32ee89 | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 909 | void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC, |
| 910 | DeclarationName Name, |
| 911 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 912 | assert(DC->LookupPtr); |
| 913 | StoredDeclsMap &Map = *DC->LookupPtr; |
| 914 | |
| 915 | // If there's an entry in the table the visible decls for this name have |
| 916 | // already been deserialized. |
| 917 | if (Map.find(Name) == Map.end()) { |
| 918 | StoredDeclsList &List = Map[Name]; |
| 919 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 920 | if (List.isNull()) |
| 921 | List.setOnlyValue(Decls[I]); |
| 922 | else |
| 923 | List.AddSubsequentDecl(Decls[I]); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 928 | DeclContext::decl_iterator DeclContext::noload_decls_begin() const { |
| 929 | return decl_iterator(FirstDecl); |
| 930 | } |
| 931 | |
| 932 | DeclContext::decl_iterator DeclContext::noload_decls_end() const { |
| 933 | return decl_iterator(); |
| 934 | } |
| 935 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 936 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 937 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 938 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 939 | |
| 940 | // FIXME: Check whether we need to load some declarations from |
| 941 | // external storage. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | return decl_iterator(FirstDecl); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 945 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 946 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 947 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 948 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | return decl_iterator(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 952 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 953 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 954 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 955 | |
| 956 | return !FirstDecl; |
| 957 | } |
| 958 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 959 | void DeclContext::removeDecl(Decl *D) { |
| 960 | assert(D->getLexicalDeclContext() == this && |
| 961 | "decl being removed from non-lexical context"); |
| 962 | assert((D->NextDeclInContext || D == LastDecl) && |
| 963 | "decl is not in decls list"); |
| 964 | |
| 965 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 966 | if (D == FirstDecl) { |
| 967 | if (D == LastDecl) |
| 968 | FirstDecl = LastDecl = 0; |
| 969 | else |
| 970 | FirstDecl = D->NextDeclInContext; |
| 971 | } else { |
| 972 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 973 | assert(I && "decl not found in linked list"); |
| 974 | if (I->NextDeclInContext == D) { |
| 975 | I->NextDeclInContext = D->NextDeclInContext; |
| 976 | if (D == LastDecl) LastDecl = I; |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | // Mark that D is no longer in the decl chain. |
| 983 | D->NextDeclInContext = 0; |
| 984 | |
| 985 | // Remove D from the lookup table if necessary. |
| 986 | if (isa<NamedDecl>(D)) { |
| 987 | NamedDecl *ND = cast<NamedDecl>(D); |
| 988 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 989 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 990 | if (!Map) return; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 991 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 992 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 993 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 994 | Pos->second.remove(ND); |
| 995 | } |
| 996 | } |
| 997 | |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 998 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 33f219d | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 999 | assert(D->getLexicalDeclContext() == this && |
| 1000 | "Decl inserted into wrong lexical context"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 020713e | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1002 | "Decl already inserted into a DeclContext"); |
| 1003 | |
| 1004 | if (FirstDecl) { |
Chris Lattner | fcd33a6 | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 1005 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 020713e | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1006 | LastDecl = D; |
| 1007 | } else { |
| 1008 | FirstDecl = LastDecl = D; |
| 1009 | } |
Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Notify a C++ record declaration that we've added a member, so it can |
| 1012 | // update it's class-specific state. |
| 1013 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 1014 | Record->addedMember(D); |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | void DeclContext::addDecl(Decl *D) { |
| 1018 | addHiddenDecl(D); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1019 | |
| 1020 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1021 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1024 | /// buildLookup - Build the lookup data structure with all of the |
| 1025 | /// declarations in DCtx (and any other contexts linked to it or |
| 1026 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1027 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1028 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | for (decl_iterator D = DCtx->decls_begin(), |
| 1030 | DEnd = DCtx->decls_end(); |
Douglas Gregor | d05cb41 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 1031 | D != DEnd; ++D) { |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1032 | // Insert this declaration into the lookup structure, but only |
| 1033 | // if it's semantically in its decl context. During non-lazy |
| 1034 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1035 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1036 | if (D->getDeclContext() == DCtx) |
| 1037 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1038 | |
Ted Kremenek | 707ece6 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1039 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 1040 | // data structure. |
| 1041 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 1042 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 1043 | I != IEnd; ++I) |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1044 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | 707ece6 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1045 | |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1046 | // If this declaration is itself a transparent declaration context or |
| 1047 | // inline namespace, add its members (recursively). |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1048 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1049 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1050 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | DeclContext::lookup_result |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1056 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1057 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1058 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1059 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1060 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1061 | if (hasExternalVisibleStorage()) { |
| 1062 | // Check to see if we've already cached the lookup results. |
| 1063 | if (LookupPtr) { |
| 1064 | StoredDeclsMap::iterator I = LookupPtr->find(Name); |
| 1065 | if (I != LookupPtr->end()) |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1066 | return I->second.getLookupResult(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1070 | return Source->FindExternalVisibleDeclsByName(this, Name); |
| 1071 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1073 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1074 | /// all of the linked DeclContexts (in declaration order!) and |
| 1075 | /// inserting their values. |
Douglas Gregor | 9615ec2 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1076 | if (!LookupPtr) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1077 | buildLookup(this); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1078 | |
Douglas Gregor | 9615ec2 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1079 | if (!LookupPtr) |
Douglas Gregor | 10dc8aa | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1080 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Douglas Gregor | 9615ec2 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1081 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1082 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1083 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 1084 | if (Pos == LookupPtr->end()) |
Douglas Gregor | 10dc8aa | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1085 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1086 | return Pos->second.getLookupResult(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1090 | DeclContext::lookup(DeclarationName Name) const { |
| 1091 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1094 | DeclContext *DeclContext::getRedeclContext() { |
Chris Lattner | 17a1bfa | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 1095 | DeclContext *Ctx = this; |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1096 | // Skip through transparent contexts. |
| 1097 | while (Ctx->isTransparentContext()) |
Douglas Gregor | 6ad0ef5 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1098 | Ctx = Ctx->getParent(); |
| 1099 | return Ctx; |
| 1100 | } |
| 1101 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1102 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 1103 | DeclContext *Ctx = this; |
| 1104 | // Skip through non-namespace, non-translation-unit contexts. |
Sebastian Redl | 4f08c96 | 2010-08-31 00:36:23 +0000 | [diff] [blame] | 1105 | while (!Ctx->isFileContext()) |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1106 | Ctx = Ctx->getParent(); |
| 1107 | return Ctx->getPrimaryContext(); |
| 1108 | } |
| 1109 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1110 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 1111 | // For non-file contexts, this is equivalent to Equals. |
| 1112 | if (!isFileContext()) |
| 1113 | return O->Equals(this); |
| 1114 | |
| 1115 | do { |
| 1116 | if (O->Equals(this)) |
| 1117 | return true; |
| 1118 | |
| 1119 | const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O); |
| 1120 | if (!NS || !NS->isInline()) |
| 1121 | break; |
| 1122 | O = NS->getParent(); |
| 1123 | } while (O); |
| 1124 | |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1128 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1129 | // FIXME: This feels like a hack. Should DeclarationName support |
| 1130 | // template-ids, or is there a better way to keep specializations |
| 1131 | // from being visible? |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1132 | if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter()) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1133 | return; |
Eli Friedman | 7316819 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 1134 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 1135 | if (FD->isFunctionTemplateSpecialization()) |
| 1136 | return; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1137 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1138 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1139 | if (PrimaryContext != this) { |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1140 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | // If we already have a lookup data structure, perform the insertion |
Argyrios Kyrtzidis | e51e554 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1145 | // into it. If we haven't deserialized externally stored decls, deserialize |
| 1146 | // them so we can add the decl. Otherwise, be lazy and don't build that |
| 1147 | // structure until someone asks for it. |
| 1148 | if (LookupPtr || !Recoverable || hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1149 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1150 | |
Sebastian Redl | bd59576 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1151 | // If we are a transparent context or inline namespace, insert into our |
| 1152 | // parent context, too. This operation is recursive. |
| 1153 | if (isTransparentContext() || isInlineNamespace()) |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1154 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 1155 | |
| 1156 | Decl *DCAsDecl = cast<Decl>(this); |
| 1157 | // Notify that a decl was made visible unless it's a Tag being defined. |
| 1158 | if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined())) |
| 1159 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 1160 | L->AddedVisibleDecl(this, D); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1163 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1164 | // Skip unnamed declarations. |
| 1165 | if (!D->getDeclName()) |
| 1166 | return; |
| 1167 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1168 | // FIXME: This feels like a hack. Should DeclarationName support |
| 1169 | // template-ids, or is there a better way to keep specializations |
| 1170 | // from being visible? |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1171 | if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter()) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | |
Argyrios Kyrtzidis | e51e554 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1174 | ASTContext *C = 0; |
| 1175 | if (!LookupPtr) { |
| 1176 | C = &getParentASTContext(); |
| 1177 | CreateStoredDeclsMap(*C); |
| 1178 | } |
| 1179 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1180 | // If there is an external AST source, load any declarations it knows about |
| 1181 | // with this declaration's name. |
| 1182 | // If the lookup table contains an entry about this name it means that we |
| 1183 | // have already checked the external source. |
| 1184 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 1185 | if (hasExternalVisibleStorage() && |
| 1186 | LookupPtr->find(D->getDeclName()) == LookupPtr->end()) |
| 1187 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
| 1188 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1189 | // Insert this declaration into the map. |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1190 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | caae716 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1191 | if (DeclNameEntries.isNull()) { |
| 1192 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | dfd6b3d | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1193 | return; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1194 | } |
Chris Lattner | 24e24d5 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 29578f3 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 1196 | // If it is possible that this is a redeclaration, check to see if there is |
| 1197 | // already a decl for which declarationReplaces returns true. If there is |
| 1198 | // one, just replace it and return. |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1199 | if (DeclNameEntries.HandleRedeclaration(D)) |
Chris Lattner | caae716 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1200 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Chris Lattner | dfd6b3d | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1202 | // Put this declaration into the appropriate slot. |
Chris Lattner | caae716 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1203 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1204 | } |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1205 | |
Argyrios Kyrtzidis | d32ee89 | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 1206 | void DeclContext::MaterializeVisibleDeclsFromExternalStorage() { |
| 1207 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1208 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 1209 | |
| 1210 | if (!LookupPtr) |
| 1211 | CreateStoredDeclsMap(getParentASTContext()); |
| 1212 | Source->MaterializeVisibleDecls(this); |
| 1213 | } |
| 1214 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1215 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 1216 | /// this context. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1218 | DeclContext::getUsingDirectives() const { |
| 1219 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1220 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 1221 | reinterpret_cast<udir_iterator>(Result.second)); |
| 1222 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1223 | |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1224 | //===----------------------------------------------------------------------===// |
| 1225 | // Creation and Destruction of StoredDeclsMaps. // |
| 1226 | //===----------------------------------------------------------------------===// |
| 1227 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1228 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 1229 | assert(!LookupPtr && "context already has a decls map"); |
| 1230 | assert(getPrimaryContext() == this && |
| 1231 | "creating decls map on non-primary context"); |
| 1232 | |
| 1233 | StoredDeclsMap *M; |
| 1234 | bool Dependent = isDependentContext(); |
| 1235 | if (Dependent) |
| 1236 | M = new DependentStoredDeclsMap(); |
| 1237 | else |
| 1238 | M = new StoredDeclsMap(); |
| 1239 | M->Previous = C.LastSDM; |
| 1240 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 1241 | LookupPtr = M; |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1242 | return M; |
| 1243 | } |
| 1244 | |
| 1245 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1246 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 1247 | // pointer because the subclass doesn't add anything that needs to |
| 1248 | // be deleted. |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1249 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 1250 | } |
| 1251 | |
| 1252 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 1253 | while (Map) { |
| 1254 | // Advance the iteration before we invalidate memory. |
| 1255 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 1256 | |
| 1257 | if (Dependent) |
| 1258 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 1259 | else |
| 1260 | delete Map; |
| 1261 | |
| 1262 | Map = Next.getPointer(); |
| 1263 | Dependent = Next.getInt(); |
| 1264 | } |
| 1265 | } |
| 1266 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1267 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1268 | DeclContext *Parent, |
| 1269 | const PartialDiagnostic &PDiag) { |
| 1270 | assert(Parent->isDependentContext() |
| 1271 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1272 | Parent = Parent->getPrimaryContext(); |
| 1273 | if (!Parent->LookupPtr) |
| 1274 | Parent->CreateStoredDeclsMap(C); |
| 1275 | |
| 1276 | DependentStoredDeclsMap *Map |
| 1277 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1278 | |
Douglas Gregor | a55530e | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1279 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1280 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | a55530e | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1281 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1282 | if (PDiag.hasStorage()) |
| 1283 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1284 | |
| 1285 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1286 | |
| 1287 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1288 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1289 | Map->FirstDiagnostic = DD; |
| 1290 | |
| 1291 | return DD; |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1292 | } |