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" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame^] | 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/DeclTemplate.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <functional> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Statistics |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 31 | #define DECL(Derived, Base) static int n##Derived##s = 0; |
| 32 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 33 | |
| 34 | static bool StatSwitch = false; |
| 35 | |
| 36 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 37 | // keep them in a hash map instead of wasting space in the Decl class. |
| 38 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 39 | |
| 40 | static DeclAttrMapTy *DeclAttrs = 0; |
| 41 | |
| 42 | const char *Decl::getDeclKindName() const { |
| 43 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 44 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 45 | #define DECL(Derived, Base) case Derived: return #Derived; |
| 46 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 50 | const char *DeclContext::getDeclKindName() const { |
| 51 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 52 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
| 53 | #define DECL_CONTEXT(Node) case Decl::Node: return #Node; |
| 54 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 58 | bool Decl::CollectingStats(bool Enable) { |
| 59 | if (Enable) |
| 60 | StatSwitch = true; |
| 61 | return StatSwitch; |
| 62 | } |
| 63 | |
| 64 | void Decl::PrintStats() { |
| 65 | fprintf(stderr, "*** Decl Stats:\n"); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 67 | int totalDecls = 0; |
| 68 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 69 | #include "clang/AST/DeclNodes.def" |
| 70 | fprintf(stderr, " %d decls total.\n", totalDecls); |
| 71 | |
| 72 | int totalBytes = 0; |
| 73 | #define DECL(Derived, Base) \ |
| 74 | if (n##Derived##s > 0) { \ |
| 75 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 76 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 77 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 78 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 79 | } |
| 80 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 82 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Decl::addDeclKind(Kind k) { |
| 86 | switch (k) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 87 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 88 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 89 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // Decl Implementation |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 97 | void Decl::setDeclContext(DeclContext *DC) { |
| 98 | if (isOutOfSemaDC()) |
| 99 | delete getMultipleDC(); |
| 100 | |
| 101 | DeclCtx = reinterpret_cast<uintptr_t>(DC); |
| 102 | } |
| 103 | |
| 104 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 105 | if (DC == getLexicalDeclContext()) |
| 106 | return; |
| 107 | |
| 108 | if (isInSemaDC()) { |
| 109 | MultipleDC *MDC = new MultipleDC(); |
| 110 | MDC->SemanticDC = getDeclContext(); |
| 111 | MDC->LexicalDC = DC; |
| 112 | DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1; |
| 113 | } else { |
| 114 | getMultipleDC()->LexicalDC = DC; |
| 115 | } |
| 116 | } |
| 117 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 118 | // Out-of-line virtual method providing a home for Decl. |
| 119 | Decl::~Decl() { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 120 | if (isOutOfSemaDC()) |
| 121 | delete getMultipleDC(); |
| 122 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 123 | if (!HasAttrs) |
| 124 | return; |
| 125 | |
| 126 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
| 127 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 128 | |
| 129 | // release attributes. |
| 130 | delete it->second; |
| 131 | invalidateAttrs(); |
| 132 | } |
| 133 | |
| 134 | void Decl::addAttr(Attr *NewAttr) { |
| 135 | if (!DeclAttrs) |
| 136 | DeclAttrs = new DeclAttrMapTy(); |
| 137 | |
| 138 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
| 139 | |
| 140 | NewAttr->setNext(ExistingAttr); |
| 141 | ExistingAttr = NewAttr; |
| 142 | |
| 143 | HasAttrs = true; |
| 144 | } |
| 145 | |
| 146 | void Decl::invalidateAttrs() { |
| 147 | if (!HasAttrs) return; |
| 148 | |
| 149 | HasAttrs = false; |
| 150 | (*DeclAttrs)[this] = 0; |
| 151 | DeclAttrs->erase(this); |
| 152 | |
| 153 | if (DeclAttrs->empty()) { |
| 154 | delete DeclAttrs; |
| 155 | DeclAttrs = 0; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | const Attr *Decl::getAttrs() const { |
| 160 | if (!HasAttrs) |
| 161 | return 0; |
| 162 | |
| 163 | return (*DeclAttrs)[this]; |
| 164 | } |
| 165 | |
| 166 | void Decl::swapAttrs(Decl *RHS) { |
| 167 | bool HasLHSAttr = this->HasAttrs; |
| 168 | bool HasRHSAttr = RHS->HasAttrs; |
| 169 | |
| 170 | // Usually, neither decl has attrs, nothing to do. |
| 171 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 172 | |
| 173 | // If 'this' has no attrs, swap the other way. |
| 174 | if (!HasLHSAttr) |
| 175 | return RHS->swapAttrs(this); |
| 176 | |
| 177 | // Handle the case when both decls have attrs. |
| 178 | if (HasRHSAttr) { |
| 179 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // Otherwise, LHS has an attr and RHS doesn't. |
| 184 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 185 | (*DeclAttrs).erase(this); |
| 186 | this->HasAttrs = false; |
| 187 | RHS->HasAttrs = true; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void Decl::Destroy(ASTContext& C) { |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 192 | #if 0 |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 193 | // FIXME: Once ownership is fully understood, we can enable this code |
| 194 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 195 | DC->decls_begin()->Destroy(C); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 197 | // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0 |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 198 | // within the loop, only the Destroy method for the first Decl |
| 199 | // will deallocate all of the Decls in a chain. |
| 200 | |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 201 | Decl* N = NextDeclInScope; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 202 | |
| 203 | while (N) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 204 | Decl* Tmp = N->NextDeclInScope; |
| 205 | N->NextDeclInScope = 0; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 206 | N->Destroy(C); |
| 207 | N = Tmp; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 208 | } |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 209 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 210 | this->~Decl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 211 | C.Deallocate((void *)this); |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 212 | #endif |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 215 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
| 216 | return DeclContext::CastTo<Decl>(D); |
| 217 | } |
| 218 | |
| 219 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
| 220 | return DeclContext::CastTo<DeclContext>(D); |
| 221 | } |
| 222 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
| 224 | // DeclContext Implementation |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | |
Argyrios Kyrtzidis | 20bc676 | 2008-11-19 17:36:39 +0000 | [diff] [blame] | 227 | const DeclContext *DeclContext::getParent() const { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 228 | if (const Decl *D = dyn_cast<Decl>(this)) |
| 229 | return D->getDeclContext(); |
| 230 | |
| 231 | return NULL; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 232 | } |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 233 | |
| 234 | const DeclContext *DeclContext::getLexicalParent() const { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 235 | if (const Decl *D = dyn_cast<Decl>(this)) |
| 236 | return D->getLexicalDeclContext(); |
| 237 | |
Argyrios Kyrtzidis | 051c13a | 2008-11-19 18:07:24 +0000 | [diff] [blame] | 238 | return getParent(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 239 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 241 | // FIXME: We really want to use a DenseSet here to eliminate the |
| 242 | // redundant storage of the declaration names, but (1) it doesn't give |
| 243 | // us the ability to search based on DeclarationName, (2) we really |
| 244 | // need something more like a DenseMultiSet, and (3) it's |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 245 | // implemented in terms of DenseMap anyway. However, this data |
| 246 | // structure is really space-inefficient, so we'll have to do |
| 247 | // something. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 248 | typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> > |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 249 | StoredDeclsMap; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 250 | |
| 251 | DeclContext::~DeclContext() { |
| 252 | unsigned Size = LookupPtr.getInt(); |
| 253 | if (Size == LookupIsMap) { |
| 254 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 255 | delete Map; |
| 256 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 257 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 258 | delete [] Array; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 263 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
| 264 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 267 | bool DeclContext::isTransparentContext() const { |
| 268 | if (DeclKind == Decl::Enum) |
| 269 | return true; // FIXME: Check for C++0x scoped enums |
| 270 | else if (DeclKind == Decl::LinkageSpec) |
| 271 | return true; |
| 272 | else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 273 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 274 | else if (DeclKind == Decl::Namespace) |
| 275 | return false; // FIXME: Check for C++0x inline namespaces |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 280 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 281 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 282 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 283 | case Decl::LinkageSpec: |
| 284 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 285 | // There is only one DeclContext for these entities. |
| 286 | return this; |
| 287 | |
| 288 | case Decl::Namespace: |
| 289 | // The original namespace is our primary context. |
| 290 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 291 | |
| 292 | case Decl::Enum: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 293 | case Decl::Record: |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 294 | case Decl::CXXRecord: |
| 295 | // If this is a tag type that has a definition or is currently |
| 296 | // being defined, that definition is our primary context. |
| 297 | if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl)) |
| 298 | if (TagT->isBeingDefined() || |
| 299 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 300 | return TagT->getDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 301 | return this; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 302 | |
| 303 | case Decl::ObjCMethod: |
| 304 | return this; |
| 305 | |
| 306 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 307 | case Decl::ObjCProtocol: |
| 308 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 309 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 310 | return this; |
| 311 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 312 | case Decl::ObjCImplementation: |
| 313 | case Decl::ObjCCategoryImpl: |
| 314 | return this; |
| 315 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 316 | default: |
| 317 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 318 | "Unknown DeclContext kind"); |
| 319 | return this; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | DeclContext *DeclContext::getNextContext() { |
| 324 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 325 | case Decl::TranslationUnit: |
| 326 | case Decl::Enum: |
| 327 | case Decl::Record: |
| 328 | case Decl::CXXRecord: |
| 329 | case Decl::ObjCMethod: |
| 330 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 331 | case Decl::ObjCCategory: |
| 332 | case Decl::ObjCProtocol: |
| 333 | case Decl::ObjCImplementation: |
| 334 | case Decl::ObjCCategoryImpl: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 335 | case Decl::LinkageSpec: |
| 336 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 337 | // There is only one DeclContext for these entities. |
| 338 | return 0; |
| 339 | |
| 340 | case Decl::Namespace: |
| 341 | // Return the next namespace |
| 342 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 343 | |
| 344 | default: |
| 345 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 346 | "Unknown DeclContext kind"); |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 351 | void DeclContext::addDecl(Decl *D) { |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 352 | assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"); |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 353 | assert(!D->NextDeclInScope && D != LastDecl && |
| 354 | "Decl already inserted into a DeclContext"); |
| 355 | |
| 356 | if (FirstDecl) { |
| 357 | LastDecl->NextDeclInScope = D; |
| 358 | LastDecl = D; |
| 359 | } else { |
| 360 | FirstDecl = LastDecl = D; |
| 361 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 362 | |
| 363 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 364 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 367 | /// buildLookup - Build the lookup data structure with all of the |
| 368 | /// declarations in DCtx (and any other contexts linked to it or |
| 369 | /// transparent contexts nested within it). |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 370 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 371 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 372 | for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); |
| 373 | D != DEnd; ++D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 374 | // Insert this declaration into the lookup structure |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 375 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 376 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 377 | |
| 378 | // If this declaration is itself a transparent declaration context, |
| 379 | // add its members (recursively). |
| 380 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 381 | if (InnerCtx->isTransparentContext()) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 382 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 387 | DeclContext::lookup_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 388 | DeclContext::lookup(DeclarationName Name) { |
| 389 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 390 | if (PrimaryContext != this) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 391 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 393 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 394 | /// all of the linked DeclContexts (in declaration order!) and |
| 395 | /// inserting their values. |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 396 | if (LookupPtr.getPointer() == 0) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 397 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 398 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 399 | if (isLookupMap()) { |
| 400 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 401 | StoredDeclsMap::iterator Pos = Map->find(Name); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 402 | if (Pos != Map->end()) |
| 403 | return lookup_result(&Pos->second.front(), |
| 404 | &Pos->second.front() + Pos->second.size()); |
| 405 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // We have a small array. Look into it. |
| 409 | unsigned Size = LookupPtr.getInt(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 410 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 411 | for (unsigned Idx = 0; Idx != Size; ++Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 412 | if (Array[Idx]->getDeclName() == Name) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 413 | unsigned Last = Idx + 1; |
| 414 | while (Last != Size && Array[Last]->getDeclName() == Name) |
| 415 | ++Last; |
| 416 | return lookup_result(&Array[Idx], &Array[Last]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 419 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | DeclContext::lookup_const_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 423 | DeclContext::lookup(DeclarationName Name) const { |
| 424 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 427 | const DeclContext *DeclContext::getLookupContext() const { |
| 428 | const DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 429 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 430 | while (Ctx->isTransparentContext()) |
| 431 | Ctx = Ctx->getParent(); |
| 432 | return Ctx; |
| 433 | } |
| 434 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 435 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 436 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 437 | if (PrimaryContext != this) { |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 438 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | // If we already have a lookup data structure, perform the insertion |
| 443 | // into it. Otherwise, be lazy and don't build that structure until |
| 444 | // someone asks for it. |
| 445 | if (LookupPtr.getPointer()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 446 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 448 | // If we are a transparent context, insert into our parent context, |
| 449 | // too. This operation is recursive. |
| 450 | if (isTransparentContext()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 451 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 454 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 455 | // Skip unnamed declarations. |
| 456 | if (!D->getDeclName()) |
| 457 | return; |
| 458 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 459 | bool MayBeRedeclaration = true; |
| 460 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 461 | if (!isLookupMap()) { |
| 462 | unsigned Size = LookupPtr.getInt(); |
| 463 | |
| 464 | // The lookup data is stored as an array. Search through the array |
| 465 | // to find the insertion location. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 466 | NamedDecl **Array; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 467 | if (Size == 0) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 468 | Array = new NamedDecl*[LookupIsMap - 1]; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 469 | LookupPtr.setPointer(Array); |
| 470 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 471 | Array = static_cast<NamedDecl **>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // We always keep declarations of the same name next to each other |
| 475 | // in the array, so that it is easy to return multiple results |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 476 | // from lookup(). |
| 477 | unsigned FirstMatch; |
| 478 | for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch) |
| 479 | if (Array[FirstMatch]->getDeclName() == D->getDeclName()) |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 480 | break; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 482 | unsigned InsertPos = FirstMatch; |
| 483 | if (FirstMatch != Size) { |
| 484 | // We found another declaration with the same name. First |
| 485 | // determine whether this is a redeclaration of an existing |
| 486 | // declaration in this scope, in which case we will replace the |
| 487 | // existing declaration. |
| 488 | unsigned LastMatch = FirstMatch; |
| 489 | for (; LastMatch != Size; ++LastMatch) { |
| 490 | if (Array[LastMatch]->getDeclName() != D->getDeclName()) |
| 491 | break; |
| 492 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 493 | if (D->declarationReplaces(Array[LastMatch])) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 494 | // D is a redeclaration of an existing element in the |
| 495 | // array. Replace that element with D. |
| 496 | Array[LastMatch] = D; |
| 497 | return; |
| 498 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 501 | // [FirstMatch, LastMatch) contains the set of declarations that |
| 502 | // have the same name as this declaration. Determine where the |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 503 | // declaration D will be inserted into this range. |
| 504 | if (D->getKind() == Decl::UsingDirective || |
| 505 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 506 | InsertPos = LastMatch; |
| 507 | else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 508 | InsertPos = LastMatch - 1; |
| 509 | else |
| 510 | InsertPos = LastMatch; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (Size < LookupIsMap - 1) { |
| 514 | // The new declaration will fit in the array. Insert the new |
| 515 | // declaration at the position Match in the array. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 516 | for (unsigned Idx = Size; Idx > InsertPos; --Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 517 | Array[Idx] = Array[Idx-1]; |
| 518 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 519 | Array[InsertPos] = D; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 520 | LookupPtr.setInt(Size + 1); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | // We've reached capacity in this array. Create a map and copy in |
| 525 | // all of the declarations that were stored in the array. |
| 526 | StoredDeclsMap *Map = new StoredDeclsMap(16); |
| 527 | LookupPtr.setPointer(Map); |
| 528 | LookupPtr.setInt(LookupIsMap); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 529 | for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 530 | makeDeclVisibleInContextImpl(Array[Idx]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 531 | delete [] Array; |
| 532 | |
| 533 | // Fall through to perform insertion into the map. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 534 | MayBeRedeclaration = false; |
| 535 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 536 | |
| 537 | // Insert this declaration into the map. |
| 538 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 539 | StoredDeclsMap::iterator Pos = Map->find(D->getDeclName()); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 540 | if (Pos != Map->end()) { |
| 541 | if (MayBeRedeclaration) { |
| 542 | // Determine if this declaration is actually a redeclaration. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 543 | std::vector<NamedDecl *>::iterator Redecl |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 544 | = std::find_if(Pos->second.begin(), Pos->second.end(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 545 | std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces), |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 546 | D)); |
| 547 | if (Redecl != Pos->second.end()) { |
| 548 | *Redecl = D; |
| 549 | return; |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 553 | // Put this declaration into the appropriate slot. |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 554 | if (D->getKind() == Decl::UsingDirective || |
| 555 | D->getIdentifierNamespace() == Decl::IDNS_Tag |
| 556 | || Pos->second.empty()) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 557 | Pos->second.push_back(D); |
| 558 | else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 559 | NamedDecl *TagD = Pos->second.back(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 560 | Pos->second.back() = D; |
| 561 | Pos->second.push_back(TagD); |
| 562 | } else |
| 563 | Pos->second.push_back(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 564 | } else { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 565 | (*Map)[D->getDeclName()].push_back(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 568 | |
| 569 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 570 | /// this context. |
| 571 | DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const { |
| 572 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
| 573 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 574 | reinterpret_cast<udir_iterator>(Result.second)); |
| 575 | } |
| 576 | |