blob: e761346c677cc04dd091edd62c08e8167fd50c0b [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
Douglas Gregor546be3c2009-12-30 17:04:44 +0000171 /// \brief Sets the name to look up.
172 void setLookupName(DeclarationName Name) {
173 this->Name = Name;
174 }
175
John McCall7d384dd2009-11-18 07:57:50 +0000176 /// Gets the kind of lookup to perform.
177 Sema::LookupNameKind getLookupKind() const {
178 return LookupKind;
179 }
180
181 /// True if this lookup is just looking for an existing declaration.
182 bool isForRedeclaration() const {
183 return Redecl;
184 }
185
186 /// Sets whether tag declarations should be hidden by non-tag
187 /// declarations during resolution. The default is true.
188 void setHideTags(bool Hide) {
189 HideTags = Hide;
190 }
191
John McCall7d384dd2009-11-18 07:57:50 +0000192 bool isAmbiguous() const {
193 return getResultKind() == Ambiguous;
194 }
195
John McCall68263142009-11-18 22:49:29 +0000196 /// Determines if this names a single result which is not an
197 /// unresolved value using decl. If so, it is safe to call
198 /// getFoundDecl().
199 bool isSingleResult() const {
200 return getResultKind() == Found;
201 }
202
John McCall7453ed42009-11-22 00:44:51 +0000203 /// Determines if the results are overloaded.
204 bool isOverloadedResult() const {
205 return getResultKind() == FoundOverloaded;
206 }
207
John McCall129e2df2009-11-30 22:42:35 +0000208 bool isUnresolvableResult() const {
209 return getResultKind() == FoundUnresolvedValue;
210 }
211
John McCall7d384dd2009-11-18 07:57:50 +0000212 LookupResultKind getResultKind() const {
213 sanity();
214 return ResultKind;
215 }
216
217 AmbiguityKind getAmbiguityKind() const {
218 assert(isAmbiguous());
219 return Ambiguity;
220 }
221
222 iterator begin() const { return Decls.begin(); }
223 iterator end() const { return Decls.end(); }
224
225 /// \brief Return true if no decls were found
226 bool empty() const { return Decls.empty(); }
227
228 /// \brief Return the base paths structure that's associated with
229 /// these results, or null if none is.
230 CXXBasePaths *getBasePaths() const {
231 return Paths;
232 }
233
John McCall1d7c5282009-12-18 10:40:03 +0000234 /// \brief Tests whether the given declaration is acceptable.
235 bool isAcceptableDecl(NamedDecl *D) const {
236 assert(IsAcceptableFn);
237 return IsAcceptableFn(D, IDNS);
238 }
239
240 /// \brief Returns the identifier namespace mask for this lookup.
241 unsigned getIdentifierNamespace() const {
242 return IDNS;
243 }
244
245 /// \brief Add a declaration to these results. Does not test the
246 /// acceptance criteria.
John McCall7d384dd2009-11-18 07:57:50 +0000247 void addDecl(NamedDecl *D) {
248 Decls.push_back(D);
249 ResultKind = Found;
250 }
251
252 /// \brief Add all the declarations from another set of lookup
253 /// results.
254 void addAllDecls(const LookupResult &Other) {
255 Decls.append(Other.begin(), Other.end());
256 ResultKind = Found;
257 }
258
259 /// \brief Hides a set of declarations.
260 template <class NamedDeclSet> void hideDecls(const NamedDeclSet &Set) {
261 unsigned I = 0, N = Decls.size();
262 while (I < N) {
263 if (Set.count(Decls[I]))
264 Decls[I] = Decls[--N];
265 else
266 I++;
267 }
268 Decls.set_size(N);
269 }
270
John McCall68263142009-11-18 22:49:29 +0000271 /// \brief Resolves the result kind of the lookup, possibly hiding
272 /// decls.
John McCall7d384dd2009-11-18 07:57:50 +0000273 ///
274 /// This should be called in any environment where lookup might
275 /// generate multiple lookup results.
276 void resolveKind();
277
John McCall68263142009-11-18 22:49:29 +0000278 /// \brief Re-resolves the result kind of the lookup after a set of
279 /// removals has been performed.
280 void resolveKindAfterFilter() {
281 if (Decls.empty())
282 ResultKind = NotFound;
283 else {
284 ResultKind = Found;
285 resolveKind();
286 }
287 }
288
John McCallba135432009-11-21 08:51:07 +0000289 template <class DeclClass>
290 DeclClass *getAsSingle() const {
291 if (getResultKind() != Found) return 0;
292 return dyn_cast<DeclClass>(getFoundDecl());
293 }
294
John McCall7d384dd2009-11-18 07:57:50 +0000295 /// \brief Fetch the unique decl found by this lookup. Asserts
296 /// that one was found.
297 ///
298 /// This is intended for users who have examined the result kind
299 /// and are certain that there is only one result.
300 NamedDecl *getFoundDecl() const {
301 assert(getResultKind() == Found
302 && "getFoundDecl called on non-unique result");
303 return Decls[0]->getUnderlyingDecl();
304 }
305
John McCall68263142009-11-18 22:49:29 +0000306 /// Fetches a representative decl. Useful for lazy diagnostics.
307 NamedDecl *getRepresentativeDecl() const {
308 assert(!Decls.empty() && "cannot get representative of empty set");
309 return Decls[0];
310 }
311
John McCall7d384dd2009-11-18 07:57:50 +0000312 /// \brief Asks if the result is a single tag decl.
313 bool isSingleTagDecl() const {
314 return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
315 }
316
317 /// \brief Make these results show that the name was found in
318 /// base classes of different types.
319 ///
320 /// The given paths object is copied and invalidated.
321 void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
322
323 /// \brief Make these results show that the name was found in
324 /// distinct base classes of the same type.
325 ///
326 /// The given paths object is copied and invalidated.
327 void setAmbiguousBaseSubobjects(CXXBasePaths &P);
328
329 /// \brief Make these results show that the name was found in
330 /// different contexts and a tag decl was hidden by an ordinary
331 /// decl in a different context.
332 void setAmbiguousQualifiedTagHiding() {
333 setAmbiguous(AmbiguousTagHiding);
334 }
335
336 /// \brief Clears out any current state.
337 void clear() {
338 ResultKind = NotFound;
339 Decls.clear();
340 if (Paths) deletePaths(Paths);
341 Paths = NULL;
342 }
343
344 /// \brief Clears out any current state and re-initializes for a
345 /// different kind of lookup.
346 void clear(Sema::LookupNameKind Kind) {
347 clear();
348 LookupKind = Kind;
John McCall1d7c5282009-12-18 10:40:03 +0000349 configure();
John McCall7d384dd2009-11-18 07:57:50 +0000350 }
351
352 void print(llvm::raw_ostream &);
353
354 /// Suppress the diagnostics that would normally fire because of this
355 /// lookup. This happens during (e.g.) redeclaration lookups.
356 void suppressDiagnostics() {
357 Diagnose = false;
358 }
359
360 /// Sets a 'context' source range.
361 void setContextRange(SourceRange SR) {
362 NameContextRange = SR;
363 }
364
365 /// Gets the source range of the context of this name; for C++
366 /// qualified lookups, this is the source range of the scope
367 /// specifier.
368 SourceRange getContextRange() const {
369 return NameContextRange;
370 }
371
372 /// Gets the location of the identifier. This isn't always defined:
373 /// sometimes we're doing lookups on synthesized names.
374 SourceLocation getNameLoc() const {
375 return NameLoc;
376 }
377
Douglas Gregor546be3c2009-12-30 17:04:44 +0000378 /// \brief Get the Sema object that this lookup result is searching
379 /// with.
380 Sema &getSema() const { return SemaRef; }
381
John McCall68263142009-11-18 22:49:29 +0000382 /// A class for iterating through a result set and possibly
383 /// filtering out results. The results returned are possibly
384 /// sugared.
385 class Filter {
386 LookupResult &Results;
387 unsigned I;
John McCallf7a1a742009-11-24 19:00:30 +0000388 bool Changed;
John McCall68263142009-11-18 22:49:29 +0000389#ifndef NDEBUG
390 bool CalledDone;
391#endif
392
393 friend class LookupResult;
394 Filter(LookupResult &Results)
John McCallf7a1a742009-11-24 19:00:30 +0000395 : Results(Results), I(0), Changed(false)
John McCall68263142009-11-18 22:49:29 +0000396#ifndef NDEBUG
397 , CalledDone(false)
398#endif
399 {}
400
401 public:
402#ifndef NDEBUG
403 ~Filter() {
404 assert(CalledDone &&
405 "LookupResult::Filter destroyed without done() call");
406 }
407#endif
408
409 bool hasNext() const {
410 return I != Results.Decls.size();
411 }
412
413 NamedDecl *next() {
414 assert(I < Results.Decls.size() && "next() called on empty filter");
415 return Results.Decls[I++];
416 }
417
418 /// Erase the last element returned from this iterator.
419 void erase() {
420 Results.Decls[--I] = Results.Decls.back();
421 Results.Decls.pop_back();
John McCallf7a1a742009-11-24 19:00:30 +0000422 Changed = true;
423 }
424
425 void replace(NamedDecl *D) {
426 Results.Decls[I-1] = D;
427 Changed = true;
John McCall68263142009-11-18 22:49:29 +0000428 }
429
430 void done() {
431#ifndef NDEBUG
432 assert(!CalledDone && "done() called twice");
433 CalledDone = true;
434#endif
435
John McCallf7a1a742009-11-24 19:00:30 +0000436 if (Changed)
John McCall68263142009-11-18 22:49:29 +0000437 Results.resolveKindAfterFilter();
438 }
439 };
440
441 /// Create a filter for this result set.
442 Filter makeFilter() {
443 return Filter(*this);
444 }
445
John McCall7d384dd2009-11-18 07:57:50 +0000446private:
447 void diagnose() {
448 if (isAmbiguous())
449 SemaRef.DiagnoseAmbiguousLookup(*this);
450 }
451
452 void setAmbiguous(AmbiguityKind AK) {
453 ResultKind = Ambiguous;
454 Ambiguity = AK;
455 }
456
457 void addDeclsFromBasePaths(const CXXBasePaths &P);
John McCall1d7c5282009-12-18 10:40:03 +0000458 void configure();
John McCall7d384dd2009-11-18 07:57:50 +0000459
460 // Sanity checks.
461 void sanity() const {
462 assert(ResultKind != NotFound || Decls.size() == 0);
463 assert(ResultKind != Found || Decls.size() == 1);
John McCall7453ed42009-11-22 00:44:51 +0000464 assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
John McCall97d9e212009-11-22 20:57:36 +0000465 (Decls.size() == 1 &&
466 isa<FunctionTemplateDecl>(Decls[0]->getUnderlyingDecl())));
John McCall7453ed42009-11-22 00:44:51 +0000467 assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
468 assert(ResultKind != Ambiguous || Decls.size() > 1 ||
469 (Decls.size() == 1 && Ambiguity == AmbiguousBaseSubobjects));
John McCall7d384dd2009-11-18 07:57:50 +0000470 assert((Paths != NULL) == (ResultKind == Ambiguous &&
471 (Ambiguity == AmbiguousBaseSubobjectTypes ||
472 Ambiguity == AmbiguousBaseSubobjects)));
473 }
474
John McCall7453ed42009-11-22 00:44:51 +0000475 bool sanityCheckUnresolved() const {
476 for (DeclsTy::const_iterator I = Decls.begin(), E = Decls.end();
477 I != E; ++I)
478 if (isa<UnresolvedUsingValueDecl>(*I))
479 return true;
480 return false;
481 }
482
John McCall7d384dd2009-11-18 07:57:50 +0000483 static void deletePaths(CXXBasePaths *);
484
485 // Results.
486 LookupResultKind ResultKind;
487 AmbiguityKind Ambiguity; // ill-defined unless ambiguous
488 DeclsTy Decls;
489 CXXBasePaths *Paths;
490
491 // Parameters.
492 Sema &SemaRef;
493 DeclarationName Name;
494 SourceLocation NameLoc;
495 SourceRange NameContextRange;
496 Sema::LookupNameKind LookupKind;
John McCall1d7c5282009-12-18 10:40:03 +0000497 ResultFilter IsAcceptableFn; // set by configure()
498 unsigned IDNS; // set by configure()
499
John McCall7d384dd2009-11-18 07:57:50 +0000500 bool Redecl;
501
502 /// \brief True if tag declarations should be hidden if non-tags
503 /// are present
504 bool HideTags;
505
506 bool Diagnose;
507};
508
Douglas Gregor546be3c2009-12-30 17:04:44 +0000509 /// \brief Consumes visible declarations found when searching for
510 /// all visible names within a given scope or context.
511 ///
512 /// This abstract class is meant to be subclassed by clients of \c
513 /// Sema::LookupVisibleDecls(), each of which should override the \c
514 /// FoundDecl() function to process declarations as they are found.
515 class VisibleDeclConsumer {
516 public:
517 /// \brief Destroys the visible declaration consumer.
518 virtual ~VisibleDeclConsumer();
519
520 /// \brief Invoked each time \p Sema::LookupVisibleDecls() finds a
521 /// declaration visible from the current scope or context.
522 ///
523 /// \param ND the declaration found.
524 ///
525 /// \param Hiding a declaration that hides the declaration \p ND,
526 /// or NULL if no such declaration exists.
Douglas Gregor0cc84042010-01-14 15:47:35 +0000527 ///
528 /// \param InBaseClass whether this declaration was found in base
529 /// class of the context we searched.
530 virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding,
531 bool InBaseClass) = 0;
Douglas Gregor546be3c2009-12-30 17:04:44 +0000532 };
John McCall7d384dd2009-11-18 07:57:50 +0000533}
534
535#endif