1 //===--- Lookup.h - Classes for name lookup ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the LookupResult class, which is integral to
11 // Sema's name-lookup subsystem.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SEMA_LOOKUP_H
16 #define LLVM_CLANG_SEMA_LOOKUP_H
22 /// @brief Represents the results of name lookup.
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.
31 enum LookupResultKind {
32 /// @brief No entity found met the criteria.
35 /// @brief No entity found met the criteria within the current
36 /// instantiation,, but there were dependent base classes of the
37 /// current instantiation that could not be searched.
38 NotFoundInCurrentInstantiation,
40 /// @brief Name lookup found a single declaration that met the
41 /// criteria. getFoundDecl() will return this declaration.
44 /// @brief Name lookup found a set of overloaded functions that
48 /// @brief Name lookup found an unresolvable value declaration
49 /// and cannot yet complete. This only happens in C++ dependent
50 /// contexts with dependent using declarations.
53 /// @brief Name lookup results in an ambiguity; use
54 /// getAmbiguityKind to figure out what kind of ambiguity
60 /// Name lookup results in an ambiguity because multiple
61 /// entities that meet the lookup criteria were found in
62 /// subobjects of different types. For example:
64 /// struct A { void f(int); }
65 /// struct B { void f(double); }
66 /// struct C : A, B { };
68 /// c.f(0); // error: A::f and B::f come from subobjects of different
69 /// // types. overload resolution is not performed.
72 AmbiguousBaseSubobjectTypes,
74 /// Name lookup results in an ambiguity because multiple
75 /// nonstatic entities that meet the lookup criteria were found
76 /// in different subobjects of the same type. For example:
78 /// struct A { int x; };
81 /// struct D : B, C { };
83 /// return d.x; // error: 'x' is found in two A subobjects (of B and C)
86 AmbiguousBaseSubobjects,
88 /// Name lookup results in an ambiguity because multiple definitions
89 /// of entity that meet the lookup criteria were found in different
90 /// declaration contexts.
94 /// namespace B { int i; }
96 /// using namespace B;
97 /// return i; // error 'i' is found in namespace A and A::B
103 /// Name lookup results in an ambiguity because an entity with a
104 /// tag name was hidden by an entity with an ordinary name from
105 /// a different context.
107 /// namespace A { struct Foo {}; }
108 /// namespace B { void Foo(); }
110 /// using namespace A;
111 /// using namespace B;
114 /// C::Foo(); // error: tag 'A::Foo' is hidden by an object in a
115 /// // different namespace
121 /// A little identifier for flagging temporary lookup results.
122 enum TemporaryToken {
126 typedef UnresolvedSetImpl::iterator iterator;
128 LookupResult(Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
129 Sema::LookupNameKind LookupKind,
130 Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
131 : ResultKind(NotFound),
137 LookupKind(LookupKind),
139 Redecl(Redecl != Sema::NotForRedeclaration),
141 Diagnose(Redecl == Sema::NotForRedeclaration)
146 /// Creates a temporary lookup result, initializing its core data
147 /// using the information from another result. Diagnostics are always
149 LookupResult(TemporaryToken _, const LookupResult &Other)
150 : ResultKind(NotFound),
153 SemaRef(Other.SemaRef),
155 NameLoc(Other.NameLoc),
156 LookupKind(Other.LookupKind),
158 Redecl(Other.Redecl),
159 HideTags(Other.HideTags),
164 if (Diagnose) diagnose();
165 if (Paths) deletePaths(Paths);
168 /// Gets the name to look up.
169 DeclarationName getLookupName() const {
173 /// \brief Sets the name to look up.
174 void setLookupName(DeclarationName Name) {
178 /// Gets the kind of lookup to perform.
179 Sema::LookupNameKind getLookupKind() const {
183 /// True if this lookup is just looking for an existing declaration.
184 bool isForRedeclaration() const {
188 /// Sets whether tag declarations should be hidden by non-tag
189 /// declarations during resolution. The default is true.
190 void setHideTags(bool Hide) {
194 bool isAmbiguous() const {
195 return getResultKind() == Ambiguous;
198 /// Determines if this names a single result which is not an
199 /// unresolved value using decl. If so, it is safe to call
201 bool isSingleResult() const {
202 return getResultKind() == Found;
205 /// Determines if the results are overloaded.
206 bool isOverloadedResult() const {
207 return getResultKind() == FoundOverloaded;
210 bool isUnresolvableResult() const {
211 return getResultKind() == FoundUnresolvedValue;
214 LookupResultKind getResultKind() const {
219 AmbiguityKind getAmbiguityKind() const {
220 assert(isAmbiguous());
224 const UnresolvedSetImpl &asUnresolvedSet() const {
228 iterator begin() const { return iterator(Decls.begin()); }
229 iterator end() const { return iterator(Decls.end()); }
231 /// \brief Return true if no decls were found
232 bool empty() const { return Decls.empty(); }
234 /// \brief Return the base paths structure that's associated with
235 /// these results, or null if none is.
236 CXXBasePaths *getBasePaths() const {
240 /// \brief Tests whether the given declaration is acceptable.
241 bool isAcceptableDecl(NamedDecl *D) const {
242 return D->isInIdentifierNamespace(IDNS);
245 /// \brief Returns the identifier namespace mask for this lookup.
246 unsigned getIdentifierNamespace() const {
250 /// \brief Returns whether these results arose from performing a
251 /// lookup into a class.
252 bool isClassLookup() const {
253 return NamingClass != 0;
256 /// \brief Returns the 'naming class' for this lookup, i.e. the
257 /// class which was looked into to find these results.
259 /// C++0x [class.access.base]p5:
260 /// The access to a member is affected by the class in which the
261 /// member is named. This naming class is the class in which the
262 /// member name was looked up and found. [Note: this class can be
263 /// explicit, e.g., when a qualified-id is used, or implicit,
264 /// e.g., when a class member access operator (5.2.5) is used
265 /// (including cases where an implicit "this->" is added). If both
266 /// a class member access operator and a qualified-id are used to
267 /// name the member (as in p->T::m), the class naming the member
268 /// is the class named by the nested-name-specifier of the
269 /// qualified-id (that is, T). -- end note ]
271 /// This is set by the lookup routines when they find results in a class.
272 CXXRecordDecl *getNamingClass() const {
276 /// \brief Sets the 'naming class' for this lookup.
277 void setNamingClass(CXXRecordDecl *Record) {
278 NamingClass = Record;
281 /// \brief Returns the base object type associated with this lookup;
282 /// important for [class.protected]. Most lookups do not have an
283 /// associated base object.
284 QualType getBaseObjectType() const {
285 return BaseObjectType;
288 /// \brief Sets the base object type for this lookup.
289 void setBaseObjectType(QualType T) {
293 /// \brief Add a declaration to these results with its natural access.
294 /// Does not test the acceptance criteria.
295 void addDecl(NamedDecl *D) {
296 addDecl(D, D->getAccess());
299 /// \brief Add a declaration to these results with the given access.
300 /// Does not test the acceptance criteria.
301 void addDecl(NamedDecl *D, AccessSpecifier AS) {
302 Decls.addDecl(D, AS);
306 /// \brief Add all the declarations from another set of lookup
308 void addAllDecls(const LookupResult &Other) {
309 Decls.append(Other.Decls.begin(), Other.Decls.end());
313 /// \brief Determine whether no result was found because we could not
314 /// search into dependent base classes of the current instantiation.
315 bool wasNotFoundInCurrentInstantiation() const {
316 return ResultKind == NotFoundInCurrentInstantiation;
319 /// \brief Note that while no result was found in the current instantiation,
320 /// there were dependent base classes that could not be searched.
321 void setNotFoundInCurrentInstantiation() {
322 assert(ResultKind == NotFound && Decls.empty());
323 ResultKind = NotFoundInCurrentInstantiation;
326 /// \brief Resolves the result kind of the lookup, possibly hiding
329 /// This should be called in any environment where lookup might
330 /// generate multiple lookup results.
333 /// \brief Re-resolves the result kind of the lookup after a set of
334 /// removals has been performed.
335 void resolveKindAfterFilter() {
337 if (ResultKind != NotFoundInCurrentInstantiation)
338 ResultKind = NotFound;
343 if (Paths && (ResultKind != Ambiguous)) {
350 template <class DeclClass>
351 DeclClass *getAsSingle() const {
352 if (getResultKind() != Found) return 0;
353 return dyn_cast<DeclClass>(getFoundDecl());
356 /// \brief Fetch the unique decl found by this lookup. Asserts
357 /// that one was found.
359 /// This is intended for users who have examined the result kind
360 /// and are certain that there is only one result.
361 NamedDecl *getFoundDecl() const {
362 assert(getResultKind() == Found
363 && "getFoundDecl called on non-unique result");
364 return (*begin())->getUnderlyingDecl();
367 /// Fetches a representative decl. Useful for lazy diagnostics.
368 NamedDecl *getRepresentativeDecl() const {
369 assert(!Decls.empty() && "cannot get representative of empty set");
373 /// \brief Asks if the result is a single tag decl.
374 bool isSingleTagDecl() const {
375 return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
378 /// \brief Make these results show that the name was found in
379 /// base classes of different types.
381 /// The given paths object is copied and invalidated.
382 void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
384 /// \brief Make these results show that the name was found in
385 /// distinct base classes of the same type.
387 /// The given paths object is copied and invalidated.
388 void setAmbiguousBaseSubobjects(CXXBasePaths &P);
390 /// \brief Make these results show that the name was found in
391 /// different contexts and a tag decl was hidden by an ordinary
392 /// decl in a different context.
393 void setAmbiguousQualifiedTagHiding() {
394 setAmbiguous(AmbiguousTagHiding);
397 /// \brief Clears out any current state.
399 ResultKind = NotFound;
401 if (Paths) deletePaths(Paths);
405 /// \brief Clears out any current state and re-initializes for a
406 /// different kind of lookup.
407 void clear(Sema::LookupNameKind Kind) {
413 /// \brief Change this lookup's redeclaration kind.
414 void setRedeclarationKind(Sema::RedeclarationKind RK) {
419 void print(llvm::raw_ostream &);
421 /// Suppress the diagnostics that would normally fire because of this
422 /// lookup. This happens during (e.g.) redeclaration lookups.
423 void suppressDiagnostics() {
427 /// Sets a 'context' source range.
428 void setContextRange(SourceRange SR) {
429 NameContextRange = SR;
432 /// Gets the source range of the context of this name; for C++
433 /// qualified lookups, this is the source range of the scope
435 SourceRange getContextRange() const {
436 return NameContextRange;
439 /// Gets the location of the identifier. This isn't always defined:
440 /// sometimes we're doing lookups on synthesized names.
441 SourceLocation getNameLoc() const {
445 /// \brief Get the Sema object that this lookup result is searching
447 Sema &getSema() const { return SemaRef; }
449 /// A class for iterating through a result set and possibly
450 /// filtering out results. The results returned are possibly
453 LookupResult &Results;
454 LookupResult::iterator I;
460 friend class LookupResult;
461 Filter(LookupResult &Results)
462 : Results(Results), I(Results.begin()), Changed(false)
472 "LookupResult::Filter destroyed without done() call");
476 bool hasNext() const {
477 return I != Results.end();
481 assert(I != Results.end() && "next() called on empty filter");
485 /// Erase the last element returned from this iterator.
487 Results.Decls.erase(--I);
491 /// Replaces the current entry with the given one, preserving the
493 void replace(NamedDecl *D) {
494 Results.Decls.replace(I-1, D);
498 /// Replaces the current entry with the given one.
499 void replace(NamedDecl *D, AccessSpecifier AS) {
500 Results.Decls.replace(I-1, D, AS);
506 assert(!CalledDone && "done() called twice");
511 Results.resolveKindAfterFilter();
515 /// Create a filter for this result set.
516 Filter makeFilter() {
517 return Filter(*this);
523 SemaRef.DiagnoseAmbiguousLookup(*this);
524 else if (isClassLookup() && SemaRef.getLangOptions().AccessControl)
525 SemaRef.CheckLookupAccess(*this);
528 void setAmbiguous(AmbiguityKind AK) {
529 ResultKind = Ambiguous;
533 void addDeclsFromBasePaths(const CXXBasePaths &P);
537 void sanity() const {
538 assert(ResultKind != NotFound || Decls.size() == 0);
539 assert(ResultKind != Found || Decls.size() == 1);
540 assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
541 (Decls.size() == 1 &&
542 isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl())));
543 assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
544 assert(ResultKind != Ambiguous || Decls.size() > 1 ||
545 (Decls.size() == 1 && Ambiguity == AmbiguousBaseSubobjects));
546 assert((Paths != NULL) == (ResultKind == Ambiguous &&
547 (Ambiguity == AmbiguousBaseSubobjectTypes ||
548 Ambiguity == AmbiguousBaseSubobjects)));
551 bool sanityCheckUnresolved() const {
552 for (iterator I = begin(), E = end(); I != E; ++I)
553 if (isa<UnresolvedUsingValueDecl>(*I))
558 static void deletePaths(CXXBasePaths *);
561 LookupResultKind ResultKind;
562 AmbiguityKind Ambiguity; // ill-defined unless ambiguous
563 UnresolvedSet<8> Decls;
565 CXXRecordDecl *NamingClass;
566 QualType BaseObjectType;
570 DeclarationName Name;
571 SourceLocation NameLoc;
572 SourceRange NameContextRange;
573 Sema::LookupNameKind LookupKind;
574 unsigned IDNS; // set by configure()
578 /// \brief True if tag declarations should be hidden if non-tags
585 /// \brief Consumes visible declarations found when searching for
586 /// all visible names within a given scope or context.
588 /// This abstract class is meant to be subclassed by clients of \c
589 /// Sema::LookupVisibleDecls(), each of which should override the \c
590 /// FoundDecl() function to process declarations as they are found.
591 class VisibleDeclConsumer {
593 /// \brief Destroys the visible declaration consumer.
594 virtual ~VisibleDeclConsumer();
596 /// \brief Invoked each time \p Sema::LookupVisibleDecls() finds a
597 /// declaration visible from the current scope or context.
599 /// \param ND the declaration found.
601 /// \param Hiding a declaration that hides the declaration \p ND,
602 /// or NULL if no such declaration exists.
604 /// \param InBaseClass whether this declaration was found in base
605 /// class of the context we searched.
606 virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding,
607 bool InBaseClass) = 0;
610 /// \brief A class for storing results from argument-dependent lookup.
613 /// A map from canonical decls to the 'most recent' decl.
614 llvm::DenseMap<NamedDecl*, NamedDecl*> Decls;
617 /// Adds a new ADL candidate to this map.
618 void insert(NamedDecl *D);
620 /// Removes any data associated with a given decl.
621 void erase(NamedDecl *D) {
622 Decls.erase(cast<NamedDecl>(D->getCanonicalDecl()));
626 typedef llvm::DenseMap<NamedDecl*,NamedDecl*>::iterator inner_iterator;
629 friend class ADLResult;
630 iterator(const inner_iterator &iter) : iter(iter) {}
634 iterator &operator++() { ++iter; return *this; }
635 iterator operator++(int) { return iterator(iter++); }
637 NamedDecl *operator*() const { return iter->second; }
639 bool operator==(const iterator &other) const { return iter == other.iter; }
640 bool operator!=(const iterator &other) const { return iter != other.iter; }
643 iterator begin() { return iterator(Decls.begin()); }
644 iterator end() { return iterator(Decls.end()); }