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