blob: 5f8f3f5f6ad6b2941c7b8120e5a170c1e4d9ad16 [file] [log] [blame]
Douglas Gregor78d70132009-01-14 22:20:51 +00001//===--------------------- 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 Gregor29dfa2f2009-01-15 00:26:24 +000015#include "SemaInherit.h"
16#include "clang/AST/ASTContext.h"
Douglas Gregor78d70132009-01-14 22:20:51 +000017#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 Gregorb9ef0552009-01-16 00:38:09 +000023#include <set>
Douglas Gregor78d70132009-01-14 22:20:51 +000024
25using 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.
38template<typename DeclIterator>
39static Decl *
40MaybeConstructOverloadSet(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.
83Sema::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.
113bool 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 Gregor6beddfe2009-01-15 02:19:31 +0000131/// @brief Moves the name-lookup results from Other to this LookupResult.
Douglas Gregord07474b2009-01-17 01:13:24 +0000132Sema::LookupResult
133Sema::LookupResult::CreateLookupResult(ASTContext &Context,
134 IdentifierResolver::iterator F,
135 IdentifierResolver::iterator L) {
136 LookupResult Result;
137 Result.Context = &Context;
138
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000139 if (F != L && isa<FunctionDecl>(*F)) {
140 IdentifierResolver::iterator Next = F;
141 ++Next;
142 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000143 Result.StoredKind = OverloadedDeclFromIdResolver;
144 Result.First = F.getAsOpaqueValue();
145 Result.Last = L.getAsOpaqueValue();
146 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000147 }
148 }
149
Douglas Gregord07474b2009-01-17 01:13:24 +0000150 Result.StoredKind = SingleDecl;
151 Result.First = reinterpret_cast<uintptr_t>(*F);
152 Result.Last = 0;
153 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000154}
155
Douglas Gregord07474b2009-01-17 01:13:24 +0000156Sema::LookupResult
157Sema::LookupResult::CreateLookupResult(ASTContext &Context,
158 DeclContext::lookup_iterator F,
159 DeclContext::lookup_iterator L) {
160 LookupResult Result;
161 Result.Context = &Context;
162
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000163 if (F != L && isa<FunctionDecl>(*F)) {
164 DeclContext::lookup_iterator Next = F;
165 ++Next;
166 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000167 Result.StoredKind = OverloadedDeclFromDeclContext;
168 Result.First = reinterpret_cast<uintptr_t>(F);
169 Result.Last = reinterpret_cast<uintptr_t>(L);
170 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000171 }
172 }
173
Douglas Gregord07474b2009-01-17 01:13:24 +0000174 Result.StoredKind = SingleDecl;
175 Result.First = reinterpret_cast<uintptr_t>(*F);
176 Result.Last = 0;
177 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000178}
179
Douglas Gregor78d70132009-01-14 22:20:51 +0000180/// @brief Determine the result of name lookup.
181Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const {
182 switch (StoredKind) {
183 case SingleDecl:
184 return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound;
185
186 case OverloadedDeclFromIdResolver:
187 case OverloadedDeclFromDeclContext:
188 return FoundOverloaded;
189
190 case AmbiguousLookup:
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000191 return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects;
Douglas Gregor78d70132009-01-14 22:20:51 +0000192 }
193
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000194 // We can't ever get here.
195 return NotFound;
Douglas Gregor78d70132009-01-14 22:20:51 +0000196}
197
198/// @brief Converts the result of name lookup into a single (possible
199/// NULL) pointer to a declaration.
200///
201/// The resulting declaration will either be the declaration we found
202/// (if only a single declaration was found), an
203/// OverloadedFunctionDecl (if an overloaded function was found), or
204/// NULL (if no declaration was found). This conversion must not be
205/// used anywhere where name lookup could result in an ambiguity.
206///
207/// The OverloadedFunctionDecl conversion is meant as a stop-gap
208/// solution, since it causes the OverloadedFunctionDecl to be
209/// leaked. FIXME: Eventually, there will be a better way to iterate
210/// over the set of overloaded functions returned by name lookup.
211Decl *Sema::LookupResult::getAsDecl() const {
212 switch (StoredKind) {
213 case SingleDecl:
214 return reinterpret_cast<Decl *>(First);
215
216 case OverloadedDeclFromIdResolver:
217 return MaybeConstructOverloadSet(*Context,
218 IdentifierResolver::iterator::getFromOpaqueValue(First),
219 IdentifierResolver::iterator::getFromOpaqueValue(Last));
220
221 case OverloadedDeclFromDeclContext:
222 return MaybeConstructOverloadSet(*Context,
223 reinterpret_cast<DeclContext::lookup_iterator>(First),
224 reinterpret_cast<DeclContext::lookup_iterator>(Last));
225
226 case AmbiguousLookup:
227 assert(false &&
228 "Name lookup returned an ambiguity that could not be handled");
229 break;
230 }
231
232 return 0;
233}
234
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000235/// @brief Retrieves the BasePaths structure describing an ambiguous
236/// name lookup.
237BasePaths *Sema::LookupResult::getBasePaths() const {
238 assert((StoredKind == AmbiguousLookup) &&
239 "getBasePaths can only be used on an ambiguous lookup");
240 return reinterpret_cast<BasePaths *>(First);
241}
242
Douglas Gregor78d70132009-01-14 22:20:51 +0000243/// @brief Perform unqualified name lookup starting from a given
244/// scope.
245///
246/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
247/// used to find names within the current scope. For example, 'x' in
248/// @code
249/// int x;
250/// int f() {
251/// return x; // unqualified name look finds 'x' in the global scope
252/// }
253/// @endcode
254///
255/// Different lookup criteria can find different names. For example, a
256/// particular scope can have both a struct and a function of the same
257/// name, and each can be found by certain lookup criteria. For more
258/// information about lookup criteria, see the documentation for the
259/// class LookupCriteria.
260///
261/// @param S The scope from which unqualified name lookup will
262/// begin. If the lookup criteria permits, name lookup may also search
263/// in the parent scopes.
264///
265/// @param Name The name of the entity that we are searching for.
266///
267/// @param Criteria The criteria that this routine will use to
268/// determine which names are visible and which names will be
269/// found. Note that name lookup will find a name that is visible by
270/// the given criteria, but the entity itself may not be semantically
271/// correct or even the kind of entity expected based on the
272/// lookup. For example, searching for a nested-name-specifier name
273/// might result in an EnumDecl, which is visible but is not permitted
274/// as a nested-name-specifier in C++03.
275///
276/// @returns The result of name lookup, which includes zero or more
277/// declarations and possibly additional information used to diagnose
278/// ambiguities.
279Sema::LookupResult
280Sema::LookupName(Scope *S, DeclarationName Name, LookupCriteria Criteria) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000281 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000282
283 if (!getLangOptions().CPlusPlus) {
284 // Unqualified name lookup in C/Objective-C is purely lexical, so
285 // search in the declarations attached to the name.
286
287 // For the purposes of unqualified name lookup, structs and unions
288 // don't have scopes at all. For example:
289 //
290 // struct X {
291 // struct T { int i; } x;
292 // };
293 //
294 // void f() {
295 // struct T t; // okay: T is defined lexically within X, but
296 // // semantically at global scope
297 // };
298 //
299 // FIXME: Is there a better way to deal with this?
300 DeclContext *SearchCtx = CurContext;
301 while (isa<RecordDecl>(SearchCtx) || isa<EnumDecl>(SearchCtx))
302 SearchCtx = SearchCtx->getParent();
303 IdentifierResolver::iterator I
304 = IdResolver.begin(Name, SearchCtx, !Criteria.RedeclarationOnly);
305
306 // Scan up the scope chain looking for a decl that matches this
307 // identifier that is in the appropriate namespace. This search
308 // should not take long, as shadowing of names is uncommon, and
309 // deep shadowing is extremely uncommon.
310 for (; I != IdResolver.end(); ++I)
311 if (Criteria.isLookupResult(*I))
Douglas Gregord07474b2009-01-17 01:13:24 +0000312 return LookupResult::CreateLookupResult(Context, *I);
Douglas Gregor78d70132009-01-14 22:20:51 +0000313 } else {
314 // Unqualified name lookup in C++ requires looking into scopes
315 // that aren't strictly lexical, and therefore we walk through the
316 // context as well as walking through the scopes.
317
318 // FIXME: does "true" for LookInParentCtx actually make sense?
319 IdentifierResolver::iterator
320 I = IdResolver.begin(Name, CurContext, true/*LookInParentCtx*/),
321 IEnd = IdResolver.end();
322 for (; S; S = S->getParent()) {
323 // Check whether the IdResolver has anything in this scope.
324 for (; I != IEnd && S->isDeclScope(*I); ++I) {
325 if (Criteria.isLookupResult(*I)) {
326 // We found something. Look for anything else in our scope
327 // with this same name and in an acceptable identifier
328 // namespace, so that we can construct an overload set if we
329 // need to.
330 IdentifierResolver::iterator LastI = I;
331 for (++LastI; LastI != IEnd; ++LastI) {
332 if (!S->isDeclScope(*LastI))
333 break;
334 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000335 return LookupResult::CreateLookupResult(Context, I, LastI);
Douglas Gregor78d70132009-01-14 22:20:51 +0000336 }
337 }
338
339 // If there is an entity associated with this scope, it's a
340 // DeclContext. We might need to perform qualified lookup into
341 // it.
342 // FIXME: We're performing redundant lookups here, where the
343 // scope stack mirrors the semantic nested of classes and
344 // namespaces. We can save some work by checking the lexical
345 // scope against the semantic scope and avoiding any lookups
346 // when they are the same.
347 // FIXME: In some cases, we know that every name that could be
348 // found by this qualified name lookup will also be on the
349 // identifier chain. For example, inside a class without any
350 // base classes, we never need to perform qualified lookup
351 // because all of the members are on top of the identifier
352 // chain. However, we cannot perform this optimization when the
353 // lexical and semantic scopes don't line up, e.g., in an
354 // out-of-line member definition.
355 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
356 while (Ctx && Ctx->isFunctionOrMethod())
357 Ctx = Ctx->getParent();
358 while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) {
359 // Look for declarations of this name in this scope.
360 if (LookupResult Result = LookupQualifiedName(Ctx, Name, Criteria))
361 return Result;
362
363 if (Criteria.RedeclarationOnly && !Ctx->isTransparentContext())
Douglas Gregord07474b2009-01-17 01:13:24 +0000364 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000365
366 Ctx = Ctx->getParent();
367 }
368 }
369 }
370
371 // If we didn't find a use of this identifier, and if the identifier
372 // corresponds to a compiler builtin, create the decl object for the builtin
373 // now, injecting it into translation unit scope, and return it.
374 if (Criteria.Kind == LookupCriteria::Ordinary) {
375 IdentifierInfo *II = Name.getAsIdentifierInfo();
376 if (Criteria.AllowLazyBuiltinCreation && II) {
377 // If this is a builtin on this (or all) targets, create the decl.
378 if (unsigned BuiltinID = II->getBuiltinID())
Douglas Gregord07474b2009-01-17 01:13:24 +0000379 return LookupResult::CreateLookupResult(Context,
Douglas Gregor78d70132009-01-14 22:20:51 +0000380 LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
381 S));
382 }
383 if (getLangOptions().ObjC1 && II) {
384 // @interface and @compatibility_alias introduce typedef-like names.
385 // Unlike typedef's, they can only be introduced at file-scope (and are
386 // therefore not scoped decls). They can, however, be shadowed by
387 // other names in IDNS_Ordinary.
388 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
389 if (IDI != ObjCInterfaceDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000390 return LookupResult::CreateLookupResult(Context, IDI->second);
Douglas Gregor78d70132009-01-14 22:20:51 +0000391 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
392 if (I != ObjCAliasDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000393 return LookupResult::CreateLookupResult(Context,
394 I->second->getClassInterface());
Douglas Gregor78d70132009-01-14 22:20:51 +0000395 }
396 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000397 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000398}
399
400/// @brief Perform qualified name lookup into a given context.
401///
402/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
403/// names when the context of those names is explicit specified, e.g.,
404/// "std::vector" or "x->member".
405///
406/// Different lookup criteria can find different names. For example, a
407/// particular scope can have both a struct and a function of the same
408/// name, and each can be found by certain lookup criteria. For more
409/// information about lookup criteria, see the documentation for the
410/// class LookupCriteria.
411///
412/// @param LookupCtx The context in which qualified name lookup will
413/// search. If the lookup criteria permits, name lookup may also search
414/// in the parent contexts or (for C++ classes) base classes.
415///
416/// @param Name The name of the entity that we are searching for.
417///
418/// @param Criteria The criteria that this routine will use to
419/// determine which names are visible and which names will be
420/// found. Note that name lookup will find a name that is visible by
421/// the given criteria, but the entity itself may not be semantically
422/// correct or even the kind of entity expected based on the
423/// lookup. For example, searching for a nested-name-specifier name
424/// might result in an EnumDecl, which is visible but is not permitted
425/// as a nested-name-specifier in C++03.
426///
427/// @returns The result of name lookup, which includes zero or more
428/// declarations and possibly additional information used to diagnose
429/// ambiguities.
430Sema::LookupResult
431Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name,
432 LookupCriteria Criteria) {
433 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
434
Douglas Gregord07474b2009-01-17 01:13:24 +0000435 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000436
437 // If we're performing qualified name lookup (e.g., lookup into a
438 // struct), find fields as part of ordinary name lookup.
439 if (Criteria.Kind == LookupCriteria::Ordinary)
440 Criteria.IDNS |= Decl::IDNS_Member;
441
442 // Perform qualified name lookup into the LookupCtx.
Douglas Gregor78d70132009-01-14 22:20:51 +0000443 DeclContext::lookup_iterator I, E;
444 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
445 if (Criteria.isLookupResult(*I))
Douglas Gregord07474b2009-01-17 01:13:24 +0000446 return LookupResult::CreateLookupResult(Context, I, E);
Douglas Gregor78d70132009-01-14 22:20:51 +0000447
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000448 // If this isn't a C++ class or we aren't allowed to look into base
449 // classes, we're done.
450 if (Criteria.RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx))
Douglas Gregord07474b2009-01-17 01:13:24 +0000451 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000452
453 // Perform lookup into our base classes.
454 BasePaths Paths;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000455 Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx)));
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000456
457 // Look for this member in our base classes
458 if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx),
459 MemberLookupCriteria(Name, Criteria), Paths))
Douglas Gregord07474b2009-01-17 01:13:24 +0000460 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000461
462 // C++ [class.member.lookup]p2:
463 // [...] If the resulting set of declarations are not all from
464 // sub-objects of the same type, or the set has a nonstatic member
465 // and includes members from distinct sub-objects, there is an
466 // ambiguity and the program is ill-formed. Otherwise that set is
467 // the result of the lookup.
468 // FIXME: support using declarations!
469 QualType SubobjectType;
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000470 int SubobjectNumber = 0;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000471 for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
472 Path != PathEnd; ++Path) {
473 const BasePathElement &PathElement = Path->back();
474
475 // Determine whether we're looking at a distinct sub-object or not.
476 if (SubobjectType.isNull()) {
477 // This is the first subobject we've looked at. Record it's type.
478 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
479 SubobjectNumber = PathElement.SubobjectNumber;
480 } else if (SubobjectType
481 != Context.getCanonicalType(PathElement.Base->getType())) {
482 // We found members of the given name in two subobjects of
483 // different types. This lookup is ambiguous.
484 BasePaths *PathsOnHeap = new BasePaths;
485 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000486 return LookupResult::CreateLookupResult(Context, PathsOnHeap, true);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000487 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
488 // We have a different subobject of the same type.
489
490 // C++ [class.member.lookup]p5:
491 // A static member, a nested type or an enumerator defined in
492 // a base class T can unambiguously be found even if an object
493 // has more than one base class subobject of type T.
494 ScopedDecl *FirstDecl = *Path->Decls.first;
495 if (isa<VarDecl>(FirstDecl) ||
496 isa<TypeDecl>(FirstDecl) ||
497 isa<EnumConstantDecl>(FirstDecl))
498 continue;
499
500 if (isa<CXXMethodDecl>(FirstDecl)) {
501 // Determine whether all of the methods are static.
502 bool AllMethodsAreStatic = true;
503 for (DeclContext::lookup_iterator Func = Path->Decls.first;
504 Func != Path->Decls.second; ++Func) {
505 if (!isa<CXXMethodDecl>(*Func)) {
506 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
507 break;
508 }
509
510 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
511 AllMethodsAreStatic = false;
512 break;
513 }
514 }
515
516 if (AllMethodsAreStatic)
517 continue;
518 }
519
520 // We have found a nonstatic member name in multiple, distinct
521 // subobjects. Name lookup is ambiguous.
522 BasePaths *PathsOnHeap = new BasePaths;
523 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000524 return LookupResult::CreateLookupResult(Context, PathsOnHeap, false);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000525 }
526 }
527
528 // Lookup in a base class succeeded; return these results.
529
530 // If we found a function declaration, return an overload set.
531 if (isa<FunctionDecl>(*Paths.front().Decls.first))
Douglas Gregord07474b2009-01-17 01:13:24 +0000532 return LookupResult::CreateLookupResult(Context,
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000533 Paths.front().Decls.first, Paths.front().Decls.second);
534
535 // We found a non-function declaration; return a single declaration.
Douglas Gregord07474b2009-01-17 01:13:24 +0000536 return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first);
Douglas Gregor78d70132009-01-14 22:20:51 +0000537}
538
539/// @brief Performs name lookup for a name that was parsed in the
540/// source code, and may contain a C++ scope specifier.
541///
542/// This routine is a convenience routine meant to be called from
543/// contexts that receive a name and an optional C++ scope specifier
544/// (e.g., "N::M::x"). It will then perform either qualified or
545/// unqualified name lookup (with LookupQualifiedName or LookupName,
546/// respectively) on the given name and return those results.
547///
548/// @param S The scope from which unqualified name lookup will
549/// begin.
550///
551/// @param SS An optional C++ scope-specified, e.g., "::N::M".
552///
553/// @param Name The name of the entity that name lookup will
554/// search for.
555///
556/// @param Criteria The criteria that will determine which entities
557/// are visible to name lookup.
558///
559/// @returns The result of qualified or unqualified name lookup.
560Sema::LookupResult
561Sema::LookupParsedName(Scope *S, const CXXScopeSpec &SS,
562 DeclarationName Name, LookupCriteria Criteria) {
563 if (SS.isSet())
564 return LookupQualifiedName(static_cast<DeclContext *>(SS.getScopeRep()),
565 Name, Criteria);
566
567 return LookupName(S, Name, Criteria);
568}
569
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000570/// @brief Produce a diagnostic describing the ambiguity that resulted
571/// from name lookup.
572///
573/// @param Result The ambiguous name lookup result.
574///
575/// @param Name The name of the entity that name lookup was
576/// searching for.
577///
578/// @param NameLoc The location of the name within the source code.
579///
580/// @param LookupRange A source range that provides more
581/// source-location information concerning the lookup itself. For
582/// example, this range might highlight a nested-name-specifier that
583/// precedes the name.
584///
585/// @returns true
586bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name,
587 SourceLocation NameLoc,
588 SourceRange LookupRange) {
589 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
590
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000591 BasePaths *Paths = Result.getBasePaths();
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000592 if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) {
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000593 QualType SubobjectType = Paths->front().back().Base->getType();
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000594 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
595 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
596 << LookupRange;
597
598 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
599 while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic())
600 ++Found;
601
602 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
Douglas Gregor98b27542009-01-17 00:42:38 +0000603
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000604 return true;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000605 }
606
607 assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes &&
608 "Unhandled form of name lookup ambiguity");
609
610 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
611 << Name << LookupRange;
612
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000613 std::set<ScopedDecl *> DeclsPrinted;
614 for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
615 Path != PathEnd; ++Path) {
616 ScopedDecl *D = *Path->Decls.first;
617 if (DeclsPrinted.insert(D).second)
618 Diag(D->getLocation(), diag::note_ambiguous_member_found);
619 }
620
Douglas Gregord07474b2009-01-17 01:13:24 +0000621 delete Paths;
622
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000623 return true;
624}