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