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) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 46 | default: llvm_unreachable("Declaration not in DeclNodes.inc!"); |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 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) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 65 | default: llvm_unreachable("Declaration context not in DeclNodes.inc!"); |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 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) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 103 | default: llvm_unreachable("Declaration not in DeclNodes.inc!"); |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 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 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 136 | bool Decl::isTemplateDecl() const { |
| 137 | return isa<TemplateDecl>(this); |
| 138 | } |
| 139 | |
Argyrios Kyrtzidis | c8680f4 | 2011-09-28 02:45:33 +0000 | [diff] [blame] | 140 | const DeclContext *Decl::getParentFunctionOrMethod() const { |
| 141 | for (const DeclContext *DC = getDeclContext(); |
| 142 | DC && !DC->isTranslationUnit() && !DC->isNamespace(); |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 143 | DC = DC->getParent()) |
| 144 | if (DC->isFunctionOrMethod()) |
Argyrios Kyrtzidis | c8680f4 | 2011-09-28 02:45:33 +0000 | [diff] [blame] | 145 | return DC; |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 146 | |
Argyrios Kyrtzidis | c8680f4 | 2011-09-28 02:45:33 +0000 | [diff] [blame] | 147 | return 0; |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Douglas Gregor | 4c3e0ee | 2011-02-17 08:47:29 +0000 | [diff] [blame] | 150 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 152 | // PrettyStackTraceDecl Implementation |
| 153 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 155 | void PrettyStackTraceDecl::print(raw_ostream &OS) const { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 156 | SourceLocation TheLoc = Loc; |
| 157 | if (TheLoc.isInvalid() && TheDecl) |
| 158 | TheLoc = TheDecl->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 160 | if (TheLoc.isValid()) { |
| 161 | TheLoc.print(OS, SM); |
| 162 | OS << ": "; |
| 163 | } |
| 164 | |
| 165 | OS << Message; |
| 166 | |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 167 | if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 168 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 169 | OS << '\n'; |
| 170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 173 | // Decl Implementation |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 176 | // Out-of-line virtual method providing a home for Decl. |
| 177 | Decl::~Decl() { } |
Douglas Gregor | f4a03cc | 2011-02-17 07:02:32 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 179 | void Decl::setDeclContext(DeclContext *DC) { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 180 | DeclCtx = DC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 184 | if (DC == getLexicalDeclContext()) |
| 185 | return; |
| 186 | |
| 187 | if (isInSemaDC()) { |
Ted Kremenek | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 188 | MultipleDC *MDC = new (getASTContext()) MultipleDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 189 | MDC->SemanticDC = getDeclContext(); |
| 190 | MDC->LexicalDC = DC; |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 191 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 192 | } else { |
| 193 | getMultipleDC()->LexicalDC = DC; |
| 194 | } |
| 195 | } |
| 196 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 197 | bool Decl::isInAnonymousNamespace() const { |
| 198 | const DeclContext *DC = getDeclContext(); |
| 199 | do { |
| 200 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) |
| 201 | if (ND->isAnonymousNamespace()) |
| 202 | return true; |
| 203 | } while ((DC = DC->getParent())); |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 208 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argyrios Kyrtzidis | 9b34669 | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 209 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 210 | return TUD; |
| 211 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 212 | DeclContext *DC = getDeclContext(); |
| 213 | assert(DC && "This decl is not contained in a translation unit!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 215 | while (!DC->isTranslationUnit()) { |
| 216 | DC = DC->getParent(); |
| 217 | assert(DC && "This decl is not contained in a translation unit!"); |
| 218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 220 | return cast<TranslationUnitDecl>(DC); |
| 221 | } |
| 222 | |
| 223 | ASTContext &Decl::getASTContext() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | return getTranslationUnitDecl()->getASTContext(); |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 227 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 228 | return getASTContext().getASTMutationListener(); |
| 229 | } |
| 230 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 231 | bool Decl::isUsed(bool CheckUsedAttr) const { |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 232 | if (Used) |
| 233 | return true; |
| 234 | |
| 235 | // Check for used attribute. |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 236 | if (CheckUsedAttr && hasAttr<UsedAttr>()) |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 237 | return true; |
| 238 | |
| 239 | // Check redeclarations for used attribute. |
| 240 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 241 | if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used) |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 248 | bool Decl::isReferenced() const { |
| 249 | if (Referenced) |
| 250 | return true; |
| 251 | |
| 252 | // Check redeclarations. |
| 253 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 254 | if (I->Referenced) |
| 255 | return true; |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 260 | /// \brief Determine the availability of the given declaration based on |
| 261 | /// the target platform. |
| 262 | /// |
| 263 | /// When it returns an availability result other than \c AR_Available, |
| 264 | /// if the \p Message parameter is non-NULL, it will be set to a |
| 265 | /// string describing why the entity is unavailable. |
| 266 | /// |
| 267 | /// FIXME: Make these strings localizable, since they end up in |
| 268 | /// diagnostics. |
| 269 | static AvailabilityResult CheckAvailability(ASTContext &Context, |
| 270 | const AvailabilityAttr *A, |
| 271 | std::string *Message) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 272 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 273 | StringRef PrettyPlatformName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 274 | = AvailabilityAttr::getPrettyPlatformName(TargetPlatform); |
| 275 | if (PrettyPlatformName.empty()) |
| 276 | PrettyPlatformName = TargetPlatform; |
| 277 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 278 | VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 279 | if (TargetMinVersion.empty()) |
| 280 | return AR_Available; |
| 281 | |
| 282 | // Match the platform name. |
| 283 | if (A->getPlatform()->getName() != TargetPlatform) |
| 284 | return AR_Available; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 285 | |
| 286 | std::string HintMessage; |
| 287 | if (!A->getMessage().empty()) { |
| 288 | HintMessage = " - "; |
| 289 | HintMessage += A->getMessage(); |
| 290 | } |
| 291 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 292 | // Make sure that this declaration has not been marked 'unavailable'. |
| 293 | if (A->getUnavailable()) { |
| 294 | if (Message) { |
| 295 | Message->clear(); |
| 296 | llvm::raw_string_ostream Out(*Message); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 297 | Out << "not available on " << PrettyPlatformName |
| 298 | << HintMessage; |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return AR_Unavailable; |
| 302 | } |
| 303 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 304 | // Make sure that this declaration has already been introduced. |
| 305 | if (!A->getIntroduced().empty() && |
| 306 | TargetMinVersion < A->getIntroduced()) { |
| 307 | if (Message) { |
| 308 | Message->clear(); |
| 309 | llvm::raw_string_ostream Out(*Message); |
| 310 | Out << "introduced in " << PrettyPlatformName << ' ' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 311 | << A->getIntroduced() << HintMessage; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return AR_NotYetIntroduced; |
| 315 | } |
| 316 | |
| 317 | // Make sure that this declaration hasn't been obsoleted. |
| 318 | if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) { |
| 319 | if (Message) { |
| 320 | Message->clear(); |
| 321 | llvm::raw_string_ostream Out(*Message); |
| 322 | Out << "obsoleted in " << PrettyPlatformName << ' ' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 323 | << A->getObsoleted() << HintMessage; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return AR_Unavailable; |
| 327 | } |
| 328 | |
| 329 | // Make sure that this declaration hasn't been deprecated. |
| 330 | if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) { |
| 331 | if (Message) { |
| 332 | Message->clear(); |
| 333 | llvm::raw_string_ostream Out(*Message); |
| 334 | Out << "first deprecated in " << PrettyPlatformName << ' ' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 335 | << A->getDeprecated() << HintMessage; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return AR_Deprecated; |
| 339 | } |
| 340 | |
| 341 | return AR_Available; |
| 342 | } |
| 343 | |
| 344 | AvailabilityResult Decl::getAvailability(std::string *Message) const { |
| 345 | AvailabilityResult Result = AR_Available; |
| 346 | std::string ResultMessage; |
| 347 | |
| 348 | for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) { |
| 349 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) { |
| 350 | if (Result >= AR_Deprecated) |
| 351 | continue; |
| 352 | |
| 353 | if (Message) |
| 354 | ResultMessage = Deprecated->getMessage(); |
| 355 | |
| 356 | Result = AR_Deprecated; |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) { |
| 361 | if (Message) |
| 362 | *Message = Unavailable->getMessage(); |
| 363 | return AR_Unavailable; |
| 364 | } |
| 365 | |
| 366 | if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) { |
| 367 | AvailabilityResult AR = CheckAvailability(getASTContext(), Availability, |
| 368 | Message); |
| 369 | |
| 370 | if (AR == AR_Unavailable) |
| 371 | return AR_Unavailable; |
| 372 | |
| 373 | if (AR > Result) { |
| 374 | Result = AR; |
| 375 | if (Message) |
| 376 | ResultMessage.swap(*Message); |
| 377 | } |
| 378 | continue; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (Message) |
| 383 | Message->swap(ResultMessage); |
| 384 | return Result; |
| 385 | } |
| 386 | |
| 387 | bool Decl::canBeWeakImported(bool &IsDefinition) const { |
| 388 | IsDefinition = false; |
| 389 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) { |
| 390 | if (!Var->hasExternalStorage() || Var->getInit()) { |
| 391 | IsDefinition = true; |
| 392 | return false; |
| 393 | } |
| 394 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 395 | if (FD->hasBody()) { |
| 396 | IsDefinition = true; |
| 397 | return false; |
| 398 | } |
| 399 | } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this)) |
| 400 | return false; |
| 401 | else if (!(getASTContext().getLangOptions().ObjCNonFragileABI && |
| 402 | isa<ObjCInterfaceDecl>(this))) |
| 403 | return false; |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | bool Decl::isWeakImported() const { |
| 409 | bool IsDefinition; |
| 410 | if (!canBeWeakImported(IsDefinition)) |
| 411 | return false; |
| 412 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 413 | for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) { |
| 414 | if (isa<WeakImportAttr>(*A)) |
| 415 | return true; |
| 416 | |
| 417 | if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) { |
| 418 | if (CheckAvailability(getASTContext(), Availability, 0) |
| 419 | == AR_NotYetIntroduced) |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return false; |
| 425 | } |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 427 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 428 | switch (DeclKind) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 429 | case Function: |
| 430 | case CXXMethod: |
| 431 | case CXXConstructor: |
| 432 | case CXXDestructor: |
| 433 | case CXXConversion: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 434 | case EnumConstant: |
| 435 | case Var: |
| 436 | case ImplicitParam: |
| 437 | case ParmVar: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 438 | case NonTypeTemplateParm: |
| 439 | case ObjCMethod: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 440 | case ObjCProperty: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 441 | return IDNS_Ordinary; |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 442 | case Label: |
| 443 | return IDNS_Label; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 444 | case IndirectField: |
| 445 | return IDNS_Ordinary | IDNS_Member; |
| 446 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 447 | case ObjCCompatibleAlias: |
| 448 | case ObjCInterface: |
| 449 | return IDNS_Ordinary | IDNS_Type; |
| 450 | |
| 451 | case Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 452 | case TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 453 | case TypeAliasTemplate: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 454 | case UnresolvedUsingTypename: |
| 455 | case TemplateTypeParm: |
| 456 | return IDNS_Ordinary | IDNS_Type; |
| 457 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 458 | case UsingShadow: |
| 459 | return 0; // we'll actually overwrite this later |
| 460 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 461 | case UnresolvedUsingValue: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 462 | return IDNS_Ordinary | IDNS_Using; |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 463 | |
| 464 | case Using: |
| 465 | return IDNS_Using; |
| 466 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 467 | case ObjCProtocol: |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 468 | return IDNS_ObjCProtocol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 470 | case Field: |
| 471 | case ObjCAtDefsField: |
| 472 | case ObjCIvar: |
| 473 | return IDNS_Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 475 | case Record: |
| 476 | case CXXRecord: |
| 477 | case Enum: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 478 | return 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 | case Namespace: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 481 | case NamespaceAlias: |
| 482 | return IDNS_Namespace; |
| 483 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 484 | case FunctionTemplate: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 485 | return IDNS_Ordinary; |
| 486 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 487 | case ClassTemplate: |
| 488 | case TemplateTemplateParm: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 489 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 491 | // Never have names. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 492 | case Friend: |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 493 | case FriendTemplate: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 494 | case AccessSpec: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 495 | case LinkageSpec: |
| 496 | case FileScopeAsm: |
| 497 | case StaticAssert: |
| 498 | case ObjCClass: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 499 | case ObjCPropertyImpl: |
| 500 | case ObjCForwardProtocol: |
| 501 | case Block: |
| 502 | case TranslationUnit: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 504 | case UsingDirective: |
| 505 | case ClassTemplateSpecialization: |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 506 | case ClassTemplatePartialSpecialization: |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 507 | case ClassScopeFunctionSpecialization: |
Douglas Gregor | bd4187b | 2010-04-22 23:19:50 +0000 | [diff] [blame] | 508 | case ObjCImplementation: |
| 509 | case ObjCCategory: |
| 510 | case ObjCCategoryImpl: |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 511 | case Import: |
Douglas Gregor | bd4187b | 2010-04-22 23:19:50 +0000 | [diff] [blame] | 512 | // Never looked up by name. |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 513 | return 0; |
| 514 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 515 | |
| 516 | return 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 519 | void Decl::setAttrs(const AttrVec &attrs) { |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 520 | assert(!HasAttrs && "Decl already contains attrs."); |
| 521 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 522 | AttrVec &AttrBlank = getASTContext().getDeclAttrs(this); |
| 523 | assert(AttrBlank.empty() && "HasAttrs was wrong?"); |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 524 | |
| 525 | AttrBlank = attrs; |
| 526 | HasAttrs = true; |
| 527 | } |
| 528 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 529 | void Decl::dropAttrs() { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 530 | if (!HasAttrs) return; |
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 | HasAttrs = false; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 533 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 536 | const AttrVec &Decl::getAttrs() const { |
| 537 | assert(HasAttrs && "No attrs to get!"); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 538 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 541 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 542 | bool HasLHSAttr = this->HasAttrs; |
| 543 | bool HasRHSAttr = RHS->HasAttrs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 545 | // Usually, neither decl has attrs, nothing to do. |
| 546 | if (!HasLHSAttr && !HasRHSAttr) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 548 | // If 'this' has no attrs, swap the other way. |
| 549 | if (!HasLHSAttr) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 550 | return RHS->swapAttrs(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 552 | ASTContext &Context = getASTContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 554 | // Handle the case when both decls have attrs. |
| 555 | if (HasRHSAttr) { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 556 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 557 | return; |
| 558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 560 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 561 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 562 | Context.eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 563 | this->HasAttrs = false; |
| 564 | RHS->HasAttrs = true; |
| 565 | } |
| 566 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 567 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 568 | Decl::Kind DK = D->getDeclKind(); |
| 569 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 570 | #define DECL(NAME, BASE) |
| 571 | #define DECL_CONTEXT(NAME) \ |
| 572 | case Decl::NAME: \ |
| 573 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 574 | #define DECL_CONTEXT_BASE(NAME) |
| 575 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 576 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 577 | #define DECL(NAME, BASE) |
| 578 | #define DECL_CONTEXT_BASE(NAME) \ |
| 579 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 580 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 581 | #include "clang/AST/DeclNodes.inc" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 582 | llvm_unreachable("a decl that inherits DeclContext isn't handled"); |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 583 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 587 | Decl::Kind DK = D->getKind(); |
| 588 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 589 | #define DECL(NAME, BASE) |
| 590 | #define DECL_CONTEXT(NAME) \ |
| 591 | case Decl::NAME: \ |
| 592 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 593 | #define DECL_CONTEXT_BASE(NAME) |
| 594 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 595 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 596 | #define DECL(NAME, BASE) |
| 597 | #define DECL_CONTEXT_BASE(NAME) \ |
| 598 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 599 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 600 | #include "clang/AST/DeclNodes.inc" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 601 | llvm_unreachable("a decl that inherits DeclContext isn't handled"); |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 602 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 605 | SourceLocation Decl::getBodyRBrace() const { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 606 | // Special handling of FunctionDecl to avoid de-serializing the body from PCH. |
| 607 | // FunctionDecl stores EndRangeLoc for this purpose. |
| 608 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 609 | const FunctionDecl *Definition; |
| 610 | if (FD->hasBody(Definition)) |
| 611 | return Definition->getSourceRange().getEnd(); |
| 612 | return SourceLocation(); |
| 613 | } |
| 614 | |
Argyrios Kyrtzidis | 6717ef4 | 2010-07-07 11:31:27 +0000 | [diff] [blame] | 615 | if (Stmt *Body = getBody()) |
| 616 | return Body->getSourceRange().getEnd(); |
| 617 | |
| 618 | return SourceLocation(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 621 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 622 | #ifndef NDEBUG |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 623 | // Suppress this check if any of the following hold: |
| 624 | // 1. this is the translation unit (and thus has no parent) |
| 625 | // 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] | 626 | // 3. this is a non-type template parameter |
| 627 | // 4. the context is not a record |
| 628 | // 5. it's invalid |
| 629 | // 6. it's a C++0x static_assert. |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 630 | if (isa<TranslationUnitDecl>(this) || |
Argyrios Kyrtzidis | 04aed0e | 2010-07-02 11:55:44 +0000 | [diff] [blame] | 631 | isa<TemplateTypeParmDecl>(this) || |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 632 | isa<NonTypeTemplateParmDecl>(this) || |
Douglas Gregor | fdd8ab1 | 2010-02-22 17:53:38 +0000 | [diff] [blame] | 633 | !isa<CXXRecordDecl>(getDeclContext()) || |
Argyrios Kyrtzidis | 65b63ec | 2010-09-08 21:32:35 +0000 | [diff] [blame] | 634 | isInvalidDecl() || |
| 635 | isa<StaticAssertDecl>(this) || |
| 636 | // FIXME: a ParmVarDecl can have ClassTemplateSpecialization |
| 637 | // as DeclContext (?). |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 638 | isa<ParmVarDecl>(this) || |
| 639 | // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have |
| 640 | // AS_none as access specifier. |
Francois Pichet | bc84532 | 2011-08-17 01:06:54 +0000 | [diff] [blame] | 641 | isa<CXXRecordDecl>(this) || |
| 642 | isa<ClassScopeFunctionSpecializationDecl>(this)) |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 643 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
| 645 | assert(Access != AS_none && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 646 | "Access specifier is AS_none inside a record decl"); |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 647 | #endif |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 648 | } |
| 649 | |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 650 | DeclContext *Decl::getNonClosureContext() { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 651 | return getDeclContext()->getNonClosureAncestor(); |
| 652 | } |
| 653 | |
| 654 | DeclContext *DeclContext::getNonClosureAncestor() { |
| 655 | DeclContext *DC = this; |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 656 | |
| 657 | // This is basically "while (DC->isClosure()) DC = DC->getParent();" |
| 658 | // except that it's significantly more efficient to cast to a known |
| 659 | // decl type and call getDeclContext() than to call getParent(). |
John McCall | 7b3f853 | 2011-06-23 21:18:31 +0000 | [diff] [blame] | 660 | while (isa<BlockDecl>(DC)) |
| 661 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
John McCall | aab9e31 | 2011-02-22 22:25:23 +0000 | [diff] [blame] | 662 | |
| 663 | assert(!DC->isClosure()); |
| 664 | return DC; |
| 665 | } |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 666 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 667 | //===----------------------------------------------------------------------===// |
| 668 | // DeclContext Implementation |
| 669 | //===----------------------------------------------------------------------===// |
| 670 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 671 | bool DeclContext::classof(const Decl *D) { |
| 672 | switch (D->getKind()) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 673 | #define DECL(NAME, BASE) |
| 674 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 675 | #define DECL_CONTEXT_BASE(NAME) |
| 676 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 677 | return true; |
| 678 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 679 | #define DECL(NAME, BASE) |
| 680 | #define DECL_CONTEXT_BASE(NAME) \ |
| 681 | if (D->getKind() >= Decl::first##NAME && \ |
| 682 | D->getKind() <= Decl::last##NAME) \ |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 683 | return true; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 684 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | } |
| 688 | |
Douglas Gregor | a2da780 | 2010-07-25 18:38:02 +0000 | [diff] [blame] | 689 | DeclContext::~DeclContext() { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 691 | /// \brief Find the parent context of this context that will be |
| 692 | /// used for unqualified name lookup. |
| 693 | /// |
| 694 | /// Generally, the parent lookup context is the semantic context. However, for |
| 695 | /// a friend function the parent lookup context is the lexical context, which |
| 696 | /// is the class in which the friend is declared. |
| 697 | DeclContext *DeclContext::getLookupParent() { |
| 698 | // FIXME: Find a better way to identify friends |
| 699 | if (isa<FunctionDecl>(this)) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 700 | if (getParent()->getRedeclContext()->isFileContext() && |
| 701 | getLexicalParent()->getRedeclContext()->isRecord()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 702 | return getLexicalParent(); |
| 703 | |
| 704 | return getParent(); |
| 705 | } |
| 706 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 707 | bool DeclContext::isInlineNamespace() const { |
| 708 | return isNamespace() && |
| 709 | cast<NamespaceDecl>(this)->isInline(); |
| 710 | } |
| 711 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 712 | bool DeclContext::isDependentContext() const { |
| 713 | if (isFileContext()) |
| 714 | return false; |
| 715 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 716 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 717 | return true; |
| 718 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 719 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 720 | if (Record->getDescribedClassTemplate()) |
| 721 | return true; |
| 722 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 723 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 724 | if (Function->getDescribedFunctionTemplate()) |
| 725 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 727 | // Friend function declarations are dependent if their *lexical* |
| 728 | // context is dependent. |
| 729 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 730 | return getLexicalParent()->isDependentContext(); |
| 731 | } |
| 732 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 733 | return getParent() && getParent()->isDependentContext(); |
| 734 | } |
| 735 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 736 | bool DeclContext::isTransparentContext() const { |
| 737 | if (DeclKind == Decl::Enum) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 738 | return !cast<EnumDecl>(this)->isScoped(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 739 | else if (DeclKind == Decl::LinkageSpec) |
| 740 | return true; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 741 | |
| 742 | return false; |
| 743 | } |
| 744 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 745 | bool DeclContext::isExternCContext() const { |
| 746 | const DeclContext *DC = this; |
| 747 | while (DC->DeclKind != Decl::TranslationUnit) { |
| 748 | if (DC->DeclKind == Decl::LinkageSpec) |
| 749 | return cast<LinkageSpecDecl>(DC)->getLanguage() |
| 750 | == LinkageSpecDecl::lang_c; |
| 751 | DC = DC->getParent(); |
| 752 | } |
| 753 | return false; |
| 754 | } |
| 755 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 756 | bool DeclContext::Encloses(const DeclContext *DC) const { |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 757 | if (getPrimaryContext() != this) |
| 758 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 760 | for (; DC; DC = DC->getParent()) |
| 761 | if (DC->getPrimaryContext() == this) |
| 762 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | return false; |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 766 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 767 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 768 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 769 | case Decl::LinkageSpec: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 771 | // There is only one DeclContext for these entities. |
| 772 | return this; |
| 773 | |
| 774 | case Decl::Namespace: |
| 775 | // The original namespace is our primary context. |
| 776 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 777 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 778 | case Decl::ObjCMethod: |
| 779 | return this; |
| 780 | |
| 781 | case Decl::ObjCInterface: |
Douglas Gregor | 53df7a1 | 2011-12-15 18:03:09 +0000 | [diff] [blame] | 782 | if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition()) |
| 783 | return Def; |
| 784 | |
| 785 | return this; |
| 786 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 787 | case Decl::ObjCProtocol: |
Douglas Gregor | 53df7a1 | 2011-12-15 18:03:09 +0000 | [diff] [blame] | 788 | // FIXME: Update when protocols properly model forward declarations. |
| 789 | // For now, it's fine to fall through |
| 790 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 791 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 792 | return this; |
| 793 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 794 | case Decl::ObjCImplementation: |
| 795 | case Decl::ObjCCategoryImpl: |
| 796 | return this; |
| 797 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 798 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 799 | if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 800 | // If this is a tag type that has a definition or is currently |
| 801 | // being defined, that definition is our primary context. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 802 | TagDecl *Tag = cast<TagDecl>(this); |
| 803 | assert(isa<TagType>(Tag->TypeForDecl) || |
| 804 | isa<InjectedClassNameType>(Tag->TypeForDecl)); |
| 805 | |
| 806 | if (TagDecl *Def = Tag->getDefinition()) |
| 807 | return Def; |
| 808 | |
| 809 | if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) { |
| 810 | const TagType *TagTy = cast<TagType>(Tag->TypeForDecl); |
| 811 | if (TagTy->isBeingDefined()) |
| 812 | // FIXME: is it necessarily being defined in the decl |
| 813 | // that owns the type? |
| 814 | return TagTy->getDecl(); |
| 815 | } |
| 816 | |
| 817 | return Tag; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 820 | assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 821 | "Unknown DeclContext kind"); |
| 822 | return this; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | DeclContext *DeclContext::getNextContext() { |
| 827 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 828 | case Decl::Namespace: |
| 829 | // Return the next namespace |
| 830 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 831 | |
| 832 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | } |
| 836 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 837 | std::pair<Decl *, Decl *> |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 838 | DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls, |
| 839 | bool FieldsAlreadyLoaded) { |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 840 | // Build up a chain of declarations via the Decl::NextDeclInContext field. |
| 841 | Decl *FirstNewDecl = 0; |
| 842 | Decl *PrevDecl = 0; |
| 843 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 844 | if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I])) |
| 845 | continue; |
| 846 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 847 | Decl *D = Decls[I]; |
| 848 | if (PrevDecl) |
| 849 | PrevDecl->NextDeclInContext = D; |
| 850 | else |
| 851 | FirstNewDecl = D; |
| 852 | |
| 853 | PrevDecl = D; |
| 854 | } |
| 855 | |
| 856 | return std::make_pair(FirstNewDecl, PrevDecl); |
| 857 | } |
| 858 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 859 | /// \brief Load the declarations within this lexical storage from an |
| 860 | /// external source. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 862 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 863 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 864 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 865 | |
Argyrios Kyrtzidis | 0dbbc04 | 2010-07-30 10:03:23 +0000 | [diff] [blame] | 866 | // Notify that we have a DeclContext that is initializing. |
| 867 | ExternalASTSource::Deserializing ADeclContext(Source); |
Douglas Gregor | 9fc18c9 | 2011-08-26 21:23:06 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 869 | // Load the external declarations, if any. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 870 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 871 | ExternalLexicalStorage = false; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 872 | switch (Source->FindExternalLexicalDecls(this, Decls)) { |
| 873 | case ELR_Success: |
| 874 | break; |
| 875 | |
| 876 | case ELR_Failure: |
| 877 | case ELR_AlreadyLoaded: |
| 878 | return; |
| 879 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 880 | |
| 881 | if (Decls.empty()) |
| 882 | return; |
| 883 | |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 884 | // We may have already loaded just the fields of this record, in which case |
| 885 | // we need to ignore them. |
| 886 | bool FieldsAlreadyLoaded = false; |
| 887 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(this)) |
| 888 | FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage; |
| 889 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 890 | // Splice the newly-read declarations into the beginning of the list |
| 891 | // of declarations. |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 892 | Decl *ExternalFirst, *ExternalLast; |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 893 | llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls, |
| 894 | FieldsAlreadyLoaded); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 895 | ExternalLast->NextDeclInContext = FirstDecl; |
| 896 | FirstDecl = ExternalFirst; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 897 | if (!LastDecl) |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 898 | LastDecl = ExternalLast; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 899 | } |
| 900 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 901 | DeclContext::lookup_result |
| 902 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 903 | DeclarationName Name) { |
| 904 | ASTContext &Context = DC->getParentASTContext(); |
| 905 | StoredDeclsMap *Map; |
| 906 | if (!(Map = DC->LookupPtr)) |
| 907 | Map = DC->CreateStoredDeclsMap(Context); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 908 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 909 | StoredDeclsList &List = (*Map)[Name]; |
| 910 | assert(List.isNull()); |
| 911 | (void) List; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 912 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 913 | return DeclContext::lookup_result(); |
| 914 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 915 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 916 | DeclContext::lookup_result |
| 917 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 918 | DeclarationName Name, |
Argyrios Kyrtzidis | 45df9c6 | 2011-09-09 06:44:14 +0000 | [diff] [blame] | 919 | ArrayRef<NamedDecl*> Decls) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 920 | ASTContext &Context = DC->getParentASTContext();; |
| 921 | |
| 922 | StoredDeclsMap *Map; |
| 923 | if (!(Map = DC->LookupPtr)) |
| 924 | Map = DC->CreateStoredDeclsMap(Context); |
| 925 | |
| 926 | StoredDeclsList &List = (*Map)[Name]; |
Argyrios Kyrtzidis | 45df9c6 | 2011-09-09 06:44:14 +0000 | [diff] [blame] | 927 | for (ArrayRef<NamedDecl*>::iterator |
| 928 | I = Decls.begin(), E = Decls.end(); I != E; ++I) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 929 | if (List.isNull()) |
Argyrios Kyrtzidis | 45df9c6 | 2011-09-09 06:44:14 +0000 | [diff] [blame] | 930 | List.setOnlyValue(*I); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 931 | else |
Argyrios Kyrtzidis | 45df9c6 | 2011-09-09 06:44:14 +0000 | [diff] [blame] | 932 | List.AddSubsequentDecl(*I); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 935 | return List.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 938 | DeclContext::decl_iterator DeclContext::noload_decls_begin() const { |
| 939 | return decl_iterator(FirstDecl); |
| 940 | } |
| 941 | |
| 942 | DeclContext::decl_iterator DeclContext::noload_decls_end() const { |
| 943 | return decl_iterator(); |
| 944 | } |
| 945 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 946 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 947 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 948 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 953 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 954 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 955 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 956 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | return decl_iterator(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 960 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 961 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 962 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 963 | |
| 964 | return !FirstDecl; |
| 965 | } |
| 966 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 967 | void DeclContext::removeDecl(Decl *D) { |
| 968 | assert(D->getLexicalDeclContext() == this && |
| 969 | "decl being removed from non-lexical context"); |
| 970 | assert((D->NextDeclInContext || D == LastDecl) && |
| 971 | "decl is not in decls list"); |
| 972 | |
| 973 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 974 | if (D == FirstDecl) { |
| 975 | if (D == LastDecl) |
| 976 | FirstDecl = LastDecl = 0; |
| 977 | else |
| 978 | FirstDecl = D->NextDeclInContext; |
| 979 | } else { |
| 980 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 981 | assert(I && "decl not found in linked list"); |
| 982 | if (I->NextDeclInContext == D) { |
| 983 | I->NextDeclInContext = D->NextDeclInContext; |
| 984 | if (D == LastDecl) LastDecl = I; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // Mark that D is no longer in the decl chain. |
| 991 | D->NextDeclInContext = 0; |
| 992 | |
| 993 | // Remove D from the lookup table if necessary. |
| 994 | if (isa<NamedDecl>(D)) { |
| 995 | NamedDecl *ND = cast<NamedDecl>(D); |
| 996 | |
Axel Naumann | 02368d0 | 2011-08-26 14:06:12 +0000 | [diff] [blame] | 997 | // Remove only decls that have a name |
| 998 | if (!ND->getDeclName()) return; |
| 999 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1000 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 1001 | if (!Map) return; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1002 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1003 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 1004 | assert(Pos != Map->end() && "no lookup entry for decl"); |
Axel Naumann | d9d137e | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 1005 | if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND) |
| 1006 | Pos->second.remove(ND); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1010 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 1011 | assert(D->getLexicalDeclContext() == this && |
| 1012 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1014 | "Decl already inserted into a DeclContext"); |
| 1015 | |
| 1016 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 1017 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1018 | LastDecl = D; |
| 1019 | } else { |
| 1020 | FirstDecl = LastDecl = D; |
| 1021 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Notify a C++ record declaration that we've added a member, so it can |
| 1024 | // update it's class-specific state. |
| 1025 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 1026 | Record->addedMember(D); |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1027 | |
| 1028 | // If this is a newly-created (not de-serialized) import declaration, wire |
| 1029 | // it in to the list of local import declarations. |
| 1030 | if (!D->isFromASTFile()) { |
| 1031 | if (ImportDecl *Import = dyn_cast<ImportDecl>(D)) |
| 1032 | D->getASTContext().addedLocalImportDecl(Import); |
| 1033 | } |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | void DeclContext::addDecl(Decl *D) { |
| 1037 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1040 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1043 | void DeclContext::addDeclInternal(Decl *D) { |
| 1044 | addHiddenDecl(D); |
| 1045 | |
| 1046 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 1047 | ND->getDeclContext()->makeDeclVisibleInContextInternal(ND); |
| 1048 | } |
| 1049 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1050 | /// buildLookup - Build the lookup data structure with all of the |
| 1051 | /// declarations in DCtx (and any other contexts linked to it or |
| 1052 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1053 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1054 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | for (decl_iterator D = DCtx->decls_begin(), |
| 1056 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 1057 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1058 | // Insert this declaration into the lookup structure, but only |
| 1059 | // if it's semantically in its decl context. During non-lazy |
| 1060 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1061 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1062 | if (D->getDeclContext() == DCtx) |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1063 | makeDeclVisibleInContextImpl(ND, false); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1064 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1065 | // Insert any forward-declared Objective-C interface into the lookup |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1066 | // data structure. |
| 1067 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1068 | makeDeclVisibleInContextImpl(Class->getForwardInterfaceDecl(), false); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1069 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1070 | // If this declaration is itself a transparent declaration context or |
| 1071 | // inline namespace, add its members (recursively). |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1072 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1073 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1074 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1080 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1081 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1082 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1083 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1084 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1085 | if (hasExternalVisibleStorage()) { |
| 1086 | // Check to see if we've already cached the lookup results. |
| 1087 | if (LookupPtr) { |
| 1088 | StoredDeclsMap::iterator I = LookupPtr->find(Name); |
| 1089 | if (I != LookupPtr->end()) |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1090 | return I->second.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1094 | return Source->FindExternalVisibleDeclsByName(this, Name); |
| 1095 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1096 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1097 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1098 | /// all of the linked DeclContexts (in declaration order!) and |
| 1099 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1100 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1101 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1103 | if (!LookupPtr) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1104 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 1105 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1106 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1107 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 1108 | if (Pos == LookupPtr->end()) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 1109 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1110 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1114 | DeclContext::lookup(DeclarationName Name) const { |
| 1115 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Douglas Gregor | b75a345 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 1118 | void DeclContext::localUncachedLookup(DeclarationName Name, |
| 1119 | llvm::SmallVectorImpl<NamedDecl *> &Results) { |
| 1120 | Results.clear(); |
| 1121 | |
| 1122 | // If there's no external storage, just perform a normal lookup and copy |
| 1123 | // the results. |
| 1124 | if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) { |
| 1125 | lookup_result LookupResults = lookup(Name); |
| 1126 | Results.insert(Results.end(), LookupResults.first, LookupResults.second); |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | // If we have a lookup table, check there first. Maybe we'll get lucky. |
| 1131 | if (LookupPtr) { |
| 1132 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 1133 | if (Pos != LookupPtr->end()) { |
| 1134 | Results.insert(Results.end(), |
| 1135 | Pos->second.getLookupResult().first, |
| 1136 | Pos->second.getLookupResult().second); |
| 1137 | return; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | // Slow case: grovel through the declarations in our chain looking for |
| 1142 | // matches. |
| 1143 | for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) { |
| 1144 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 1145 | if (ND->getDeclName() == Name) |
| 1146 | Results.push_back(ND); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1150 | DeclContext *DeclContext::getRedeclContext() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 1151 | DeclContext *Ctx = this; |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1152 | // Skip through transparent contexts. |
| 1153 | while (Ctx->isTransparentContext()) |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1154 | Ctx = Ctx->getParent(); |
| 1155 | return Ctx; |
| 1156 | } |
| 1157 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1158 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 1159 | DeclContext *Ctx = this; |
| 1160 | // Skip through non-namespace, non-translation-unit contexts. |
Sebastian Redl | 51a8a37 | 2010-08-31 00:36:23 +0000 | [diff] [blame] | 1161 | while (!Ctx->isFileContext()) |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1162 | Ctx = Ctx->getParent(); |
| 1163 | return Ctx->getPrimaryContext(); |
| 1164 | } |
| 1165 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1166 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 1167 | // For non-file contexts, this is equivalent to Equals. |
| 1168 | if (!isFileContext()) |
| 1169 | return O->Equals(this); |
| 1170 | |
| 1171 | do { |
| 1172 | if (O->Equals(this)) |
| 1173 | return true; |
| 1174 | |
| 1175 | const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O); |
| 1176 | if (!NS || !NS->isInline()) |
| 1177 | break; |
| 1178 | O = NS->getParent(); |
| 1179 | } while (O); |
| 1180 | |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1184 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) |
| 1185 | { |
| 1186 | makeDeclVisibleInContextWithFlags(D, false, Recoverable); |
| 1187 | } |
| 1188 | |
| 1189 | void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable) |
| 1190 | { |
| 1191 | makeDeclVisibleInContextWithFlags(D, true, Recoverable); |
| 1192 | } |
| 1193 | |
| 1194 | void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1195 | // FIXME: This feels like a hack. Should DeclarationName support |
| 1196 | // template-ids, or is there a better way to keep specializations |
| 1197 | // from being visible? |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1198 | if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1199 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 1200 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 1201 | if (FD->isFunctionTemplateSpecialization()) |
| 1202 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1203 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1204 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1205 | if (PrimaryContext != this) { |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1206 | PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | // If we already have a lookup data structure, perform the insertion |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1211 | // into it. If we haven't deserialized externally stored decls, deserialize |
| 1212 | // them so we can add the decl. Otherwise, be lazy and don't build that |
| 1213 | // structure until someone asks for it. |
| 1214 | if (LookupPtr || !Recoverable || hasExternalVisibleStorage()) |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1215 | makeDeclVisibleInContextImpl(D, Internal); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1216 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 1217 | // If we are a transparent context or inline namespace, insert into our |
| 1218 | // parent context, too. This operation is recursive. |
| 1219 | if (isTransparentContext() || isInlineNamespace()) |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1220 | getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 1221 | |
| 1222 | Decl *DCAsDecl = cast<Decl>(this); |
| 1223 | // Notify that a decl was made visible unless it's a Tag being defined. |
| 1224 | if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined())) |
| 1225 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 1226 | L->AddedVisibleDecl(this, D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1229 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1230 | // Skip unnamed declarations. |
| 1231 | if (!D->getDeclName()) |
| 1232 | return; |
| 1233 | |
Douglas Gregor | 5cb0ef4 | 2011-05-06 23:32:38 +0000 | [diff] [blame] | 1234 | // Skip entities that can't be found by name lookup into a particular |
| 1235 | // context. |
| 1236 | if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) || |
| 1237 | D->isTemplateParameter()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 1240 | ASTContext *C = 0; |
| 1241 | if (!LookupPtr) { |
| 1242 | C = &getParentASTContext(); |
| 1243 | CreateStoredDeclsMap(*C); |
| 1244 | } |
| 1245 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1246 | // If there is an external AST source, load any declarations it knows about |
| 1247 | // with this declaration's name. |
| 1248 | // If the lookup table contains an entry about this name it means that we |
| 1249 | // have already checked the external source. |
Sean Callanan | 9faf810 | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 1250 | if (!Internal) |
| 1251 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 1252 | if (hasExternalVisibleStorage() && |
| 1253 | LookupPtr->find(D->getDeclName()) == LookupPtr->end()) |
| 1254 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1255 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1256 | // Insert this declaration into the map. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1257 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1258 | if (DeclNameEntries.isNull()) { |
| 1259 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1260 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 1263 | // If it is possible that this is a redeclaration, check to see if there is |
| 1264 | // already a decl for which declarationReplaces returns true. If there is |
| 1265 | // one, just replace it and return. |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1266 | if (DeclNameEntries.HandleRedeclaration(D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1267 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1269 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1270 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1271 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1272 | |
| 1273 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 1274 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1276 | DeclContext::getUsingDirectives() const { |
| 1277 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1278 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 1279 | reinterpret_cast<udir_iterator>(Result.second)); |
| 1280 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1281 | |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1282 | //===----------------------------------------------------------------------===// |
| 1283 | // Creation and Destruction of StoredDeclsMaps. // |
| 1284 | //===----------------------------------------------------------------------===// |
| 1285 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1286 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 1287 | assert(!LookupPtr && "context already has a decls map"); |
| 1288 | assert(getPrimaryContext() == this && |
| 1289 | "creating decls map on non-primary context"); |
| 1290 | |
| 1291 | StoredDeclsMap *M; |
| 1292 | bool Dependent = isDependentContext(); |
| 1293 | if (Dependent) |
| 1294 | M = new DependentStoredDeclsMap(); |
| 1295 | else |
| 1296 | M = new StoredDeclsMap(); |
| 1297 | M->Previous = C.LastSDM; |
| 1298 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 1299 | LookupPtr = M; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1300 | return M; |
| 1301 | } |
| 1302 | |
| 1303 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1304 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 1305 | // pointer because the subclass doesn't add anything that needs to |
| 1306 | // be deleted. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1307 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 1308 | } |
| 1309 | |
| 1310 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 1311 | while (Map) { |
| 1312 | // Advance the iteration before we invalidate memory. |
| 1313 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 1314 | |
| 1315 | if (Dependent) |
| 1316 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 1317 | else |
| 1318 | delete Map; |
| 1319 | |
| 1320 | Map = Next.getPointer(); |
| 1321 | Dependent = Next.getInt(); |
| 1322 | } |
| 1323 | } |
| 1324 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1325 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1326 | DeclContext *Parent, |
| 1327 | const PartialDiagnostic &PDiag) { |
| 1328 | assert(Parent->isDependentContext() |
| 1329 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1330 | Parent = Parent->getPrimaryContext(); |
| 1331 | if (!Parent->LookupPtr) |
| 1332 | Parent->CreateStoredDeclsMap(C); |
| 1333 | |
| 1334 | DependentStoredDeclsMap *Map |
| 1335 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1336 | |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1337 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1338 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1339 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1340 | if (PDiag.hasStorage()) |
| 1341 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1342 | |
| 1343 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1344 | |
| 1345 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1346 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1347 | Map->FirstDiagnostic = DD; |
| 1348 | |
| 1349 | return DD; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1350 | } |