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