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