blob: abb265da49bebda80838cade1f206b1b69af0a3d [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///
Douglas Gregor52ae30c2009-01-30 01:04:22 +000034/// The existance of this routine is temporary; users of LookupResult
35/// should be able to handle multiple results, to deal with cases of
Douglas Gregor78d70132009-01-14 22:20:51 +000036/// 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
Douglas Gregor6beddfe2009-01-15 02:19:31 +000072/// @brief Moves the name-lookup results from Other to this LookupResult.
Douglas Gregord07474b2009-01-17 01:13:24 +000073Sema::LookupResult
74Sema::LookupResult::CreateLookupResult(ASTContext &Context,
75 IdentifierResolver::iterator F,
76 IdentifierResolver::iterator L) {
77 LookupResult Result;
78 Result.Context = &Context;
79
Douglas Gregor29dfa2f2009-01-15 00:26:24 +000080 if (F != L && isa<FunctionDecl>(*F)) {
81 IdentifierResolver::iterator Next = F;
82 ++Next;
83 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +000084 Result.StoredKind = OverloadedDeclFromIdResolver;
85 Result.First = F.getAsOpaqueValue();
86 Result.Last = L.getAsOpaqueValue();
87 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +000088 }
89 }
90
Douglas Gregord07474b2009-01-17 01:13:24 +000091 Result.StoredKind = SingleDecl;
92 Result.First = reinterpret_cast<uintptr_t>(*F);
93 Result.Last = 0;
94 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +000095}
96
Douglas Gregord07474b2009-01-17 01:13:24 +000097Sema::LookupResult
98Sema::LookupResult::CreateLookupResult(ASTContext &Context,
99 DeclContext::lookup_iterator F,
100 DeclContext::lookup_iterator L) {
101 LookupResult Result;
102 Result.Context = &Context;
103
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000104 if (F != L && isa<FunctionDecl>(*F)) {
105 DeclContext::lookup_iterator Next = F;
106 ++Next;
107 if (Next != L && isa<FunctionDecl>(*Next)) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000108 Result.StoredKind = OverloadedDeclFromDeclContext;
109 Result.First = reinterpret_cast<uintptr_t>(F);
110 Result.Last = reinterpret_cast<uintptr_t>(L);
111 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000112 }
113 }
114
Douglas Gregord07474b2009-01-17 01:13:24 +0000115 Result.StoredKind = SingleDecl;
116 Result.First = reinterpret_cast<uintptr_t>(*F);
117 Result.Last = 0;
118 return Result;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000119}
120
Douglas Gregor78d70132009-01-14 22:20:51 +0000121/// @brief Determine the result of name lookup.
122Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const {
123 switch (StoredKind) {
124 case SingleDecl:
125 return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound;
126
127 case OverloadedDeclFromIdResolver:
128 case OverloadedDeclFromDeclContext:
129 return FoundOverloaded;
130
131 case AmbiguousLookup:
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000132 return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects;
Douglas Gregor78d70132009-01-14 22:20:51 +0000133 }
134
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000135 // We can't ever get here.
136 return NotFound;
Douglas Gregor78d70132009-01-14 22:20:51 +0000137}
138
139/// @brief Converts the result of name lookup into a single (possible
140/// NULL) pointer to a declaration.
141///
142/// The resulting declaration will either be the declaration we found
143/// (if only a single declaration was found), an
144/// OverloadedFunctionDecl (if an overloaded function was found), or
145/// NULL (if no declaration was found). This conversion must not be
146/// used anywhere where name lookup could result in an ambiguity.
147///
148/// The OverloadedFunctionDecl conversion is meant as a stop-gap
149/// solution, since it causes the OverloadedFunctionDecl to be
150/// leaked. FIXME: Eventually, there will be a better way to iterate
151/// over the set of overloaded functions returned by name lookup.
152Decl *Sema::LookupResult::getAsDecl() const {
153 switch (StoredKind) {
154 case SingleDecl:
155 return reinterpret_cast<Decl *>(First);
156
157 case OverloadedDeclFromIdResolver:
158 return MaybeConstructOverloadSet(*Context,
159 IdentifierResolver::iterator::getFromOpaqueValue(First),
160 IdentifierResolver::iterator::getFromOpaqueValue(Last));
161
162 case OverloadedDeclFromDeclContext:
163 return MaybeConstructOverloadSet(*Context,
164 reinterpret_cast<DeclContext::lookup_iterator>(First),
165 reinterpret_cast<DeclContext::lookup_iterator>(Last));
166
167 case AmbiguousLookup:
168 assert(false &&
169 "Name lookup returned an ambiguity that could not be handled");
170 break;
171 }
172
173 return 0;
174}
175
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000176/// @brief Retrieves the BasePaths structure describing an ambiguous
177/// name lookup.
178BasePaths *Sema::LookupResult::getBasePaths() const {
179 assert((StoredKind == AmbiguousLookup) &&
180 "getBasePaths can only be used on an ambiguous lookup");
181 return reinterpret_cast<BasePaths *>(First);
182}
183
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000184// Retrieve the set of identifier namespaces that correspond to a
185// specific kind of name lookup.
186inline unsigned
187getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind,
188 bool CPlusPlus) {
189 unsigned IDNS = 0;
190 switch (NameKind) {
191 case Sema::LookupOrdinaryName:
192 IDNS = Decl::IDNS_Ordinary;
193 if (CPlusPlus)
194 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member;
195 break;
196
197 case Sema::LookupTagName:
198 IDNS = Decl::IDNS_Tag;
199 break;
200
201 case Sema::LookupMemberName:
202 IDNS = Decl::IDNS_Member;
203 if (CPlusPlus)
204 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
205 break;
206
207 case Sema::LookupNestedNameSpecifierName:
208 case Sema::LookupNamespaceName:
209 IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member;
210 break;
211 }
212 return IDNS;
213}
214
Douglas Gregor78d70132009-01-14 22:20:51 +0000215/// @brief Perform unqualified name lookup starting from a given
216/// scope.
217///
218/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
219/// used to find names within the current scope. For example, 'x' in
220/// @code
221/// int x;
222/// int f() {
223/// return x; // unqualified name look finds 'x' in the global scope
224/// }
225/// @endcode
226///
227/// Different lookup criteria can find different names. For example, a
228/// particular scope can have both a struct and a function of the same
229/// name, and each can be found by certain lookup criteria. For more
230/// information about lookup criteria, see the documentation for the
231/// class LookupCriteria.
232///
233/// @param S The scope from which unqualified name lookup will
234/// begin. If the lookup criteria permits, name lookup may also search
235/// in the parent scopes.
236///
237/// @param Name The name of the entity that we are searching for.
238///
239/// @param Criteria The criteria that this routine will use to
240/// determine which names are visible and which names will be
241/// found. Note that name lookup will find a name that is visible by
242/// the given criteria, but the entity itself may not be semantically
243/// correct or even the kind of entity expected based on the
244/// lookup. For example, searching for a nested-name-specifier name
245/// might result in an EnumDecl, which is visible but is not permitted
246/// as a nested-name-specifier in C++03.
247///
248/// @returns The result of name lookup, which includes zero or more
249/// declarations and possibly additional information used to diagnose
250/// ambiguities.
251Sema::LookupResult
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000252Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind,
253 bool RedeclarationOnly) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000254 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000255
256 if (!getLangOptions().CPlusPlus) {
257 // Unqualified name lookup in C/Objective-C is purely lexical, so
258 // search in the declarations attached to the name.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000259 unsigned IDNS = 0;
260 switch (NameKind) {
261 case Sema::LookupOrdinaryName:
262 IDNS = Decl::IDNS_Ordinary;
263 break;
Douglas Gregor78d70132009-01-14 22:20:51 +0000264
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000265 case Sema::LookupTagName:
266 IDNS = Decl::IDNS_Tag;
267 break;
268
269 case Sema::LookupMemberName:
270 IDNS = Decl::IDNS_Member;
271 break;
272
273 case Sema::LookupNestedNameSpecifierName:
274 case Sema::LookupNamespaceName:
275 assert(false && "C does not perform these kinds of name lookup");
276 break;
277 }
278
Douglas Gregor78d70132009-01-14 22:20:51 +0000279 // Scan up the scope chain looking for a decl that matches this
280 // identifier that is in the appropriate namespace. This search
281 // should not take long, as shadowing of names is uncommon, and
282 // deep shadowing is extremely uncommon.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000283 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
284 IEnd = IdResolver.end();
285 I != IEnd; ++I)
286 if ((*I)->isInIdentifierNamespace(IDNS))
Douglas Gregord07474b2009-01-17 01:13:24 +0000287 return LookupResult::CreateLookupResult(Context, *I);
Douglas Gregor78d70132009-01-14 22:20:51 +0000288 } else {
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000289 unsigned IDNS
290 = getIdentifierNamespacesFromLookupNameKind(NameKind,
291 getLangOptions().CPlusPlus);
292
Douglas Gregor78d70132009-01-14 22:20:51 +0000293 // Unqualified name lookup in C++ requires looking into scopes
294 // that aren't strictly lexical, and therefore we walk through the
295 // context as well as walking through the scopes.
296
297 // FIXME: does "true" for LookInParentCtx actually make sense?
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000298 IdentifierResolver::iterator I = IdResolver.begin(Name),
299 IEnd = IdResolver.end();
Douglas Gregor78d70132009-01-14 22:20:51 +0000300 for (; S; S = S->getParent()) {
301 // Check whether the IdResolver has anything in this scope.
302 for (; I != IEnd && S->isDeclScope(*I); ++I) {
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000303 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000304 // We found something. Look for anything else in our scope
305 // with this same name and in an acceptable identifier
306 // namespace, so that we can construct an overload set if we
307 // need to.
308 IdentifierResolver::iterator LastI = I;
309 for (++LastI; LastI != IEnd; ++LastI) {
310 if (!S->isDeclScope(*LastI))
311 break;
312 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000313 return LookupResult::CreateLookupResult(Context, I, LastI);
Douglas Gregor78d70132009-01-14 22:20:51 +0000314 }
315 }
316
317 // If there is an entity associated with this scope, it's a
318 // DeclContext. We might need to perform qualified lookup into
319 // it.
320 // FIXME: We're performing redundant lookups here, where the
321 // scope stack mirrors the semantic nested of classes and
322 // namespaces. We can save some work by checking the lexical
323 // scope against the semantic scope and avoiding any lookups
324 // when they are the same.
325 // FIXME: In some cases, we know that every name that could be
326 // found by this qualified name lookup will also be on the
327 // identifier chain. For example, inside a class without any
328 // base classes, we never need to perform qualified lookup
329 // because all of the members are on top of the identifier
330 // chain. However, we cannot perform this optimization when the
331 // lexical and semantic scopes don't line up, e.g., in an
332 // out-of-line member definition.
333 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
334 while (Ctx && Ctx->isFunctionOrMethod())
335 Ctx = Ctx->getParent();
336 while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) {
337 // Look for declarations of this name in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000338 if (LookupResult Result = LookupQualifiedName(Ctx, Name, NameKind,
339 RedeclarationOnly))
Douglas Gregor78d70132009-01-14 22:20:51 +0000340 return Result;
341
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000342 if (RedeclarationOnly && !Ctx->isTransparentContext())
Douglas Gregord07474b2009-01-17 01:13:24 +0000343 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000344
345 Ctx = Ctx->getParent();
346 }
347 }
348 }
349
350 // If we didn't find a use of this identifier, and if the identifier
351 // corresponds to a compiler builtin, create the decl object for the builtin
352 // now, injecting it into translation unit scope, and return it.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000353 if (NameKind == LookupOrdinaryName) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000354 IdentifierInfo *II = Name.getAsIdentifierInfo();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000355 if (II) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000356 // If this is a builtin on this (or all) targets, create the decl.
357 if (unsigned BuiltinID = II->getBuiltinID())
Douglas Gregord07474b2009-01-17 01:13:24 +0000358 return LookupResult::CreateLookupResult(Context,
Douglas Gregor78d70132009-01-14 22:20:51 +0000359 LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
360 S));
361 }
362 if (getLangOptions().ObjC1 && II) {
363 // @interface and @compatibility_alias introduce typedef-like names.
364 // Unlike typedef's, they can only be introduced at file-scope (and are
365 // therefore not scoped decls). They can, however, be shadowed by
366 // other names in IDNS_Ordinary.
367 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
368 if (IDI != ObjCInterfaceDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000369 return LookupResult::CreateLookupResult(Context, IDI->second);
Douglas Gregor78d70132009-01-14 22:20:51 +0000370 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
371 if (I != ObjCAliasDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000372 return LookupResult::CreateLookupResult(Context,
373 I->second->getClassInterface());
Douglas Gregor78d70132009-01-14 22:20:51 +0000374 }
375 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000376 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000377}
378
379/// @brief Perform qualified name lookup into a given context.
380///
381/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
382/// names when the context of those names is explicit specified, e.g.,
383/// "std::vector" or "x->member".
384///
385/// Different lookup criteria can find different names. For example, a
386/// particular scope can have both a struct and a function of the same
387/// name, and each can be found by certain lookup criteria. For more
388/// information about lookup criteria, see the documentation for the
389/// class LookupCriteria.
390///
391/// @param LookupCtx The context in which qualified name lookup will
392/// search. If the lookup criteria permits, name lookup may also search
393/// in the parent contexts or (for C++ classes) base classes.
394///
395/// @param Name The name of the entity that we are searching for.
396///
397/// @param Criteria The criteria that this routine will use to
398/// determine which names are visible and which names will be
399/// found. Note that name lookup will find a name that is visible by
400/// the given criteria, but the entity itself may not be semantically
401/// correct or even the kind of entity expected based on the
402/// lookup. For example, searching for a nested-name-specifier name
403/// might result in an EnumDecl, which is visible but is not permitted
404/// as a nested-name-specifier in C++03.
405///
406/// @returns The result of name lookup, which includes zero or more
407/// declarations and possibly additional information used to diagnose
408/// ambiguities.
409Sema::LookupResult
410Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name,
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000411 LookupNameKind NameKind, bool RedeclarationOnly) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000412 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
413
Douglas Gregord07474b2009-01-17 01:13:24 +0000414 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000415
416 // If we're performing qualified name lookup (e.g., lookup into a
417 // struct), find fields as part of ordinary name lookup.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000418 unsigned IDNS
419 = getIdentifierNamespacesFromLookupNameKind(NameKind,
420 getLangOptions().CPlusPlus);
421 if (NameKind == LookupOrdinaryName)
422 IDNS |= Decl::IDNS_Member;
Douglas Gregor78d70132009-01-14 22:20:51 +0000423
424 // Perform qualified name lookup into the LookupCtx.
Douglas Gregor78d70132009-01-14 22:20:51 +0000425 DeclContext::lookup_iterator I, E;
426 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000427 if (isAcceptableLookupResult(*I, NameKind, IDNS))
Douglas Gregord07474b2009-01-17 01:13:24 +0000428 return LookupResult::CreateLookupResult(Context, I, E);
Douglas Gregor78d70132009-01-14 22:20:51 +0000429
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000430 // If this isn't a C++ class or we aren't allowed to look into base
431 // classes, we're done.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000432 if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx))
Douglas Gregord07474b2009-01-17 01:13:24 +0000433 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000434
435 // Perform lookup into our base classes.
436 BasePaths Paths;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000437 Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx)));
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000438
439 // Look for this member in our base classes
440 if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx),
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000441 MemberLookupCriteria(Name, NameKind, IDNS), Paths))
Douglas Gregord07474b2009-01-17 01:13:24 +0000442 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000443
444 // C++ [class.member.lookup]p2:
445 // [...] If the resulting set of declarations are not all from
446 // sub-objects of the same type, or the set has a nonstatic member
447 // and includes members from distinct sub-objects, there is an
448 // ambiguity and the program is ill-formed. Otherwise that set is
449 // the result of the lookup.
450 // FIXME: support using declarations!
451 QualType SubobjectType;
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000452 int SubobjectNumber = 0;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000453 for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
454 Path != PathEnd; ++Path) {
455 const BasePathElement &PathElement = Path->back();
456
457 // Determine whether we're looking at a distinct sub-object or not.
458 if (SubobjectType.isNull()) {
459 // This is the first subobject we've looked at. Record it's type.
460 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
461 SubobjectNumber = PathElement.SubobjectNumber;
462 } else if (SubobjectType
463 != Context.getCanonicalType(PathElement.Base->getType())) {
464 // We found members of the given name in two subobjects of
465 // different types. This lookup is ambiguous.
466 BasePaths *PathsOnHeap = new BasePaths;
467 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000468 return LookupResult::CreateLookupResult(Context, PathsOnHeap, true);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000469 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
470 // We have a different subobject of the same type.
471
472 // C++ [class.member.lookup]p5:
473 // A static member, a nested type or an enumerator defined in
474 // a base class T can unambiguously be found even if an object
475 // has more than one base class subobject of type T.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000476 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000477 if (isa<VarDecl>(FirstDecl) ||
478 isa<TypeDecl>(FirstDecl) ||
479 isa<EnumConstantDecl>(FirstDecl))
480 continue;
481
482 if (isa<CXXMethodDecl>(FirstDecl)) {
483 // Determine whether all of the methods are static.
484 bool AllMethodsAreStatic = true;
485 for (DeclContext::lookup_iterator Func = Path->Decls.first;
486 Func != Path->Decls.second; ++Func) {
487 if (!isa<CXXMethodDecl>(*Func)) {
488 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
489 break;
490 }
491
492 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
493 AllMethodsAreStatic = false;
494 break;
495 }
496 }
497
498 if (AllMethodsAreStatic)
499 continue;
500 }
501
502 // We have found a nonstatic member name in multiple, distinct
503 // subobjects. Name lookup is ambiguous.
504 BasePaths *PathsOnHeap = new BasePaths;
505 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000506 return LookupResult::CreateLookupResult(Context, PathsOnHeap, false);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000507 }
508 }
509
510 // Lookup in a base class succeeded; return these results.
511
512 // If we found a function declaration, return an overload set.
513 if (isa<FunctionDecl>(*Paths.front().Decls.first))
Douglas Gregord07474b2009-01-17 01:13:24 +0000514 return LookupResult::CreateLookupResult(Context,
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000515 Paths.front().Decls.first, Paths.front().Decls.second);
516
517 // We found a non-function declaration; return a single declaration.
Douglas Gregord07474b2009-01-17 01:13:24 +0000518 return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first);
Douglas Gregor78d70132009-01-14 22:20:51 +0000519}
520
521/// @brief Performs name lookup for a name that was parsed in the
522/// source code, and may contain a C++ scope specifier.
523///
524/// This routine is a convenience routine meant to be called from
525/// contexts that receive a name and an optional C++ scope specifier
526/// (e.g., "N::M::x"). It will then perform either qualified or
527/// unqualified name lookup (with LookupQualifiedName or LookupName,
528/// respectively) on the given name and return those results.
529///
530/// @param S The scope from which unqualified name lookup will
531/// begin.
532///
533/// @param SS An optional C++ scope-specified, e.g., "::N::M".
534///
535/// @param Name The name of the entity that name lookup will
536/// search for.
537///
Douglas Gregor78d70132009-01-14 22:20:51 +0000538/// @returns The result of qualified or unqualified name lookup.
539Sema::LookupResult
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000540Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS,
541 DeclarationName Name, LookupNameKind NameKind,
542 bool RedeclarationOnly) {
543 if (SS) {
544 if (SS->isInvalid())
545 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000546
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000547 if (SS->isSet())
548 return LookupQualifiedName(static_cast<DeclContext *>(SS->getScopeRep()),
549 Name, NameKind, RedeclarationOnly);
550 }
551
552 return LookupName(S, Name, NameKind, RedeclarationOnly);
Douglas Gregor78d70132009-01-14 22:20:51 +0000553}
554
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000555/// @brief Produce a diagnostic describing the ambiguity that resulted
556/// from name lookup.
557///
558/// @param Result The ambiguous name lookup result.
559///
560/// @param Name The name of the entity that name lookup was
561/// searching for.
562///
563/// @param NameLoc The location of the name within the source code.
564///
565/// @param LookupRange A source range that provides more
566/// source-location information concerning the lookup itself. For
567/// example, this range might highlight a nested-name-specifier that
568/// precedes the name.
569///
570/// @returns true
571bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name,
572 SourceLocation NameLoc,
573 SourceRange LookupRange) {
574 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
575
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000576 BasePaths *Paths = Result.getBasePaths();
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000577 if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) {
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000578 QualType SubobjectType = Paths->front().back().Base->getType();
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000579 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
580 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
581 << LookupRange;
582
583 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
584 while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic())
585 ++Found;
586
587 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
Douglas Gregor98b27542009-01-17 00:42:38 +0000588
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000589 return true;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000590 }
591
592 assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes &&
593 "Unhandled form of name lookup ambiguity");
594
595 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
596 << Name << LookupRange;
597
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000598 std::set<Decl *> DeclsPrinted;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000599 for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
600 Path != PathEnd; ++Path) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000601 Decl *D = *Path->Decls.first;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000602 if (DeclsPrinted.insert(D).second)
603 Diag(D->getLocation(), diag::note_ambiguous_member_found);
604 }
605
Douglas Gregord07474b2009-01-17 01:13:24 +0000606 delete Paths;
607
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000608 return true;
609}