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