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