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