blob: 82a11b644e245c80e4197a0eae62f940c44e4df6 [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"
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +000028#include <set>
Douglas Gregor2a3009a2009-02-03 19:21:40 +000029#include <vector>
30#include <iterator>
31#include <utility>
32#include <algorithm>
Douglas Gregoreb11cd02009-01-14 22:20:51 +000033
34using namespace clang;
35
Douglas Gregor2a3009a2009-02-03 19:21:40 +000036typedef llvm::SmallVector<UsingDirectiveDecl*, 4> UsingDirectivesTy;
37typedef llvm::DenseSet<NamespaceDecl*> NamespaceSet;
38typedef llvm::SmallVector<Sema::LookupResult, 3> LookupResultsTy;
39
40/// UsingDirAncestorCompare - Implements strict weak ordering of
41/// UsingDirectives. It orders them by address of its common ancestor.
42struct UsingDirAncestorCompare {
43
44 /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext.
45 bool operator () (UsingDirectiveDecl *U, const DeclContext *Ctx) const {
46 return U->getCommonAncestor() < Ctx;
47 }
48
49 /// @brief Compares UsingDirectiveDecl common ancestor with DeclContext.
50 bool operator () (const DeclContext *Ctx, UsingDirectiveDecl *U) const {
51 return Ctx < U->getCommonAncestor();
52 }
53
54 /// @brief Compares UsingDirectiveDecl common ancestors.
55 bool operator () (UsingDirectiveDecl *U1, UsingDirectiveDecl *U2) const {
56 return U1->getCommonAncestor() < U2->getCommonAncestor();
57 }
58};
59
60/// AddNamespaceUsingDirectives - Adds all UsingDirectiveDecl's to heap UDirs
61/// (ordered by common ancestors), found in namespace NS,
62/// including all found (recursively) in their nominated namespaces.
Mike Stump1eb44332009-09-09 15:08:12 +000063void AddNamespaceUsingDirectives(ASTContext &Context,
Douglas Gregor6ab35242009-04-09 21:40:53 +000064 DeclContext *NS,
Douglas Gregor2a3009a2009-02-03 19:21:40 +000065 UsingDirectivesTy &UDirs,
66 NamespaceSet &Visited) {
67 DeclContext::udir_iterator I, End;
68
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000069 for (llvm::tie(I, End) = NS->getUsingDirectives(); I !=End; ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +000070 UDirs.push_back(*I);
71 std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare());
72 NamespaceDecl *Nominated = (*I)->getNominatedNamespace();
73 if (Visited.insert(Nominated).second)
Douglas Gregor6ab35242009-04-09 21:40:53 +000074 AddNamespaceUsingDirectives(Context, Nominated, UDirs, /*ref*/ Visited);
Douglas Gregor2a3009a2009-02-03 19:21:40 +000075 }
76}
77
78/// AddScopeUsingDirectives - Adds all UsingDirectiveDecl's found in Scope S,
79/// including all found in the namespaces they nominate.
Mike Stump1eb44332009-09-09 15:08:12 +000080static void AddScopeUsingDirectives(ASTContext &Context, Scope *S,
Douglas Gregor6ab35242009-04-09 21:40:53 +000081 UsingDirectivesTy &UDirs) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +000082 NamespaceSet VisitedNS;
83
84 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
85
86 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Ctx))
87 VisitedNS.insert(NS);
88
Douglas Gregor6ab35242009-04-09 21:40:53 +000089 AddNamespaceUsingDirectives(Context, Ctx, UDirs, /*ref*/ VisitedNS);
Douglas Gregor2a3009a2009-02-03 19:21:40 +000090
91 } else {
Chris Lattnerb28317a2009-03-28 19:18:32 +000092 Scope::udir_iterator I = S->using_directives_begin(),
93 End = S->using_directives_end();
Douglas Gregor2a3009a2009-02-03 19:21:40 +000094
95 for (; I != End; ++I) {
Chris Lattnerb28317a2009-03-28 19:18:32 +000096 UsingDirectiveDecl *UD = I->getAs<UsingDirectiveDecl>();
Douglas Gregor2a3009a2009-02-03 19:21:40 +000097 UDirs.push_back(UD);
98 std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare());
99
100 NamespaceDecl *Nominated = UD->getNominatedNamespace();
101 if (!VisitedNS.count(Nominated)) {
102 VisitedNS.insert(Nominated);
Mike Stump1eb44332009-09-09 15:08:12 +0000103 AddNamespaceUsingDirectives(Context, Nominated, UDirs,
Douglas Gregor6ab35242009-04-09 21:40:53 +0000104 /*ref*/ VisitedNS);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000105 }
106 }
107 }
108}
109
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000110/// MaybeConstructOverloadSet - Name lookup has determined that the
111/// elements in [I, IEnd) have the name that we are looking for, and
112/// *I is a match for the namespace. This routine returns an
113/// appropriate Decl for name lookup, which may either be *I or an
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000114/// OverloadedFunctionDecl that represents the overloaded functions in
Mike Stump1eb44332009-09-09 15:08:12 +0000115/// [I, IEnd).
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000116///
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000117/// The existance of this routine is temporary; users of LookupResult
118/// should be able to handle multiple results, to deal with cases of
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000119/// ambiguity and overloaded functions without needing to create a
120/// Decl node.
121template<typename DeclIterator>
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000122static NamedDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000123MaybeConstructOverloadSet(ASTContext &Context,
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000124 DeclIterator I, DeclIterator IEnd) {
125 assert(I != IEnd && "Iterator range cannot be empty");
Mike Stump1eb44332009-09-09 15:08:12 +0000126 assert(!isa<OverloadedFunctionDecl>(*I) &&
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000127 "Cannot have an overloaded function");
128
Douglas Gregore53060f2009-06-25 22:08:12 +0000129 if ((*I)->isFunctionOrFunctionTemplate()) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000130 // If we found a function, there might be more functions. If
131 // so, collect them into an overload set.
132 DeclIterator Last = I;
133 OverloadedFunctionDecl *Ovl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000134 for (++Last;
135 Last != IEnd && (*Last)->isFunctionOrFunctionTemplate();
Douglas Gregore53060f2009-06-25 22:08:12 +0000136 ++Last) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000137 if (!Ovl) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000138 // FIXME: We leak this overload set. Eventually, we want to stop
139 // building the declarations for these overload sets, so there will be
140 // nothing to leak.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000141 Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(),
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000142 (*I)->getDeclName());
Anders Carlssone136e0e2009-06-26 06:29:23 +0000143 NamedDecl *ND = (*I)->getUnderlyingDecl();
Anders Carlsson58badb72009-06-26 05:26:50 +0000144 if (isa<FunctionDecl>(ND))
145 Ovl->addOverload(cast<FunctionDecl>(ND));
Douglas Gregore53060f2009-06-25 22:08:12 +0000146 else
Anders Carlsson58badb72009-06-26 05:26:50 +0000147 Ovl->addOverload(cast<FunctionTemplateDecl>(ND));
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000148 }
Douglas Gregore53060f2009-06-25 22:08:12 +0000149
Anders Carlssone136e0e2009-06-26 06:29:23 +0000150 NamedDecl *ND = (*Last)->getUnderlyingDecl();
Anders Carlsson58badb72009-06-26 05:26:50 +0000151 if (isa<FunctionDecl>(ND))
152 Ovl->addOverload(cast<FunctionDecl>(ND));
Douglas Gregore53060f2009-06-25 22:08:12 +0000153 else
Anders Carlsson58badb72009-06-26 05:26:50 +0000154 Ovl->addOverload(cast<FunctionTemplateDecl>(ND));
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000157 // If we had more than one function, we built an overload
158 // set. Return it.
159 if (Ovl)
160 return Ovl;
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000163 return *I;
164}
165
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000166/// Merges together multiple LookupResults dealing with duplicated Decl's.
167static Sema::LookupResult
168MergeLookupResults(ASTContext &Context, LookupResultsTy &Results) {
169 typedef Sema::LookupResult LResult;
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000170 typedef llvm::SmallPtrSet<NamedDecl*, 4> DeclsSetTy;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000171
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000172 // Remove duplicated Decl pointing at same Decl, by storing them in
173 // associative collection. This might be case for code like:
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000174 //
175 // namespace A { int i; }
176 // namespace B { using namespace A; }
177 // namespace C { using namespace A; }
178 //
179 // void foo() {
180 // using namespace B;
181 // using namespace C;
182 // ++i; // finds A::i, from both namespace B and C at global scope
183 // }
184 //
185 // C++ [namespace.qual].p3:
186 // The same declaration found more than once is not an ambiguity
187 // (because it is still a unique declaration).
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000188 DeclsSetTy FoundDecls;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000189
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000190 // Counter of tag names, and functions for resolving ambiguity
191 // and name hiding.
192 std::size_t TagNames = 0, Functions = 0, OrdinaryNonFunc = 0;
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000193
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000194 LookupResultsTy::iterator I = Results.begin(), End = Results.end();
195
196 // No name lookup results, return early.
197 if (I == End) return LResult::CreateLookupResult(Context, 0);
198
199 // Keep track of the tag declaration we found. We only use this if
200 // we find a single tag declaration.
201 TagDecl *TagFound = 0;
202
203 for (; I != End; ++I) {
204 switch (I->getKind()) {
205 case LResult::NotFound:
206 assert(false &&
207 "Should be always successful name lookup result here.");
208 break;
209
210 case LResult::AmbiguousReference:
211 case LResult::AmbiguousBaseSubobjectTypes:
212 case LResult::AmbiguousBaseSubobjects:
213 assert(false && "Shouldn't get ambiguous lookup here.");
214 break;
215
216 case LResult::Found: {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000217 NamedDecl *ND = I->getAsDecl()->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000219 if (TagDecl *TD = dyn_cast<TagDecl>(ND)) {
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000220 TagFound = TD->getCanonicalDecl();
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000221 TagNames += FoundDecls.insert(TagFound)? 1 : 0;
Douglas Gregore53060f2009-06-25 22:08:12 +0000222 } else if (ND->isFunctionOrFunctionTemplate())
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000223 Functions += FoundDecls.insert(ND)? 1 : 0;
224 else
225 FoundDecls.insert(ND);
226 break;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000227 }
228
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000229 case LResult::FoundOverloaded:
230 for (LResult::iterator FI = I->begin(), FEnd = I->end(); FI != FEnd; ++FI)
231 Functions += FoundDecls.insert(*FI)? 1 : 0;
232 break;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000233 }
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000234 }
235 OrdinaryNonFunc = FoundDecls.size() - TagNames - Functions;
236 bool Ambiguous = false, NameHidesTags = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000238 if (FoundDecls.size() == 1) {
239 // 1) Exactly one result.
240 } else if (TagNames > 1) {
241 // 2) Multiple tag names (even though they may be hidden by an
242 // object name).
243 Ambiguous = true;
244 } else if (FoundDecls.size() - TagNames == 1) {
245 // 3) Ordinary name hides (optional) tag.
246 NameHidesTags = TagFound;
247 } else if (Functions) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000248 // C++ [basic.lookup].p1:
249 // ... Name lookup may associate more than one declaration with
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000250 // a name if it finds the name to be a function name; the declarations
251 // are said to form a set of overloaded functions (13.1).
252 // Overload resolution (13.3) takes place after name lookup has succeeded.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000253 //
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000254 if (!OrdinaryNonFunc) {
255 // 4) Functions hide tag names.
256 NameHidesTags = TagFound;
257 } else {
258 // 5) Functions + ordinary names.
259 Ambiguous = true;
260 }
261 } else {
262 // 6) Multiple non-tag names
263 Ambiguous = true;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000264 }
265
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000266 if (Ambiguous)
Mike Stump1eb44332009-09-09 15:08:12 +0000267 return LResult::CreateLookupResult(Context,
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000268 FoundDecls.begin(), FoundDecls.size());
269 if (NameHidesTags) {
270 // There's only one tag, TagFound. Remove it.
271 assert(TagFound && FoundDecls.count(TagFound) && "No tag name found?");
272 FoundDecls.erase(TagFound);
273 }
274
275 // Return successful name lookup result.
276 return LResult::CreateLookupResult(Context,
277 MaybeConstructOverloadSet(Context,
278 FoundDecls.begin(),
279 FoundDecls.end()));
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000280}
281
282// Retrieve the set of identifier namespaces that correspond to a
283// specific kind of name lookup.
Mike Stump1eb44332009-09-09 15:08:12 +0000284inline unsigned
285getIdentifierNamespacesFromLookupNameKind(Sema::LookupNameKind NameKind,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000286 bool CPlusPlus) {
287 unsigned IDNS = 0;
288 switch (NameKind) {
289 case Sema::LookupOrdinaryName:
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000290 case Sema::LookupOperatorName:
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000291 case Sema::LookupRedeclarationWithLinkage:
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000292 IDNS = Decl::IDNS_Ordinary;
293 if (CPlusPlus)
294 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member;
295 break;
296
297 case Sema::LookupTagName:
298 IDNS = Decl::IDNS_Tag;
299 break;
300
301 case Sema::LookupMemberName:
302 IDNS = Decl::IDNS_Member;
303 if (CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000304 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000305 break;
306
307 case Sema::LookupNestedNameSpecifierName:
308 case Sema::LookupNamespaceName:
309 IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member;
310 break;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000311
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000312 case Sema::LookupObjCProtocolName:
313 IDNS = Decl::IDNS_ObjCProtocol;
314 break;
315
316 case Sema::LookupObjCImplementationName:
317 IDNS = Decl::IDNS_ObjCImplementation;
318 break;
319
320 case Sema::LookupObjCCategoryImplName:
321 IDNS = Decl::IDNS_ObjCCategoryImpl;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000322 break;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000323 }
324 return IDNS;
325}
326
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000327Sema::LookupResult
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000328Sema::LookupResult::CreateLookupResult(ASTContext &Context, NamedDecl *D) {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000329 if (D)
330 D = D->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000332 LookupResult Result;
333 Result.StoredKind = (D && isa<OverloadedFunctionDecl>(D))?
334 OverloadedDeclSingleDecl : SingleDecl;
335 Result.First = reinterpret_cast<uintptr_t>(D);
336 Result.Last = 0;
337 Result.Context = &Context;
338 return Result;
339}
340
Douglas Gregor4bb64e72009-01-15 02:19:31 +0000341/// @brief Moves the name-lookup results from Other to this LookupResult.
Douglas Gregor69d993a2009-01-17 01:13:24 +0000342Sema::LookupResult
Mike Stump1eb44332009-09-09 15:08:12 +0000343Sema::LookupResult::CreateLookupResult(ASTContext &Context,
344 IdentifierResolver::iterator F,
Douglas Gregor69d993a2009-01-17 01:13:24 +0000345 IdentifierResolver::iterator L) {
346 LookupResult Result;
347 Result.Context = &Context;
348
Douglas Gregore53060f2009-06-25 22:08:12 +0000349 if (F != L && (*F)->isFunctionOrFunctionTemplate()) {
Douglas Gregor7176fff2009-01-15 00:26:24 +0000350 IdentifierResolver::iterator Next = F;
351 ++Next;
Douglas Gregore53060f2009-06-25 22:08:12 +0000352 if (Next != L && (*Next)->isFunctionOrFunctionTemplate()) {
Douglas Gregor69d993a2009-01-17 01:13:24 +0000353 Result.StoredKind = OverloadedDeclFromIdResolver;
354 Result.First = F.getAsOpaqueValue();
355 Result.Last = L.getAsOpaqueValue();
356 return Result;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358 }
Douglas Gregor516ff432009-04-24 02:57:34 +0000359
Anders Carlssone136e0e2009-06-26 06:29:23 +0000360 NamedDecl *D = *F;
361 if (D)
362 D = D->getUnderlyingDecl();
Anders Carlsson8b50d012009-06-26 03:37:05 +0000363
Douglas Gregor69d993a2009-01-17 01:13:24 +0000364 Result.StoredKind = SingleDecl;
Douglas Gregor516ff432009-04-24 02:57:34 +0000365 Result.First = reinterpret_cast<uintptr_t>(D);
Douglas Gregor69d993a2009-01-17 01:13:24 +0000366 Result.Last = 0;
367 return Result;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000368}
369
Douglas Gregor69d993a2009-01-17 01:13:24 +0000370Sema::LookupResult
Mike Stump1eb44332009-09-09 15:08:12 +0000371Sema::LookupResult::CreateLookupResult(ASTContext &Context,
372 DeclContext::lookup_iterator F,
Douglas Gregor69d993a2009-01-17 01:13:24 +0000373 DeclContext::lookup_iterator L) {
374 LookupResult Result;
375 Result.Context = &Context;
376
Douglas Gregore53060f2009-06-25 22:08:12 +0000377 if (F != L && (*F)->isFunctionOrFunctionTemplate()) {
Douglas Gregor7176fff2009-01-15 00:26:24 +0000378 DeclContext::lookup_iterator Next = F;
379 ++Next;
Douglas Gregore53060f2009-06-25 22:08:12 +0000380 if (Next != L && (*Next)->isFunctionOrFunctionTemplate()) {
Douglas Gregor69d993a2009-01-17 01:13:24 +0000381 Result.StoredKind = OverloadedDeclFromDeclContext;
382 Result.First = reinterpret_cast<uintptr_t>(F);
383 Result.Last = reinterpret_cast<uintptr_t>(L);
384 return Result;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000385 }
386 }
Douglas Gregor516ff432009-04-24 02:57:34 +0000387
Anders Carlssone136e0e2009-06-26 06:29:23 +0000388 NamedDecl *D = *F;
389 if (D)
390 D = D->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor69d993a2009-01-17 01:13:24 +0000392 Result.StoredKind = SingleDecl;
Douglas Gregor516ff432009-04-24 02:57:34 +0000393 Result.First = reinterpret_cast<uintptr_t>(D);
Douglas Gregor69d993a2009-01-17 01:13:24 +0000394 Result.Last = 0;
395 return Result;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000396}
397
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000398/// @brief Determine the result of name lookup.
399Sema::LookupResult::LookupKind Sema::LookupResult::getKind() const {
400 switch (StoredKind) {
401 case SingleDecl:
402 return (reinterpret_cast<Decl *>(First) != 0)? Found : NotFound;
403
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000404 case OverloadedDeclSingleDecl:
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000405 case OverloadedDeclFromIdResolver:
406 case OverloadedDeclFromDeclContext:
407 return FoundOverloaded;
408
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000409 case AmbiguousLookupStoresBasePaths:
Douglas Gregor7176fff2009-01-15 00:26:24 +0000410 return Last? AmbiguousBaseSubobjectTypes : AmbiguousBaseSubobjects;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000411
412 case AmbiguousLookupStoresDecls:
413 return AmbiguousReference;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000414 }
415
Douglas Gregor7176fff2009-01-15 00:26:24 +0000416 // We can't ever get here.
417 return NotFound;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000418}
419
420/// @brief Converts the result of name lookup into a single (possible
421/// NULL) pointer to a declaration.
422///
423/// The resulting declaration will either be the declaration we found
424/// (if only a single declaration was found), an
425/// OverloadedFunctionDecl (if an overloaded function was found), or
426/// NULL (if no declaration was found). This conversion must not be
Mike Stump1eb44332009-09-09 15:08:12 +0000427/// used anywhere where name lookup could result in an ambiguity.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000428///
429/// The OverloadedFunctionDecl conversion is meant as a stop-gap
430/// solution, since it causes the OverloadedFunctionDecl to be
431/// leaked. FIXME: Eventually, there will be a better way to iterate
432/// over the set of overloaded functions returned by name lookup.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000433NamedDecl *Sema::LookupResult::getAsDecl() const {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000434 switch (StoredKind) {
435 case SingleDecl:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000436 return reinterpret_cast<NamedDecl *>(First);
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000437
438 case OverloadedDeclFromIdResolver:
439 return MaybeConstructOverloadSet(*Context,
440 IdentifierResolver::iterator::getFromOpaqueValue(First),
441 IdentifierResolver::iterator::getFromOpaqueValue(Last));
442
443 case OverloadedDeclFromDeclContext:
Mike Stump1eb44332009-09-09 15:08:12 +0000444 return MaybeConstructOverloadSet(*Context,
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000445 reinterpret_cast<DeclContext::lookup_iterator>(First),
446 reinterpret_cast<DeclContext::lookup_iterator>(Last));
447
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000448 case OverloadedDeclSingleDecl:
449 return reinterpret_cast<OverloadedFunctionDecl*>(First);
450
451 case AmbiguousLookupStoresDecls:
452 case AmbiguousLookupStoresBasePaths:
Mike Stump1eb44332009-09-09 15:08:12 +0000453 assert(false &&
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000454 "Name lookup returned an ambiguity that could not be handled");
455 break;
456 }
457
458 return 0;
459}
460
Douglas Gregor7176fff2009-01-15 00:26:24 +0000461/// @brief Retrieves the BasePaths structure describing an ambiguous
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000462/// name lookup, or null.
Douglas Gregora8f32e02009-10-06 17:59:45 +0000463CXXBasePaths *Sema::LookupResult::getBasePaths() const {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000464 if (StoredKind == AmbiguousLookupStoresBasePaths)
Douglas Gregora8f32e02009-10-06 17:59:45 +0000465 return reinterpret_cast<CXXBasePaths *>(First);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000466 return 0;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000467}
468
Mike Stump1eb44332009-09-09 15:08:12 +0000469Sema::LookupResult::iterator::reference
Douglas Gregord8635172009-02-02 21:35:47 +0000470Sema::LookupResult::iterator::operator*() const {
471 switch (Result->StoredKind) {
472 case SingleDecl:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000473 return reinterpret_cast<NamedDecl*>(Current);
Douglas Gregord8635172009-02-02 21:35:47 +0000474
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000475 case OverloadedDeclSingleDecl:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000476 return *reinterpret_cast<NamedDecl**>(Current);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000477
Douglas Gregord8635172009-02-02 21:35:47 +0000478 case OverloadedDeclFromIdResolver:
479 return *IdentifierResolver::iterator::getFromOpaqueValue(Current);
480
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000481 case AmbiguousLookupStoresBasePaths:
Douglas Gregor31a19b62009-04-01 21:51:26 +0000482 if (Result->Last)
483 return *reinterpret_cast<NamedDecl**>(Current);
484
485 // Fall through to handle the DeclContext::lookup_iterator we're
486 // storing.
487
488 case OverloadedDeclFromDeclContext:
489 case AmbiguousLookupStoresDecls:
490 return *reinterpret_cast<DeclContext::lookup_iterator>(Current);
Douglas Gregord8635172009-02-02 21:35:47 +0000491 }
492
493 return 0;
494}
495
496Sema::LookupResult::iterator& Sema::LookupResult::iterator::operator++() {
497 switch (Result->StoredKind) {
498 case SingleDecl:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000499 Current = reinterpret_cast<uintptr_t>((NamedDecl*)0);
Douglas Gregord8635172009-02-02 21:35:47 +0000500 break;
501
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000502 case OverloadedDeclSingleDecl: {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000503 NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000504 ++I;
505 Current = reinterpret_cast<uintptr_t>(I);
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000506 break;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000507 }
508
Douglas Gregord8635172009-02-02 21:35:47 +0000509 case OverloadedDeclFromIdResolver: {
Mike Stump1eb44332009-09-09 15:08:12 +0000510 IdentifierResolver::iterator I
Douglas Gregord8635172009-02-02 21:35:47 +0000511 = IdentifierResolver::iterator::getFromOpaqueValue(Current);
512 ++I;
513 Current = I.getAsOpaqueValue();
514 break;
515 }
516
Mike Stump1eb44332009-09-09 15:08:12 +0000517 case AmbiguousLookupStoresBasePaths:
Douglas Gregor31a19b62009-04-01 21:51:26 +0000518 if (Result->Last) {
519 NamedDecl ** I = reinterpret_cast<NamedDecl**>(Current);
520 ++I;
521 Current = reinterpret_cast<uintptr_t>(I);
522 break;
523 }
524 // Fall through to handle the DeclContext::lookup_iterator we're
525 // storing.
526
527 case OverloadedDeclFromDeclContext:
528 case AmbiguousLookupStoresDecls: {
Mike Stump1eb44332009-09-09 15:08:12 +0000529 DeclContext::lookup_iterator I
Douglas Gregord8635172009-02-02 21:35:47 +0000530 = reinterpret_cast<DeclContext::lookup_iterator>(Current);
531 ++I;
532 Current = reinterpret_cast<uintptr_t>(I);
533 break;
534 }
Douglas Gregord8635172009-02-02 21:35:47 +0000535 }
536
537 return *this;
538}
539
540Sema::LookupResult::iterator Sema::LookupResult::begin() {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000541 switch (StoredKind) {
542 case SingleDecl:
543 case OverloadedDeclFromIdResolver:
544 case OverloadedDeclFromDeclContext:
545 case AmbiguousLookupStoresDecls:
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000546 return iterator(this, First);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000547
548 case OverloadedDeclSingleDecl: {
549 OverloadedFunctionDecl * Ovl =
550 reinterpret_cast<OverloadedFunctionDecl*>(First);
Mike Stump1eb44332009-09-09 15:08:12 +0000551 return iterator(this,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000552 reinterpret_cast<uintptr_t>(&(*Ovl->function_begin())));
553 }
554
555 case AmbiguousLookupStoresBasePaths:
556 if (Last)
Mike Stump1eb44332009-09-09 15:08:12 +0000557 return iterator(this,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000558 reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_begin()));
559 else
560 return iterator(this,
561 reinterpret_cast<uintptr_t>(getBasePaths()->front().Decls.first));
562 }
563
564 // Required to suppress GCC warning.
565 return iterator();
Douglas Gregord8635172009-02-02 21:35:47 +0000566}
567
568Sema::LookupResult::iterator Sema::LookupResult::end() {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000569 switch (StoredKind) {
570 case SingleDecl:
571 case OverloadedDeclFromIdResolver:
572 case OverloadedDeclFromDeclContext:
573 case AmbiguousLookupStoresDecls:
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000574 return iterator(this, Last);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000575
576 case OverloadedDeclSingleDecl: {
577 OverloadedFunctionDecl * Ovl =
578 reinterpret_cast<OverloadedFunctionDecl*>(First);
Mike Stump1eb44332009-09-09 15:08:12 +0000579 return iterator(this,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000580 reinterpret_cast<uintptr_t>(&(*Ovl->function_end())));
581 }
582
583 case AmbiguousLookupStoresBasePaths:
584 if (Last)
Mike Stump1eb44332009-09-09 15:08:12 +0000585 return iterator(this,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000586 reinterpret_cast<uintptr_t>(getBasePaths()->found_decls_end()));
587 else
588 return iterator(this, reinterpret_cast<uintptr_t>(
589 getBasePaths()->front().Decls.second));
590 }
591
592 // Required to suppress GCC warning.
593 return iterator();
594}
595
596void Sema::LookupResult::Destroy() {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000597 if (CXXBasePaths *Paths = getBasePaths())
Douglas Gregor31a19b62009-04-01 21:51:26 +0000598 delete Paths;
599 else if (getKind() == AmbiguousReference)
600 delete[] reinterpret_cast<NamedDecl **>(First);
Douglas Gregord8635172009-02-02 21:35:47 +0000601}
602
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000603static void
604CppNamespaceLookup(ASTContext &Context, DeclContext *NS,
605 DeclarationName Name, Sema::LookupNameKind NameKind,
606 unsigned IDNS, LookupResultsTy &Results,
607 UsingDirectivesTy *UDirs = 0) {
608
609 assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
610
611 // Perform qualified name lookup into the LookupCtx.
612 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000613 for (llvm::tie(I, E) = NS->lookup(Name); I != E; ++I)
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000614 if (Sema::isAcceptableLookupResult(*I, NameKind, IDNS)) {
615 Results.push_back(Sema::LookupResult::CreateLookupResult(Context, I, E));
616 break;
617 }
618
619 if (UDirs) {
620 // For each UsingDirectiveDecl, which common ancestor is equal
621 // to NS, we preform qualified name lookup into namespace nominated by it.
622 UsingDirectivesTy::const_iterator UI, UEnd;
623 llvm::tie(UI, UEnd) =
624 std::equal_range(UDirs->begin(), UDirs->end(), NS,
625 UsingDirAncestorCompare());
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000627 for (; UI != UEnd; ++UI)
628 CppNamespaceLookup(Context, (*UI)->getNominatedNamespace(),
629 Name, NameKind, IDNS, Results);
630 }
631}
632
633static bool isNamespaceOrTranslationUnitScope(Scope *S) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000634 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()))
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000635 return Ctx->isFileContext();
636 return false;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000637}
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000638
Douglas Gregore942bbe2009-09-10 16:57:35 +0000639// Find the next outer declaration context corresponding to this scope.
640static DeclContext *findOuterContext(Scope *S) {
641 for (S = S->getParent(); S; S = S->getParent())
642 if (S->getEntity())
643 return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext();
644
645 return 0;
646}
647
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000648std::pair<bool, Sema::LookupResult>
649Sema::CppLookupName(Scope *S, DeclarationName Name,
650 LookupNameKind NameKind, bool RedeclarationOnly) {
651 assert(getLangOptions().CPlusPlus &&
652 "Can perform only C++ lookup");
Mike Stump1eb44332009-09-09 15:08:12 +0000653 unsigned IDNS
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000654 = getIdentifierNamespacesFromLookupNameKind(NameKind, /*CPlusPlus*/ true);
John McCall02cace72009-08-28 07:59:38 +0000655
656 // If we're testing for redeclarations, also look in the friend namespaces.
657 if (RedeclarationOnly) {
658 if (IDNS & Decl::IDNS_Tag) IDNS |= Decl::IDNS_TagFriend;
659 if (IDNS & Decl::IDNS_Ordinary) IDNS |= Decl::IDNS_OrdinaryFriend;
660 }
661
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000662 Scope *Initial = S;
Mike Stump1eb44332009-09-09 15:08:12 +0000663 IdentifierResolver::iterator
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000664 I = IdResolver.begin(Name),
665 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000666
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000667 // First we lookup local scope.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000668 // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir]
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000669 // ...During unqualified name lookup (3.4.1), the names appear as if
670 // they were declared in the nearest enclosing namespace which contains
671 // both the using-directive and the nominated namespace.
Eli Friedman33a31382009-08-05 19:21:58 +0000672 // [Note: in this context, "contains" means "contains directly or
Mike Stump1eb44332009-09-09 15:08:12 +0000673 // indirectly".
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000674 //
675 // For example:
676 // namespace A { int i; }
677 // void foo() {
678 // int i;
679 // {
680 // using namespace A;
681 // ++i; // finds local 'i', A::i appears at global scope
682 // }
683 // }
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000684 //
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000685 for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000686 // Check whether the IdResolver has anything in this scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000687 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000688 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
689 // We found something. Look for anything else in our scope
690 // with this same name and in an acceptable identifier
691 // namespace, so that we can construct an overload set if we
692 // need to.
693 IdentifierResolver::iterator LastI = I;
694 for (++LastI; LastI != IEnd; ++LastI) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000695 if (!S->isDeclScope(DeclPtrTy::make(*LastI)))
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000696 break;
697 }
698 LookupResult Result =
699 LookupResult::CreateLookupResult(Context, I, LastI);
700 return std::make_pair(true, Result);
701 }
702 }
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000703 if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
704 LookupResult R;
Douglas Gregore942bbe2009-09-10 16:57:35 +0000705
706 DeclContext *OuterCtx = findOuterContext(S);
707 for (; Ctx && Ctx->getPrimaryContext() != OuterCtx;
708 Ctx = Ctx->getLookupParent()) {
709 if (Ctx->isFunctionOrMethod())
710 continue;
711
712 // Perform qualified name lookup into this context.
713 // FIXME: In some cases, we know that every name that could be found by
714 // this qualified name lookup will also be on the identifier chain. For
715 // example, inside a class without any base classes, we never need to
716 // perform qualified lookup because all of the members are on top of the
717 // identifier chain.
Douglas Gregor551f48c2009-03-27 04:21:56 +0000718 R = LookupQualifiedName(Ctx, Name, NameKind, RedeclarationOnly);
Douglas Gregorc19ee3e2009-06-17 23:37:01 +0000719 if (R)
Douglas Gregor551f48c2009-03-27 04:21:56 +0000720 return std::make_pair(true, R);
721 }
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000722 }
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000723 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000724
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000725 // Collect UsingDirectiveDecls in all scopes, and recursively all
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000726 // nominated namespaces by those using-directives.
Mike Stump390b4cc2009-05-16 07:39:55 +0000727 // UsingDirectives are pushed to heap, in common ancestor pointer value order.
728 // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
729 // don't build it for each lookup!
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000730 UsingDirectivesTy UDirs;
731 for (Scope *SC = Initial; SC; SC = SC->getParent())
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000732 if (SC->getFlags() & Scope::DeclScope)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000733 AddScopeUsingDirectives(Context, SC, UDirs);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000734
735 // Sort heapified UsingDirectiveDecls.
Douglas Gregorb738e082009-05-18 22:06:54 +0000736 std::sort_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000737
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000738 // Lookup namespace scope, and global scope.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000739 // Unqualified name lookup in C++ requires looking into scopes
740 // that aren't strictly lexical, and therefore we walk through the
741 // context as well as walking through the scopes.
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000742
743 LookupResultsTy LookupResults;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000744 bool LookedInCtx = false;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000745 for (; S; S = S->getParent()) {
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000746 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Douglas Gregora24eb4e2009-08-24 18:55:03 +0000747 if (Ctx->isTransparentContext())
748 continue;
749
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000750 assert(Ctx && Ctx->isFileContext() &&
751 "We should have been looking only at file context here already.");
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000752
753 // Check whether the IdResolver has anything in this scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000754 for (; I != IEnd && S->isDeclScope(DeclPtrTy::make(*I)); ++I) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000755 if (isAcceptableLookupResult(*I, NameKind, IDNS)) {
756 // We found something. Look for anything else in our scope
757 // with this same name and in an acceptable identifier
758 // namespace, so that we can construct an overload set if we
759 // need to.
760 IdentifierResolver::iterator LastI = I;
761 for (++LastI; LastI != IEnd; ++LastI) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000762 if (!S->isDeclScope(DeclPtrTy::make(*LastI)))
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000763 break;
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000766 // We store name lookup result, and continue trying to look into
767 // associated context, and maybe namespaces nominated by
768 // using-directives.
769 LookupResults.push_back(
770 LookupResult::CreateLookupResult(Context, I, LastI));
771 break;
772 }
773 }
774
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000775 LookedInCtx = true;
776 // Look into context considering using-directives.
777 CppNamespaceLookup(Context, Ctx, Name, NameKind, IDNS,
778 LookupResults, &UDirs);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000779
John McCall71fdaf42009-10-07 22:04:40 +0000780 LookupResult Result;
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000781 if ((Result = MergeLookupResults(Context, LookupResults)) ||
782 (RedeclarationOnly && !Ctx->isTransparentContext()))
783 return std::make_pair(true, Result);
784 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000785
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000786 if (!(LookedInCtx || LookupResults.empty())) {
787 // We didn't Performed lookup in Scope entity, so we return
788 // result form IdentifierResolver.
789 assert((LookupResults.size() == 1) && "Wrong size!");
790 return std::make_pair(true, LookupResults.front());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000791 }
792 return std::make_pair(false, LookupResult());
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000793}
794
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000795/// @brief Perform unqualified name lookup starting from a given
796/// scope.
797///
798/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
799/// used to find names within the current scope. For example, 'x' in
800/// @code
801/// int x;
802/// int f() {
803/// return x; // unqualified name look finds 'x' in the global scope
804/// }
805/// @endcode
806///
807/// Different lookup criteria can find different names. For example, a
808/// particular scope can have both a struct and a function of the same
809/// name, and each can be found by certain lookup criteria. For more
810/// information about lookup criteria, see the documentation for the
811/// class LookupCriteria.
812///
813/// @param S The scope from which unqualified name lookup will
814/// begin. If the lookup criteria permits, name lookup may also search
815/// in the parent scopes.
816///
817/// @param Name The name of the entity that we are searching for.
818///
Douglas Gregor3e41d602009-02-13 23:20:09 +0000819/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +0000820/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +0000821/// C library functions (like "malloc") are implicitly declared.
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000822///
823/// @returns The result of name lookup, which includes zero or more
824/// declarations and possibly additional information used to diagnose
825/// ambiguities.
Mike Stump1eb44332009-09-09 15:08:12 +0000826Sema::LookupResult
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000827Sema::LookupName(Scope *S, DeclarationName Name, LookupNameKind NameKind,
Douglas Gregor3e41d602009-02-13 23:20:09 +0000828 bool RedeclarationOnly, bool AllowBuiltinCreation,
829 SourceLocation Loc) {
Douglas Gregor69d993a2009-01-17 01:13:24 +0000830 if (!Name) return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000831
832 if (!getLangOptions().CPlusPlus) {
833 // Unqualified name lookup in C/Objective-C is purely lexical, so
834 // search in the declarations attached to the name.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000835 unsigned IDNS = 0;
836 switch (NameKind) {
837 case Sema::LookupOrdinaryName:
838 IDNS = Decl::IDNS_Ordinary;
839 break;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000840
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000841 case Sema::LookupTagName:
842 IDNS = Decl::IDNS_Tag;
843 break;
844
845 case Sema::LookupMemberName:
846 IDNS = Decl::IDNS_Member;
847 break;
848
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000849 case Sema::LookupOperatorName:
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000850 case Sema::LookupNestedNameSpecifierName:
851 case Sema::LookupNamespaceName:
852 assert(false && "C does not perform these kinds of name lookup");
853 break;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000854
855 case Sema::LookupRedeclarationWithLinkage:
856 // Find the nearest non-transparent declaration scope.
857 while (!(S->getFlags() & Scope::DeclScope) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000858 (S->getEntity() &&
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000859 static_cast<DeclContext *>(S->getEntity())
860 ->isTransparentContext()))
861 S = S->getParent();
862 IDNS = Decl::IDNS_Ordinary;
863 break;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000864
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000865 case Sema::LookupObjCProtocolName:
866 IDNS = Decl::IDNS_ObjCProtocol;
867 break;
868
869 case Sema::LookupObjCImplementationName:
870 IDNS = Decl::IDNS_ObjCImplementation;
871 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000873 case Sema::LookupObjCCategoryImplName:
874 IDNS = Decl::IDNS_ObjCCategoryImpl;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000875 break;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000876 }
877
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000878 // Scan up the scope chain looking for a decl that matches this
879 // identifier that is in the appropriate namespace. This search
880 // should not take long, as shadowing of names is uncommon, and
881 // deep shadowing is extremely uncommon.
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000882 bool LeftStartingScope = false;
883
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000884 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Mike Stump1eb44332009-09-09 15:08:12 +0000885 IEnd = IdResolver.end();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000886 I != IEnd; ++I)
Douglas Gregorf9201e02009-02-11 23:02:49 +0000887 if ((*I)->isInIdentifierNamespace(IDNS)) {
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000888 if (NameKind == LookupRedeclarationWithLinkage) {
889 // Determine whether this (or a previous) declaration is
890 // out-of-scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000891 if (!LeftStartingScope && !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000892 LeftStartingScope = true;
893
894 // If we found something outside of our starting scope that
895 // does not have linkage, skip it.
896 if (LeftStartingScope && !((*I)->hasLinkage()))
897 continue;
898 }
899
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000900 if ((*I)->getAttr<OverloadableAttr>()) {
Douglas Gregorf9201e02009-02-11 23:02:49 +0000901 // If this declaration has the "overloadable" attribute, we
902 // might have a set of overloaded functions.
903
904 // Figure out what scope the identifier is in.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000905 while (!(S->getFlags() & Scope::DeclScope) ||
906 !S->isDeclScope(DeclPtrTy::make(*I)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000907 S = S->getParent();
908
909 // Find the last declaration in this scope (with the same
910 // name, naturally).
911 IdentifierResolver::iterator LastI = I;
912 for (++LastI; LastI != IEnd; ++LastI) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000913 if (!S->isDeclScope(DeclPtrTy::make(*LastI)))
Douglas Gregorf9201e02009-02-11 23:02:49 +0000914 break;
915 }
916
917 return LookupResult::CreateLookupResult(Context, I, LastI);
918 }
919
920 // We have a single lookup result.
Douglas Gregor69d993a2009-01-17 01:13:24 +0000921 return LookupResult::CreateLookupResult(Context, *I);
Douglas Gregorf9201e02009-02-11 23:02:49 +0000922 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000923 } else {
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000924 // Perform C++ unqualified name lookup.
925 std::pair<bool, LookupResult> MaybeResult =
926 CppLookupName(S, Name, NameKind, RedeclarationOnly);
927 if (MaybeResult.first)
928 return MaybeResult.second;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000929 }
930
931 // If we didn't find a use of this identifier, and if the identifier
932 // corresponds to a compiler builtin, create the decl object for the builtin
933 // now, injecting it into translation unit scope, and return it.
Mike Stump1eb44332009-09-09 15:08:12 +0000934 if (NameKind == LookupOrdinaryName ||
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000935 NameKind == LookupRedeclarationWithLinkage) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000936 IdentifierInfo *II = Name.getAsIdentifierInfo();
Douglas Gregor3e41d602009-02-13 23:20:09 +0000937 if (II && AllowBuiltinCreation) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000938 // If this is a builtin on this (or all) targets, create the decl.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000939 if (unsigned BuiltinID = II->getBuiltinID()) {
940 // In C++, we don't have any predefined library functions like
941 // 'malloc'. Instead, we'll just error.
Mike Stump1eb44332009-09-09 15:08:12 +0000942 if (getLangOptions().CPlusPlus &&
Douglas Gregor3e41d602009-02-13 23:20:09 +0000943 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
944 return LookupResult::CreateLookupResult(Context, 0);
945
Douglas Gregor69d993a2009-01-17 01:13:24 +0000946 return LookupResult::CreateLookupResult(Context,
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000947 LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
Douglas Gregor3e41d602009-02-13 23:20:09 +0000948 S, RedeclarationOnly, Loc));
949 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000950 }
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000951 }
Douglas Gregor69d993a2009-01-17 01:13:24 +0000952 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000953}
954
955/// @brief Perform qualified name lookup into a given context.
956///
957/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
958/// names when the context of those names is explicit specified, e.g.,
959/// "std::vector" or "x->member".
960///
961/// Different lookup criteria can find different names. For example, a
962/// particular scope can have both a struct and a function of the same
963/// name, and each can be found by certain lookup criteria. For more
964/// information about lookup criteria, see the documentation for the
965/// class LookupCriteria.
966///
967/// @param LookupCtx The context in which qualified name lookup will
968/// search. If the lookup criteria permits, name lookup may also search
969/// in the parent contexts or (for C++ classes) base classes.
970///
971/// @param Name The name of the entity that we are searching for.
972///
973/// @param Criteria The criteria that this routine will use to
974/// determine which names are visible and which names will be
975/// found. Note that name lookup will find a name that is visible by
976/// the given criteria, but the entity itself may not be semantically
977/// correct or even the kind of entity expected based on the
978/// lookup. For example, searching for a nested-name-specifier name
979/// might result in an EnumDecl, which is visible but is not permitted
980/// as a nested-name-specifier in C++03.
981///
982/// @returns The result of name lookup, which includes zero or more
983/// declarations and possibly additional information used to diagnose
984/// ambiguities.
985Sema::LookupResult
986Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000987 LookupNameKind NameKind, bool RedeclarationOnly) {
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000988 assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
Mike Stump1eb44332009-09-09 15:08:12 +0000989
990 if (!Name)
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000991 return LookupResult::CreateLookupResult(Context, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000993 // If we're performing qualified name lookup (e.g., lookup into a
994 // struct), find fields as part of ordinary name lookup.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000995 unsigned IDNS
Mike Stump1eb44332009-09-09 15:08:12 +0000996 = getIdentifierNamespacesFromLookupNameKind(NameKind,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000997 getLangOptions().CPlusPlus);
998 if (NameKind == LookupOrdinaryName)
999 IDNS |= Decl::IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001001 // Make sure that the declaration context is complete.
1002 assert((!isa<TagDecl>(LookupCtx) ||
1003 LookupCtx->isDependentContext() ||
1004 cast<TagDecl>(LookupCtx)->isDefinition() ||
1005 Context.getTypeDeclType(cast<TagDecl>(LookupCtx))->getAs<TagType>()
1006 ->isBeingDefined()) &&
1007 "Declaration context must already be complete!");
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001009 // Perform qualified name lookup into the LookupCtx.
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001010 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001011 for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
Douglas Gregor4c921ae2009-01-30 01:04:22 +00001012 if (isAcceptableLookupResult(*I, NameKind, IDNS))
Douglas Gregor69d993a2009-01-17 01:13:24 +00001013 return LookupResult::CreateLookupResult(Context, I, E);
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001014
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001015 // If this isn't a C++ class, we aren't allowed to look into base
Douglas Gregor4719f4e2009-09-11 22:57:37 +00001016 // classes, we're done.
1017 if (RedeclarationOnly || !isa<CXXRecordDecl>(LookupCtx))
Douglas Gregor69d993a2009-01-17 01:13:24 +00001018 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor7176fff2009-01-15 00:26:24 +00001019
1020 // Perform lookup into our base classes.
Douglas Gregora8f32e02009-10-06 17:59:45 +00001021 CXXRecordDecl *LookupRec = cast<CXXRecordDecl>(LookupCtx);
1022 CXXBasePaths Paths;
1023 Paths.setOrigin(LookupRec);
Douglas Gregor7176fff2009-01-15 00:26:24 +00001024
1025 // Look for this member in our base classes
Douglas Gregora8f32e02009-10-06 17:59:45 +00001026 CXXRecordDecl::BaseMatchesCallback *BaseCallback = 0;
1027 switch (NameKind) {
1028 case LookupOrdinaryName:
1029 case LookupMemberName:
1030 case LookupRedeclarationWithLinkage:
1031 BaseCallback = &CXXRecordDecl::FindOrdinaryMember;
1032 break;
1033
1034 case LookupTagName:
1035 BaseCallback = &CXXRecordDecl::FindTagMember;
1036 break;
1037
1038 case LookupOperatorName:
1039 case LookupNamespaceName:
1040 case LookupObjCProtocolName:
1041 case LookupObjCImplementationName:
1042 case LookupObjCCategoryImplName:
1043 // These lookups will never find a member in a C++ class (or base class).
1044 return LookupResult::CreateLookupResult(Context, 0);
1045
1046 case LookupNestedNameSpecifierName:
1047 BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember;
1048 break;
1049 }
1050
1051 if (!LookupRec->lookupInBases(BaseCallback, Name.getAsOpaquePtr(), Paths))
Douglas Gregor69d993a2009-01-17 01:13:24 +00001052 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor7176fff2009-01-15 00:26:24 +00001053
1054 // C++ [class.member.lookup]p2:
1055 // [...] If the resulting set of declarations are not all from
1056 // sub-objects of the same type, or the set has a nonstatic member
1057 // and includes members from distinct sub-objects, there is an
1058 // ambiguity and the program is ill-formed. Otherwise that set is
1059 // the result of the lookup.
1060 // FIXME: support using declarations!
1061 QualType SubobjectType;
Daniel Dunbarf1853192009-01-15 18:32:35 +00001062 int SubobjectNumber = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +00001063 for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
Douglas Gregor7176fff2009-01-15 00:26:24 +00001064 Path != PathEnd; ++Path) {
Douglas Gregora8f32e02009-10-06 17:59:45 +00001065 const CXXBasePathElement &PathElement = Path->back();
Douglas Gregor7176fff2009-01-15 00:26:24 +00001066
1067 // Determine whether we're looking at a distinct sub-object or not.
1068 if (SubobjectType.isNull()) {
1069 // This is the first subobject we've looked at. Record it's type.
1070 SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
1071 SubobjectNumber = PathElement.SubobjectNumber;
Mike Stump1eb44332009-09-09 15:08:12 +00001072 } else if (SubobjectType
Douglas Gregor7176fff2009-01-15 00:26:24 +00001073 != Context.getCanonicalType(PathElement.Base->getType())) {
1074 // We found members of the given name in two subobjects of
1075 // different types. This lookup is ambiguous.
Douglas Gregora8f32e02009-10-06 17:59:45 +00001076 CXXBasePaths *PathsOnHeap = new CXXBasePaths;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001077 PathsOnHeap->swap(Paths);
Douglas Gregor69d993a2009-01-17 01:13:24 +00001078 return LookupResult::CreateLookupResult(Context, PathsOnHeap, true);
Douglas Gregor7176fff2009-01-15 00:26:24 +00001079 } else if (SubobjectNumber != PathElement.SubobjectNumber) {
1080 // We have a different subobject of the same type.
1081
1082 // C++ [class.member.lookup]p5:
1083 // A static member, a nested type or an enumerator defined in
1084 // a base class T can unambiguously be found even if an object
Mike Stump1eb44332009-09-09 15:08:12 +00001085 // has more than one base class subobject of type T.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001086 Decl *FirstDecl = *Path->Decls.first;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001087 if (isa<VarDecl>(FirstDecl) ||
1088 isa<TypeDecl>(FirstDecl) ||
1089 isa<EnumConstantDecl>(FirstDecl))
1090 continue;
1091
1092 if (isa<CXXMethodDecl>(FirstDecl)) {
1093 // Determine whether all of the methods are static.
1094 bool AllMethodsAreStatic = true;
1095 for (DeclContext::lookup_iterator Func = Path->Decls.first;
1096 Func != Path->Decls.second; ++Func) {
1097 if (!isa<CXXMethodDecl>(*Func)) {
1098 assert(isa<TagDecl>(*Func) && "Non-function must be a tag decl");
1099 break;
1100 }
1101
1102 if (!cast<CXXMethodDecl>(*Func)->isStatic()) {
1103 AllMethodsAreStatic = false;
1104 break;
1105 }
1106 }
1107
1108 if (AllMethodsAreStatic)
1109 continue;
1110 }
1111
1112 // We have found a nonstatic member name in multiple, distinct
1113 // subobjects. Name lookup is ambiguous.
Douglas Gregora8f32e02009-10-06 17:59:45 +00001114 CXXBasePaths *PathsOnHeap = new CXXBasePaths;
Douglas Gregor7176fff2009-01-15 00:26:24 +00001115 PathsOnHeap->swap(Paths);
Douglas Gregor69d993a2009-01-17 01:13:24 +00001116 return LookupResult::CreateLookupResult(Context, PathsOnHeap, false);
Douglas Gregor7176fff2009-01-15 00:26:24 +00001117 }
1118 }
1119
1120 // Lookup in a base class succeeded; return these results.
1121
1122 // If we found a function declaration, return an overload set.
Douglas Gregore53060f2009-06-25 22:08:12 +00001123 if ((*Paths.front().Decls.first)->isFunctionOrFunctionTemplate())
Mike Stump1eb44332009-09-09 15:08:12 +00001124 return LookupResult::CreateLookupResult(Context,
Douglas Gregor7176fff2009-01-15 00:26:24 +00001125 Paths.front().Decls.first, Paths.front().Decls.second);
1126
1127 // We found a non-function declaration; return a single declaration.
Douglas Gregor69d993a2009-01-17 01:13:24 +00001128 return LookupResult::CreateLookupResult(Context, *Paths.front().Decls.first);
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001129}
1130
1131/// @brief Performs name lookup for a name that was parsed in the
1132/// source code, and may contain a C++ scope specifier.
1133///
1134/// This routine is a convenience routine meant to be called from
1135/// contexts that receive a name and an optional C++ scope specifier
1136/// (e.g., "N::M::x"). It will then perform either qualified or
1137/// unqualified name lookup (with LookupQualifiedName or LookupName,
1138/// respectively) on the given name and return those results.
1139///
1140/// @param S The scope from which unqualified name lookup will
1141/// begin.
Mike Stump1eb44332009-09-09 15:08:12 +00001142///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001143/// @param SS An optional C++ scope-specifier, e.g., "::N::M".
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001144///
1145/// @param Name The name of the entity that name lookup will
1146/// search for.
1147///
Douglas Gregor3e41d602009-02-13 23:20:09 +00001148/// @param Loc If provided, the source location where we're performing
Mike Stump1eb44332009-09-09 15:08:12 +00001149/// name lookup. At present, this is only used to produce diagnostics when
Douglas Gregor3e41d602009-02-13 23:20:09 +00001150/// C library functions (like "malloc") are implicitly declared.
1151///
Douglas Gregor495c35d2009-08-25 22:51:20 +00001152/// @param EnteringContext Indicates whether we are going to enter the
1153/// context of the scope-specifier SS (if present).
1154///
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001155/// @returns The result of qualified or unqualified name lookup.
1156Sema::LookupResult
Mike Stump1eb44332009-09-09 15:08:12 +00001157Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS,
Douglas Gregor4c921ae2009-01-30 01:04:22 +00001158 DeclarationName Name, LookupNameKind NameKind,
Douglas Gregor3e41d602009-02-13 23:20:09 +00001159 bool RedeclarationOnly, bool AllowBuiltinCreation,
Douglas Gregor495c35d2009-08-25 22:51:20 +00001160 SourceLocation Loc,
1161 bool EnteringContext) {
1162 if (SS && SS->isInvalid()) {
1163 // When the scope specifier is invalid, don't even look for
Douglas Gregor42af25f2009-05-11 19:58:34 +00001164 // anything.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001165 return LookupResult::CreateLookupResult(Context, 0);
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor495c35d2009-08-25 22:51:20 +00001168 if (SS && SS->isSet()) {
1169 if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001170 // We have resolved the scope specifier to a particular declaration
Douglas Gregor495c35d2009-08-25 22:51:20 +00001171 // contex, and will perform name lookup in that context.
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001172 if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS))
Douglas Gregor495c35d2009-08-25 22:51:20 +00001173 return LookupResult::CreateLookupResult(Context, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor495c35d2009-08-25 22:51:20 +00001175 return LookupQualifiedName(DC, Name, NameKind, RedeclarationOnly);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001176 }
Douglas Gregor42af25f2009-05-11 19:58:34 +00001177
Douglas Gregor495c35d2009-08-25 22:51:20 +00001178 // We could not resolve the scope specified to a specific declaration
Mike Stump1eb44332009-09-09 15:08:12 +00001179 // context, which means that SS refers to an unknown specialization.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001180 // Name lookup can't find anything in this case.
1181 return LookupResult::CreateLookupResult(Context, 0);
Douglas Gregor4c921ae2009-01-30 01:04:22 +00001182 }
1183
Mike Stump1eb44332009-09-09 15:08:12 +00001184 // Perform unqualified name lookup starting in the given scope.
1185 return LookupName(S, Name, NameKind, RedeclarationOnly, AllowBuiltinCreation,
Douglas Gregor495c35d2009-08-25 22:51:20 +00001186 Loc);
Douglas Gregoreb11cd02009-01-14 22:20:51 +00001187}
1188
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001189
Douglas Gregor7176fff2009-01-15 00:26:24 +00001190/// @brief Produce a diagnostic describing the ambiguity that resulted
1191/// from name lookup.
1192///
1193/// @param Result The ambiguous name lookup result.
Mike Stump1eb44332009-09-09 15:08:12 +00001194///
Douglas Gregor7176fff2009-01-15 00:26:24 +00001195/// @param Name The name of the entity that name lookup was
1196/// searching for.
1197///
1198/// @param NameLoc The location of the name within the source code.
1199///
1200/// @param LookupRange A source range that provides more
1201/// source-location information concerning the lookup itself. For
1202/// example, this range might highlight a nested-name-specifier that
1203/// precedes the name.
1204///
1205/// @returns true
1206bool Sema::DiagnoseAmbiguousLookup(LookupResult &Result, DeclarationName Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001207 SourceLocation NameLoc,
Douglas Gregor7176fff2009-01-15 00:26:24 +00001208 SourceRange LookupRange) {
1209 assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
1210
Douglas Gregora8f32e02009-10-06 17:59:45 +00001211 if (CXXBasePaths *Paths = Result.getBasePaths()) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001212 if (Result.getKind() == LookupResult::AmbiguousBaseSubobjects) {
1213 QualType SubobjectType = Paths->front().back().Base->getType();
1214 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
1215 << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
1216 << LookupRange;
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001217
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001218 DeclContext::lookup_iterator Found = Paths->front().Decls.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001219 while (isa<CXXMethodDecl>(*Found) &&
Douglas Gregor31a19b62009-04-01 21:51:26 +00001220 cast<CXXMethodDecl>(*Found)->isStatic())
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001221 ++Found;
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001222
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001223 Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
1224
Douglas Gregor31a19b62009-04-01 21:51:26 +00001225 Result.Destroy();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001226 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001227 }
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001228
1229 assert(Result.getKind() == LookupResult::AmbiguousBaseSubobjectTypes &&
1230 "Unhandled form of name lookup ambiguity");
1231
1232 Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
1233 << Name << LookupRange;
1234
1235 std::set<Decl *> DeclsPrinted;
Douglas Gregora8f32e02009-10-06 17:59:45 +00001236 for (CXXBasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001237 Path != PathEnd; ++Path) {
1238 Decl *D = *Path->Decls.first;
1239 if (DeclsPrinted.insert(D).second)
1240 Diag(D->getLocation(), diag::note_ambiguous_member_found);
1241 }
1242
Douglas Gregor31a19b62009-04-01 21:51:26 +00001243 Result.Destroy();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001244 return true;
1245 } else if (Result.getKind() == LookupResult::AmbiguousReference) {
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001246 Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange;
1247
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001248 NamedDecl **DI = reinterpret_cast<NamedDecl **>(Result.First),
Douglas Gregor31a19b62009-04-01 21:51:26 +00001249 **DEnd = reinterpret_cast<NamedDecl **>(Result.Last);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001250
Chris Lattner48458d22009-02-03 21:29:32 +00001251 for (; DI != DEnd; ++DI)
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001252 Diag((*DI)->getLocation(), diag::note_ambiguous_candidate) << *DI;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001253
Douglas Gregor31a19b62009-04-01 21:51:26 +00001254 Result.Destroy();
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001255 return true;
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +00001256 }
1257
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001258 assert(false && "Unhandled form of name lookup ambiguity");
Douglas Gregor69d993a2009-01-17 01:13:24 +00001259
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001260 // We can't reach here.
Douglas Gregor7176fff2009-01-15 00:26:24 +00001261 return true;
1262}
Douglas Gregorfa047642009-02-04 00:32:51 +00001263
Mike Stump1eb44332009-09-09 15:08:12 +00001264static void
1265addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001266 ASTContext &Context,
1267 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001268 Sema::AssociatedClassSet &AssociatedClasses);
1269
1270static void CollectNamespace(Sema::AssociatedNamespaceSet &Namespaces,
1271 DeclContext *Ctx) {
1272 if (Ctx->isFileContext())
1273 Namespaces.insert(Ctx);
1274}
Douglas Gregor69be8d62009-07-08 07:51:57 +00001275
Mike Stump1eb44332009-09-09 15:08:12 +00001276// \brief Add the associated classes and namespaces for argument-dependent
Douglas Gregor69be8d62009-07-08 07:51:57 +00001277// lookup that involves a template argument (C++ [basic.lookup.koenig]p2).
Mike Stump1eb44332009-09-09 15:08:12 +00001278static void
1279addAssociatedClassesAndNamespaces(const TemplateArgument &Arg,
Douglas Gregor69be8d62009-07-08 07:51:57 +00001280 ASTContext &Context,
1281 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001282 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregor69be8d62009-07-08 07:51:57 +00001283 // C++ [basic.lookup.koenig]p2, last bullet:
Mike Stump1eb44332009-09-09 15:08:12 +00001284 // -- [...] ;
Douglas Gregor69be8d62009-07-08 07:51:57 +00001285 switch (Arg.getKind()) {
1286 case TemplateArgument::Null:
1287 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Douglas Gregor69be8d62009-07-08 07:51:57 +00001289 case TemplateArgument::Type:
1290 // [...] the namespaces and classes associated with the types of the
1291 // template arguments provided for template type parameters (excluding
1292 // template template parameters)
1293 addAssociatedClassesAndNamespaces(Arg.getAsType(), Context,
1294 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001295 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001296 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregor69be8d62009-07-08 07:51:57 +00001298 case TemplateArgument::Declaration:
Mike Stump1eb44332009-09-09 15:08:12 +00001299 // [...] the namespaces in which any template template arguments are
1300 // defined; and the classes in which any member templates used as
Douglas Gregor69be8d62009-07-08 07:51:57 +00001301 // template template arguments are defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001302 if (ClassTemplateDecl *ClassTemplate
Douglas Gregor69be8d62009-07-08 07:51:57 +00001303 = dyn_cast<ClassTemplateDecl>(Arg.getAsDecl())) {
1304 DeclContext *Ctx = ClassTemplate->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);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001311 }
1312 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregor69be8d62009-07-08 07:51:57 +00001314 case TemplateArgument::Integral:
1315 case TemplateArgument::Expression:
Mike Stump1eb44332009-09-09 15:08:12 +00001316 // [Note: non-type template arguments do not contribute to the set of
Douglas Gregor69be8d62009-07-08 07:51:57 +00001317 // associated namespaces. ]
1318 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Douglas Gregor69be8d62009-07-08 07:51:57 +00001320 case TemplateArgument::Pack:
1321 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1322 PEnd = Arg.pack_end();
1323 P != PEnd; ++P)
1324 addAssociatedClassesAndNamespaces(*P, Context,
1325 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001326 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001327 break;
1328 }
1329}
1330
Douglas Gregorfa047642009-02-04 00:32:51 +00001331// \brief Add the associated classes and namespaces for
Mike Stump1eb44332009-09-09 15:08:12 +00001332// argument-dependent lookup with an argument of class type
1333// (C++ [basic.lookup.koenig]p2).
1334static void
1335addAssociatedClassesAndNamespaces(CXXRecordDecl *Class,
Douglas Gregorfa047642009-02-04 00:32:51 +00001336 ASTContext &Context,
1337 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001338 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001339 // C++ [basic.lookup.koenig]p2:
1340 // [...]
1341 // -- If T is a class type (including unions), its associated
1342 // classes are: the class itself; the class of which it is a
1343 // member, if any; and its direct and indirect base
1344 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001345 // which its associated classes are defined.
Douglas Gregorfa047642009-02-04 00:32:51 +00001346
1347 // Add the class of which it is a member, if any.
1348 DeclContext *Ctx = Class->getDeclContext();
1349 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1350 AssociatedClasses.insert(EnclosingClass);
Douglas Gregorfa047642009-02-04 00:32:51 +00001351 // Add the associated namespace for this class.
1352 while (Ctx->isRecord())
1353 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001354 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Douglas Gregorfa047642009-02-04 00:32:51 +00001356 // Add the class itself. If we've already seen this class, we don't
1357 // need to visit base classes.
1358 if (!AssociatedClasses.insert(Class))
1359 return;
1360
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // -- If T is a template-id, its associated namespaces and classes are
1362 // the namespace in which the template is defined; for member
Douglas Gregor69be8d62009-07-08 07:51:57 +00001363 // templates, the member template’s class; the namespaces and classes
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // associated with the types of the template arguments provided for
Douglas Gregor69be8d62009-07-08 07:51:57 +00001365 // template type parameters (excluding template template parameters); the
Mike Stump1eb44332009-09-09 15:08:12 +00001366 // namespaces in which any template template arguments are defined; and
1367 // the classes in which any member templates used as template template
1368 // arguments are defined. [Note: non-type template arguments do not
Douglas Gregor69be8d62009-07-08 07:51:57 +00001369 // contribute to the set of associated namespaces. ]
Mike Stump1eb44332009-09-09 15:08:12 +00001370 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor69be8d62009-07-08 07:51:57 +00001371 = dyn_cast<ClassTemplateSpecializationDecl>(Class)) {
1372 DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext();
1373 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1374 AssociatedClasses.insert(EnclosingClass);
1375 // Add the associated namespace for this class.
1376 while (Ctx->isRecord())
1377 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001378 CollectNamespace(AssociatedNamespaces, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregor69be8d62009-07-08 07:51:57 +00001380 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1381 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1382 addAssociatedClassesAndNamespaces(TemplateArgs[I], Context,
1383 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001384 AssociatedClasses);
Douglas Gregor69be8d62009-07-08 07:51:57 +00001385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregorfa047642009-02-04 00:32:51 +00001387 // Add direct and indirect base classes along with their associated
1388 // namespaces.
1389 llvm::SmallVector<CXXRecordDecl *, 32> Bases;
1390 Bases.push_back(Class);
1391 while (!Bases.empty()) {
1392 // Pop this class off the stack.
1393 Class = Bases.back();
1394 Bases.pop_back();
1395
1396 // Visit the base classes.
1397 for (CXXRecordDecl::base_class_iterator Base = Class->bases_begin(),
1398 BaseEnd = Class->bases_end();
1399 Base != BaseEnd; ++Base) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001400 const RecordType *BaseType = Base->getType()->getAs<RecordType>();
Douglas Gregorfa047642009-02-04 00:32:51 +00001401 CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl());
1402 if (AssociatedClasses.insert(BaseDecl)) {
1403 // Find the associated namespace for this base class.
1404 DeclContext *BaseCtx = BaseDecl->getDeclContext();
1405 while (BaseCtx->isRecord())
1406 BaseCtx = BaseCtx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001407 CollectNamespace(AssociatedNamespaces, BaseCtx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001408
1409 // Make sure we visit the bases of this base class.
1410 if (BaseDecl->bases_begin() != BaseDecl->bases_end())
1411 Bases.push_back(BaseDecl);
1412 }
1413 }
1414 }
1415}
1416
1417// \brief Add the associated classes and namespaces for
1418// argument-dependent lookup with an argument of type T
Mike Stump1eb44332009-09-09 15:08:12 +00001419// (C++ [basic.lookup.koenig]p2).
1420static void
1421addAssociatedClassesAndNamespaces(QualType T,
Douglas Gregorfa047642009-02-04 00:32:51 +00001422 ASTContext &Context,
1423 Sema::AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001424 Sema::AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001425 // C++ [basic.lookup.koenig]p2:
1426 //
1427 // For each argument type T in the function call, there is a set
1428 // of zero or more associated namespaces and a set of zero or more
1429 // associated classes to be considered. The sets of namespaces and
1430 // classes is determined entirely by the types of the function
1431 // arguments (and the namespace of any template template
1432 // argument). Typedef names and using-declarations used to specify
1433 // the types do not contribute to this set. The sets of namespaces
1434 // and classes are determined in the following way:
1435 T = Context.getCanonicalType(T).getUnqualifiedType();
1436
1437 // -- If T is a pointer to U or an array of U, its associated
Mike Stump1eb44332009-09-09 15:08:12 +00001438 // namespaces and classes are those associated with U.
Douglas Gregorfa047642009-02-04 00:32:51 +00001439 //
1440 // We handle this by unwrapping pointer and array types immediately,
1441 // to avoid unnecessary recursion.
1442 while (true) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001443 if (const PointerType *Ptr = T->getAs<PointerType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001444 T = Ptr->getPointeeType();
1445 else if (const ArrayType *Ptr = Context.getAsArrayType(T))
1446 T = Ptr->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001447 else
Douglas Gregorfa047642009-02-04 00:32:51 +00001448 break;
1449 }
1450
1451 // -- If T is a fundamental type, its associated sets of
1452 // namespaces and classes are both empty.
John McCall183700f2009-09-21 23:43:11 +00001453 if (T->getAs<BuiltinType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001454 return;
1455
1456 // -- If T is a class type (including unions), its associated
1457 // classes are: the class itself; the class of which it is a
1458 // member, if any; and its direct and indirect base
1459 // classes. Its associated namespaces are the namespaces in
Mike Stump1eb44332009-09-09 15:08:12 +00001460 // which its associated classes are defined.
Ted Kremenek6217b802009-07-29 21:53:49 +00001461 if (const RecordType *ClassType = T->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +00001462 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001463 = dyn_cast<CXXRecordDecl>(ClassType->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001464 addAssociatedClassesAndNamespaces(ClassDecl, Context,
1465 AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001466 AssociatedClasses);
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001467 return;
1468 }
Douglas Gregorfa047642009-02-04 00:32:51 +00001469
1470 // -- If T is an enumeration type, its associated namespace is
1471 // the namespace in which it is defined. If it is class
1472 // member, its associated class is the member’s class; else
Mike Stump1eb44332009-09-09 15:08:12 +00001473 // it has no associated class.
John McCall183700f2009-09-21 23:43:11 +00001474 if (const EnumType *EnumT = T->getAs<EnumType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001475 EnumDecl *Enum = EnumT->getDecl();
1476
1477 DeclContext *Ctx = Enum->getDeclContext();
1478 if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
1479 AssociatedClasses.insert(EnclosingClass);
1480
1481 // Add the associated namespace for this class.
1482 while (Ctx->isRecord())
1483 Ctx = Ctx->getParent();
John McCall6ff07852009-08-07 22:18:02 +00001484 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001485
1486 return;
1487 }
1488
1489 // -- If T is a function type, its associated namespaces and
1490 // classes are those associated with the function parameter
1491 // types and those associated with the return type.
John McCall183700f2009-09-21 23:43:11 +00001492 if (const FunctionType *FnType = T->getAs<FunctionType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001493 // Return type
John McCall183700f2009-09-21 23:43:11 +00001494 addAssociatedClassesAndNamespaces(FnType->getResultType(),
Douglas Gregorfa047642009-02-04 00:32:51 +00001495 Context,
John McCall6ff07852009-08-07 22:18:02 +00001496 AssociatedNamespaces, AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001497
John McCall183700f2009-09-21 23:43:11 +00001498 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType);
Douglas Gregorfa047642009-02-04 00:32:51 +00001499 if (!Proto)
1500 return;
1501
1502 // Argument types
Douglas Gregor72564e72009-02-26 23:50:07 +00001503 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001504 ArgEnd = Proto->arg_type_end();
Douglas Gregorfa047642009-02-04 00:32:51 +00001505 Arg != ArgEnd; ++Arg)
1506 addAssociatedClassesAndNamespaces(*Arg, Context,
John McCall6ff07852009-08-07 22:18:02 +00001507 AssociatedNamespaces, AssociatedClasses);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregorfa047642009-02-04 00:32:51 +00001509 return;
1510 }
1511
1512 // -- If T is a pointer to a member function of a class X, its
1513 // associated namespaces and classes are those associated
1514 // with the function parameter types and return type,
Mike Stump1eb44332009-09-09 15:08:12 +00001515 // together with those associated with X.
Douglas Gregorfa047642009-02-04 00:32:51 +00001516 //
1517 // -- If T is a pointer to a data member of class X, its
1518 // associated namespaces and classes are those associated
1519 // with the member type together with those associated with
Mike Stump1eb44332009-09-09 15:08:12 +00001520 // X.
Ted Kremenek6217b802009-07-29 21:53:49 +00001521 if (const MemberPointerType *MemberPtr = T->getAs<MemberPointerType>()) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001522 // Handle the type that the pointer to member points to.
1523 addAssociatedClassesAndNamespaces(MemberPtr->getPointeeType(),
1524 Context,
John McCall6ff07852009-08-07 22:18:02 +00001525 AssociatedNamespaces,
1526 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001527
1528 // Handle the class type into which this points.
Ted Kremenek6217b802009-07-29 21:53:49 +00001529 if (const RecordType *Class = MemberPtr->getClass()->getAs<RecordType>())
Douglas Gregorfa047642009-02-04 00:32:51 +00001530 addAssociatedClassesAndNamespaces(cast<CXXRecordDecl>(Class->getDecl()),
1531 Context,
John McCall6ff07852009-08-07 22:18:02 +00001532 AssociatedNamespaces,
1533 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001534
1535 return;
1536 }
1537
1538 // FIXME: What about block pointers?
1539 // FIXME: What about Objective-C message sends?
1540}
1541
1542/// \brief Find the associated classes and namespaces for
1543/// argument-dependent lookup for a call with the given set of
1544/// arguments.
1545///
1546/// This routine computes the sets of associated classes and associated
Mike Stump1eb44332009-09-09 15:08:12 +00001547/// namespaces searched by argument-dependent lookup
Douglas Gregorfa047642009-02-04 00:32:51 +00001548/// (C++ [basic.lookup.argdep]) for a given set of arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001549void
Douglas Gregorfa047642009-02-04 00:32:51 +00001550Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs,
1551 AssociatedNamespaceSet &AssociatedNamespaces,
John McCall6ff07852009-08-07 22:18:02 +00001552 AssociatedClassSet &AssociatedClasses) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001553 AssociatedNamespaces.clear();
1554 AssociatedClasses.clear();
1555
1556 // C++ [basic.lookup.koenig]p2:
1557 // For each argument type T in the function call, there is a set
1558 // of zero or more associated namespaces and a set of zero or more
1559 // associated classes to be considered. The sets of namespaces and
1560 // classes is determined entirely by the types of the function
1561 // arguments (and the namespace of any template template
Mike Stump1eb44332009-09-09 15:08:12 +00001562 // argument).
Douglas Gregorfa047642009-02-04 00:32:51 +00001563 for (unsigned ArgIdx = 0; ArgIdx != NumArgs; ++ArgIdx) {
1564 Expr *Arg = Args[ArgIdx];
1565
1566 if (Arg->getType() != Context.OverloadTy) {
1567 addAssociatedClassesAndNamespaces(Arg->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001568 AssociatedNamespaces,
1569 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001570 continue;
1571 }
1572
1573 // [...] In addition, if the argument is the name or address of a
1574 // set of overloaded functions and/or function templates, its
1575 // associated classes and namespaces are the union of those
1576 // associated with each of the members of the set: the namespace
1577 // in which the function or function template is defined and the
1578 // classes and namespaces associated with its (non-dependent)
1579 // parameter types and return type.
1580 DeclRefExpr *DRE = 0;
Douglas Gregordaa439a2009-07-08 10:57:20 +00001581 TemplateIdRefExpr *TIRE = 0;
1582 Arg = Arg->IgnoreParens();
Douglas Gregorfa047642009-02-04 00:32:51 +00001583 if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg)) {
Douglas Gregordaa439a2009-07-08 10:57:20 +00001584 if (unaryOp->getOpcode() == UnaryOperator::AddrOf) {
Douglas Gregorfa047642009-02-04 00:32:51 +00001585 DRE = dyn_cast<DeclRefExpr>(unaryOp->getSubExpr());
Douglas Gregordaa439a2009-07-08 10:57:20 +00001586 TIRE = dyn_cast<TemplateIdRefExpr>(unaryOp->getSubExpr());
1587 }
1588 } else {
Douglas Gregorfa047642009-02-04 00:32:51 +00001589 DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregordaa439a2009-07-08 10:57:20 +00001590 TIRE = dyn_cast<TemplateIdRefExpr>(Arg);
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Douglas Gregordaa439a2009-07-08 10:57:20 +00001593 OverloadedFunctionDecl *Ovl = 0;
1594 if (DRE)
1595 Ovl = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl());
1596 else if (TIRE)
Douglas Gregord99cbe62009-07-29 18:26:50 +00001597 Ovl = TIRE->getTemplateName().getAsOverloadedFunctionDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001598 if (!Ovl)
1599 continue;
1600
1601 for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
1602 FuncEnd = Ovl->function_end();
1603 Func != FuncEnd; ++Func) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001604 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*Func);
1605 if (!FDecl)
1606 FDecl = cast<FunctionTemplateDecl>(*Func)->getTemplatedDecl();
Douglas Gregorfa047642009-02-04 00:32:51 +00001607
1608 // Add the namespace in which this function was defined. Note
1609 // that, if this is a member function, we do *not* consider the
1610 // enclosing namespace of its class.
1611 DeclContext *Ctx = FDecl->getDeclContext();
John McCall6ff07852009-08-07 22:18:02 +00001612 CollectNamespace(AssociatedNamespaces, Ctx);
Douglas Gregorfa047642009-02-04 00:32:51 +00001613
1614 // Add the classes and namespaces associated with the parameter
1615 // types and return type of this function.
1616 addAssociatedClassesAndNamespaces(FDecl->getType(), Context,
John McCall6ff07852009-08-07 22:18:02 +00001617 AssociatedNamespaces,
1618 AssociatedClasses);
Douglas Gregorfa047642009-02-04 00:32:51 +00001619 }
1620 }
1621}
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001622
1623/// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is
1624/// an acceptable non-member overloaded operator for a call whose
1625/// arguments have types T1 (and, if non-empty, T2). This routine
1626/// implements the check in C++ [over.match.oper]p3b2 concerning
1627/// enumeration types.
Mike Stump1eb44332009-09-09 15:08:12 +00001628static bool
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001629IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn,
1630 QualType T1, QualType T2,
1631 ASTContext &Context) {
Douglas Gregorba498172009-03-13 21:01:28 +00001632 if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType()))
1633 return true;
1634
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001635 if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType()))
1636 return true;
1637
John McCall183700f2009-09-21 23:43:11 +00001638 const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001639 if (Proto->getNumArgs() < 1)
1640 return false;
1641
1642 if (T1->isEnumeralType()) {
1643 QualType ArgType = Proto->getArgType(0).getNonReferenceType();
1644 if (Context.getCanonicalType(T1).getUnqualifiedType()
1645 == Context.getCanonicalType(ArgType).getUnqualifiedType())
1646 return true;
1647 }
1648
1649 if (Proto->getNumArgs() < 2)
1650 return false;
1651
1652 if (!T2.isNull() && T2->isEnumeralType()) {
1653 QualType ArgType = Proto->getArgType(1).getNonReferenceType();
1654 if (Context.getCanonicalType(T2).getUnqualifiedType()
1655 == Context.getCanonicalType(ArgType).getUnqualifiedType())
1656 return true;
1657 }
1658
1659 return false;
1660}
1661
Douglas Gregor6e378de2009-04-23 23:18:26 +00001662/// \brief Find the protocol with the given name, if any.
1663ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II) {
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001664 Decl *D = LookupName(TUScope, II, LookupObjCProtocolName).getAsDecl();
Douglas Gregor6e378de2009-04-23 23:18:26 +00001665 return cast_or_null<ObjCProtocolDecl>(D);
1666}
1667
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001668/// \brief Find the Objective-C category implementation with the given
1669/// name, if any.
1670ObjCCategoryImplDecl *Sema::LookupObjCCategoryImpl(IdentifierInfo *II) {
1671 Decl *D = LookupName(TUScope, II, LookupObjCCategoryImplName).getAsDecl();
1672 return cast_or_null<ObjCCategoryImplDecl>(D);
1673}
1674
John McCall67d1a672009-08-06 02:15:43 +00001675// Attempts to find a declaration in the given declaration context
1676// with exactly the given type. Returns null if no such declaration
1677// was found.
1678Decl *Sema::LookupQualifiedNameWithType(DeclContext *DC,
1679 DeclarationName Name,
1680 QualType T) {
1681 LookupResult result =
1682 LookupQualifiedName(DC, Name, LookupOrdinaryName, true);
1683
1684 CanQualType CQT = Context.getCanonicalType(T);
1685
1686 for (LookupResult::iterator ir = result.begin(), ie = result.end();
1687 ir != ie; ++ir)
1688 if (FunctionDecl *CurFD = dyn_cast<FunctionDecl>(*ir))
1689 if (Context.getCanonicalType(CurFD->getType()) == CQT)
1690 return CurFD;
1691
1692 return NULL;
1693}
1694
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001695void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
Mike Stump1eb44332009-09-09 15:08:12 +00001696 QualType T1, QualType T2,
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001697 FunctionSet &Functions) {
1698 // C++ [over.match.oper]p3:
1699 // -- The set of non-member candidates is the result of the
1700 // unqualified lookup of operator@ in the context of the
1701 // expression according to the usual rules for name lookup in
1702 // unqualified function calls (3.4.2) except that all member
1703 // functions are ignored. However, if no operand has a class
1704 // type, only those non-member functions in the lookup set
Eli Friedman33a31382009-08-05 19:21:58 +00001705 // that have a first parameter of type T1 or "reference to
1706 // (possibly cv-qualified) T1", when T1 is an enumeration
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001707 // type, or (if there is a right operand) a second parameter
Eli Friedman33a31382009-08-05 19:21:58 +00001708 // of type T2 or "reference to (possibly cv-qualified) T2",
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001709 // when T2 is an enumeration type, are candidate functions.
1710 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
1711 LookupResult Operators = LookupName(S, OpName, LookupOperatorName);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001713 assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
1714
1715 if (!Operators)
1716 return;
1717
1718 for (LookupResult::iterator Op = Operators.begin(), OpEnd = Operators.end();
1719 Op != OpEnd; ++Op) {
Douglas Gregor364e0212009-06-27 21:05:07 +00001720 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Op)) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001721 if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context))
1722 Functions.insert(FD); // FIXME: canonical FD
Mike Stump1eb44332009-09-09 15:08:12 +00001723 } else if (FunctionTemplateDecl *FunTmpl
Douglas Gregor364e0212009-06-27 21:05:07 +00001724 = dyn_cast<FunctionTemplateDecl>(*Op)) {
1725 // FIXME: friend operators?
Mike Stump1eb44332009-09-09 15:08:12 +00001726 // FIXME: do we need to check IsAcceptableNonMemberOperatorCandidate,
Douglas Gregor364e0212009-06-27 21:05:07 +00001727 // later?
1728 if (!FunTmpl->getDeclContext()->isRecord())
1729 Functions.insert(FunTmpl);
1730 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001731 }
1732}
1733
John McCall6ff07852009-08-07 22:18:02 +00001734static void CollectFunctionDecl(Sema::FunctionSet &Functions,
1735 Decl *D) {
1736 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1737 Functions.insert(Func);
1738 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
1739 Functions.insert(FunTmpl);
1740}
1741
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001742void Sema::ArgumentDependentLookup(DeclarationName Name,
1743 Expr **Args, unsigned NumArgs,
1744 FunctionSet &Functions) {
1745 // Find all of the associated namespaces and classes based on the
1746 // arguments we have.
1747 AssociatedNamespaceSet AssociatedNamespaces;
1748 AssociatedClassSet AssociatedClasses;
Mike Stump1eb44332009-09-09 15:08:12 +00001749 FindAssociatedClassesAndNamespaces(Args, NumArgs,
John McCall6ff07852009-08-07 22:18:02 +00001750 AssociatedNamespaces,
1751 AssociatedClasses);
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001752
1753 // C++ [basic.lookup.argdep]p3:
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001754 // Let X be the lookup set produced by unqualified lookup (3.4.1)
1755 // and let Y be the lookup set produced by argument dependent
1756 // lookup (defined as follows). If X contains [...] then Y is
1757 // empty. Otherwise Y is the set of declarations found in the
1758 // namespaces associated with the argument types as described
1759 // below. The set of declarations found by the lookup of the name
1760 // is the union of X and Y.
1761 //
1762 // Here, we compute Y and add its members to the overloaded
1763 // candidate set.
1764 for (AssociatedNamespaceSet::iterator NS = AssociatedNamespaces.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001765 NSEnd = AssociatedNamespaces.end();
1766 NS != NSEnd; ++NS) {
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001767 // When considering an associated namespace, the lookup is the
1768 // same as the lookup performed when the associated namespace is
1769 // used as a qualifier (3.4.3.2) except that:
1770 //
1771 // -- Any using-directives in the associated namespace are
1772 // ignored.
1773 //
John McCall6ff07852009-08-07 22:18:02 +00001774 // -- Any namespace-scope friend functions declared in
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001775 // associated classes are visible within their respective
1776 // namespaces even if they are not visible during an ordinary
1777 // lookup (11.4).
1778 DeclContext::lookup_iterator I, E;
John McCall3f9a8a62009-08-11 06:59:38 +00001779 for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) {
John McCall6ff07852009-08-07 22:18:02 +00001780 Decl *D = *I;
John McCall02cace72009-08-28 07:59:38 +00001781 // If the only declaration here is an ordinary friend, consider
1782 // it only if it was declared in an associated classes.
1783 if (D->getIdentifierNamespace() == Decl::IDNS_OrdinaryFriend) {
John McCall3f9a8a62009-08-11 06:59:38 +00001784 DeclContext *LexDC = D->getLexicalDeclContext();
1785 if (!AssociatedClasses.count(cast<CXXRecordDecl>(LexDC)))
1786 continue;
1787 }
Mike Stump1eb44332009-09-09 15:08:12 +00001788
John McCall6ff07852009-08-07 22:18:02 +00001789 CollectFunctionDecl(Functions, D);
Douglas Gregor44bc2d52009-06-23 20:14:09 +00001790 }
1791 }
Douglas Gregor3fd95ce2009-03-13 00:33:25 +00001792}