blob: da2deec02ad6fa6d15e1f056cca426de527fe9df [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 Gregorbd4b0852009-02-02 21:35:47 +0000184Sema::LookupResult::iterator::reference
185Sema::LookupResult::iterator::operator*() const {
186 switch (Result->StoredKind) {
187 case SingleDecl:
188 return reinterpret_cast<Decl*>(Current);
189
190 case OverloadedDeclFromIdResolver:
191 return *IdentifierResolver::iterator::getFromOpaqueValue(Current);
192
193 case OverloadedDeclFromDeclContext:
194 return *reinterpret_cast<DeclContext::lookup_iterator>(Current);
195
196 case AmbiguousLookup:
197 assert(false && "Cannot look into ambiguous lookup results");
198 break;
199 }
200
201 return 0;
202}
203
204Sema::LookupResult::iterator& Sema::LookupResult::iterator::operator++() {
205 switch (Result->StoredKind) {
206 case SingleDecl:
207 Current = reinterpret_cast<uintptr_t>((Decl*)0);
208 break;
209
210 case OverloadedDeclFromIdResolver: {
211 IdentifierResolver::iterator I
212 = IdentifierResolver::iterator::getFromOpaqueValue(Current);
213 ++I;
214 Current = I.getAsOpaqueValue();
215 break;
216 }
217
218 case OverloadedDeclFromDeclContext: {
219 DeclContext::lookup_iterator I
220 = reinterpret_cast<DeclContext::lookup_iterator>(Current);
221 ++I;
222 Current = reinterpret_cast<uintptr_t>(I);
223 break;
224 }
225
226 case AmbiguousLookup:
227 assert(false && "Cannot look into ambiguous lookup results");
228 break;
229 }
230
231 return *this;
232}
233
234Sema::LookupResult::iterator Sema::LookupResult::begin() {
235 assert(StoredKind != AmbiguousLookup && "Lookup into an ambiguous result");
236 return iterator(this, First);
237}
238
239Sema::LookupResult::iterator Sema::LookupResult::end() {
240 assert(StoredKind != AmbiguousLookup && "Lookup into an ambiguous result");
241 return iterator(this, Last);
242}
243
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000244// Retrieve the set of identifier namespaces that correspond to a
245// specific kind of name lookup.
246inline unsigned
247getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind,
248 bool CPlusPlus) {
249 unsigned IDNS = 0;
250 switch (NameKind) {
251 case Sema::LookupOrdinaryName:
252 IDNS = Decl::IDNS_Ordinary;
253 if (CPlusPlus)
254 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member;
255 break;
256
257 case Sema::LookupTagName:
258 IDNS = Decl::IDNS_Tag;
259 break;
260
261 case Sema::LookupMemberName:
262 IDNS = Decl::IDNS_Member;
263 if (CPlusPlus)
264 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
265 break;
266
267 case Sema::LookupNestedNameSpecifierName:
268 case Sema::LookupNamespaceName:
269 IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member;
270 break;
271 }
272 return IDNS;
273}
274
Douglas Gregor78d70132009-01-14 22:20:51 +0000275/// @brief Perform unqualified name lookup starting from a given
276/// scope.
277///
278/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
279/// used to find names within the current scope. For example, 'x' in
280/// @code
281/// int x;
282/// int f() {
283/// return x; // unqualified name look finds 'x' in the global scope
284/// }
285/// @endcode
286///
287/// Different lookup criteria can find different names. For example, a
288/// particular scope can have both a struct and a function of the same
289/// name, and each can be found by certain lookup criteria. For more
290/// information about lookup criteria, see the documentation for the
291/// class LookupCriteria.
292///
293/// @param S The scope from which unqualified name lookup will
294/// begin. If the lookup criteria permits, name lookup may also search
295/// in the parent scopes.
296///
297/// @param Name The name of the entity that we are searching for.
298///
299/// @param Criteria The criteria that this routine will use to
300/// determine which names are visible and which names will be
301/// found. Note that name lookup will find a name that is visible by
302/// the given criteria, but the entity itself may not be semantically
303/// correct or even the kind of entity expected based on the
304/// lookup. For example, searching for a nested-name-specifier name
305/// might result in an EnumDecl, which is visible but is not permitted
306/// as a nested-name-specifier in C++03.
307///
308/// @returns The result of name lookup, which includes zero or more
309/// declarations and possibly additional information used to diagnose
310/// ambiguities.
311Sema::LookupResult
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000312Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind,
313 bool RedeclarationOnly) {
Douglas Gregord07474b2009-01-17 01:13:24 +0000314 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000315
316 if (!getLangOptions().CPlusPlus) {
317 // Unqualified name lookup in C/Objective-C is purely lexical, so
318 // search in the declarations attached to the name.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000319 unsigned IDNS = 0;
320 switch (NameKind) {
321 case Sema::LookupOrdinaryName:
322 IDNS = Decl::IDNS_Ordinary;
323 break;
Douglas Gregor78d70132009-01-14 22:20:51 +0000324
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000325 case Sema::LookupTagName:
326 IDNS = Decl::IDNS_Tag;
327 break;
328
329 case Sema::LookupMemberName:
330 IDNS = Decl::IDNS_Member;
331 break;
332
333 case Sema::LookupNestedNameSpecifierName:
334 case Sema::LookupNamespaceName:
335 assert(false && "C does not perform these kinds of name lookup");
336 break;
337 }
338
Douglas Gregor78d70132009-01-14 22:20:51 +0000339 // Scan up the scope chain looking for a decl that matches this
340 // identifier that is in the appropriate namespace. This search
341 // should not take long, as shadowing of names is uncommon, and
342 // deep shadowing is extremely uncommon.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000343 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
344 IEnd = IdResolver.end();
345 I != IEnd; ++I)
346 if ((*I)->isInIdentifierNamespace(IDNS))
Douglas Gregord07474b2009-01-17 01:13:24 +0000347 return LookupResult::CreateLookupResult(Context, *I);
Douglas Gregor78d70132009-01-14 22:20:51 +0000348 } else {
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000349 unsigned IDNS
350 = getIdentifierNamespacesFromLookupNameKind(NameKind,
351 getLangOptions().CPlusPlus);
352
Douglas Gregor78d70132009-01-14 22:20:51 +0000353 // Unqualified name lookup in C++ requires looking into scopes
354 // that aren't strictly lexical, and therefore we walk through the
355 // context as well as walking through the scopes.
356
357 // FIXME: does "true" for LookInParentCtx actually make sense?
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000358 IdentifierResolver::iterator I = IdResolver.begin(Name),
359 IEnd = IdResolver.end();
Douglas Gregor78d70132009-01-14 22:20:51 +0000360 for (; S; S = S->getParent()) {
361 // Check whether the IdResolver has anything in this scope.
362 for (; I != IEnd && S->isDeclScope(*I); ++I) {
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000363 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000364 // We found something. Look for anything else in our scope
365 // with this same name and in an acceptable identifier
366 // namespace, so that we can construct an overload set if we
367 // need to.
368 IdentifierResolver::iterator LastI = I;
369 for (++LastI; LastI != IEnd; ++LastI) {
370 if (!S->isDeclScope(*LastI))
371 break;
372 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000373 return LookupResult::CreateLookupResult(Context, I, LastI);
Douglas Gregor78d70132009-01-14 22:20:51 +0000374 }
375 }
376
377 // If there is an entity associated with this scope, it's a
378 // DeclContext. We might need to perform qualified lookup into
379 // it.
380 // FIXME: We're performing redundant lookups here, where the
381 // scope stack mirrors the semantic nested of classes and
382 // namespaces. We can save some work by checking the lexical
383 // scope against the semantic scope and avoiding any lookups
384 // when they are the same.
385 // FIXME: In some cases, we know that every name that could be
386 // found by this qualified name lookup will also be on the
387 // identifier chain. For example, inside a class without any
388 // base classes, we never need to perform qualified lookup
389 // because all of the members are on top of the identifier
390 // chain. However, we cannot perform this optimization when the
391 // lexical and semantic scopes don't line up, e.g., in an
392 // out-of-line member definition.
393 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
394 while (Ctx && Ctx->isFunctionOrMethod())
395 Ctx = Ctx->getParent();
396 while (Ctx && (Ctx->isNamespace() || Ctx->isRecord())) {
397 // Look for declarations of this name in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000398 if (LookupResult Result = LookupQualifiedName(Ctx, Name, NameKind,
399 RedeclarationOnly))
Douglas Gregor78d70132009-01-14 22:20:51 +0000400 return Result;
401
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000402 if (RedeclarationOnly && !Ctx->isTransparentContext())
Douglas Gregord07474b2009-01-17 01:13:24 +0000403 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000404
405 Ctx = Ctx->getParent();
406 }
407 }
408 }
409
410 // If we didn't find a use of this identifier, and if the identifier
411 // corresponds to a compiler builtin, create the decl object for the builtin
412 // now, injecting it into translation unit scope, and return it.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000413 if (NameKind == LookupOrdinaryName) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000414 IdentifierInfo *II = Name.getAsIdentifierInfo();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000415 if (II) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000416 // If this is a builtin on this (or all) targets, create the decl.
417 if (unsigned BuiltinID = II->getBuiltinID())
Douglas Gregord07474b2009-01-17 01:13:24 +0000418 return LookupResult::CreateLookupResult(Context,
Douglas Gregor78d70132009-01-14 22:20:51 +0000419 LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
420 S));
421 }
422 if (getLangOptions().ObjC1 && II) {
423 // @interface and @compatibility_alias introduce typedef-like names.
424 // Unlike typedef's, they can only be introduced at file-scope (and are
425 // therefore not scoped decls). They can, however, be shadowed by
426 // other names in IDNS_Ordinary.
427 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
428 if (IDI != ObjCInterfaceDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000429 return LookupResult::CreateLookupResult(Context, IDI->second);
Douglas Gregor78d70132009-01-14 22:20:51 +0000430 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
431 if (I != ObjCAliasDecls.end())
Douglas Gregord07474b2009-01-17 01:13:24 +0000432 return LookupResult::CreateLookupResult(Context,
433 I->second->getClassInterface());
Douglas Gregor78d70132009-01-14 22:20:51 +0000434 }
435 }
Douglas Gregord07474b2009-01-17 01:13:24 +0000436 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000437}
438
439/// @brief Perform qualified name lookup into a given context.
440///
441/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
442/// names when the context of those names is explicit specified, e.g.,
443/// "std::vector" or "x->member".
444///
445/// Different lookup criteria can find different names. For example, a
446/// particular scope can have both a struct and a function of the same
447/// name, and each can be found by certain lookup criteria. For more
448/// information about lookup criteria, see the documentation for the
449/// class LookupCriteria.
450///
451/// @param LookupCtx The context in which qualified name lookup will
452/// search. If the lookup criteria permits, name lookup may also search
453/// in the parent contexts or (for C++ classes) base classes.
454///
455/// @param Name The name of the entity that we are searching for.
456///
457/// @param Criteria The criteria that this routine will use to
458/// determine which names are visible and which names will be
459/// found. Note that name lookup will find a name that is visible by
460/// the given criteria, but the entity itself may not be semantically
461/// correct or even the kind of entity expected based on the
462/// lookup. For example, searching for a nested-name-specifier name
463/// might result in an EnumDecl, which is visible but is not permitted
464/// as a nested-name-specifier in C++03.
465///
466/// @returns The result of name lookup, which includes zero or more
467/// declarations and possibly additional information used to diagnose
468/// ambiguities.
469Sema::LookupResult
470Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name,
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000471 LookupNameKind NameKind, bool RedeclarationOnly) {
Douglas Gregor78d70132009-01-14 22:20:51 +0000472 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
473
Douglas Gregord07474b2009-01-17 01:13:24 +0000474 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000475
476 // If we're performing qualified name lookup (e.g., lookup into a
477 // struct), find fields as part of ordinary name lookup.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000478 unsigned IDNS
479 = getIdentifierNamespacesFromLookupNameKind(NameKind,
480 getLangOptions().CPlusPlus);
481 if (NameKind == LookupOrdinaryName)
482 IDNS |= Decl::IDNS_Member;
Douglas Gregor78d70132009-01-14 22:20:51 +0000483
484 // Perform qualified name lookup into the LookupCtx.
Douglas Gregor78d70132009-01-14 22:20:51 +0000485 DeclContext::lookup_iterator I, E;
486 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000487 if (isAcceptableLookupResult(*I, NameKind, IDNS))
Douglas Gregord07474b2009-01-17 01:13:24 +0000488 return LookupResult::CreateLookupResult(Context, I, E);
Douglas Gregor78d70132009-01-14 22:20:51 +0000489
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000490 // If this isn't a C++ class or we aren't allowed to look into base
491 // classes, we're done.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000492 if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx))
Douglas Gregord07474b2009-01-17 01:13:24 +0000493 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000494
495 // Perform lookup into our base classes.
496 BasePaths Paths;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000497 Paths.setOrigin(Context.getTypeDeclType(cast<RecordDecl>(LookupCtx)));
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000498
499 // Look for this member in our base classes
500 if (!LookupInBases(cast<CXXRecordDecl>(LookupCtx),
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000501 MemberLookupCriteria(Name, NameKind, IDNS), Paths))
Douglas Gregord07474b2009-01-17 01:13:24 +0000502 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000503
504 // C++ [class.member.lookup]p2:
505 // [...] If the resulting set of declarations are not all from
506 // sub-objects of the same type, or the set has a nonstatic member
507 // and includes members from distinct sub-objects, there is an
508 // ambiguity and the program is ill-formed. Otherwise that set is
509 // the result of the lookup.
510 // FIXME: support using declarations!
511 QualType SubobjectType;
Daniel Dunbarddebeca2009-01-15 18:32:35 +0000512 int SubobjectNumber = 0;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000513 for (BasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
514 Path != PathEnd; ++Path) {
515 const BasePathElement &PathElement = Path->back();
516
517 // Determine whether we're looking at a distinct sub-object or not.
518 if (SubobjectType.isNull()) {
519 // This is the first subobject we've looked at. Record it's type.
520 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
521 SubobjectNumber = PathElement.SubobjectNumber;
522 } else if (SubobjectType
523 != Context.getCanonicalType(PathElement.Base->getType())) {
524 // We found members of the given name in two subobjects of
525 // different types. This lookup is ambiguous.
526 BasePaths *PathsOnHeap = new BasePaths;
527 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000528 return LookupResult::CreateLookupResult(Context, PathsOnHeap, true);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000529 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
530 // We have a different subobject of the same type.
531
532 // C++ [class.member.lookup]p5:
533 // A static member, a nested type or an enumerator defined in
534 // a base class T can unambiguously be found even if an object
535 // has more than one base class subobject of type T.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000536 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000537 if (isa<VarDecl>(FirstDecl) ||
538 isa<TypeDecl>(FirstDecl) ||
539 isa<EnumConstantDecl>(FirstDecl))
540 continue;
541
542 if (isa<CXXMethodDecl>(FirstDecl)) {
543 // Determine whether all of the methods are static.
544 bool AllMethodsAreStatic = true;
545 for (DeclContext::lookup_iterator Func = Path->Decls.first;
546 Func != Path->Decls.second; ++Func) {
547 if (!isa<CXXMethodDecl>(*Func)) {
548 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
549 break;
550 }
551
552 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
553 AllMethodsAreStatic = false;
554 break;
555 }
556 }
557
558 if (AllMethodsAreStatic)
559 continue;
560 }
561
562 // We have found a nonstatic member name in multiple, distinct
563 // subobjects. Name lookup is ambiguous.
564 BasePaths *PathsOnHeap = new BasePaths;
565 PathsOnHeap->swap(Paths);
Douglas Gregord07474b2009-01-17 01:13:24 +0000566 return LookupResult::CreateLookupResult(Context, PathsOnHeap, false);
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000567 }
568 }
569
570 // Lookup in a base class succeeded; return these results.
571
572 // If we found a function declaration, return an overload set.
573 if (isa<FunctionDecl>(*Paths.front().Decls.first))
Douglas Gregord07474b2009-01-17 01:13:24 +0000574 return LookupResult::CreateLookupResult(Context,
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000575 Paths.front().Decls.first, Paths.front().Decls.second);
576
577 // We found a non-function declaration; return a single declaration.
Douglas Gregord07474b2009-01-17 01:13:24 +0000578 return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first);
Douglas Gregor78d70132009-01-14 22:20:51 +0000579}
580
581/// @brief Performs name lookup for a name that was parsed in the
582/// source code, and may contain a C++ scope specifier.
583///
584/// This routine is a convenience routine meant to be called from
585/// contexts that receive a name and an optional C++ scope specifier
586/// (e.g., "N::M::x"). It will then perform either qualified or
587/// unqualified name lookup (with LookupQualifiedName or LookupName,
588/// respectively) on the given name and return those results.
589///
590/// @param S The scope from which unqualified name lookup will
591/// begin.
592///
593/// @param SS An optional C++ scope-specified, e.g., "::N::M".
594///
595/// @param Name The name of the entity that name lookup will
596/// search for.
597///
Douglas Gregor78d70132009-01-14 22:20:51 +0000598/// @returns The result of qualified or unqualified name lookup.
599Sema::LookupResult
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000600Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS,
601 DeclarationName Name, LookupNameKind NameKind,
602 bool RedeclarationOnly) {
603 if (SS) {
604 if (SS->isInvalid())
605 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor78d70132009-01-14 22:20:51 +0000606
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000607 if (SS->isSet())
608 return LookupQualifiedName(static_cast<DeclContext *>(SS->getScopeRep()),
609 Name, NameKind, RedeclarationOnly);
610 }
611
612 return LookupName(S, Name, NameKind, RedeclarationOnly);
Douglas Gregor78d70132009-01-14 22:20:51 +0000613}
614
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000615/// @brief Produce a diagnostic describing the ambiguity that resulted
616/// from name lookup.
617///
618/// @param Result The ambiguous name lookup result.
619///
620/// @param Name The name of the entity that name lookup was
621/// searching for.
622///
623/// @param NameLoc The location of the name within the source code.
624///
625/// @param LookupRange A source range that provides more
626/// source-location information concerning the lookup itself. For
627/// example, this range might highlight a nested-name-specifier that
628/// precedes the name.
629///
630/// @returns true
631bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name,
632 SourceLocation NameLoc,
633 SourceRange LookupRange) {
634 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
635
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000636 BasePaths *Paths = Result.getBasePaths();
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000637 if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) {
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000638 QualType SubobjectType = Paths->front().back().Base->getType();
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000639 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
640 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
641 << LookupRange;
642
643 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
644 while (isa<CXXMethodDecl>(*Found) && cast<CXXMethodDecl>(*Found)->isStatic())
645 ++Found;
646
647 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
Douglas Gregor98b27542009-01-17 00:42:38 +0000648
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000649 return true;
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000650 }
651
652 assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes &&
653 "Unhandled form of name lookup ambiguity");
654
655 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
656 << Name << LookupRange;
657
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000658 std::set<Decl *> DeclsPrinted;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000659 for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
660 Path != PathEnd; ++Path) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000661 Decl *D = *Path->Decls.first;
Douglas Gregorb9ef0552009-01-16 00:38:09 +0000662 if (DeclsPrinted.insert(D).second)
663 Diag(D->getLocation(), diag::note_ambiguous_member_found);
664 }
665
Douglas Gregord07474b2009-01-17 01:13:24 +0000666 delete Paths;
667
Douglas Gregor29dfa2f2009-01-15 00:26:24 +0000668 return true;
669}