Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1 | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// |
| 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 name lookup for C, C++, Objective-C, and |
| 11 | // Objective-C++. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "Sema.h" |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "clang/Basic/LangOptions.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 25 | #include <set> |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <iterator> |
| 28 | #include <utility> |
| 29 | #include <algorithm> |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 33 | typedef llvm::SmallVector<UsingDirectiveDecl*, 4> UsingDirectivesTy; |
| 34 | typedef llvm::DenseSet<NamespaceDecl*> NamespaceSet; |
| 35 | typedef llvm::SmallVector<Sema::LookupResult, 3> LookupResultsTy; |
| 36 | |
| 37 | /// UsingDirAncestorCompare - Implements strict weak ordering of |
| 38 | /// UsingDirectives. It orders them by address of its common ancestor. |
| 39 | struct UsingDirAncestorCompare { |
| 40 | |
| 41 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 42 | bool operator () (UsingDirectiveDecl *U, const DeclContext *Ctx) const { |
| 43 | return U->getCommonAncestor() < Ctx; |
| 44 | } |
| 45 | |
| 46 | /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext. |
| 47 | bool operator () (const DeclContext *Ctx, UsingDirectiveDecl *U) const { |
| 48 | return Ctx < U->getCommonAncestor(); |
| 49 | } |
| 50 | |
| 51 | /// @brief Compares UsingDirectiveDecl common ancestors. |
| 52 | bool operator () (UsingDirectiveDecl *U1, UsingDirectiveDecl *U2) const { |
| 53 | return U1->getCommonAncestor() < U2->getCommonAncestor(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | /// AddNamespaceUsingDirectives - Adds all UsingDirectiveDecl's to heap UDirs |
| 58 | /// (ordered by common ancestors), found in namespace NS, |
| 59 | /// including all found (recursively) in their nominated namespaces. |
| 60 | void AddNamespaceUsingDirectives(DeclContext *NS, |
| 61 | UsingDirectivesTy &UDirs, |
| 62 | NamespaceSet &Visited) { |
| 63 | DeclContext::udir_iterator I, End; |
| 64 | |
| 65 | for (llvm::tie(I, End) = NS->getUsingDirectives(); I !=End; ++I) { |
| 66 | UDirs.push_back(*I); |
| 67 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 68 | NamespaceDecl *Nominated = (*I)->getNominatedNamespace(); |
| 69 | if (Visited.insert(Nominated).second) |
| 70 | AddNamespaceUsingDirectives(Nominated, UDirs, /*ref*/ Visited); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// AddScopeUsingDirectives - Adds all UsingDirectiveDecl's found in Scope S, |
| 75 | /// including all found in the namespaces they nominate. |
| 76 | static void AddScopeUsingDirectives(Scope *S, UsingDirectivesTy &UDirs) { |
| 77 | NamespaceSet VisitedNS; |
| 78 | |
| 79 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) { |
| 80 | |
| 81 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 82 | VisitedNS.insert(NS); |
| 83 | |
| 84 | AddNamespaceUsingDirectives(Ctx, UDirs, /*ref*/ VisitedNS); |
| 85 | |
| 86 | } else { |
| 87 | Scope::udir_iterator |
| 88 | I = S->using_directives_begin(), |
| 89 | End = S->using_directives_end(); |
| 90 | |
| 91 | for (; I != End; ++I) { |
| 92 | UsingDirectiveDecl * UD = static_cast<UsingDirectiveDecl*>(*I); |
| 93 | UDirs.push_back(UD); |
| 94 | std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare()); |
| 95 | |
| 96 | NamespaceDecl *Nominated = UD->getNominatedNamespace(); |
| 97 | if (!VisitedNS.count(Nominated)) { |
| 98 | VisitedNS.insert(Nominated); |
| 99 | AddNamespaceUsingDirectives(Nominated, UDirs, /*ref*/ VisitedNS); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 105 | /// MaybeConstructOverloadSet - Name lookup has determined that the |
| 106 | /// elements in [I, IEnd) have the name that we are looking for, and |
| 107 | /// *I is a match for the namespace. This routine returns an |
| 108 | /// appropriate Decl for name lookup, which may either be *I or an |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 109 | /// OverloadedFunctionDecl that represents the overloaded functions in |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 110 | /// [I, IEnd). |
| 111 | /// |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 112 | /// The existance of this routine is temporary; users of LookupResult |
| 113 | /// should be able to handle multiple results, to deal with cases of |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 114 | /// ambiguity and overloaded functions without needing to create a |
| 115 | /// Decl node. |
| 116 | template<typename DeclIterator> |
| 117 | static Decl * |
| 118 | MaybeConstructOverloadSet(ASTContext &Context, |
| 119 | DeclIterator I, DeclIterator IEnd) { |
| 120 | assert(I != IEnd && "Iterator range cannot be empty"); |
| 121 | assert(!isa<OverloadedFunctionDecl>(*I) && |
| 122 | "Cannot have an overloaded function"); |
| 123 | |
| 124 | if (isa<FunctionDecl>(*I)) { |
| 125 | // If we found a function, there might be more functions. If |
| 126 | // so, collect them into an overload set. |
| 127 | DeclIterator Last = I; |
| 128 | OverloadedFunctionDecl *Ovl = 0; |
| 129 | for (++Last; Last != IEnd && isa<FunctionDecl>(*Last); ++Last) { |
| 130 | if (!Ovl) { |
| 131 | // FIXME: We leak this overload set. Eventually, we want to |
| 132 | // stop building the declarations for these overload sets, so |
| 133 | // there will be nothing to leak. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 134 | Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(), |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 135 | (*I)->getDeclName()); |
| 136 | Ovl->addOverload(cast<FunctionDecl>(*I)); |
| 137 | } |
| 138 | Ovl->addOverload(cast<FunctionDecl>(*Last)); |
| 139 | } |
| 140 | |
| 141 | // If we had more than one function, we built an overload |
| 142 | // set. Return it. |
| 143 | if (Ovl) |
| 144 | return Ovl; |
| 145 | } |
| 146 | |
| 147 | return *I; |
| 148 | } |
| 149 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 150 | /// Merges together multiple LookupResults dealing with duplicated Decl's. |
| 151 | static Sema::LookupResult |
| 152 | MergeLookupResults(ASTContext &Context, LookupResultsTy &Results) { |
| 153 | typedef Sema::LookupResult LResult; |
| 154 | typedef llvm::SmallVector<NamedDecl*, 3> DeclsVecTy; |
| 155 | |
| 156 | DeclsVecTy FoundDecls; |
| 157 | OverloadedFunctionDecl *FoundOverloaded = 0; |
| 158 | |
| 159 | LookupResultsTy::iterator I = Results.begin(), End = Results.end(); |
| 160 | for (; I != End; ++I) { |
| 161 | |
| 162 | switch (I->getKind()) { |
| 163 | case LResult::NotFound: |
| 164 | assert(false && |
| 165 | "Should be always successful name lookup result here."); |
| 166 | break; |
| 167 | |
| 168 | case LResult::AmbiguousReference: |
| 169 | assert(false && |
| 170 | "Shouldn't get ambiguous reference here."); |
| 171 | break; |
| 172 | |
| 173 | case LResult::Found: |
| 174 | FoundDecls.push_back(cast<NamedDecl>(I->getAsDecl())); |
| 175 | break; |
| 176 | |
| 177 | case LResult::AmbiguousBaseSubobjectTypes: |
| 178 | case LResult::AmbiguousBaseSubobjects: { |
| 179 | LResult Ambig = *I; |
| 180 | // Free rest of OverloadedFunctionDecl, and other BasePaths |
| 181 | // stored in LookupResults. |
| 182 | #if 0 |
| 183 | // FIXME: |
| 184 | // It should not happen, when we look start in each DeclContext only once. |
| 185 | // See FIXME in CppLookupName. |
| 186 | assert(Results.size() == 1 && "Multiple LookupResults should be not case " |
| 187 | "here, since using-directives can't occur at class scope."); |
| 188 | #else |
| 189 | if (FoundOverloaded) |
| 190 | FoundOverloaded->Destroy(Context); |
| 191 | |
| 192 | for (++I; I != End; ++I) { |
| 193 | if (I->getKind() == LResult::FoundOverloaded) |
| 194 | cast<OverloadedFunctionDecl>(I->getAsDecl())->Destroy(Context); |
| 195 | else if (BasePaths *Paths = I->getBasePaths()) |
| 196 | delete Paths; |
| 197 | assert(I->getKind() != LResult::AmbiguousReference && |
| 198 | "Shouldn't get ambiguous reference here."); |
| 199 | } |
| 200 | #endif |
| 201 | return Ambig; |
| 202 | } |
| 203 | |
| 204 | case LResult::FoundOverloaded: |
| 205 | if (FoundOverloaded) { |
| 206 | // We have one spare OverloadedFunctionDecl already, so we store |
| 207 | // its function decls. |
| 208 | #if 0 |
| 209 | FIXME: As soon as, stop call MaybeConstructOverloadSet here, |
| 210 | which requires iterator::reference of type NamedDecl*, FoundDecls |
| 211 | can be SmallVector<Decl*, > |
| 212 | or when Lookup*Name() starts return NamedDecl's* instead of Decl's |
| 213 | |
| 214 | this will work: |
| 215 | std::copy(I->begin(), I->end(), std::back_inserter(FoundDecls)); |
| 216 | #else |
| 217 | Sema::LookupResult::iterator OI = I->begin(), OEnd = I->end(); |
| 218 | for (; OI != OEnd; ++OI) |
| 219 | FoundDecls.push_back(cast<NamedDecl>(*OI)); |
| 220 | #endif |
| 221 | } else { |
| 222 | // First time we found OverloadedFunctionDecl, we want to conserve |
| 223 | // it, and possibly add other found Decls later. |
| 224 | FoundOverloaded = cast<OverloadedFunctionDecl>(I->getAsDecl()); |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Remove duplicated Decl pointing at same Decl, this might be case |
| 231 | // for code like: |
| 232 | // |
| 233 | // namespace A { int i; } |
| 234 | // namespace B { using namespace A; } |
| 235 | // namespace C { using namespace A; } |
| 236 | // |
| 237 | // void foo() { |
| 238 | // using namespace B; |
| 239 | // using namespace C; |
| 240 | // ++i; // finds A::i, from both namespace B and C at global scope |
| 241 | // } |
| 242 | // |
| 243 | // C++ [namespace.qual].p3: |
| 244 | // The same declaration found more than once is not an ambiguity |
| 245 | // (because it is still a unique declaration). |
| 246 | // |
| 247 | // FIXME: At this point happens too, because we are doing redundant lookups. |
| 248 | // |
| 249 | DeclsVecTy::iterator DI = FoundDecls.begin(), DEnd = FoundDecls.end(); |
| 250 | // FIXME: Using SmallPtrSet instead would be better, once OverloadedFunctionDecl |
| 251 | // dies. |
| 252 | std::sort(DI, DEnd); |
| 253 | DEnd = std::unique(DI, DEnd); |
| 254 | |
| 255 | if (FoundOverloaded) { |
| 256 | // We found overloaded functions result. We want to add any other |
| 257 | // found decls, that are not already in FoundOverloaded, and are functions |
| 258 | // or methods. |
| 259 | OverloadedFunctionDecl::function_iterator |
| 260 | FBegin = FoundOverloaded->function_begin(), |
| 261 | FEnd = FoundOverloaded->function_end(); |
| 262 | // FIXME: This is O(n^2)! |
Chris Lattner | b02f5f2 | 2009-02-03 21:29:32 +0000 | [diff] [blame] | 263 | for (; DI < DEnd; ++DI) { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 264 | FunctionDecl *Fun = dyn_cast<FunctionDecl>(*DI); |
| 265 | if (Fun && (std::find(FBegin, FEnd, Fun) != FEnd)) { /* Skip.*/ } |
| 266 | else DEnd = std::remove(DI, DEnd, *DI); |
| 267 | } |
| 268 | |
| 269 | for (DI = FoundDecls.begin(); DI != DEnd; ++DI) |
| 270 | FoundOverloaded->addOverload(cast<FunctionDecl>(*DI)); |
| 271 | |
| 272 | return LResult::CreateLookupResult(Context, FoundOverloaded); |
| 273 | } else if (std::size_t FoundLen = std::distance(FoundDecls.begin(), DEnd)) { |
| 274 | // We might found multiple TagDecls pointing at same definition. |
| 275 | if (TagDecl *R = dyn_cast<TagDecl>(*FoundDecls.begin())) { |
| 276 | TagDecl *Canonical = Context.getCanonicalDecl(R); |
| 277 | DeclsVecTy::iterator RI = FoundDecls.begin(), REnd = DEnd; |
| 278 | for (;;) { |
| 279 | ++RI; |
| 280 | if (RI == REnd) { |
| 281 | FoundLen = 1; |
| 282 | break; |
| 283 | } |
| 284 | R = dyn_cast<TagDecl>(*RI); |
| 285 | if (R && Canonical == Context.getCanonicalDecl(R)) { /* Skip */} |
| 286 | else break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // We might find FunctionDecls in two (or more) distinct DeclContexts. |
| 291 | // 3.4.1.p1 ... Name lookup may associate more than one declaration with |
| 292 | // a name if it finds the name to be a function name; the declarations |
| 293 | // are said to form a set of overloaded functions (13.1). |
| 294 | // Overload resolution (13.3) takes place after name lookup has succeeded. |
| 295 | |
| 296 | Decl *D = MaybeConstructOverloadSet(Context, DI, DEnd); |
| 297 | if ((FoundLen == 1) || isa<OverloadedFunctionDecl>(D)) |
| 298 | return LResult::CreateLookupResult(Context, D); |
| 299 | |
| 300 | // Found multiple Decls, it is ambiguous reference. |
| 301 | return LResult::CreateLookupResult(Context, FoundDecls.begin(), FoundLen); |
| 302 | } |
| 303 | |
| 304 | LResult Result = LResult::CreateLookupResult(Context, 0); |
| 305 | return Result; |
| 306 | } |
| 307 | |
| 308 | // Retrieve the set of identifier namespaces that correspond to a |
| 309 | // specific kind of name lookup. |
| 310 | inline unsigned |
| 311 | getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind, |
| 312 | bool CPlusPlus) { |
| 313 | unsigned IDNS = 0; |
| 314 | switch (NameKind) { |
| 315 | case Sema::LookupOrdinaryName: |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame^] | 316 | case Sema::LookupOperatorName: |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 317 | IDNS = Decl::IDNS_Ordinary; |
| 318 | if (CPlusPlus) |
| 319 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
| 320 | break; |
| 321 | |
| 322 | case Sema::LookupTagName: |
| 323 | IDNS = Decl::IDNS_Tag; |
| 324 | break; |
| 325 | |
| 326 | case Sema::LookupMemberName: |
| 327 | IDNS = Decl::IDNS_Member; |
| 328 | if (CPlusPlus) |
| 329 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
| 330 | break; |
| 331 | |
| 332 | case Sema::LookupNestedNameSpecifierName: |
| 333 | case Sema::LookupNamespaceName: |
| 334 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 335 | break; |
| 336 | } |
| 337 | return IDNS; |
| 338 | } |
| 339 | |
| 340 | // Return qualified name for Decl D, like A::B::i, for i being member of |
| 341 | // namespace A::B. |
| 342 | std::string |
| 343 | getQualifiedName(Decl *D) { |
| 344 | std::vector<std::string> Names; |
| 345 | std::string QualName; |
| 346 | DeclContext *Ctx = D->getDeclContext(); |
| 347 | |
| 348 | if (Ctx->isFunctionOrMethod()) |
| 349 | return cast<NamedDecl>(D)->getNameAsString(); |
| 350 | |
| 351 | while (Ctx) { |
| 352 | if (Ctx->isFunctionOrMethod()) { |
| 353 | // FIXME: That probably is happend, because D was member of local |
| 354 | // scope class/struct/union. How do we handle this case? |
| 355 | break; |
| 356 | } |
| 357 | if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) { |
| 358 | Names.push_back(ND->getNameAsString()); |
| 359 | } else { |
| 360 | break; |
| 361 | } |
| 362 | Ctx = Ctx->getParent(); |
| 363 | } |
| 364 | for (std::vector<std::string>::reverse_iterator I= Names.rbegin(), End =Names.rend(); I!=End; ++I) |
| 365 | QualName += *I + "::"; |
| 366 | QualName += cast<NamedDecl>(D)->getNameAsString(); |
| 367 | return QualName; |
| 368 | } |
| 369 | |
| 370 | Sema::LookupResult |
| 371 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, Decl *D) { |
| 372 | LookupResult Result; |
| 373 | Result.StoredKind = (D && isa<OverloadedFunctionDecl>(D))? |
| 374 | OverloadedDeclSingleDecl : SingleDecl; |
| 375 | Result.First = reinterpret_cast<uintptr_t>(D); |
| 376 | Result.Last = 0; |
| 377 | Result.Context = &Context; |
| 378 | return Result; |
| 379 | } |
| 380 | |
Douglas Gregor | 6beddfe | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 381 | /// @brief Moves the name-lookup results from Other to this LookupResult. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 382 | Sema::LookupResult |
| 383 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 384 | IdentifierResolver::iterator F, |
| 385 | IdentifierResolver::iterator L) { |
| 386 | LookupResult Result; |
| 387 | Result.Context = &Context; |
| 388 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 389 | if (F != L && isa<FunctionDecl>(*F)) { |
| 390 | IdentifierResolver::iterator Next = F; |
| 391 | ++Next; |
| 392 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 393 | Result.StoredKind = OverloadedDeclFromIdResolver; |
| 394 | Result.First = F.getAsOpaqueValue(); |
| 395 | Result.Last = L.getAsOpaqueValue(); |
| 396 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 400 | Result.StoredKind = SingleDecl; |
| 401 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 402 | Result.Last = 0; |
| 403 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 406 | Sema::LookupResult |
| 407 | Sema::LookupResult::CreateLookupResult(ASTContext &Context, |
| 408 | DeclContext::lookup_iterator F, |
| 409 | DeclContext::lookup_iterator L) { |
| 410 | LookupResult Result; |
| 411 | Result.Context = &Context; |
| 412 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 413 | if (F != L && isa<FunctionDecl>(*F)) { |
| 414 | DeclContext::lookup_iterator Next = F; |
| 415 | ++Next; |
| 416 | if (Next != L && isa<FunctionDecl>(*Next)) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 417 | Result.StoredKind = OverloadedDeclFromDeclContext; |
| 418 | Result.First = reinterpret_cast<uintptr_t>(F); |
| 419 | Result.Last = reinterpret_cast<uintptr_t>(L); |
| 420 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 424 | Result.StoredKind = SingleDecl; |
| 425 | Result.First = reinterpret_cast<uintptr_t>(*F); |
| 426 | Result.Last = 0; |
| 427 | return Result; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 430 | /// @brief Determine the result of name lookup. |
| 431 | Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const { |
| 432 | switch (StoredKind) { |
| 433 | case SingleDecl: |
| 434 | return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound; |
| 435 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 436 | case OverloadedDeclSingleDecl: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 437 | case OverloadedDeclFromIdResolver: |
| 438 | case OverloadedDeclFromDeclContext: |
| 439 | return FoundOverloaded; |
| 440 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 441 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 442 | return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 443 | |
| 444 | case AmbiguousLookupStoresDecls: |
| 445 | return AmbiguousReference; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 448 | // We can't ever get here. |
| 449 | return NotFound; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// @brief Converts the result of name lookup into a single (possible |
| 453 | /// NULL) pointer to a declaration. |
| 454 | /// |
| 455 | /// The resulting declaration will either be the declaration we found |
| 456 | /// (if only a single declaration was found), an |
| 457 | /// OverloadedFunctionDecl (if an overloaded function was found), or |
| 458 | /// NULL (if no declaration was found). This conversion must not be |
| 459 | /// used anywhere where name lookup could result in an ambiguity. |
| 460 | /// |
| 461 | /// The OverloadedFunctionDecl conversion is meant as a stop-gap |
| 462 | /// solution, since it causes the OverloadedFunctionDecl to be |
| 463 | /// leaked. FIXME: Eventually, there will be a better way to iterate |
| 464 | /// over the set of overloaded functions returned by name lookup. |
| 465 | Decl *Sema::LookupResult::getAsDecl() const { |
| 466 | switch (StoredKind) { |
| 467 | case SingleDecl: |
| 468 | return reinterpret_cast<Decl *>(First); |
| 469 | |
| 470 | case OverloadedDeclFromIdResolver: |
| 471 | return MaybeConstructOverloadSet(*Context, |
| 472 | IdentifierResolver::iterator::getFromOpaqueValue(First), |
| 473 | IdentifierResolver::iterator::getFromOpaqueValue(Last)); |
| 474 | |
| 475 | case OverloadedDeclFromDeclContext: |
| 476 | return MaybeConstructOverloadSet(*Context, |
| 477 | reinterpret_cast<DeclContext::lookup_iterator>(First), |
| 478 | reinterpret_cast<DeclContext::lookup_iterator>(Last)); |
| 479 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 480 | case OverloadedDeclSingleDecl: |
| 481 | return reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 482 | |
| 483 | case AmbiguousLookupStoresDecls: |
| 484 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 485 | assert(false && |
| 486 | "Name lookup returned an ambiguity that could not be handled"); |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 493 | /// @brief Retrieves the BasePaths structure describing an ambiguous |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 494 | /// name lookup, or null. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 495 | BasePaths *Sema::LookupResult::getBasePaths() const { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 496 | if (StoredKind == AmbiguousLookupStoresBasePaths) |
| 497 | return reinterpret_cast<BasePaths *>(First); |
| 498 | return 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 501 | Sema::LookupResult::iterator::reference |
| 502 | Sema::LookupResult::iterator::operator*() const { |
| 503 | switch (Result->StoredKind) { |
| 504 | case SingleDecl: |
| 505 | return reinterpret_cast<Decl*>(Current); |
| 506 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 507 | case OverloadedDeclSingleDecl: |
| 508 | return *reinterpret_cast<Decl**>(Current); |
| 509 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 510 | case OverloadedDeclFromIdResolver: |
| 511 | return *IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 512 | |
| 513 | case OverloadedDeclFromDeclContext: |
| 514 | return *reinterpret_cast<DeclContext::lookup_iterator>(Current); |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 515 | |
| 516 | case AmbiguousLookupStoresDecls: |
| 517 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 518 | assert(false && "Cannot look into ambiguous lookup results"); |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | Sema::LookupResult::iterator& Sema::LookupResult::iterator::operator++() { |
| 526 | switch (Result->StoredKind) { |
| 527 | case SingleDecl: |
| 528 | Current = reinterpret_cast<uintptr_t>((Decl*)0); |
| 529 | break; |
| 530 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 531 | case OverloadedDeclSingleDecl: { |
| 532 | Decl ** I = reinterpret_cast<Decl**>(Current); |
| 533 | ++I; |
| 534 | Current = reinterpret_cast<uintptr_t>(I); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame^] | 535 | break; |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 538 | case OverloadedDeclFromIdResolver: { |
| 539 | IdentifierResolver::iterator I |
| 540 | = IdentifierResolver::iterator::getFromOpaqueValue(Current); |
| 541 | ++I; |
| 542 | Current = I.getAsOpaqueValue(); |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | case OverloadedDeclFromDeclContext: { |
| 547 | DeclContext::lookup_iterator I |
| 548 | = reinterpret_cast<DeclContext::lookup_iterator>(Current); |
| 549 | ++I; |
| 550 | Current = reinterpret_cast<uintptr_t>(I); |
| 551 | break; |
| 552 | } |
| 553 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 554 | case AmbiguousLookupStoresDecls: |
| 555 | case AmbiguousLookupStoresBasePaths: |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 556 | assert(false && "Cannot look into ambiguous lookup results"); |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | return *this; |
| 561 | } |
| 562 | |
| 563 | Sema::LookupResult::iterator Sema::LookupResult::begin() { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 564 | assert(!isAmbiguous() && "Lookup into an ambiguous result"); |
| 565 | if (StoredKind != OverloadedDeclSingleDecl) |
| 566 | return iterator(this, First); |
| 567 | OverloadedFunctionDecl * Ovl = |
| 568 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 569 | return iterator(this, reinterpret_cast<uintptr_t>(&(*Ovl->function_begin()))); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | Sema::LookupResult::iterator Sema::LookupResult::end() { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 573 | assert(!isAmbiguous() && "Lookup into an ambiguous result"); |
| 574 | if (StoredKind != OverloadedDeclSingleDecl) |
| 575 | return iterator(this, Last); |
| 576 | OverloadedFunctionDecl * Ovl = |
| 577 | reinterpret_cast<OverloadedFunctionDecl*>(First); |
| 578 | return iterator(this, reinterpret_cast<uintptr_t>(&(*Ovl->function_end()))); |
Douglas Gregor | bd4b085 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 581 | static bool isFunctionLocalScope(Scope *S) { |
| 582 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
| 583 | return Ctx->isFunctionOrMethod(); |
| 584 | return true; |
| 585 | } |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 586 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 587 | std::pair<bool, Sema::LookupResult> |
| 588 | Sema::CppLookupName(Scope *S, DeclarationName Name, |
| 589 | LookupNameKind NameKind, bool RedeclarationOnly) { |
| 590 | assert(getLangOptions().CPlusPlus && |
| 591 | "Can perform only C++ lookup"); |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame^] | 592 | unsigned IDNS |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 593 | = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true); |
| 594 | Scope *Initial = S; |
| 595 | IdentifierResolver::iterator |
| 596 | I = IdResolver.begin(Name), |
| 597 | IEnd = IdResolver.end(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 599 | // First we lookup local scope. |
| 600 | // We don't consider using-dirctives, as per 7.3.4.p1 [namespace.udir] |
| 601 | // ...During unqualified name lookup (3.4.1), the names appear as if |
| 602 | // they were declared in the nearest enclosing namespace which contains |
| 603 | // both the using-directive and the nominated namespace. |
| 604 | // [Note: in this context, “contains” means “contains directly or |
| 605 | // indirectly”. |
| 606 | // |
| 607 | // For example: |
| 608 | // namespace A { int i; } |
| 609 | // void foo() { |
| 610 | // int i; |
| 611 | // { |
| 612 | // using namespace A; |
| 613 | // ++i; // finds local 'i', A::i appears at global scope |
| 614 | // } |
| 615 | // } |
| 616 | // |
| 617 | for (; S && isFunctionLocalScope(S); S = S->getParent()) { |
| 618 | // Check whether the IdResolver has anything in this scope. |
| 619 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
| 620 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 621 | // We found something. Look for anything else in our scope |
| 622 | // with this same name and in an acceptable identifier |
| 623 | // namespace, so that we can construct an overload set if we |
| 624 | // need to. |
| 625 | IdentifierResolver::iterator LastI = I; |
| 626 | for (++LastI; LastI != IEnd; ++LastI) { |
| 627 | if (!S->isDeclScope(*LastI)) |
| 628 | break; |
| 629 | } |
| 630 | LookupResult Result = |
| 631 | LookupResult::CreateLookupResult(Context, I, LastI); |
| 632 | return std::make_pair(true, Result); |
| 633 | } |
| 634 | } |
| 635 | // NB: Icky, we need to look in function scope, but we need to check its |
| 636 | // parent DeclContext (instead S->getParent()) for member name lookup, |
| 637 | // in case it is out of line method definition. Like in: |
| 638 | // |
| 639 | // class C { |
| 640 | // int i; |
| 641 | // void foo(); |
| 642 | // }; |
| 643 | // |
| 644 | // C::foo() { |
| 645 | // (void) i; |
| 646 | // } |
| 647 | // |
| 648 | // FIXME: Maybe we should do member name lookup here instead? |
| 649 | if (S->getEntity() && isFunctionLocalScope(S)) |
| 650 | break; |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 651 | } |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 652 | |
| 653 | // Collect UsingDirectiveDecls in all scopes, and recursivly all |
| 654 | // nominated namespaces by those using-directives. |
| 655 | // UsingDirectives are pushed to heap, in common ancestor pointer |
| 656 | // value order. |
| 657 | // FIXME: Cache this sorted list in Scope structure, and maybe |
| 658 | // DeclContext, so we don't build it for each lookup! |
| 659 | UsingDirectivesTy UDirs; |
| 660 | for (Scope *SC = Initial; SC; SC = SC->getParent()) |
| 661 | AddScopeUsingDirectives(SC, UDirs); |
| 662 | |
| 663 | // Sort heapified UsingDirectiveDecls. |
| 664 | std::sort_heap(UDirs.begin(), UDirs.end()); |
| 665 | |
| 666 | // Lookup namespace scope, global scope, or possibly (CXX)Record DeclContext |
| 667 | // for member name lookup. |
| 668 | // Unqualified name lookup in C++ requires looking into scopes |
| 669 | // that aren't strictly lexical, and therefore we walk through the |
| 670 | // context as well as walking through the scopes. |
| 671 | for (; S; S = S->getParent()) { |
| 672 | LookupResultsTy LookupResults; |
| 673 | bool LookedInCtx = false; |
| 674 | |
| 675 | // Check whether the IdResolver has anything in this scope. |
| 676 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
| 677 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) { |
| 678 | // We found something. Look for anything else in our scope |
| 679 | // with this same name and in an acceptable identifier |
| 680 | // namespace, so that we can construct an overload set if we |
| 681 | // need to. |
| 682 | IdentifierResolver::iterator LastI = I; |
| 683 | for (++LastI; LastI != IEnd; ++LastI) { |
| 684 | if (!S->isDeclScope(*LastI)) |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | // We store name lookup result, and continue trying to look into |
| 689 | // associated context, and maybe namespaces nominated by |
| 690 | // using-directives. |
| 691 | LookupResults.push_back( |
| 692 | LookupResult::CreateLookupResult(Context, I, LastI)); |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | // If there is an entity associated with this scope, it's a |
| 698 | // DeclContext. We might need to perform qualified lookup into |
| 699 | // it, or namespaces nominated by using-directives. |
| 700 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 701 | |
| 702 | if (Ctx && isa<TranslationUnitDecl>(Ctx)) { |
| 703 | UsingDirectivesTy::const_iterator UI, UEnd; |
| 704 | // For each UsingDirectiveDecl, which common ancestor is equal |
| 705 | // to Ctx, we preform qualified name lookup into namespace nominated |
| 706 | // by it. |
| 707 | llvm::tie(UI, UEnd) = |
| 708 | std::equal_range(UDirs.begin(), UDirs.end(), Ctx, |
| 709 | UsingDirAncestorCompare()); |
| 710 | for (; UI != UEnd; ++UI) { |
| 711 | // FIXME: We will have to ensure, that we won't consider |
| 712 | // again using-directives during qualified name lookup! |
| 713 | // (Once using-directives support for qualified name lookup gets |
| 714 | // implemented). |
| 715 | if (LookupResult R = LookupQualifiedName((*UI)->getNominatedNamespace(), |
| 716 | Name, NameKind, RedeclarationOnly)) |
| 717 | LookupResults.push_back(R); |
| 718 | } |
| 719 | LookupResult Result; |
| 720 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 721 | RedeclarationOnly) { |
| 722 | return std::make_pair(true, Result); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // FIXME: We're performing redundant lookups here, where the |
| 727 | // scope stack mirrors the semantic nested of classes and |
| 728 | // namespaces. We can save some work by checking the lexical |
| 729 | // scope against the semantic scope and avoiding any lookups |
| 730 | // when they are the same. |
| 731 | // FIXME: In some cases, we know that every name that could be |
| 732 | // found by this qualified name lookup will also be on the |
| 733 | // identifier chain. For example, inside a class without any |
| 734 | // base classes, we never need to perform qualified lookup |
| 735 | // because all of the members are on top of the identifier |
| 736 | // chain. However, we cannot perform this optimization when the |
| 737 | // lexical and semantic scopes don't line up, e.g., in an |
| 738 | // out-of-line member definition. |
| 739 | while (Ctx && Ctx->isFunctionOrMethod()) |
| 740 | Ctx = Ctx->getParent(); |
| 741 | while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) { |
| 742 | LookedInCtx = true; |
| 743 | // Look for declarations of this name in this scope. |
| 744 | if (LookupResult R = LookupQualifiedName(Ctx, Name, NameKind, |
| 745 | RedeclarationOnly)) |
| 746 | // We store that, to investigate futher, wheather reference |
| 747 | // to this Decl is no ambiguous. |
| 748 | LookupResults.push_back(R); |
| 749 | |
| 750 | if (Ctx->isNamespace()) { |
| 751 | // For each UsingDirectiveDecl, which common ancestor is equal |
| 752 | // to Ctx, we preform qualified name lookup into namespace nominated |
| 753 | // by it. |
| 754 | UsingDirectivesTy::const_iterator UI, UEnd; |
| 755 | llvm::tie(UI, UEnd) = |
| 756 | std::equal_range(UDirs.begin(), UDirs.end(), Ctx, |
| 757 | UsingDirAncestorCompare()); |
| 758 | for (; UI != UEnd; ++UI) { |
| 759 | // FIXME: We will have to ensure, that we won't consider |
| 760 | // again using-directives during qualified name lookup! |
| 761 | // (Once using-directives support for qualified name lookup gets |
| 762 | // implemented). |
| 763 | LookupResult R = LookupQualifiedName((*UI)->getNominatedNamespace(), |
| 764 | Name, NameKind, |
| 765 | RedeclarationOnly); |
| 766 | if (R) |
| 767 | LookupResults.push_back(R); |
| 768 | } |
| 769 | } |
| 770 | LookupResult Result; |
| 771 | if ((Result = MergeLookupResults(Context, LookupResults)) || |
| 772 | (RedeclarationOnly && !Ctx->isTransparentContext())) { |
| 773 | return std::make_pair(true, Result); |
| 774 | } |
| 775 | Ctx = Ctx->getParent(); |
| 776 | } |
| 777 | |
| 778 | if (!(LookedInCtx || LookupResults.empty())) { |
| 779 | // We didn't Performed lookup in Scope entity, so we return |
| 780 | // result form IdentifierResolver. |
| 781 | assert((LookupResults.size() == 1) && "Wrong size!"); |
| 782 | return std::make_pair(true, LookupResults.front()); |
| 783 | } |
| 784 | } |
| 785 | return std::make_pair(false, LookupResult()); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 788 | /// @brief Perform unqualified name lookup starting from a given |
| 789 | /// scope. |
| 790 | /// |
| 791 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 792 | /// used to find names within the current scope. For example, 'x' in |
| 793 | /// @code |
| 794 | /// int x; |
| 795 | /// int f() { |
| 796 | /// return x; // unqualified name look finds 'x' in the global scope |
| 797 | /// } |
| 798 | /// @endcode |
| 799 | /// |
| 800 | /// Different lookup criteria can find different names. For example, a |
| 801 | /// particular scope can have both a struct and a function of the same |
| 802 | /// name, and each can be found by certain lookup criteria. For more |
| 803 | /// information about lookup criteria, see the documentation for the |
| 804 | /// class LookupCriteria. |
| 805 | /// |
| 806 | /// @param S The scope from which unqualified name lookup will |
| 807 | /// begin. If the lookup criteria permits, name lookup may also search |
| 808 | /// in the parent scopes. |
| 809 | /// |
| 810 | /// @param Name The name of the entity that we are searching for. |
| 811 | /// |
| 812 | /// @param Criteria The criteria that this routine will use to |
| 813 | /// determine which names are visible and which names will be |
| 814 | /// found. Note that name lookup will find a name that is visible by |
| 815 | /// the given criteria, but the entity itself may not be semantically |
| 816 | /// correct or even the kind of entity expected based on the |
| 817 | /// lookup. For example, searching for a nested-name-specifier name |
| 818 | /// might result in an EnumDecl, which is visible but is not permitted |
| 819 | /// as a nested-name-specifier in C++03. |
| 820 | /// |
| 821 | /// @returns The result of name lookup, which includes zero or more |
| 822 | /// declarations and possibly additional information used to diagnose |
| 823 | /// ambiguities. |
| 824 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 825 | Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind, |
| 826 | bool RedeclarationOnly) { |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 827 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 828 | |
| 829 | if (!getLangOptions().CPlusPlus) { |
| 830 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 831 | // search in the declarations attached to the name. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 832 | unsigned IDNS = 0; |
| 833 | switch (NameKind) { |
| 834 | case Sema::LookupOrdinaryName: |
| 835 | IDNS = Decl::IDNS_Ordinary; |
| 836 | break; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 838 | case Sema::LookupTagName: |
| 839 | IDNS = Decl::IDNS_Tag; |
| 840 | break; |
| 841 | |
| 842 | case Sema::LookupMemberName: |
| 843 | IDNS = Decl::IDNS_Member; |
| 844 | break; |
| 845 | |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame^] | 846 | case Sema::LookupOperatorName: |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 847 | case Sema::LookupNestedNameSpecifierName: |
| 848 | case Sema::LookupNamespaceName: |
| 849 | assert(false && "C does not perform these kinds of name lookup"); |
| 850 | break; |
| 851 | } |
| 852 | |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 853 | // Scan up the scope chain looking for a decl that matches this |
| 854 | // identifier that is in the appropriate namespace. This search |
| 855 | // should not take long, as shadowing of names is uncommon, and |
| 856 | // deep shadowing is extremely uncommon. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 857 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), |
| 858 | IEnd = IdResolver.end(); |
| 859 | I != IEnd; ++I) |
| 860 | if ((*I)->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 861 | return LookupResult::CreateLookupResult(Context, *I); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 862 | } else { |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 863 | // Perform C++ unqualified name lookup. |
| 864 | std::pair<bool, LookupResult> MaybeResult = |
| 865 | CppLookupName(S, Name, NameKind, RedeclarationOnly); |
| 866 | if (MaybeResult.first) |
| 867 | return MaybeResult.second; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | // If we didn't find a use of this identifier, and if the identifier |
| 871 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 872 | // now, injecting it into translation unit scope, and return it. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 873 | if (NameKind == LookupOrdinaryName) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 874 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 875 | if (II) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 876 | // If this is a builtin on this (or all) targets, create the decl. |
| 877 | if (unsigned BuiltinID = II->getBuiltinID()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 878 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 879 | LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 880 | S)); |
| 881 | } |
| 882 | if (getLangOptions().ObjC1 && II) { |
| 883 | // @interface and @compatibility_alias introduce typedef-like names. |
| 884 | // Unlike typedef's, they can only be introduced at file-scope (and are |
| 885 | // therefore not scoped decls). They can, however, be shadowed by |
| 886 | // other names in IDNS_Ordinary. |
| 887 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 888 | if (IDI != ObjCInterfaceDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 889 | return LookupResult::CreateLookupResult(Context, IDI->second); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 890 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 891 | if (I != ObjCAliasDecls.end()) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 892 | return LookupResult::CreateLookupResult(Context, |
| 893 | I->second->getClassInterface()); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 896 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /// @brief Perform qualified name lookup into a given context. |
| 900 | /// |
| 901 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 902 | /// names when the context of those names is explicit specified, e.g., |
| 903 | /// "std::vector" or "x->member". |
| 904 | /// |
| 905 | /// Different lookup criteria can find different names. For example, a |
| 906 | /// particular scope can have both a struct and a function of the same |
| 907 | /// name, and each can be found by certain lookup criteria. For more |
| 908 | /// information about lookup criteria, see the documentation for the |
| 909 | /// class LookupCriteria. |
| 910 | /// |
| 911 | /// @param LookupCtx The context in which qualified name lookup will |
| 912 | /// search. If the lookup criteria permits, name lookup may also search |
| 913 | /// in the parent contexts or (for C++ classes) base classes. |
| 914 | /// |
| 915 | /// @param Name The name of the entity that we are searching for. |
| 916 | /// |
| 917 | /// @param Criteria The criteria that this routine will use to |
| 918 | /// determine which names are visible and which names will be |
| 919 | /// found. Note that name lookup will find a name that is visible by |
| 920 | /// the given criteria, but the entity itself may not be semantically |
| 921 | /// correct or even the kind of entity expected based on the |
| 922 | /// lookup. For example, searching for a nested-name-specifier name |
| 923 | /// might result in an EnumDecl, which is visible but is not permitted |
| 924 | /// as a nested-name-specifier in C++03. |
| 925 | /// |
| 926 | /// @returns The result of name lookup, which includes zero or more |
| 927 | /// declarations and possibly additional information used to diagnose |
| 928 | /// ambiguities. |
| 929 | Sema::LookupResult |
| 930 | Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 931 | LookupNameKind NameKind, bool RedeclarationOnly) { |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 932 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
| 933 | |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 934 | if (!Name) return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 935 | |
| 936 | // If we're performing qualified name lookup (e.g., lookup into a |
| 937 | // struct), find fields as part of ordinary name lookup. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 938 | unsigned IDNS |
| 939 | = getIdentifierNamespacesFromLookupNameKind(NameKind, |
| 940 | getLangOptions().CPlusPlus); |
| 941 | if (NameKind == LookupOrdinaryName) |
| 942 | IDNS |= Decl::IDNS_Member; |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 943 | |
| 944 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 945 | DeclContext::lookup_iterator I, E; |
| 946 | for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I) |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 947 | if (isAcceptableLookupResult(*I, NameKind, IDNS)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 948 | return LookupResult::CreateLookupResult(Context, I, E); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 950 | // If this isn't a C++ class or we aren't allowed to look into base |
| 951 | // classes, we're done. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 952 | if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 953 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 954 | |
| 955 | // Perform lookup into our base classes. |
| 956 | BasePaths Paths; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 957 | Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx))); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 958 | |
| 959 | // Look for this member in our base classes |
| 960 | if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 961 | MemberLookupCriteria(Name, NameKind, IDNS), Paths)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 962 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 963 | |
| 964 | // C++ [class.member.lookup]p2: |
| 965 | // [...] If the resulting set of declarations are not all from |
| 966 | // sub-objects of the same type, or the set has a nonstatic member |
| 967 | // and includes members from distinct sub-objects, there is an |
| 968 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 969 | // the result of the lookup. |
| 970 | // FIXME: support using declarations! |
| 971 | QualType SubobjectType; |
Daniel Dunbar | ddebeca | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 972 | int SubobjectNumber = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 973 | for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
| 974 | Path != PathEnd; ++Path) { |
| 975 | const BasePathElement &PathElement = Path->back(); |
| 976 | |
| 977 | // Determine whether we're looking at a distinct sub-object or not. |
| 978 | if (SubobjectType.isNull()) { |
| 979 | // This is the first subobject we've looked at. Record it's type. |
| 980 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 981 | SubobjectNumber = PathElement.SubobjectNumber; |
| 982 | } else if (SubobjectType |
| 983 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 984 | // We found members of the given name in two subobjects of |
| 985 | // different types. This lookup is ambiguous. |
| 986 | BasePaths *PathsOnHeap = new BasePaths; |
| 987 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 988 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, true); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 989 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 990 | // We have a different subobject of the same type. |
| 991 | |
| 992 | // C++ [class.member.lookup]p5: |
| 993 | // A static member, a nested type or an enumerator defined in |
| 994 | // a base class T can unambiguously be found even if an object |
| 995 | // has more than one base class subobject of type T. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 996 | Decl *FirstDecl = *Path->Decls.first; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 997 | if (isa<VarDecl>(FirstDecl) || |
| 998 | isa<TypeDecl>(FirstDecl) || |
| 999 | isa<EnumConstantDecl>(FirstDecl)) |
| 1000 | continue; |
| 1001 | |
| 1002 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 1003 | // Determine whether all of the methods are static. |
| 1004 | bool AllMethodsAreStatic = true; |
| 1005 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 1006 | Func != Path->Decls.second; ++Func) { |
| 1007 | if (!isa<CXXMethodDecl>(*Func)) { |
| 1008 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 1013 | AllMethodsAreStatic = false; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (AllMethodsAreStatic) |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
| 1022 | // We have found a nonstatic member name in multiple, distinct |
| 1023 | // subobjects. Name lookup is ambiguous. |
| 1024 | BasePaths *PathsOnHeap = new BasePaths; |
| 1025 | PathsOnHeap->swap(Paths); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1026 | return LookupResult::CreateLookupResult(Context, PathsOnHeap, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // Lookup in a base class succeeded; return these results. |
| 1031 | |
| 1032 | // If we found a function declaration, return an overload set. |
| 1033 | if (isa<FunctionDecl>(*Paths.front().Decls.first)) |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1034 | return LookupResult::CreateLookupResult(Context, |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1035 | Paths.front().Decls.first, Paths.front().Decls.second); |
| 1036 | |
| 1037 | // We found a non-function declaration; return a single declaration. |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1038 | return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /// @brief Performs name lookup for a name that was parsed in the |
| 1042 | /// source code, and may contain a C++ scope specifier. |
| 1043 | /// |
| 1044 | /// This routine is a convenience routine meant to be called from |
| 1045 | /// contexts that receive a name and an optional C++ scope specifier |
| 1046 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 1047 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 1048 | /// respectively) on the given name and return those results. |
| 1049 | /// |
| 1050 | /// @param S The scope from which unqualified name lookup will |
| 1051 | /// begin. |
| 1052 | /// |
| 1053 | /// @param SS An optional C++ scope-specified, e.g., "::N::M". |
| 1054 | /// |
| 1055 | /// @param Name The name of the entity that name lookup will |
| 1056 | /// search for. |
| 1057 | /// |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1058 | /// @returns The result of qualified or unqualified name lookup. |
| 1059 | Sema::LookupResult |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1060 | Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS, |
| 1061 | DeclarationName Name, LookupNameKind NameKind, |
| 1062 | bool RedeclarationOnly) { |
| 1063 | if (SS) { |
| 1064 | if (SS->isInvalid()) |
| 1065 | return LookupResult::CreateLookupResult(Context, 0); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1067 | if (SS->isSet()) |
| 1068 | return LookupQualifiedName(static_cast<DeclContext *>(SS->getScopeRep()), |
| 1069 | Name, NameKind, RedeclarationOnly); |
| 1070 | } |
| 1071 | |
| 1072 | return LookupName(S, Name, NameKind, RedeclarationOnly); |
Douglas Gregor | 78d7013 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1076 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 1077 | /// from name lookup. |
| 1078 | /// |
| 1079 | /// @param Result The ambiguous name lookup result. |
| 1080 | /// |
| 1081 | /// @param Name The name of the entity that name lookup was |
| 1082 | /// searching for. |
| 1083 | /// |
| 1084 | /// @param NameLoc The location of the name within the source code. |
| 1085 | /// |
| 1086 | /// @param LookupRange A source range that provides more |
| 1087 | /// source-location information concerning the lookup itself. For |
| 1088 | /// example, this range might highlight a nested-name-specifier that |
| 1089 | /// precedes the name. |
| 1090 | /// |
| 1091 | /// @returns true |
| 1092 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name, |
| 1093 | SourceLocation NameLoc, |
| 1094 | SourceRange LookupRange) { |
| 1095 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 1096 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1097 | if (BasePaths *Paths = Result.getBasePaths()) |
| 1098 | { |
| 1099 | if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) { |
| 1100 | QualType SubobjectType = Paths->front().back().Base->getType(); |
| 1101 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 1102 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 1103 | << LookupRange; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1105 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 1106 | while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic()) |
| 1107 | ++Found; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1109 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
| 1110 | |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
| 1114 | assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes && |
| 1115 | "Unhandled form of name lookup ambiguity"); |
| 1116 | |
| 1117 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 1118 | << Name << LookupRange; |
| 1119 | |
| 1120 | std::set<Decl *> DeclsPrinted; |
| 1121 | for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end(); |
| 1122 | Path != PathEnd; ++Path) { |
| 1123 | Decl *D = *Path->Decls.first; |
| 1124 | if (DeclsPrinted.insert(D).second) |
| 1125 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 1126 | } |
| 1127 | |
| 1128 | delete Paths; |
| 1129 | return true; |
| 1130 | } else if (Result.getKind() == LookupResult::AmbiguousReference) { |
| 1131 | |
| 1132 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; |
| 1133 | |
| 1134 | Decl **DI = reinterpret_cast<Decl **>(Result.First), |
| 1135 | **DEnd = reinterpret_cast<Decl **>(Result.Last); |
| 1136 | |
Chris Lattner | b02f5f2 | 2009-02-03 21:29:32 +0000 | [diff] [blame] | 1137 | for (; DI != DEnd; ++DI) |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1138 | Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) |
| 1139 | << getQualifiedName(*DI); |
| 1140 | |
| 1141 | delete[] reinterpret_cast<Decl **>(Result.First); |
Douglas Gregor | 98b2754 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1142 | |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1143 | return true; |
Douglas Gregor | b9ef055 | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1146 | assert(false && "Unhandled form of name lookup ambiguity"); |
Douglas Gregor | d07474b | 2009-01-17 01:13:24 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 7a7be65 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1148 | // We can't reach here. |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1149 | return true; |
| 1150 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1151 | |
| 1152 | // \brief Add the associated classes and namespaces for |
| 1153 | // argument-dependent lookup with an argument of class type |
| 1154 | // (C++ [basic.lookup.koenig]p2). |
| 1155 | static void |
| 1156 | addAssociatedClassesAndNamespaces(CXXRecordDecl *Class, |
| 1157 | ASTContext &Context, |
| 1158 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1159 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1160 | // C++ [basic.lookup.koenig]p2: |
| 1161 | // [...] |
| 1162 | // -- If T is a class type (including unions), its associated |
| 1163 | // classes are: the class itself; the class of which it is a |
| 1164 | // member, if any; and its direct and indirect base |
| 1165 | // classes. Its associated namespaces are the namespaces in |
| 1166 | // which its associated classes are defined. |
| 1167 | |
| 1168 | // Add the class of which it is a member, if any. |
| 1169 | DeclContext *Ctx = Class->getDeclContext(); |
| 1170 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1171 | AssociatedClasses.insert(EnclosingClass); |
| 1172 | |
| 1173 | // Add the associated namespace for this class. |
| 1174 | while (Ctx->isRecord()) |
| 1175 | Ctx = Ctx->getParent(); |
| 1176 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1177 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1178 | |
| 1179 | // Add the class itself. If we've already seen this class, we don't |
| 1180 | // need to visit base classes. |
| 1181 | if (!AssociatedClasses.insert(Class)) |
| 1182 | return; |
| 1183 | |
| 1184 | // FIXME: Handle class template specializations |
| 1185 | |
| 1186 | // Add direct and indirect base classes along with their associated |
| 1187 | // namespaces. |
| 1188 | llvm::SmallVector<CXXRecordDecl *, 32> Bases; |
| 1189 | Bases.push_back(Class); |
| 1190 | while (!Bases.empty()) { |
| 1191 | // Pop this class off the stack. |
| 1192 | Class = Bases.back(); |
| 1193 | Bases.pop_back(); |
| 1194 | |
| 1195 | // Visit the base classes. |
| 1196 | for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(), |
| 1197 | BaseEnd = Class->bases_end(); |
| 1198 | Base != BaseEnd; ++Base) { |
| 1199 | const RecordType *BaseType = Base->getType()->getAsRecordType(); |
| 1200 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 1201 | if (AssociatedClasses.insert(BaseDecl)) { |
| 1202 | // Find the associated namespace for this base class. |
| 1203 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); |
| 1204 | while (BaseCtx->isRecord()) |
| 1205 | BaseCtx = BaseCtx->getParent(); |
| 1206 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(BaseCtx)) |
| 1207 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1208 | |
| 1209 | // Make sure we visit the bases of this base class. |
| 1210 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) |
| 1211 | Bases.push_back(BaseDecl); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // \brief Add the associated classes and namespaces for |
| 1218 | // argument-dependent lookup with an argument of type T |
| 1219 | // (C++ [basic.lookup.koenig]p2). |
| 1220 | static void |
| 1221 | addAssociatedClassesAndNamespaces(QualType T, |
| 1222 | ASTContext &Context, |
| 1223 | Sema::AssociatedNamespaceSet &AssociatedNamespaces, |
| 1224 | Sema::AssociatedClassSet &AssociatedClasses) { |
| 1225 | // C++ [basic.lookup.koenig]p2: |
| 1226 | // |
| 1227 | // For each argument type T in the function call, there is a set |
| 1228 | // of zero or more associated namespaces and a set of zero or more |
| 1229 | // associated classes to be considered. The sets of namespaces and |
| 1230 | // classes is determined entirely by the types of the function |
| 1231 | // arguments (and the namespace of any template template |
| 1232 | // argument). Typedef names and using-declarations used to specify |
| 1233 | // the types do not contribute to this set. The sets of namespaces |
| 1234 | // and classes are determined in the following way: |
| 1235 | T = Context.getCanonicalType(T).getUnqualifiedType(); |
| 1236 | |
| 1237 | // -- If T is a pointer to U or an array of U, its associated |
| 1238 | // namespaces and classes are those associated with U. |
| 1239 | // |
| 1240 | // We handle this by unwrapping pointer and array types immediately, |
| 1241 | // to avoid unnecessary recursion. |
| 1242 | while (true) { |
| 1243 | if (const PointerType *Ptr = T->getAsPointerType()) |
| 1244 | T = Ptr->getPointeeType(); |
| 1245 | else if (const ArrayType *Ptr = Context.getAsArrayType(T)) |
| 1246 | T = Ptr->getElementType(); |
| 1247 | else |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | // -- If T is a fundamental type, its associated sets of |
| 1252 | // namespaces and classes are both empty. |
| 1253 | if (T->getAsBuiltinType()) |
| 1254 | return; |
| 1255 | |
| 1256 | // -- If T is a class type (including unions), its associated |
| 1257 | // classes are: the class itself; the class of which it is a |
| 1258 | // member, if any; and its direct and indirect base |
| 1259 | // classes. Its associated namespaces are the namespaces in |
| 1260 | // which its associated classes are defined. |
| 1261 | if (const CXXRecordType *ClassType |
| 1262 | = dyn_cast_or_null<CXXRecordType>(T->getAsRecordType())) { |
| 1263 | addAssociatedClassesAndNamespaces(ClassType->getDecl(), |
| 1264 | Context, AssociatedNamespaces, |
| 1265 | AssociatedClasses); |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | // -- If T is an enumeration type, its associated namespace is |
| 1270 | // the namespace in which it is defined. If it is class |
| 1271 | // member, its associated class is the member’s class; else |
| 1272 | // it has no associated class. |
| 1273 | if (const EnumType *EnumT = T->getAsEnumType()) { |
| 1274 | EnumDecl *Enum = EnumT->getDecl(); |
| 1275 | |
| 1276 | DeclContext *Ctx = Enum->getDeclContext(); |
| 1277 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) |
| 1278 | AssociatedClasses.insert(EnclosingClass); |
| 1279 | |
| 1280 | // Add the associated namespace for this class. |
| 1281 | while (Ctx->isRecord()) |
| 1282 | Ctx = Ctx->getParent(); |
| 1283 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1284 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1285 | |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | // -- If T is a function type, its associated namespaces and |
| 1290 | // classes are those associated with the function parameter |
| 1291 | // types and those associated with the return type. |
| 1292 | if (const FunctionType *FunctionType = T->getAsFunctionType()) { |
| 1293 | // Return type |
| 1294 | addAssociatedClassesAndNamespaces(FunctionType->getResultType(), |
| 1295 | Context, |
| 1296 | AssociatedNamespaces, AssociatedClasses); |
| 1297 | |
| 1298 | const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FunctionType); |
| 1299 | if (!Proto) |
| 1300 | return; |
| 1301 | |
| 1302 | // Argument types |
| 1303 | for (FunctionTypeProto::arg_type_iterator Arg = Proto->arg_type_begin(), |
| 1304 | ArgEnd = Proto->arg_type_end(); |
| 1305 | Arg != ArgEnd; ++Arg) |
| 1306 | addAssociatedClassesAndNamespaces(*Arg, Context, |
| 1307 | AssociatedNamespaces, AssociatedClasses); |
| 1308 | |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | // -- If T is a pointer to a member function of a class X, its |
| 1313 | // associated namespaces and classes are those associated |
| 1314 | // with the function parameter types and return type, |
| 1315 | // together with those associated with X. |
| 1316 | // |
| 1317 | // -- If T is a pointer to a data member of class X, its |
| 1318 | // associated namespaces and classes are those associated |
| 1319 | // with the member type together with those associated with |
| 1320 | // X. |
| 1321 | if (const MemberPointerType *MemberPtr = T->getAsMemberPointerType()) { |
| 1322 | // Handle the type that the pointer to member points to. |
| 1323 | addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(), |
| 1324 | Context, |
| 1325 | AssociatedNamespaces, AssociatedClasses); |
| 1326 | |
| 1327 | // Handle the class type into which this points. |
| 1328 | if (const RecordType *Class = MemberPtr->getClass()->getAsRecordType()) |
| 1329 | addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()), |
| 1330 | Context, |
| 1331 | AssociatedNamespaces, AssociatedClasses); |
| 1332 | |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | // FIXME: What about block pointers? |
| 1337 | // FIXME: What about Objective-C message sends? |
| 1338 | } |
| 1339 | |
| 1340 | /// \brief Find the associated classes and namespaces for |
| 1341 | /// argument-dependent lookup for a call with the given set of |
| 1342 | /// arguments. |
| 1343 | /// |
| 1344 | /// This routine computes the sets of associated classes and associated |
| 1345 | /// namespaces searched by argument-dependent lookup |
| 1346 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. |
| 1347 | void |
| 1348 | Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, |
| 1349 | AssociatedNamespaceSet &AssociatedNamespaces, |
| 1350 | AssociatedClassSet &AssociatedClasses) { |
| 1351 | AssociatedNamespaces.clear(); |
| 1352 | AssociatedClasses.clear(); |
| 1353 | |
| 1354 | // C++ [basic.lookup.koenig]p2: |
| 1355 | // For each argument type T in the function call, there is a set |
| 1356 | // of zero or more associated namespaces and a set of zero or more |
| 1357 | // associated classes to be considered. The sets of namespaces and |
| 1358 | // classes is determined entirely by the types of the function |
| 1359 | // arguments (and the namespace of any template template |
| 1360 | // argument). |
| 1361 | for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) { |
| 1362 | Expr *Arg = Args[ArgIdx]; |
| 1363 | |
| 1364 | if (Arg->getType() != Context.OverloadTy) { |
| 1365 | addAssociatedClassesAndNamespaces(Arg->getType(), Context, |
| 1366 | AssociatedNamespaces, AssociatedClasses); |
| 1367 | continue; |
| 1368 | } |
| 1369 | |
| 1370 | // [...] In addition, if the argument is the name or address of a |
| 1371 | // set of overloaded functions and/or function templates, its |
| 1372 | // associated classes and namespaces are the union of those |
| 1373 | // associated with each of the members of the set: the namespace |
| 1374 | // in which the function or function template is defined and the |
| 1375 | // classes and namespaces associated with its (non-dependent) |
| 1376 | // parameter types and return type. |
| 1377 | DeclRefExpr *DRE = 0; |
| 1378 | if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1379 | if (unaryOp->getOpcode() == UnaryOperator::AddrOf) |
| 1380 | DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr()); |
| 1381 | } else |
| 1382 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 1383 | if (!DRE) |
| 1384 | continue; |
| 1385 | |
| 1386 | OverloadedFunctionDecl *Ovl |
| 1387 | = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl()); |
| 1388 | if (!Ovl) |
| 1389 | continue; |
| 1390 | |
| 1391 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 1392 | FuncEnd = Ovl->function_end(); |
| 1393 | Func != FuncEnd; ++Func) { |
| 1394 | FunctionDecl *FDecl = cast<FunctionDecl>(*Func); |
| 1395 | |
| 1396 | // Add the namespace in which this function was defined. Note |
| 1397 | // that, if this is a member function, we do *not* consider the |
| 1398 | // enclosing namespace of its class. |
| 1399 | DeclContext *Ctx = FDecl->getDeclContext(); |
| 1400 | if (NamespaceDecl *EnclosingNamespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 1401 | AssociatedNamespaces.insert(EnclosingNamespace); |
| 1402 | |
| 1403 | // Add the classes and namespaces associated with the parameter |
| 1404 | // types and return type of this function. |
| 1405 | addAssociatedClassesAndNamespaces(FDecl->getType(), Context, |
| 1406 | AssociatedNamespaces, AssociatedClasses); |
| 1407 | } |
| 1408 | } |
| 1409 | } |