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