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" |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame^] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/DeclTemplate.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 25 | #include <cstdio> |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 26 | #include <functional> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 27 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Statistics |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 34 | #define DECL(Derived, Base) static int n##Derived##s = 0; |
| 35 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 36 | |
| 37 | static bool StatSwitch = false; |
| 38 | |
| 39 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 40 | // keep them in a hash map instead of wasting space in the Decl class. |
| 41 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 42 | |
| 43 | static DeclAttrMapTy *DeclAttrs = 0; |
| 44 | |
| 45 | const char *Decl::getDeclKindName() const { |
| 46 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 47 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 48 | #define DECL(Derived, Base) case Derived: return #Derived; |
| 49 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 53 | const char *DeclContext::getDeclKindName() const { |
| 54 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 55 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 56 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 57 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 61 | bool Decl::CollectingStats(bool Enable) { |
| 62 | if (Enable) |
| 63 | StatSwitch = true; |
| 64 | return StatSwitch; |
| 65 | } |
| 66 | |
| 67 | void Decl::PrintStats() { |
| 68 | fprintf(stderr, "*** Decl Stats:\n"); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 70 | int totalDecls = 0; |
| 71 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 72 | #include "clang/AST/DeclNodes.def" |
| 73 | fprintf(stderr, " %d decls total.\n", totalDecls); |
| 74 | |
| 75 | int totalBytes = 0; |
| 76 | #define DECL(Derived, Base) \ |
| 77 | if (n##Derived##s > 0) { \ |
| 78 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 79 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 80 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 81 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 82 | } |
| 83 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 84 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 85 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void Decl::addDeclKind(Kind k) { |
| 89 | switch (k) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 90 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 91 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 92 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 97 | // PrettyStackTraceDecl Implementation |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
| 100 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 101 | SourceLocation TheLoc = Loc; |
| 102 | if (TheLoc.isInvalid() && TheDecl) |
| 103 | TheLoc = TheDecl->getLocation(); |
| 104 | |
| 105 | if (TheLoc.isValid()) { |
| 106 | TheLoc.print(OS, SM); |
| 107 | OS << ": "; |
| 108 | } |
| 109 | |
| 110 | OS << Message; |
| 111 | |
| 112 | if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
| 113 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 114 | OS << '\n'; |
| 115 | } |
| 116 | |
| 117 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 118 | // Decl Implementation |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 121 | // Out-of-line virtual method providing a home for Decl. |
| 122 | Decl::~Decl() { |
| 123 | if (isOutOfSemaDC()) |
| 124 | delete getMultipleDC(); |
| 125 | |
| 126 | assert(!HasAttrs && "attributes should have been freed by Destroy"); |
| 127 | } |
| 128 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 129 | void Decl::setDeclContext(DeclContext *DC) { |
| 130 | if (isOutOfSemaDC()) |
| 131 | delete getMultipleDC(); |
| 132 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 133 | DeclCtx = DC; |
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 | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 144 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 145 | } else { |
| 146 | getMultipleDC()->LexicalDC = DC; |
| 147 | } |
| 148 | } |
| 149 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 150 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 151 | switch (DeclKind) { |
| 152 | default: |
| 153 | if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) |
| 154 | return IDNS_Ordinary; |
| 155 | assert(0 && "Unknown decl kind!"); |
| 156 | case OverloadedFunction: |
| 157 | case Typedef: |
| 158 | case EnumConstant: |
| 159 | case Var: |
| 160 | case ImplicitParam: |
| 161 | case ParmVar: |
| 162 | case OriginalParmVar: |
| 163 | case NonTypeTemplateParm: |
| 164 | case ObjCMethod: |
| 165 | case ObjCContainer: |
| 166 | case ObjCCategory: |
| 167 | case ObjCInterface: |
| 168 | case ObjCCategoryImpl: |
| 169 | case ObjCProperty: |
| 170 | case ObjCCompatibleAlias: |
| 171 | return IDNS_Ordinary; |
| 172 | |
| 173 | case ObjCProtocol: |
| 174 | return IDNS_Protocol; |
| 175 | |
| 176 | case Field: |
| 177 | case ObjCAtDefsField: |
| 178 | case ObjCIvar: |
| 179 | return IDNS_Member; |
| 180 | |
| 181 | case Record: |
| 182 | case CXXRecord: |
| 183 | case Enum: |
| 184 | case TemplateTypeParm: |
| 185 | return IDNS_Tag; |
| 186 | |
| 187 | case Namespace: |
| 188 | case Template: |
| 189 | case FunctionTemplate: |
| 190 | case ClassTemplate: |
| 191 | case TemplateTemplateParm: |
Anders Carlsson | faf0e87 | 2009-03-28 23:02:53 +0000 | [diff] [blame] | 192 | case NamespaceAlias: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 285 | // Observe the unrolled recursion. By setting N->NextDeclInContext = 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 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 289 | Decl* N = getNextDeclInContext(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 290 | |
| 291 | while (N) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 292 | Decl* Tmp = N->getNextDeclInContext(); |
| 293 | N->NextDeclInContext = 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 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 367 | DeclContext::~DeclContext() { |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 368 | if (isLookupMap()) |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 369 | delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 370 | else |
| 371 | delete [] static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 375 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
| 376 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 379 | bool DeclContext::isTransparentContext() const { |
| 380 | if (DeclKind == Decl::Enum) |
| 381 | return true; // FIXME: Check for C++0x scoped enums |
| 382 | else if (DeclKind == Decl::LinkageSpec) |
| 383 | return true; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 384 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 385 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 386 | else if (DeclKind == Decl::Namespace) |
| 387 | return false; // FIXME: Check for C++0x inline namespaces |
| 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 392 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 393 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 394 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 395 | case Decl::LinkageSpec: |
| 396 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 397 | // There is only one DeclContext for these entities. |
| 398 | return this; |
| 399 | |
| 400 | case Decl::Namespace: |
| 401 | // The original namespace is our primary context. |
| 402 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 403 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 404 | case Decl::ObjCMethod: |
| 405 | return this; |
| 406 | |
| 407 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 408 | case Decl::ObjCProtocol: |
| 409 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 410 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 411 | return this; |
| 412 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 413 | case Decl::ObjCImplementation: |
| 414 | case Decl::ObjCCategoryImpl: |
| 415 | return this; |
| 416 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 417 | default: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 418 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 419 | // If this is a tag type that has a definition or is currently |
| 420 | // being defined, that definition is our primary context. |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 421 | if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType()) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 422 | if (TagT->isBeingDefined() || |
| 423 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 424 | return TagT->getDecl(); |
| 425 | return this; |
| 426 | } |
| 427 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 428 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 429 | "Unknown DeclContext kind"); |
| 430 | return this; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | DeclContext *DeclContext::getNextContext() { |
| 435 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 436 | case Decl::Namespace: |
| 437 | // Return the next namespace |
| 438 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 439 | |
| 440 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | } |
| 444 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 445 | void DeclContext::addDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 446 | assert(D->getLexicalDeclContext() == this && |
| 447 | "Decl inserted into wrong lexical context"); |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 448 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 449 | "Decl already inserted into a DeclContext"); |
| 450 | |
| 451 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 452 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 453 | LastDecl = D; |
| 454 | } else { |
| 455 | FirstDecl = LastDecl = D; |
| 456 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 457 | |
| 458 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 459 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 462 | /// buildLookup - Build the lookup data structure with all of the |
| 463 | /// declarations in DCtx (and any other contexts linked to it or |
| 464 | /// transparent contexts nested within it). |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 465 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 466 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 467 | for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); |
| 468 | D != DEnd; ++D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 469 | // Insert this declaration into the lookup structure |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 470 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 471 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 472 | |
| 473 | // If this declaration is itself a transparent declaration context, |
| 474 | // add its members (recursively). |
| 475 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 476 | if (InnerCtx->isTransparentContext()) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 477 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 482 | DeclContext::lookup_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 483 | DeclContext::lookup(DeclarationName Name) { |
| 484 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 485 | if (PrimaryContext != this) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 486 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 488 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 489 | /// all of the linked DeclContexts (in declaration order!) and |
| 490 | /// inserting their values. |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 491 | if (LookupPtr.getPointer() == 0) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 492 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 494 | if (isLookupMap()) { |
| 495 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 496 | StoredDeclsMap::iterator Pos = Map->find(Name); |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 497 | if (Pos == Map->end()) |
| 498 | return lookup_result(0, 0); |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 499 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // We have a small array. Look into it. |
| 503 | unsigned Size = LookupPtr.getInt(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 504 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 505 | for (unsigned Idx = 0; Idx != Size; ++Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 506 | if (Array[Idx]->getDeclName() == Name) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 507 | unsigned Last = Idx + 1; |
| 508 | while (Last != Size && Array[Last]->getDeclName() == Name) |
| 509 | ++Last; |
| 510 | return lookup_result(&Array[Idx], &Array[Last]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 513 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | DeclContext::lookup_const_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 517 | DeclContext::lookup(DeclarationName Name) const { |
| 518 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 521 | DeclContext *DeclContext::getLookupContext() { |
| 522 | DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 523 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 524 | while (Ctx->isTransparentContext()) |
| 525 | Ctx = Ctx->getParent(); |
| 526 | return Ctx; |
| 527 | } |
| 528 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 529 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 530 | DeclContext *Ctx = this; |
| 531 | // Skip through non-namespace, non-translation-unit contexts. |
| 532 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 533 | Ctx = Ctx->getParent(); |
| 534 | return Ctx->getPrimaryContext(); |
| 535 | } |
| 536 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 537 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 538 | // FIXME: This feels like a hack. Should DeclarationName support |
| 539 | // template-ids, or is there a better way to keep specializations |
| 540 | // from being visible? |
| 541 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 542 | return; |
| 543 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 544 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 545 | if (PrimaryContext != this) { |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 546 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | |
| 550 | // If we already have a lookup data structure, perform the insertion |
| 551 | // into it. Otherwise, be lazy and don't build that structure until |
| 552 | // someone asks for it. |
| 553 | if (LookupPtr.getPointer()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 554 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 556 | // If we are a transparent context, insert into our parent context, |
| 557 | // too. This operation is recursive. |
| 558 | if (isTransparentContext()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 559 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 562 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 563 | // Skip unnamed declarations. |
| 564 | if (!D->getDeclName()) |
| 565 | return; |
| 566 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 567 | // FIXME: This feels like a hack. Should DeclarationName support |
| 568 | // template-ids, or is there a better way to keep specializations |
| 569 | // from being visible? |
| 570 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 571 | return; |
| 572 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 573 | bool MayBeRedeclaration = true; |
| 574 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 575 | if (!isLookupMap()) { |
| 576 | unsigned Size = LookupPtr.getInt(); |
| 577 | |
| 578 | // The lookup data is stored as an array. Search through the array |
| 579 | // to find the insertion location. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 580 | NamedDecl **Array; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 581 | if (Size == 0) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 582 | Array = new NamedDecl*[LookupIsMap - 1]; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 583 | LookupPtr.setPointer(Array); |
| 584 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 585 | Array = static_cast<NamedDecl **>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // We always keep declarations of the same name next to each other |
| 589 | // in the array, so that it is easy to return multiple results |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 590 | // from lookup(). |
| 591 | unsigned FirstMatch; |
| 592 | for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch) |
| 593 | if (Array[FirstMatch]->getDeclName() == D->getDeclName()) |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 594 | break; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 596 | unsigned InsertPos = FirstMatch; |
| 597 | if (FirstMatch != Size) { |
| 598 | // We found another declaration with the same name. First |
| 599 | // determine whether this is a redeclaration of an existing |
| 600 | // declaration in this scope, in which case we will replace the |
| 601 | // existing declaration. |
| 602 | unsigned LastMatch = FirstMatch; |
| 603 | for (; LastMatch != Size; ++LastMatch) { |
| 604 | if (Array[LastMatch]->getDeclName() != D->getDeclName()) |
| 605 | break; |
| 606 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 607 | if (D->declarationReplaces(Array[LastMatch])) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 608 | // D is a redeclaration of an existing element in the |
| 609 | // array. Replace that element with D. |
| 610 | Array[LastMatch] = D; |
| 611 | return; |
| 612 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 615 | // [FirstMatch, LastMatch) contains the set of declarations that |
| 616 | // have the same name as this declaration. Determine where the |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 617 | // declaration D will be inserted into this range. |
| 618 | if (D->getKind() == Decl::UsingDirective || |
| 619 | D->getIdentifierNamespace() == Decl::IDNS_Tag) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 620 | InsertPos = LastMatch; |
| 621 | else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 622 | InsertPos = LastMatch - 1; |
| 623 | else |
| 624 | InsertPos = LastMatch; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | if (Size < LookupIsMap - 1) { |
| 628 | // The new declaration will fit in the array. Insert the new |
| 629 | // declaration at the position Match in the array. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 630 | for (unsigned Idx = Size; Idx > InsertPos; --Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 631 | Array[Idx] = Array[Idx-1]; |
| 632 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 633 | Array[InsertPos] = D; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 634 | LookupPtr.setInt(Size + 1); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | // We've reached capacity in this array. Create a map and copy in |
| 639 | // all of the declarations that were stored in the array. |
| 640 | StoredDeclsMap *Map = new StoredDeclsMap(16); |
| 641 | LookupPtr.setPointer(Map); |
| 642 | LookupPtr.setInt(LookupIsMap); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 643 | for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 644 | makeDeclVisibleInContextImpl(Array[Idx]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 645 | delete [] Array; |
| 646 | |
| 647 | // Fall through to perform insertion into the map. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 648 | MayBeRedeclaration = false; |
| 649 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 650 | |
| 651 | // Insert this declaration into the map. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 652 | StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 653 | StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; |
| 654 | if (DeclNameEntries.isNull()) { |
| 655 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 656 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 657 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 658 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 659 | // If it is possible that this is a redeclaration, check to see if there is |
| 660 | // already a decl for which declarationReplaces returns true. If there is |
| 661 | // one, just replace it and return. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 662 | if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D)) |
| 663 | return; |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 664 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 665 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 666 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 667 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 668 | |
| 669 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 670 | /// this context. |
| 671 | DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const { |
| 672 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
| 673 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 674 | reinterpret_cast<udir_iterator>(Result.second)); |
| 675 | } |
| 676 | |