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