blob: 70dca799ae1dd86c23bbf2c061a715b34efbf59c [file] [log] [blame]
Douglas Gregoreb11cd02009-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 Gregor7176fff2009-01-15 00:26:24 +000015#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Douglas Gregoreb11cd02009-01-14 22:20:51 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
Douglas Gregor42af25f2009-05-11 19:58:34 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregorfa047642009-02-04 00:32:51 +000021#include "clang/AST/Expr.h"
Douglas Gregordaa439a2009-07-08 10:57:20 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregoreb11cd02009-01-14 22:20:51 +000023#include "clang/Parse/DeclSpec.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Douglas Gregoreb11cd02009-01-14 22:20:51 +000025#include "clang/Basic/LangOptions.h"
26#include "llvm/ADT/STLExtras.h"
Douglas Gregorfa047642009-02-04 00:32:51 +000027#include "llvm/ADT/SmallPtrSet.h"
John McCall6e247262009-10-10 05:48:19 +000028#include "llvm/Support/ErrorHandling.h"
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +000029#include <set>
Douglas Gregor2a3009a2009-02-03 19:21:40 +000030#include <vector>
31#include <iterator>
32#include <utility>
33#include <algorithm>
Douglas Gregoreb11cd02009-01-14 22:20:51 +000034
35using namespace clang;
36
John McCalld7be78a2009-11-10 07:01:13 +000037namespace {
38 class UnqualUsingEntry {
39 const DeclContext *Nominated;
40 const DeclContext *CommonAncestor;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000041
John McCalld7be78a2009-11-10 07:01:13 +000042 public:
43 UnqualUsingEntry(const DeclContext *Nominated,
44 const DeclContext *CommonAncestor)
45 : Nominated(Nominated), CommonAncestor(CommonAncestor) {
46 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +000047
John McCalld7be78a2009-11-10 07:01:13 +000048 const DeclContext *getCommonAncestor() const {
49 return CommonAncestor;
50 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +000051
John McCalld7be78a2009-11-10 07:01:13 +000052 const DeclContext *getNominatedNamespace() const {
53 return Nominated;
54 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +000055
John McCalld7be78a2009-11-10 07:01:13 +000056 // Sort by the pointer value of the common ancestor.
57 struct Comparator {
58 bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) {
59 return L.getCommonAncestor() < R.getCommonAncestor();
60 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +000061
John McCalld7be78a2009-11-10 07:01:13 +000062 bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) {
63 return E.getCommonAncestor() < DC;
64 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +000065
John McCalld7be78a2009-11-10 07:01:13 +000066 bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) {
67 return DC < E.getCommonAncestor();
68 }
69 };
70 };
Douglas Gregor2a3009a2009-02-03 19:21:40 +000071
John McCalld7be78a2009-11-10 07:01:13 +000072 /// A collection of using directives, as used by C++ unqualified
73 /// lookup.
74 class UnqualUsingDirectiveSet {
75 typedef llvm::SmallVector<UnqualUsingEntry, 8> ListTy;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000076
John McCalld7be78a2009-11-10 07:01:13 +000077 ListTy list;
78 llvm::SmallPtrSet<DeclContext*, 8> visited;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000079
John McCalld7be78a2009-11-10 07:01:13 +000080 public:
81 UnqualUsingDirectiveSet() {}
Douglas Gregor2a3009a2009-02-03 19:21:40 +000082
John McCalld7be78a2009-11-10 07:01:13 +000083 void visitScopeChain(Scope *S, Scope *InnermostFileScope) {
84 // C++ [namespace.udir]p1:
85 // During unqualified name lookup, the names appear as if they
86 // were declared in the nearest enclosing namespace which contains
87 // both the using-directive and the nominated namespace.
88 DeclContext *InnermostFileDC
89 = static_cast<DeclContext*>(InnermostFileScope->getEntity());
90 assert(InnermostFileDC && InnermostFileDC->isFileContext());
Douglas Gregor2a3009a2009-02-03 19:21:40 +000091
John McCalld7be78a2009-11-10 07:01:13 +000092 for (; S; S = S->getParent()) {
John McCalld7be78a2009-11-10 07:01:13 +000093 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
94 DeclContext *EffectiveDC = (Ctx->isFileContext() ? Ctx : InnermostFileDC);
95 visit(Ctx, EffectiveDC);
96 } else {
97 Scope::udir_iterator I = S->using_directives_begin(),
98 End = S->using_directives_end();
99
100 for (; I != End; ++I)
101 visit(I->getAs<UsingDirectiveDecl>(), InnermostFileDC);
102 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000103 }
104 }
John McCalld7be78a2009-11-10 07:01:13 +0000105
106 // Visits a context and collect all of its using directives
107 // recursively. Treats all using directives as if they were
108 // declared in the context.
109 //
110 // A given context is only every visited once, so it is important
111 // that contexts be visited from the inside out in order to get
112 // the effective DCs right.
113 void visit(DeclContext *DC, DeclContext *EffectiveDC) {
114 if (!visited.insert(DC))
115 return;
116
117 addUsingDirectives(DC, EffectiveDC);
118 }
119
120 // Visits a using directive and collects all of its using
121 // directives recursively. Treats all using directives as if they
122 // were declared in the effective DC.
123 void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
124 DeclContext *NS = UD->getNominatedNamespace();
125 if (!visited.insert(NS))
126 return;
127
128 addUsingDirective(UD, EffectiveDC);
129 addUsingDirectives(NS, EffectiveDC);
130 }
131
132 // Adds all the using directives in a context (and those nominated
133 // by its using directives, transitively) as if they appeared in
134 // the given effective context.
135 void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) {
136 llvm::SmallVector<DeclContext*,4> queue;
137 while (true) {
138 DeclContext::udir_iterator I, End;
139 for (llvm::tie(I, End) = DC->getUsingDirectives(); I != End; ++I) {
140 UsingDirectiveDecl *UD = *I;
141 DeclContext *NS = UD->getNominatedNamespace();
142 if (visited.insert(NS)) {
143 addUsingDirective(UD, EffectiveDC);
144 queue.push_back(NS);
145 }
146 }
147
148 if (queue.empty())
149 return;
150
151 DC = queue.back();
152 queue.pop_back();
153 }
154 }
155
156 // Add a using directive as if it had been declared in the given
157 // context. This helps implement C++ [namespace.udir]p3:
158 // The using-directive is transitive: if a scope contains a
159 // using-directive that nominates a second namespace that itself
160 // contains using-directives, the effect is as if the
161 // using-directives from the second namespace also appeared in
162 // the first.
163 void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
164 // Find the common ancestor between the effective context and
165 // the nominated namespace.
166 DeclContext *Common = UD->getNominatedNamespace();
167 while (!Common->Encloses(EffectiveDC))
168 Common = Common->getParent();
John McCall12ea5782009-11-10 09:20:04 +0000169 Common = Common->getPrimaryContext();
John McCalld7be78a2009-11-10 07:01:13 +0000170
171 list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common));
172 }
173
174 void done() {
175 std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator());
176 }
177
178 typedef ListTy::iterator iterator;
179 typedef ListTy::const_iterator const_iterator;
180
181 iterator begin() { return list.begin(); }
182 iterator end() { return list.end(); }
183 const_iterator begin() const { return list.begin(); }
184 const_iterator end() const { return list.end(); }
185
186 std::pair<const_iterator,const_iterator>
187 getNamespacesFor(DeclContext *DC) const {
John McCall12ea5782009-11-10 09:20:04 +0000188 return std::equal_range(begin(), end(), DC->getPrimaryContext(),
John McCalld7be78a2009-11-10 07:01:13 +0000189 UnqualUsingEntry::Comparator());
190 }
191 };
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000192}
193
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000194// Retrieve the set of identifier namespaces that correspond to a
195// specific kind of name lookup.
Mike Stump1eb44332009-09-09 15:08:12 +0000196inline unsigned
197getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000198 bool CPlusPlus) {
199 unsigned IDNS = 0;
200 switch (NameKind) {
201 case Sema::LookupOrdinaryName:
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000202 case Sema::LookupOperatorName:
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000203 case Sema::LookupRedeclarationWithLinkage:
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000204 IDNS = Decl::IDNS_Ordinary;
205 if (CPlusPlus)
206 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member;
207 break;
208
209 case Sema::LookupTagName:
210 IDNS = Decl::IDNS_Tag;
211 break;
212
213 case Sema::LookupMemberName:
214 IDNS = Decl::IDNS_Member;
215 if (CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000216 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000217 break;
218
219 case Sema::LookupNestedNameSpecifierName:
220 case Sema::LookupNamespaceName:
221 IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member;
222 break;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000223
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000224 case Sema::LookupObjCProtocolName:
225 IDNS = Decl::IDNS_ObjCProtocol;
226 break;
227
228 case Sema::LookupObjCImplementationName:
229 IDNS = Decl::IDNS_ObjCImplementation;
230 break;
231
232 case Sema::LookupObjCCategoryImplName:
233 IDNS = Decl::IDNS_ObjCCategoryImpl;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000234 break;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000235 }
236 return IDNS;
237}
238
John McCallf36e02d2009-10-09 21:13:30 +0000239// Necessary because CXXBasePaths is not complete in Sema.h
240void Sema::LookupResult::deletePaths(CXXBasePaths *Paths) {
241 delete Paths;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000242}
243
John McCallf36e02d2009-10-09 21:13:30 +0000244void Sema::LookupResult::resolveKind() {
245 unsigned N = Decls.size();
Douglas Gregor69d993a2009-01-17 01:13:24 +0000246
John McCallf36e02d2009-10-09 21:13:30 +0000247 // Fast case: no possible ambiguity.
248 if (N <= 1) return;
249
John McCall6e247262009-10-10 05:48:19 +0000250 // Don't do any extra resolution if we've already resolved as ambiguous.
John McCalla24dc2e2009-11-17 02:14:36 +0000251 if (ResultKind == Ambiguous) return;
John McCall6e247262009-10-10 05:48:19 +0000252
John McCallf36e02d2009-10-09 21:13:30 +0000253 llvm::SmallPtrSet<NamedDecl*, 16> Unique;
254
255 bool Ambiguous = false;
256 bool HasTag = false, HasFunction = false, HasNonFunction = false;
John McCall314be4e2009-11-17 07:50:12 +0000257 bool HasUnresolved = false;
John McCallf36e02d2009-10-09 21:13:30 +0000258
259 unsigned UniqueTagIndex = 0;
260
261 unsigned I = 0;
262 while (I < N) {
John McCall314be4e2009-11-17 07:50:12 +0000263 NamedDecl *D = Decls[I]->getUnderlyingDecl();
264 D = cast<NamedDecl>(D->getCanonicalDecl());
John McCallf36e02d2009-10-09 21:13:30 +0000265
John McCall314be4e2009-11-17 07:50:12 +0000266 if (!Unique.insert(D)) {
John McCallf36e02d2009-10-09 21:13:30 +0000267 // If it's not unique, pull something off the back (and
268 // continue at this index).
269 Decls[I] = Decls[--N];
270 } else if (isa<UnresolvedUsingDecl>(D)) {
John McCall314be4e2009-11-17 07:50:12 +0000271 HasUnresolved = true;
John McCallf36e02d2009-10-09 21:13:30 +0000272 Decls[I] = Decls[--N];
273 } else {
274 // Otherwise, do some decl type analysis and then continue.
275 if (isa<TagDecl>(D)) {
276 if (HasTag)
277 Ambiguous = true;
278 UniqueTagIndex = I;
279 HasTag = true;
280 } else if (D->isFunctionOrFunctionTemplate()) {
281 HasFunction = true;
282 } else {
283 if (HasNonFunction)
284 Ambiguous = true;
285 HasNonFunction = true;
286 }
287 I++;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289 }
Douglas Gregor516ff432009-04-24 02:57:34 +0000290
John McCall314be4e2009-11-17 07:50:12 +0000291 // Postpone all other decisions if we have an unresolved decl, even
292 // if we can prove ambiguity. We can probably do better than this.
293 if (HasUnresolved) {
294 ResultKind = LookupResult::FoundOverloaded;
295 return;
296 }
297
John McCallf36e02d2009-10-09 21:13:30 +0000298 // C++ [basic.scope.hiding]p2:
299 // A class name or enumeration name can be hidden by the name of
300 // an object, function, or enumerator declared in the same
301 // scope. If a class or enumeration name and an object, function,
302 // or enumerator are declared in the same scope (in any order)
303 // with the same name, the class or enumeration name is hidden
304 // wherever the object, function, or enumerator name is visible.
305 // But it's still an error if there are distinct tag types found,
306 // even if they're not visible. (ref?)
John McCall9488ea12009-11-17 05:59:44 +0000307 if (HideTags && HasTag && !Ambiguous && (HasFunction || HasNonFunction))
John McCallf36e02d2009-10-09 21:13:30 +0000308 Decls[UniqueTagIndex] = Decls[--N];
Anders Carlsson8b50d012009-06-26 03:37:05 +0000309
John McCallf36e02d2009-10-09 21:13:30 +0000310 Decls.set_size(N);
Douglas Gregor7176fff2009-01-15 00:26:24 +0000311
John McCallf36e02d2009-10-09 21:13:30 +0000312 if (HasFunction && HasNonFunction)
313 Ambiguous = true;
Douglas Gregor69d993a2009-01-17 01:13:24 +0000314
John McCallf36e02d2009-10-09 21:13:30 +0000315 if (Ambiguous)
John McCall6e247262009-10-10 05:48:19 +0000316 setAmbiguous(LookupResult::AmbiguousReference);
John McCallf36e02d2009-10-09 21:13:30 +0000317 else if (N > 1)
John McCalla24dc2e2009-11-17 02:14:36 +0000318 ResultKind = LookupResult::FoundOverloaded;
John McCallf36e02d2009-10-09 21:13:30 +0000319 else
John McCalla24dc2e2009-11-17 02:14:36 +0000320 ResultKind = LookupResult::Found;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000321}
322
323/// @brief Converts the result of name lookup into a single (possible
324/// NULL) pointer to a declaration.
325///
326/// The resulting declaration will either be the declaration we found
327/// (if only a single declaration was found), an
328/// OverloadedFunctionDecl (if an overloaded function was found), or
329/// NULL (if no declaration was found). This conversion must not be
Mike Stump1eb44332009-09-09 15:08:12 +0000330/// used anywhere where name lookup could result in an ambiguity.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000331///
332/// The OverloadedFunctionDecl conversion is meant as a stop-gap
333/// solution, since it causes the OverloadedFunctionDecl to be
334/// leaked. FIXME: Eventually, there will be a better way to iterate
335/// over the set of overloaded functions returned by name lookup.
John McCallf36e02d2009-10-09 21:13:30 +0000336NamedDecl *Sema::LookupResult::getAsSingleDecl(ASTContext &C) const {
337 size_t size = Decls.size();
338 if (size == 0) return 0;
John McCall314be4e2009-11-17 07:50:12 +0000339 if (size == 1) return (*begin())->getUnderlyingDecl();
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000340
John McCallf36e02d2009-10-09 21:13:30 +0000341 if (isAmbiguous()) return 0;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000342
John McCallf36e02d2009-10-09 21:13:30 +0000343 iterator I = begin(), E = end();
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000344
John McCallf36e02d2009-10-09 21:13:30 +0000345 OverloadedFunctionDecl *Ovl
346 = OverloadedFunctionDecl::Create(C, (*I)->getDeclContext(),
347 (*I)->getDeclName());
348 for (; I != E; ++I) {
John McCall314be4e2009-11-17 07:50:12 +0000349 NamedDecl *ND = (*I)->getUnderlyingDecl();
John McCallf36e02d2009-10-09 21:13:30 +0000350 assert(ND->isFunctionOrFunctionTemplate());
351 if (isa<FunctionDecl>(ND))
352 Ovl->addOverload(cast<FunctionDecl>(ND));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000353 else
John McCallf36e02d2009-10-09 21:13:30 +0000354 Ovl->addOverload(cast<FunctionTemplateDecl>(ND));
355 // FIXME: UnresolvedUsingDecls.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000356 }
John McCallf36e02d2009-10-09 21:13:30 +0000357
358 return Ovl;
Douglas Gregord8635172009-02-02 21:35:47 +0000359}
360
John McCallf36e02d2009-10-09 21:13:30 +0000361void Sema::LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) {
362 CXXBasePaths::paths_iterator I, E;
363 DeclContext::lookup_iterator DI, DE;
364 for (I = P.begin(), E = P.end(); I != E; ++I)
365 for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI)
366 addDecl(*DI);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000367}
368
John McCallf36e02d2009-10-09 21:13:30 +0000369void Sema::LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) {
370 Paths = new CXXBasePaths;
371 Paths->swap(P);
372 addDeclsFromBasePaths(*Paths);
373 resolveKind();
John McCall6e247262009-10-10 05:48:19 +0000374 setAmbiguous(AmbiguousBaseSubobjects);
Douglas Gregord8635172009-02-02 21:35:47 +0000375}
376
John McCallf36e02d2009-10-09 21:13:30 +0000377void Sema::LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) {
378 Paths = new CXXBasePaths;
379 Paths->swap(P);
380 addDeclsFromBasePaths(*Paths);
381 resolveKind();
John McCall6e247262009-10-10 05:48:19 +0000382 setAmbiguous(AmbiguousBaseSubobjectTypes);
John McCallf36e02d2009-10-09 21:13:30 +0000383}
384
385void Sema::LookupResult::print(llvm::raw_ostream &Out) {
386 Out << Decls.size() << " result(s)";
387 if (isAmbiguous()) Out << ", ambiguous";
388 if (Paths) Out << ", base paths present";
389
390 for (iterator I = begin(), E = end(); I != E; ++I) {
391 Out << "\n";
392 (*I)->print(Out, 2);
393 }
394}
395
396// Adds all qualifying matches for a name within a decl context to the
397// given lookup result. Returns true if any matches were found.
John McCalld7be78a2009-11-10 07:01:13 +0000398static bool LookupDirect(Sema::LookupResult &R,
John McCalla24dc2e2009-11-17 02:14:36 +0000399 const DeclContext *DC) {
John McCallf36e02d2009-10-09 21:13:30 +0000400 bool Found = false;
401
John McCalld7be78a2009-11-10 07:01:13 +0000402 DeclContext::lookup_const_iterator I, E;
John McCalla24dc2e2009-11-17 02:14:36 +0000403 for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I)
404 if (Sema::isAcceptableLookupResult(*I, R.getLookupKind(),
405 R.getIdentifierNamespace()))
John McCallf36e02d2009-10-09 21:13:30 +0000406 R.addDecl(*I), Found = true;
407
408 return Found;
409}
410
John McCalld7be78a2009-11-10 07:01:13 +0000411// Performs C++ unqualified lookup into the given file context.
John McCallf36e02d2009-10-09 21:13:30 +0000412static bool
413CppNamespaceLookup(Sema::LookupResult &R, ASTContext &Context, DeclContext *NS,
John McCalla24dc2e2009-11-17 02:14:36 +0000414 UnqualUsingDirectiveSet &UDirs) {
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000415
416 assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
417
John McCalld7be78a2009-11-10 07:01:13 +0000418 // Perform direct name lookup into the LookupCtx.
John McCalla24dc2e2009-11-17 02:14:36 +0000419 bool Found = LookupDirect(R, NS);
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000420
John McCalld7be78a2009-11-10 07:01:13 +0000421 // Perform direct name lookup into the namespaces nominated by the
422 // using directives whose common ancestor is this namespace.
423 UnqualUsingDirectiveSet::const_iterator UI, UEnd;
424 llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
John McCalld7be78a2009-11-10 07:01:13 +0000426 for (; UI != UEnd; ++UI)
John McCalla24dc2e2009-11-17 02:14:36 +0000427 if (LookupDirect(R, UI->getNominatedNamespace()))
John McCalld7be78a2009-11-10 07:01:13 +0000428 Found = true;
John McCallf36e02d2009-10-09 21:13:30 +0000429
430 R.resolveKind();
431
432 return Found;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000433}
434
435static bool isNamespaceOrTranslationUnitScope(Scope *S) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000436 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()))
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000437 return Ctx->isFileContext();
438 return false;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000439}
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000440
Douglas Gregore942bbe2009-09-10 16:57:35 +0000441// Find the next outer declaration context corresponding to this scope.
442static DeclContext *findOuterContext(Scope *S) {
443 for (S = S->getParent(); S; S = S->getParent())
444 if (S->getEntity())
445 return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext();
446
447 return 0;
448}
449
John McCalla24dc2e2009-11-17 02:14:36 +0000450bool Sema::CppLookupName(LookupResult &R, Scope *S) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000451 assert(getLangOptions().CPlusPlus &&
452 "Can perform only C++ lookup");
John McCalla24dc2e2009-11-17 02:14:36 +0000453 LookupNameKind NameKind = R.getLookupKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000454 unsigned IDNS
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000455 = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true);
John McCall02cace72009-08-28 07:59:38 +0000456
457 // If we're testing for redeclarations, also look in the friend namespaces.
John McCalla24dc2e2009-11-17 02:14:36 +0000458 if (R.isForRedeclaration()) {
John McCall02cace72009-08-28 07:59:38 +0000459 if (IDNS & Decl::IDNS_Tag) IDNS |= Decl::IDNS_TagFriend;
460 if (IDNS & Decl::IDNS_Ordinary) IDNS |= Decl::IDNS_OrdinaryFriend;
461 }
462
John McCalla24dc2e2009-11-17 02:14:36 +0000463 R.setIdentifierNamespace(IDNS);
464
465 DeclarationName Name = R.getLookupName();
466
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000467 Scope *Initial = S;
Mike Stump1eb44332009-09-09 15:08:12 +0000468 IdentifierResolver::iterator
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000469 I = IdResolver.begin(Name),
470 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000471
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000472 // First we lookup local scope.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000473 // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir]
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000474 // ...During unqualified name lookup (3.4.1), the names appear as if
475 // they were declared in the nearest enclosing namespace which contains
476 // both the using-directive and the nominated namespace.
Eli Friedman33a31382009-08-05 19:21:58 +0000477 // [Note: in this context, "contains" means "contains directly or
Mike Stump1eb44332009-09-09 15:08:12 +0000478 // indirectly".
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000479 //
480 // For example:
481 // namespace A { int i; }
482 // void foo() {
483 // int i;
484 // {
485 // using namespace A;
486 // ++i; // finds local 'i', A::i appears at global scope
487 // }
488 // }
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000489 //
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000490 for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000491 // Check whether the IdResolver has anything in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000492 bool Found = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000493 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000494 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
John McCallf36e02d2009-10-09 21:13:30 +0000495 Found = true;
496 R.addDecl(*I);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000497 }
498 }
John McCallf36e02d2009-10-09 21:13:30 +0000499 if (Found) {
500 R.resolveKind();
501 return true;
502 }
503
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000504 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
Douglas Gregore942bbe2009-09-10 16:57:35 +0000505 DeclContext *OuterCtx = findOuterContext(S);
506 for (; Ctx && Ctx->getPrimaryContext() != OuterCtx;
507 Ctx = Ctx->getLookupParent()) {
508 if (Ctx->isFunctionOrMethod())
509 continue;
510
511 // Perform qualified name lookup into this context.
512 // FIXME: In some cases, we know that every name that could be found by
513 // this qualified name lookup will also be on the identifier chain. For
514 // example, inside a class without any base classes, we never need to
515 // perform qualified lookup because all of the members are on top of the
516 // identifier chain.
John McCalla24dc2e2009-11-17 02:14:36 +0000517 if (LookupQualifiedName(R, Ctx))
John McCallf36e02d2009-10-09 21:13:30 +0000518 return true;
Douglas Gregor551f48c2009-03-27 04:21:56 +0000519 }
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000520 }
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000521 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000522
John McCalld7be78a2009-11-10 07:01:13 +0000523 // Stop if we ran out of scopes.
524 // FIXME: This really, really shouldn't be happening.
525 if (!S) return false;
526
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000527 // Collect UsingDirectiveDecls in all scopes, and recursively all
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000528 // nominated namespaces by those using-directives.
John McCalld7be78a2009-11-10 07:01:13 +0000529 //
Mike Stump390b4cc2009-05-16 07:39:55 +0000530 // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
531 // don't build it for each lookup!
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000532
John McCalld7be78a2009-11-10 07:01:13 +0000533 UnqualUsingDirectiveSet UDirs;
534 UDirs.visitScopeChain(Initial, S);
535 UDirs.done();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000536
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000537 // Lookup namespace scope, and global scope.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000538 // Unqualified name lookup in C++ requires looking into scopes
539 // that aren't strictly lexical, and therefore we walk through the
540 // context as well as walking through the scopes.
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000541
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000542 for (; S; S = S->getParent()) {
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000543 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Douglas Gregora24eb4e2009-08-24 18:55:03 +0000544 if (Ctx->isTransparentContext())
545 continue;
546
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000547 assert(Ctx && Ctx->isFileContext() &&
548 "We should have been looking only at file context here already.");
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000549
550 // Check whether the IdResolver has anything in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000551 bool Found = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000553 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
554 // We found something. Look for anything else in our scope
555 // with this same name and in an acceptable identifier
556 // namespace, so that we can construct an overload set if we
557 // need to.
John McCallf36e02d2009-10-09 21:13:30 +0000558 Found = true;
559 R.addDecl(*I);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000560 }
561 }
562
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000563 // Look into context considering using-directives.
John McCalla24dc2e2009-11-17 02:14:36 +0000564 if (CppNamespaceLookup(R, Context, Ctx, UDirs))
John McCallf36e02d2009-10-09 21:13:30 +0000565 Found = true;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000566
John McCallf36e02d2009-10-09 21:13:30 +0000567 if (Found) {
568 R.resolveKind();
569 return true;
570 }
571
John McCalla24dc2e2009-11-17 02:14:36 +0000572 if (R.isForRedeclaration() && !Ctx->isTransparentContext())
John McCallf36e02d2009-10-09 21:13:30 +0000573 return false;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000574 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000575
John McCallf36e02d2009-10-09 21:13:30 +0000576 return !R.empty();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000577}
578
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000579/// @brief Perform unqualified name lookup starting from a given
580/// scope.
581///
582/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
583/// used to find names within the current scope. For example, 'x' in
584/// @code
585/// int x;
586/// int f() {
587/// return x; // unqualified name look finds 'x' in the global scope
588/// }
589/// @endcode
590///
591/// Different lookup criteria can find different names. For example, a
592/// particular scope can have both a struct and a function of the same
593/// name, and each can be found by certain lookup criteria. For more
594/// information about lookup criteria, see the documentation for the
595/// class LookupCriteria.
596///
597/// @param S The scope from which unqualified name lookup will
598/// begin. If the lookup criteria permits, name lookup may also search
599/// in the parent scopes.
600///
601/// @param Name The name of the entity that we are searching for.
602///
Douglas Gregor3e41d602009-02-13 23:20:09 +0000603/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +0000604/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +0000605/// C library functions (like "malloc") are implicitly declared.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000606///
607/// @returns The result of name lookup, which includes zero or more
608/// declarations and possibly additional information used to diagnose
609/// ambiguities.
John McCalla24dc2e2009-11-17 02:14:36 +0000610bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) {
611 DeclarationName Name = R.getLookupName();
John McCallf36e02d2009-10-09 21:13:30 +0000612 if (!Name) return false;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000613
John McCalla24dc2e2009-11-17 02:14:36 +0000614 LookupNameKind NameKind = R.getLookupKind();
615
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000616 if (!getLangOptions().CPlusPlus) {
617 // Unqualified name lookup in C/Objective-C is purely lexical, so
618 // search in the declarations attached to the name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000619 unsigned IDNS = 0;
620 switch (NameKind) {
621 case Sema::LookupOrdinaryName:
622 IDNS = Decl::IDNS_Ordinary;
623 break;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000624
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000625 case Sema::LookupTagName:
626 IDNS = Decl::IDNS_Tag;
627 break;
628
629 case Sema::LookupMemberName:
630 IDNS = Decl::IDNS_Member;
631 break;
632
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000633 case Sema::LookupOperatorName:
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000634 case Sema::LookupNestedNameSpecifierName:
635 case Sema::LookupNamespaceName:
636 assert(false && "C does not perform these kinds of name lookup");
637 break;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000638
639 case Sema::LookupRedeclarationWithLinkage:
640 // Find the nearest non-transparent declaration scope.
641 while (!(S->getFlags() & Scope::DeclScope) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000642 (S->getEntity() &&
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000643 static_cast<DeclContext *>(S->getEntity())
644 ->isTransparentContext()))
645 S = S->getParent();
646 IDNS = Decl::IDNS_Ordinary;
647 break;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000648
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000649 case Sema::LookupObjCProtocolName:
650 IDNS = Decl::IDNS_ObjCProtocol;
651 break;
652
653 case Sema::LookupObjCImplementationName:
654 IDNS = Decl::IDNS_ObjCImplementation;
655 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000657 case Sema::LookupObjCCategoryImplName:
658 IDNS = Decl::IDNS_ObjCCategoryImpl;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000659 break;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000660 }
661
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000662 // Scan up the scope chain looking for a decl that matches this
663 // identifier that is in the appropriate namespace. This search
664 // should not take long, as shadowing of names is uncommon, and
665 // deep shadowing is extremely uncommon.
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000666 bool LeftStartingScope = false;
667
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000668 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Mike Stump1eb44332009-09-09 15:08:12 +0000669 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000670 I != IEnd; ++I)
Douglas Gregorf9201e02009-02-11 23:02:49 +0000671 if ((*I)->isInIdentifierNamespace(IDNS)) {
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000672 if (NameKind == LookupRedeclarationWithLinkage) {
673 // Determine whether this (or a previous) declaration is
674 // out-of-scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000675 if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000676 LeftStartingScope = true;
677
678 // If we found something outside of our starting scope that
679 // does not have linkage, skip it.
680 if (LeftStartingScope && !((*I)->hasLinkage()))
681 continue;
682 }
683
John McCallf36e02d2009-10-09 21:13:30 +0000684 R.addDecl(*I);
685
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000686 if ((*I)->getAttr<OverloadableAttr>()) {
Douglas Gregorf9201e02009-02-11 23:02:49 +0000687 // If this declaration has the "overloadable" attribute, we
688 // might have a set of overloaded functions.
689
690 // Figure out what scope the identifier is in.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000691 while (!(S->getFlags() & Scope::DeclScope) ||
692 !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000693 S = S->getParent();
694
695 // Find the last declaration in this scope (with the same
696 // name, naturally).
697 IdentifierResolver::iterator LastI = I;
698 for (++LastI; LastI != IEnd; ++LastI) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000699 if (!S->isDeclScope(DeclPtrTy::make(*LastI)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000700 break;
John McCallf36e02d2009-10-09 21:13:30 +0000701 R.addDecl(*LastI);
Douglas Gregorf9201e02009-02-11 23:02:49 +0000702 }
Douglas Gregorf9201e02009-02-11 23:02:49 +0000703 }
704
John McCallf36e02d2009-10-09 21:13:30 +0000705 R.resolveKind();
706
707 return true;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000708 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000709 } else {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000710 // Perform C++ unqualified name lookup.
John McCalla24dc2e2009-11-17 02:14:36 +0000711 if (CppLookupName(R, S))
John McCallf36e02d2009-10-09 21:13:30 +0000712 return true;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000713 }
714
715 // If we didn't find a use of this identifier, and if the identifier
716 // corresponds to a compiler builtin, create the decl object for the builtin
717 // now, injecting it into translation unit scope, and return it.
Mike Stump1eb44332009-09-09 15:08:12 +0000718 if (NameKind == LookupOrdinaryName ||
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000719 NameKind == LookupRedeclarationWithLinkage) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000720 IdentifierInfo *II = Name.getAsIdentifierInfo();
Douglas Gregor3e41d602009-02-13 23:20:09 +0000721 if (II && AllowBuiltinCreation) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000722 // If this is a builtin on this (or all) targets, create the decl.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000723 if (unsigned BuiltinID = II->getBuiltinID()) {
724 // In C++, we don't have any predefined library functions like
725 // 'malloc'. Instead, we'll just error.
Mike Stump1eb44332009-09-09 15:08:12 +0000726 if (getLangOptions().CPlusPlus &&
Douglas Gregor3e41d602009-02-13 23:20:09 +0000727 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
John McCallf36e02d2009-10-09 21:13:30 +0000728 return false;
Douglas Gregor3e41d602009-02-13 23:20:09 +0000729
John McCallf36e02d2009-10-09 21:13:30 +0000730 NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
John McCalla24dc2e2009-11-17 02:14:36 +0000731 S, R.isForRedeclaration(),
732 R.getNameLoc());
John McCallf36e02d2009-10-09 21:13:30 +0000733 if (D) R.addDecl(D);
734 return (D != NULL);
Douglas Gregor3e41d602009-02-13 23:20:09 +0000735 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000736 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000737 }
John McCallf36e02d2009-10-09 21:13:30 +0000738 return false;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000739}
740
John McCall6e247262009-10-10 05:48:19 +0000741/// @brief Perform qualified name lookup in the namespaces nominated by
742/// using directives by the given context.
743///
744/// C++98 [namespace.qual]p2:
745/// Given X::m (where X is a user-declared namespace), or given ::m
746/// (where X is the global namespace), let S be the set of all
747/// declarations of m in X and in the transitive closure of all
748/// namespaces nominated by using-directives in X and its used
749/// namespaces, except that using-directives are ignored in any
750/// namespace, including X, directly containing one or more
751/// declarations of m. No namespace is searched more than once in
752/// the lookup of a name. If S is the empty set, the program is
753/// ill-formed. Otherwise, if S has exactly one member, or if the
754/// context of the reference is a using-declaration
755/// (namespace.udecl), S is the required set of declarations of
756/// m. Otherwise if the use of m is not one that allows a unique
757/// declaration to be chosen from S, the program is ill-formed.
758/// C++98 [namespace.qual]p5:
759/// During the lookup of a qualified namespace member name, if the
760/// lookup finds more than one declaration of the member, and if one
761/// declaration introduces a class name or enumeration name and the
762/// other declarations either introduce the same object, the same
763/// enumerator or a set of functions, the non-type name hides the
764/// class or enumeration name if and only if the declarations are
765/// from the same namespace; otherwise (the declarations are from
766/// different namespaces), the program is ill-formed.
767static bool LookupQualifiedNameInUsingDirectives(Sema::LookupResult &R,
John McCalla24dc2e2009-11-17 02:14:36 +0000768 DeclContext *StartDC) {
John McCall6e247262009-10-10 05:48:19 +0000769 assert(StartDC->isFileContext() && "start context is not a file context");
770
771 DeclContext::udir_iterator I = StartDC->using_directives_begin();
772 DeclContext::udir_iterator E = StartDC->using_directives_end();
773
774 if (I == E) return false;
775
776 // We have at least added all these contexts to the queue.
777 llvm::DenseSet<DeclContext*> Visited;
778 Visited.insert(StartDC);
779
780 // We have not yet looked into these namespaces, much less added
781 // their "using-children" to the queue.
782 llvm::SmallVector<NamespaceDecl*, 8> Queue;
783
784 // We have already looked into the initial namespace; seed the queue
785 // with its using-children.
786 for (; I != E; ++I) {
John McCalld9f01d42009-11-10 09:25:37 +0000787 NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace();
John McCall6e247262009-10-10 05:48:19 +0000788 if (Visited.insert(ND).second)
789 Queue.push_back(ND);
790 }
791
792 // The easiest way to implement the restriction in [namespace.qual]p5
793 // is to check whether any of the individual results found a tag
794 // and, if so, to declare an ambiguity if the final result is not
795 // a tag.
796 bool FoundTag = false;
797 bool FoundNonTag = false;
798
John McCalla24dc2e2009-11-17 02:14:36 +0000799 Sema::LookupResult LocalR(Sema::LookupResult::Temporary, R);
John McCall6e247262009-10-10 05:48:19 +0000800
801 bool Found = false;
802 while (!Queue.empty()) {
803 NamespaceDecl *ND = Queue.back();
804 Queue.pop_back();
805
806 // We go through some convolutions here to avoid copying results
807 // between LookupResults.
808 bool UseLocal = !R.empty();
809 Sema::LookupResult &DirectR = UseLocal ? LocalR : R;
John McCalla24dc2e2009-11-17 02:14:36 +0000810 bool FoundDirect = LookupDirect(DirectR, ND);
John McCall6e247262009-10-10 05:48:19 +0000811
812 if (FoundDirect) {
813 // First do any local hiding.
814 DirectR.resolveKind();
815
816 // If the local result is a tag, remember that.
817 if (DirectR.isSingleTagDecl())
818 FoundTag = true;
819 else
820 FoundNonTag = true;
821
822 // Append the local results to the total results if necessary.
823 if (UseLocal) {
824 R.addAllDecls(LocalR);
825 LocalR.clear();
826 }
827 }
828
829 // If we find names in this namespace, ignore its using directives.
830 if (FoundDirect) {
831 Found = true;
832 continue;
833 }
834
835 for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) {
836 NamespaceDecl *Nom = (*I)->getNominatedNamespace();
837 if (Visited.insert(Nom).second)
838 Queue.push_back(Nom);
839 }
840 }
841
842 if (Found) {
843 if (FoundTag && FoundNonTag)
844 R.setAmbiguousQualifiedTagHiding();
845 else
846 R.resolveKind();
847 }
848
849 return Found;
850}
851
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000852/// @brief Perform qualified name lookup into a given context.
853///
854/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
855/// names when the context of those names is explicit specified, e.g.,
856/// "std::vector" or "x->member".
857///
858/// Different lookup criteria can find different names. For example, a
859/// particular scope can have both a struct and a function of the same
860/// name, and each can be found by certain lookup criteria. For more
861/// information about lookup criteria, see the documentation for the
862/// class LookupCriteria.
863///
864/// @param LookupCtx The context in which qualified name lookup will
865/// search. If the lookup criteria permits, name lookup may also search
866/// in the parent contexts or (for C++ classes) base classes.
867///
868/// @param Name The name of the entity that we are searching for.
869///
870/// @param Criteria The criteria that this routine will use to
871/// determine which names are visible and which names will be
872/// found. Note that name lookup will find a name that is visible by
873/// the given criteria, but the entity itself may not be semantically
874/// correct or even the kind of entity expected based on the
875/// lookup. For example, searching for a nested-name-specifier name
876/// might result in an EnumDecl, which is visible but is not permitted
877/// as a nested-name-specifier in C++03.
878///
879/// @returns The result of name lookup, which includes zero or more
880/// declarations and possibly additional information used to diagnose
881/// ambiguities.
John McCalla24dc2e2009-11-17 02:14:36 +0000882bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000883 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
Mike Stump1eb44332009-09-09 15:08:12 +0000884
John McCalla24dc2e2009-11-17 02:14:36 +0000885 if (!R.getLookupName())
John McCallf36e02d2009-10-09 21:13:30 +0000886 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000888 // If we're performing qualified name lookup (e.g., lookup into a
889 // struct), find fields as part of ordinary name lookup.
John McCalla24dc2e2009-11-17 02:14:36 +0000890 LookupNameKind NameKind = R.getLookupKind();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000891 unsigned IDNS
Mike Stump1eb44332009-09-09 15:08:12 +0000892 = getIdentifierNamespacesFromLookupNameKind(NameKind,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000893 getLangOptions().CPlusPlus);
894 if (NameKind == LookupOrdinaryName)
895 IDNS |= Decl::IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
John McCalla24dc2e2009-11-17 02:14:36 +0000897 R.setIdentifierNamespace(IDNS);
898
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000899 // Make sure that the declaration context is complete.
900 assert((!isa<TagDecl>(LookupCtx) ||
901 LookupCtx->isDependentContext() ||
902 cast<TagDecl>(LookupCtx)->isDefinition() ||
903 Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>()
904 ->isBeingDefined()) &&
905 "Declaration context must already be complete!");
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000907 // Perform qualified name lookup into the LookupCtx.
John McCalla24dc2e2009-11-17 02:14:36 +0000908 if (LookupDirect(R, LookupCtx)) {
John McCallf36e02d2009-10-09 21:13:30 +0000909 R.resolveKind();
910 return true;
911 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000912
John McCall6e247262009-10-10 05:48:19 +0000913 // Don't descend into implied contexts for redeclarations.
914 // C++98 [namespace.qual]p6:
915 // In a declaration for a namespace member in which the
916 // declarator-id is a qualified-id, given that the qualified-id
917 // for the namespace member has the form
918 // nested-name-specifier unqualified-id
919 // the unqualified-id shall name a member of the namespace
920 // designated by the nested-name-specifier.
921 // See also [class.mfct]p5 and [class.static.data]p2.
John McCalla24dc2e2009-11-17 02:14:36 +0000922 if (R.isForRedeclaration())
John McCall6e247262009-10-10 05:48:19 +0000923 return false;
924
John McCalla24dc2e2009-11-17 02:14:36 +0000925 // If this is a namespace, look it up in the implied namespaces.
John McCall6e247262009-10-10 05:48:19 +0000926 if (LookupCtx->isFileContext())
John McCalla24dc2e2009-11-17 02:14:36 +0000927 return LookupQualifiedNameInUsingDirectives(R, LookupCtx);
John McCall6e247262009-10-10 05:48:19 +0000928
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000929 // If this isn't a C++ class, we aren't allowed to look into base
Douglas Gregor4719f4e2009-09-11 22:57:37 +0000930 // classes, we're done.
John McCall6e247262009-10-10 05:48:19 +0000931 if (!isa<CXXRecordDecl>(LookupCtx))
John McCallf36e02d2009-10-09 21:13:30 +0000932 return false;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000933
934 // Perform lookup into our base classes.
Douglas Gregora8f32e02009-10-06 17:59:45 +0000935 CXXRecordDecl *LookupRec = cast<CXXRecordDecl>(LookupCtx);
936 CXXBasePaths Paths;
937 Paths.setOrigin(LookupRec);
Douglas Gregor7176fff2009-01-15 00:26:24 +0000938
939 // Look for this member in our base classes
Douglas Gregora8f32e02009-10-06 17:59:45 +0000940 CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0;
John McCalla24dc2e2009-11-17 02:14:36 +0000941 switch (R.getLookupKind()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000942 case LookupOrdinaryName:
943 case LookupMemberName:
944 case LookupRedeclarationWithLinkage:
945 BaseCallback = &CXXRecordDecl::FindOrdinaryMember;
946 break;
947
948 case LookupTagName:
949 BaseCallback = &CXXRecordDecl::FindTagMember;
950 break;
951
952 case LookupOperatorName:
953 case LookupNamespaceName:
954 case LookupObjCProtocolName:
955 case LookupObjCImplementationName:
956 case LookupObjCCategoryImplName:
957 // These lookups will never find a member in a C++ class (or base class).
John McCallf36e02d2009-10-09 21:13:30 +0000958 return false;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000959
960 case LookupNestedNameSpecifierName:
961 BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember;
962 break;
963 }
964
John McCalla24dc2e2009-11-17 02:14:36 +0000965 if (!LookupRec->lookupInBases(BaseCallback,
966 R.getLookupName().getAsOpaquePtr(), Paths))
John McCallf36e02d2009-10-09 21:13:30 +0000967 return false;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000968
969 // C++ [class.member.lookup]p2:
970 // [...] If the resulting set of declarations are not all from
971 // sub-objects of the same type, or the set has a nonstatic member
972 // and includes members from distinct sub-objects, there is an
973 // ambiguity and the program is ill-formed. Otherwise that set is
974 // the result of the lookup.
975 // FIXME: support using declarations!
976 QualType SubobjectType;
Daniel Dunbarf1853192009-01-15 18:32:35 +0000977 int SubobjectNumber = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000978 for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000979 Path != PathEnd; ++Path) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000980 const CXXBasePathElement &PathElement = Path->back();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000981
982 // Determine whether we're looking at a distinct sub-object or not.
983 if (SubobjectType.isNull()) {
John McCallf36e02d2009-10-09 21:13:30 +0000984 // This is the first subobject we've looked at. Record its type.
Douglas Gregor7176fff2009-01-15 00:26:24 +0000985 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
986 SubobjectNumber = PathElement.SubobjectNumber;
Mike Stump1eb44332009-09-09 15:08:12 +0000987 } else if (SubobjectType
Douglas Gregor7176fff2009-01-15 00:26:24 +0000988 != Context.getCanonicalType(PathElement.Base->getType())) {
989 // We found members of the given name in two subobjects of
990 // different types. This lookup is ambiguous.
John McCallf36e02d2009-10-09 21:13:30 +0000991 R.setAmbiguousBaseSubobjectTypes(Paths);
992 return true;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000993 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
994 // We have a different subobject of the same type.
995
996 // C++ [class.member.lookup]p5:
997 // A static member, a nested type or an enumerator defined in
998 // a base class T can unambiguously be found even if an object
Mike Stump1eb44332009-09-09 15:08:12 +0000999 // has more than one base class subobject of type T.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001000 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001001 if (isa<VarDecl>(FirstDecl) ||
1002 isa<TypeDecl>(FirstDecl) ||
1003 isa<EnumConstantDecl>(FirstDecl))
1004 continue;
1005
1006 if (isa<CXXMethodDecl>(FirstDecl)) {
1007 // Determine whether all of the methods are static.
1008 bool AllMethodsAreStatic = true;
1009 for (DeclContext::lookup_iterator Func = Path->Decls.first;
1010 Func != Path->Decls.second; ++Func) {
1011 if (!isa<CXXMethodDecl>(*Func)) {
1012 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
1013 break;
1014 }
1015
1016 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
1017 AllMethodsAreStatic = false;
1018 break;
1019 }
1020 }
1021
1022 if (AllMethodsAreStatic)
1023 continue;
1024 }
1025
1026 // We have found a nonstatic member name in multiple, distinct
1027 // subobjects. Name lookup is ambiguous.
John McCallf36e02d2009-10-09 21:13:30 +00001028 R.setAmbiguousBaseSubobjects(Paths);
1029 return true;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001030 }
1031 }
1032
1033 // Lookup in a base class succeeded; return these results.
1034
John McCallf36e02d2009-10-09 21:13:30 +00001035 DeclContext::lookup_iterator I, E;
1036 for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I)
1037 R.addDecl(*I);
1038 R.resolveKind();
1039 return true;
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001040}
1041
1042/// @brief Performs name lookup for a name that was parsed in the
1043/// source code, and may contain a C++ scope specifier.
1044///
1045/// This routine is a convenience routine meant to be called from
1046/// contexts that receive a name and an optional C++ scope specifier
1047/// (e.g., "N::M::x"). It will then perform either qualified or
1048/// unqualified name lookup (with LookupQualifiedName or LookupName,
1049/// respectively) on the given name and return those results.
1050///
1051/// @param S The scope from which unqualified name lookup will
1052/// begin.
Mike Stump1eb44332009-09-09 15:08:12 +00001053///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001054/// @param SS An optional C++ scope-specifier, e.g., "::N::M".
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001055///
1056/// @param Name The name of the entity that name lookup will
1057/// search for.
1058///
Douglas Gregor3e41d602009-02-13 23:20:09 +00001059/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +00001060/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +00001061/// C library functions (like "malloc") are implicitly declared.
1062///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001063/// @param EnteringContext Indicates whether we are going to enter the
1064/// context of the scope-specifier SS (if present).
1065///
John McCallf36e02d2009-10-09 21:13:30 +00001066/// @returns True if any decls were found (but possibly ambiguous)
1067bool Sema::LookupParsedName(LookupResult &R, Scope *S, const CXXScopeSpec *SS,
John McCalla24dc2e2009-11-17 02:14:36 +00001068 bool AllowBuiltinCreation, bool EnteringContext) {
Douglas Gregor495c35d2009-08-25 22:51:20 +00001069 if (SS && SS->isInvalid()) {
1070 // When the scope specifier is invalid, don't even look for
Douglas Gregor42af25f2009-05-11 19:58:34 +00001071 // anything.
John McCallf36e02d2009-10-09 21:13:30 +00001072 return false;
Douglas Gregor495c35d2009-08-25 22:51:20 +00001073 }
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregor495c35d2009-08-25 22:51:20 +00001075 if (SS && SS->isSet()) {
1076 if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001077 // We have resolved the scope specifier to a particular declaration
Douglas Gregor495c35d2009-08-25 22:51:20 +00001078 // contex, and will perform name lookup in that context.
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001079 if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS))
John McCallf36e02d2009-10-09 21:13:30 +00001080 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001081
John McCalla24dc2e2009-11-17 02:14:36 +00001082 R.setContextRange(SS->getRange());
1083
1084 return LookupQualifiedName(R, DC);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001085 }
Douglas Gregor42af25f2009-05-11 19:58:34 +00001086
Douglas Gregor495c35d2009-08-25 22:51:20 +00001087 // We could not resolve the scope specified to a specific declaration
Mike Stump1eb44332009-09-09 15:08:12 +00001088 // context, which means that SS refers to an unknown specialization.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001089 // Name lookup can't find anything in this case.
John McCallf36e02d2009-10-09 21:13:30 +00001090 return false;
Douglas Gregor4c921ae2009-01-30 01:04:22 +00001091 }
1092
Mike Stump1eb44332009-09-09 15:08:12 +00001093 // Perform unqualified name lookup starting in the given scope.
John McCalla24dc2e2009-11-17 02:14:36 +00001094 return LookupName(R, S, AllowBuiltinCreation);
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001095}
1096
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001097
Douglas Gregor7176fff2009-01-15 00:26:24 +00001098/// @brief Produce a diagnostic describing the ambiguity that resulted
1099/// from name lookup.
1100///
1101/// @param Result The ambiguous name lookup result.
Mike Stump1eb44332009-09-09 15:08:12 +00001102///
Douglas Gregor7176fff2009-01-15 00:26:24 +00001103/// @param Name The name of the entity that name lookup was
1104/// searching for.
1105///
1106/// @param NameLoc The location of the name within the source code.
1107///
1108/// @param LookupRange A source range that provides more
1109/// source-location information concerning the lookup itself. For
1110/// example, this range might highlight a nested-name-specifier that
1111/// precedes the name.
1112///
1113/// @returns true
John McCalla24dc2e2009-11-17 02:14:36 +00001114bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) {
Douglas Gregor7176fff2009-01-15 00:26:24 +00001115 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
1116
John McCalla24dc2e2009-11-17 02:14:36 +00001117 DeclarationName Name = Result.getLookupName();
1118 SourceLocation NameLoc = Result.getNameLoc();
1119 SourceRange LookupRange = Result.getContextRange();
1120
John McCall6e247262009-10-10 05:48:19 +00001121 switch (Result.getAmbiguityKind()) {
1122 case LookupResult::AmbiguousBaseSubobjects: {
1123 CXXBasePaths *Paths = Result.getBasePaths();
1124 QualType SubobjectType = Paths->front().back().Base->getType();
1125 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
1126 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
1127 << LookupRange;
1128
1129 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
1130 while (isa<CXXMethodDecl>(*Found) &&
1131 cast<CXXMethodDecl>(*Found)->isStatic())
1132 ++Found;
1133
1134 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
1135
1136 return true;
1137 }
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001138
John McCall6e247262009-10-10 05:48:19 +00001139 case LookupResult::AmbiguousBaseSubobjectTypes: {
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001140 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
1141 << Name << LookupRange;
John McCall6e247262009-10-10 05:48:19 +00001142
1143 CXXBasePaths *Paths = Result.getBasePaths();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001144 std::set<Decl *> DeclsPrinted;
John McCall6e247262009-10-10 05:48:19 +00001145 for (CXXBasePaths::paths_iterator Path = Paths->begin(),
1146 PathEnd = Paths->end();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001147 Path != PathEnd; ++Path) {
1148 Decl *D = *Path->Decls.first;
1149 if (DeclsPrinted.insert(D).second)
1150 Diag(D->getLocation(), diag::note_ambiguous_member_found);
1151 }
1152
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001153 return true;
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001154 }
1155
John McCall6e247262009-10-10 05:48:19 +00001156 case LookupResult::AmbiguousTagHiding: {
1157 Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange;
Douglas Gregor69d993a2009-01-17 01:13:24 +00001158
John McCall6e247262009-10-10 05:48:19 +00001159 llvm::SmallPtrSet<NamedDecl*,8> TagDecls;
1160
1161 LookupResult::iterator DI, DE = Result.end();
1162 for (DI = Result.begin(); DI != DE; ++DI)
1163 if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) {
1164 TagDecls.insert(TD);
1165 Diag(TD->getLocation(), diag::note_hidden_tag);
1166 }
1167
1168 for (DI = Result.begin(); DI != DE; ++DI)
1169 if (!isa<TagDecl>(*DI))
1170 Diag((*DI)->getLocation(), diag::note_hiding_object);
1171
1172 // For recovery purposes, go ahead and implement the hiding.
1173 Result.hideDecls(TagDecls);
1174
1175 return true;
1176 }
1177
1178 case LookupResult::AmbiguousReference: {
1179 Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange;
John McCallf36e02d2009-10-09 21:13:30 +00001180
John McCall6e247262009-10-10 05:48:19 +00001181 LookupResult::iterator DI = Result.begin(), DE = Result.end();
1182 for (; DI != DE; ++DI)
1183 Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI;
John McCallf36e02d2009-10-09 21:13:30 +00001184
John McCall6e247262009-10-10 05:48:19 +00001185 return true;
1186 }
1187 }
1188
1189 llvm::llvm_unreachable("unknown ambiguity kind");
Douglas Gregor7176fff2009-01-15 00:26:24 +00001190 return true;
1191}
Douglas Gregorfa047642009-02-04 00:32:51 +00001192
Mike Stump1eb44332009-09-09 15:08:12 +00001193static void
1194addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001195 ASTContext &Context,
1196 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001197 Sema::AssociatedClassSet &AssociatedClasses);
1198
1199static void CollectNamespace(Sema::AssociatedNamespaceSet &Namespaces,
1200 DeclContext *Ctx) {
1201 if (Ctx->isFileContext())
1202 Namespaces.insert(Ctx);
1203}
Douglas Gregor69be8d62009-07-08 07:51:57 +00001204
Mike Stump1eb44332009-09-09 15:08:12 +00001205// \brief Add the associated classes and namespaces for argument-dependent
Douglas Gregor69be8d62009-07-08 07:51:57 +00001206// lookup that involves a template argument (C++ [basic.lookup.koenig]p2).
Mike Stump1eb44332009-09-09 15:08:12 +00001207static void
1208addAssociatedClassesAndNamespaces(const TemplateArgument &Arg,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001209 ASTContext &Context,
1210 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001211 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregor69be8d62009-07-08 07:51:57 +00001212 // C++ [basic.lookup.koenig]p2, last bullet:
Mike Stump1eb44332009-09-09 15:08:12 +00001213 // -- [...] ;
Douglas Gregor69be8d62009-07-08 07:51:57 +00001214 switch (Arg.getKind()) {
1215 case TemplateArgument::Null:
1216 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregor69be8d62009-07-08 07:51:57 +00001218 case TemplateArgument::Type:
1219 // [...] the namespaces and classes associated with the types of the
1220 // template arguments provided for template type parameters (excluding
1221 // template template parameters)
1222 addAssociatedClassesAndNamespaces(Arg.getAsType(), Context,
1223 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001224 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001225 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor788cd062009-11-11 01:00:40 +00001227 case TemplateArgument::Template: {
Mike Stump1eb44332009-09-09 15:08:12 +00001228 // [...] the namespaces in which any template template arguments are
1229 // defined; and the classes in which any member templates used as
Douglas Gregor69be8d62009-07-08 07:51:57 +00001230 // template template arguments are defined.
Douglas Gregor788cd062009-11-11 01:00:40 +00001231 TemplateName Template = Arg.getAsTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +00001232 if (ClassTemplateDecl *ClassTemplate
Douglas Gregor788cd062009-11-11 01:00:40 +00001233 = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) {
Douglas Gregor69be8d62009-07-08 07:51:57 +00001234 DeclContext *Ctx = ClassTemplate->getDeclContext();
1235 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1236 AssociatedClasses.insert(EnclosingClass);
1237 // Add the associated namespace for this class.
1238 while (Ctx->isRecord())
1239 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001240 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001241 }
1242 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00001243 }
1244
1245 case TemplateArgument::Declaration:
Douglas Gregor69be8d62009-07-08 07:51:57 +00001246 case TemplateArgument::Integral:
1247 case TemplateArgument::Expression:
Mike Stump1eb44332009-09-09 15:08:12 +00001248 // [Note: non-type template arguments do not contribute to the set of
Douglas Gregor69be8d62009-07-08 07:51:57 +00001249 // associated namespaces. ]
1250 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregor69be8d62009-07-08 07:51:57 +00001252 case TemplateArgument::Pack:
1253 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1254 PEnd = Arg.pack_end();
1255 P != PEnd; ++P)
1256 addAssociatedClassesAndNamespaces(*P, Context,
1257 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001258 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001259 break;
1260 }
1261}
1262
Douglas Gregorfa047642009-02-04 00:32:51 +00001263// \brief Add the associated classes and namespaces for
Mike Stump1eb44332009-09-09 15:08:12 +00001264// argument-dependent lookup with an argument of class type
1265// (C++ [basic.lookup.koenig]p2).
1266static void
1267addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
Douglas Gregorfa047642009-02-04 00:32:51 +00001268 ASTContext &Context,
1269 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001270 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001271 // C++ [basic.lookup.koenig]p2:
1272 // [...]
1273 // -- If T is a class type (including unions), its associated
1274 // classes are: the class itself; the class of which it is a
1275 // member, if any; and its direct and indirect base
1276 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001277 // which its associated classes are defined.
Douglas Gregorfa047642009-02-04 00:32:51 +00001278
1279 // Add the class of which it is a member, if any.
1280 DeclContext *Ctx = Class->getDeclContext();
1281 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1282 AssociatedClasses.insert(EnclosingClass);
Douglas Gregorfa047642009-02-04 00:32:51 +00001283 // Add the associated namespace for this class.
1284 while (Ctx->isRecord())
1285 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001286 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregorfa047642009-02-04 00:32:51 +00001288 // Add the class itself. If we've already seen this class, we don't
1289 // need to visit base classes.
1290 if (!AssociatedClasses.insert(Class))
1291 return;
1292
Mike Stump1eb44332009-09-09 15:08:12 +00001293 // -- If T is a template-id, its associated namespaces and classes are
1294 // the namespace in which the template is defined; for member
Douglas Gregor69be8d62009-07-08 07:51:57 +00001295 // templates, the member template’s class; the namespaces and classes
Mike Stump1eb44332009-09-09 15:08:12 +00001296 // associated with the types of the template arguments provided for
Douglas Gregor69be8d62009-07-08 07:51:57 +00001297 // template type parameters (excluding template template parameters); the
Mike Stump1eb44332009-09-09 15:08:12 +00001298 // namespaces in which any template template arguments are defined; and
1299 // the classes in which any member templates used as template template
1300 // arguments are defined. [Note: non-type template arguments do not
Douglas Gregor69be8d62009-07-08 07:51:57 +00001301 // contribute to the set of associated namespaces. ]
Mike Stump1eb44332009-09-09 15:08:12 +00001302 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor69be8d62009-07-08 07:51:57 +00001303 = dyn_cast<ClassTemplateSpecializationDecl>(Class)) {
1304 DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext();
1305 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1306 AssociatedClasses.insert(EnclosingClass);
1307 // Add the associated namespace for this class.
1308 while (Ctx->isRecord())
1309 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001310 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Douglas Gregor69be8d62009-07-08 07:51:57 +00001312 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1313 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1314 addAssociatedClassesAndNamespaces(TemplateArgs[I], Context,
1315 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001316 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregorfa047642009-02-04 00:32:51 +00001319 // Add direct and indirect base classes along with their associated
1320 // namespaces.
1321 llvm::SmallVector<CXXRecordDecl *, 32> Bases;
1322 Bases.push_back(Class);
1323 while (!Bases.empty()) {
1324 // Pop this class off the stack.
1325 Class = Bases.back();
1326 Bases.pop_back();
1327
1328 // Visit the base classes.
1329 for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(),
1330 BaseEnd = Class->bases_end();
1331 Base != BaseEnd; ++Base) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001332 const RecordType *BaseType = Base->getType()->getAs<RecordType>();
Sebastian Redlbbc1cc52009-10-25 09:35:33 +00001333 // In dependent contexts, we do ADL twice, and the first time around,
1334 // the base type might be a dependent TemplateSpecializationType, or a
1335 // TemplateTypeParmType. If that happens, simply ignore it.
1336 // FIXME: If we want to support export, we probably need to add the
1337 // namespace of the template in a TemplateSpecializationType, or even
1338 // the classes and namespaces of known non-dependent arguments.
1339 if (!BaseType)
1340 continue;
Douglas Gregorfa047642009-02-04 00:32:51 +00001341 CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl());
1342 if (AssociatedClasses.insert(BaseDecl)) {
1343 // Find the associated namespace for this base class.
1344 DeclContext *BaseCtx = BaseDecl->getDeclContext();
1345 while (BaseCtx->isRecord())
1346 BaseCtx = BaseCtx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001347 CollectNamespace(AssociatedNamespaces, BaseCtx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001348
1349 // Make sure we visit the bases of this base class.
1350 if (BaseDecl->bases_begin() != BaseDecl->bases_end())
1351 Bases.push_back(BaseDecl);
1352 }
1353 }
1354 }
1355}
1356
1357// \brief Add the associated classes and namespaces for
1358// argument-dependent lookup with an argument of type T
Mike Stump1eb44332009-09-09 15:08:12 +00001359// (C++ [basic.lookup.koenig]p2).
1360static void
1361addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregorfa047642009-02-04 00:32:51 +00001362 ASTContext &Context,
1363 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001364 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001365 // C++ [basic.lookup.koenig]p2:
1366 //
1367 // For each argument type T in the function call, there is a set
1368 // of zero or more associated namespaces and a set of zero or more
1369 // associated classes to be considered. The sets of namespaces and
1370 // classes is determined entirely by the types of the function
1371 // arguments (and the namespace of any template template
1372 // argument). Typedef names and using-declarations used to specify
1373 // the types do not contribute to this set. The sets of namespaces
1374 // and classes are determined in the following way:
1375 T = Context.getCanonicalType(T).getUnqualifiedType();
1376
1377 // -- If T is a pointer to U or an array of U, its associated
Mike Stump1eb44332009-09-09 15:08:12 +00001378 // namespaces and classes are those associated with U.
Douglas Gregorfa047642009-02-04 00:32:51 +00001379 //
1380 // We handle this by unwrapping pointer and array types immediately,
1381 // to avoid unnecessary recursion.
1382 while (true) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001383 if (const PointerType *Ptr = T->getAs<PointerType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001384 T = Ptr->getPointeeType();
1385 else if (const ArrayType *Ptr = Context.getAsArrayType(T))
1386 T = Ptr->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001387 else
Douglas Gregorfa047642009-02-04 00:32:51 +00001388 break;
1389 }
1390
1391 // -- If T is a fundamental type, its associated sets of
1392 // namespaces and classes are both empty.
John McCall183700f2009-09-21 23:43:11 +00001393 if (T->getAs<BuiltinType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001394 return;
1395
1396 // -- If T is a class type (including unions), its associated
1397 // classes are: the class itself; the class of which it is a
1398 // member, if any; and its direct and indirect base
1399 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001400 // which its associated classes are defined.
Ted Kremenek6217b802009-07-29 21:53:49 +00001401 if (const RecordType *ClassType = T->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +00001402 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001403 = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001404 addAssociatedClassesAndNamespaces(ClassDecl, Context,
1405 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001406 AssociatedClasses);
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001407 return;
1408 }
Douglas Gregorfa047642009-02-04 00:32:51 +00001409
1410 // -- If T is an enumeration type, its associated namespace is
1411 // the namespace in which it is defined. If it is class
1412 // member, its associated class is the member’s class; else
Mike Stump1eb44332009-09-09 15:08:12 +00001413 // it has no associated class.
John McCall183700f2009-09-21 23:43:11 +00001414 if (const EnumType *EnumT = T->getAs<EnumType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001415 EnumDecl *Enum = EnumT->getDecl();
1416
1417 DeclContext *Ctx = Enum->getDeclContext();
1418 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1419 AssociatedClasses.insert(EnclosingClass);
1420
1421 // Add the associated namespace for this class.
1422 while (Ctx->isRecord())
1423 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001424 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001425
1426 return;
1427 }
1428
1429 // -- If T is a function type, its associated namespaces and
1430 // classes are those associated with the function parameter
1431 // types and those associated with the return type.
John McCall183700f2009-09-21 23:43:11 +00001432 if (const FunctionType *FnType = T->getAs<FunctionType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001433 // Return type
John McCall183700f2009-09-21 23:43:11 +00001434 addAssociatedClassesAndNamespaces(FnType->getResultType(),
Douglas Gregorfa047642009-02-04 00:32:51 +00001435 Context,
John McCall6ff07852009-08-07 22:18:02 +00001436 AssociatedNamespaces, AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001437
John McCall183700f2009-09-21 23:43:11 +00001438 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType);
Douglas Gregorfa047642009-02-04 00:32:51 +00001439 if (!Proto)
1440 return;
1441
1442 // Argument types
Douglas Gregor72564e72009-02-26 23:50:07 +00001443 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001444 ArgEnd = Proto->arg_type_end();
Douglas Gregorfa047642009-02-04 00:32:51 +00001445 Arg != ArgEnd; ++Arg)
1446 addAssociatedClassesAndNamespaces(*Arg, Context,
John McCall6ff07852009-08-07 22:18:02 +00001447 AssociatedNamespaces, AssociatedClasses);
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Douglas Gregorfa047642009-02-04 00:32:51 +00001449 return;
1450 }
1451
1452 // -- If T is a pointer to a member function of a class X, its
1453 // associated namespaces and classes are those associated
1454 // with the function parameter types and return type,
Mike Stump1eb44332009-09-09 15:08:12 +00001455 // together with those associated with X.
Douglas Gregorfa047642009-02-04 00:32:51 +00001456 //
1457 // -- If T is a pointer to a data member of class X, its
1458 // associated namespaces and classes are those associated
1459 // with the member type together with those associated with
Mike Stump1eb44332009-09-09 15:08:12 +00001460 // X.
Ted Kremenek6217b802009-07-29 21:53:49 +00001461 if (const MemberPointerType *MemberPtr = T->getAs<MemberPointerType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001462 // Handle the type that the pointer to member points to.
1463 addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(),
1464 Context,
John McCall6ff07852009-08-07 22:18:02 +00001465 AssociatedNamespaces,
1466 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001467
1468 // Handle the class type into which this points.
Ted Kremenek6217b802009-07-29 21:53:49 +00001469 if (const RecordType *Class = MemberPtr->getClass()->getAs<RecordType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001470 addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()),
1471 Context,
John McCall6ff07852009-08-07 22:18:02 +00001472 AssociatedNamespaces,
1473 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001474
1475 return;
1476 }
1477
1478 // FIXME: What about block pointers?
1479 // FIXME: What about Objective-C message sends?
1480}
1481
1482/// \brief Find the associated classes and namespaces for
1483/// argument-dependent lookup for a call with the given set of
1484/// arguments.
1485///
1486/// This routine computes the sets of associated classes and associated
Mike Stump1eb44332009-09-09 15:08:12 +00001487/// namespaces searched by argument-dependent lookup
Douglas Gregorfa047642009-02-04 00:32:51 +00001488/// (C++ [basic.lookup.argdep]) for a given set of arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001489void
Douglas Gregorfa047642009-02-04 00:32:51 +00001490Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs,
1491 AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001492 AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001493 AssociatedNamespaces.clear();
1494 AssociatedClasses.clear();
1495
1496 // C++ [basic.lookup.koenig]p2:
1497 // For each argument type T in the function call, there is a set
1498 // of zero or more associated namespaces and a set of zero or more
1499 // associated classes to be considered. The sets of namespaces and
1500 // classes is determined entirely by the types of the function
1501 // arguments (and the namespace of any template template
Mike Stump1eb44332009-09-09 15:08:12 +00001502 // argument).
Douglas Gregorfa047642009-02-04 00:32:51 +00001503 for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) {
1504 Expr *Arg = Args[ArgIdx];
1505
1506 if (Arg->getType() != Context.OverloadTy) {
1507 addAssociatedClassesAndNamespaces(Arg->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001508 AssociatedNamespaces,
1509 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001510 continue;
1511 }
1512
1513 // [...] In addition, if the argument is the name or address of a
1514 // set of overloaded functions and/or function templates, its
1515 // associated classes and namespaces are the union of those
1516 // associated with each of the members of the set: the namespace
1517 // in which the function or function template is defined and the
1518 // classes and namespaces associated with its (non-dependent)
1519 // parameter types and return type.
1520 DeclRefExpr *DRE = 0;
Douglas Gregordaa439a2009-07-08 10:57:20 +00001521 TemplateIdRefExpr *TIRE = 0;
1522 Arg = Arg->IgnoreParens();
Douglas Gregorfa047642009-02-04 00:32:51 +00001523 if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) {
Douglas Gregordaa439a2009-07-08 10:57:20 +00001524 if (unaryOp->getOpcode() == UnaryOperator::AddrOf) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001525 DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr());
Douglas Gregordaa439a2009-07-08 10:57:20 +00001526 TIRE = dyn_cast<TemplateIdRefExpr>(unaryOp->getSubExpr());
1527 }
1528 } else {
Douglas Gregorfa047642009-02-04 00:32:51 +00001529 DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregordaa439a2009-07-08 10:57:20 +00001530 TIRE = dyn_cast<TemplateIdRefExpr>(Arg);
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregordaa439a2009-07-08 10:57:20 +00001533 OverloadedFunctionDecl *Ovl = 0;
1534 if (DRE)
1535 Ovl = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl());
1536 else if (TIRE)
Douglas Gregord99cbe62009-07-29 18:26:50 +00001537 Ovl = TIRE->getTemplateName().getAsOverloadedFunctionDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001538 if (!Ovl)
1539 continue;
1540
1541 for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
1542 FuncEnd = Ovl->function_end();
1543 Func != FuncEnd; ++Func) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001544 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*Func);
1545 if (!FDecl)
1546 FDecl = cast<FunctionTemplateDecl>(*Func)->getTemplatedDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001547
1548 // Add the namespace in which this function was defined. Note
1549 // that, if this is a member function, we do *not* consider the
1550 // enclosing namespace of its class.
1551 DeclContext *Ctx = FDecl->getDeclContext();
John McCall6ff07852009-08-07 22:18:02 +00001552 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001553
1554 // Add the classes and namespaces associated with the parameter
1555 // types and return type of this function.
1556 addAssociatedClassesAndNamespaces(FDecl->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001557 AssociatedNamespaces,
1558 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001559 }
1560 }
1561}
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001562
1563/// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is
1564/// an acceptable non-member overloaded operator for a call whose
1565/// arguments have types T1 (and, if non-empty, T2). This routine
1566/// implements the check in C++ [over.match.oper]p3b2 concerning
1567/// enumeration types.
Mike Stump1eb44332009-09-09 15:08:12 +00001568static bool
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001569IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn,
1570 QualType T1, QualType T2,
1571 ASTContext &Context) {
Douglas Gregorba498172009-03-13 21:01:28 +00001572 if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType()))
1573 return true;
1574
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001575 if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType()))
1576 return true;
1577
John McCall183700f2009-09-21 23:43:11 +00001578 const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001579 if (Proto->getNumArgs() < 1)
1580 return false;
1581
1582 if (T1->isEnumeralType()) {
1583 QualType ArgType = Proto->getArgType(0).getNonReferenceType();
Douglas Gregora4923eb2009-11-16 21:35:15 +00001584 if (Context.hasSameUnqualifiedType(T1, ArgType))
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001585 return true;
1586 }
1587
1588 if (Proto->getNumArgs() < 2)
1589 return false;
1590
1591 if (!T2.isNull() && T2->isEnumeralType()) {
1592 QualType ArgType = Proto->getArgType(1).getNonReferenceType();
Douglas Gregora4923eb2009-11-16 21:35:15 +00001593 if (Context.hasSameUnqualifiedType(T2, ArgType))
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001594 return true;
1595 }
1596
1597 return false;
1598}
1599
Douglas Gregor6e378de2009-04-23 23:18:26 +00001600/// \brief Find the protocol with the given name, if any.
1601ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) {
John McCallf36e02d2009-10-09 21:13:30 +00001602 Decl *D = LookupSingleName(TUScope, II, LookupObjCProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +00001603 return cast_or_null<ObjCProtocolDecl>(D);
1604}
1605
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001606/// \brief Find the Objective-C category implementation with the given
1607/// name, if any.
1608ObjCCategoryImplDecl *Sema::LookupObjCCategoryImpl(IdentifierInfo *II) {
John McCallf36e02d2009-10-09 21:13:30 +00001609 Decl *D = LookupSingleName(TUScope, II, LookupObjCCategoryImplName);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001610 return cast_or_null<ObjCCategoryImplDecl>(D);
1611}
1612
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001613void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
Mike Stump1eb44332009-09-09 15:08:12 +00001614 QualType T1, QualType T2,
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001615 FunctionSet &Functions) {
1616 // C++ [over.match.oper]p3:
1617 // -- The set of non-member candidates is the result of the
1618 // unqualified lookup of operator@ in the context of the
1619 // expression according to the usual rules for name lookup in
1620 // unqualified function calls (3.4.2) except that all member
1621 // functions are ignored. However, if no operand has a class
1622 // type, only those non-member functions in the lookup set
Eli Friedman33a31382009-08-05 19:21:58 +00001623 // that have a first parameter of type T1 or "reference to
1624 // (possibly cv-qualified) T1", when T1 is an enumeration
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001625 // type, or (if there is a right operand) a second parameter
Eli Friedman33a31382009-08-05 19:21:58 +00001626 // of type T2 or "reference to (possibly cv-qualified) T2",
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001627 // when T2 is an enumeration type, are candidate functions.
1628 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
John McCalla24dc2e2009-11-17 02:14:36 +00001629 LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName);
1630 LookupName(Operators, S);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001632 assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
1633
John McCallf36e02d2009-10-09 21:13:30 +00001634 if (Operators.empty())
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001635 return;
1636
1637 for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end();
1638 Op != OpEnd; ++Op) {
Douglas Gregor364e0212009-06-27 21:05:07 +00001639 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001640 if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context))
1641 Functions.insert(FD); // FIXME: canonical FD
Mike Stump1eb44332009-09-09 15:08:12 +00001642 } else if (FunctionTemplateDecl *FunTmpl
Douglas Gregor364e0212009-06-27 21:05:07 +00001643 = dyn_cast<FunctionTemplateDecl>(*Op)) {
1644 // FIXME: friend operators?
Mike Stump1eb44332009-09-09 15:08:12 +00001645 // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate,
Douglas Gregor364e0212009-06-27 21:05:07 +00001646 // later?
1647 if (!FunTmpl->getDeclContext()->isRecord())
1648 Functions.insert(FunTmpl);
1649 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001650 }
1651}
1652
John McCall6ff07852009-08-07 22:18:02 +00001653static void CollectFunctionDecl(Sema::FunctionSet &Functions,
1654 Decl *D) {
1655 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1656 Functions.insert(Func);
1657 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
1658 Functions.insert(FunTmpl);
1659}
1660
Sebastian Redl644be852009-10-23 19:23:15 +00001661void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator,
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001662 Expr **Args, unsigned NumArgs,
1663 FunctionSet &Functions) {
1664 // Find all of the associated namespaces and classes based on the
1665 // arguments we have.
1666 AssociatedNamespaceSet AssociatedNamespaces;
1667 AssociatedClassSet AssociatedClasses;
Mike Stump1eb44332009-09-09 15:08:12 +00001668 FindAssociatedClassesAndNamespaces(Args, NumArgs,
John McCall6ff07852009-08-07 22:18:02 +00001669 AssociatedNamespaces,
1670 AssociatedClasses);
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001671
Sebastian Redl644be852009-10-23 19:23:15 +00001672 QualType T1, T2;
1673 if (Operator) {
1674 T1 = Args[0]->getType();
1675 if (NumArgs >= 2)
1676 T2 = Args[1]->getType();
1677 }
1678
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001679 // C++ [basic.lookup.argdep]p3:
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001680 // Let X be the lookup set produced by unqualified lookup (3.4.1)
1681 // and let Y be the lookup set produced by argument dependent
1682 // lookup (defined as follows). If X contains [...] then Y is
1683 // empty. Otherwise Y is the set of declarations found in the
1684 // namespaces associated with the argument types as described
1685 // below. The set of declarations found by the lookup of the name
1686 // is the union of X and Y.
1687 //
1688 // Here, we compute Y and add its members to the overloaded
1689 // candidate set.
1690 for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001691 NSEnd = AssociatedNamespaces.end();
1692 NS != NSEnd; ++NS) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001693 // When considering an associated namespace, the lookup is the
1694 // same as the lookup performed when the associated namespace is
1695 // used as a qualifier (3.4.3.2) except that:
1696 //
1697 // -- Any using-directives in the associated namespace are
1698 // ignored.
1699 //
John McCall6ff07852009-08-07 22:18:02 +00001700 // -- Any namespace-scope friend functions declared in
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001701 // associated classes are visible within their respective
1702 // namespaces even if they are not visible during an ordinary
1703 // lookup (11.4).
1704 DeclContext::lookup_iterator I, E;
John McCall3f9a8a62009-08-11 06:59:38 +00001705 for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) {
John McCall6ff07852009-08-07 22:18:02 +00001706 Decl *D = *I;
John McCall02cace72009-08-28 07:59:38 +00001707 // If the only declaration here is an ordinary friend, consider
1708 // it only if it was declared in an associated classes.
1709 if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) {
John McCall3f9a8a62009-08-11 06:59:38 +00001710 DeclContext *LexDC = D->getLexicalDeclContext();
1711 if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC)))
1712 continue;
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Sebastian Redl644be852009-10-23 19:23:15 +00001715 FunctionDecl *Fn;
1716 if (!Operator || !(Fn = dyn_cast<FunctionDecl>(D)) ||
1717 IsAcceptableNonMemberOperatorCandidate(Fn, T1, T2, Context))
1718 CollectFunctionDecl(Functions, D);
Douglas Gregor44bc2d52009-06-23 20:14:09 +00001719 }
1720 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001721}