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