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