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