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