Eli Friedman | 56d2937 | 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 | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 24 | #include "clang/AST/Type.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 27 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Statistics |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 38 | #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0; |
| 39 | #define ABSTRACT_DECL(DECL) |
| 40 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 41 | |
| 42 | static bool StatSwitch = false; |
| 43 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 44 | const char *Decl::getDeclKindName() const { |
| 45 | switch (DeclKind) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 46 | default: assert(0 && "Declaration not in DeclNodes.inc!"); |
| 47 | #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED; |
| 48 | #define ABSTRACT_DECL(DECL) |
| 49 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Douglas Gregor | 4273857 | 2010-03-05 00:26:45 +0000 | [diff] [blame] | 53 | void Decl::setInvalidDecl(bool Invalid) { |
| 54 | InvalidDecl = Invalid; |
| 55 | if (Invalid) { |
| 56 | // Defensive maneuver for ill-formed code: we're likely not to make it to |
| 57 | // a point where we set the access specifier, so default it to "public" |
| 58 | // to avoid triggering asserts elsewhere in the front end. |
| 59 | setAccess(AS_public); |
| 60 | } |
| 61 | } |
| 62 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 63 | const char *DeclContext::getDeclKindName() const { |
| 64 | switch (DeclKind) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 65 | default: assert(0 && "Declaration context not in DeclNodes.inc!"); |
| 66 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED; |
| 67 | #define ABSTRACT_DECL(DECL) |
| 68 | #include "clang/AST/DeclNodes.inc" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 72 | bool Decl::CollectingStats(bool Enable) { |
Kovarththanan Rajaratnam | 2024f4d | 2009-11-29 14:54:35 +0000 | [diff] [blame] | 73 | if (Enable) StatSwitch = true; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 74 | return StatSwitch; |
| 75 | } |
| 76 | |
| 77 | void Decl::PrintStats() { |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 78 | llvm::errs() << "\n*** Decl Stats:\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 80 | int totalDecls = 0; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 81 | #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s; |
| 82 | #define ABSTRACT_DECL(DECL) |
| 83 | #include "clang/AST/DeclNodes.inc" |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 84 | llvm::errs() << " " << totalDecls << " decls total.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 86 | int totalBytes = 0; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 87 | #define DECL(DERIVED, BASE) \ |
| 88 | if (n##DERIVED##s > 0) { \ |
| 89 | totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \ |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 90 | llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \ |
| 91 | << sizeof(DERIVED##Decl) << " each (" \ |
| 92 | << n##DERIVED##s * sizeof(DERIVED##Decl) \ |
| 93 | << " bytes)\n"; \ |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 94 | } |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 95 | #define ABSTRACT_DECL(DECL) |
| 96 | #include "clang/AST/DeclNodes.inc" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 98 | llvm::errs() << "Total bytes = " << totalBytes << "\n"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 101 | void Decl::add(Kind k) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 102 | switch (k) { |
Sean Hunt | 9a55591 | 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 | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Anders Carlsson | 67e3320 | 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 | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 113 | if (const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 114 | = dyn_cast<NonTypeTemplateParmDecl>(this)) |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 115 | return NTTP->isParameterPack(); |
Douglas Gregor | 61c4d28 | 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 | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Douglas Gregor | 1fe85ea | 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 | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 129 | bool Decl::isFunctionOrFunctionTemplate() const { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 130 | if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this)) |
Anders Carlsson | 58badb7 | 2009-06-26 05:26:50 +0000 | [diff] [blame] | 131 | return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 133 | return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); |
| 134 | } |
| 135 | |
Douglas Gregor | 79c2278 | 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 | 4c3e0ee | 2011-02-17 08:47:29 +0000 | [diff] [blame] | 146 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 148 | // PrettyStackTraceDecl Implementation |
| 149 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 151 | void PrettyStackTraceDecl::print(raw_ostream &OS) const { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 152 | SourceLocation TheLoc = Loc; |
| 153 | if (TheLoc.isInvalid() && TheDecl) |
| 154 | TheLoc = TheDecl->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 49f28ca | 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 | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 163 | if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 164 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 165 | OS << '\n'; |
| 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 169 | // Decl Implementation |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Douglas Gregor | da2142f | 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 | f4a03cc | 2011-02-17 07:02:32 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 175 | void Decl::setDeclContext(DeclContext *DC) { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 176 | DeclCtx = DC; |
Douglas Gregor | 4afa39d | 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 | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 184 | MultipleDC *MDC = new (getASTContext()) MultipleDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 185 | MDC->SemanticDC = getDeclContext(); |
| 186 | MDC->LexicalDC = DC; |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 187 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 188 | } else { |
| 189 | getMultipleDC()->LexicalDC = DC; |
| 190 | } |
| 191 | } |
| 192 | |
John McCall | 9aeed32 | 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 | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 204 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argyrios Kyrtzidis | 9b34669 | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 205 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 206 | return TUD; |
| 207 | |
Argyrios Kyrtzidis | 3708b3d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Argyrios Kyrtzidis | 3708b3d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 216 | return cast<TranslationUnitDecl>(DC); |
| 217 | } |
| 218 | |
| 219 | ASTContext &Decl::getASTContext() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | return getTranslationUnitDecl()->getASTContext(); |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 223 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 224 | return getASTContext().getASTMutationListener(); |
| 225 | } |
| 226 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 227 | bool Decl::isUsed(bool CheckUsedAttr) const { |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 228 | if (Used) |
| 229 | return true; |
| 230 | |
| 231 | // Check for used attribute. |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 232 | if (CheckUsedAttr && hasAttr<UsedAttr>()) |
Tanya Lattner | 12ead49 | 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 | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 237 | if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used) |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
Argyrios Kyrtzidis | 6b6b42a | 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 | 0a0d2b1 | 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) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 268 | StringRef TargetPlatform = Context.Target.getPlatformName(); |
| 269 | StringRef PrettyPlatformName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 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 | b53e417 | 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 | 0a0d2b1 | 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 | 0a0d2b1 | 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 | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 416 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 417 | switch (DeclKind) { |
John McCall | 9488ea1 | 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 | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 423 | case EnumConstant: |
| 424 | case Var: |
| 425 | case ImplicitParam: |
| 426 | case ParmVar: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 427 | case NonTypeTemplateParm: |
| 428 | case ObjCMethod: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 429 | case ObjCProperty: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 430 | return IDNS_Ordinary; |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 431 | case Label: |
| 432 | return IDNS_Label; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 433 | case IndirectField: |
| 434 | return IDNS_Ordinary | IDNS_Member; |
| 435 | |
John McCall | 0d6b164 | 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 | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 441 | case TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 442 | case TypeAliasTemplate: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 443 | case UnresolvedUsingTypename: |
| 444 | case TemplateTypeParm: |
| 445 | return IDNS_Ordinary | IDNS_Type; |
| 446 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 447 | case UsingShadow: |
| 448 | return 0; // we'll actually overwrite this later |
| 449 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 450 | case UnresolvedUsingValue: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 451 | return IDNS_Ordinary | IDNS_Using; |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 452 | |
| 453 | case Using: |
| 454 | return IDNS_Using; |
| 455 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 456 | case ObjCProtocol: |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 457 | return IDNS_ObjCProtocol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 459 | case Field: |
| 460 | case ObjCAtDefsField: |
| 461 | case ObjCIvar: |
| 462 | return IDNS_Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 464 | case Record: |
| 465 | case CXXRecord: |
| 466 | case Enum: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 467 | return IDNS_Tag | IDNS_Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 469 | case Namespace: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 470 | case NamespaceAlias: |
| 471 | return IDNS_Namespace; |
| 472 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 473 | case FunctionTemplate: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 474 | return IDNS_Ordinary; |
| 475 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 476 | case ClassTemplate: |
| 477 | case TemplateTemplateParm: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 478 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 480 | // Never have names. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 481 | case Friend: |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 482 | case FriendTemplate: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 483 | case AccessSpec: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 484 | case LinkageSpec: |
| 485 | case FileScopeAsm: |
| 486 | case StaticAssert: |
| 487 | case ObjCClass: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 488 | case ObjCPropertyImpl: |
| 489 | case ObjCForwardProtocol: |
| 490 | case Block: |
| 491 | case TranslationUnit: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 493 | case UsingDirective: |
| 494 | case ClassTemplateSpecialization: |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 495 | case ClassTemplatePartialSpecialization: |
Douglas Gregor | bd4187b | 2010-04-22 23:19:50 +0000 | [diff] [blame] | 496 | case ObjCImplementation: |
| 497 | case ObjCCategory: |
| 498 | case ObjCCategoryImpl: |
| 499 | // Never looked up by name. |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 502 | |
| 503 | return 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 506 | void Decl::setAttrs(const AttrVec &attrs) { |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 507 | assert(!HasAttrs && "Decl already contains attrs."); |
| 508 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 509 | AttrVec &AttrBlank = getASTContext().getDeclAttrs(this); |
| 510 | assert(AttrBlank.empty() && "HasAttrs was wrong?"); |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 511 | |
| 512 | AttrBlank = attrs; |
| 513 | HasAttrs = true; |
| 514 | } |
| 515 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 516 | void Decl::dropAttrs() { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 517 | if (!HasAttrs) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 519 | HasAttrs = false; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 520 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 523 | const AttrVec &Decl::getAttrs() const { |
| 524 | assert(HasAttrs && "No attrs to get!"); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 525 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 528 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 529 | bool HasLHSAttr = this->HasAttrs; |
| 530 | bool HasRHSAttr = RHS->HasAttrs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 532 | // Usually, neither decl has attrs, nothing to do. |
| 533 | if (!HasLHSAttr && !HasRHSAttr) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 535 | // If 'this' has no attrs, swap the other way. |
| 536 | if (!HasLHSAttr) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 537 | return RHS->swapAttrs(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 539 | ASTContext &Context = getASTContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 541 | // Handle the case when both decls have attrs. |
| 542 | if (HasRHSAttr) { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 543 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 544 | return; |
| 545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 547 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 548 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 549 | Context.eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 550 | this->HasAttrs = false; |
| 551 | RHS->HasAttrs = true; |
| 552 | } |
| 553 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 554 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 555 | Decl::Kind DK = D->getDeclKind(); |
| 556 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 557 | #define DECL(NAME, BASE) |
| 558 | #define DECL_CONTEXT(NAME) \ |
| 559 | case Decl::NAME: \ |
| 560 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 561 | #define DECL_CONTEXT_BASE(NAME) |
| 562 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 563 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 564 | #define DECL(NAME, BASE) |
| 565 | #define DECL_CONTEXT_BASE(NAME) \ |
| 566 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 567 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 568 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 569 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 570 | return 0; |
| 571 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 575 | Decl::Kind DK = D->getKind(); |
| 576 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 577 | #define DECL(NAME, BASE) |
| 578 | #define DECL_CONTEXT(NAME) \ |
| 579 | case Decl::NAME: \ |
| 580 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 581 | #define DECL_CONTEXT_BASE(NAME) |
| 582 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 583 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 584 | #define DECL(NAME, BASE) |
| 585 | #define DECL_CONTEXT_BASE(NAME) \ |
| 586 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 587 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 588 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 589 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 590 | return 0; |
| 591 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 594 | SourceLocation Decl::getBodyRBrace() const { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 595 | // Special handling of FunctionDecl to avoid de-serializing the body from PCH. |
| 596 | // FunctionDecl stores EndRangeLoc for this purpose. |
| 597 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 598 | const FunctionDecl *Definition; |
| 599 | if (FD->hasBody(Definition)) |
| 600 | return Definition->getSourceRange().getEnd(); |
| 601 | return SourceLocation(); |
| 602 | } |
| 603 | |
Argyrios Kyrtzidis | 6717ef4 | 2010-07-07 11:31:27 +0000 | [diff] [blame] | 604 | if (Stmt *Body = getBody()) |
| 605 | return Body->getSourceRange().getEnd(); |
| 606 | |
| 607 | return SourceLocation(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 610 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 611 | #ifndef NDEBUG |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 612 | // Suppress this check if any of the following hold: |
| 613 | // 1. this is the translation unit (and thus has no parent) |
| 614 | // 2. this is a template parameter (and thus doesn't belong to its context) |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 615 | // 3. this is a non-type template parameter |
| 616 | // 4. the context is not a record |
| 617 | // 5. it's invalid |
| 618 | // 6. it's a C++0x static_assert. |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 619 | if (isa<TranslationUnitDecl>(this) || |
Argyrios Kyrtzidis | 04aed0e | 2010-07-02 11:55:44 +0000 | [diff] [blame] | 620 | isa<TemplateTypeParmDecl>(this) || |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 621 | isa<NonTypeTemplateParmDecl>(this) || |
Douglas Gregor | fdd8ab1 | 2010-02-22 17:53:38 +0000 | [diff] [blame] | 622 | !isa<CXXRecordDecl>(getDeclContext()) || |
Argyrios Kyrtzidis | 65b63ec | 2010-09-08 21:32:35 +0000 | [diff] [blame] | 623 | isInvalidDecl() || |
| 624 | isa<StaticAssertDecl>(this) || |
| 625 | // FIXME: a ParmVarDecl can have ClassTemplateSpecialization |
| 626 | // as DeclContext (?). |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 627 | isa<ParmVarDecl>(this) || |
| 628 | // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have |
| 629 | // AS_none as access specifier. |
| 630 | isa<CXXRecordDecl>(this)) |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 631 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
| 633 | assert(Access != AS_none && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 634 | "Access specifier is AS_none inside a record decl"); |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 635 | #endif |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 638 | DeclContext *Decl::getNonClosureContext() { |
| 639 | DeclContext *DC = getDeclContext(); |
| 640 | |
| 641 | // This is basically "while (DC->isClosure()) DC = DC->getParent();" |
| 642 | // except that it's significantly more efficient to cast to a known |
| 643 | // decl type and call getDeclContext() than to call getParent(). |
John McCall | 7b3f853 | 2011-06-23 21:18:31 +0000 | [diff] [blame] | 644 | while (isa<BlockDecl>(DC)) |
| 645 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 646 | |
| 647 | assert(!DC->isClosure()); |
| 648 | return DC; |
| 649 | } |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 650 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 651 | //===----------------------------------------------------------------------===// |
| 652 | // DeclContext Implementation |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 655 | bool DeclContext::classof(const Decl *D) { |
| 656 | switch (D->getKind()) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 657 | #define DECL(NAME, BASE) |
| 658 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 659 | #define DECL_CONTEXT_BASE(NAME) |
| 660 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 661 | return true; |
| 662 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 663 | #define DECL(NAME, BASE) |
| 664 | #define DECL_CONTEXT_BASE(NAME) \ |
| 665 | if (D->getKind() >= Decl::first##NAME && \ |
| 666 | D->getKind() <= Decl::last##NAME) \ |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 667 | return true; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 668 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | } |
| 672 | |
Douglas Gregor | a2da780 | 2010-07-25 18:38:02 +0000 | [diff] [blame] | 673 | DeclContext::~DeclContext() { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 675 | /// \brief Find the parent context of this context that will be |
| 676 | /// used for unqualified name lookup. |
| 677 | /// |
| 678 | /// Generally, the parent lookup context is the semantic context. However, for |
| 679 | /// a friend function the parent lookup context is the lexical context, which |
| 680 | /// is the class in which the friend is declared. |
| 681 | DeclContext *DeclContext::getLookupParent() { |
| 682 | // FIXME: Find a better way to identify friends |
| 683 | if (isa<FunctionDecl>(this)) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 684 | if (getParent()->getRedeclContext()->isFileContext() && |
| 685 | getLexicalParent()->getRedeclContext()->isRecord()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 686 | return getLexicalParent(); |
| 687 | |
| 688 | return getParent(); |
| 689 | } |
| 690 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 691 | bool DeclContext::isInlineNamespace() const { |
| 692 | return isNamespace() && |
| 693 | cast<NamespaceDecl>(this)->isInline(); |
| 694 | } |
| 695 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 696 | bool DeclContext::isDependentContext() const { |
| 697 | if (isFileContext()) |
| 698 | return false; |
| 699 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 700 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 701 | return true; |
| 702 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 703 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 704 | if (Record->getDescribedClassTemplate()) |
| 705 | return true; |
| 706 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 707 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 708 | if (Function->getDescribedFunctionTemplate()) |
| 709 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 711 | // Friend function declarations are dependent if their *lexical* |
| 712 | // context is dependent. |
| 713 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 714 | return getLexicalParent()->isDependentContext(); |
| 715 | } |
| 716 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 717 | return getParent() && getParent()->isDependentContext(); |
| 718 | } |
| 719 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 720 | bool DeclContext::isTransparentContext() const { |
| 721 | if (DeclKind == Decl::Enum) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 722 | return !cast<EnumDecl>(this)->isScoped(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 723 | else if (DeclKind == Decl::LinkageSpec) |
| 724 | return true; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 725 | |
| 726 | return false; |
| 727 | } |
| 728 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 729 | bool DeclContext::isExternCContext() const { |
| 730 | const DeclContext *DC = this; |
| 731 | while (DC->DeclKind != Decl::TranslationUnit) { |
| 732 | if (DC->DeclKind == Decl::LinkageSpec) |
| 733 | return cast<LinkageSpecDecl>(DC)->getLanguage() |
| 734 | == LinkageSpecDecl::lang_c; |
| 735 | DC = DC->getParent(); |
| 736 | } |
| 737 | return false; |
| 738 | } |
| 739 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 740 | bool DeclContext::Encloses(const DeclContext *DC) const { |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 741 | if (getPrimaryContext() != this) |
| 742 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 744 | for (; DC; DC = DC->getParent()) |
| 745 | if (DC->getPrimaryContext() == this) |
| 746 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | return false; |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 750 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 751 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 752 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 753 | case Decl::LinkageSpec: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 755 | // There is only one DeclContext for these entities. |
| 756 | return this; |
| 757 | |
| 758 | case Decl::Namespace: |
| 759 | // The original namespace is our primary context. |
| 760 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 761 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 762 | case Decl::ObjCMethod: |
| 763 | return this; |
| 764 | |
| 765 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 766 | case Decl::ObjCProtocol: |
| 767 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 768 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 769 | return this; |
| 770 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 771 | case Decl::ObjCImplementation: |
| 772 | case Decl::ObjCCategoryImpl: |
| 773 | return this; |
| 774 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 775 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 776 | if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 777 | // If this is a tag type that has a definition or is currently |
| 778 | // being defined, that definition is our primary context. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 779 | TagDecl *Tag = cast<TagDecl>(this); |
| 780 | assert(isa<TagType>(Tag->TypeForDecl) || |
| 781 | isa<InjectedClassNameType>(Tag->TypeForDecl)); |
| 782 | |
| 783 | if (TagDecl *Def = Tag->getDefinition()) |
| 784 | return Def; |
| 785 | |
| 786 | if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) { |
| 787 | const TagType *TagTy = cast<TagType>(Tag->TypeForDecl); |
| 788 | if (TagTy->isBeingDefined()) |
| 789 | // FIXME: is it necessarily being defined in the decl |
| 790 | // that owns the type? |
| 791 | return TagTy->getDecl(); |
| 792 | } |
| 793 | |
| 794 | return Tag; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 797 | assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 798 | "Unknown DeclContext kind"); |
| 799 | return this; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | DeclContext *DeclContext::getNextContext() { |
| 804 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 805 | case Decl::Namespace: |
| 806 | // Return the next namespace |
| 807 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 808 | |
| 809 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | } |
| 813 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 814 | std::pair<Decl *, Decl *> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 815 | DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls) { |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 816 | // Build up a chain of declarations via the Decl::NextDeclInContext field. |
| 817 | Decl *FirstNewDecl = 0; |
| 818 | Decl *PrevDecl = 0; |
| 819 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 820 | Decl *D = Decls[I]; |
| 821 | if (PrevDecl) |
| 822 | PrevDecl->NextDeclInContext = D; |
| 823 | else |
| 824 | FirstNewDecl = D; |
| 825 | |
| 826 | PrevDecl = D; |
| 827 | } |
| 828 | |
| 829 | return std::make_pair(FirstNewDecl, PrevDecl); |
| 830 | } |
| 831 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 832 | /// \brief Load the declarations within this lexical storage from an |
| 833 | /// external source. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 835 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 836 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 837 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 838 | |
Argyrios Kyrtzidis | 0dbbc04 | 2010-07-30 10:03:23 +0000 | [diff] [blame] | 839 | // Notify that we have a DeclContext that is initializing. |
| 840 | ExternalASTSource::Deserializing ADeclContext(Source); |
| 841 | |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 842 | // Load the external declarations, if any. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 843 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 844 | ExternalLexicalStorage = false; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 845 | switch (Source->FindExternalLexicalDecls(this, Decls)) { |
| 846 | case ELR_Success: |
| 847 | break; |
| 848 | |
| 849 | case ELR_Failure: |
| 850 | case ELR_AlreadyLoaded: |
| 851 | return; |
| 852 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 853 | |
| 854 | if (Decls.empty()) |
| 855 | return; |
| 856 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 857 | // We may have already loaded just the fields of this record, in which case |
| 858 | // don't add the decls, just replace the FirstDecl/LastDecl chain. |
| 859 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(this)) |
| 860 | if (RD->LoadedFieldsFromExternalStorage) { |
| 861 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 862 | return; |
| 863 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 864 | |
| 865 | // Splice the newly-read declarations into the beginning of the list |
| 866 | // of declarations. |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 867 | Decl *ExternalFirst, *ExternalLast; |
| 868 | llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls); |
| 869 | ExternalLast->NextDeclInContext = FirstDecl; |
| 870 | FirstDecl = ExternalFirst; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 871 | if (!LastDecl) |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 872 | LastDecl = ExternalLast; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 873 | } |
| 874 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 875 | DeclContext::lookup_result |
| 876 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 877 | DeclarationName Name) { |
| 878 | ASTContext &Context = DC->getParentASTContext(); |
| 879 | StoredDeclsMap *Map; |
| 880 | if (!(Map = DC->LookupPtr)) |
| 881 | Map = DC->CreateStoredDeclsMap(Context); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 882 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 883 | StoredDeclsList &List = (*Map)[Name]; |
| 884 | assert(List.isNull()); |
| 885 | (void) List; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 886 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 887 | return DeclContext::lookup_result(); |
| 888 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 889 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 890 | DeclContext::lookup_result |
| 891 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 892 | DeclarationName Name, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 893 | SmallVectorImpl<NamedDecl*> &Decls) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 894 | ASTContext &Context = DC->getParentASTContext();; |
| 895 | |
| 896 | StoredDeclsMap *Map; |
| 897 | if (!(Map = DC->LookupPtr)) |
| 898 | Map = DC->CreateStoredDeclsMap(Context); |
| 899 | |
| 900 | StoredDeclsList &List = (*Map)[Name]; |
| 901 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 902 | if (List.isNull()) |
| 903 | List.setOnlyValue(Decls[I]); |
| 904 | else |
| 905 | List.AddSubsequentDecl(Decls[I]); |
| 906 | } |
| 907 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 908 | return List.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 911 | void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC, |
| 912 | DeclarationName Name, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 913 | SmallVectorImpl<NamedDecl*> &Decls) { |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 914 | assert(DC->LookupPtr); |
| 915 | StoredDeclsMap &Map = *DC->LookupPtr; |
| 916 | |
| 917 | // If there's an entry in the table the visible decls for this name have |
| 918 | // already been deserialized. |
| 919 | if (Map.find(Name) == Map.end()) { |
| 920 | StoredDeclsList &List = Map[Name]; |
| 921 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 922 | if (List.isNull()) |
| 923 | List.setOnlyValue(Decls[I]); |
| 924 | else |
| 925 | List.AddSubsequentDecl(Decls[I]); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 930 | DeclContext::decl_iterator DeclContext::noload_decls_begin() const { |
| 931 | return decl_iterator(FirstDecl); |
| 932 | } |
| 933 | |
| 934 | DeclContext::decl_iterator DeclContext::noload_decls_end() const { |
| 935 | return decl_iterator(); |
| 936 | } |
| 937 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 938 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 939 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 940 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 941 | |
| 942 | // FIXME: Check whether we need to load some declarations from |
| 943 | // external storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 947 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 948 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 949 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 950 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | return decl_iterator(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 954 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 955 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 956 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 957 | |
| 958 | return !FirstDecl; |
| 959 | } |
| 960 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 961 | void DeclContext::removeDecl(Decl *D) { |
| 962 | assert(D->getLexicalDeclContext() == this && |
| 963 | "decl being removed from non-lexical context"); |
| 964 | assert((D->NextDeclInContext || D == LastDecl) && |
| 965 | "decl is not in decls list"); |
| 966 | |
| 967 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 968 | if (D == FirstDecl) { |
| 969 | if (D == LastDecl) |
| 970 | FirstDecl = LastDecl = 0; |
| 971 | else |
| 972 | FirstDecl = D->NextDeclInContext; |
| 973 | } else { |
| 974 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 975 | assert(I && "decl not found in linked list"); |
| 976 | if (I->NextDeclInContext == D) { |
| 977 | I->NextDeclInContext = D->NextDeclInContext; |
| 978 | if (D == LastDecl) LastDecl = I; |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // Mark that D is no longer in the decl chain. |
| 985 | D->NextDeclInContext = 0; |
| 986 | |
| 987 | // Remove D from the lookup table if necessary. |
| 988 | if (isa<NamedDecl>(D)) { |
| 989 | NamedDecl *ND = cast<NamedDecl>(D); |
| 990 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 991 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 992 | if (!Map) return; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 993 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 994 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 995 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 996 | Pos->second.remove(ND); |
| 997 | } |
| 998 | } |
| 999 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1000 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 1001 | assert(D->getLexicalDeclContext() == this && |
| 1002 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1004 | "Decl already inserted into a DeclContext"); |
| 1005 | |
| 1006 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 1007 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1008 | LastDecl = D; |
| 1009 | } else { |
| 1010 | FirstDecl = LastDecl = D; |
| 1011 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1012 | |
| 1013 | // Notify a C++ record declaration that we've added a member, so it can |
| 1014 | // update it's class-specific state. |
| 1015 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 1016 | Record->addedMember(D); |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | void DeclContext::addDecl(Decl *D) { |
| 1020 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1023 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1026 | /// buildLookup - Build the lookup data structure with all of the |
| 1027 | /// declarations in DCtx (and any other contexts linked to it or |
| 1028 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1029 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1030 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | for (decl_iterator D = DCtx->decls_begin(), |
| 1032 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 1033 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1034 | // Insert this declaration into the lookup structure, but only |
| 1035 | // if it's semantically in its decl context. During non-lazy |
| 1036 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1037 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1038 | if (D->getDeclContext() == DCtx) |
| 1039 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1040 | |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1041 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 1042 | // data structure. |
| 1043 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 1044 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 1045 | I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1046 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1047 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1048 | // If this declaration is itself a transparent declaration context or |
| 1049 | // inline namespace, add its members (recursively). |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1050 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1051 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1052 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1058 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1059 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1060 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1061 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1062 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1063 | if (hasExternalVisibleStorage()) { |
| 1064 | // Check to see if we've already cached the lookup results. |
| 1065 | if (LookupPtr) { |
| 1066 | StoredDeclsMap::iterator I = LookupPtr->find(Name); |
| 1067 | if (I != LookupPtr->end()) |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1068 | return I->second.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1072 | return Source->FindExternalVisibleDeclsByName(this, Name); |
| 1073 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1074 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1075 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1076 | /// all of the linked DeclContexts (in declaration order!) and |
| 1077 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1078 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1079 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1081 | if (!LookupPtr) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1082 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1083 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1084 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1085 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 1086 | if (Pos == LookupPtr->end()) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1087 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1088 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1092 | DeclContext::lookup(DeclarationName Name) const { |
| 1093 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1096 | DeclContext *DeclContext::getRedeclContext() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 1097 | DeclContext *Ctx = this; |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1098 | // Skip through transparent contexts. |
| 1099 | while (Ctx->isTransparentContext()) |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1100 | Ctx = Ctx->getParent(); |
| 1101 | return Ctx; |
| 1102 | } |
| 1103 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1104 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 1105 | DeclContext *Ctx = this; |
| 1106 | // Skip through non-namespace, non-translation-unit contexts. |
Sebastian Redl | 51a8a37 | 2010-08-31 00:36:23 +0000 | [diff] [blame] | 1107 | while (!Ctx->isFileContext()) |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1108 | Ctx = Ctx->getParent(); |
| 1109 | return Ctx->getPrimaryContext(); |
| 1110 | } |
| 1111 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1112 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 1113 | // For non-file contexts, this is equivalent to Equals. |
| 1114 | if (!isFileContext()) |
| 1115 | return O->Equals(this); |
| 1116 | |
| 1117 | do { |
| 1118 | if (O->Equals(this)) |
| 1119 | return true; |
| 1120 | |
| 1121 | const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O); |
| 1122 | if (!NS || !NS->isInline()) |
| 1123 | break; |
| 1124 | O = NS->getParent(); |
| 1125 | } while (O); |
| 1126 | |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1130 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1131 | // FIXME: This feels like a hack. Should DeclarationName support |
| 1132 | // template-ids, or is there a better way to keep specializations |
| 1133 | // from being visible? |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1134 | if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1135 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 1136 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 1137 | if (FD->isFunctionTemplateSpecialization()) |
| 1138 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1139 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1140 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1141 | if (PrimaryContext != this) { |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1142 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1143 | return; |
| 1144 | } |
| 1145 | |
| 1146 | // If we already have a lookup data structure, perform the insertion |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1147 | // into it. If we haven't deserialized externally stored decls, deserialize |
| 1148 | // them so we can add the decl. Otherwise, be lazy and don't build that |
| 1149 | // structure until someone asks for it. |
| 1150 | if (LookupPtr || !Recoverable || hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1151 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1152 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1153 | // If we are a transparent context or inline namespace, insert into our |
| 1154 | // parent context, too. This operation is recursive. |
| 1155 | if (isTransparentContext() || isInlineNamespace()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 1156 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 1157 | |
| 1158 | Decl *DCAsDecl = cast<Decl>(this); |
| 1159 | // Notify that a decl was made visible unless it's a Tag being defined. |
| 1160 | if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined())) |
| 1161 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 1162 | L->AddedVisibleDecl(this, D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1165 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1166 | // Skip unnamed declarations. |
| 1167 | if (!D->getDeclName()) |
| 1168 | return; |
| 1169 | |
Douglas Gregor | 5cb0ef4 | 2011-05-06 23:32:38 +0000 | [diff] [blame] | 1170 | // Skip entities that can't be found by name lookup into a particular |
| 1171 | // context. |
| 1172 | if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) || |
| 1173 | D->isTemplateParameter()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1176 | ASTContext *C = 0; |
| 1177 | if (!LookupPtr) { |
| 1178 | C = &getParentASTContext(); |
| 1179 | CreateStoredDeclsMap(*C); |
| 1180 | } |
| 1181 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1182 | // If there is an external AST source, load any declarations it knows about |
| 1183 | // with this declaration's name. |
| 1184 | // If the lookup table contains an entry about this name it means that we |
| 1185 | // have already checked the external source. |
| 1186 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 1187 | if (hasExternalVisibleStorage() && |
| 1188 | LookupPtr->find(D->getDeclName()) == LookupPtr->end()) |
| 1189 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
| 1190 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1191 | // Insert this declaration into the map. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1192 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1193 | if (DeclNameEntries.isNull()) { |
| 1194 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1195 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1196 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 1198 | // If it is possible that this is a redeclaration, check to see if there is |
| 1199 | // already a decl for which declarationReplaces returns true. If there is |
| 1200 | // one, just replace it and return. |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1201 | if (DeclNameEntries.HandleRedeclaration(D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1202 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1204 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1205 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1206 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1207 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 1208 | void DeclContext::MaterializeVisibleDeclsFromExternalStorage() { |
| 1209 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1210 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 1211 | |
| 1212 | if (!LookupPtr) |
| 1213 | CreateStoredDeclsMap(getParentASTContext()); |
| 1214 | Source->MaterializeVisibleDecls(this); |
| 1215 | } |
| 1216 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1217 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 1218 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1220 | DeclContext::getUsingDirectives() const { |
| 1221 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1222 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 1223 | reinterpret_cast<udir_iterator>(Result.second)); |
| 1224 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1225 | |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1226 | //===----------------------------------------------------------------------===// |
| 1227 | // Creation and Destruction of StoredDeclsMaps. // |
| 1228 | //===----------------------------------------------------------------------===// |
| 1229 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1230 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 1231 | assert(!LookupPtr && "context already has a decls map"); |
| 1232 | assert(getPrimaryContext() == this && |
| 1233 | "creating decls map on non-primary context"); |
| 1234 | |
| 1235 | StoredDeclsMap *M; |
| 1236 | bool Dependent = isDependentContext(); |
| 1237 | if (Dependent) |
| 1238 | M = new DependentStoredDeclsMap(); |
| 1239 | else |
| 1240 | M = new StoredDeclsMap(); |
| 1241 | M->Previous = C.LastSDM; |
| 1242 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 1243 | LookupPtr = M; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1244 | return M; |
| 1245 | } |
| 1246 | |
| 1247 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1248 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 1249 | // pointer because the subclass doesn't add anything that needs to |
| 1250 | // be deleted. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1251 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 1252 | } |
| 1253 | |
| 1254 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 1255 | while (Map) { |
| 1256 | // Advance the iteration before we invalidate memory. |
| 1257 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 1258 | |
| 1259 | if (Dependent) |
| 1260 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 1261 | else |
| 1262 | delete Map; |
| 1263 | |
| 1264 | Map = Next.getPointer(); |
| 1265 | Dependent = Next.getInt(); |
| 1266 | } |
| 1267 | } |
| 1268 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1269 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1270 | DeclContext *Parent, |
| 1271 | const PartialDiagnostic &PDiag) { |
| 1272 | assert(Parent->isDependentContext() |
| 1273 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1274 | Parent = Parent->getPrimaryContext(); |
| 1275 | if (!Parent->LookupPtr) |
| 1276 | Parent->CreateStoredDeclsMap(C); |
| 1277 | |
| 1278 | DependentStoredDeclsMap *Map |
| 1279 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1280 | |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1281 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1282 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1283 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1284 | if (PDiag.hasStorage()) |
| 1285 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1286 | |
| 1287 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1288 | |
| 1289 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1290 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1291 | Map->FirstDiagnostic = DD; |
| 1292 | |
| 1293 | return DD; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1294 | } |