Eli Friedman | a8a09ac | 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 | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | d2b6edc | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argiris Kirtzidis | 6df1fc0 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExternalASTSource.h" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 22 | #include "clang/AST/Type.h" |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 23 | #include "clang/AST/Stmt.h" |
| 24 | #include "clang/AST/StmtCXX.h" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Chris Lattner | 7d6220c | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 29 | #include <vector> |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Statistics |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Douglas Gregor | 469fc9a | 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 | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 38 | |
| 39 | static bool StatSwitch = false; |
| 40 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 41 | const char *Decl::getDeclKindName() const { |
| 42 | switch (DeclKind) { |
Douglas Gregor | 469fc9a | 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 | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 49 | const char *DeclContext::getDeclKindName() const { |
| 50 | switch (DeclKind) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 51 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
Argiris Kirtzidis | 2d9b761 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 52 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 53 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 57 | bool Decl::CollectingStats(bool Enable) { |
| 58 | if (Enable) |
| 59 | StatSwitch = true; |
| 60 | return StatSwitch; |
| 61 | } |
| 62 | |
| 63 | void Decl::PrintStats() { |
| 64 | fprintf(stderr, "*** Decl Stats:\n"); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 66 | int totalDecls = 0; |
| 67 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 68 | #include "clang/AST/DeclNodes.def" |
| 69 | fprintf(stderr, " %d decls total.\n", totalDecls); |
| 70 | |
| 71 | int totalBytes = 0; |
| 72 | #define DECL(Derived, Base) \ |
| 73 | if (n##Derived##s > 0) { \ |
| 74 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 75 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 76 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 77 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 78 | } |
| 79 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 81 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Decl::addDeclKind(Kind k) { |
| 85 | switch (k) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 86 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 87 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 88 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Anders Carlsson | c2018a7 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 92 | bool Decl::isTemplateParameterPack() const { |
| 93 | if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) |
| 94 | return TTP->isParameterPack(); |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 99 | bool Decl::isFunctionOrFunctionTemplate() const { |
Anders Carlsson | 5847da5 | 2009-06-26 05:26:50 +0000 | [diff] [blame] | 100 | if (const UsingDecl *UD = dyn_cast<UsingDecl>(this)) |
| 101 | return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); |
| 102 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 103 | return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); |
| 104 | } |
| 105 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 107 | // PrettyStackTraceDecl Implementation |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 111 | SourceLocation TheLoc = Loc; |
| 112 | if (TheLoc.isInvalid() && TheDecl) |
| 113 | TheLoc = TheDecl->getLocation(); |
| 114 | |
| 115 | if (TheLoc.isValid()) { |
| 116 | TheLoc.print(OS, SM); |
| 117 | OS << ": "; |
| 118 | } |
| 119 | |
| 120 | OS << Message; |
| 121 | |
| 122 | if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
| 123 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 124 | OS << '\n'; |
| 125 | } |
| 126 | |
| 127 | //===----------------------------------------------------------------------===// |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 128 | // Decl Implementation |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 131 | // Out-of-line virtual method providing a home for Decl. |
| 132 | Decl::~Decl() { |
| 133 | if (isOutOfSemaDC()) |
| 134 | delete getMultipleDC(); |
| 135 | |
| 136 | assert(!HasAttrs && "attributes should have been freed by Destroy"); |
| 137 | } |
| 138 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 139 | void Decl::setDeclContext(DeclContext *DC) { |
| 140 | if (isOutOfSemaDC()) |
| 141 | delete getMultipleDC(); |
| 142 | |
Chris Lattner | 5ce4f6d | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 143 | DeclCtx = DC; |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 147 | if (DC == getLexicalDeclContext()) |
| 148 | return; |
| 149 | |
| 150 | if (isInSemaDC()) { |
| 151 | MultipleDC *MDC = new MultipleDC(); |
| 152 | MDC->SemanticDC = getDeclContext(); |
| 153 | MDC->LexicalDC = DC; |
Chris Lattner | 5ce4f6d | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 154 | DeclCtx = MDC; |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 155 | } else { |
| 156 | getMultipleDC()->LexicalDC = DC; |
| 157 | } |
| 158 | } |
| 159 | |
Argiris Kirtzidis | 2a6dca1 | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 160 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argiris Kirtzidis | f7993cb | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 161 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 162 | return TUD; |
| 163 | |
Argiris Kirtzidis | 2a6dca1 | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 164 | DeclContext *DC = getDeclContext(); |
| 165 | assert(DC && "This decl is not contained in a translation unit!"); |
| 166 | |
| 167 | while (!DC->isTranslationUnit()) { |
| 168 | DC = DC->getParent(); |
| 169 | assert(DC && "This decl is not contained in a translation unit!"); |
| 170 | } |
| 171 | |
| 172 | return cast<TranslationUnitDecl>(DC); |
| 173 | } |
| 174 | |
| 175 | ASTContext &Decl::getASTContext() const { |
| 176 | return getTranslationUnitDecl()->getASTContext(); |
| 177 | } |
| 178 | |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 179 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 180 | switch (DeclKind) { |
| 181 | default: |
| 182 | if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) |
| 183 | return IDNS_Ordinary; |
| 184 | assert(0 && "Unknown decl kind!"); |
| 185 | case OverloadedFunction: |
| 186 | case Typedef: |
| 187 | case EnumConstant: |
| 188 | case Var: |
| 189 | case ImplicitParam: |
| 190 | case ParmVar: |
| 191 | case OriginalParmVar: |
| 192 | case NonTypeTemplateParm: |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 193 | case Using: |
Anders Carlsson | 4e75006 | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 194 | case UnresolvedUsing: |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 195 | case ObjCMethod: |
| 196 | case ObjCContainer: |
| 197 | case ObjCCategory: |
| 198 | case ObjCInterface: |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 199 | case ObjCProperty: |
| 200 | case ObjCCompatibleAlias: |
| 201 | return IDNS_Ordinary; |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 202 | |
John McCall | 7be34f4 | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 203 | case FriendClass: |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 204 | case FriendFunction: |
| 205 | return IDNS_Friend; |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 206 | |
| 207 | case ObjCProtocol: |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 208 | return IDNS_ObjCProtocol; |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 210 | case ObjCImplementation: |
| 211 | return IDNS_ObjCImplementation; |
| 212 | |
| 213 | case ObjCCategoryImpl: |
| 214 | return IDNS_ObjCCategoryImpl; |
| 215 | |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 216 | case Field: |
| 217 | case ObjCAtDefsField: |
| 218 | case ObjCIvar: |
| 219 | return IDNS_Member; |
| 220 | |
| 221 | case Record: |
| 222 | case CXXRecord: |
| 223 | case Enum: |
| 224 | case TemplateTypeParm: |
| 225 | return IDNS_Tag; |
| 226 | |
| 227 | case Namespace: |
| 228 | case Template: |
| 229 | case FunctionTemplate: |
| 230 | case ClassTemplate: |
| 231 | case TemplateTemplateParm: |
Anders Carlsson | 1bf91fb | 2009-03-28 23:02:53 +0000 | [diff] [blame] | 232 | case NamespaceAlias: |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 233 | return IDNS_Tag | IDNS_Ordinary; |
| 234 | |
| 235 | // Never have names. |
| 236 | case LinkageSpec: |
| 237 | case FileScopeAsm: |
| 238 | case StaticAssert: |
| 239 | case ObjCClass: |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 240 | case ObjCPropertyImpl: |
| 241 | case ObjCForwardProtocol: |
| 242 | case Block: |
| 243 | case TranslationUnit: |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 245 | // Aren't looked up? |
| 246 | case UsingDirective: |
| 247 | case ClassTemplateSpecialization: |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 248 | case ClassTemplatePartialSpecialization: |
Chris Lattner | 8752fe0 | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 253 | void Decl::addAttr(Attr *NewAttr) { |
| 254 | Attr *&ExistingAttr = getASTContext().getDeclAttrs(this); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 255 | |
| 256 | NewAttr->setNext(ExistingAttr); |
| 257 | ExistingAttr = NewAttr; |
| 258 | |
| 259 | HasAttrs = true; |
| 260 | } |
| 261 | |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 262 | void Decl::invalidateAttrs() { |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 263 | if (!HasAttrs) return; |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 264 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 265 | HasAttrs = false; |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 266 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 269 | const Attr *Decl::getAttrsImpl() const { |
Chris Lattner | 004a9bb | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 270 | assert(HasAttrs && "getAttrs() should verify this!"); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 271 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 274 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 275 | bool HasLHSAttr = this->HasAttrs; |
| 276 | bool HasRHSAttr = RHS->HasAttrs; |
| 277 | |
| 278 | // Usually, neither decl has attrs, nothing to do. |
| 279 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 280 | |
| 281 | // If 'this' has no attrs, swap the other way. |
| 282 | if (!HasLHSAttr) |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 283 | return RHS->swapAttrs(this); |
| 284 | |
| 285 | ASTContext &Context = getASTContext(); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 286 | |
| 287 | // Handle the case when both decls have attrs. |
| 288 | if (HasRHSAttr) { |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 289 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
| 293 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 294 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 295 | Context.eraseDeclAttrs(this); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 296 | this->HasAttrs = false; |
| 297 | RHS->HasAttrs = true; |
| 298 | } |
| 299 | |
| 300 | |
Chris Lattner | 0fc6d64 | 2009-03-04 06:05:19 +0000 | [diff] [blame] | 301 | void Decl::Destroy(ASTContext &C) { |
| 302 | // Free attributes for this decl. |
| 303 | if (HasAttrs) { |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 304 | C.getDeclAttrs(this)->Destroy(C); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 305 | invalidateAttrs(); |
Chris Lattner | 0fc6d64 | 2009-03-04 06:05:19 +0000 | [diff] [blame] | 306 | HasAttrs = false; |
| 307 | } |
| 308 | |
Douglas Gregor | 8314d74 | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 309 | #if 0 |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 310 | // FIXME: Once ownership is fully understood, we can enable this code |
| 311 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 312 | DC->decls_begin()->Destroy(C); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 313 | |
Chris Lattner | fb8416a | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 314 | // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0 |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 315 | // within the loop, only the Destroy method for the first Decl |
| 316 | // will deallocate all of the Decls in a chain. |
| 317 | |
Chris Lattner | fb8416a | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 318 | Decl* N = getNextDeclInContext(); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 319 | |
| 320 | while (N) { |
Chris Lattner | fb8416a | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 321 | Decl* Tmp = N->getNextDeclInContext(); |
| 322 | N->NextDeclInContext = 0; |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 323 | N->Destroy(C); |
| 324 | N = Tmp; |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 325 | } |
Douglas Gregor | 8314d74 | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 326 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 327 | this->~Decl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 328 | C.Deallocate((void *)this); |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 329 | #endif |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 332 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 333 | Decl::Kind DK = D->getDeclKind(); |
| 334 | switch(DK) { |
| 335 | #define DECL_CONTEXT(Name) \ |
| 336 | case Decl::Name: \ |
| 337 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 338 | #define DECL_CONTEXT_BASE(Name) |
| 339 | #include "clang/AST/DeclNodes.def" |
| 340 | default: |
| 341 | #define DECL_CONTEXT_BASE(Name) \ |
| 342 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 343 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 344 | #include "clang/AST/DeclNodes.def" |
| 345 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 346 | return 0; |
| 347 | } |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 351 | Decl::Kind DK = D->getKind(); |
| 352 | switch(DK) { |
| 353 | #define DECL_CONTEXT(Name) \ |
| 354 | case Decl::Name: \ |
| 355 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 356 | #define DECL_CONTEXT_BASE(Name) |
| 357 | #include "clang/AST/DeclNodes.def" |
| 358 | default: |
| 359 | #define DECL_CONTEXT_BASE(Name) \ |
| 360 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 361 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 362 | #include "clang/AST/DeclNodes.def" |
| 363 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 364 | return 0; |
| 365 | } |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 368 | CompoundStmt* Decl::getCompoundBody() const { |
| 369 | return dyn_cast_or_null<CompoundStmt>(getBody()); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 372 | SourceLocation Decl::getBodyRBrace() const { |
| 373 | Stmt *Body = getBody(); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 374 | if (!Body) |
| 375 | return SourceLocation(); |
| 376 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body)) |
| 377 | return CS->getRBracLoc(); |
| 378 | assert(isa<CXXTryStmt>(Body) && |
| 379 | "Body can only be CompoundStmt or CXXTryStmt"); |
| 380 | return cast<CXXTryStmt>(Body)->getSourceRange().getEnd(); |
| 381 | } |
| 382 | |
Anders Carlsson | 8ea6a32 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 383 | #ifndef NDEBUG |
| 384 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | a470cea | 2009-04-07 20:58:25 +0000 | [diff] [blame] | 385 | assert((Access != AS_none || isa<TranslationUnitDecl>(this) || |
| 386 | !isa<CXXRecordDecl>(getDeclContext())) && |
Anders Carlsson | 8ea6a32 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 387 | "Access specifier is AS_none inside a record decl"); |
| 388 | } |
| 389 | |
| 390 | #endif |
| 391 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 392 | //===----------------------------------------------------------------------===// |
| 393 | // DeclContext Implementation |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 396 | bool DeclContext::classof(const Decl *D) { |
| 397 | switch (D->getKind()) { |
| 398 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 399 | #define DECL_CONTEXT_BASE(Name) |
| 400 | #include "clang/AST/DeclNodes.def" |
| 401 | return true; |
| 402 | default: |
| 403 | #define DECL_CONTEXT_BASE(Name) \ |
| 404 | if (D->getKind() >= Decl::Name##First && \ |
| 405 | D->getKind() <= Decl::Name##Last) \ |
| 406 | return true; |
| 407 | #include "clang/AST/DeclNodes.def" |
| 408 | return false; |
| 409 | } |
| 410 | } |
| 411 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 412 | DeclContext::~DeclContext() { |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 413 | delete static_cast<StoredDeclsMap*>(LookupPtr); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void DeclContext::DestroyDecls(ASTContext &C) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 417 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 418 | (*D++)->Destroy(C); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Douglas Gregor | 1a6b88e | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 421 | bool DeclContext::isDependentContext() const { |
| 422 | if (isFileContext()) |
| 423 | return false; |
| 424 | |
Douglas Gregor | 58944ac | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 425 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 426 | return true; |
| 427 | |
Douglas Gregor | 1a6b88e | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 428 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 429 | if (Record->getDescribedClassTemplate()) |
| 430 | return true; |
| 431 | |
| 432 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) |
| 433 | if (Function->getDescribedFunctionTemplate()) |
| 434 | return true; |
| 435 | |
| 436 | return getParent() && getParent()->isDependentContext(); |
| 437 | } |
| 438 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 439 | bool DeclContext::isTransparentContext() const { |
| 440 | if (DeclKind == Decl::Enum) |
| 441 | return true; // FIXME: Check for C++0x scoped enums |
| 442 | else if (DeclKind == Decl::LinkageSpec) |
| 443 | return true; |
Douglas Gregor | 1db1293 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 444 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 445 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 446 | else if (DeclKind == Decl::Namespace) |
| 447 | return false; // FIXME: Check for C++0x inline namespaces |
| 448 | |
| 449 | return false; |
| 450 | } |
| 451 | |
Douglas Gregor | 430390d | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 452 | bool DeclContext::Encloses(DeclContext *DC) { |
| 453 | if (getPrimaryContext() != this) |
| 454 | return getPrimaryContext()->Encloses(DC); |
| 455 | |
| 456 | for (; DC; DC = DC->getParent()) |
| 457 | if (DC->getPrimaryContext() == this) |
| 458 | return true; |
| 459 | return false; |
| 460 | } |
| 461 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 462 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 463 | switch (DeclKind) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 464 | case Decl::TranslationUnit: |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 465 | case Decl::LinkageSpec: |
| 466 | case Decl::Block: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 467 | // There is only one DeclContext for these entities. |
| 468 | return this; |
| 469 | |
| 470 | case Decl::Namespace: |
| 471 | // The original namespace is our primary context. |
| 472 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 473 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 474 | case Decl::ObjCMethod: |
| 475 | return this; |
| 476 | |
| 477 | case Decl::ObjCInterface: |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 478 | case Decl::ObjCProtocol: |
| 479 | case Decl::ObjCCategory: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 480 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 481 | return this; |
| 482 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 483 | case Decl::ObjCImplementation: |
| 484 | case Decl::ObjCCategoryImpl: |
| 485 | return this; |
| 486 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 487 | default: |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 488 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 489 | // If this is a tag type that has a definition or is currently |
| 490 | // being defined, that definition is our primary context. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 491 | if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>()) |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 492 | if (TagT->isBeingDefined() || |
| 493 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 494 | return TagT->getDecl(); |
| 495 | return this; |
| 496 | } |
| 497 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 498 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 499 | "Unknown DeclContext kind"); |
| 500 | return this; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | DeclContext *DeclContext::getNextContext() { |
| 505 | switch (DeclKind) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 506 | case Decl::Namespace: |
| 507 | // Return the next namespace |
| 508 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 509 | |
| 510 | default: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | } |
| 514 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 515 | /// \brief Load the declarations within this lexical storage from an |
| 516 | /// external source. |
| 517 | void |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 518 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 519 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 520 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 521 | |
Eli Friedman | cdf5974 | 2009-04-27 23:43:36 +0000 | [diff] [blame] | 522 | llvm::SmallVector<uint32_t, 64> Decls; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 523 | if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this), |
| 524 | Decls)) |
| 525 | return; |
| 526 | |
| 527 | // There is no longer any lexical storage in this context |
| 528 | ExternalLexicalStorage = false; |
| 529 | |
| 530 | if (Decls.empty()) |
| 531 | return; |
| 532 | |
| 533 | // Resolve all of the declaration IDs into declarations, building up |
| 534 | // a chain of declarations via the Decl::NextDeclInContext field. |
| 535 | Decl *FirstNewDecl = 0; |
| 536 | Decl *PrevDecl = 0; |
| 537 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 538 | Decl *D = Source->GetDecl(Decls[I]); |
| 539 | if (PrevDecl) |
| 540 | PrevDecl->NextDeclInContext = D; |
| 541 | else |
| 542 | FirstNewDecl = D; |
| 543 | |
| 544 | PrevDecl = D; |
| 545 | } |
| 546 | |
| 547 | // Splice the newly-read declarations into the beginning of the list |
| 548 | // of declarations. |
| 549 | PrevDecl->NextDeclInContext = FirstDecl; |
| 550 | FirstDecl = FirstNewDecl; |
| 551 | if (!LastDecl) |
| 552 | LastDecl = PrevDecl; |
| 553 | } |
| 554 | |
| 555 | void |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 556 | DeclContext::LoadVisibleDeclsFromExternalStorage() const { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 557 | DeclContext *This = const_cast<DeclContext *>(this); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 558 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 559 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 560 | |
| 561 | llvm::SmallVector<VisibleDeclaration, 64> Decls; |
| 562 | if (Source->ReadDeclsVisibleInContext(This, Decls)) |
| 563 | return; |
| 564 | |
| 565 | // There is no longer any visible storage in this context |
| 566 | ExternalVisibleStorage = false; |
| 567 | |
| 568 | // Load the declaration IDs for all of the names visible in this |
| 569 | // context. |
| 570 | assert(!LookupPtr && "Have a lookup map before de-serialization?"); |
| 571 | StoredDeclsMap *Map = new StoredDeclsMap; |
| 572 | LookupPtr = Map; |
| 573 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 574 | (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations); |
| 575 | } |
| 576 | } |
| 577 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 578 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 579 | if (hasExternalLexicalStorage()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 580 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 581 | |
| 582 | // FIXME: Check whether we need to load some declarations from |
| 583 | // external storage. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 584 | return decl_iterator(FirstDecl); |
| 585 | } |
| 586 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 587 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 588 | if (hasExternalLexicalStorage()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 589 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 591 | return decl_iterator(); |
| 592 | } |
| 593 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 594 | bool DeclContext::decls_empty() const { |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 595 | if (hasExternalLexicalStorage()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 596 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | ac8f280 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 597 | |
| 598 | return !FirstDecl; |
| 599 | } |
| 600 | |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 601 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 5360207 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 602 | assert(D->getLexicalDeclContext() == this && |
| 603 | "Decl inserted into wrong lexical context"); |
Chris Lattner | fb8416a | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 604 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 605 | "Decl already inserted into a DeclContext"); |
| 606 | |
| 607 | if (FirstDecl) { |
Chris Lattner | fb8416a | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 608 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 609 | LastDecl = D; |
| 610 | } else { |
| 611 | FirstDecl = LastDecl = D; |
| 612 | } |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | void DeclContext::addDecl(Decl *D) { |
| 616 | addHiddenDecl(D); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 617 | |
| 618 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 619 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 622 | /// buildLookup - Build the lookup data structure with all of the |
| 623 | /// declarations in DCtx (and any other contexts linked to it or |
| 624 | /// transparent contexts nested within it). |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 625 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 626 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 627 | for (decl_iterator D = DCtx->decls_begin(), |
| 628 | DEnd = DCtx->decls_end(); |
Douglas Gregor | f0464ec | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 629 | D != DEnd; ++D) { |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 630 | // Insert this declaration into the lookup structure, but only |
| 631 | // if it's semantically in its decl context. During non-lazy |
| 632 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 633 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 634 | if (D->getDeclContext() == DCtx) |
| 635 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 636 | |
| 637 | // If this declaration is itself a transparent declaration context, |
| 638 | // add its members (recursively). |
| 639 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 640 | if (InnerCtx->isTransparentContext()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 641 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 646 | DeclContext::lookup_result |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 647 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 648 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 649 | if (PrimaryContext != this) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 650 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 651 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 652 | if (hasExternalVisibleStorage()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 653 | LoadVisibleDeclsFromExternalStorage(); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 655 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 656 | /// all of the linked DeclContexts (in declaration order!) and |
| 657 | /// inserting their values. |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 658 | if (!LookupPtr) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 659 | buildLookup(this); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 661 | if (!LookupPtr) |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 662 | return lookup_result(0, 0); |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 663 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 665 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr); |
| 666 | StoredDeclsMap::iterator Pos = Map->find(Name); |
| 667 | if (Pos == Map->end()) |
| 668 | return lookup_result(0, 0); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 669 | return Pos->second.getLookupResult(getParentASTContext()); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | DeclContext::lookup_const_result |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 673 | DeclContext::lookup(DeclarationName Name) const { |
| 674 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Chris Lattner | 9a502bd | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 677 | DeclContext *DeclContext::getLookupContext() { |
| 678 | DeclContext *Ctx = this; |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 679 | // Skip through transparent contexts. |
Douglas Gregor | 69e781f | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 680 | while (Ctx->isTransparentContext()) |
| 681 | Ctx = Ctx->getParent(); |
| 682 | return Ctx; |
| 683 | } |
| 684 | |
Douglas Gregor | 0d93f69 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 685 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 686 | DeclContext *Ctx = this; |
| 687 | // Skip through non-namespace, non-translation-unit contexts. |
| 688 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 689 | Ctx = Ctx->getParent(); |
| 690 | return Ctx->getPrimaryContext(); |
| 691 | } |
| 692 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 693 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 694 | // FIXME: This feels like a hack. Should DeclarationName support |
| 695 | // template-ids, or is there a better way to keep specializations |
| 696 | // from being visible? |
| 697 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 698 | return; |
| 699 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 700 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 701 | if (PrimaryContext != this) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 702 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
| 705 | |
| 706 | // If we already have a lookup data structure, perform the insertion |
| 707 | // into it. Otherwise, be lazy and don't build that structure until |
| 708 | // someone asks for it. |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 709 | if (LookupPtr) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 710 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 712 | // If we are a transparent context, insert into our parent context, |
| 713 | // too. This operation is recursive. |
| 714 | if (isTransparentContext()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 715 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 718 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 719 | // Skip unnamed declarations. |
| 720 | if (!D->getDeclName()) |
| 721 | return; |
| 722 | |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 723 | // FIXME: This feels like a hack. Should DeclarationName support |
| 724 | // template-ids, or is there a better way to keep specializations |
| 725 | // from being visible? |
| 726 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 727 | return; |
| 728 | |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 729 | if (!LookupPtr) |
| 730 | LookupPtr = new StoredDeclsMap; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 731 | |
| 732 | // Insert this declaration into the map. |
Douglas Gregor | 13c27cd | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 733 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr); |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 734 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 735 | if (DeclNameEntries.isNull()) { |
| 736 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | 6719b1f | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 737 | return; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 739 | |
Chris Lattner | ba589d4 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 740 | // If it is possible that this is a redeclaration, check to see if there is |
| 741 | // already a decl for which declarationReplaces returns true. If there is |
| 742 | // one, just replace it and return. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 743 | if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D)) |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 744 | return; |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 6719b1f | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 746 | // Put this declaration into the appropriate slot. |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 747 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 748 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 749 | |
| 750 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 751 | /// this context. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 752 | DeclContext::udir_iterator_range |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 753 | DeclContext::getUsingDirectives() const { |
| 754 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 755 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 756 | reinterpret_cast<udir_iterator>(Result.second)); |
| 757 | } |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 758 | |
| 759 | void StoredDeclsList::materializeDecls(ASTContext &Context) { |
| 760 | if (isNull()) |
| 761 | return; |
| 762 | |
| 763 | switch ((DataKind)(Data & 0x03)) { |
| 764 | case DK_Decl: |
| 765 | case DK_Decl_Vector: |
| 766 | break; |
| 767 | |
| 768 | case DK_DeclID: { |
| 769 | // Resolve this declaration ID to an actual declaration by |
| 770 | // querying the external AST source. |
| 771 | unsigned DeclID = Data >> 2; |
| 772 | |
| 773 | ExternalASTSource *Source = Context.getExternalSource(); |
| 774 | assert(Source && "No external AST source available!"); |
| 775 | |
| 776 | Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID)); |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | case DK_ID_Vector: { |
| 781 | // We have a vector of declaration IDs. Resolve all of them to |
| 782 | // actual declarations. |
| 783 | VectorTy &Vector = *getAsVector(); |
| 784 | ExternalASTSource *Source = Context.getExternalSource(); |
| 785 | assert(Source && "No external AST source available!"); |
| 786 | |
| 787 | for (unsigned I = 0, N = Vector.size(); I != N; ++I) |
| 788 | Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I])); |
| 789 | |
| 790 | Data = (Data & ~0x03) | DK_Decl_Vector; |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | } |