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