blob: 7353bc37e4b5be8cc01ee2610c8088924605ef87 [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.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000056 Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(),
Douglas Gregor78d70132009-01-14 22:20:51 +000057 (*I)->getDeclName());
58 Ovl->addOverload(cast<FunctionDecl>(*I));
59 }
60 Ovl->addOverload(cast<FunctionDecl>(*Last));
61 }
62
63 // If we had more than one function, we built an overload
64 // set. Return it.
65 if (Ovl)
66 return Ovl;
67 }
68
69 return *I;
70}
71
72/// @brief Constructs name lookup criteria.
73///
74/// @param K The kind of name that we're searching for.
75///
76/// @param RedeclarationOnly If true, then name lookup will only look
77/// into the current scope for names, not in parent scopes. This
78/// option should be set when we're looking to introduce a new
79/// declaration into scope.
80///
81/// @param CPlusPlus Whether we are performing C++ name lookup or not.
82Sema::LookupCriteria::LookupCriteria(NameKind K, bool RedeclarationOnly,
83 bool CPlusPlus)
84 : Kind(K), AllowLazyBuiltinCreation(K == Ordinary),
85 RedeclarationOnly(RedeclarationOnly) {
86 switch (Kind) {
87 case Ordinary:
88 IDNS = Decl::IDNS_Ordinary;
89 if (CPlusPlus)
90 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member;
91 break;
92
93 case Tag:
94 IDNS = Decl::IDNS_Tag;
95 break;
96
97 case Member:
98 IDNS = Decl::IDNS_Member;
99 if (CPlusPlus)
100 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
101 break;
102
103 case NestedNameSpecifier:
104 case Namespace:
105 IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member;
106 break;
107 }
108}
109
110/// isLookupResult - Determines whether D is a suitable lookup result
111/// according to the lookup criteria.
112bool Sema::LookupCriteria::isLookupResult(Decl *D) const {
113 switch (Kind) {
114 case Ordinary:
115 case Tag:
116 case Member:
117 return D->isInIdentifierNamespace(IDNS);
118
119 case NestedNameSpecifier:
120 return isa<TypedefDecl>(D) || D->isInIdentifierNamespace(Decl::IDNS_Tag);
121
122 case Namespace:
123 return isa<NamespaceDecl>(D);
124 }
125
126 assert(false && "isLookupResult always returns before this point");
127 return false;
128}
129
Douglas Gregor6beddfe2009-01-15 02:19:31 +0000130/// @brief Moves the name-lookup results from Other to this LookupResult.
Douglas Gregord07474b2009-01-17 01:13:24 +0000131Sema::LookupResult
132Sema::LookupResult::CreateLookupResult(ASTContext &Context,
133 IdentifierResolver::iterator F,
134 IdentifierResolver::iterator L) {
135 LookupResult Result;
136 Result.Context = &Context;
137
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000138 if (F != L && isa<FunctionDecl>(*F)) {
139 IdentifierResolver::iterator Next = F;
140 ++Next;
141 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000142 Result.StoredKind = OverloadedDeclFromIdResolver;
143 Result.First = F.getAsOpaqueValue();
144 Result.Last = L.getAsOpaqueValue();
145 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000146 }
147 }
148
Douglas Gregord07474b2009-01-17 01:13:24 +0000149 Result.StoredKind = SingleDecl;
150 Result.First = reinterpret_cast<uintptr_t>(*F);
151 Result.Last = 0;
152 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000153}
154
Douglas Gregord07474b2009-01-17 01:13:24 +0000155Sema::LookupResult
156Sema::LookupResult::CreateLookupResult(ASTContext &Context,
157 DeclContext::lookup_iterator F,
158 DeclContext::lookup_iterator L) {
159 LookupResult Result;
160 Result.Context = &Context;
161
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000162 if (F != L && isa<FunctionDecl>(*F)) {
163 DeclContext::lookup_iterator Next = F;
164 ++Next;
165 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000166 Result.StoredKind = OverloadedDeclFromDeclContext;
167 Result.First = reinterpret_cast<uintptr_t>(F);
168 Result.Last = reinterpret_cast<uintptr_t>(L);
169 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000170 }
171 }
172
Douglas Gregord07474b2009-01-17 01:13:24 +0000173 Result.StoredKind = SingleDecl;
174 Result.First = reinterpret_cast<uintptr_t>(*F);
175 Result.Last = 0;
176 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000177}
178
Douglas Gregor78d70132009-01-14 22:20:51 +0000179/// @brief Determine the result of name lookup.
180Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const {
181 switch (StoredKind) {
182 case SingleDecl:
183 return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound;
184
185 case OverloadedDeclFromIdResolver:
186 case OverloadedDeclFromDeclContext:
187 return FoundOverloaded;
188
189 case AmbiguousLookup:
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000190 return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects;
Douglas Gregor78d70132009-01-14 22:20:51 +0000191 }
192
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000193 // We can't ever get here.
194 return NotFound;
Douglas Gregor78d70132009-01-14 22:20:51 +0000195}
196
197/// @brief Converts the result of name lookup into a single (possible
198/// NULL) pointer to a declaration.
199///
200/// The resulting declaration will either be the declaration we found
201/// (if only a single declaration was found), an
202/// OverloadedFunctionDecl (if an overloaded function was found), or
203/// NULL (if no declaration was found). This conversion must not be
204/// used anywhere where name lookup could result in an ambiguity.
205///
206/// The OverloadedFunctionDecl conversion is meant as a stop-gap
207/// solution, since it causes the OverloadedFunctionDecl to be
208/// leaked. FIXME: Eventually, there will be a better way to iterate
209/// over the set of overloaded functions returned by name lookup.
210Decl *Sema::LookupResult::getAsDecl() const {
211 switch (StoredKind) {
212 case SingleDecl:
213 return reinterpret_cast<Decl *>(First);
214
215 case OverloadedDeclFromIdResolver:
216 return MaybeConstructOverloadSet(*Context,
217 IdentifierResolver::iterator::getFromOpaqueValue(First),
218 IdentifierResolver::iterator::getFromOpaqueValue(Last));
219
220 case OverloadedDeclFromDeclContext:
221 return MaybeConstructOverloadSet(*Context,
222 reinterpret_cast<DeclContext::lookup_iterator>(First),
223 reinterpret_cast<DeclContext::lookup_iterator>(Last));
224
225 case AmbiguousLookup:
226 assert(false &&
227 "Name lookup returned an ambiguity that could not be handled");
228 break;
229 }
230
231 return 0;
232}
233
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000234/// @brief Retrieves the BasePaths structure describing an ambiguous
235/// name lookup.
236BasePaths *Sema::LookupResult::getBasePaths() const {
237 assert((StoredKind == AmbiguousLookup) &&
238 "getBasePaths can only be used on an ambiguous lookup");
239 return reinterpret_cast<BasePaths *>(First);
240}
241
Douglas Gregor78d70132009-01-14 22:20:51 +0000242/// @brief Perform unqualified name lookup starting from a given
243/// scope.
244///
245/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
246/// used to find names within the current scope. For example, 'x' in
247/// @code
248/// int x;
249/// int f() {
250/// return x; // unqualified name look finds 'x' in the global scope
251/// }
252/// @endcode
253///
254/// Different lookup criteria can find different names. For example, a
255/// particular scope can have both a struct and a function of the same
256/// name, and each can be found by certain lookup criteria. For more
257/// information about lookup criteria, see the documentation for the
258/// class LookupCriteria.
259///
260/// @param S The scope from which unqualified name lookup will
261/// begin. If the lookup criteria permits, name lookup may also search
262/// in the parent scopes.
263///
264/// @param Name The name of the entity that we are searching for.
265///
266/// @param Criteria The criteria that this routine will use to
267/// determine which names are visible and which names will be
268/// found. Note that name lookup will find a name that is visible by
269/// the given criteria, but the entity itself may not be semantically
270/// correct or even the kind of entity expected based on the
271/// lookup. For example, searching for a nested-name-specifier name
272/// might result in an EnumDecl, which is visible but is not permitted
273/// as a nested-name-specifier in C++03.
274///
275/// @returns The result of name lookup, which includes zero or more
276/// declarations and possibly additional information used to diagnose
277/// ambiguities.
278Sema::LookupResult
279Sema::LookupName(Scope *S, DeclarationName Name, LookupCriteria Criteria) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000280 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000281
282 if (!getLangOptions().CPlusPlus) {
283 // Unqualified name lookup in C/Objective-C is purely lexical, so
284 // search in the declarations attached to the name.
285
286 // For the purposes of unqualified name lookup, structs and unions
287 // don't have scopes at all. For example:
288 //
289 // struct X {
290 // struct T { int i; } x;
291 // };
292 //
293 // void f() {
294 // struct T t; // okay: T is defined lexically within X, but
295 // // semantically at global scope
296 // };
297 //
298 // FIXME: Is there a better way to deal with this?
299 DeclContext *SearchCtx = CurContext;
300 while (isa<RecordDecl>(SearchCtx) || isa<EnumDecl>(SearchCtx))
301 SearchCtx = SearchCtx->getParent();
302 IdentifierResolver::iterator I
303 = IdResolver.begin(Name, SearchCtx, !Criteria.RedeclarationOnly);
304
305 // Scan up the scope chain looking for a decl that matches this
306 // identifier that is in the appropriate namespace. This search
307 // should not take long, as shadowing of names is uncommon, and
308 // deep shadowing is extremely uncommon.
309 for (; I != IdResolver.end(); ++I)
310 if (Criteria.isLookupResult(*I))
Douglas Gregord07474b2009-01-17 01:13:24 +0000311 return LookupResult::CreateLookupResult(Context, *I);
Douglas Gregor78d70132009-01-14 22:20:51 +0000312 } else {
313 // Unqualified name lookup in C++ requires looking into scopes
314 // that aren't strictly lexical, and therefore we walk through the
315 // context as well as walking through the scopes.
316
317 // FIXME: does "true" for LookInParentCtx actually make sense?
318 IdentifierResolver::iterator
319 I = IdResolver.begin(Name, CurContext, true/*LookInParentCtx*/),
320 IEnd = IdResolver.end();
321 for (; S; S = S->getParent()) {
322 // Check whether the IdResolver has anything in this scope.
323 for (; I != IEnd && S->isDeclScope(*I); ++I) {
324 if (Criteria.isLookupResult(*I)) {
325 // We found something. Look for anything else in our scope
326 // with this same name and in an acceptable identifier
327 // namespace, so that we can construct an overload set if we
328 // need to.
329 IdentifierResolver::iterator LastI = I;
330 for (++LastI; LastI != IEnd; ++LastI) {
331 if (!S->isDeclScope(*LastI))
332 break;
333 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000334 return LookupResult::CreateLookupResult(Context, I, LastI);
Douglas Gregor78d70132009-01-14 22:20:51 +0000335 }
336 }
337
338 // If there is an entity associated with this scope, it's a
339 // DeclContext. We might need to perform qualified lookup into
340 // it.
341 // FIXME: We're performing redundant lookups here, where the
342 // scope stack mirrors the semantic nested of classes and
343 // namespaces. We can save some work by checking the lexical
344 // scope against the semantic scope and avoiding any lookups
345 // when they are the same.
346 // FIXME: In some cases, we know that every name that could be
347 // found by this qualified name lookup will also be on the
348 // identifier chain. For example, inside a class without any
349 // base classes, we never need to perform qualified lookup
350 // because all of the members are on top of the identifier
351 // chain. However, we cannot perform this optimization when the
352 // lexical and semantic scopes don't line up, e.g., in an
353 // out-of-line member definition.
354 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
355 while (Ctx && Ctx->isFunctionOrMethod())
356 Ctx = Ctx->getParent();
357 while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) {
358 // Look for declarations of this name in this scope.
359 if (LookupResult Result = LookupQualifiedName(Ctx, Name, Criteria))
360 return Result;
361
362 if (Criteria.RedeclarationOnly && !Ctx->isTransparentContext())
Douglas Gregord07474b2009-01-17 01:13:24 +0000363 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000364
365 Ctx = Ctx->getParent();
366 }
367 }
368 }
369
370 // If we didn't find a use of this identifier, and if the identifier
371 // corresponds to a compiler builtin, create the decl object for the builtin
372 // now, injecting it into translation unit scope, and return it.
373 if (Criteria.Kind == LookupCriteria::Ordinary) {
374 IdentifierInfo *II = Name.getAsIdentifierInfo();
375 if (Criteria.AllowLazyBuiltinCreation && II) {
376 // If this is a builtin on this (or all) targets, create the decl.
377 if (unsigned BuiltinID = II->getBuiltinID())
Douglas Gregord07474b2009-01-17 01:13:24 +0000378 return LookupResult::CreateLookupResult(Context,
Douglas Gregor78d70132009-01-14 22:20:51 +0000379 LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
380 S));
381 }
382 if (getLangOptions().ObjC1 && II) {
383 // @interface and @compatibility_alias introduce typedef-like names.
384 // Unlike typedef's, they can only be introduced at file-scope (and are
385 // therefore not scoped decls). They can, however, be shadowed by
386 // other names in IDNS_Ordinary.
387 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
388 if (IDI != ObjCInterfaceDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000389 return LookupResult::CreateLookupResult(Context, IDI->second);
Douglas Gregor78d70132009-01-14 22:20:51 +0000390 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
391 if (I != ObjCAliasDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000392 return LookupResult::CreateLookupResult(Context,
393 I->second->getClassInterface());
Douglas Gregor78d70132009-01-14 22:20:51 +0000394 }
395 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000396 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000397}
398
399/// @brief Perform qualified name lookup into a given context.
400///
401/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
402/// names when the context of those names is explicit specified, e.g.,
403/// "std::vector" or "x->member".
404///
405/// Different lookup criteria can find different names. For example, a
406/// particular scope can have both a struct and a function of the same
407/// name, and each can be found by certain lookup criteria. For more
408/// information about lookup criteria, see the documentation for the
409/// class LookupCriteria.
410///
411/// @param LookupCtx The context in which qualified name lookup will
412/// search. If the lookup criteria permits, name lookup may also search
413/// in the parent contexts or (for C++ classes) base classes.
414///
415/// @param Name The name of the entity that we are searching for.
416///
417/// @param Criteria The criteria that this routine will use to
418/// determine which names are visible and which names will be
419/// found. Note that name lookup will find a name that is visible by
420/// the given criteria, but the entity itself may not be semantically
421/// correct or even the kind of entity expected based on the
422/// lookup. For example, searching for a nested-name-specifier name
423/// might result in an EnumDecl, which is visible but is not permitted
424/// as a nested-name-specifier in C++03.
425///
426/// @returns The result of name lookup, which includes zero or more
427/// declarations and possibly additional information used to diagnose
428/// ambiguities.
429Sema::LookupResult
430Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name,
431 LookupCriteria Criteria) {
432 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
433
Douglas Gregord07474b2009-01-17 01:13:24 +0000434 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000435
436 // If we're performing qualified name lookup (e.g., lookup into a
437 // struct), find fields as part of ordinary name lookup.
438 if (Criteria.Kind == LookupCriteria::Ordinary)
439 Criteria.IDNS |= Decl::IDNS_Member;
440
441 // Perform qualified name lookup into the LookupCtx.
Douglas Gregor78d70132009-01-14 22:20:51 +0000442 DeclContext::lookup_iterator I, E;
443 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
444 if (Criteria.isLookupResult(*I))
Douglas Gregord07474b2009-01-17 01:13:24 +0000445 return LookupResult::CreateLookupResult(Context, I, E);
Douglas Gregor78d70132009-01-14 22:20:51 +0000446
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000447 // If this isn't a C++ class or we aren't allowed to look into base
448 // classes, we're done.
449 if (Criteria.RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx))
Douglas Gregord07474b2009-01-17 01:13:24 +0000450 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000451
452 // Perform lookup into our base classes.
453 BasePaths Paths;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000454 Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx)));
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000455
456 // Look for this member in our base classes
457 if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx),
458 MemberLookupCriteria(Name, Criteria), Paths))
Douglas Gregord07474b2009-01-17 01:13:24 +0000459 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000460
461 // C++ [class.member.lookup]p2:
462 // [...] If the resulting set of declarations are not all from
463 // sub-objects of the same type, or the set has a nonstatic member
464 // and includes members from distinct sub-objects, there is an
465 // ambiguity and the program is ill-formed. Otherwise that set is
466 // the result of the lookup.
467 // FIXME: support using declarations!
468 QualType SubobjectType;
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000469 int SubobjectNumber = 0;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000470 for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
471 Path != PathEnd; ++Path) {
472 const BasePathElement &PathElement = Path->back();
473
474 // Determine whether we're looking at a distinct sub-object or not.
475 if (SubobjectType.isNull()) {
476 // This is the first subobject we've looked at. Record it's type.
477 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
478 SubobjectNumber = PathElement.SubobjectNumber;
479 } else if (SubobjectType
480 != Context.getCanonicalType(PathElement.Base->getType())) {
481 // We found members of the given name in two subobjects of
482 // different types. This lookup is ambiguous.
483 BasePaths *PathsOnHeap = new BasePaths;
484 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000485 return LookupResult::CreateLookupResult(Context, PathsOnHeap, true);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000486 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
487 // We have a different subobject of the same type.
488
489 // C++ [class.member.lookup]p5:
490 // A static member, a nested type or an enumerator defined in
491 // a base class T can unambiguously be found even if an object
492 // has more than one base class subobject of type T.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000493 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000494 if (isa<VarDecl>(FirstDecl) ||
495 isa<TypeDecl>(FirstDecl) ||
496 isa<EnumConstantDecl>(FirstDecl))
497 continue;
498
499 if (isa<CXXMethodDecl>(FirstDecl)) {
500 // Determine whether all of the methods are static.
501 bool AllMethodsAreStatic = true;
502 for (DeclContext::lookup_iterator Func = Path->Decls.first;
503 Func != Path->Decls.second; ++Func) {
504 if (!isa<CXXMethodDecl>(*Func)) {
505 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
506 break;
507 }
508
509 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
510 AllMethodsAreStatic = false;
511 break;
512 }
513 }
514
515 if (AllMethodsAreStatic)
516 continue;
517 }
518
519 // We have found a nonstatic member name in multiple, distinct
520 // subobjects. Name lookup is ambiguous.
521 BasePaths *PathsOnHeap = new BasePaths;
522 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000523 return LookupResult::CreateLookupResult(Context, PathsOnHeap, false);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000524 }
525 }
526
527 // Lookup in a base class succeeded; return these results.
528
529 // If we found a function declaration, return an overload set.
530 if (isa<FunctionDecl>(*Paths.front().Decls.first))
Douglas Gregord07474b2009-01-17 01:13:24 +0000531 return LookupResult::CreateLookupResult(Context,
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000532 Paths.front().Decls.first, Paths.front().Decls.second);
533
534 // We found a non-function declaration; return a single declaration.
Douglas Gregord07474b2009-01-17 01:13:24 +0000535 return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first);
Douglas Gregor78d70132009-01-14 22:20:51 +0000536}
537
538/// @brief Performs name lookup for a name that was parsed in the
539/// source code, and may contain a C++ scope specifier.
540///
541/// This routine is a convenience routine meant to be called from
542/// contexts that receive a name and an optional C++ scope specifier
543/// (e.g., "N::M::x"). It will then perform either qualified or
544/// unqualified name lookup (with LookupQualifiedName or LookupName,
545/// respectively) on the given name and return those results.
546///
547/// @param S The scope from which unqualified name lookup will
548/// begin.
549///
550/// @param SS An optional C++ scope-specified, e.g., "::N::M".
551///
552/// @param Name The name of the entity that name lookup will
553/// search for.
554///
555/// @param Criteria The criteria that will determine which entities
556/// are visible to name lookup.
557///
558/// @returns The result of qualified or unqualified name lookup.
559Sema::LookupResult
560Sema::LookupParsedName(Scope *S, const CXXScopeSpec &SS,
561 DeclarationName Name, LookupCriteria Criteria) {
562 if (SS.isSet())
563 return LookupQualifiedName(static_cast<DeclContext *>(SS.getScopeRep()),
564 Name, Criteria);
565
566 return LookupName(S, Name, Criteria);
567}
568
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000569/// @brief Produce a diagnostic describing the ambiguity that resulted
570/// from name lookup.
571///
572/// @param Result The ambiguous name lookup result.
573///
574/// @param Name The name of the entity that name lookup was
575/// searching for.
576///
577/// @param NameLoc The location of the name within the source code.
578///
579/// @param LookupRange A source range that provides more
580/// source-location information concerning the lookup itself. For
581/// example, this range might highlight a nested-name-specifier that
582/// precedes the name.
583///
584/// @returns true
585bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name,
586 SourceLocation NameLoc,
587 SourceRange LookupRange) {
588 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
589
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000590 BasePaths *Paths = Result.getBasePaths();
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000591 if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) {
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000592 QualType SubobjectType = Paths->front().back().Base->getType();
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000593 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
594 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
595 << LookupRange;
596
597 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
598 while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic())
599 ++Found;
600
601 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
Douglas Gregor98b27542009-01-17 00:42:38 +0000602
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000603 return true;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000604 }
605
606 assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes &&
607 "Unhandled form of name lookup ambiguity");
608
609 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
610 << Name << LookupRange;
611
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000612 std::set<Decl *> DeclsPrinted;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000613 for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
614 Path != PathEnd; ++Path) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000615 Decl *D = *Path->Decls.first;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000616 if (DeclsPrinted.insert(D).second)
617 Diag(D->getLocation(), diag::note_ambiguous_member_found);
618 }
619
Douglas Gregord07474b2009-01-17 01:13:24 +0000620 delete Paths;
621
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000622 return true;
623}