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