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