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