blob: 6b7e28ce3dcc58d711e0747f8fa40768c9e5a061 [file] [log] [blame]
John McCall7d384dd2009-11-18 07:57:50 +00001//===--- Lookup.h - Classes for name lookup ---------------------*- C++ -*-===//
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 defines the LookupResult class, which is integral to
11// Sema's name-lookup subsystem.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SEMA_LOOKUP_H
16#define LLVM_CLANG_SEMA_LOOKUP_H
17
18#include "Sema.h"
19
20namespace clang {
21
22/// @brief Represents the results of name lookup.
23///
24/// An instance of the LookupResult class captures the results of a
25/// single name lookup, which can return no result (nothing found),
26/// a single declaration, a set of overloaded functions, or an
27/// ambiguity. Use the getKind() method to determine which of these
28/// results occurred for a given lookup.
John McCall7d384dd2009-11-18 07:57:50 +000029class LookupResult {
30public:
31 enum LookupResultKind {
32 /// @brief No entity found met the criteria.
33 NotFound = 0,
34
35 /// @brief Name lookup found a single declaration that met the
John McCall51fa86f2009-12-02 08:47:38 +000036 /// criteria. getFoundDecl() will return this declaration.
John McCall7d384dd2009-11-18 07:57:50 +000037 Found,
38
39 /// @brief Name lookup found a set of overloaded functions that
John McCall51fa86f2009-12-02 08:47:38 +000040 /// met the criteria.
John McCall7d384dd2009-11-18 07:57:50 +000041 FoundOverloaded,
42
43 /// @brief Name lookup found an unresolvable value declaration
44 /// and cannot yet complete. This only happens in C++ dependent
45 /// contexts with dependent using declarations.
46 FoundUnresolvedValue,
47
48 /// @brief Name lookup results in an ambiguity; use
49 /// getAmbiguityKind to figure out what kind of ambiguity
50 /// we have.
51 Ambiguous
52 };
53
54 enum AmbiguityKind {
55 /// Name lookup results in an ambiguity because multiple
56 /// entities that meet the lookup criteria were found in
57 /// subobjects of different types. For example:
58 /// @code
59 /// struct A { void f(int); }
60 /// struct B { void f(double); }
61 /// struct C : A, B { };
62 /// void test(C c) {
63 /// c.f(0); // error: A::f and B::f come from subobjects of different
64 /// // types. overload resolution is not performed.
65 /// }
66 /// @endcode
67 AmbiguousBaseSubobjectTypes,
68
69 /// Name lookup results in an ambiguity because multiple
70 /// nonstatic entities that meet the lookup criteria were found
71 /// in different subobjects of the same type. For example:
72 /// @code
73 /// struct A { int x; };
74 /// struct B : A { };
75 /// struct C : A { };
76 /// struct D : B, C { };
77 /// int test(D d) {
78 /// return d.x; // error: 'x' is found in two A subobjects (of B and C)
79 /// }
80 /// @endcode
81 AmbiguousBaseSubobjects,
82
83 /// Name lookup results in an ambiguity because multiple definitions
84 /// of entity that meet the lookup criteria were found in different
85 /// declaration contexts.
86 /// @code
87 /// namespace A {
88 /// int i;
89 /// namespace B { int i; }
90 /// int test() {
91 /// using namespace B;
92 /// return i; // error 'i' is found in namespace A and A::B
93 /// }
94 /// }
95 /// @endcode
96 AmbiguousReference,
97
98 /// Name lookup results in an ambiguity because an entity with a
99 /// tag name was hidden by an entity with an ordinary name from
100 /// a different context.
101 /// @code
102 /// namespace A { struct Foo {}; }
103 /// namespace B { void Foo(); }
104 /// namespace C {
105 /// using namespace A;
106 /// using namespace B;
107 /// }
108 /// void test() {
109 /// C::Foo(); // error: tag 'A::Foo' is hidden by an object in a
110 /// // different namespace
111 /// }
112 /// @endcode
113 AmbiguousTagHiding
114 };
115
116 /// A little identifier for flagging temporary lookup results.
117 enum TemporaryToken {
118 Temporary
119 };
120
121 typedef llvm::SmallVector<NamedDecl*, 4> DeclsTy;
122 typedef DeclsTy::const_iterator iterator;
123
John McCall1d7c5282009-12-18 10:40:03 +0000124 typedef bool (*ResultFilter)(NamedDecl*, unsigned IDNS);
125
John McCall7d384dd2009-11-18 07:57:50 +0000126 LookupResult(Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
127 Sema::LookupNameKind LookupKind,
128 Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
129 : ResultKind(NotFound),
130 Paths(0),
131 SemaRef(SemaRef),
132 Name(Name),
133 NameLoc(NameLoc),
134 LookupKind(LookupKind),
John McCall1d7c5282009-12-18 10:40:03 +0000135 IsAcceptableFn(0),
John McCall7d384dd2009-11-18 07:57:50 +0000136 IDNS(0),
137 Redecl(Redecl != Sema::NotForRedeclaration),
138 HideTags(true),
139 Diagnose(Redecl == Sema::NotForRedeclaration)
John McCall1d7c5282009-12-18 10:40:03 +0000140 {
141 configure();
142 }
John McCall7d384dd2009-11-18 07:57:50 +0000143
144 /// Creates a temporary lookup result, initializing its core data
145 /// using the information from another result. Diagnostics are always
146 /// disabled.
147 LookupResult(TemporaryToken _, const LookupResult &Other)
148 : ResultKind(NotFound),
149 Paths(0),
150 SemaRef(Other.SemaRef),
151 Name(Other.Name),
152 NameLoc(Other.NameLoc),
153 LookupKind(Other.LookupKind),
John McCall1d7c5282009-12-18 10:40:03 +0000154 IsAcceptableFn(Other.IsAcceptableFn),
John McCall7d384dd2009-11-18 07:57:50 +0000155 IDNS(Other.IDNS),
156 Redecl(Other.Redecl),
157 HideTags(Other.HideTags),
158 Diagnose(false)
159 {}
160
161 ~LookupResult() {
162 if (Diagnose) diagnose();
163 if (Paths) deletePaths(Paths);
164 }
165
166 /// Gets the name to look up.
167 DeclarationName getLookupName() const {
168 return Name;
169 }
170
171 /// Gets the kind of lookup to perform.
172 Sema::LookupNameKind getLookupKind() const {
173 return LookupKind;
174 }
175
176 /// True if this lookup is just looking for an existing declaration.
177 bool isForRedeclaration() const {
178 return Redecl;
179 }
180
181 /// Sets whether tag declarations should be hidden by non-tag
182 /// declarations during resolution. The default is true.
183 void setHideTags(bool Hide) {
184 HideTags = Hide;
185 }
186
John McCall7d384dd2009-11-18 07:57:50 +0000187 bool isAmbiguous() const {
188 return getResultKind() == Ambiguous;
189 }
190
John McCall68263142009-11-18 22:49:29 +0000191 /// Determines if this names a single result which is not an
192 /// unresolved value using decl. If so, it is safe to call
193 /// getFoundDecl().
194 bool isSingleResult() const {
195 return getResultKind() == Found;
196 }
197
John McCall7453ed42009-11-22 00:44:51 +0000198 /// Determines if the results are overloaded.
199 bool isOverloadedResult() const {
200 return getResultKind() == FoundOverloaded;
201 }
202
John McCall129e2df2009-11-30 22:42:35 +0000203 bool isUnresolvableResult() const {
204 return getResultKind() == FoundUnresolvedValue;
205 }
206
John McCall7d384dd2009-11-18 07:57:50 +0000207 LookupResultKind getResultKind() const {
208 sanity();
209 return ResultKind;
210 }
211
212 AmbiguityKind getAmbiguityKind() const {
213 assert(isAmbiguous());
214 return Ambiguity;
215 }
216
217 iterator begin() const { return Decls.begin(); }
218 iterator end() const { return Decls.end(); }
219
220 /// \brief Return true if no decls were found
221 bool empty() const { return Decls.empty(); }
222
223 /// \brief Return the base paths structure that's associated with
224 /// these results, or null if none is.
225 CXXBasePaths *getBasePaths() const {
226 return Paths;
227 }
228
John McCall1d7c5282009-12-18 10:40:03 +0000229 /// \brief Tests whether the given declaration is acceptable.
230 bool isAcceptableDecl(NamedDecl *D) const {
231 assert(IsAcceptableFn);
232 return IsAcceptableFn(D, IDNS);
233 }
234
235 /// \brief Returns the identifier namespace mask for this lookup.
236 unsigned getIdentifierNamespace() const {
237 return IDNS;
238 }
239
240 /// \brief Add a declaration to these results. Does not test the
241 /// acceptance criteria.
John McCall7d384dd2009-11-18 07:57:50 +0000242 void addDecl(NamedDecl *D) {
243 Decls.push_back(D);
244 ResultKind = Found;
245 }
246
247 /// \brief Add all the declarations from another set of lookup
248 /// results.
249 void addAllDecls(const LookupResult &Other) {
250 Decls.append(Other.begin(), Other.end());
251 ResultKind = Found;
252 }
253
254 /// \brief Hides a set of declarations.
255 template <class NamedDeclSet> void hideDecls(const NamedDeclSet &Set) {
256 unsigned I = 0, N = Decls.size();
257 while (I < N) {
258 if (Set.count(Decls[I]))
259 Decls[I] = Decls[--N];
260 else
261 I++;
262 }
263 Decls.set_size(N);
264 }
265
John McCall68263142009-11-18 22:49:29 +0000266 /// \brief Resolves the result kind of the lookup, possibly hiding
267 /// decls.
John McCall7d384dd2009-11-18 07:57:50 +0000268 ///
269 /// This should be called in any environment where lookup might
270 /// generate multiple lookup results.
271 void resolveKind();
272
John McCall68263142009-11-18 22:49:29 +0000273 /// \brief Re-resolves the result kind of the lookup after a set of
274 /// removals has been performed.
275 void resolveKindAfterFilter() {
276 if (Decls.empty())
277 ResultKind = NotFound;
278 else {
279 ResultKind = Found;
280 resolveKind();
281 }
282 }
283
John McCallba135432009-11-21 08:51:07 +0000284 template <class DeclClass>
285 DeclClass *getAsSingle() const {
286 if (getResultKind() != Found) return 0;
287 return dyn_cast<DeclClass>(getFoundDecl());
288 }
289
John McCall7d384dd2009-11-18 07:57:50 +0000290 /// \brief Fetch the unique decl found by this lookup. Asserts
291 /// that one was found.
292 ///
293 /// This is intended for users who have examined the result kind
294 /// and are certain that there is only one result.
295 NamedDecl *getFoundDecl() const {
296 assert(getResultKind() == Found
297 && "getFoundDecl called on non-unique result");
298 return Decls[0]->getUnderlyingDecl();
299 }
300
John McCall68263142009-11-18 22:49:29 +0000301 /// Fetches a representative decl. Useful for lazy diagnostics.
302 NamedDecl *getRepresentativeDecl() const {
303 assert(!Decls.empty() && "cannot get representative of empty set");
304 return Decls[0];
305 }
306
John McCall7d384dd2009-11-18 07:57:50 +0000307 /// \brief Asks if the result is a single tag decl.
308 bool isSingleTagDecl() const {
309 return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
310 }
311
312 /// \brief Make these results show that the name was found in
313 /// base classes of different types.
314 ///
315 /// The given paths object is copied and invalidated.
316 void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
317
318 /// \brief Make these results show that the name was found in
319 /// distinct base classes of the same type.
320 ///
321 /// The given paths object is copied and invalidated.
322 void setAmbiguousBaseSubobjects(CXXBasePaths &P);
323
324 /// \brief Make these results show that the name was found in
325 /// different contexts and a tag decl was hidden by an ordinary
326 /// decl in a different context.
327 void setAmbiguousQualifiedTagHiding() {
328 setAmbiguous(AmbiguousTagHiding);
329 }
330
331 /// \brief Clears out any current state.
332 void clear() {
333 ResultKind = NotFound;
334 Decls.clear();
335 if (Paths) deletePaths(Paths);
336 Paths = NULL;
337 }
338
339 /// \brief Clears out any current state and re-initializes for a
340 /// different kind of lookup.
341 void clear(Sema::LookupNameKind Kind) {
342 clear();
343 LookupKind = Kind;
John McCall1d7c5282009-12-18 10:40:03 +0000344 configure();
John McCall7d384dd2009-11-18 07:57:50 +0000345 }
346
347 void print(llvm::raw_ostream &);
348
349 /// Suppress the diagnostics that would normally fire because of this
350 /// lookup. This happens during (e.g.) redeclaration lookups.
351 void suppressDiagnostics() {
352 Diagnose = false;
353 }
354
355 /// Sets a 'context' source range.
356 void setContextRange(SourceRange SR) {
357 NameContextRange = SR;
358 }
359
360 /// Gets the source range of the context of this name; for C++
361 /// qualified lookups, this is the source range of the scope
362 /// specifier.
363 SourceRange getContextRange() const {
364 return NameContextRange;
365 }
366
367 /// Gets the location of the identifier. This isn't always defined:
368 /// sometimes we're doing lookups on synthesized names.
369 SourceLocation getNameLoc() const {
370 return NameLoc;
371 }
372
John McCall68263142009-11-18 22:49:29 +0000373 /// A class for iterating through a result set and possibly
374 /// filtering out results. The results returned are possibly
375 /// sugared.
376 class Filter {
377 LookupResult &Results;
378 unsigned I;
John McCallf7a1a742009-11-24 19:00:30 +0000379 bool Changed;
John McCall68263142009-11-18 22:49:29 +0000380#ifndef NDEBUG
381 bool CalledDone;
382#endif
383
384 friend class LookupResult;
385 Filter(LookupResult &Results)
John McCallf7a1a742009-11-24 19:00:30 +0000386 : Results(Results), I(0), Changed(false)
John McCall68263142009-11-18 22:49:29 +0000387#ifndef NDEBUG
388 , CalledDone(false)
389#endif
390 {}
391
392 public:
393#ifndef NDEBUG
394 ~Filter() {
395 assert(CalledDone &&
396 "LookupResult::Filter destroyed without done() call");
397 }
398#endif
399
400 bool hasNext() const {
401 return I != Results.Decls.size();
402 }
403
404 NamedDecl *next() {
405 assert(I < Results.Decls.size() && "next() called on empty filter");
406 return Results.Decls[I++];
407 }
408
409 /// Erase the last element returned from this iterator.
410 void erase() {
411 Results.Decls[--I] = Results.Decls.back();
412 Results.Decls.pop_back();
John McCallf7a1a742009-11-24 19:00:30 +0000413 Changed = true;
414 }
415
416 void replace(NamedDecl *D) {
417 Results.Decls[I-1] = D;
418 Changed = true;
John McCall68263142009-11-18 22:49:29 +0000419 }
420
421 void done() {
422#ifndef NDEBUG
423 assert(!CalledDone && "done() called twice");
424 CalledDone = true;
425#endif
426
John McCallf7a1a742009-11-24 19:00:30 +0000427 if (Changed)
John McCall68263142009-11-18 22:49:29 +0000428 Results.resolveKindAfterFilter();
429 }
430 };
431
432 /// Create a filter for this result set.
433 Filter makeFilter() {
434 return Filter(*this);
435 }
436
John McCall7d384dd2009-11-18 07:57:50 +0000437private:
438 void diagnose() {
439 if (isAmbiguous())
440 SemaRef.DiagnoseAmbiguousLookup(*this);
441 }
442
443 void setAmbiguous(AmbiguityKind AK) {
444 ResultKind = Ambiguous;
445 Ambiguity = AK;
446 }
447
448 void addDeclsFromBasePaths(const CXXBasePaths &P);
John McCall1d7c5282009-12-18 10:40:03 +0000449 void configure();
John McCall7d384dd2009-11-18 07:57:50 +0000450
451 // Sanity checks.
452 void sanity() const {
453 assert(ResultKind != NotFound || Decls.size() == 0);
454 assert(ResultKind != Found || Decls.size() == 1);
John McCall7453ed42009-11-22 00:44:51 +0000455 assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
John McCall97d9e212009-11-22 20:57:36 +0000456 (Decls.size() == 1 &&
457 isa<FunctionTemplateDecl>(Decls[0]->getUnderlyingDecl())));
John McCall7453ed42009-11-22 00:44:51 +0000458 assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
459 assert(ResultKind != Ambiguous || Decls.size() > 1 ||
460 (Decls.size() == 1 && Ambiguity == AmbiguousBaseSubobjects));
John McCall7d384dd2009-11-18 07:57:50 +0000461 assert((Paths != NULL) == (ResultKind == Ambiguous &&
462 (Ambiguity == AmbiguousBaseSubobjectTypes ||
463 Ambiguity == AmbiguousBaseSubobjects)));
464 }
465
John McCall7453ed42009-11-22 00:44:51 +0000466 bool sanityCheckUnresolved() const {
467 for (DeclsTy::const_iterator I = Decls.begin(), E = Decls.end();
468 I != E; ++I)
469 if (isa<UnresolvedUsingValueDecl>(*I))
470 return true;
471 return false;
472 }
473
John McCall7d384dd2009-11-18 07:57:50 +0000474 static void deletePaths(CXXBasePaths *);
475
476 // Results.
477 LookupResultKind ResultKind;
478 AmbiguityKind Ambiguity; // ill-defined unless ambiguous
479 DeclsTy Decls;
480 CXXBasePaths *Paths;
481
482 // Parameters.
483 Sema &SemaRef;
484 DeclarationName Name;
485 SourceLocation NameLoc;
486 SourceRange NameContextRange;
487 Sema::LookupNameKind LookupKind;
John McCall1d7c5282009-12-18 10:40:03 +0000488 ResultFilter IsAcceptableFn; // set by configure()
489 unsigned IDNS; // set by configure()
490
John McCall7d384dd2009-11-18 07:57:50 +0000491 bool Redecl;
492
493 /// \brief True if tag declarations should be hidden if non-tags
494 /// are present
495 bool HideTags;
496
497 bool Diagnose;
498};
499
500}
501
502#endif