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