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