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" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 25 | #include <cstdio> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Statistics |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 33 | #define DECL(Derived, Base) static int n##Derived##s = 0; |
| 34 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 35 | |
| 36 | static bool StatSwitch = false; |
| 37 | |
| 38 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 39 | // keep them in a hash map instead of wasting space in the Decl class. |
| 40 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 41 | |
| 42 | static DeclAttrMapTy *DeclAttrs = 0; |
| 43 | |
| 44 | const char *Decl::getDeclKindName() const { |
| 45 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 46 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 47 | #define DECL(Derived, Base) case Derived: return #Derived; |
| 48 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 52 | const char *DeclContext::getDeclKindName() const { |
| 53 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 54 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 55 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 56 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 60 | bool Decl::CollectingStats(bool Enable) { |
| 61 | if (Enable) |
| 62 | StatSwitch = true; |
| 63 | return StatSwitch; |
| 64 | } |
| 65 | |
| 66 | void Decl::PrintStats() { |
| 67 | fprintf(stderr, "*** Decl Stats:\n"); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 69 | int totalDecls = 0; |
| 70 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 71 | #include "clang/AST/DeclNodes.def" |
| 72 | fprintf(stderr, " %d decls total.\n", totalDecls); |
| 73 | |
| 74 | int totalBytes = 0; |
| 75 | #define DECL(Derived, Base) \ |
| 76 | if (n##Derived##s > 0) { \ |
| 77 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 78 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 79 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 80 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 81 | } |
| 82 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 84 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void Decl::addDeclKind(Kind k) { |
| 88 | switch (k) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 89 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 90 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 91 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 96 | // PrettyStackTraceDecl Implementation |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
| 99 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 100 | SourceLocation TheLoc = Loc; |
| 101 | if (TheLoc.isInvalid() && TheDecl) |
| 102 | TheLoc = TheDecl->getLocation(); |
| 103 | |
| 104 | if (TheLoc.isValid()) { |
| 105 | TheLoc.print(OS, SM); |
| 106 | OS << ": "; |
| 107 | } |
| 108 | |
| 109 | OS << Message; |
| 110 | |
| 111 | if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
| 112 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 113 | OS << '\n'; |
| 114 | } |
| 115 | |
| 116 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 117 | // Decl Implementation |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 120 | // Out-of-line virtual method providing a home for Decl. |
| 121 | Decl::~Decl() { |
| 122 | if (isOutOfSemaDC()) |
| 123 | delete getMultipleDC(); |
| 124 | |
| 125 | assert(!HasAttrs && "attributes should have been freed by Destroy"); |
| 126 | } |
| 127 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 128 | void Decl::setDeclContext(DeclContext *DC) { |
| 129 | if (isOutOfSemaDC()) |
| 130 | delete getMultipleDC(); |
| 131 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 132 | DeclCtx = DC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 136 | if (DC == getLexicalDeclContext()) |
| 137 | return; |
| 138 | |
| 139 | if (isInSemaDC()) { |
| 140 | MultipleDC *MDC = new MultipleDC(); |
| 141 | MDC->SemanticDC = getDeclContext(); |
| 142 | MDC->LexicalDC = DC; |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 143 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 144 | } else { |
| 145 | getMultipleDC()->LexicalDC = DC; |
| 146 | } |
| 147 | } |
| 148 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 149 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 150 | switch (DeclKind) { |
| 151 | default: |
| 152 | if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) |
| 153 | return IDNS_Ordinary; |
| 154 | assert(0 && "Unknown decl kind!"); |
| 155 | case OverloadedFunction: |
| 156 | case Typedef: |
| 157 | case EnumConstant: |
| 158 | case Var: |
| 159 | case ImplicitParam: |
| 160 | case ParmVar: |
| 161 | case OriginalParmVar: |
| 162 | case NonTypeTemplateParm: |
| 163 | case ObjCMethod: |
| 164 | case ObjCContainer: |
| 165 | case ObjCCategory: |
| 166 | case ObjCInterface: |
| 167 | case ObjCCategoryImpl: |
| 168 | case ObjCProperty: |
| 169 | case ObjCCompatibleAlias: |
| 170 | return IDNS_Ordinary; |
| 171 | |
| 172 | case ObjCProtocol: |
| 173 | return IDNS_Protocol; |
| 174 | |
| 175 | case Field: |
| 176 | case ObjCAtDefsField: |
| 177 | case ObjCIvar: |
| 178 | return IDNS_Member; |
| 179 | |
| 180 | case Record: |
| 181 | case CXXRecord: |
| 182 | case Enum: |
| 183 | case TemplateTypeParm: |
| 184 | return IDNS_Tag; |
| 185 | |
| 186 | case Namespace: |
| 187 | case Template: |
| 188 | case FunctionTemplate: |
| 189 | case ClassTemplate: |
| 190 | case TemplateTemplateParm: |
Anders Carlsson | faf0e87 | 2009-03-28 23:02:53 +0000 | [diff] [blame] | 191 | case NamespaceAlias: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 192 | return IDNS_Tag | IDNS_Ordinary; |
| 193 | |
| 194 | // Never have names. |
| 195 | case LinkageSpec: |
| 196 | case FileScopeAsm: |
| 197 | case StaticAssert: |
| 198 | case ObjCClass: |
| 199 | case ObjCImplementation: |
| 200 | case ObjCPropertyImpl: |
| 201 | case ObjCForwardProtocol: |
| 202 | case Block: |
| 203 | case TranslationUnit: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 205 | // Aren't looked up? |
| 206 | case UsingDirective: |
| 207 | case ClassTemplateSpecialization: |
| 208 | return 0; |
| 209 | } |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void Decl::addAttr(Attr *NewAttr) { |
| 213 | if (!DeclAttrs) |
| 214 | DeclAttrs = new DeclAttrMapTy(); |
| 215 | |
| 216 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
| 217 | |
| 218 | NewAttr->setNext(ExistingAttr); |
| 219 | ExistingAttr = NewAttr; |
| 220 | |
| 221 | HasAttrs = true; |
| 222 | } |
| 223 | |
| 224 | void Decl::invalidateAttrs() { |
| 225 | if (!HasAttrs) return; |
| 226 | |
| 227 | HasAttrs = false; |
| 228 | (*DeclAttrs)[this] = 0; |
| 229 | DeclAttrs->erase(this); |
| 230 | |
| 231 | if (DeclAttrs->empty()) { |
| 232 | delete DeclAttrs; |
| 233 | DeclAttrs = 0; |
| 234 | } |
| 235 | } |
| 236 | |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 237 | const Attr *Decl::getAttrsImpl() const { |
| 238 | assert(HasAttrs && "getAttrs() should verify this!"); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 239 | return (*DeclAttrs)[this]; |
| 240 | } |
| 241 | |
| 242 | void Decl::swapAttrs(Decl *RHS) { |
| 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) |
| 251 | return RHS->swapAttrs(this); |
| 252 | |
| 253 | // Handle the case when both decls have attrs. |
| 254 | if (HasRHSAttr) { |
| 255 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | // Otherwise, LHS has an attr and RHS doesn't. |
| 260 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 261 | (*DeclAttrs).erase(this); |
| 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) { |
| 270 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
| 271 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 272 | |
| 273 | // release attributes. |
| 274 | it->second->Destroy(C); |
| 275 | invalidateAttrs(); |
| 276 | HasAttrs = false; |
| 277 | } |
| 278 | |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 279 | #if 0 |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 280 | // FIXME: Once ownership is fully understood, we can enable this code |
| 281 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 282 | DC->decls_begin()->Destroy(C); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 284 | // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0 |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 285 | // within the loop, only the Destroy method for the first Decl |
| 286 | // will deallocate all of the Decls in a chain. |
| 287 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 288 | Decl* N = getNextDeclInContext(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 289 | |
| 290 | while (N) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 291 | Decl* Tmp = N->getNextDeclInContext(); |
| 292 | N->NextDeclInContext = 0; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 293 | N->Destroy(C); |
| 294 | N = Tmp; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 295 | } |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 296 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 297 | this->~Decl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 298 | C.Deallocate((void *)this); |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 299 | #endif |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 302 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 303 | Decl::Kind DK = D->getDeclKind(); |
| 304 | switch(DK) { |
| 305 | #define DECL_CONTEXT(Name) \ |
| 306 | case Decl::Name: \ |
| 307 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 308 | #define DECL_CONTEXT_BASE(Name) |
| 309 | #include "clang/AST/DeclNodes.def" |
| 310 | default: |
| 311 | #define DECL_CONTEXT_BASE(Name) \ |
| 312 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 313 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 314 | #include "clang/AST/DeclNodes.def" |
| 315 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 316 | return 0; |
| 317 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 321 | Decl::Kind DK = D->getKind(); |
| 322 | switch(DK) { |
| 323 | #define DECL_CONTEXT(Name) \ |
| 324 | case Decl::Name: \ |
| 325 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 326 | #define DECL_CONTEXT_BASE(Name) |
| 327 | #include "clang/AST/DeclNodes.def" |
| 328 | default: |
| 329 | #define DECL_CONTEXT_BASE(Name) \ |
| 330 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 331 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 332 | #include "clang/AST/DeclNodes.def" |
| 333 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 334 | return 0; |
| 335 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 338 | #ifndef NDEBUG |
| 339 | void Decl::CheckAccessDeclContext() const { |
Douglas Gregor | 5c27f2b | 2009-04-07 20:58:25 +0000 | [diff] [blame] | 340 | assert((Access != AS_none || isa<TranslationUnitDecl>(this) || |
| 341 | !isa<CXXRecordDecl>(getDeclContext())) && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 342 | "Access specifier is AS_none inside a record decl"); |
| 343 | } |
| 344 | |
| 345 | #endif |
| 346 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 347 | //===----------------------------------------------------------------------===// |
| 348 | // DeclContext Implementation |
| 349 | //===----------------------------------------------------------------------===// |
| 350 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 351 | bool DeclContext::classof(const Decl *D) { |
| 352 | switch (D->getKind()) { |
| 353 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 354 | #define DECL_CONTEXT_BASE(Name) |
| 355 | #include "clang/AST/DeclNodes.def" |
| 356 | return true; |
| 357 | default: |
| 358 | #define DECL_CONTEXT_BASE(Name) \ |
| 359 | if (D->getKind() >= Decl::Name##First && \ |
| 360 | D->getKind() <= Decl::Name##Last) \ |
| 361 | return true; |
| 362 | #include "clang/AST/DeclNodes.def" |
| 363 | return false; |
| 364 | } |
| 365 | } |
| 366 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 367 | DeclContext::~DeclContext() { |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 368 | delete static_cast<StoredDeclsMap*>(LookupPtr); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 372 | for (decl_iterator D = decls_begin(C); D != decls_end(C); ) |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 373 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 376 | bool DeclContext::isTransparentContext() const { |
| 377 | if (DeclKind == Decl::Enum) |
| 378 | return true; // FIXME: Check for C++0x scoped enums |
| 379 | else if (DeclKind == Decl::LinkageSpec) |
| 380 | return true; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 381 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 382 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 383 | else if (DeclKind == Decl::Namespace) |
| 384 | return false; // FIXME: Check for C++0x inline namespaces |
| 385 | |
| 386 | return false; |
| 387 | } |
| 388 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 389 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 390 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 391 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 392 | case Decl::LinkageSpec: |
| 393 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 394 | // There is only one DeclContext for these entities. |
| 395 | return this; |
| 396 | |
| 397 | case Decl::Namespace: |
| 398 | // The original namespace is our primary context. |
| 399 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 400 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 401 | case Decl::ObjCMethod: |
| 402 | return this; |
| 403 | |
| 404 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 405 | case Decl::ObjCProtocol: |
| 406 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 407 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 408 | return this; |
| 409 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 410 | case Decl::ObjCImplementation: |
| 411 | case Decl::ObjCCategoryImpl: |
| 412 | return this; |
| 413 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 414 | default: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 415 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 416 | // If this is a tag type that has a definition or is currently |
| 417 | // being defined, that definition is our primary context. |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 418 | if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 419 | if (TagT->isBeingDefined() || |
| 420 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 421 | return TagT->getDecl(); |
| 422 | return this; |
| 423 | } |
| 424 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 425 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 426 | "Unknown DeclContext kind"); |
| 427 | return this; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | DeclContext *DeclContext::getNextContext() { |
| 432 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 433 | case Decl::Namespace: |
| 434 | // Return the next namespace |
| 435 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 436 | |
| 437 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | } |
| 441 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 442 | DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const { |
| 443 | return decl_iterator(FirstDecl); |
| 444 | } |
| 445 | |
| 446 | DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const { |
| 447 | return decl_iterator(); |
| 448 | } |
| 449 | |
| 450 | void DeclContext::addDecl(ASTContext &Context, Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 451 | assert(D->getLexicalDeclContext() == this && |
| 452 | "Decl inserted into wrong lexical context"); |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 453 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 454 | "Decl already inserted into a DeclContext"); |
| 455 | |
| 456 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 457 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 458 | LastDecl = D; |
| 459 | } else { |
| 460 | FirstDecl = LastDecl = D; |
| 461 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 462 | |
| 463 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 464 | ND->getDeclContext()->makeDeclVisibleInContext(Context, ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 467 | /// buildLookup - Build the lookup data structure with all of the |
| 468 | /// declarations in DCtx (and any other contexts linked to it or |
| 469 | /// transparent contexts nested within it). |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 470 | void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 471 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 472 | for (decl_iterator D = DCtx->decls_begin(Context), |
| 473 | DEnd = DCtx->decls_end(Context); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 474 | D != DEnd; ++D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 475 | // Insert this declaration into the lookup structure |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 476 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 477 | makeDeclVisibleInContextImpl(Context, ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 478 | |
| 479 | // If this declaration is itself a transparent declaration context, |
| 480 | // add its members (recursively). |
| 481 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 482 | if (InnerCtx->isTransparentContext()) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 483 | buildLookup(Context, InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 488 | DeclContext::lookup_result |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 489 | DeclContext::lookup(ASTContext &Context, DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 490 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 491 | if (PrimaryContext != this) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 492 | return PrimaryContext->lookup(Context, Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 494 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 495 | /// all of the linked DeclContexts (in declaration order!) and |
| 496 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 497 | if (!LookupPtr) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 498 | buildLookup(Context, this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 500 | if (!LookupPtr) |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 501 | return lookup_result(0, 0); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 502 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 504 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr); |
| 505 | StoredDeclsMap::iterator Pos = Map->find(Name); |
| 506 | if (Pos == Map->end()) |
| 507 | return lookup_result(0, 0); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 508 | return Pos->second.getLookupResult(Context); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | DeclContext::lookup_const_result |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 512 | DeclContext::lookup(ASTContext &Context, DeclarationName Name) const { |
| 513 | return const_cast<DeclContext*>(this)->lookup(Context, Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 516 | DeclContext *DeclContext::getLookupContext() { |
| 517 | DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 518 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 519 | while (Ctx->isTransparentContext()) |
| 520 | Ctx = Ctx->getParent(); |
| 521 | return Ctx; |
| 522 | } |
| 523 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 524 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 525 | DeclContext *Ctx = this; |
| 526 | // Skip through non-namespace, non-translation-unit contexts. |
| 527 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 528 | Ctx = Ctx->getParent(); |
| 529 | return Ctx->getPrimaryContext(); |
| 530 | } |
| 531 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 532 | void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 533 | // FIXME: This feels like a hack. Should DeclarationName support |
| 534 | // template-ids, or is there a better way to keep specializations |
| 535 | // from being visible? |
| 536 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 537 | return; |
| 538 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 539 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 540 | if (PrimaryContext != this) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 541 | PrimaryContext->makeDeclVisibleInContext(Context, D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | |
| 545 | // If we already have a lookup data structure, perform the insertion |
| 546 | // into it. Otherwise, be lazy and don't build that structure until |
| 547 | // someone asks for it. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 548 | if (LookupPtr) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 549 | makeDeclVisibleInContextImpl(Context, D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 551 | // If we are a transparent context, insert into our parent context, |
| 552 | // too. This operation is recursive. |
| 553 | if (isTransparentContext()) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 554 | getParent()->makeDeclVisibleInContext(Context, D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 557 | void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context, |
| 558 | NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 559 | // Skip unnamed declarations. |
| 560 | if (!D->getDeclName()) |
| 561 | return; |
| 562 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 563 | // FIXME: This feels like a hack. Should DeclarationName support |
| 564 | // template-ids, or is there a better way to keep specializations |
| 565 | // from being visible? |
| 566 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 567 | return; |
| 568 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 569 | if (!LookupPtr) |
| 570 | LookupPtr = new StoredDeclsMap; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 571 | |
| 572 | // Insert this declaration into the map. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 573 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr); |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 574 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 575 | if (DeclNameEntries.isNull()) { |
| 576 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 577 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 578 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 579 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 580 | // If it is possible that this is a redeclaration, check to see if there is |
| 581 | // already a decl for which declarationReplaces returns true. If there is |
| 582 | // one, just replace it and return. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 583 | if (DeclNameEntries.HandleRedeclaration(Context, D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 584 | return; |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 585 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 586 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 587 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 588 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 589 | |
| 590 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 591 | /// this context. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame^] | 592 | DeclContext::udir_iterator_range |
| 593 | DeclContext::getUsingDirectives(ASTContext &Context) const { |
| 594 | lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 595 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 596 | reinterpret_cast<udir_iterator>(Result.second)); |
| 597 | } |