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