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