Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 1 | //===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl and DeclContext classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclBase.h" |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 24 | #include "clang/AST/Type.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 27 | #include "clang/AST/ASTMutationListener.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 31 | #include <cstdio> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 32 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Statistics |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 39 | #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0; |
| 40 | #define ABSTRACT_DECL(DECL) |
| 41 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 42 | |
| 43 | static bool StatSwitch = false; |
| 44 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 45 | const char *Decl::getDeclKindName() const { |
| 46 | switch (DeclKind) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 47 | default: assert(0 && "Declaration not in DeclNodes.inc!"); |
| 48 | #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED; |
| 49 | #define ABSTRACT_DECL(DECL) |
| 50 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Douglas Gregor | 4273857 | 2010-03-05 00:26:45 +0000 | [diff] [blame] | 54 | void Decl::setInvalidDecl(bool Invalid) { |
| 55 | InvalidDecl = Invalid; |
| 56 | if (Invalid) { |
| 57 | // Defensive maneuver for ill-formed code: we're likely not to make it to |
| 58 | // a point where we set the access specifier, so default it to "public" |
| 59 | // to avoid triggering asserts elsewhere in the front end. |
| 60 | setAccess(AS_public); |
| 61 | } |
| 62 | } |
| 63 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 64 | const char *DeclContext::getDeclKindName() const { |
| 65 | switch (DeclKind) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 66 | default: assert(0 && "Declaration context not in DeclNodes.inc!"); |
| 67 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED; |
| 68 | #define ABSTRACT_DECL(DECL) |
| 69 | #include "clang/AST/DeclNodes.inc" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 73 | bool Decl::CollectingStats(bool Enable) { |
Kovarththanan Rajaratnam | 2024f4d | 2009-11-29 14:54:35 +0000 | [diff] [blame] | 74 | if (Enable) StatSwitch = true; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 75 | return StatSwitch; |
| 76 | } |
| 77 | |
| 78 | void Decl::PrintStats() { |
| 79 | fprintf(stderr, "*** Decl Stats:\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 81 | int totalDecls = 0; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 82 | #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s; |
| 83 | #define ABSTRACT_DECL(DECL) |
| 84 | #include "clang/AST/DeclNodes.inc" |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 85 | fprintf(stderr, " %d decls total.\n", totalDecls); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 87 | int totalBytes = 0; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 88 | #define DECL(DERIVED, BASE) \ |
| 89 | if (n##DERIVED##s > 0) { \ |
| 90 | totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \ |
| 91 | fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \ |
| 92 | n##DERIVED##s, (int)sizeof(DERIVED##Decl), \ |
| 93 | (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \ |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 94 | } |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 95 | #define ABSTRACT_DECL(DECL) |
| 96 | #include "clang/AST/DeclNodes.inc" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 98 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 101 | void Decl::add(Kind k) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 102 | switch (k) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 103 | default: assert(0 && "Declaration not in DeclNodes.inc!"); |
| 104 | #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break; |
| 105 | #define ABSTRACT_DECL(DECL) |
| 106 | #include "clang/AST/DeclNodes.inc" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 110 | bool Decl::isTemplateParameterPack() const { |
| 111 | if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) |
| 112 | return TTP->isParameterPack(); |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 113 | if (const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 114 | = dyn_cast<NonTypeTemplateParmDecl>(this)) |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 115 | return NTTP->isParameterPack(); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 116 | if (const TemplateTemplateParmDecl *TTP |
| 117 | = dyn_cast<TemplateTemplateParmDecl>(this)) |
| 118 | return TTP->isParameterPack(); |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 122 | bool Decl::isParameterPack() const { |
| 123 | if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this)) |
| 124 | return Parm->isParameterPack(); |
| 125 | |
| 126 | return isTemplateParameterPack(); |
| 127 | } |
| 128 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 129 | bool Decl::isFunctionOrFunctionTemplate() const { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 130 | if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this)) |
Anders Carlsson | 58badb7 | 2009-06-26 05:26:50 +0000 | [diff] [blame] | 131 | return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 133 | return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); |
| 134 | } |
| 135 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 136 | bool Decl::isDefinedOutsideFunctionOrMethod() const { |
| 137 | for (const DeclContext *DC = getDeclContext(); |
| 138 | DC && !DC->isTranslationUnit(); |
| 139 | DC = DC->getParent()) |
| 140 | if (DC->isFunctionOrMethod()) |
| 141 | return false; |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Douglas Gregor | 4c3e0ee | 2011-02-17 08:47:29 +0000 | [diff] [blame] | 146 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 148 | // PrettyStackTraceDecl Implementation |
| 149 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 151 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 152 | SourceLocation TheLoc = Loc; |
| 153 | if (TheLoc.isInvalid() && TheDecl) |
| 154 | TheLoc = TheDecl->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 156 | if (TheLoc.isValid()) { |
| 157 | TheLoc.print(OS, SM); |
| 158 | OS << ": "; |
| 159 | } |
| 160 | |
| 161 | OS << Message; |
| 162 | |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 163 | if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 164 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 165 | OS << '\n'; |
| 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 169 | // Decl Implementation |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame^] | 172 | // Out-of-line virtual method providing a home for Decl. |
| 173 | Decl::~Decl() { } |
Douglas Gregor | f4a03cc | 2011-02-17 07:02:32 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 175 | void Decl::setDeclContext(DeclContext *DC) { |
| 176 | if (isOutOfSemaDC()) |
| 177 | delete getMultipleDC(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 179 | DeclCtx = DC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 183 | if (DC == getLexicalDeclContext()) |
| 184 | return; |
| 185 | |
| 186 | if (isInSemaDC()) { |
Ted Kremenek | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 187 | MultipleDC *MDC = new (getASTContext()) MultipleDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 188 | MDC->SemanticDC = getDeclContext(); |
| 189 | MDC->LexicalDC = DC; |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 190 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 191 | } else { |
| 192 | getMultipleDC()->LexicalDC = DC; |
| 193 | } |
| 194 | } |
| 195 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 196 | bool Decl::isInAnonymousNamespace() const { |
| 197 | const DeclContext *DC = getDeclContext(); |
| 198 | do { |
| 199 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) |
| 200 | if (ND->isAnonymousNamespace()) |
| 201 | return true; |
| 202 | } while ((DC = DC->getParent())); |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 207 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argyrios Kyrtzidis | 9b34669 | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 208 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 209 | return TUD; |
| 210 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 211 | DeclContext *DC = getDeclContext(); |
| 212 | assert(DC && "This decl is not contained in a translation unit!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 214 | while (!DC->isTranslationUnit()) { |
| 215 | DC = DC->getParent(); |
| 216 | assert(DC && "This decl is not contained in a translation unit!"); |
| 217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 219 | return cast<TranslationUnitDecl>(DC); |
| 220 | } |
| 221 | |
| 222 | ASTContext &Decl::getASTContext() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | return getTranslationUnitDecl()->getASTContext(); |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 226 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 227 | return getASTContext().getASTMutationListener(); |
| 228 | } |
| 229 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 230 | bool Decl::isUsed(bool CheckUsedAttr) const { |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 231 | if (Used) |
| 232 | return true; |
| 233 | |
| 234 | // Check for used attribute. |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 235 | if (CheckUsedAttr && hasAttr<UsedAttr>()) |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 236 | return true; |
| 237 | |
| 238 | // Check redeclarations for used attribute. |
| 239 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 240 | if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used) |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 248 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 249 | switch (DeclKind) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 250 | case Function: |
| 251 | case CXXMethod: |
| 252 | case CXXConstructor: |
| 253 | case CXXDestructor: |
| 254 | case CXXConversion: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 255 | case EnumConstant: |
| 256 | case Var: |
| 257 | case ImplicitParam: |
| 258 | case ParmVar: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 259 | case NonTypeTemplateParm: |
| 260 | case ObjCMethod: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 261 | case ObjCProperty: |
Daniel Dunbar | 00b40d3 | 2010-04-23 13:07:39 +0000 | [diff] [blame] | 262 | return IDNS_Ordinary; |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 263 | case Label: |
| 264 | return IDNS_Label; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 265 | case IndirectField: |
| 266 | return IDNS_Ordinary | IDNS_Member; |
| 267 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 268 | case ObjCCompatibleAlias: |
| 269 | case ObjCInterface: |
| 270 | return IDNS_Ordinary | IDNS_Type; |
| 271 | |
| 272 | case Typedef: |
| 273 | case UnresolvedUsingTypename: |
| 274 | case TemplateTypeParm: |
| 275 | return IDNS_Ordinary | IDNS_Type; |
| 276 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 277 | case UsingShadow: |
| 278 | return 0; // we'll actually overwrite this later |
| 279 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 280 | case UnresolvedUsingValue: |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 281 | return IDNS_Ordinary | IDNS_Using; |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 282 | |
| 283 | case Using: |
| 284 | return IDNS_Using; |
| 285 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 286 | case ObjCProtocol: |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 287 | return IDNS_ObjCProtocol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 289 | case Field: |
| 290 | case ObjCAtDefsField: |
| 291 | case ObjCIvar: |
| 292 | return IDNS_Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 294 | case Record: |
| 295 | case CXXRecord: |
| 296 | case Enum: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 297 | return IDNS_Tag | IDNS_Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 299 | case Namespace: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 300 | case NamespaceAlias: |
| 301 | return IDNS_Namespace; |
| 302 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 303 | case FunctionTemplate: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 304 | return IDNS_Ordinary; |
| 305 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 306 | case ClassTemplate: |
| 307 | case TemplateTemplateParm: |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 308 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 310 | // Never have names. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 311 | case Friend: |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 312 | case FriendTemplate: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 313 | case AccessSpec: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 314 | case LinkageSpec: |
| 315 | case FileScopeAsm: |
| 316 | case StaticAssert: |
| 317 | case ObjCClass: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 318 | case ObjCPropertyImpl: |
| 319 | case ObjCForwardProtocol: |
| 320 | case Block: |
| 321 | case TranslationUnit: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 323 | case UsingDirective: |
| 324 | case ClassTemplateSpecialization: |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 325 | case ClassTemplatePartialSpecialization: |
Douglas Gregor | bd4187b | 2010-04-22 23:19:50 +0000 | [diff] [blame] | 326 | case ObjCImplementation: |
| 327 | case ObjCCategory: |
| 328 | case ObjCCategoryImpl: |
| 329 | // Never looked up by name. |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 330 | return 0; |
| 331 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 332 | |
| 333 | return 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 336 | void Decl::setAttrs(const AttrVec &attrs) { |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 337 | assert(!HasAttrs && "Decl already contains attrs."); |
| 338 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 339 | AttrVec &AttrBlank = getASTContext().getDeclAttrs(this); |
| 340 | assert(AttrBlank.empty() && "HasAttrs was wrong?"); |
Argyrios Kyrtzidis | 1715bf5 | 2010-06-11 23:09:25 +0000 | [diff] [blame] | 341 | |
| 342 | AttrBlank = attrs; |
| 343 | HasAttrs = true; |
| 344 | } |
| 345 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 346 | void Decl::dropAttrs() { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 347 | if (!HasAttrs) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 349 | HasAttrs = false; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 350 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 353 | const AttrVec &Decl::getAttrs() const { |
| 354 | assert(HasAttrs && "No attrs to get!"); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 355 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 358 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 359 | bool HasLHSAttr = this->HasAttrs; |
| 360 | bool HasRHSAttr = RHS->HasAttrs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 362 | // Usually, neither decl has attrs, nothing to do. |
| 363 | if (!HasLHSAttr && !HasRHSAttr) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 365 | // If 'this' has no attrs, swap the other way. |
| 366 | if (!HasLHSAttr) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 367 | return RHS->swapAttrs(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 369 | ASTContext &Context = getASTContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 371 | // Handle the case when both decls have attrs. |
| 372 | if (HasRHSAttr) { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 373 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 374 | return; |
| 375 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 377 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 378 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 379 | Context.eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 380 | this->HasAttrs = false; |
| 381 | RHS->HasAttrs = true; |
| 382 | } |
| 383 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 384 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 385 | Decl::Kind DK = D->getDeclKind(); |
| 386 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 387 | #define DECL(NAME, BASE) |
| 388 | #define DECL_CONTEXT(NAME) \ |
| 389 | case Decl::NAME: \ |
| 390 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 391 | #define DECL_CONTEXT_BASE(NAME) |
| 392 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 393 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 394 | #define DECL(NAME, BASE) |
| 395 | #define DECL_CONTEXT_BASE(NAME) \ |
| 396 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 397 | return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D)); |
| 398 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 399 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 400 | return 0; |
| 401 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 405 | Decl::Kind DK = D->getKind(); |
| 406 | switch(DK) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 407 | #define DECL(NAME, BASE) |
| 408 | #define DECL_CONTEXT(NAME) \ |
| 409 | case Decl::NAME: \ |
| 410 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 411 | #define DECL_CONTEXT_BASE(NAME) |
| 412 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 413 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 414 | #define DECL(NAME, BASE) |
| 415 | #define DECL_CONTEXT_BASE(NAME) \ |
| 416 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 417 | return static_cast<NAME##Decl*>(const_cast<Decl*>(D)); |
| 418 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 419 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 420 | return 0; |
| 421 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 424 | SourceLocation Decl::getBodyRBrace() const { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 425 | // Special handling of FunctionDecl to avoid de-serializing the body from PCH. |
| 426 | // FunctionDecl stores EndRangeLoc for this purpose. |
| 427 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) { |
| 428 | const FunctionDecl *Definition; |
| 429 | if (FD->hasBody(Definition)) |
| 430 | return Definition->getSourceRange().getEnd(); |
| 431 | return SourceLocation(); |
| 432 | } |
| 433 | |
Argyrios Kyrtzidis | 6717ef4 | 2010-07-07 11:31:27 +0000 | [diff] [blame] | 434 | if (Stmt *Body = getBody()) |
| 435 | return Body->getSourceRange().getEnd(); |
| 436 | |
| 437 | return SourceLocation(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 440 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 441 | #ifndef NDEBUG |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 442 | // Suppress this check if any of the following hold: |
| 443 | // 1. this is the translation unit (and thus has no parent) |
| 444 | // 2. this is a template parameter (and thus doesn't belong to its context) |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 445 | // 3. this is a non-type template parameter |
| 446 | // 4. the context is not a record |
| 447 | // 5. it's invalid |
| 448 | // 6. it's a C++0x static_assert. |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 449 | if (isa<TranslationUnitDecl>(this) || |
Argyrios Kyrtzidis | 04aed0e | 2010-07-02 11:55:44 +0000 | [diff] [blame] | 450 | isa<TemplateTypeParmDecl>(this) || |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 451 | isa<NonTypeTemplateParmDecl>(this) || |
Douglas Gregor | fdd8ab1 | 2010-02-22 17:53:38 +0000 | [diff] [blame] | 452 | !isa<CXXRecordDecl>(getDeclContext()) || |
Argyrios Kyrtzidis | 65b63ec | 2010-09-08 21:32:35 +0000 | [diff] [blame] | 453 | isInvalidDecl() || |
| 454 | isa<StaticAssertDecl>(this) || |
| 455 | // FIXME: a ParmVarDecl can have ClassTemplateSpecialization |
| 456 | // as DeclContext (?). |
Argyrios Kyrtzidis | d580e56 | 2010-09-08 21:58:42 +0000 | [diff] [blame] | 457 | isa<ParmVarDecl>(this) || |
| 458 | // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have |
| 459 | // AS_none as access specifier. |
| 460 | isa<CXXRecordDecl>(this)) |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 461 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
| 463 | assert(Access != AS_none && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 464 | "Access specifier is AS_none inside a record decl"); |
Douglas Gregor | 3a1c36c | 2010-12-02 00:22:25 +0000 | [diff] [blame] | 465 | #endif |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 468 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
| 470 | // DeclContext Implementation |
| 471 | //===----------------------------------------------------------------------===// |
| 472 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 473 | bool DeclContext::classof(const Decl *D) { |
| 474 | switch (D->getKind()) { |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 475 | #define DECL(NAME, BASE) |
| 476 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 477 | #define DECL_CONTEXT_BASE(NAME) |
| 478 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 479 | return true; |
| 480 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 481 | #define DECL(NAME, BASE) |
| 482 | #define DECL_CONTEXT_BASE(NAME) \ |
| 483 | if (D->getKind() >= Decl::first##NAME && \ |
| 484 | D->getKind() <= Decl::last##NAME) \ |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 485 | return true; |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 486 | #include "clang/AST/DeclNodes.inc" |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
Douglas Gregor | a2da780 | 2010-07-25 18:38:02 +0000 | [diff] [blame] | 491 | DeclContext::~DeclContext() { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 493 | /// \brief Find the parent context of this context that will be |
| 494 | /// used for unqualified name lookup. |
| 495 | /// |
| 496 | /// Generally, the parent lookup context is the semantic context. However, for |
| 497 | /// a friend function the parent lookup context is the lexical context, which |
| 498 | /// is the class in which the friend is declared. |
| 499 | DeclContext *DeclContext::getLookupParent() { |
| 500 | // FIXME: Find a better way to identify friends |
| 501 | if (isa<FunctionDecl>(this)) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 502 | if (getParent()->getRedeclContext()->isFileContext() && |
| 503 | getLexicalParent()->getRedeclContext()->isRecord()) |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 504 | return getLexicalParent(); |
| 505 | |
| 506 | return getParent(); |
| 507 | } |
| 508 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 509 | bool DeclContext::isInlineNamespace() const { |
| 510 | return isNamespace() && |
| 511 | cast<NamespaceDecl>(this)->isInline(); |
| 512 | } |
| 513 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 514 | bool DeclContext::isDependentContext() const { |
| 515 | if (isFileContext()) |
| 516 | return false; |
| 517 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 518 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 519 | return true; |
| 520 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 521 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 522 | if (Record->getDescribedClassTemplate()) |
| 523 | return true; |
| 524 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 525 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 526 | if (Function->getDescribedFunctionTemplate()) |
| 527 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 529 | // Friend function declarations are dependent if their *lexical* |
| 530 | // context is dependent. |
| 531 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 532 | return getLexicalParent()->isDependentContext(); |
| 533 | } |
| 534 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 535 | return getParent() && getParent()->isDependentContext(); |
| 536 | } |
| 537 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 538 | bool DeclContext::isTransparentContext() const { |
| 539 | if (DeclKind == Decl::Enum) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 540 | return !cast<EnumDecl>(this)->isScoped(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 541 | else if (DeclKind == Decl::LinkageSpec) |
| 542 | return true; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 547 | bool DeclContext::isExternCContext() const { |
| 548 | const DeclContext *DC = this; |
| 549 | while (DC->DeclKind != Decl::TranslationUnit) { |
| 550 | if (DC->DeclKind == Decl::LinkageSpec) |
| 551 | return cast<LinkageSpecDecl>(DC)->getLanguage() |
| 552 | == LinkageSpecDecl::lang_c; |
| 553 | DC = DC->getParent(); |
| 554 | } |
| 555 | return false; |
| 556 | } |
| 557 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 558 | bool DeclContext::Encloses(const DeclContext *DC) const { |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 559 | if (getPrimaryContext() != this) |
| 560 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 562 | for (; DC; DC = DC->getParent()) |
| 563 | if (DC->getPrimaryContext() == this) |
| 564 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | return false; |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 568 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 569 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 570 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 571 | case Decl::LinkageSpec: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 573 | // There is only one DeclContext for these entities. |
| 574 | return this; |
| 575 | |
| 576 | case Decl::Namespace: |
| 577 | // The original namespace is our primary context. |
| 578 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 579 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 580 | case Decl::ObjCMethod: |
| 581 | return this; |
| 582 | |
| 583 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 584 | case Decl::ObjCProtocol: |
| 585 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 586 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 587 | return this; |
| 588 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 589 | case Decl::ObjCImplementation: |
| 590 | case Decl::ObjCCategoryImpl: |
| 591 | return this; |
| 592 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 593 | default: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 594 | if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 595 | // If this is a tag type that has a definition or is currently |
| 596 | // being defined, that definition is our primary context. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 597 | TagDecl *Tag = cast<TagDecl>(this); |
| 598 | assert(isa<TagType>(Tag->TypeForDecl) || |
| 599 | isa<InjectedClassNameType>(Tag->TypeForDecl)); |
| 600 | |
| 601 | if (TagDecl *Def = Tag->getDefinition()) |
| 602 | return Def; |
| 603 | |
| 604 | if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) { |
| 605 | const TagType *TagTy = cast<TagType>(Tag->TypeForDecl); |
| 606 | if (TagTy->isBeingDefined()) |
| 607 | // FIXME: is it necessarily being defined in the decl |
| 608 | // that owns the type? |
| 609 | return TagTy->getDecl(); |
| 610 | } |
| 611 | |
| 612 | return Tag; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 615 | assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction && |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 616 | "Unknown DeclContext kind"); |
| 617 | return this; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | DeclContext *DeclContext::getNextContext() { |
| 622 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 623 | case Decl::Namespace: |
| 624 | // Return the next namespace |
| 625 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 626 | |
| 627 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | } |
| 631 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 632 | std::pair<Decl *, Decl *> |
| 633 | DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) { |
| 634 | // Build up a chain of declarations via the Decl::NextDeclInContext field. |
| 635 | Decl *FirstNewDecl = 0; |
| 636 | Decl *PrevDecl = 0; |
| 637 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 638 | Decl *D = Decls[I]; |
| 639 | if (PrevDecl) |
| 640 | PrevDecl->NextDeclInContext = D; |
| 641 | else |
| 642 | FirstNewDecl = D; |
| 643 | |
| 644 | PrevDecl = D; |
| 645 | } |
| 646 | |
| 647 | return std::make_pair(FirstNewDecl, PrevDecl); |
| 648 | } |
| 649 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 650 | /// \brief Load the declarations within this lexical storage from an |
| 651 | /// external source. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 653 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 654 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 655 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 656 | |
Argyrios Kyrtzidis | 0dbbc04 | 2010-07-30 10:03:23 +0000 | [diff] [blame] | 657 | // Notify that we have a DeclContext that is initializing. |
| 658 | ExternalASTSource::Deserializing ADeclContext(Source); |
| 659 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 660 | llvm::SmallVector<Decl*, 64> Decls; |
| 661 | if (Source->FindExternalLexicalDecls(this, Decls)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 662 | return; |
| 663 | |
| 664 | // There is no longer any lexical storage in this context |
| 665 | ExternalLexicalStorage = false; |
| 666 | |
| 667 | if (Decls.empty()) |
| 668 | return; |
| 669 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 670 | // We may have already loaded just the fields of this record, in which case |
| 671 | // don't add the decls, just replace the FirstDecl/LastDecl chain. |
| 672 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(this)) |
| 673 | if (RD->LoadedFieldsFromExternalStorage) { |
| 674 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 675 | return; |
| 676 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 677 | |
| 678 | // Splice the newly-read declarations into the beginning of the list |
| 679 | // of declarations. |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 680 | Decl *ExternalFirst, *ExternalLast; |
| 681 | llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls); |
| 682 | ExternalLast->NextDeclInContext = FirstDecl; |
| 683 | FirstDecl = ExternalFirst; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 684 | if (!LastDecl) |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 685 | LastDecl = ExternalLast; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 686 | } |
| 687 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 688 | DeclContext::lookup_result |
| 689 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 690 | DeclarationName Name) { |
| 691 | ASTContext &Context = DC->getParentASTContext(); |
| 692 | StoredDeclsMap *Map; |
| 693 | if (!(Map = DC->LookupPtr)) |
| 694 | Map = DC->CreateStoredDeclsMap(Context); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 695 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 696 | StoredDeclsList &List = (*Map)[Name]; |
| 697 | assert(List.isNull()); |
| 698 | (void) List; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 699 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 700 | return DeclContext::lookup_result(); |
| 701 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 702 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 703 | DeclContext::lookup_result |
| 704 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 705 | DeclarationName Name, |
| 706 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 707 | ASTContext &Context = DC->getParentASTContext();; |
| 708 | |
| 709 | StoredDeclsMap *Map; |
| 710 | if (!(Map = DC->LookupPtr)) |
| 711 | Map = DC->CreateStoredDeclsMap(Context); |
| 712 | |
| 713 | StoredDeclsList &List = (*Map)[Name]; |
| 714 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 715 | if (List.isNull()) |
| 716 | List.setOnlyValue(Decls[I]); |
| 717 | else |
| 718 | List.AddSubsequentDecl(Decls[I]); |
| 719 | } |
| 720 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 721 | return List.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 724 | void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC, |
| 725 | DeclarationName Name, |
| 726 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 727 | assert(DC->LookupPtr); |
| 728 | StoredDeclsMap &Map = *DC->LookupPtr; |
| 729 | |
| 730 | // If there's an entry in the table the visible decls for this name have |
| 731 | // already been deserialized. |
| 732 | if (Map.find(Name) == Map.end()) { |
| 733 | StoredDeclsList &List = Map[Name]; |
| 734 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 735 | if (List.isNull()) |
| 736 | List.setOnlyValue(Decls[I]); |
| 737 | else |
| 738 | List.AddSubsequentDecl(Decls[I]); |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 743 | DeclContext::decl_iterator DeclContext::noload_decls_begin() const { |
| 744 | return decl_iterator(FirstDecl); |
| 745 | } |
| 746 | |
| 747 | DeclContext::decl_iterator DeclContext::noload_decls_end() const { |
| 748 | return decl_iterator(); |
| 749 | } |
| 750 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 751 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 752 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 753 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 754 | |
| 755 | // FIXME: Check whether we need to load some declarations from |
| 756 | // external storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 760 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 761 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 762 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 763 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | return decl_iterator(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 767 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 768 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 769 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 770 | |
| 771 | return !FirstDecl; |
| 772 | } |
| 773 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 774 | void DeclContext::removeDecl(Decl *D) { |
| 775 | assert(D->getLexicalDeclContext() == this && |
| 776 | "decl being removed from non-lexical context"); |
| 777 | assert((D->NextDeclInContext || D == LastDecl) && |
| 778 | "decl is not in decls list"); |
| 779 | |
| 780 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 781 | if (D == FirstDecl) { |
| 782 | if (D == LastDecl) |
| 783 | FirstDecl = LastDecl = 0; |
| 784 | else |
| 785 | FirstDecl = D->NextDeclInContext; |
| 786 | } else { |
| 787 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 788 | assert(I && "decl not found in linked list"); |
| 789 | if (I->NextDeclInContext == D) { |
| 790 | I->NextDeclInContext = D->NextDeclInContext; |
| 791 | if (D == LastDecl) LastDecl = I; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Mark that D is no longer in the decl chain. |
| 798 | D->NextDeclInContext = 0; |
| 799 | |
| 800 | // Remove D from the lookup table if necessary. |
| 801 | if (isa<NamedDecl>(D)) { |
| 802 | NamedDecl *ND = cast<NamedDecl>(D); |
| 803 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 804 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 805 | if (!Map) return; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 806 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 807 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 808 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 809 | Pos->second.remove(ND); |
| 810 | } |
| 811 | } |
| 812 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 813 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 814 | assert(D->getLexicalDeclContext() == this && |
| 815 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 817 | "Decl already inserted into a DeclContext"); |
| 818 | |
| 819 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 820 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 821 | LastDecl = D; |
| 822 | } else { |
| 823 | FirstDecl = LastDecl = D; |
| 824 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 825 | |
| 826 | // Notify a C++ record declaration that we've added a member, so it can |
| 827 | // update it's class-specific state. |
| 828 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 829 | Record->addedMember(D); |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void DeclContext::addDecl(Decl *D) { |
| 833 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 834 | |
| 835 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 836 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 839 | /// buildLookup - Build the lookup data structure with all of the |
| 840 | /// declarations in DCtx (and any other contexts linked to it or |
| 841 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 842 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 843 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | for (decl_iterator D = DCtx->decls_begin(), |
| 845 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 846 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 847 | // Insert this declaration into the lookup structure, but only |
| 848 | // if it's semantically in its decl context. During non-lazy |
| 849 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 850 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 851 | if (D->getDeclContext() == DCtx) |
| 852 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 854 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 855 | // data structure. |
| 856 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 857 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 858 | I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 859 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 860 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 861 | // If this declaration is itself a transparent declaration context or |
| 862 | // inline namespace, add its members (recursively). |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 863 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 864 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 865 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 871 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 872 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 873 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 874 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 875 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 876 | if (hasExternalVisibleStorage()) { |
| 877 | // Check to see if we've already cached the lookup results. |
| 878 | if (LookupPtr) { |
| 879 | StoredDeclsMap::iterator I = LookupPtr->find(Name); |
| 880 | if (I != LookupPtr->end()) |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 881 | return I->second.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 885 | return Source->FindExternalVisibleDeclsByName(this, Name); |
| 886 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 887 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 888 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 889 | /// all of the linked DeclContexts (in declaration order!) and |
| 890 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 891 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 892 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 894 | if (!LookupPtr) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 895 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 897 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 898 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 899 | if (Pos == LookupPtr->end()) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 900 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 901 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 905 | DeclContext::lookup(DeclarationName Name) const { |
| 906 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 909 | DeclContext *DeclContext::getRedeclContext() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 910 | DeclContext *Ctx = this; |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 911 | // Skip through transparent contexts. |
| 912 | while (Ctx->isTransparentContext()) |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 913 | Ctx = Ctx->getParent(); |
| 914 | return Ctx; |
| 915 | } |
| 916 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 917 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 918 | DeclContext *Ctx = this; |
| 919 | // Skip through non-namespace, non-translation-unit contexts. |
Sebastian Redl | 51a8a37 | 2010-08-31 00:36:23 +0000 | [diff] [blame] | 920 | while (!Ctx->isFileContext()) |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 921 | Ctx = Ctx->getParent(); |
| 922 | return Ctx->getPrimaryContext(); |
| 923 | } |
| 924 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 925 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 926 | // For non-file contexts, this is equivalent to Equals. |
| 927 | if (!isFileContext()) |
| 928 | return O->Equals(this); |
| 929 | |
| 930 | do { |
| 931 | if (O->Equals(this)) |
| 932 | return true; |
| 933 | |
| 934 | const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O); |
| 935 | if (!NS || !NS->isInline()) |
| 936 | break; |
| 937 | O = NS->getParent(); |
| 938 | } while (O); |
| 939 | |
| 940 | return false; |
| 941 | } |
| 942 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 943 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 944 | // FIXME: This feels like a hack. Should DeclarationName support |
| 945 | // template-ids, or is there a better way to keep specializations |
| 946 | // from being visible? |
| 947 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 948 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 949 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 950 | if (FD->isFunctionTemplateSpecialization()) |
| 951 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 952 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 953 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 954 | if (PrimaryContext != this) { |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 955 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | |
| 959 | // If we already have a lookup data structure, perform the insertion |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 960 | // into it. If we haven't deserialized externally stored decls, deserialize |
| 961 | // them so we can add the decl. Otherwise, be lazy and don't build that |
| 962 | // structure until someone asks for it. |
| 963 | if (LookupPtr || !Recoverable || hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 964 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 965 | |
Sebastian Redl | 410c4f2 | 2010-08-31 20:53:31 +0000 | [diff] [blame] | 966 | // If we are a transparent context or inline namespace, insert into our |
| 967 | // parent context, too. This operation is recursive. |
| 968 | if (isTransparentContext() || isInlineNamespace()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 969 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 970 | |
| 971 | Decl *DCAsDecl = cast<Decl>(this); |
| 972 | // Notify that a decl was made visible unless it's a Tag being defined. |
| 973 | if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined())) |
| 974 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 975 | L->AddedVisibleDecl(this, D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 978 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 979 | // Skip unnamed declarations. |
| 980 | if (!D->getDeclName()) |
| 981 | return; |
| 982 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 983 | // FIXME: This feels like a hack. Should DeclarationName support |
| 984 | // template-ids, or is there a better way to keep specializations |
| 985 | // from being visible? |
| 986 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 987 | return; |
| 988 | |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 989 | ASTContext *C = 0; |
| 990 | if (!LookupPtr) { |
| 991 | C = &getParentASTContext(); |
| 992 | CreateStoredDeclsMap(*C); |
| 993 | } |
| 994 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 995 | // If there is an external AST source, load any declarations it knows about |
| 996 | // with this declaration's name. |
| 997 | // If the lookup table contains an entry about this name it means that we |
| 998 | // have already checked the external source. |
| 999 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 1000 | if (hasExternalVisibleStorage() && |
| 1001 | LookupPtr->find(D->getDeclName()) == LookupPtr->end()) |
| 1002 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
| 1003 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1004 | // Insert this declaration into the map. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1005 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1006 | if (DeclNameEntries.isNull()) { |
| 1007 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1008 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1009 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 1011 | // If it is possible that this is a redeclaration, check to see if there is |
| 1012 | // already a decl for which declarationReplaces returns true. If there is |
| 1013 | // one, just replace it and return. |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 1014 | if (DeclNameEntries.HandleRedeclaration(D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1015 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 1017 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 1018 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1019 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1020 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 1021 | void DeclContext::MaterializeVisibleDeclsFromExternalStorage() { |
| 1022 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1023 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 1024 | |
| 1025 | if (!LookupPtr) |
| 1026 | CreateStoredDeclsMap(getParentASTContext()); |
| 1027 | Source->MaterializeVisibleDecls(this); |
| 1028 | } |
| 1029 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1030 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 1031 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1033 | DeclContext::getUsingDirectives() const { |
| 1034 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1035 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 1036 | reinterpret_cast<udir_iterator>(Result.second)); |
| 1037 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1038 | |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1039 | //===----------------------------------------------------------------------===// |
| 1040 | // Creation and Destruction of StoredDeclsMaps. // |
| 1041 | //===----------------------------------------------------------------------===// |
| 1042 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1043 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 1044 | assert(!LookupPtr && "context already has a decls map"); |
| 1045 | assert(getPrimaryContext() == this && |
| 1046 | "creating decls map on non-primary context"); |
| 1047 | |
| 1048 | StoredDeclsMap *M; |
| 1049 | bool Dependent = isDependentContext(); |
| 1050 | if (Dependent) |
| 1051 | M = new DependentStoredDeclsMap(); |
| 1052 | else |
| 1053 | M = new StoredDeclsMap(); |
| 1054 | M->Previous = C.LastSDM; |
| 1055 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 1056 | LookupPtr = M; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1057 | return M; |
| 1058 | } |
| 1059 | |
| 1060 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1061 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 1062 | // pointer because the subclass doesn't add anything that needs to |
| 1063 | // be deleted. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1064 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 1065 | } |
| 1066 | |
| 1067 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 1068 | while (Map) { |
| 1069 | // Advance the iteration before we invalidate memory. |
| 1070 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 1071 | |
| 1072 | if (Dependent) |
| 1073 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 1074 | else |
| 1075 | delete Map; |
| 1076 | |
| 1077 | Map = Next.getPointer(); |
| 1078 | Dependent = Next.getInt(); |
| 1079 | } |
| 1080 | } |
| 1081 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1082 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1083 | DeclContext *Parent, |
| 1084 | const PartialDiagnostic &PDiag) { |
| 1085 | assert(Parent->isDependentContext() |
| 1086 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1087 | Parent = Parent->getPrimaryContext(); |
| 1088 | if (!Parent->LookupPtr) |
| 1089 | Parent->CreateStoredDeclsMap(C); |
| 1090 | |
| 1091 | DependentStoredDeclsMap *Map |
| 1092 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1093 | |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1094 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1095 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1096 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1097 | if (PDiag.hasStorage()) |
| 1098 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1099 | |
| 1100 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1101 | |
| 1102 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1103 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1104 | Map->FirstDiagnostic = DD; |
| 1105 | |
| 1106 | return DD; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1107 | } |