Eli Friedman | a8a09ac | 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 | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Argiris Kirtzidis | 6df1fc0 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/DeclTemplate.h" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 7d6220c | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 24 | #include <functional> |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 25 | #include <vector> |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Statistics |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 32 | #define DECL(Derived, Base) static int n##Derived##s = 0; |
| 33 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 34 | |
| 35 | static bool StatSwitch = false; |
| 36 | |
| 37 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 38 | // keep them in a hash map instead of wasting space in the Decl class. |
| 39 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 40 | |
| 41 | static DeclAttrMapTy *DeclAttrs = 0; |
| 42 | |
| 43 | const char *Decl::getDeclKindName() const { |
| 44 | switch (DeclKind) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 45 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 46 | #define DECL(Derived, Base) case Derived: return #Derived; |
| 47 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 51 | const char *DeclContext::getDeclKindName() const { |
| 52 | switch (DeclKind) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 53 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
Argiris Kirtzidis | 2d9b761 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 54 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 55 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 59 | bool Decl::CollectingStats(bool Enable) { |
| 60 | if (Enable) |
| 61 | StatSwitch = true; |
| 62 | return StatSwitch; |
| 63 | } |
| 64 | |
| 65 | void Decl::PrintStats() { |
| 66 | fprintf(stderr, "*** Decl Stats:\n"); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 68 | int totalDecls = 0; |
| 69 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 70 | #include "clang/AST/DeclNodes.def" |
| 71 | fprintf(stderr, " %d decls total.\n", totalDecls); |
| 72 | |
| 73 | int totalBytes = 0; |
| 74 | #define DECL(Derived, Base) \ |
| 75 | if (n##Derived##s > 0) { \ |
| 76 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 77 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 78 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 79 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 80 | } |
| 81 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 83 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Decl::addDeclKind(Kind k) { |
| 87 | switch (k) { |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 88 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 89 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 90 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Decl Implementation |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 98 | void Decl::setDeclContext(DeclContext *DC) { |
| 99 | if (isOutOfSemaDC()) |
| 100 | delete getMultipleDC(); |
| 101 | |
| 102 | DeclCtx = reinterpret_cast<uintptr_t>(DC); |
| 103 | } |
| 104 | |
| 105 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 106 | if (DC == getLexicalDeclContext()) |
| 107 | return; |
| 108 | |
| 109 | if (isInSemaDC()) { |
| 110 | MultipleDC *MDC = new MultipleDC(); |
| 111 | MDC->SemanticDC = getDeclContext(); |
| 112 | MDC->LexicalDC = DC; |
| 113 | DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1; |
| 114 | } else { |
| 115 | getMultipleDC()->LexicalDC = DC; |
| 116 | } |
| 117 | } |
| 118 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 119 | // Out-of-line virtual method providing a home for Decl. |
| 120 | Decl::~Decl() { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 121 | if (isOutOfSemaDC()) |
| 122 | delete getMultipleDC(); |
| 123 | |
Chris Lattner | 0fc6d64 | 2009-03-04 06:05:19 +0000 | [diff] [blame^] | 124 | assert(!HasAttrs && "attributes should have been freed by Destroy"); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void Decl::addAttr(Attr *NewAttr) { |
| 128 | if (!DeclAttrs) |
| 129 | DeclAttrs = new DeclAttrMapTy(); |
| 130 | |
| 131 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
| 132 | |
| 133 | NewAttr->setNext(ExistingAttr); |
| 134 | ExistingAttr = NewAttr; |
| 135 | |
| 136 | HasAttrs = true; |
| 137 | } |
| 138 | |
| 139 | void Decl::invalidateAttrs() { |
| 140 | if (!HasAttrs) return; |
| 141 | |
| 142 | HasAttrs = false; |
| 143 | (*DeclAttrs)[this] = 0; |
| 144 | DeclAttrs->erase(this); |
| 145 | |
| 146 | if (DeclAttrs->empty()) { |
| 147 | delete DeclAttrs; |
| 148 | DeclAttrs = 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | const Attr *Decl::getAttrs() const { |
| 153 | if (!HasAttrs) |
| 154 | return 0; |
| 155 | |
| 156 | return (*DeclAttrs)[this]; |
| 157 | } |
| 158 | |
| 159 | void Decl::swapAttrs(Decl *RHS) { |
| 160 | bool HasLHSAttr = this->HasAttrs; |
| 161 | bool HasRHSAttr = RHS->HasAttrs; |
| 162 | |
| 163 | // Usually, neither decl has attrs, nothing to do. |
| 164 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 165 | |
| 166 | // If 'this' has no attrs, swap the other way. |
| 167 | if (!HasLHSAttr) |
| 168 | return RHS->swapAttrs(this); |
| 169 | |
| 170 | // Handle the case when both decls have attrs. |
| 171 | if (HasRHSAttr) { |
| 172 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // Otherwise, LHS has an attr and RHS doesn't. |
| 177 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 178 | (*DeclAttrs).erase(this); |
| 179 | this->HasAttrs = false; |
| 180 | RHS->HasAttrs = true; |
| 181 | } |
| 182 | |
| 183 | |
Chris Lattner | 0fc6d64 | 2009-03-04 06:05:19 +0000 | [diff] [blame^] | 184 | void Decl::Destroy(ASTContext &C) { |
| 185 | // Free attributes for this decl. |
| 186 | if (HasAttrs) { |
| 187 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
| 188 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 189 | |
| 190 | // release attributes. |
| 191 | it->second->Destroy(C); |
| 192 | invalidateAttrs(); |
| 193 | HasAttrs = false; |
| 194 | } |
| 195 | |
Douglas Gregor | 8314d74 | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 196 | #if 0 |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 197 | // FIXME: Once ownership is fully understood, we can enable this code |
| 198 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 199 | DC->decls_begin()->Destroy(C); |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 201 | // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0 |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 202 | // within the loop, only the Destroy method for the first Decl |
| 203 | // will deallocate all of the Decls in a chain. |
| 204 | |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 205 | Decl* N = NextDeclInScope; |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 206 | |
| 207 | while (N) { |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 208 | Decl* Tmp = N->NextDeclInScope; |
| 209 | N->NextDeclInScope = 0; |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 210 | N->Destroy(C); |
| 211 | N = Tmp; |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 212 | } |
Douglas Gregor | 8314d74 | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 213 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 214 | this->~Decl(); |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 215 | C.Deallocate((void *)this); |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 216 | #endif |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 219 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 220 | Decl::Kind DK = D->getDeclKind(); |
| 221 | switch(DK) { |
| 222 | #define DECL_CONTEXT(Name) \ |
| 223 | case Decl::Name: \ |
| 224 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 225 | #define DECL_CONTEXT_BASE(Name) |
| 226 | #include "clang/AST/DeclNodes.def" |
| 227 | default: |
| 228 | #define DECL_CONTEXT_BASE(Name) \ |
| 229 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 230 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 231 | #include "clang/AST/DeclNodes.def" |
| 232 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 233 | return 0; |
| 234 | } |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 238 | Decl::Kind DK = D->getKind(); |
| 239 | switch(DK) { |
| 240 | #define DECL_CONTEXT(Name) \ |
| 241 | case Decl::Name: \ |
| 242 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 243 | #define DECL_CONTEXT_BASE(Name) |
| 244 | #include "clang/AST/DeclNodes.def" |
| 245 | default: |
| 246 | #define DECL_CONTEXT_BASE(Name) \ |
| 247 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 248 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 249 | #include "clang/AST/DeclNodes.def" |
| 250 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 251 | return 0; |
| 252 | } |
Argiris Kirtzidis | 4b662ea | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Eli Friedman | a8a09ac | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
| 256 | // DeclContext Implementation |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
Argiris Kirtzidis | 981aa13 | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 259 | bool DeclContext::classof(const Decl *D) { |
| 260 | switch (D->getKind()) { |
| 261 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 262 | #define DECL_CONTEXT_BASE(Name) |
| 263 | #include "clang/AST/DeclNodes.def" |
| 264 | return true; |
| 265 | default: |
| 266 | #define DECL_CONTEXT_BASE(Name) \ |
| 267 | if (D->getKind() >= Decl::Name##First && \ |
| 268 | D->getKind() <= Decl::Name##Last) \ |
| 269 | return true; |
| 270 | #include "clang/AST/DeclNodes.def" |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 275 | /// StoredDeclsList - This is an array of decls optimized a common case of only |
| 276 | /// containing one entry. |
| 277 | struct StoredDeclsList { |
| 278 | /// Data - If the integer is 0, then the pointer is a NamedDecl*. If the |
| 279 | /// integer is 1, then it is a VectorTy; |
| 280 | llvm::PointerIntPair<void*, 1, bool> Data; |
| 281 | |
| 282 | /// VectorTy - When in vector form, this is what the Data pointer points to. |
| 283 | typedef llvm::SmallVector<NamedDecl*, 4> VectorTy; |
| 284 | public: |
| 285 | StoredDeclsList() {} |
| 286 | StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) { |
| 287 | if (isVector()) |
| 288 | Data.setPointer(new VectorTy(getVector())); |
| 289 | } |
| 290 | |
| 291 | ~StoredDeclsList() { |
| 292 | // If this is a vector-form, free the vector. |
| 293 | if (isVector()) |
| 294 | delete &getVector(); |
| 295 | } |
| 296 | |
Chris Lattner | 51e57e6 | 2009-02-23 18:17:44 +0000 | [diff] [blame] | 297 | StoredDeclsList &operator=(const StoredDeclsList &RHS) { |
| 298 | if (isVector()) |
| 299 | delete &getVector(); |
| 300 | Data = RHS.Data; |
| 301 | if (isVector()) |
| 302 | Data.setPointer(new VectorTy(getVector())); |
| 303 | return *this; |
| 304 | } |
| 305 | |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 306 | bool isVector() const { return Data.getInt() != 0; } |
| 307 | bool isInline() const { return Data.getInt() == 0; } |
| 308 | bool isNull() const { return Data.getPointer() == 0; } |
| 309 | |
| 310 | void setOnlyValue(NamedDecl *ND) { |
| 311 | assert(isInline() && "Not inline"); |
| 312 | Data.setPointer(ND); |
| 313 | } |
| 314 | |
| 315 | /// getLookupResult - Return an array of all the decls that this list |
| 316 | /// represents. |
| 317 | DeclContext::lookup_result getLookupResult() { |
| 318 | // If we have a single inline unit, return it. |
| 319 | if (isInline()) { |
| 320 | assert(!isNull() && "Empty list isn't allowed"); |
| 321 | |
| 322 | // Data is a raw pointer to a NamedDecl*, return it. |
| 323 | void *Ptr = &Data; |
| 324 | return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1); |
| 325 | } |
| 326 | |
| 327 | // Otherwise, we have a range result. |
| 328 | VectorTy &V = getVector(); |
| 329 | return DeclContext::lookup_result(&V[0], &V[0]+V.size()); |
| 330 | } |
| 331 | |
| 332 | /// HandleRedeclaration - If this is a redeclaration of an existing decl, |
| 333 | /// replace the old one with D and return true. Otherwise return false. |
| 334 | bool HandleRedeclaration(NamedDecl *D) { |
| 335 | // Most decls only have one entry in their list, special case it. |
| 336 | if (isInline()) { |
| 337 | if (!D->declarationReplaces(getInlineValue())) |
| 338 | return false; |
| 339 | setOnlyValue(D); |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | // Determine if this declaration is actually a redeclaration. |
| 344 | VectorTy &Vec = getVector(); |
| 345 | VectorTy::iterator RDI |
| 346 | = std::find_if(Vec.begin(), Vec.end(), |
| 347 | std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces), |
| 348 | D)); |
| 349 | if (RDI == Vec.end()) |
| 350 | return false; |
| 351 | *RDI = D; |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | /// AddSubsequentDecl - This is called on the second and later decl when it is |
| 356 | /// not a redeclaration to merge it into the appropriate place in our list. |
| 357 | /// |
| 358 | void AddSubsequentDecl(NamedDecl *D) { |
| 359 | // If this is the second decl added to the list, convert this to vector |
| 360 | // form. |
| 361 | if (isInline()) { |
| 362 | NamedDecl *OldD = getInlineValue(); |
| 363 | Data.setInt(1); |
| 364 | VectorTy *VT = new VectorTy(); |
| 365 | VT->push_back(OldD); |
| 366 | Data.setPointer(VT); |
| 367 | } |
| 368 | |
| 369 | VectorTy &Vec = getVector(); |
| 370 | if (isa<UsingDirectiveDecl>(D) || |
| 371 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 372 | Vec.push_back(D); |
| 373 | else if (Vec.back()->getIdentifierNamespace() == Decl::IDNS_Tag) { |
| 374 | NamedDecl *TagD = Vec.back(); |
| 375 | Vec.back() = D; |
| 376 | Vec.push_back(TagD); |
| 377 | } else |
| 378 | Vec.push_back(D); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | private: |
| 383 | VectorTy &getVector() const { |
| 384 | assert(isVector() && "Not in vector form"); |
| 385 | return *static_cast<VectorTy*>(Data.getPointer()); |
| 386 | } |
| 387 | |
| 388 | NamedDecl *getInlineValue() const { |
| 389 | assert(isInline() && "Not in inline form"); |
| 390 | return (NamedDecl*)Data.getPointer(); |
| 391 | } |
| 392 | }; |
| 393 | |
| 394 | |
| 395 | |
| 396 | typedef llvm::DenseMap<DeclarationName, StoredDeclsList> StoredDeclsMap; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 397 | |
| 398 | DeclContext::~DeclContext() { |
| 399 | unsigned Size = LookupPtr.getInt(); |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 400 | if (Size == LookupIsMap) |
| 401 | delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 402 | else |
| 403 | delete [] static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | f4006be | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 407 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
| 408 | (*D++)->Destroy(C); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 411 | bool DeclContext::isTransparentContext() const { |
| 412 | if (DeclKind == Decl::Enum) |
| 413 | return true; // FIXME: Check for C++0x scoped enums |
| 414 | else if (DeclKind == Decl::LinkageSpec) |
| 415 | return true; |
Douglas Gregor | 1db1293 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 416 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 417 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 418 | else if (DeclKind == Decl::Namespace) |
| 419 | return false; // FIXME: Check for C++0x inline namespaces |
| 420 | |
| 421 | return false; |
| 422 | } |
| 423 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 424 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 425 | switch (DeclKind) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 426 | case Decl::TranslationUnit: |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 427 | case Decl::LinkageSpec: |
| 428 | case Decl::Block: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 429 | // There is only one DeclContext for these entities. |
| 430 | return this; |
| 431 | |
| 432 | case Decl::Namespace: |
| 433 | // The original namespace is our primary context. |
| 434 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 435 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 436 | case Decl::ObjCMethod: |
| 437 | return this; |
| 438 | |
| 439 | case Decl::ObjCInterface: |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 440 | case Decl::ObjCProtocol: |
| 441 | case Decl::ObjCCategory: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 442 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 443 | return this; |
| 444 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 445 | case Decl::ObjCImplementation: |
| 446 | case Decl::ObjCCategoryImpl: |
| 447 | return this; |
| 448 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 449 | default: |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 450 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 451 | // If this is a tag type that has a definition or is currently |
| 452 | // being defined, that definition is our primary context. |
Douglas Gregor | 9c7825b | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 453 | if (const TagType *TagT = cast<TagDecl>(this)->TypeForDecl->getAsTagType()) |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 454 | if (TagT->isBeingDefined() || |
| 455 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 456 | return TagT->getDecl(); |
| 457 | return this; |
| 458 | } |
| 459 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 460 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 461 | "Unknown DeclContext kind"); |
| 462 | return this; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | DeclContext *DeclContext::getNextContext() { |
| 467 | switch (DeclKind) { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 468 | case Decl::Namespace: |
| 469 | // Return the next namespace |
| 470 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 471 | |
| 472 | default: |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | } |
| 476 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 477 | void DeclContext::addDecl(Decl *D) { |
Chris Lattner | 5360207 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 478 | assert(D->getLexicalDeclContext() == this && |
| 479 | "Decl inserted into wrong lexical context"); |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 480 | assert(!D->NextDeclInScope && D != LastDecl && |
| 481 | "Decl already inserted into a DeclContext"); |
| 482 | |
| 483 | if (FirstDecl) { |
| 484 | LastDecl->NextDeclInScope = D; |
| 485 | LastDecl = D; |
| 486 | } else { |
| 487 | FirstDecl = LastDecl = D; |
| 488 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 489 | |
| 490 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 491 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 494 | /// buildLookup - Build the lookup data structure with all of the |
| 495 | /// declarations in DCtx (and any other contexts linked to it or |
| 496 | /// transparent contexts nested within it). |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 497 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 498 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | f0464ec | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 499 | for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); |
| 500 | D != DEnd; ++D) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 501 | // Insert this declaration into the lookup structure |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 502 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 503 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 504 | |
| 505 | // If this declaration is itself a transparent declaration context, |
| 506 | // add its members (recursively). |
| 507 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 508 | if (InnerCtx->isTransparentContext()) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 509 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 514 | DeclContext::lookup_result |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 515 | DeclContext::lookup(DeclarationName Name) { |
| 516 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 517 | if (PrimaryContext != this) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 518 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 520 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 521 | /// all of the linked DeclContexts (in declaration order!) and |
| 522 | /// inserting their values. |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 523 | if (LookupPtr.getPointer() == 0) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 524 | buildLookup(this); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 526 | if (isLookupMap()) { |
| 527 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 528 | StoredDeclsMap::iterator Pos = Map->find(Name); |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 529 | if (Pos == Map->end()) |
| 530 | return lookup_result(0, 0); |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 531 | return Pos->second.getLookupResult(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // We have a small array. Look into it. |
| 535 | unsigned Size = LookupPtr.getInt(); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 536 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 537 | for (unsigned Idx = 0; Idx != Size; ++Idx) |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 538 | if (Array[Idx]->getDeclName() == Name) { |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 539 | unsigned Last = Idx + 1; |
| 540 | while (Last != Size && Array[Last]->getDeclName() == Name) |
| 541 | ++Last; |
| 542 | return lookup_result(&Array[Idx], &Array[Last]); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 545 | return lookup_result(0, 0); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | DeclContext::lookup_const_result |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 549 | DeclContext::lookup(DeclarationName Name) const { |
| 550 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | 38d38a0 | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 553 | const DeclContext *DeclContext::getLookupContext() const { |
| 554 | const DeclContext *Ctx = this; |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 555 | // Skip through transparent contexts. |
Douglas Gregor | 69e781f | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 556 | while (Ctx->isTransparentContext()) |
| 557 | Ctx = Ctx->getParent(); |
| 558 | return Ctx; |
| 559 | } |
| 560 | |
Douglas Gregor | 0d93f69 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 561 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 562 | DeclContext *Ctx = this; |
| 563 | // Skip through non-namespace, non-translation-unit contexts. |
| 564 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 565 | Ctx = Ctx->getParent(); |
| 566 | return Ctx->getPrimaryContext(); |
| 567 | } |
| 568 | |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 569 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 570 | // FIXME: This feels like a hack. Should DeclarationName support |
| 571 | // template-ids, or is there a better way to keep specializations |
| 572 | // from being visible? |
| 573 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 574 | return; |
| 575 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 576 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 577 | if (PrimaryContext != this) { |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 578 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
| 582 | // If we already have a lookup data structure, perform the insertion |
| 583 | // into it. Otherwise, be lazy and don't build that structure until |
| 584 | // someone asks for it. |
| 585 | if (LookupPtr.getPointer()) |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 586 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 588 | // If we are a transparent context, insert into our parent context, |
| 589 | // too. This operation is recursive. |
| 590 | if (isTransparentContext()) |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 591 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 594 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 595 | // Skip unnamed declarations. |
| 596 | if (!D->getDeclName()) |
| 597 | return; |
| 598 | |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 599 | // FIXME: This feels like a hack. Should DeclarationName support |
| 600 | // template-ids, or is there a better way to keep specializations |
| 601 | // from being visible? |
| 602 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 603 | return; |
| 604 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 605 | bool MayBeRedeclaration = true; |
| 606 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 607 | if (!isLookupMap()) { |
| 608 | unsigned Size = LookupPtr.getInt(); |
| 609 | |
| 610 | // The lookup data is stored as an array. Search through the array |
| 611 | // to find the insertion location. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 612 | NamedDecl **Array; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 613 | if (Size == 0) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 614 | Array = new NamedDecl*[LookupIsMap - 1]; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 615 | LookupPtr.setPointer(Array); |
| 616 | } else { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 617 | Array = static_cast<NamedDecl **>(LookupPtr.getPointer()); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // We always keep declarations of the same name next to each other |
| 621 | // in the array, so that it is easy to return multiple results |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 622 | // from lookup(). |
| 623 | unsigned FirstMatch; |
| 624 | for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch) |
| 625 | if (Array[FirstMatch]->getDeclName() == D->getDeclName()) |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 626 | break; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 628 | unsigned InsertPos = FirstMatch; |
| 629 | if (FirstMatch != Size) { |
| 630 | // We found another declaration with the same name. First |
| 631 | // determine whether this is a redeclaration of an existing |
| 632 | // declaration in this scope, in which case we will replace the |
| 633 | // existing declaration. |
| 634 | unsigned LastMatch = FirstMatch; |
| 635 | for (; LastMatch != Size; ++LastMatch) { |
| 636 | if (Array[LastMatch]->getDeclName() != D->getDeclName()) |
| 637 | break; |
| 638 | |
Douglas Gregor | 6e71edc | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 639 | if (D->declarationReplaces(Array[LastMatch])) { |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 640 | // D is a redeclaration of an existing element in the |
| 641 | // array. Replace that element with D. |
| 642 | Array[LastMatch] = D; |
| 643 | return; |
| 644 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 647 | // [FirstMatch, LastMatch) contains the set of declarations that |
| 648 | // have the same name as this declaration. Determine where the |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 649 | // declaration D will be inserted into this range. |
| 650 | if (D->getKind() == Decl::UsingDirective || |
| 651 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 652 | InsertPos = LastMatch; |
| 653 | else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 654 | InsertPos = LastMatch - 1; |
| 655 | else |
| 656 | InsertPos = LastMatch; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (Size < LookupIsMap - 1) { |
| 660 | // The new declaration will fit in the array. Insert the new |
| 661 | // declaration at the position Match in the array. |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 662 | for (unsigned Idx = Size; Idx > InsertPos; --Idx) |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 663 | Array[Idx] = Array[Idx-1]; |
| 664 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 665 | Array[InsertPos] = D; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 666 | LookupPtr.setInt(Size + 1); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | // We've reached capacity in this array. Create a map and copy in |
| 671 | // all of the declarations that were stored in the array. |
| 672 | StoredDeclsMap *Map = new StoredDeclsMap(16); |
| 673 | LookupPtr.setPointer(Map); |
| 674 | LookupPtr.setInt(LookupIsMap); |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 675 | for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx) |
Douglas Gregor | 9ac66d2 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 676 | makeDeclVisibleInContextImpl(Array[Idx]); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 677 | delete [] Array; |
| 678 | |
| 679 | // Fall through to perform insertion into the map. |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 680 | MayBeRedeclaration = false; |
| 681 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 682 | |
| 683 | // Insert this declaration into the map. |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 684 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 685 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 686 | if (DeclNameEntries.isNull()) { |
| 687 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | 6719b1f | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 688 | return; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 690 | |
Chris Lattner | ba589d4 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 691 | // If it is possible that this is a redeclaration, check to see if there is |
| 692 | // already a decl for which declarationReplaces returns true. If there is |
| 693 | // one, just replace it and return. |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 694 | if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D)) |
| 695 | return; |
Chris Lattner | 265f01d | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 6719b1f | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 697 | // Put this declaration into the appropriate slot. |
Chris Lattner | 77820d5 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 698 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 699 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 700 | |
| 701 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 702 | /// this context. |
| 703 | DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const { |
| 704 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
| 705 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 706 | reinterpret_cast<udir_iterator>(Result.second)); |
| 707 | } |
| 708 | |