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