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