blob: 78f79eac2ba113470f9905189a15e033f0b7e5b3 [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
124 LookupResult(Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
125 Sema::LookupNameKind LookupKind,
126 Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
127 : ResultKind(NotFound),
128 Paths(0),
129 SemaRef(SemaRef),
130 Name(Name),
131 NameLoc(NameLoc),
132 LookupKind(LookupKind),
133 IDNS(0),
134 Redecl(Redecl != Sema::NotForRedeclaration),
135 HideTags(true),
136 Diagnose(Redecl == Sema::NotForRedeclaration)
137 {}
138
139 /// Creates a temporary lookup result, initializing its core data
140 /// using the information from another result. Diagnostics are always
141 /// disabled.
142 LookupResult(TemporaryToken _, const LookupResult &Other)
143 : ResultKind(NotFound),
144 Paths(0),
145 SemaRef(Other.SemaRef),
146 Name(Other.Name),
147 NameLoc(Other.NameLoc),
148 LookupKind(Other.LookupKind),
149 IDNS(Other.IDNS),
150 Redecl(Other.Redecl),
151 HideTags(Other.HideTags),
152 Diagnose(false)
153 {}
154
155 ~LookupResult() {
156 if (Diagnose) diagnose();
157 if (Paths) deletePaths(Paths);
158 }
159
160 /// Gets the name to look up.
161 DeclarationName getLookupName() const {
162 return Name;
163 }
164
165 /// Gets the kind of lookup to perform.
166 Sema::LookupNameKind getLookupKind() const {
167 return LookupKind;
168 }
169
170 /// True if this lookup is just looking for an existing declaration.
171 bool isForRedeclaration() const {
172 return Redecl;
173 }
174
175 /// Sets whether tag declarations should be hidden by non-tag
176 /// declarations during resolution. The default is true.
177 void setHideTags(bool Hide) {
178 HideTags = Hide;
179 }
180
181 /// The identifier namespace of this lookup. This information is
182 /// private to the lookup routines.
183 unsigned getIdentifierNamespace() const {
184 assert(IDNS);
185 return IDNS;
186 }
187
188 void setIdentifierNamespace(unsigned NS) {
189 IDNS = NS;
190 }
191
192 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
234 /// \brief Add a declaration to these results.
235 void addDecl(NamedDecl *D) {
236 Decls.push_back(D);
237 ResultKind = Found;
238 }
239
240 /// \brief Add all the declarations from another set of lookup
241 /// results.
242 void addAllDecls(const LookupResult &Other) {
243 Decls.append(Other.begin(), Other.end());
244 ResultKind = Found;
245 }
246
247 /// \brief Hides a set of declarations.
248 template <class NamedDeclSet> void hideDecls(const NamedDeclSet &Set) {
249 unsigned I = 0, N = Decls.size();
250 while (I < N) {
251 if (Set.count(Decls[I]))
252 Decls[I] = Decls[--N];
253 else
254 I++;
255 }
256 Decls.set_size(N);
257 }
258
John McCall68263142009-11-18 22:49:29 +0000259 /// \brief Resolves the result kind of the lookup, possibly hiding
260 /// decls.
John McCall7d384dd2009-11-18 07:57:50 +0000261 ///
262 /// This should be called in any environment where lookup might
263 /// generate multiple lookup results.
264 void resolveKind();
265
John McCall68263142009-11-18 22:49:29 +0000266 /// \brief Re-resolves the result kind of the lookup after a set of
267 /// removals has been performed.
268 void resolveKindAfterFilter() {
269 if (Decls.empty())
270 ResultKind = NotFound;
271 else {
272 ResultKind = Found;
273 resolveKind();
274 }
275 }
276
John McCallba135432009-11-21 08:51:07 +0000277 template <class DeclClass>
278 DeclClass *getAsSingle() const {
279 if (getResultKind() != Found) return 0;
280 return dyn_cast<DeclClass>(getFoundDecl());
281 }
282
John McCall7d384dd2009-11-18 07:57:50 +0000283 /// \brief Fetch the unique decl found by this lookup. Asserts
284 /// that one was found.
285 ///
286 /// This is intended for users who have examined the result kind
287 /// and are certain that there is only one result.
288 NamedDecl *getFoundDecl() const {
289 assert(getResultKind() == Found
290 && "getFoundDecl called on non-unique result");
291 return Decls[0]->getUnderlyingDecl();
292 }
293
John McCall68263142009-11-18 22:49:29 +0000294 /// Fetches a representative decl. Useful for lazy diagnostics.
295 NamedDecl *getRepresentativeDecl() const {
296 assert(!Decls.empty() && "cannot get representative of empty set");
297 return Decls[0];
298 }
299
John McCall7d384dd2009-11-18 07:57:50 +0000300 /// \brief Asks if the result is a single tag decl.
301 bool isSingleTagDecl() const {
302 return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
303 }
304
305 /// \brief Make these results show that the name was found in
306 /// base classes of different types.
307 ///
308 /// The given paths object is copied and invalidated.
309 void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
310
311 /// \brief Make these results show that the name was found in
312 /// distinct base classes of the same type.
313 ///
314 /// The given paths object is copied and invalidated.
315 void setAmbiguousBaseSubobjects(CXXBasePaths &P);
316
317 /// \brief Make these results show that the name was found in
318 /// different contexts and a tag decl was hidden by an ordinary
319 /// decl in a different context.
320 void setAmbiguousQualifiedTagHiding() {
321 setAmbiguous(AmbiguousTagHiding);
322 }
323
324 /// \brief Clears out any current state.
325 void clear() {
326 ResultKind = NotFound;
327 Decls.clear();
328 if (Paths) deletePaths(Paths);
329 Paths = NULL;
330 }
331
332 /// \brief Clears out any current state and re-initializes for a
333 /// different kind of lookup.
334 void clear(Sema::LookupNameKind Kind) {
335 clear();
336 LookupKind = Kind;
337 }
338
339 void print(llvm::raw_ostream &);
340
341 /// Suppress the diagnostics that would normally fire because of this
342 /// lookup. This happens during (e.g.) redeclaration lookups.
343 void suppressDiagnostics() {
344 Diagnose = false;
345 }
346
347 /// Sets a 'context' source range.
348 void setContextRange(SourceRange SR) {
349 NameContextRange = SR;
350 }
351
352 /// Gets the source range of the context of this name; for C++
353 /// qualified lookups, this is the source range of the scope
354 /// specifier.
355 SourceRange getContextRange() const {
356 return NameContextRange;
357 }
358
359 /// Gets the location of the identifier. This isn't always defined:
360 /// sometimes we're doing lookups on synthesized names.
361 SourceLocation getNameLoc() const {
362 return NameLoc;
363 }
364
John McCall68263142009-11-18 22:49:29 +0000365 /// A class for iterating through a result set and possibly
366 /// filtering out results. The results returned are possibly
367 /// sugared.
368 class Filter {
369 LookupResult &Results;
370 unsigned I;
John McCallf7a1a742009-11-24 19:00:30 +0000371 bool Changed;
John McCall68263142009-11-18 22:49:29 +0000372#ifndef NDEBUG
373 bool CalledDone;
374#endif
375
376 friend class LookupResult;
377 Filter(LookupResult &Results)
John McCallf7a1a742009-11-24 19:00:30 +0000378 : Results(Results), I(0), Changed(false)
John McCall68263142009-11-18 22:49:29 +0000379#ifndef NDEBUG
380 , CalledDone(false)
381#endif
382 {}
383
384 public:
385#ifndef NDEBUG
386 ~Filter() {
387 assert(CalledDone &&
388 "LookupResult::Filter destroyed without done() call");
389 }
390#endif
391
392 bool hasNext() const {
393 return I != Results.Decls.size();
394 }
395
396 NamedDecl *next() {
397 assert(I < Results.Decls.size() && "next() called on empty filter");
398 return Results.Decls[I++];
399 }
400
401 /// Erase the last element returned from this iterator.
402 void erase() {
403 Results.Decls[--I] = Results.Decls.back();
404 Results.Decls.pop_back();
John McCallf7a1a742009-11-24 19:00:30 +0000405 Changed = true;
406 }
407
408 void replace(NamedDecl *D) {
409 Results.Decls[I-1] = D;
410 Changed = true;
John McCall68263142009-11-18 22:49:29 +0000411 }
412
413 void done() {
414#ifndef NDEBUG
415 assert(!CalledDone && "done() called twice");
416 CalledDone = true;
417#endif
418
John McCallf7a1a742009-11-24 19:00:30 +0000419 if (Changed)
John McCall68263142009-11-18 22:49:29 +0000420 Results.resolveKindAfterFilter();
421 }
422 };
423
424 /// Create a filter for this result set.
425 Filter makeFilter() {
426 return Filter(*this);
427 }
428
John McCall7d384dd2009-11-18 07:57:50 +0000429private:
430 void diagnose() {
431 if (isAmbiguous())
432 SemaRef.DiagnoseAmbiguousLookup(*this);
433 }
434
435 void setAmbiguous(AmbiguityKind AK) {
436 ResultKind = Ambiguous;
437 Ambiguity = AK;
438 }
439
440 void addDeclsFromBasePaths(const CXXBasePaths &P);
441
442 // Sanity checks.
443 void sanity() const {
444 assert(ResultKind != NotFound || Decls.size() == 0);
445 assert(ResultKind != Found || Decls.size() == 1);
John McCall7453ed42009-11-22 00:44:51 +0000446 assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
John McCall97d9e212009-11-22 20:57:36 +0000447 (Decls.size() == 1 &&
448 isa<FunctionTemplateDecl>(Decls[0]->getUnderlyingDecl())));
John McCall7453ed42009-11-22 00:44:51 +0000449 assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
450 assert(ResultKind != Ambiguous || Decls.size() > 1 ||
451 (Decls.size() == 1 && Ambiguity == AmbiguousBaseSubobjects));
John McCall7d384dd2009-11-18 07:57:50 +0000452 assert((Paths != NULL) == (ResultKind == Ambiguous &&
453 (Ambiguity == AmbiguousBaseSubobjectTypes ||
454 Ambiguity == AmbiguousBaseSubobjects)));
455 }
456
John McCall7453ed42009-11-22 00:44:51 +0000457 bool sanityCheckUnresolved() const {
458 for (DeclsTy::const_iterator I = Decls.begin(), E = Decls.end();
459 I != E; ++I)
460 if (isa<UnresolvedUsingValueDecl>(*I))
461 return true;
462 return false;
463 }
464
John McCall7d384dd2009-11-18 07:57:50 +0000465 static void deletePaths(CXXBasePaths *);
466
467 // Results.
468 LookupResultKind ResultKind;
469 AmbiguityKind Ambiguity; // ill-defined unless ambiguous
470 DeclsTy Decls;
471 CXXBasePaths *Paths;
472
473 // Parameters.
474 Sema &SemaRef;
475 DeclarationName Name;
476 SourceLocation NameLoc;
477 SourceRange NameContextRange;
478 Sema::LookupNameKind LookupKind;
479 unsigned IDNS; // ill-defined until set by lookup
480 bool Redecl;
481
482 /// \brief True if tag declarations should be hidden if non-tags
483 /// are present
484 bool HideTags;
485
486 bool Diagnose;
487};
488
489}
490
491#endif