Douglas Gregor | eb11cd0 | 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 | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | eb11cd0 | 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" |
| 20 | #include "clang/Parse/DeclSpec.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 23 | #include <set> |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | /// MaybeConstructOverloadSet - Name lookup has determined that the |
| 28 | /// elements in [I, IEnd) have the name that we are looking for, and |
| 29 | /// *I is a match for the namespace. This routine returns an |
| 30 | /// appropriate Decl for name lookup, which may either be *I or an |
| 31 | /// OverloadeFunctionDecl that represents the overloaded functions in |
| 32 | /// [I, IEnd). |
| 33 | /// |
| 34 | /// The existance of this routine is temporary; LookupDecl should |
| 35 | /// probably be able to return multiple results, to deal with cases of |
| 36 | /// ambiguity and overloaded functions without needing to create a |
| 37 | /// Decl node. |
| 38 | template<typename DeclIterator> |
| 39 | static Decl * |
| 40 | MaybeConstructOverloadSet(ASTContext &Context, |
| 41 | DeclIterator I, DeclIterator IEnd) { |
| 42 | assert(I != IEnd && "Iterator range cannot be empty"); |
| 43 | assert(!isa<OverloadedFunctionDecl>(*I) && |
| 44 | "Cannot have an overloaded function"); |
| 45 | |
| 46 | if (isa<FunctionDecl>(*I)) { |
| 47 | // If we found a function, there might be more functions. If |
| 48 | // so, collect them into an overload set. |
| 49 | DeclIterator Last = I; |
| 50 | OverloadedFunctionDecl *Ovl = 0; |
| 51 | for (++Last; Last != IEnd && isa<FunctionDecl>(*Last); ++Last) { |
| 52 | if (!Ovl) { |
| 53 | // FIXME: We leak this overload set. Eventually, we want to |
| 54 | // stop building the declarations for these overload sets, so |
| 55 | // there will be nothing to leak. |
| 56 | Ovl = OverloadedFunctionDecl::Create(Context, |
| 57 | cast<ScopedDecl>(*I)->getDeclContext(), |
| 58 | (*I)->getDeclName()); |
| 59 | Ovl->addOverload(cast<FunctionDecl>(*I)); |
| 60 | } |
| 61 | Ovl->addOverload(cast<FunctionDecl>(*Last)); |
| 62 | } |
| 63 | |
| 64 | // If we had more than one function, we built an overload |
| 65 | // set. Return it. |
| 66 | if (Ovl) |
| 67 | return Ovl; |
| 68 | } |
| 69 | |
| 70 | return *I; |
| 71 | } |
| 72 | |
| 73 | /// @brief Constructs name lookup criteria. |
| 74 | /// |
| 75 | /// @param K The kind of name that we're searching for. |
| 76 | /// |
| 77 | /// @param RedeclarationOnly If true, then name lookup will only look |
| 78 | /// into the current scope for names, not in parent scopes. This |
| 79 | /// option should be set when we're looking to introduce a new |
| 80 | /// declaration into scope. |
| 81 | /// |
| 82 | /// @param CPlusPlus Whether we are performing C++ name lookup or not. |
| 83 | Sema::LookupCriteria::LookupCriteria(NameKind K, bool RedeclarationOnly, |
| 84 | bool CPlusPlus) |
| 85 | : Kind(K), AllowLazyBuiltinCreation(K == Ordinary), |
| 86 | RedeclarationOnly(RedeclarationOnly) { |
| 87 | switch (Kind) { |
| 88 | case Ordinary: |
| 89 | IDNS = Decl::IDNS_Ordinary; |
| 90 | if (CPlusPlus) |
| 91 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member; |
| 92 | break; |
| 93 | |
| 94 | case Tag: |
| 95 | IDNS = Decl::IDNS_Tag; |
| 96 | break; |
| 97 | |
| 98 | case Member: |
| 99 | IDNS = Decl::IDNS_Member; |
| 100 | if (CPlusPlus) |
| 101 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; |
| 102 | break; |
| 103 | |
| 104 | case NestedNameSpecifier: |
| 105 | case Namespace: |
| 106 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// isLookupResult - Determines whether D is a suitable lookup result |
| 112 | /// according to the lookup criteria. |
| 113 | bool Sema::LookupCriteria::isLookupResult(Decl *D) const { |
| 114 | switch (Kind) { |
| 115 | case Ordinary: |
| 116 | case Tag: |
| 117 | case Member: |
| 118 | return D->isInIdentifierNamespace(IDNS); |
| 119 | |
| 120 | case NestedNameSpecifier: |
| 121 | return isa<TypedefDecl>(D) || D->isInIdentifierNamespace(Decl::IDNS_Tag); |
| 122 | |
| 123 | case Namespace: |
| 124 | return isa<NamespaceDecl>(D); |
| 125 | } |
| 126 | |
| 127 | assert(false && "isLookupResult always returns before this point"); |
| 128 | return false; |
| 129 | } |
| 130 | |
Douglas Gregor | 4bb64e7 | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 131 | /// @brief Moves the name-lookup results from Other to the |
| 132 | /// newly-constructed LookupResult. |
| 133 | Sema::LookupResult::LookupResult(const LookupResult& Other) |
| 134 | : StoredKind(Other.StoredKind), First(Other.First), Last(Other.Last), |
| 135 | Context(Other.Context) { |
| 136 | Other.StoredKind = Dead; |
| 137 | } |
| 138 | |
| 139 | /// @brief Moves the name-lookup results from Other to this LookupResult. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 140 | Sema::LookupResult::LookupResult(ASTContext &Context, |
| 141 | IdentifierResolver::iterator F, |
| 142 | IdentifierResolver::iterator L) |
| 143 | : Context(&Context) { |
| 144 | if (F != L && isa<FunctionDecl>(*F)) { |
| 145 | IdentifierResolver::iterator Next = F; |
| 146 | ++Next; |
| 147 | if (Next != L && isa<FunctionDecl>(*Next)) { |
| 148 | StoredKind = OverloadedDeclFromIdResolver; |
| 149 | First = F.getAsOpaqueValue(); |
| 150 | Last = L.getAsOpaqueValue(); |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | StoredKind = SingleDecl; |
| 156 | First = reinterpret_cast<uintptr_t>(*F); |
| 157 | Last = 0; |
| 158 | } |
| 159 | |
| 160 | Sema::LookupResult::LookupResult(ASTContext &Context, |
| 161 | DeclContext::lookup_iterator F, |
| 162 | DeclContext::lookup_iterator L) |
| 163 | : Context(&Context) { |
| 164 | if (F != L && isa<FunctionDecl>(*F)) { |
| 165 | DeclContext::lookup_iterator Next = F; |
| 166 | ++Next; |
| 167 | if (Next != L && isa<FunctionDecl>(*Next)) { |
| 168 | StoredKind = OverloadedDeclFromDeclContext; |
| 169 | First = reinterpret_cast<uintptr_t>(F); |
| 170 | Last = reinterpret_cast<uintptr_t>(L); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | StoredKind = SingleDecl; |
| 176 | First = reinterpret_cast<uintptr_t>(*F); |
| 177 | Last = 0; |
| 178 | } |
| 179 | |
Douglas Gregor | 4bb64e7 | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 180 | Sema::LookupResult::~LookupResult() { |
| 181 | if (StoredKind == AmbiguousLookup) |
| 182 | delete getBasePaths(); |
| 183 | } |
| 184 | |
| 185 | Sema::LookupResult& Sema::LookupResult::operator=(const LookupResult& Other) { |
| 186 | if (StoredKind == AmbiguousLookup) |
| 187 | delete getBasePaths(); |
| 188 | |
| 189 | StoredKind = Other.StoredKind; |
| 190 | First = Other.First; |
| 191 | Last = Other.Last; |
| 192 | Context = Other.Context; |
| 193 | |
| 194 | Other.StoredKind = Dead; |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 199 | /// @brief Determine the result of name lookup. |
| 200 | Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const { |
| 201 | switch (StoredKind) { |
| 202 | case SingleDecl: |
| 203 | return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound; |
| 204 | |
| 205 | case OverloadedDeclFromIdResolver: |
| 206 | case OverloadedDeclFromDeclContext: |
| 207 | return FoundOverloaded; |
| 208 | |
| 209 | case AmbiguousLookup: |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 210 | return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects; |
Douglas Gregor | 4bb64e7 | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 211 | |
| 212 | case Dead: |
| 213 | assert(false && "Attempt to look at a dead LookupResult"); |
| 214 | break; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 217 | // We can't ever get here. |
| 218 | return NotFound; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /// @brief Converts the result of name lookup into a single (possible |
| 222 | /// NULL) pointer to a declaration. |
| 223 | /// |
| 224 | /// The resulting declaration will either be the declaration we found |
| 225 | /// (if only a single declaration was found), an |
| 226 | /// OverloadedFunctionDecl (if an overloaded function was found), or |
| 227 | /// NULL (if no declaration was found). This conversion must not be |
| 228 | /// used anywhere where name lookup could result in an ambiguity. |
| 229 | /// |
| 230 | /// The OverloadedFunctionDecl conversion is meant as a stop-gap |
| 231 | /// solution, since it causes the OverloadedFunctionDecl to be |
| 232 | /// leaked. FIXME: Eventually, there will be a better way to iterate |
| 233 | /// over the set of overloaded functions returned by name lookup. |
| 234 | Decl *Sema::LookupResult::getAsDecl() const { |
| 235 | switch (StoredKind) { |
| 236 | case SingleDecl: |
| 237 | return reinterpret_cast<Decl *>(First); |
| 238 | |
| 239 | case OverloadedDeclFromIdResolver: |
| 240 | return MaybeConstructOverloadSet(*Context, |
| 241 | IdentifierResolver::iterator::getFromOpaqueValue(First), |
| 242 | IdentifierResolver::iterator::getFromOpaqueValue(Last)); |
| 243 | |
| 244 | case OverloadedDeclFromDeclContext: |
| 245 | return MaybeConstructOverloadSet(*Context, |
| 246 | reinterpret_cast<DeclContext::lookup_iterator>(First), |
| 247 | reinterpret_cast<DeclContext::lookup_iterator>(Last)); |
| 248 | |
| 249 | case AmbiguousLookup: |
| 250 | assert(false && |
| 251 | "Name lookup returned an ambiguity that could not be handled"); |
| 252 | break; |
Douglas Gregor | 4bb64e7 | 2009-01-15 02:19:31 +0000 | [diff] [blame] | 253 | |
| 254 | case Dead: |
| 255 | assert(false && "Attempt to look at a dead LookupResult"); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 261 | /// @brief Retrieves the BasePaths structure describing an ambiguous |
| 262 | /// name lookup. |
| 263 | BasePaths *Sema::LookupResult::getBasePaths() const { |
| 264 | assert((StoredKind == AmbiguousLookup) && |
| 265 | "getBasePaths can only be used on an ambiguous lookup"); |
| 266 | return reinterpret_cast<BasePaths *>(First); |
| 267 | } |
| 268 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 269 | /// @brief Perform unqualified name lookup starting from a given |
| 270 | /// scope. |
| 271 | /// |
| 272 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is |
| 273 | /// used to find names within the current scope. For example, 'x' in |
| 274 | /// @code |
| 275 | /// int x; |
| 276 | /// int f() { |
| 277 | /// return x; // unqualified name look finds 'x' in the global scope |
| 278 | /// } |
| 279 | /// @endcode |
| 280 | /// |
| 281 | /// Different lookup criteria can find different names. For example, a |
| 282 | /// particular scope can have both a struct and a function of the same |
| 283 | /// name, and each can be found by certain lookup criteria. For more |
| 284 | /// information about lookup criteria, see the documentation for the |
| 285 | /// class LookupCriteria. |
| 286 | /// |
| 287 | /// @param S The scope from which unqualified name lookup will |
| 288 | /// begin. If the lookup criteria permits, name lookup may also search |
| 289 | /// in the parent scopes. |
| 290 | /// |
| 291 | /// @param Name The name of the entity that we are searching for. |
| 292 | /// |
| 293 | /// @param Criteria The criteria that this routine will use to |
| 294 | /// determine which names are visible and which names will be |
| 295 | /// found. Note that name lookup will find a name that is visible by |
| 296 | /// the given criteria, but the entity itself may not be semantically |
| 297 | /// correct or even the kind of entity expected based on the |
| 298 | /// lookup. For example, searching for a nested-name-specifier name |
| 299 | /// might result in an EnumDecl, which is visible but is not permitted |
| 300 | /// as a nested-name-specifier in C++03. |
| 301 | /// |
| 302 | /// @returns The result of name lookup, which includes zero or more |
| 303 | /// declarations and possibly additional information used to diagnose |
| 304 | /// ambiguities. |
| 305 | Sema::LookupResult |
| 306 | Sema::LookupName(Scope *S, DeclarationName Name, LookupCriteria Criteria) { |
| 307 | if (!Name) return LookupResult(Context, 0); |
| 308 | |
| 309 | if (!getLangOptions().CPlusPlus) { |
| 310 | // Unqualified name lookup in C/Objective-C is purely lexical, so |
| 311 | // search in the declarations attached to the name. |
| 312 | |
| 313 | // For the purposes of unqualified name lookup, structs and unions |
| 314 | // don't have scopes at all. For example: |
| 315 | // |
| 316 | // struct X { |
| 317 | // struct T { int i; } x; |
| 318 | // }; |
| 319 | // |
| 320 | // void f() { |
| 321 | // struct T t; // okay: T is defined lexically within X, but |
| 322 | // // semantically at global scope |
| 323 | // }; |
| 324 | // |
| 325 | // FIXME: Is there a better way to deal with this? |
| 326 | DeclContext *SearchCtx = CurContext; |
| 327 | while (isa<RecordDecl>(SearchCtx) || isa<EnumDecl>(SearchCtx)) |
| 328 | SearchCtx = SearchCtx->getParent(); |
| 329 | IdentifierResolver::iterator I |
| 330 | = IdResolver.begin(Name, SearchCtx, !Criteria.RedeclarationOnly); |
| 331 | |
| 332 | // Scan up the scope chain looking for a decl that matches this |
| 333 | // identifier that is in the appropriate namespace. This search |
| 334 | // should not take long, as shadowing of names is uncommon, and |
| 335 | // deep shadowing is extremely uncommon. |
| 336 | for (; I != IdResolver.end(); ++I) |
| 337 | if (Criteria.isLookupResult(*I)) |
| 338 | return LookupResult(Context, *I); |
| 339 | } else { |
| 340 | // Unqualified name lookup in C++ requires looking into scopes |
| 341 | // that aren't strictly lexical, and therefore we walk through the |
| 342 | // context as well as walking through the scopes. |
| 343 | |
| 344 | // FIXME: does "true" for LookInParentCtx actually make sense? |
| 345 | IdentifierResolver::iterator |
| 346 | I = IdResolver.begin(Name, CurContext, true/*LookInParentCtx*/), |
| 347 | IEnd = IdResolver.end(); |
| 348 | for (; S; S = S->getParent()) { |
| 349 | // Check whether the IdResolver has anything in this scope. |
| 350 | for (; I != IEnd && S->isDeclScope(*I); ++I) { |
| 351 | if (Criteria.isLookupResult(*I)) { |
| 352 | // We found something. Look for anything else in our scope |
| 353 | // with this same name and in an acceptable identifier |
| 354 | // namespace, so that we can construct an overload set if we |
| 355 | // need to. |
| 356 | IdentifierResolver::iterator LastI = I; |
| 357 | for (++LastI; LastI != IEnd; ++LastI) { |
| 358 | if (!S->isDeclScope(*LastI)) |
| 359 | break; |
| 360 | } |
| 361 | return LookupResult(Context, I, LastI); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // If there is an entity associated with this scope, it's a |
| 366 | // DeclContext. We might need to perform qualified lookup into |
| 367 | // it. |
| 368 | // FIXME: We're performing redundant lookups here, where the |
| 369 | // scope stack mirrors the semantic nested of classes and |
| 370 | // namespaces. We can save some work by checking the lexical |
| 371 | // scope against the semantic scope and avoiding any lookups |
| 372 | // when they are the same. |
| 373 | // FIXME: In some cases, we know that every name that could be |
| 374 | // found by this qualified name lookup will also be on the |
| 375 | // identifier chain. For example, inside a class without any |
| 376 | // base classes, we never need to perform qualified lookup |
| 377 | // because all of the members are on top of the identifier |
| 378 | // chain. However, we cannot perform this optimization when the |
| 379 | // lexical and semantic scopes don't line up, e.g., in an |
| 380 | // out-of-line member definition. |
| 381 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 382 | while (Ctx && Ctx->isFunctionOrMethod()) |
| 383 | Ctx = Ctx->getParent(); |
| 384 | while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) { |
| 385 | // Look for declarations of this name in this scope. |
| 386 | if (LookupResult Result = LookupQualifiedName(Ctx, Name, Criteria)) |
| 387 | return Result; |
| 388 | |
| 389 | if (Criteria.RedeclarationOnly && !Ctx->isTransparentContext()) |
| 390 | return LookupResult(Context, 0); |
| 391 | |
| 392 | Ctx = Ctx->getParent(); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // If we didn't find a use of this identifier, and if the identifier |
| 398 | // corresponds to a compiler builtin, create the decl object for the builtin |
| 399 | // now, injecting it into translation unit scope, and return it. |
| 400 | if (Criteria.Kind == LookupCriteria::Ordinary) { |
| 401 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 402 | if (Criteria.AllowLazyBuiltinCreation && II) { |
| 403 | // If this is a builtin on this (or all) targets, create the decl. |
| 404 | if (unsigned BuiltinID = II->getBuiltinID()) |
| 405 | return LookupResult(Context, |
| 406 | LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 407 | S)); |
| 408 | } |
| 409 | if (getLangOptions().ObjC1 && II) { |
| 410 | // @interface and @compatibility_alias introduce typedef-like names. |
| 411 | // Unlike typedef's, they can only be introduced at file-scope (and are |
| 412 | // therefore not scoped decls). They can, however, be shadowed by |
| 413 | // other names in IDNS_Ordinary. |
| 414 | ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II); |
| 415 | if (IDI != ObjCInterfaceDecls.end()) |
| 416 | return LookupResult(Context, IDI->second); |
| 417 | ObjCAliasTy::iterator I = ObjCAliasDecls.find(II); |
| 418 | if (I != ObjCAliasDecls.end()) |
| 419 | return LookupResult(Context, I->second->getClassInterface()); |
| 420 | } |
| 421 | } |
| 422 | return LookupResult(Context, 0); |
| 423 | } |
| 424 | |
| 425 | /// @brief Perform qualified name lookup into a given context. |
| 426 | /// |
| 427 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find |
| 428 | /// names when the context of those names is explicit specified, e.g., |
| 429 | /// "std::vector" or "x->member". |
| 430 | /// |
| 431 | /// Different lookup criteria can find different names. For example, a |
| 432 | /// particular scope can have both a struct and a function of the same |
| 433 | /// name, and each can be found by certain lookup criteria. For more |
| 434 | /// information about lookup criteria, see the documentation for the |
| 435 | /// class LookupCriteria. |
| 436 | /// |
| 437 | /// @param LookupCtx The context in which qualified name lookup will |
| 438 | /// search. If the lookup criteria permits, name lookup may also search |
| 439 | /// in the parent contexts or (for C++ classes) base classes. |
| 440 | /// |
| 441 | /// @param Name The name of the entity that we are searching for. |
| 442 | /// |
| 443 | /// @param Criteria The criteria that this routine will use to |
| 444 | /// determine which names are visible and which names will be |
| 445 | /// found. Note that name lookup will find a name that is visible by |
| 446 | /// the given criteria, but the entity itself may not be semantically |
| 447 | /// correct or even the kind of entity expected based on the |
| 448 | /// lookup. For example, searching for a nested-name-specifier name |
| 449 | /// might result in an EnumDecl, which is visible but is not permitted |
| 450 | /// as a nested-name-specifier in C++03. |
| 451 | /// |
| 452 | /// @returns The result of name lookup, which includes zero or more |
| 453 | /// declarations and possibly additional information used to diagnose |
| 454 | /// ambiguities. |
| 455 | Sema::LookupResult |
| 456 | Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, |
| 457 | LookupCriteria Criteria) { |
| 458 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context"); |
| 459 | |
| 460 | if (!Name) return LookupResult(Context, 0); |
| 461 | |
| 462 | // If we're performing qualified name lookup (e.g., lookup into a |
| 463 | // struct), find fields as part of ordinary name lookup. |
| 464 | if (Criteria.Kind == LookupCriteria::Ordinary) |
| 465 | Criteria.IDNS |= Decl::IDNS_Member; |
| 466 | |
| 467 | // Perform qualified name lookup into the LookupCtx. |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 468 | DeclContext::lookup_iterator I, E; |
| 469 | for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I) |
| 470 | if (Criteria.isLookupResult(*I)) |
| 471 | return LookupResult(Context, I, E); |
| 472 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 473 | // If this isn't a C++ class or we aren't allowed to look into base |
| 474 | // classes, we're done. |
| 475 | if (Criteria.RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx)) |
| 476 | return LookupResult(Context, 0); |
| 477 | |
| 478 | // Perform lookup into our base classes. |
| 479 | BasePaths Paths; |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 480 | Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx))); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 481 | |
| 482 | // Look for this member in our base classes |
| 483 | if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx), |
| 484 | MemberLookupCriteria(Name, Criteria), Paths)) |
| 485 | return LookupResult(Context, 0); |
| 486 | |
| 487 | // C++ [class.member.lookup]p2: |
| 488 | // [...] If the resulting set of declarations are not all from |
| 489 | // sub-objects of the same type, or the set has a nonstatic member |
| 490 | // and includes members from distinct sub-objects, there is an |
| 491 | // ambiguity and the program is ill-formed. Otherwise that set is |
| 492 | // the result of the lookup. |
| 493 | // FIXME: support using declarations! |
| 494 | QualType SubobjectType; |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 495 | int SubobjectNumber = 0; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 496 | for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); |
| 497 | Path != PathEnd; ++Path) { |
| 498 | const BasePathElement &PathElement = Path->back(); |
| 499 | |
| 500 | // Determine whether we're looking at a distinct sub-object or not. |
| 501 | if (SubobjectType.isNull()) { |
| 502 | // This is the first subobject we've looked at. Record it's type. |
| 503 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); |
| 504 | SubobjectNumber = PathElement.SubobjectNumber; |
| 505 | } else if (SubobjectType |
| 506 | != Context.getCanonicalType(PathElement.Base->getType())) { |
| 507 | // We found members of the given name in two subobjects of |
| 508 | // different types. This lookup is ambiguous. |
| 509 | BasePaths *PathsOnHeap = new BasePaths; |
| 510 | PathsOnHeap->swap(Paths); |
| 511 | return LookupResult(Context, PathsOnHeap, true); |
| 512 | } else if (SubobjectNumber != PathElement.SubobjectNumber) { |
| 513 | // We have a different subobject of the same type. |
| 514 | |
| 515 | // C++ [class.member.lookup]p5: |
| 516 | // A static member, a nested type or an enumerator defined in |
| 517 | // a base class T can unambiguously be found even if an object |
| 518 | // has more than one base class subobject of type T. |
| 519 | ScopedDecl *FirstDecl = *Path->Decls.first; |
| 520 | if (isa<VarDecl>(FirstDecl) || |
| 521 | isa<TypeDecl>(FirstDecl) || |
| 522 | isa<EnumConstantDecl>(FirstDecl)) |
| 523 | continue; |
| 524 | |
| 525 | if (isa<CXXMethodDecl>(FirstDecl)) { |
| 526 | // Determine whether all of the methods are static. |
| 527 | bool AllMethodsAreStatic = true; |
| 528 | for (DeclContext::lookup_iterator Func = Path->Decls.first; |
| 529 | Func != Path->Decls.second; ++Func) { |
| 530 | if (!isa<CXXMethodDecl>(*Func)) { |
| 531 | assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl"); |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | if (!cast<CXXMethodDecl>(*Func)->isStatic()) { |
| 536 | AllMethodsAreStatic = false; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (AllMethodsAreStatic) |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | // We have found a nonstatic member name in multiple, distinct |
| 546 | // subobjects. Name lookup is ambiguous. |
| 547 | BasePaths *PathsOnHeap = new BasePaths; |
| 548 | PathsOnHeap->swap(Paths); |
| 549 | return LookupResult(Context, PathsOnHeap, false); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // Lookup in a base class succeeded; return these results. |
| 554 | |
| 555 | // If we found a function declaration, return an overload set. |
| 556 | if (isa<FunctionDecl>(*Paths.front().Decls.first)) |
| 557 | return LookupResult(Context, |
| 558 | Paths.front().Decls.first, Paths.front().Decls.second); |
| 559 | |
| 560 | // We found a non-function declaration; return a single declaration. |
| 561 | return LookupResult(Context, *Paths.front().Decls.first); |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /// @brief Performs name lookup for a name that was parsed in the |
| 565 | /// source code, and may contain a C++ scope specifier. |
| 566 | /// |
| 567 | /// This routine is a convenience routine meant to be called from |
| 568 | /// contexts that receive a name and an optional C++ scope specifier |
| 569 | /// (e.g., "N::M::x"). It will then perform either qualified or |
| 570 | /// unqualified name lookup (with LookupQualifiedName or LookupName, |
| 571 | /// respectively) on the given name and return those results. |
| 572 | /// |
| 573 | /// @param S The scope from which unqualified name lookup will |
| 574 | /// begin. |
| 575 | /// |
| 576 | /// @param SS An optional C++ scope-specified, e.g., "::N::M". |
| 577 | /// |
| 578 | /// @param Name The name of the entity that name lookup will |
| 579 | /// search for. |
| 580 | /// |
| 581 | /// @param Criteria The criteria that will determine which entities |
| 582 | /// are visible to name lookup. |
| 583 | /// |
| 584 | /// @returns The result of qualified or unqualified name lookup. |
| 585 | Sema::LookupResult |
| 586 | Sema::LookupParsedName(Scope *S, const CXXScopeSpec &SS, |
| 587 | DeclarationName Name, LookupCriteria Criteria) { |
| 588 | if (SS.isSet()) |
| 589 | return LookupQualifiedName(static_cast<DeclContext *>(SS.getScopeRep()), |
| 590 | Name, Criteria); |
| 591 | |
| 592 | return LookupName(S, Name, Criteria); |
| 593 | } |
| 594 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 595 | /// @brief Produce a diagnostic describing the ambiguity that resulted |
| 596 | /// from name lookup. |
| 597 | /// |
| 598 | /// @param Result The ambiguous name lookup result. |
| 599 | /// |
| 600 | /// @param Name The name of the entity that name lookup was |
| 601 | /// searching for. |
| 602 | /// |
| 603 | /// @param NameLoc The location of the name within the source code. |
| 604 | /// |
| 605 | /// @param LookupRange A source range that provides more |
| 606 | /// source-location information concerning the lookup itself. For |
| 607 | /// example, this range might highlight a nested-name-specifier that |
| 608 | /// precedes the name. |
| 609 | /// |
| 610 | /// @returns true |
| 611 | bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name, |
| 612 | SourceLocation NameLoc, |
| 613 | SourceRange LookupRange) { |
| 614 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous"); |
| 615 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 616 | BasePaths *Paths = Result.getBasePaths(); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 617 | if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 618 | QualType SubobjectType = Paths->front().back().Base->getType(); |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 619 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) |
| 620 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) |
| 621 | << LookupRange; |
| 622 | |
| 623 | DeclContext::lookup_iterator Found = Paths->front().Decls.first; |
| 624 | while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic()) |
| 625 | ++Found; |
| 626 | |
| 627 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame^] | 628 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 629 | return true; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes && |
| 633 | "Unhandled form of name lookup ambiguity"); |
| 634 | |
| 635 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) |
| 636 | << Name << LookupRange; |
| 637 | |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 638 | std::set<ScopedDecl *> DeclsPrinted; |
| 639 | for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end(); |
| 640 | Path != PathEnd; ++Path) { |
| 641 | ScopedDecl *D = *Path->Decls.first; |
| 642 | if (DeclsPrinted.insert(D).second) |
| 643 | Diag(D->getLocation(), diag::note_ambiguous_member_found); |
| 644 | } |
| 645 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 646 | return true; |
| 647 | } |