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