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