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 { |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 413 | // If the decl is the toplevel translation unit or if we're not in a |
| 414 | // record decl context, we don't need to check anything. |
| 415 | if (isa<TranslationUnitDecl>(this) || |
| 416 | !isa<CXXRecordDecl>(getDeclContext())) |
| 417 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
| 419 | assert(Access != AS_none && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 420 | "Access specifier is AS_none inside a record decl"); |
| 421 | } |
| 422 | |
| 423 | #endif |
| 424 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 425 | //===----------------------------------------------------------------------===// |
| 426 | // DeclContext Implementation |
| 427 | //===----------------------------------------------------------------------===// |
| 428 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 429 | bool DeclContext::classof(const Decl *D) { |
| 430 | switch (D->getKind()) { |
| 431 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 432 | #define DECL_CONTEXT_BASE(Name) |
| 433 | #include "clang/AST/DeclNodes.def" |
| 434 | return true; |
| 435 | default: |
| 436 | #define DECL_CONTEXT_BASE(Name) \ |
| 437 | if (D->getKind() >= Decl::Name##First && \ |
| 438 | D->getKind() <= Decl::Name##Last) \ |
| 439 | return true; |
| 440 | #include "clang/AST/DeclNodes.def" |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 445 | DeclContext::~DeclContext() { |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 446 | delete static_cast<StoredDeclsMap*>(LookupPtr); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void DeclContext::DestroyDecls(ASTContext &C) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 450 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 451 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 454 | /// \brief Find the parent context of this context that will be |
| 455 | /// used for unqualified name lookup. |
| 456 | /// |
| 457 | /// Generally, the parent lookup context is the semantic context. However, for |
| 458 | /// a friend function the parent lookup context is the lexical context, which |
| 459 | /// is the class in which the friend is declared. |
| 460 | DeclContext *DeclContext::getLookupParent() { |
| 461 | // FIXME: Find a better way to identify friends |
| 462 | if (isa<FunctionDecl>(this)) |
| 463 | if (getParent()->getLookupContext()->isFileContext() && |
| 464 | getLexicalParent()->getLookupContext()->isRecord()) |
| 465 | return getLexicalParent(); |
| 466 | |
| 467 | return getParent(); |
| 468 | } |
| 469 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 470 | bool DeclContext::isDependentContext() const { |
| 471 | if (isFileContext()) |
| 472 | return false; |
| 473 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 474 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 475 | return true; |
| 476 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 477 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 478 | if (Record->getDescribedClassTemplate()) |
| 479 | return true; |
| 480 | |
| 481 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) |
| 482 | if (Function->getDescribedFunctionTemplate()) |
| 483 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 485 | return getParent() && getParent()->isDependentContext(); |
| 486 | } |
| 487 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 488 | bool DeclContext::isTransparentContext() const { |
| 489 | if (DeclKind == Decl::Enum) |
| 490 | return true; // FIXME: Check for C++0x scoped enums |
| 491 | else if (DeclKind == Decl::LinkageSpec) |
| 492 | return true; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 493 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 494 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 495 | else if (DeclKind == Decl::Namespace) |
| 496 | return false; // FIXME: Check for C++0x inline namespaces |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 501 | bool DeclContext::Encloses(DeclContext *DC) { |
| 502 | if (getPrimaryContext() != this) |
| 503 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 505 | for (; DC; DC = DC->getParent()) |
| 506 | if (DC->getPrimaryContext() == this) |
| 507 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | return false; |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 511 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 512 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 513 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 514 | case Decl::LinkageSpec: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 516 | // There is only one DeclContext for these entities. |
| 517 | return this; |
| 518 | |
| 519 | case Decl::Namespace: |
| 520 | // The original namespace is our primary context. |
| 521 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 522 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 523 | case Decl::ObjCMethod: |
| 524 | return this; |
| 525 | |
| 526 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 527 | case Decl::ObjCProtocol: |
| 528 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 529 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 530 | return this; |
| 531 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 532 | case Decl::ObjCImplementation: |
| 533 | case Decl::ObjCCategoryImpl: |
| 534 | return this; |
| 535 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 536 | default: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 537 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 538 | // If this is a tag type that has a definition or is currently |
| 539 | // being defined, that definition is our primary context. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 540 | if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | if (TagT->isBeingDefined() || |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 542 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 543 | return TagT->getDecl(); |
| 544 | return this; |
| 545 | } |
| 546 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 547 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 548 | "Unknown DeclContext kind"); |
| 549 | return this; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | DeclContext *DeclContext::getNextContext() { |
| 554 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 555 | case Decl::Namespace: |
| 556 | // Return the next namespace |
| 557 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 558 | |
| 559 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | } |
| 563 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 564 | /// \brief Load the declarations within this lexical storage from an |
| 565 | /// external source. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 567 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 568 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 569 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 570 | |
Eli Friedman | b0156ea | 2009-04-27 23:43:36 +0000 | [diff] [blame] | 571 | llvm::SmallVector<uint32_t, 64> Decls; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 573 | Decls)) |
| 574 | return; |
| 575 | |
| 576 | // There is no longer any lexical storage in this context |
| 577 | ExternalLexicalStorage = false; |
| 578 | |
| 579 | if (Decls.empty()) |
| 580 | return; |
| 581 | |
| 582 | // Resolve all of the declaration IDs into declarations, building up |
| 583 | // a chain of declarations via the Decl::NextDeclInContext field. |
| 584 | Decl *FirstNewDecl = 0; |
| 585 | Decl *PrevDecl = 0; |
| 586 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 587 | Decl *D = Source->GetDecl(Decls[I]); |
| 588 | if (PrevDecl) |
| 589 | PrevDecl->NextDeclInContext = D; |
| 590 | else |
| 591 | FirstNewDecl = D; |
| 592 | |
| 593 | PrevDecl = D; |
| 594 | } |
| 595 | |
| 596 | // Splice the newly-read declarations into the beginning of the list |
| 597 | // of declarations. |
| 598 | PrevDecl->NextDeclInContext = FirstDecl; |
| 599 | FirstDecl = FirstNewDecl; |
| 600 | if (!LastDecl) |
| 601 | LastDecl = PrevDecl; |
| 602 | } |
| 603 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 605 | DeclContext::LoadVisibleDeclsFromExternalStorage() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 606 | DeclContext *This = const_cast<DeclContext *>(this); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 607 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 608 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 609 | |
| 610 | llvm::SmallVector<VisibleDeclaration, 64> Decls; |
| 611 | if (Source->ReadDeclsVisibleInContext(This, Decls)) |
| 612 | return; |
| 613 | |
| 614 | // There is no longer any visible storage in this context |
| 615 | ExternalVisibleStorage = false; |
| 616 | |
| 617 | // Load the declaration IDs for all of the names visible in this |
| 618 | // context. |
| 619 | assert(!LookupPtr && "Have a lookup map before de-serialization?"); |
| 620 | StoredDeclsMap *Map = new StoredDeclsMap; |
| 621 | LookupPtr = Map; |
| 622 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 623 | (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations); |
| 624 | } |
| 625 | } |
| 626 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 627 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 628 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 629 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 630 | |
| 631 | // FIXME: Check whether we need to load some declarations from |
| 632 | // external storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 636 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 637 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 638 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 639 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | return decl_iterator(); |
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 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 644 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 645 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 646 | |
| 647 | return !FirstDecl; |
| 648 | } |
| 649 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 650 | void DeclContext::removeDecl(Decl *D) { |
| 651 | assert(D->getLexicalDeclContext() == this && |
| 652 | "decl being removed from non-lexical context"); |
| 653 | assert((D->NextDeclInContext || D == LastDecl) && |
| 654 | "decl is not in decls list"); |
| 655 | |
| 656 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 657 | if (D == FirstDecl) { |
| 658 | if (D == LastDecl) |
| 659 | FirstDecl = LastDecl = 0; |
| 660 | else |
| 661 | FirstDecl = D->NextDeclInContext; |
| 662 | } else { |
| 663 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 664 | assert(I && "decl not found in linked list"); |
| 665 | if (I->NextDeclInContext == D) { |
| 666 | I->NextDeclInContext = D->NextDeclInContext; |
| 667 | if (D == LastDecl) LastDecl = I; |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // Mark that D is no longer in the decl chain. |
| 674 | D->NextDeclInContext = 0; |
| 675 | |
| 676 | // Remove D from the lookup table if necessary. |
| 677 | if (isa<NamedDecl>(D)) { |
| 678 | NamedDecl *ND = cast<NamedDecl>(D); |
| 679 | |
| 680 | void *OpaqueMap = getPrimaryContext()->LookupPtr; |
| 681 | if (!OpaqueMap) return; |
| 682 | |
| 683 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap); |
| 684 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 685 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 686 | Pos->second.remove(ND); |
| 687 | } |
| 688 | } |
| 689 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 690 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 691 | assert(D->getLexicalDeclContext() == this && |
| 692 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 694 | "Decl already inserted into a DeclContext"); |
| 695 | |
| 696 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 697 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 698 | LastDecl = D; |
| 699 | } else { |
| 700 | FirstDecl = LastDecl = D; |
| 701 | } |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void DeclContext::addDecl(Decl *D) { |
| 705 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 706 | |
| 707 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 708 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 711 | /// buildLookup - Build the lookup data structure with all of the |
| 712 | /// declarations in DCtx (and any other contexts linked to it or |
| 713 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 714 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 715 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | for (decl_iterator D = DCtx->decls_begin(), |
| 717 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 718 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 719 | // Insert this declaration into the lookup structure, but only |
| 720 | // if it's semantically in its decl context. During non-lazy |
| 721 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 722 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 723 | if (D->getDeclContext() == DCtx) |
| 724 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 725 | |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 726 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 727 | // data structure. |
| 728 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 729 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 730 | I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 731 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 733 | // If this declaration is itself a transparent declaration context, |
| 734 | // add its members (recursively). |
| 735 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 736 | if (InnerCtx->isTransparentContext()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 737 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 743 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 744 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 745 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 746 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 748 | if (hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 749 | LoadVisibleDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 751 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 752 | /// all of the linked DeclContexts (in declaration order!) and |
| 753 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 754 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 755 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 757 | if (!LookupPtr) |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 758 | return lookup_result(0, 0); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 759 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 761 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr); |
| 762 | StoredDeclsMap::iterator Pos = Map->find(Name); |
| 763 | if (Pos == Map->end()) |
| 764 | return lookup_result(0, 0); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 765 | return Pos->second.getLookupResult(getParentASTContext()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 769 | DeclContext::lookup(DeclarationName Name) const { |
| 770 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 773 | DeclContext *DeclContext::getLookupContext() { |
| 774 | DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 775 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 776 | while (Ctx->isTransparentContext()) |
| 777 | Ctx = Ctx->getParent(); |
| 778 | return Ctx; |
| 779 | } |
| 780 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 781 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 782 | DeclContext *Ctx = this; |
| 783 | // Skip through non-namespace, non-translation-unit contexts. |
| 784 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 785 | Ctx = Ctx->getParent(); |
| 786 | return Ctx->getPrimaryContext(); |
| 787 | } |
| 788 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 789 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 790 | // FIXME: This feels like a hack. Should DeclarationName support |
| 791 | // template-ids, or is there a better way to keep specializations |
| 792 | // from being visible? |
| 793 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 794 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 795 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 796 | if (FD->isFunctionTemplateSpecialization()) |
| 797 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 798 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 799 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 800 | if (PrimaryContext != this) { |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 801 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 802 | return; |
| 803 | } |
| 804 | |
| 805 | // If we already have a lookup data structure, perform the insertion |
| 806 | // into it. Otherwise, be lazy and don't build that structure until |
| 807 | // someone asks for it. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 808 | if (LookupPtr || !Recoverable) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 809 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 811 | // If we are a transparent context, insert into our parent context, |
| 812 | // too. This operation is recursive. |
| 813 | if (isTransparentContext()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 814 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 817 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 818 | // Skip unnamed declarations. |
| 819 | if (!D->getDeclName()) |
| 820 | return; |
| 821 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 822 | // FIXME: This feels like a hack. Should DeclarationName support |
| 823 | // template-ids, or is there a better way to keep specializations |
| 824 | // from being visible? |
| 825 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 826 | return; |
| 827 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 828 | if (!LookupPtr) |
| 829 | LookupPtr = new StoredDeclsMap; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 830 | |
| 831 | // Insert this declaration into the map. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 832 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr); |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 833 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 834 | if (DeclNameEntries.isNull()) { |
| 835 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 836 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 838 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 839 | // If it is possible that this is a redeclaration, check to see if there is |
| 840 | // already a decl for which declarationReplaces returns true. If there is |
| 841 | // one, just replace it and return. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 842 | if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 843 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 845 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 846 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 847 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 848 | |
| 849 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 850 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 852 | DeclContext::getUsingDirectives() const { |
| 853 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 854 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 855 | reinterpret_cast<udir_iterator>(Result.second)); |
| 856 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 857 | |
| 858 | void StoredDeclsList::materializeDecls(ASTContext &Context) { |
| 859 | if (isNull()) |
| 860 | return; |
| 861 | |
| 862 | switch ((DataKind)(Data & 0x03)) { |
| 863 | case DK_Decl: |
| 864 | case DK_Decl_Vector: |
| 865 | break; |
| 866 | |
| 867 | case DK_DeclID: { |
| 868 | // Resolve this declaration ID to an actual declaration by |
| 869 | // querying the external AST source. |
| 870 | unsigned DeclID = Data >> 2; |
| 871 | |
| 872 | ExternalASTSource *Source = Context.getExternalSource(); |
| 873 | assert(Source && "No external AST source available!"); |
| 874 | |
| 875 | Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID)); |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | case DK_ID_Vector: { |
| 880 | // We have a vector of declaration IDs. Resolve all of them to |
| 881 | // actual declarations. |
| 882 | VectorTy &Vector = *getAsVector(); |
| 883 | ExternalASTSource *Source = Context.getExternalSource(); |
| 884 | assert(Source && "No external AST source available!"); |
| 885 | |
| 886 | for (unsigned I = 0, N = Vector.size(); I != N; ++I) |
| 887 | Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I])); |
| 888 | |
| 889 | Data = (Data & ~0x03) | DK_Decl_Vector; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | } |