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