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!"); |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 53 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 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) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 216 | Decl::Kind DK = D->getDeclKind(); |
| 217 | switch(DK) { |
| 218 | #define DECL_CONTEXT(Name) \ |
| 219 | case Decl::Name: \ |
| 220 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 221 | #define DECL_CONTEXT_BASE(Name) |
| 222 | #include "clang/AST/DeclNodes.def" |
| 223 | default: |
| 224 | #define DECL_CONTEXT_BASE(Name) \ |
| 225 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 226 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 227 | #include "clang/AST/DeclNodes.def" |
| 228 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 229 | return 0; |
| 230 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 234 | Decl::Kind DK = D->getKind(); |
| 235 | switch(DK) { |
| 236 | #define DECL_CONTEXT(Name) \ |
| 237 | case Decl::Name: \ |
| 238 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 239 | #define DECL_CONTEXT_BASE(Name) |
| 240 | #include "clang/AST/DeclNodes.def" |
| 241 | default: |
| 242 | #define DECL_CONTEXT_BASE(Name) \ |
| 243 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 244 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 245 | #include "clang/AST/DeclNodes.def" |
| 246 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 247 | return 0; |
| 248 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 251 | //===----------------------------------------------------------------------===// |
| 252 | // DeclContext Implementation |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 255 | bool DeclContext::classof(const Decl *D) { |
| 256 | switch (D->getKind()) { |
| 257 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 258 | #define DECL_CONTEXT_BASE(Name) |
| 259 | #include "clang/AST/DeclNodes.def" |
| 260 | return true; |
| 261 | default: |
| 262 | #define DECL_CONTEXT_BASE(Name) \ |
| 263 | if (D->getKind() >= Decl::Name##First && \ |
| 264 | D->getKind() <= Decl::Name##Last) \ |
| 265 | return true; |
| 266 | #include "clang/AST/DeclNodes.def" |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 271 | /// StoredDeclsList - This is an array of decls optimized a common case of only |
| 272 | /// containing one entry. |
| 273 | struct StoredDeclsList { |
| 274 | /// Data - If the integer is 0, then the pointer is a NamedDecl*. If the |
| 275 | /// integer is 1, then it is a VectorTy; |
| 276 | llvm::PointerIntPair<void*, 1, bool> Data; |
| 277 | |
| 278 | /// VectorTy - When in vector form, this is what the Data pointer points to. |
| 279 | typedef llvm::SmallVector<NamedDecl*, 4> VectorTy; |
| 280 | public: |
| 281 | StoredDeclsList() {} |
| 282 | StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) { |
| 283 | if (isVector()) |
| 284 | Data.setPointer(new VectorTy(getVector())); |
| 285 | } |
| 286 | |
| 287 | ~StoredDeclsList() { |
| 288 | // If this is a vector-form, free the vector. |
| 289 | if (isVector()) |
| 290 | delete &getVector(); |
| 291 | } |
| 292 | |
Chris Lattner | 01011d4 | 2009-02-23 18:17:44 +0000 | [diff] [blame] | 293 | StoredDeclsList &operator=(const StoredDeclsList &RHS) { |
| 294 | if (isVector()) |
| 295 | delete &getVector(); |
| 296 | Data = RHS.Data; |
| 297 | if (isVector()) |
| 298 | Data.setPointer(new VectorTy(getVector())); |
| 299 | return *this; |
| 300 | } |
| 301 | |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 302 | bool isVector() const { return Data.getInt() != 0; } |
| 303 | bool isInline() const { return Data.getInt() == 0; } |
| 304 | bool isNull() const { return Data.getPointer() == 0; } |
| 305 | |
| 306 | void setOnlyValue(NamedDecl *ND) { |
| 307 | assert(isInline() && "Not inline"); |
| 308 | Data.setPointer(ND); |
| 309 | } |
| 310 | |
| 311 | /// getLookupResult - Return an array of all the decls that this list |
| 312 | /// represents. |
| 313 | DeclContext::lookup_result getLookupResult() { |
| 314 | // If we have a single inline unit, return it. |
| 315 | if (isInline()) { |
| 316 | assert(!isNull() && "Empty list isn't allowed"); |
| 317 | |
| 318 | // Data is a raw pointer to a NamedDecl*, return it. |
| 319 | void *Ptr = &Data; |
| 320 | return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1); |
| 321 | } |
| 322 | |
| 323 | // Otherwise, we have a range result. |
| 324 | VectorTy &V = getVector(); |
| 325 | return DeclContext::lookup_result(&V[0], &V[0]+V.size()); |
| 326 | } |
| 327 | |
| 328 | /// HandleRedeclaration - If this is a redeclaration of an existing decl, |
| 329 | /// replace the old one with D and return true. Otherwise return false. |
| 330 | bool HandleRedeclaration(NamedDecl *D) { |
| 331 | // Most decls only have one entry in their list, special case it. |
| 332 | if (isInline()) { |
| 333 | if (!D->declarationReplaces(getInlineValue())) |
| 334 | return false; |
| 335 | setOnlyValue(D); |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | // Determine if this declaration is actually a redeclaration. |
| 340 | VectorTy &Vec = getVector(); |
| 341 | VectorTy::iterator RDI |
| 342 | = std::find_if(Vec.begin(), Vec.end(), |
| 343 | std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces), |
| 344 | D)); |
| 345 | if (RDI == Vec.end()) |
| 346 | return false; |
| 347 | *RDI = D; |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | /// AddSubsequentDecl - This is called on the second and later decl when it is |
| 352 | /// not a redeclaration to merge it into the appropriate place in our list. |
| 353 | /// |
| 354 | void AddSubsequentDecl(NamedDecl *D) { |
| 355 | // If this is the second decl added to the list, convert this to vector |
| 356 | // form. |
| 357 | if (isInline()) { |
| 358 | NamedDecl *OldD = getInlineValue(); |
| 359 | Data.setInt(1); |
| 360 | VectorTy *VT = new VectorTy(); |
| 361 | VT->push_back(OldD); |
| 362 | Data.setPointer(VT); |
| 363 | } |
| 364 | |
| 365 | VectorTy &Vec = getVector(); |
| 366 | if (isa<UsingDirectiveDecl>(D) || |
| 367 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 368 | Vec.push_back(D); |
| 369 | else if (Vec.back()->getIdentifierNamespace() == Decl::IDNS_Tag) { |
| 370 | NamedDecl *TagD = Vec.back(); |
| 371 | Vec.back() = D; |
| 372 | Vec.push_back(TagD); |
| 373 | } else |
| 374 | Vec.push_back(D); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | private: |
| 379 | VectorTy &getVector() const { |
| 380 | assert(isVector() && "Not in vector form"); |
| 381 | return *static_cast<VectorTy*>(Data.getPointer()); |
| 382 | } |
| 383 | |
| 384 | NamedDecl *getInlineValue() const { |
| 385 | assert(isInline() && "Not in inline form"); |
| 386 | return (NamedDecl*)Data.getPointer(); |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | |
| 391 | |
| 392 | typedef llvm::DenseMap<DeclarationName, StoredDeclsList> StoredDeclsMap; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 393 | |
| 394 | DeclContext::~DeclContext() { |
| 395 | unsigned Size = LookupPtr.getInt(); |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 396 | if (Size == LookupIsMap) |
| 397 | delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 398 | else |
| 399 | delete [] static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 403 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
| 404 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 407 | bool DeclContext::isTransparentContext() const { |
| 408 | if (DeclKind == Decl::Enum) |
| 409 | return true; // FIXME: Check for C++0x scoped enums |
| 410 | else if (DeclKind == Decl::LinkageSpec) |
| 411 | return true; |
| 412 | else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 413 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 414 | else if (DeclKind == Decl::Namespace) |
| 415 | return false; // FIXME: Check for C++0x inline namespaces |
| 416 | |
| 417 | return false; |
| 418 | } |
| 419 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 420 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 421 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 422 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 423 | case Decl::LinkageSpec: |
| 424 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 425 | // There is only one DeclContext for these entities. |
| 426 | return this; |
| 427 | |
| 428 | case Decl::Namespace: |
| 429 | // The original namespace is our primary context. |
| 430 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 431 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 432 | case Decl::ObjCMethod: |
| 433 | return this; |
| 434 | |
| 435 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 436 | case Decl::ObjCProtocol: |
| 437 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 438 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 439 | return this; |
| 440 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 441 | case Decl::ObjCImplementation: |
| 442 | case Decl::ObjCCategoryImpl: |
| 443 | return this; |
| 444 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 445 | default: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 446 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 447 | // If this is a tag type that has a definition or is currently |
| 448 | // being defined, that definition is our primary context. |
| 449 | if (TagType *TagT |
| 450 | = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl)) |
| 451 | if (TagT->isBeingDefined() || |
| 452 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 453 | return TagT->getDecl(); |
| 454 | return this; |
| 455 | } |
| 456 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 457 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 458 | "Unknown DeclContext kind"); |
| 459 | return this; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | DeclContext *DeclContext::getNextContext() { |
| 464 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 465 | case Decl::Namespace: |
| 466 | // Return the next namespace |
| 467 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 468 | |
| 469 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | } |
| 473 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 474 | void DeclContext::addDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 475 | assert(D->getLexicalDeclContext() == this && |
| 476 | "Decl inserted into wrong lexical context"); |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 477 | assert(!D->NextDeclInScope && D != LastDecl && |
| 478 | "Decl already inserted into a DeclContext"); |
| 479 | |
| 480 | if (FirstDecl) { |
| 481 | LastDecl->NextDeclInScope = D; |
| 482 | LastDecl = D; |
| 483 | } else { |
| 484 | FirstDecl = LastDecl = D; |
| 485 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 486 | |
| 487 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 488 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 491 | /// buildLookup - Build the lookup data structure with all of the |
| 492 | /// declarations in DCtx (and any other contexts linked to it or |
| 493 | /// transparent contexts nested within it). |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 494 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 495 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 496 | for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); |
| 497 | D != DEnd; ++D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 498 | // Insert this declaration into the lookup structure |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 499 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 500 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 501 | |
| 502 | // If this declaration is itself a transparent declaration context, |
| 503 | // add its members (recursively). |
| 504 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 505 | if (InnerCtx->isTransparentContext()) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 506 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 511 | DeclContext::lookup_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 512 | DeclContext::lookup(DeclarationName Name) { |
| 513 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 514 | if (PrimaryContext != this) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 515 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 517 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 518 | /// all of the linked DeclContexts (in declaration order!) and |
| 519 | /// inserting their values. |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 520 | if (LookupPtr.getPointer() == 0) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 521 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 522 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 523 | if (isLookupMap()) { |
| 524 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 525 | StoredDeclsMap::iterator Pos = Map->find(Name); |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 526 | if (Pos == Map->end()) |
| 527 | return lookup_result(0, 0); |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 528 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // We have a small array. Look into it. |
| 532 | unsigned Size = LookupPtr.getInt(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 533 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 534 | for (unsigned Idx = 0; Idx != Size; ++Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 535 | if (Array[Idx]->getDeclName() == Name) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 536 | unsigned Last = Idx + 1; |
| 537 | while (Last != Size && Array[Last]->getDeclName() == Name) |
| 538 | ++Last; |
| 539 | return lookup_result(&Array[Idx], &Array[Last]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 542 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | DeclContext::lookup_const_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 546 | DeclContext::lookup(DeclarationName Name) const { |
| 547 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 550 | const DeclContext *DeclContext::getLookupContext() const { |
| 551 | const DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 552 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 553 | while (Ctx->isTransparentContext()) |
| 554 | Ctx = Ctx->getParent(); |
| 555 | return Ctx; |
| 556 | } |
| 557 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame^] | 558 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 559 | DeclContext *Ctx = this; |
| 560 | // Skip through non-namespace, non-translation-unit contexts. |
| 561 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 562 | Ctx = Ctx->getParent(); |
| 563 | return Ctx->getPrimaryContext(); |
| 564 | } |
| 565 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 566 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 567 | // FIXME: This feels like a hack. Should DeclarationName support |
| 568 | // template-ids, or is there a better way to keep specializations |
| 569 | // from being visible? |
| 570 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 571 | return; |
| 572 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 573 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 574 | if (PrimaryContext != this) { |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 575 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 576 | return; |
| 577 | } |
| 578 | |
| 579 | // If we already have a lookup data structure, perform the insertion |
| 580 | // into it. Otherwise, be lazy and don't build that structure until |
| 581 | // someone asks for it. |
| 582 | if (LookupPtr.getPointer()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 583 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 585 | // If we are a transparent context, insert into our parent context, |
| 586 | // too. This operation is recursive. |
| 587 | if (isTransparentContext()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 588 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 591 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 592 | // Skip unnamed declarations. |
| 593 | if (!D->getDeclName()) |
| 594 | return; |
| 595 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 596 | // FIXME: This feels like a hack. Should DeclarationName support |
| 597 | // template-ids, or is there a better way to keep specializations |
| 598 | // from being visible? |
| 599 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 600 | return; |
| 601 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 602 | bool MayBeRedeclaration = true; |
| 603 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 604 | if (!isLookupMap()) { |
| 605 | unsigned Size = LookupPtr.getInt(); |
| 606 | |
| 607 | // The lookup data is stored as an array. Search through the array |
| 608 | // to find the insertion location. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 609 | NamedDecl **Array; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 610 | if (Size == 0) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 611 | Array = new NamedDecl*[LookupIsMap - 1]; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 612 | LookupPtr.setPointer(Array); |
| 613 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 614 | Array = static_cast<NamedDecl **>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // We always keep declarations of the same name next to each other |
| 618 | // in the array, so that it is easy to return multiple results |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 619 | // from lookup(). |
| 620 | unsigned FirstMatch; |
| 621 | for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch) |
| 622 | if (Array[FirstMatch]->getDeclName() == D->getDeclName()) |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 623 | break; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 625 | unsigned InsertPos = FirstMatch; |
| 626 | if (FirstMatch != Size) { |
| 627 | // We found another declaration with the same name. First |
| 628 | // determine whether this is a redeclaration of an existing |
| 629 | // declaration in this scope, in which case we will replace the |
| 630 | // existing declaration. |
| 631 | unsigned LastMatch = FirstMatch; |
| 632 | for (; LastMatch != Size; ++LastMatch) { |
| 633 | if (Array[LastMatch]->getDeclName() != D->getDeclName()) |
| 634 | break; |
| 635 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 636 | if (D->declarationReplaces(Array[LastMatch])) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 637 | // D is a redeclaration of an existing element in the |
| 638 | // array. Replace that element with D. |
| 639 | Array[LastMatch] = D; |
| 640 | return; |
| 641 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 644 | // [FirstMatch, LastMatch) contains the set of declarations that |
| 645 | // have the same name as this declaration. Determine where the |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 646 | // declaration D will be inserted into this range. |
| 647 | if (D->getKind() == Decl::UsingDirective || |
| 648 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 649 | InsertPos = LastMatch; |
| 650 | else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 651 | InsertPos = LastMatch - 1; |
| 652 | else |
| 653 | InsertPos = LastMatch; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | if (Size < LookupIsMap - 1) { |
| 657 | // The new declaration will fit in the array. Insert the new |
| 658 | // declaration at the position Match in the array. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 659 | for (unsigned Idx = Size; Idx > InsertPos; --Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 660 | Array[Idx] = Array[Idx-1]; |
| 661 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 662 | Array[InsertPos] = D; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 663 | LookupPtr.setInt(Size + 1); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | // We've reached capacity in this array. Create a map and copy in |
| 668 | // all of the declarations that were stored in the array. |
| 669 | StoredDeclsMap *Map = new StoredDeclsMap(16); |
| 670 | LookupPtr.setPointer(Map); |
| 671 | LookupPtr.setInt(LookupIsMap); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 672 | for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 673 | makeDeclVisibleInContextImpl(Array[Idx]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 674 | delete [] Array; |
| 675 | |
| 676 | // Fall through to perform insertion into the map. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 677 | MayBeRedeclaration = false; |
| 678 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 679 | |
| 680 | // Insert this declaration into the map. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 681 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 682 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 683 | if (DeclNameEntries.isNull()) { |
| 684 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 685 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 686 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 687 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 688 | // If it is possible that this is a redeclaration, check to see if there is |
| 689 | // already a decl for which declarationReplaces returns true. If there is |
| 690 | // one, just replace it and return. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 691 | if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D)) |
| 692 | return; |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 693 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 694 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 695 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 697 | |
| 698 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 699 | /// this context. |
| 700 | DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const { |
| 701 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
| 702 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 703 | reinterpret_cast<udir_iterator>(Result.second)); |
| 704 | } |
| 705 | |