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