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