blob: f37fb8be2493127b0762fe8ebb7c83cec40e12d0 [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.
John McCall7ba107a2009-11-18 02:36:19 +0000248 if (N == 0) return;
249 if (N == 1) {
250 if (isa<UnresolvedUsingValueDecl>(Decls[0]))
251 ResultKind = FoundUnresolvedValue;
252 return;
253 }
John McCallf36e02d2009-10-09 21:13:30 +0000254
John McCall6e247262009-10-10 05:48:19 +0000255 // Don't do any extra resolution if we've already resolved as ambiguous.
John McCalla24dc2e2009-11-17 02:14:36 +0000256 if (ResultKind == Ambiguous) return;
John McCall6e247262009-10-10 05:48:19 +0000257
John McCallf36e02d2009-10-09 21:13:30 +0000258 llvm::SmallPtrSet<NamedDecl*, 16> Unique;
259
260 bool Ambiguous = false;
261 bool HasTag = false, HasFunction = false, HasNonFunction = false;
John McCall7ba107a2009-11-18 02:36:19 +0000262 bool HasUnresolved = false;
John McCallf36e02d2009-10-09 21:13:30 +0000263
264 unsigned UniqueTagIndex = 0;
265
266 unsigned I = 0;
267 while (I < N) {
John McCall314be4e2009-11-17 07:50:12 +0000268 NamedDecl *D = Decls[I]->getUnderlyingDecl();
269 D = cast<NamedDecl>(D->getCanonicalDecl());
John McCallf36e02d2009-10-09 21:13:30 +0000270
John McCall314be4e2009-11-17 07:50:12 +0000271 if (!Unique.insert(D)) {
John McCallf36e02d2009-10-09 21:13:30 +0000272 // If it's not unique, pull something off the back (and
273 // continue at this index).
274 Decls[I] = Decls[--N];
John McCall7ba107a2009-11-18 02:36:19 +0000275 } else if (isa<UnresolvedUsingValueDecl>(D)) {
276 // FIXME: support unresolved using value declarations
John McCallf36e02d2009-10-09 21:13:30 +0000277 Decls[I] = Decls[--N];
278 } else {
279 // Otherwise, do some decl type analysis and then continue.
John McCall7ba107a2009-11-18 02:36:19 +0000280
281 if (isa<UnresolvedUsingValueDecl>(D)) {
282 HasUnresolved = true;
283 } else if (isa<TagDecl>(D)) {
John McCallf36e02d2009-10-09 21:13:30 +0000284 if (HasTag)
285 Ambiguous = true;
286 UniqueTagIndex = I;
287 HasTag = true;
288 } else if (D->isFunctionOrFunctionTemplate()) {
289 HasFunction = true;
290 } else {
291 if (HasNonFunction)
292 Ambiguous = true;
293 HasNonFunction = true;
294 }
295 I++;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297 }
Douglas Gregor516ff432009-04-24 02:57:34 +0000298
John McCallf36e02d2009-10-09 21:13:30 +0000299 // C++ [basic.scope.hiding]p2:
300 // A class name or enumeration name can be hidden by the name of
301 // an object, function, or enumerator declared in the same
302 // scope. If a class or enumeration name and an object, function,
303 // or enumerator are declared in the same scope (in any order)
304 // with the same name, the class or enumeration name is hidden
305 // wherever the object, function, or enumerator name is visible.
306 // But it's still an error if there are distinct tag types found,
307 // even if they're not visible. (ref?)
John McCall7ba107a2009-11-18 02:36:19 +0000308 if (HideTags && HasTag && !Ambiguous && !HasUnresolved &&
309 (HasFunction || HasNonFunction))
John McCallf36e02d2009-10-09 21:13:30 +0000310 Decls[UniqueTagIndex] = Decls[--N];
Anders Carlsson8b50d012009-06-26 03:37:05 +0000311
John McCallf36e02d2009-10-09 21:13:30 +0000312 Decls.set_size(N);
Douglas Gregor7176fff2009-01-15 00:26:24 +0000313
John McCallf36e02d2009-10-09 21:13:30 +0000314 if (HasFunction && HasNonFunction)
315 Ambiguous = true;
Douglas Gregor69d993a2009-01-17 01:13:24 +0000316
John McCallf36e02d2009-10-09 21:13:30 +0000317 if (Ambiguous)
John McCall6e247262009-10-10 05:48:19 +0000318 setAmbiguous(LookupResult::AmbiguousReference);
John McCall7ba107a2009-11-18 02:36:19 +0000319 else if (HasUnresolved)
320 ResultKind = LookupResult::FoundUnresolvedValue;
John McCallf36e02d2009-10-09 21:13:30 +0000321 else if (N > 1)
John McCalla24dc2e2009-11-17 02:14:36 +0000322 ResultKind = LookupResult::FoundOverloaded;
John McCallf36e02d2009-10-09 21:13:30 +0000323 else
John McCalla24dc2e2009-11-17 02:14:36 +0000324 ResultKind = LookupResult::Found;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000325}
326
327/// @brief Converts the result of name lookup into a single (possible
328/// NULL) pointer to a declaration.
329///
330/// The resulting declaration will either be the declaration we found
331/// (if only a single declaration was found), an
332/// OverloadedFunctionDecl (if an overloaded function was found), or
333/// NULL (if no declaration was found). This conversion must not be
Mike Stump1eb44332009-09-09 15:08:12 +0000334/// used anywhere where name lookup could result in an ambiguity.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000335///
336/// The OverloadedFunctionDecl conversion is meant as a stop-gap
337/// solution, since it causes the OverloadedFunctionDecl to be
338/// leaked. FIXME: Eventually, there will be a better way to iterate
339/// over the set of overloaded functions returned by name lookup.
John McCallf36e02d2009-10-09 21:13:30 +0000340NamedDecl *Sema::LookupResult::getAsSingleDecl(ASTContext &C) const {
341 size_t size = Decls.size();
342 if (size == 0) return 0;
John McCall314be4e2009-11-17 07:50:12 +0000343 if (size == 1) return (*begin())->getUnderlyingDecl();
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000344
John McCallf36e02d2009-10-09 21:13:30 +0000345 if (isAmbiguous()) return 0;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000346
John McCallf36e02d2009-10-09 21:13:30 +0000347 iterator I = begin(), E = end();
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000348
John McCallf36e02d2009-10-09 21:13:30 +0000349 OverloadedFunctionDecl *Ovl
350 = OverloadedFunctionDecl::Create(C, (*I)->getDeclContext(),
351 (*I)->getDeclName());
352 for (; I != E; ++I) {
John McCall314be4e2009-11-17 07:50:12 +0000353 NamedDecl *ND = (*I)->getUnderlyingDecl();
John McCallf36e02d2009-10-09 21:13:30 +0000354 assert(ND->isFunctionOrFunctionTemplate());
355 if (isa<FunctionDecl>(ND))
356 Ovl->addOverload(cast<FunctionDecl>(ND));
Douglas Gregor31a19b62009-04-01 21:51:26 +0000357 else
John McCallf36e02d2009-10-09 21:13:30 +0000358 Ovl->addOverload(cast<FunctionTemplateDecl>(ND));
359 // FIXME: UnresolvedUsingDecls.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000360 }
John McCallf36e02d2009-10-09 21:13:30 +0000361
362 return Ovl;
Douglas Gregord8635172009-02-02 21:35:47 +0000363}
364
John McCallf36e02d2009-10-09 21:13:30 +0000365void Sema::LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) {
366 CXXBasePaths::paths_iterator I, E;
367 DeclContext::lookup_iterator DI, DE;
368 for (I = P.begin(), E = P.end(); I != E; ++I)
369 for (llvm::tie(DI,DE) = I->Decls; DI != DE; ++DI)
370 addDecl(*DI);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000371}
372
John McCallf36e02d2009-10-09 21:13:30 +0000373void Sema::LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) {
374 Paths = new CXXBasePaths;
375 Paths->swap(P);
376 addDeclsFromBasePaths(*Paths);
377 resolveKind();
John McCall6e247262009-10-10 05:48:19 +0000378 setAmbiguous(AmbiguousBaseSubobjects);
Douglas Gregord8635172009-02-02 21:35:47 +0000379}
380
John McCallf36e02d2009-10-09 21:13:30 +0000381void Sema::LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) {
382 Paths = new CXXBasePaths;
383 Paths->swap(P);
384 addDeclsFromBasePaths(*Paths);
385 resolveKind();
John McCall6e247262009-10-10 05:48:19 +0000386 setAmbiguous(AmbiguousBaseSubobjectTypes);
John McCallf36e02d2009-10-09 21:13:30 +0000387}
388
389void Sema::LookupResult::print(llvm::raw_ostream &Out) {
390 Out << Decls.size() << " result(s)";
391 if (isAmbiguous()) Out << ", ambiguous";
392 if (Paths) Out << ", base paths present";
393
394 for (iterator I = begin(), E = end(); I != E; ++I) {
395 Out << "\n";
396 (*I)->print(Out, 2);
397 }
398}
399
400// Adds all qualifying matches for a name within a decl context to the
401// given lookup result. Returns true if any matches were found.
John McCalld7be78a2009-11-10 07:01:13 +0000402static bool LookupDirect(Sema::LookupResult &R,
John McCalla24dc2e2009-11-17 02:14:36 +0000403 const DeclContext *DC) {
John McCallf36e02d2009-10-09 21:13:30 +0000404 bool Found = false;
405
John McCalld7be78a2009-11-10 07:01:13 +0000406 DeclContext::lookup_const_iterator I, E;
John McCalla24dc2e2009-11-17 02:14:36 +0000407 for (llvm::tie(I, E) = DC->lookup(R.getLookupName()); I != E; ++I)
408 if (Sema::isAcceptableLookupResult(*I, R.getLookupKind(),
409 R.getIdentifierNamespace()))
John McCallf36e02d2009-10-09 21:13:30 +0000410 R.addDecl(*I), Found = true;
411
412 return Found;
413}
414
John McCalld7be78a2009-11-10 07:01:13 +0000415// Performs C++ unqualified lookup into the given file context.
John McCallf36e02d2009-10-09 21:13:30 +0000416static bool
417CppNamespaceLookup(Sema::LookupResult &R, ASTContext &Context, DeclContext *NS,
John McCalla24dc2e2009-11-17 02:14:36 +0000418 UnqualUsingDirectiveSet &UDirs) {
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000419
420 assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
421
John McCalld7be78a2009-11-10 07:01:13 +0000422 // Perform direct name lookup into the LookupCtx.
John McCalla24dc2e2009-11-17 02:14:36 +0000423 bool Found = LookupDirect(R, NS);
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000424
John McCalld7be78a2009-11-10 07:01:13 +0000425 // Perform direct name lookup into the namespaces nominated by the
426 // using directives whose common ancestor is this namespace.
427 UnqualUsingDirectiveSet::const_iterator UI, UEnd;
428 llvm::tie(UI, UEnd) = UDirs.getNamespacesFor(NS);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
John McCalld7be78a2009-11-10 07:01:13 +0000430 for (; UI != UEnd; ++UI)
John McCalla24dc2e2009-11-17 02:14:36 +0000431 if (LookupDirect(R, UI->getNominatedNamespace()))
John McCalld7be78a2009-11-10 07:01:13 +0000432 Found = true;
John McCallf36e02d2009-10-09 21:13:30 +0000433
434 R.resolveKind();
435
436 return Found;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000437}
438
439static bool isNamespaceOrTranslationUnitScope(Scope *S) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000440 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()))
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000441 return Ctx->isFileContext();
442 return false;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000443}
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000444
Douglas Gregore942bbe2009-09-10 16:57:35 +0000445// Find the next outer declaration context corresponding to this scope.
446static DeclContext *findOuterContext(Scope *S) {
447 for (S = S->getParent(); S; S = S->getParent())
448 if (S->getEntity())
449 return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext();
450
451 return 0;
452}
453
John McCalla24dc2e2009-11-17 02:14:36 +0000454bool Sema::CppLookupName(LookupResult &R, Scope *S) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000455 assert(getLangOptions().CPlusPlus &&
456 "Can perform only C++ lookup");
John McCalla24dc2e2009-11-17 02:14:36 +0000457 LookupNameKind NameKind = R.getLookupKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000458 unsigned IDNS
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000459 = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true);
John McCall02cace72009-08-28 07:59:38 +0000460
461 // If we're testing for redeclarations, also look in the friend namespaces.
John McCalla24dc2e2009-11-17 02:14:36 +0000462 if (R.isForRedeclaration()) {
John McCall02cace72009-08-28 07:59:38 +0000463 if (IDNS & Decl::IDNS_Tag) IDNS |= Decl::IDNS_TagFriend;
464 if (IDNS & Decl::IDNS_Ordinary) IDNS |= Decl::IDNS_OrdinaryFriend;
465 }
466
John McCalla24dc2e2009-11-17 02:14:36 +0000467 R.setIdentifierNamespace(IDNS);
468
469 DeclarationName Name = R.getLookupName();
470
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000471 Scope *Initial = S;
Mike Stump1eb44332009-09-09 15:08:12 +0000472 IdentifierResolver::iterator
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000473 I = IdResolver.begin(Name),
474 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000475
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000476 // First we lookup local scope.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000477 // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir]
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000478 // ...During unqualified name lookup (3.4.1), the names appear as if
479 // they were declared in the nearest enclosing namespace which contains
480 // both the using-directive and the nominated namespace.
Eli Friedman33a31382009-08-05 19:21:58 +0000481 // [Note: in this context, "contains" means "contains directly or
Mike Stump1eb44332009-09-09 15:08:12 +0000482 // indirectly".
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000483 //
484 // For example:
485 // namespace A { int i; }
486 // void foo() {
487 // int i;
488 // {
489 // using namespace A;
490 // ++i; // finds local 'i', A::i appears at global scope
491 // }
492 // }
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000493 //
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000494 for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000495 // Check whether the IdResolver has anything in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000496 bool Found = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000497 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000498 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
John McCallf36e02d2009-10-09 21:13:30 +0000499 Found = true;
500 R.addDecl(*I);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000501 }
502 }
John McCallf36e02d2009-10-09 21:13:30 +0000503 if (Found) {
504 R.resolveKind();
505 return true;
506 }
507
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000508 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
Douglas Gregore942bbe2009-09-10 16:57:35 +0000509 DeclContext *OuterCtx = findOuterContext(S);
510 for (; Ctx && Ctx->getPrimaryContext() != OuterCtx;
511 Ctx = Ctx->getLookupParent()) {
512 if (Ctx->isFunctionOrMethod())
513 continue;
514
515 // Perform qualified name lookup into this context.
516 // FIXME: In some cases, we know that every name that could be found by
517 // this qualified name lookup will also be on the identifier chain. For
518 // example, inside a class without any base classes, we never need to
519 // perform qualified lookup because all of the members are on top of the
520 // identifier chain.
John McCalla24dc2e2009-11-17 02:14:36 +0000521 if (LookupQualifiedName(R, Ctx))
John McCallf36e02d2009-10-09 21:13:30 +0000522 return true;
Douglas Gregor551f48c2009-03-27 04:21:56 +0000523 }
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000524 }
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000525 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000526
John McCalld7be78a2009-11-10 07:01:13 +0000527 // Stop if we ran out of scopes.
528 // FIXME: This really, really shouldn't be happening.
529 if (!S) return false;
530
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000531 // Collect UsingDirectiveDecls in all scopes, and recursively all
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000532 // nominated namespaces by those using-directives.
John McCalld7be78a2009-11-10 07:01:13 +0000533 //
Mike Stump390b4cc2009-05-16 07:39:55 +0000534 // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
535 // don't build it for each lookup!
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000536
John McCalld7be78a2009-11-10 07:01:13 +0000537 UnqualUsingDirectiveSet UDirs;
538 UDirs.visitScopeChain(Initial, S);
539 UDirs.done();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000540
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000541 // Lookup namespace scope, and global scope.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000542 // Unqualified name lookup in C++ requires looking into scopes
543 // that aren't strictly lexical, and therefore we walk through the
544 // context as well as walking through the scopes.
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000545
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000546 for (; S; S = S->getParent()) {
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000547 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Douglas Gregora24eb4e2009-08-24 18:55:03 +0000548 if (Ctx->isTransparentContext())
549 continue;
550
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000551 assert(Ctx && Ctx->isFileContext() &&
552 "We should have been looking only at file context here already.");
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000553
554 // Check whether the IdResolver has anything in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000555 bool Found = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000556 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000557 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
558 // We found something. Look for anything else in our scope
559 // with this same name and in an acceptable identifier
560 // namespace, so that we can construct an overload set if we
561 // need to.
John McCallf36e02d2009-10-09 21:13:30 +0000562 Found = true;
563 R.addDecl(*I);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000564 }
565 }
566
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000567 // Look into context considering using-directives.
John McCalla24dc2e2009-11-17 02:14:36 +0000568 if (CppNamespaceLookup(R, Context, Ctx, UDirs))
John McCallf36e02d2009-10-09 21:13:30 +0000569 Found = true;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000570
John McCallf36e02d2009-10-09 21:13:30 +0000571 if (Found) {
572 R.resolveKind();
573 return true;
574 }
575
John McCalla24dc2e2009-11-17 02:14:36 +0000576 if (R.isForRedeclaration() && !Ctx->isTransparentContext())
John McCallf36e02d2009-10-09 21:13:30 +0000577 return false;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000578 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000579
John McCallf36e02d2009-10-09 21:13:30 +0000580 return !R.empty();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000581}
582
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000583/// @brief Perform unqualified name lookup starting from a given
584/// scope.
585///
586/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
587/// used to find names within the current scope. For example, 'x' in
588/// @code
589/// int x;
590/// int f() {
591/// return x; // unqualified name look finds 'x' in the global scope
592/// }
593/// @endcode
594///
595/// Different lookup criteria can find different names. For example, a
596/// particular scope can have both a struct and a function of the same
597/// name, and each can be found by certain lookup criteria. For more
598/// information about lookup criteria, see the documentation for the
599/// class LookupCriteria.
600///
601/// @param S The scope from which unqualified name lookup will
602/// begin. If the lookup criteria permits, name lookup may also search
603/// in the parent scopes.
604///
605/// @param Name The name of the entity that we are searching for.
606///
Douglas Gregor3e41d602009-02-13 23:20:09 +0000607/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +0000608/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +0000609/// C library functions (like "malloc") are implicitly declared.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000610///
611/// @returns The result of name lookup, which includes zero or more
612/// declarations and possibly additional information used to diagnose
613/// ambiguities.
John McCalla24dc2e2009-11-17 02:14:36 +0000614bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) {
615 DeclarationName Name = R.getLookupName();
John McCallf36e02d2009-10-09 21:13:30 +0000616 if (!Name) return false;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000617
John McCalla24dc2e2009-11-17 02:14:36 +0000618 LookupNameKind NameKind = R.getLookupKind();
619
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000620 if (!getLangOptions().CPlusPlus) {
621 // Unqualified name lookup in C/Objective-C is purely lexical, so
622 // search in the declarations attached to the name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000623 unsigned IDNS = 0;
624 switch (NameKind) {
625 case Sema::LookupOrdinaryName:
626 IDNS = Decl::IDNS_Ordinary;
627 break;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000628
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000629 case Sema::LookupTagName:
630 IDNS = Decl::IDNS_Tag;
631 break;
632
633 case Sema::LookupMemberName:
634 IDNS = Decl::IDNS_Member;
635 break;
636
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000637 case Sema::LookupOperatorName:
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000638 case Sema::LookupNestedNameSpecifierName:
639 case Sema::LookupNamespaceName:
640 assert(false && "C does not perform these kinds of name lookup");
641 break;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000642
643 case Sema::LookupRedeclarationWithLinkage:
644 // Find the nearest non-transparent declaration scope.
645 while (!(S->getFlags() & Scope::DeclScope) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000646 (S->getEntity() &&
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000647 static_cast<DeclContext *>(S->getEntity())
648 ->isTransparentContext()))
649 S = S->getParent();
650 IDNS = Decl::IDNS_Ordinary;
651 break;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000652
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000653 case Sema::LookupObjCProtocolName:
654 IDNS = Decl::IDNS_ObjCProtocol;
655 break;
656
657 case Sema::LookupObjCImplementationName:
658 IDNS = Decl::IDNS_ObjCImplementation;
659 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000661 case Sema::LookupObjCCategoryImplName:
662 IDNS = Decl::IDNS_ObjCCategoryImpl;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000663 break;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000664 }
665
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000666 // Scan up the scope chain looking for a decl that matches this
667 // identifier that is in the appropriate namespace. This search
668 // should not take long, as shadowing of names is uncommon, and
669 // deep shadowing is extremely uncommon.
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000670 bool LeftStartingScope = false;
671
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000672 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Mike Stump1eb44332009-09-09 15:08:12 +0000673 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000674 I != IEnd; ++I)
Douglas Gregorf9201e02009-02-11 23:02:49 +0000675 if ((*I)->isInIdentifierNamespace(IDNS)) {
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000676 if (NameKind == LookupRedeclarationWithLinkage) {
677 // Determine whether this (or a previous) declaration is
678 // out-of-scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000679 if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000680 LeftStartingScope = true;
681
682 // If we found something outside of our starting scope that
683 // does not have linkage, skip it.
684 if (LeftStartingScope && !((*I)->hasLinkage()))
685 continue;
686 }
687
John McCallf36e02d2009-10-09 21:13:30 +0000688 R.addDecl(*I);
689
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000690 if ((*I)->getAttr<OverloadableAttr>()) {
Douglas Gregorf9201e02009-02-11 23:02:49 +0000691 // If this declaration has the "overloadable" attribute, we
692 // might have a set of overloaded functions.
693
694 // Figure out what scope the identifier is in.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000695 while (!(S->getFlags() & Scope::DeclScope) ||
696 !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000697 S = S->getParent();
698
699 // Find the last declaration in this scope (with the same
700 // name, naturally).
701 IdentifierResolver::iterator LastI = I;
702 for (++LastI; LastI != IEnd; ++LastI) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000703 if (!S->isDeclScope(DeclPtrTy::make(*LastI)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000704 break;
John McCallf36e02d2009-10-09 21:13:30 +0000705 R.addDecl(*LastI);
Douglas Gregorf9201e02009-02-11 23:02:49 +0000706 }
Douglas Gregorf9201e02009-02-11 23:02:49 +0000707 }
708
John McCallf36e02d2009-10-09 21:13:30 +0000709 R.resolveKind();
710
711 return true;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000712 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000713 } else {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000714 // Perform C++ unqualified name lookup.
John McCalla24dc2e2009-11-17 02:14:36 +0000715 if (CppLookupName(R, S))
John McCallf36e02d2009-10-09 21:13:30 +0000716 return true;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000717 }
718
719 // If we didn't find a use of this identifier, and if the identifier
720 // corresponds to a compiler builtin, create the decl object for the builtin
721 // now, injecting it into translation unit scope, and return it.
Mike Stump1eb44332009-09-09 15:08:12 +0000722 if (NameKind == LookupOrdinaryName ||
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000723 NameKind == LookupRedeclarationWithLinkage) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000724 IdentifierInfo *II = Name.getAsIdentifierInfo();
Douglas Gregor3e41d602009-02-13 23:20:09 +0000725 if (II && AllowBuiltinCreation) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000726 // If this is a builtin on this (or all) targets, create the decl.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000727 if (unsigned BuiltinID = II->getBuiltinID()) {
728 // In C++, we don't have any predefined library functions like
729 // 'malloc'. Instead, we'll just error.
Mike Stump1eb44332009-09-09 15:08:12 +0000730 if (getLangOptions().CPlusPlus &&
Douglas Gregor3e41d602009-02-13 23:20:09 +0000731 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
John McCallf36e02d2009-10-09 21:13:30 +0000732 return false;
Douglas Gregor3e41d602009-02-13 23:20:09 +0000733
John McCallf36e02d2009-10-09 21:13:30 +0000734 NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
John McCalla24dc2e2009-11-17 02:14:36 +0000735 S, R.isForRedeclaration(),
736 R.getNameLoc());
John McCallf36e02d2009-10-09 21:13:30 +0000737 if (D) R.addDecl(D);
738 return (D != NULL);
Douglas Gregor3e41d602009-02-13 23:20:09 +0000739 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000740 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000741 }
John McCallf36e02d2009-10-09 21:13:30 +0000742 return false;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000743}
744
John McCall6e247262009-10-10 05:48:19 +0000745/// @brief Perform qualified name lookup in the namespaces nominated by
746/// using directives by the given context.
747///
748/// C++98 [namespace.qual]p2:
749/// Given X::m (where X is a user-declared namespace), or given ::m
750/// (where X is the global namespace), let S be the set of all
751/// declarations of m in X and in the transitive closure of all
752/// namespaces nominated by using-directives in X and its used
753/// namespaces, except that using-directives are ignored in any
754/// namespace, including X, directly containing one or more
755/// declarations of m. No namespace is searched more than once in
756/// the lookup of a name. If S is the empty set, the program is
757/// ill-formed. Otherwise, if S has exactly one member, or if the
758/// context of the reference is a using-declaration
759/// (namespace.udecl), S is the required set of declarations of
760/// m. Otherwise if the use of m is not one that allows a unique
761/// declaration to be chosen from S, the program is ill-formed.
762/// C++98 [namespace.qual]p5:
763/// During the lookup of a qualified namespace member name, if the
764/// lookup finds more than one declaration of the member, and if one
765/// declaration introduces a class name or enumeration name and the
766/// other declarations either introduce the same object, the same
767/// enumerator or a set of functions, the non-type name hides the
768/// class or enumeration name if and only if the declarations are
769/// from the same namespace; otherwise (the declarations are from
770/// different namespaces), the program is ill-formed.
771static bool LookupQualifiedNameInUsingDirectives(Sema::LookupResult &R,
John McCalla24dc2e2009-11-17 02:14:36 +0000772 DeclContext *StartDC) {
John McCall6e247262009-10-10 05:48:19 +0000773 assert(StartDC->isFileContext() && "start context is not a file context");
774
775 DeclContext::udir_iterator I = StartDC->using_directives_begin();
776 DeclContext::udir_iterator E = StartDC->using_directives_end();
777
778 if (I == E) return false;
779
780 // We have at least added all these contexts to the queue.
781 llvm::DenseSet<DeclContext*> Visited;
782 Visited.insert(StartDC);
783
784 // We have not yet looked into these namespaces, much less added
785 // their "using-children" to the queue.
786 llvm::SmallVector<NamespaceDecl*, 8> Queue;
787
788 // We have already looked into the initial namespace; seed the queue
789 // with its using-children.
790 for (; I != E; ++I) {
John McCalld9f01d42009-11-10 09:25:37 +0000791 NamespaceDecl *ND = (*I)->getNominatedNamespace()->getOriginalNamespace();
John McCall6e247262009-10-10 05:48:19 +0000792 if (Visited.insert(ND).second)
793 Queue.push_back(ND);
794 }
795
796 // The easiest way to implement the restriction in [namespace.qual]p5
797 // is to check whether any of the individual results found a tag
798 // and, if so, to declare an ambiguity if the final result is not
799 // a tag.
800 bool FoundTag = false;
801 bool FoundNonTag = false;
802
John McCalla24dc2e2009-11-17 02:14:36 +0000803 Sema::LookupResult LocalR(Sema::LookupResult::Temporary, R);
John McCall6e247262009-10-10 05:48:19 +0000804
805 bool Found = false;
806 while (!Queue.empty()) {
807 NamespaceDecl *ND = Queue.back();
808 Queue.pop_back();
809
810 // We go through some convolutions here to avoid copying results
811 // between LookupResults.
812 bool UseLocal = !R.empty();
813 Sema::LookupResult &DirectR = UseLocal ? LocalR : R;
John McCalla24dc2e2009-11-17 02:14:36 +0000814 bool FoundDirect = LookupDirect(DirectR, ND);
John McCall6e247262009-10-10 05:48:19 +0000815
816 if (FoundDirect) {
817 // First do any local hiding.
818 DirectR.resolveKind();
819
820 // If the local result is a tag, remember that.
821 if (DirectR.isSingleTagDecl())
822 FoundTag = true;
823 else
824 FoundNonTag = true;
825
826 // Append the local results to the total results if necessary.
827 if (UseLocal) {
828 R.addAllDecls(LocalR);
829 LocalR.clear();
830 }
831 }
832
833 // If we find names in this namespace, ignore its using directives.
834 if (FoundDirect) {
835 Found = true;
836 continue;
837 }
838
839 for (llvm::tie(I,E) = ND->getUsingDirectives(); I != E; ++I) {
840 NamespaceDecl *Nom = (*I)->getNominatedNamespace();
841 if (Visited.insert(Nom).second)
842 Queue.push_back(Nom);
843 }
844 }
845
846 if (Found) {
847 if (FoundTag && FoundNonTag)
848 R.setAmbiguousQualifiedTagHiding();
849 else
850 R.resolveKind();
851 }
852
853 return Found;
854}
855
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000856/// @brief Perform qualified name lookup into a given context.
857///
858/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
859/// names when the context of those names is explicit specified, e.g.,
860/// "std::vector" or "x->member".
861///
862/// Different lookup criteria can find different names. For example, a
863/// particular scope can have both a struct and a function of the same
864/// name, and each can be found by certain lookup criteria. For more
865/// information about lookup criteria, see the documentation for the
866/// class LookupCriteria.
867///
868/// @param LookupCtx The context in which qualified name lookup will
869/// search. If the lookup criteria permits, name lookup may also search
870/// in the parent contexts or (for C++ classes) base classes.
871///
872/// @param Name The name of the entity that we are searching for.
873///
874/// @param Criteria The criteria that this routine will use to
875/// determine which names are visible and which names will be
876/// found. Note that name lookup will find a name that is visible by
877/// the given criteria, but the entity itself may not be semantically
878/// correct or even the kind of entity expected based on the
879/// lookup. For example, searching for a nested-name-specifier name
880/// might result in an EnumDecl, which is visible but is not permitted
881/// as a nested-name-specifier in C++03.
882///
883/// @returns The result of name lookup, which includes zero or more
884/// declarations and possibly additional information used to diagnose
885/// ambiguities.
John McCalla24dc2e2009-11-17 02:14:36 +0000886bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000887 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
Mike Stump1eb44332009-09-09 15:08:12 +0000888
John McCalla24dc2e2009-11-17 02:14:36 +0000889 if (!R.getLookupName())
John McCallf36e02d2009-10-09 21:13:30 +0000890 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000892 // If we're performing qualified name lookup (e.g., lookup into a
893 // struct), find fields as part of ordinary name lookup.
John McCalla24dc2e2009-11-17 02:14:36 +0000894 LookupNameKind NameKind = R.getLookupKind();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000895 unsigned IDNS
Mike Stump1eb44332009-09-09 15:08:12 +0000896 = getIdentifierNamespacesFromLookupNameKind(NameKind,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000897 getLangOptions().CPlusPlus);
898 if (NameKind == LookupOrdinaryName)
899 IDNS |= Decl::IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
John McCalla24dc2e2009-11-17 02:14:36 +0000901 R.setIdentifierNamespace(IDNS);
902
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000903 // Make sure that the declaration context is complete.
904 assert((!isa<TagDecl>(LookupCtx) ||
905 LookupCtx->isDependentContext() ||
906 cast<TagDecl>(LookupCtx)->isDefinition() ||
907 Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>()
908 ->isBeingDefined()) &&
909 "Declaration context must already be complete!");
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000911 // Perform qualified name lookup into the LookupCtx.
John McCalla24dc2e2009-11-17 02:14:36 +0000912 if (LookupDirect(R, LookupCtx)) {
John McCallf36e02d2009-10-09 21:13:30 +0000913 R.resolveKind();
914 return true;
915 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000916
John McCall6e247262009-10-10 05:48:19 +0000917 // Don't descend into implied contexts for redeclarations.
918 // C++98 [namespace.qual]p6:
919 // In a declaration for a namespace member in which the
920 // declarator-id is a qualified-id, given that the qualified-id
921 // for the namespace member has the form
922 // nested-name-specifier unqualified-id
923 // the unqualified-id shall name a member of the namespace
924 // designated by the nested-name-specifier.
925 // See also [class.mfct]p5 and [class.static.data]p2.
John McCalla24dc2e2009-11-17 02:14:36 +0000926 if (R.isForRedeclaration())
John McCall6e247262009-10-10 05:48:19 +0000927 return false;
928
John McCalla24dc2e2009-11-17 02:14:36 +0000929 // If this is a namespace, look it up in the implied namespaces.
John McCall6e247262009-10-10 05:48:19 +0000930 if (LookupCtx->isFileContext())
John McCalla24dc2e2009-11-17 02:14:36 +0000931 return LookupQualifiedNameInUsingDirectives(R, LookupCtx);
John McCall6e247262009-10-10 05:48:19 +0000932
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000933 // If this isn't a C++ class, we aren't allowed to look into base
Douglas Gregor4719f4e2009-09-11 22:57:37 +0000934 // classes, we're done.
John McCall6e247262009-10-10 05:48:19 +0000935 if (!isa<CXXRecordDecl>(LookupCtx))
John McCallf36e02d2009-10-09 21:13:30 +0000936 return false;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000937
938 // Perform lookup into our base classes.
Douglas Gregora8f32e02009-10-06 17:59:45 +0000939 CXXRecordDecl *LookupRec = cast<CXXRecordDecl>(LookupCtx);
940 CXXBasePaths Paths;
941 Paths.setOrigin(LookupRec);
Douglas Gregor7176fff2009-01-15 00:26:24 +0000942
943 // Look for this member in our base classes
Douglas Gregora8f32e02009-10-06 17:59:45 +0000944 CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0;
John McCalla24dc2e2009-11-17 02:14:36 +0000945 switch (R.getLookupKind()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000946 case LookupOrdinaryName:
947 case LookupMemberName:
948 case LookupRedeclarationWithLinkage:
949 BaseCallback = &CXXRecordDecl::FindOrdinaryMember;
950 break;
951
952 case LookupTagName:
953 BaseCallback = &CXXRecordDecl::FindTagMember;
954 break;
955
956 case LookupOperatorName:
957 case LookupNamespaceName:
958 case LookupObjCProtocolName:
959 case LookupObjCImplementationName:
960 case LookupObjCCategoryImplName:
961 // These lookups will never find a member in a C++ class (or base class).
John McCallf36e02d2009-10-09 21:13:30 +0000962 return false;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000963
964 case LookupNestedNameSpecifierName:
965 BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember;
966 break;
967 }
968
John McCalla24dc2e2009-11-17 02:14:36 +0000969 if (!LookupRec->lookupInBases(BaseCallback,
970 R.getLookupName().getAsOpaquePtr(), Paths))
John McCallf36e02d2009-10-09 21:13:30 +0000971 return false;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000972
973 // C++ [class.member.lookup]p2:
974 // [...] If the resulting set of declarations are not all from
975 // sub-objects of the same type, or the set has a nonstatic member
976 // and includes members from distinct sub-objects, there is an
977 // ambiguity and the program is ill-formed. Otherwise that set is
978 // the result of the lookup.
979 // FIXME: support using declarations!
980 QualType SubobjectType;
Daniel Dunbarf1853192009-01-15 18:32:35 +0000981 int SubobjectNumber = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000982 for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000983 Path != PathEnd; ++Path) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000984 const CXXBasePathElement &PathElement = Path->back();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000985
986 // Determine whether we're looking at a distinct sub-object or not.
987 if (SubobjectType.isNull()) {
John McCallf36e02d2009-10-09 21:13:30 +0000988 // This is the first subobject we've looked at. Record its type.
Douglas Gregor7176fff2009-01-15 00:26:24 +0000989 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
990 SubobjectNumber = PathElement.SubobjectNumber;
Mike Stump1eb44332009-09-09 15:08:12 +0000991 } else if (SubobjectType
Douglas Gregor7176fff2009-01-15 00:26:24 +0000992 != Context.getCanonicalType(PathElement.Base->getType())) {
993 // We found members of the given name in two subobjects of
994 // different types. This lookup is ambiguous.
John McCallf36e02d2009-10-09 21:13:30 +0000995 R.setAmbiguousBaseSubobjectTypes(Paths);
996 return true;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000997 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
998 // We have a different subobject of the same type.
999
1000 // C++ [class.member.lookup]p5:
1001 // A static member, a nested type or an enumerator defined in
1002 // a base class T can unambiguously be found even if an object
Mike Stump1eb44332009-09-09 15:08:12 +00001003 // has more than one base class subobject of type T.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001004 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001005 if (isa<VarDecl>(FirstDecl) ||
1006 isa<TypeDecl>(FirstDecl) ||
1007 isa<EnumConstantDecl>(FirstDecl))
1008 continue;
1009
1010 if (isa<CXXMethodDecl>(FirstDecl)) {
1011 // Determine whether all of the methods are static.
1012 bool AllMethodsAreStatic = true;
1013 for (DeclContext::lookup_iterator Func = Path->Decls.first;
1014 Func != Path->Decls.second; ++Func) {
1015 if (!isa<CXXMethodDecl>(*Func)) {
1016 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
1017 break;
1018 }
1019
1020 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
1021 AllMethodsAreStatic = false;
1022 break;
1023 }
1024 }
1025
1026 if (AllMethodsAreStatic)
1027 continue;
1028 }
1029
1030 // We have found a nonstatic member name in multiple, distinct
1031 // subobjects. Name lookup is ambiguous.
John McCallf36e02d2009-10-09 21:13:30 +00001032 R.setAmbiguousBaseSubobjects(Paths);
1033 return true;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001034 }
1035 }
1036
1037 // Lookup in a base class succeeded; return these results.
1038
John McCallf36e02d2009-10-09 21:13:30 +00001039 DeclContext::lookup_iterator I, E;
1040 for (llvm::tie(I,E) = Paths.front().Decls; I != E; ++I)
1041 R.addDecl(*I);
1042 R.resolveKind();
1043 return true;
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001044}
1045
1046/// @brief Performs name lookup for a name that was parsed in the
1047/// source code, and may contain a C++ scope specifier.
1048///
1049/// This routine is a convenience routine meant to be called from
1050/// contexts that receive a name and an optional C++ scope specifier
1051/// (e.g., "N::M::x"). It will then perform either qualified or
1052/// unqualified name lookup (with LookupQualifiedName or LookupName,
1053/// respectively) on the given name and return those results.
1054///
1055/// @param S The scope from which unqualified name lookup will
1056/// begin.
Mike Stump1eb44332009-09-09 15:08:12 +00001057///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001058/// @param SS An optional C++ scope-specifier, e.g., "::N::M".
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001059///
1060/// @param Name The name of the entity that name lookup will
1061/// search for.
1062///
Douglas Gregor3e41d602009-02-13 23:20:09 +00001063/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +00001064/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +00001065/// C library functions (like "malloc") are implicitly declared.
1066///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001067/// @param EnteringContext Indicates whether we are going to enter the
1068/// context of the scope-specifier SS (if present).
1069///
John McCallf36e02d2009-10-09 21:13:30 +00001070/// @returns True if any decls were found (but possibly ambiguous)
1071bool Sema::LookupParsedName(LookupResult &R, Scope *S, const CXXScopeSpec *SS,
John McCalla24dc2e2009-11-17 02:14:36 +00001072 bool AllowBuiltinCreation, bool EnteringContext) {
Douglas Gregor495c35d2009-08-25 22:51:20 +00001073 if (SS && SS->isInvalid()) {
1074 // When the scope specifier is invalid, don't even look for
Douglas Gregor42af25f2009-05-11 19:58:34 +00001075 // anything.
John McCallf36e02d2009-10-09 21:13:30 +00001076 return false;
Douglas Gregor495c35d2009-08-25 22:51:20 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregor495c35d2009-08-25 22:51:20 +00001079 if (SS && SS->isSet()) {
1080 if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001081 // We have resolved the scope specifier to a particular declaration
Douglas Gregor495c35d2009-08-25 22:51:20 +00001082 // contex, and will perform name lookup in that context.
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001083 if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS))
John McCallf36e02d2009-10-09 21:13:30 +00001084 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
John McCalla24dc2e2009-11-17 02:14:36 +00001086 R.setContextRange(SS->getRange());
1087
1088 return LookupQualifiedName(R, DC);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001089 }
Douglas Gregor42af25f2009-05-11 19:58:34 +00001090
Douglas Gregor495c35d2009-08-25 22:51:20 +00001091 // We could not resolve the scope specified to a specific declaration
Mike Stump1eb44332009-09-09 15:08:12 +00001092 // context, which means that SS refers to an unknown specialization.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001093 // Name lookup can't find anything in this case.
John McCallf36e02d2009-10-09 21:13:30 +00001094 return false;
Douglas Gregor4c921ae2009-01-30 01:04:22 +00001095 }
1096
Mike Stump1eb44332009-09-09 15:08:12 +00001097 // Perform unqualified name lookup starting in the given scope.
John McCalla24dc2e2009-11-17 02:14:36 +00001098 return LookupName(R, S, AllowBuiltinCreation);
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001099}
1100
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001101
Douglas Gregor7176fff2009-01-15 00:26:24 +00001102/// @brief Produce a diagnostic describing the ambiguity that resulted
1103/// from name lookup.
1104///
1105/// @param Result The ambiguous name lookup result.
Mike Stump1eb44332009-09-09 15:08:12 +00001106///
Douglas Gregor7176fff2009-01-15 00:26:24 +00001107/// @param Name The name of the entity that name lookup was
1108/// searching for.
1109///
1110/// @param NameLoc The location of the name within the source code.
1111///
1112/// @param LookupRange A source range that provides more
1113/// source-location information concerning the lookup itself. For
1114/// example, this range might highlight a nested-name-specifier that
1115/// precedes the name.
1116///
1117/// @returns true
John McCalla24dc2e2009-11-17 02:14:36 +00001118bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result) {
Douglas Gregor7176fff2009-01-15 00:26:24 +00001119 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
1120
John McCalla24dc2e2009-11-17 02:14:36 +00001121 DeclarationName Name = Result.getLookupName();
1122 SourceLocation NameLoc = Result.getNameLoc();
1123 SourceRange LookupRange = Result.getContextRange();
1124
John McCall6e247262009-10-10 05:48:19 +00001125 switch (Result.getAmbiguityKind()) {
1126 case LookupResult::AmbiguousBaseSubobjects: {
1127 CXXBasePaths *Paths = Result.getBasePaths();
1128 QualType SubobjectType = Paths->front().back().Base->getType();
1129 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
1130 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
1131 << LookupRange;
1132
1133 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
1134 while (isa<CXXMethodDecl>(*Found) &&
1135 cast<CXXMethodDecl>(*Found)->isStatic())
1136 ++Found;
1137
1138 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
1139
1140 return true;
1141 }
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001142
John McCall6e247262009-10-10 05:48:19 +00001143 case LookupResult::AmbiguousBaseSubobjectTypes: {
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001144 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
1145 << Name << LookupRange;
John McCall6e247262009-10-10 05:48:19 +00001146
1147 CXXBasePaths *Paths = Result.getBasePaths();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001148 std::set<Decl *> DeclsPrinted;
John McCall6e247262009-10-10 05:48:19 +00001149 for (CXXBasePaths::paths_iterator Path = Paths->begin(),
1150 PathEnd = Paths->end();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001151 Path != PathEnd; ++Path) {
1152 Decl *D = *Path->Decls.first;
1153 if (DeclsPrinted.insert(D).second)
1154 Diag(D->getLocation(), diag::note_ambiguous_member_found);
1155 }
1156
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001157 return true;
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001158 }
1159
John McCall6e247262009-10-10 05:48:19 +00001160 case LookupResult::AmbiguousTagHiding: {
1161 Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange;
Douglas Gregor69d993a2009-01-17 01:13:24 +00001162
John McCall6e247262009-10-10 05:48:19 +00001163 llvm::SmallPtrSet<NamedDecl*,8> TagDecls;
1164
1165 LookupResult::iterator DI, DE = Result.end();
1166 for (DI = Result.begin(); DI != DE; ++DI)
1167 if (TagDecl *TD = dyn_cast<TagDecl>(*DI)) {
1168 TagDecls.insert(TD);
1169 Diag(TD->getLocation(), diag::note_hidden_tag);
1170 }
1171
1172 for (DI = Result.begin(); DI != DE; ++DI)
1173 if (!isa<TagDecl>(*DI))
1174 Diag((*DI)->getLocation(), diag::note_hiding_object);
1175
1176 // For recovery purposes, go ahead and implement the hiding.
1177 Result.hideDecls(TagDecls);
1178
1179 return true;
1180 }
1181
1182 case LookupResult::AmbiguousReference: {
1183 Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange;
John McCallf36e02d2009-10-09 21:13:30 +00001184
John McCall6e247262009-10-10 05:48:19 +00001185 LookupResult::iterator DI = Result.begin(), DE = Result.end();
1186 for (; DI != DE; ++DI)
1187 Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI;
John McCallf36e02d2009-10-09 21:13:30 +00001188
John McCall6e247262009-10-10 05:48:19 +00001189 return true;
1190 }
1191 }
1192
1193 llvm::llvm_unreachable("unknown ambiguity kind");
Douglas Gregor7176fff2009-01-15 00:26:24 +00001194 return true;
1195}
Douglas Gregorfa047642009-02-04 00:32:51 +00001196
Mike Stump1eb44332009-09-09 15:08:12 +00001197static void
1198addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001199 ASTContext &Context,
1200 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001201 Sema::AssociatedClassSet &AssociatedClasses);
1202
1203static void CollectNamespace(Sema::AssociatedNamespaceSet &Namespaces,
1204 DeclContext *Ctx) {
1205 if (Ctx->isFileContext())
1206 Namespaces.insert(Ctx);
1207}
Douglas Gregor69be8d62009-07-08 07:51:57 +00001208
Mike Stump1eb44332009-09-09 15:08:12 +00001209// \brief Add the associated classes and namespaces for argument-dependent
Douglas Gregor69be8d62009-07-08 07:51:57 +00001210// lookup that involves a template argument (C++ [basic.lookup.koenig]p2).
Mike Stump1eb44332009-09-09 15:08:12 +00001211static void
1212addAssociatedClassesAndNamespaces(const TemplateArgument &Arg,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001213 ASTContext &Context,
1214 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001215 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregor69be8d62009-07-08 07:51:57 +00001216 // C++ [basic.lookup.koenig]p2, last bullet:
Mike Stump1eb44332009-09-09 15:08:12 +00001217 // -- [...] ;
Douglas Gregor69be8d62009-07-08 07:51:57 +00001218 switch (Arg.getKind()) {
1219 case TemplateArgument::Null:
1220 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor69be8d62009-07-08 07:51:57 +00001222 case TemplateArgument::Type:
1223 // [...] the namespaces and classes associated with the types of the
1224 // template arguments provided for template type parameters (excluding
1225 // template template parameters)
1226 addAssociatedClassesAndNamespaces(Arg.getAsType(), Context,
1227 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001228 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001229 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Douglas Gregor788cd062009-11-11 01:00:40 +00001231 case TemplateArgument::Template: {
Mike Stump1eb44332009-09-09 15:08:12 +00001232 // [...] the namespaces in which any template template arguments are
1233 // defined; and the classes in which any member templates used as
Douglas Gregor69be8d62009-07-08 07:51:57 +00001234 // template template arguments are defined.
Douglas Gregor788cd062009-11-11 01:00:40 +00001235 TemplateName Template = Arg.getAsTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +00001236 if (ClassTemplateDecl *ClassTemplate
Douglas Gregor788cd062009-11-11 01:00:40 +00001237 = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) {
Douglas Gregor69be8d62009-07-08 07:51:57 +00001238 DeclContext *Ctx = ClassTemplate->getDeclContext();
1239 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1240 AssociatedClasses.insert(EnclosingClass);
1241 // Add the associated namespace for this class.
1242 while (Ctx->isRecord())
1243 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001244 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001245 }
1246 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00001247 }
1248
1249 case TemplateArgument::Declaration:
Douglas Gregor69be8d62009-07-08 07:51:57 +00001250 case TemplateArgument::Integral:
1251 case TemplateArgument::Expression:
Mike Stump1eb44332009-09-09 15:08:12 +00001252 // [Note: non-type template arguments do not contribute to the set of
Douglas Gregor69be8d62009-07-08 07:51:57 +00001253 // associated namespaces. ]
1254 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Douglas Gregor69be8d62009-07-08 07:51:57 +00001256 case TemplateArgument::Pack:
1257 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1258 PEnd = Arg.pack_end();
1259 P != PEnd; ++P)
1260 addAssociatedClassesAndNamespaces(*P, Context,
1261 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001262 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001263 break;
1264 }
1265}
1266
Douglas Gregorfa047642009-02-04 00:32:51 +00001267// \brief Add the associated classes and namespaces for
Mike Stump1eb44332009-09-09 15:08:12 +00001268// argument-dependent lookup with an argument of class type
1269// (C++ [basic.lookup.koenig]p2).
1270static void
1271addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
Douglas Gregorfa047642009-02-04 00:32:51 +00001272 ASTContext &Context,
1273 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001274 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001275 // C++ [basic.lookup.koenig]p2:
1276 // [...]
1277 // -- If T is a class type (including unions), its associated
1278 // classes are: the class itself; the class of which it is a
1279 // member, if any; and its direct and indirect base
1280 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001281 // which its associated classes are defined.
Douglas Gregorfa047642009-02-04 00:32:51 +00001282
1283 // Add the class of which it is a member, if any.
1284 DeclContext *Ctx = Class->getDeclContext();
1285 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1286 AssociatedClasses.insert(EnclosingClass);
Douglas Gregorfa047642009-02-04 00:32:51 +00001287 // Add the associated namespace for this class.
1288 while (Ctx->isRecord())
1289 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001290 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregorfa047642009-02-04 00:32:51 +00001292 // Add the class itself. If we've already seen this class, we don't
1293 // need to visit base classes.
1294 if (!AssociatedClasses.insert(Class))
1295 return;
1296
Mike Stump1eb44332009-09-09 15:08:12 +00001297 // -- If T is a template-id, its associated namespaces and classes are
1298 // the namespace in which the template is defined; for member
Douglas Gregor69be8d62009-07-08 07:51:57 +00001299 // templates, the member template’s class; the namespaces and classes
Mike Stump1eb44332009-09-09 15:08:12 +00001300 // associated with the types of the template arguments provided for
Douglas Gregor69be8d62009-07-08 07:51:57 +00001301 // template type parameters (excluding template template parameters); the
Mike Stump1eb44332009-09-09 15:08:12 +00001302 // namespaces in which any template template arguments are defined; and
1303 // the classes in which any member templates used as template template
1304 // arguments are defined. [Note: non-type template arguments do not
Douglas Gregor69be8d62009-07-08 07:51:57 +00001305 // contribute to the set of associated namespaces. ]
Mike Stump1eb44332009-09-09 15:08:12 +00001306 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor69be8d62009-07-08 07:51:57 +00001307 = dyn_cast<ClassTemplateSpecializationDecl>(Class)) {
1308 DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext();
1309 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1310 AssociatedClasses.insert(EnclosingClass);
1311 // Add the associated namespace for this class.
1312 while (Ctx->isRecord())
1313 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001314 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Douglas Gregor69be8d62009-07-08 07:51:57 +00001316 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1317 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1318 addAssociatedClassesAndNamespaces(TemplateArgs[I], Context,
1319 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001320 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregorfa047642009-02-04 00:32:51 +00001323 // Add direct and indirect base classes along with their associated
1324 // namespaces.
1325 llvm::SmallVector<CXXRecordDecl *, 32> Bases;
1326 Bases.push_back(Class);
1327 while (!Bases.empty()) {
1328 // Pop this class off the stack.
1329 Class = Bases.back();
1330 Bases.pop_back();
1331
1332 // Visit the base classes.
1333 for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(),
1334 BaseEnd = Class->bases_end();
1335 Base != BaseEnd; ++Base) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001336 const RecordType *BaseType = Base->getType()->getAs<RecordType>();
Sebastian Redlbbc1cc52009-10-25 09:35:33 +00001337 // In dependent contexts, we do ADL twice, and the first time around,
1338 // the base type might be a dependent TemplateSpecializationType, or a
1339 // TemplateTypeParmType. If that happens, simply ignore it.
1340 // FIXME: If we want to support export, we probably need to add the
1341 // namespace of the template in a TemplateSpecializationType, or even
1342 // the classes and namespaces of known non-dependent arguments.
1343 if (!BaseType)
1344 continue;
Douglas Gregorfa047642009-02-04 00:32:51 +00001345 CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl());
1346 if (AssociatedClasses.insert(BaseDecl)) {
1347 // Find the associated namespace for this base class.
1348 DeclContext *BaseCtx = BaseDecl->getDeclContext();
1349 while (BaseCtx->isRecord())
1350 BaseCtx = BaseCtx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001351 CollectNamespace(AssociatedNamespaces, BaseCtx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001352
1353 // Make sure we visit the bases of this base class.
1354 if (BaseDecl->bases_begin() != BaseDecl->bases_end())
1355 Bases.push_back(BaseDecl);
1356 }
1357 }
1358 }
1359}
1360
1361// \brief Add the associated classes and namespaces for
1362// argument-dependent lookup with an argument of type T
Mike Stump1eb44332009-09-09 15:08:12 +00001363// (C++ [basic.lookup.koenig]p2).
1364static void
1365addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregorfa047642009-02-04 00:32:51 +00001366 ASTContext &Context,
1367 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001368 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001369 // C++ [basic.lookup.koenig]p2:
1370 //
1371 // For each argument type T in the function call, there is a set
1372 // of zero or more associated namespaces and a set of zero or more
1373 // associated classes to be considered. The sets of namespaces and
1374 // classes is determined entirely by the types of the function
1375 // arguments (and the namespace of any template template
1376 // argument). Typedef names and using-declarations used to specify
1377 // the types do not contribute to this set. The sets of namespaces
1378 // and classes are determined in the following way:
1379 T = Context.getCanonicalType(T).getUnqualifiedType();
1380
1381 // -- If T is a pointer to U or an array of U, its associated
Mike Stump1eb44332009-09-09 15:08:12 +00001382 // namespaces and classes are those associated with U.
Douglas Gregorfa047642009-02-04 00:32:51 +00001383 //
1384 // We handle this by unwrapping pointer and array types immediately,
1385 // to avoid unnecessary recursion.
1386 while (true) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001387 if (const PointerType *Ptr = T->getAs<PointerType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001388 T = Ptr->getPointeeType();
1389 else if (const ArrayType *Ptr = Context.getAsArrayType(T))
1390 T = Ptr->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001391 else
Douglas Gregorfa047642009-02-04 00:32:51 +00001392 break;
1393 }
1394
1395 // -- If T is a fundamental type, its associated sets of
1396 // namespaces and classes are both empty.
John McCall183700f2009-09-21 23:43:11 +00001397 if (T->getAs<BuiltinType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001398 return;
1399
1400 // -- If T is a class type (including unions), its associated
1401 // classes are: the class itself; the class of which it is a
1402 // member, if any; and its direct and indirect base
1403 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001404 // which its associated classes are defined.
Ted Kremenek6217b802009-07-29 21:53:49 +00001405 if (const RecordType *ClassType = T->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +00001406 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001407 = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001408 addAssociatedClassesAndNamespaces(ClassDecl, Context,
1409 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001410 AssociatedClasses);
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001411 return;
1412 }
Douglas Gregorfa047642009-02-04 00:32:51 +00001413
1414 // -- If T is an enumeration type, its associated namespace is
1415 // the namespace in which it is defined. If it is class
1416 // member, its associated class is the member’s class; else
Mike Stump1eb44332009-09-09 15:08:12 +00001417 // it has no associated class.
John McCall183700f2009-09-21 23:43:11 +00001418 if (const EnumType *EnumT = T->getAs<EnumType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001419 EnumDecl *Enum = EnumT->getDecl();
1420
1421 DeclContext *Ctx = Enum->getDeclContext();
1422 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1423 AssociatedClasses.insert(EnclosingClass);
1424
1425 // Add the associated namespace for this class.
1426 while (Ctx->isRecord())
1427 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001428 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001429
1430 return;
1431 }
1432
1433 // -- If T is a function type, its associated namespaces and
1434 // classes are those associated with the function parameter
1435 // types and those associated with the return type.
John McCall183700f2009-09-21 23:43:11 +00001436 if (const FunctionType *FnType = T->getAs<FunctionType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001437 // Return type
John McCall183700f2009-09-21 23:43:11 +00001438 addAssociatedClassesAndNamespaces(FnType->getResultType(),
Douglas Gregorfa047642009-02-04 00:32:51 +00001439 Context,
John McCall6ff07852009-08-07 22:18:02 +00001440 AssociatedNamespaces, AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001441
John McCall183700f2009-09-21 23:43:11 +00001442 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType);
Douglas Gregorfa047642009-02-04 00:32:51 +00001443 if (!Proto)
1444 return;
1445
1446 // Argument types
Douglas Gregor72564e72009-02-26 23:50:07 +00001447 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001448 ArgEnd = Proto->arg_type_end();
Douglas Gregorfa047642009-02-04 00:32:51 +00001449 Arg != ArgEnd; ++Arg)
1450 addAssociatedClassesAndNamespaces(*Arg, Context,
John McCall6ff07852009-08-07 22:18:02 +00001451 AssociatedNamespaces, AssociatedClasses);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregorfa047642009-02-04 00:32:51 +00001453 return;
1454 }
1455
1456 // -- If T is a pointer to a member function of a class X, its
1457 // associated namespaces and classes are those associated
1458 // with the function parameter types and return type,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 // together with those associated with X.
Douglas Gregorfa047642009-02-04 00:32:51 +00001460 //
1461 // -- If T is a pointer to a data member of class X, its
1462 // associated namespaces and classes are those associated
1463 // with the member type together with those associated with
Mike Stump1eb44332009-09-09 15:08:12 +00001464 // X.
Ted Kremenek6217b802009-07-29 21:53:49 +00001465 if (const MemberPointerType *MemberPtr = T->getAs<MemberPointerType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001466 // Handle the type that the pointer to member points to.
1467 addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(),
1468 Context,
John McCall6ff07852009-08-07 22:18:02 +00001469 AssociatedNamespaces,
1470 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001471
1472 // Handle the class type into which this points.
Ted Kremenek6217b802009-07-29 21:53:49 +00001473 if (const RecordType *Class = MemberPtr->getClass()->getAs<RecordType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001474 addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()),
1475 Context,
John McCall6ff07852009-08-07 22:18:02 +00001476 AssociatedNamespaces,
1477 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001478
1479 return;
1480 }
1481
1482 // FIXME: What about block pointers?
1483 // FIXME: What about Objective-C message sends?
1484}
1485
1486/// \brief Find the associated classes and namespaces for
1487/// argument-dependent lookup for a call with the given set of
1488/// arguments.
1489///
1490/// This routine computes the sets of associated classes and associated
Mike Stump1eb44332009-09-09 15:08:12 +00001491/// namespaces searched by argument-dependent lookup
Douglas Gregorfa047642009-02-04 00:32:51 +00001492/// (C++ [basic.lookup.argdep]) for a given set of arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001493void
Douglas Gregorfa047642009-02-04 00:32:51 +00001494Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs,
1495 AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001496 AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001497 AssociatedNamespaces.clear();
1498 AssociatedClasses.clear();
1499
1500 // C++ [basic.lookup.koenig]p2:
1501 // For each argument type T in the function call, there is a set
1502 // of zero or more associated namespaces and a set of zero or more
1503 // associated classes to be considered. The sets of namespaces and
1504 // classes is determined entirely by the types of the function
1505 // arguments (and the namespace of any template template
Mike Stump1eb44332009-09-09 15:08:12 +00001506 // argument).
Douglas Gregorfa047642009-02-04 00:32:51 +00001507 for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) {
1508 Expr *Arg = Args[ArgIdx];
1509
1510 if (Arg->getType() != Context.OverloadTy) {
1511 addAssociatedClassesAndNamespaces(Arg->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001512 AssociatedNamespaces,
1513 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001514 continue;
1515 }
1516
1517 // [...] In addition, if the argument is the name or address of a
1518 // set of overloaded functions and/or function templates, its
1519 // associated classes and namespaces are the union of those
1520 // associated with each of the members of the set: the namespace
1521 // in which the function or function template is defined and the
1522 // classes and namespaces associated with its (non-dependent)
1523 // parameter types and return type.
1524 DeclRefExpr *DRE = 0;
Douglas Gregordaa439a2009-07-08 10:57:20 +00001525 TemplateIdRefExpr *TIRE = 0;
1526 Arg = Arg->IgnoreParens();
Douglas Gregorfa047642009-02-04 00:32:51 +00001527 if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) {
Douglas Gregordaa439a2009-07-08 10:57:20 +00001528 if (unaryOp->getOpcode() == UnaryOperator::AddrOf) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001529 DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr());
Douglas Gregordaa439a2009-07-08 10:57:20 +00001530 TIRE = dyn_cast<TemplateIdRefExpr>(unaryOp->getSubExpr());
1531 }
1532 } else {
Douglas Gregorfa047642009-02-04 00:32:51 +00001533 DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregordaa439a2009-07-08 10:57:20 +00001534 TIRE = dyn_cast<TemplateIdRefExpr>(Arg);
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregordaa439a2009-07-08 10:57:20 +00001537 OverloadedFunctionDecl *Ovl = 0;
1538 if (DRE)
1539 Ovl = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl());
1540 else if (TIRE)
Douglas Gregord99cbe62009-07-29 18:26:50 +00001541 Ovl = TIRE->getTemplateName().getAsOverloadedFunctionDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001542 if (!Ovl)
1543 continue;
1544
1545 for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
1546 FuncEnd = Ovl->function_end();
1547 Func != FuncEnd; ++Func) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001548 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*Func);
1549 if (!FDecl)
1550 FDecl = cast<FunctionTemplateDecl>(*Func)->getTemplatedDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001551
1552 // Add the namespace in which this function was defined. Note
1553 // that, if this is a member function, we do *not* consider the
1554 // enclosing namespace of its class.
1555 DeclContext *Ctx = FDecl->getDeclContext();
John McCall6ff07852009-08-07 22:18:02 +00001556 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001557
1558 // Add the classes and namespaces associated with the parameter
1559 // types and return type of this function.
1560 addAssociatedClassesAndNamespaces(FDecl->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001561 AssociatedNamespaces,
1562 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001563 }
1564 }
1565}
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001566
1567/// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is
1568/// an acceptable non-member overloaded operator for a call whose
1569/// arguments have types T1 (and, if non-empty, T2). This routine
1570/// implements the check in C++ [over.match.oper]p3b2 concerning
1571/// enumeration types.
Mike Stump1eb44332009-09-09 15:08:12 +00001572static bool
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001573IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn,
1574 QualType T1, QualType T2,
1575 ASTContext &Context) {
Douglas Gregorba498172009-03-13 21:01:28 +00001576 if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType()))
1577 return true;
1578
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001579 if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType()))
1580 return true;
1581
John McCall183700f2009-09-21 23:43:11 +00001582 const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001583 if (Proto->getNumArgs() < 1)
1584 return false;
1585
1586 if (T1->isEnumeralType()) {
1587 QualType ArgType = Proto->getArgType(0).getNonReferenceType();
Douglas Gregora4923eb2009-11-16 21:35:15 +00001588 if (Context.hasSameUnqualifiedType(T1, ArgType))
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001589 return true;
1590 }
1591
1592 if (Proto->getNumArgs() < 2)
1593 return false;
1594
1595 if (!T2.isNull() && T2->isEnumeralType()) {
1596 QualType ArgType = Proto->getArgType(1).getNonReferenceType();
Douglas Gregora4923eb2009-11-16 21:35:15 +00001597 if (Context.hasSameUnqualifiedType(T2, ArgType))
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001598 return true;
1599 }
1600
1601 return false;
1602}
1603
Douglas Gregor6e378de2009-04-23 23:18:26 +00001604/// \brief Find the protocol with the given name, if any.
1605ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) {
John McCallf36e02d2009-10-09 21:13:30 +00001606 Decl *D = LookupSingleName(TUScope, II, LookupObjCProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +00001607 return cast_or_null<ObjCProtocolDecl>(D);
1608}
1609
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001610/// \brief Find the Objective-C category implementation with the given
1611/// name, if any.
1612ObjCCategoryImplDecl *Sema::LookupObjCCategoryImpl(IdentifierInfo *II) {
John McCallf36e02d2009-10-09 21:13:30 +00001613 Decl *D = LookupSingleName(TUScope, II, LookupObjCCategoryImplName);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001614 return cast_or_null<ObjCCategoryImplDecl>(D);
1615}
1616
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001617void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
Mike Stump1eb44332009-09-09 15:08:12 +00001618 QualType T1, QualType T2,
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001619 FunctionSet &Functions) {
1620 // C++ [over.match.oper]p3:
1621 // -- The set of non-member candidates is the result of the
1622 // unqualified lookup of operator@ in the context of the
1623 // expression according to the usual rules for name lookup in
1624 // unqualified function calls (3.4.2) except that all member
1625 // functions are ignored. However, if no operand has a class
1626 // type, only those non-member functions in the lookup set
Eli Friedman33a31382009-08-05 19:21:58 +00001627 // that have a first parameter of type T1 or "reference to
1628 // (possibly cv-qualified) T1", when T1 is an enumeration
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001629 // type, or (if there is a right operand) a second parameter
Eli Friedman33a31382009-08-05 19:21:58 +00001630 // of type T2 or "reference to (possibly cv-qualified) T2",
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001631 // when T2 is an enumeration type, are candidate functions.
1632 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
John McCalla24dc2e2009-11-17 02:14:36 +00001633 LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName);
1634 LookupName(Operators, S);
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001636 assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
1637
John McCallf36e02d2009-10-09 21:13:30 +00001638 if (Operators.empty())
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001639 return;
1640
1641 for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end();
1642 Op != OpEnd; ++Op) {
Douglas Gregor364e0212009-06-27 21:05:07 +00001643 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001644 if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context))
1645 Functions.insert(FD); // FIXME: canonical FD
Mike Stump1eb44332009-09-09 15:08:12 +00001646 } else if (FunctionTemplateDecl *FunTmpl
Douglas Gregor364e0212009-06-27 21:05:07 +00001647 = dyn_cast<FunctionTemplateDecl>(*Op)) {
1648 // FIXME: friend operators?
Mike Stump1eb44332009-09-09 15:08:12 +00001649 // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate,
Douglas Gregor364e0212009-06-27 21:05:07 +00001650 // later?
1651 if (!FunTmpl->getDeclContext()->isRecord())
1652 Functions.insert(FunTmpl);
1653 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001654 }
1655}
1656
John McCall6ff07852009-08-07 22:18:02 +00001657static void CollectFunctionDecl(Sema::FunctionSet &Functions,
1658 Decl *D) {
1659 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1660 Functions.insert(Func);
1661 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
1662 Functions.insert(FunTmpl);
1663}
1664
Sebastian Redl644be852009-10-23 19:23:15 +00001665void Sema::ArgumentDependentLookup(DeclarationName Name, bool Operator,
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001666 Expr **Args, unsigned NumArgs,
1667 FunctionSet &Functions) {
1668 // Find all of the associated namespaces and classes based on the
1669 // arguments we have.
1670 AssociatedNamespaceSet AssociatedNamespaces;
1671 AssociatedClassSet AssociatedClasses;
Mike Stump1eb44332009-09-09 15:08:12 +00001672 FindAssociatedClassesAndNamespaces(Args, NumArgs,
John McCall6ff07852009-08-07 22:18:02 +00001673 AssociatedNamespaces,
1674 AssociatedClasses);
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001675
Sebastian Redl644be852009-10-23 19:23:15 +00001676 QualType T1, T2;
1677 if (Operator) {
1678 T1 = Args[0]->getType();
1679 if (NumArgs >= 2)
1680 T2 = Args[1]->getType();
1681 }
1682
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001683 // C++ [basic.lookup.argdep]p3:
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001684 // Let X be the lookup set produced by unqualified lookup (3.4.1)
1685 // and let Y be the lookup set produced by argument dependent
1686 // lookup (defined as follows). If X contains [...] then Y is
1687 // empty. Otherwise Y is the set of declarations found in the
1688 // namespaces associated with the argument types as described
1689 // below. The set of declarations found by the lookup of the name
1690 // is the union of X and Y.
1691 //
1692 // Here, we compute Y and add its members to the overloaded
1693 // candidate set.
1694 for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001695 NSEnd = AssociatedNamespaces.end();
1696 NS != NSEnd; ++NS) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001697 // When considering an associated namespace, the lookup is the
1698 // same as the lookup performed when the associated namespace is
1699 // used as a qualifier (3.4.3.2) except that:
1700 //
1701 // -- Any using-directives in the associated namespace are
1702 // ignored.
1703 //
John McCall6ff07852009-08-07 22:18:02 +00001704 // -- Any namespace-scope friend functions declared in
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001705 // associated classes are visible within their respective
1706 // namespaces even if they are not visible during an ordinary
1707 // lookup (11.4).
1708 DeclContext::lookup_iterator I, E;
John McCall3f9a8a62009-08-11 06:59:38 +00001709 for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) {
John McCall6ff07852009-08-07 22:18:02 +00001710 Decl *D = *I;
John McCall02cace72009-08-28 07:59:38 +00001711 // If the only declaration here is an ordinary friend, consider
1712 // it only if it was declared in an associated classes.
1713 if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) {
John McCall3f9a8a62009-08-11 06:59:38 +00001714 DeclContext *LexDC = D->getLexicalDeclContext();
1715 if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC)))
1716 continue;
1717 }
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Sebastian Redl644be852009-10-23 19:23:15 +00001719 FunctionDecl *Fn;
1720 if (!Operator || !(Fn = dyn_cast<FunctionDecl>(D)) ||
1721 IsAcceptableNonMemberOperatorCandidate(Fn, T1, T2, Context))
1722 CollectFunctionDecl(Functions, D);
Douglas Gregor44bc2d52009-06-23 20:14:09 +00001723 }
1724 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001725}