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