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