Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:00

0001 //===- Lookup.h - Classes for name lookup -----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the LookupResult class, which is integral to
0010 // Sema's name-lookup subsystem.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SEMA_LOOKUP_H
0015 #define LLVM_CLANG_SEMA_LOOKUP_H
0016 
0017 #include "clang/AST/Decl.h"
0018 #include "clang/AST/DeclBase.h"
0019 #include "clang/AST/DeclCXX.h"
0020 #include "clang/AST/DeclarationName.h"
0021 #include "clang/AST/Type.h"
0022 #include "clang/AST/UnresolvedSet.h"
0023 #include "clang/Basic/LLVM.h"
0024 #include "clang/Basic/LangOptions.h"
0025 #include "clang/Basic/SourceLocation.h"
0026 #include "clang/Basic/Specifiers.h"
0027 #include "clang/Sema/Sema.h"
0028 #include "llvm/ADT/MapVector.h"
0029 #include "llvm/ADT/STLExtras.h"
0030 #include "llvm/Support/Casting.h"
0031 #include <cassert>
0032 #include <optional>
0033 #include <utility>
0034 
0035 namespace clang {
0036 
0037 class CXXBasePaths;
0038 
0039 /// Represents the results of name lookup.
0040 ///
0041 /// An instance of the LookupResult class captures the results of a
0042 /// single name lookup, which can return no result (nothing found),
0043 /// a single declaration, a set of overloaded functions, or an
0044 /// ambiguity. Use the getKind() method to determine which of these
0045 /// results occurred for a given lookup.
0046 class LookupResult {
0047 public:
0048   enum LookupResultKind {
0049     /// No entity found met the criteria.
0050     NotFound = 0,
0051 
0052     /// No entity found met the criteria within the current
0053     /// instantiation,, but there were dependent base classes of the
0054     /// current instantiation that could not be searched.
0055     NotFoundInCurrentInstantiation,
0056 
0057     /// Name lookup found a single declaration that met the
0058     /// criteria.  getFoundDecl() will return this declaration.
0059     Found,
0060 
0061     /// Name lookup found a set of overloaded functions that
0062     /// met the criteria.
0063     FoundOverloaded,
0064 
0065     /// Name lookup found an unresolvable value declaration
0066     /// and cannot yet complete.  This only happens in C++ dependent
0067     /// contexts with dependent using declarations.
0068     FoundUnresolvedValue,
0069 
0070     /// Name lookup results in an ambiguity; use
0071     /// getAmbiguityKind to figure out what kind of ambiguity
0072     /// we have.
0073     Ambiguous
0074   };
0075 
0076   enum AmbiguityKind {
0077     /// Name lookup results in an ambiguity because multiple
0078     /// entities that meet the lookup criteria were found in
0079     /// subobjects of different types. For example:
0080     /// @code
0081     /// struct A { void f(int); }
0082     /// struct B { void f(double); }
0083     /// struct C : A, B { };
0084     /// void test(C c) {
0085     ///   c.f(0); // error: A::f and B::f come from subobjects of different
0086     ///           // types. overload resolution is not performed.
0087     /// }
0088     /// @endcode
0089     AmbiguousBaseSubobjectTypes,
0090 
0091     /// Name lookup results in an ambiguity because multiple
0092     /// nonstatic entities that meet the lookup criteria were found
0093     /// in different subobjects of the same type. For example:
0094     /// @code
0095     /// struct A { int x; };
0096     /// struct B : A { };
0097     /// struct C : A { };
0098     /// struct D : B, C { };
0099     /// int test(D d) {
0100     ///   return d.x; // error: 'x' is found in two A subobjects (of B and C)
0101     /// }
0102     /// @endcode
0103     AmbiguousBaseSubobjects,
0104 
0105     /// Name lookup results in an ambiguity because multiple definitions
0106     /// of entity that meet the lookup criteria were found in different
0107     /// declaration contexts.
0108     /// @code
0109     /// namespace A {
0110     ///   int i;
0111     ///   namespace B { int i; }
0112     ///   int test() {
0113     ///     using namespace B;
0114     ///     return i; // error 'i' is found in namespace A and A::B
0115     ///    }
0116     /// }
0117     /// @endcode
0118     AmbiguousReference,
0119 
0120     /// Name lookup results in an ambiguity because multiple placeholder
0121     /// variables were found in the same scope.
0122     /// @code
0123     /// void f() {
0124     ///    int _ = 0;
0125     ///    int _ = 0;
0126     ///    return _; // ambiguous use of placeholder variable
0127     /// }
0128     /// @endcode
0129     AmbiguousReferenceToPlaceholderVariable,
0130 
0131     /// Name lookup results in an ambiguity because an entity with a
0132     /// tag name was hidden by an entity with an ordinary name from
0133     /// a different context.
0134     /// @code
0135     /// namespace A { struct Foo {}; }
0136     /// namespace B { void Foo(); }
0137     /// namespace C {
0138     ///   using namespace A;
0139     ///   using namespace B;
0140     /// }
0141     /// void test() {
0142     ///   C::Foo(); // error: tag 'A::Foo' is hidden by an object in a
0143     ///             // different namespace
0144     /// }
0145     /// @endcode
0146     AmbiguousTagHiding
0147   };
0148 
0149   /// A little identifier for flagging temporary lookup results.
0150   enum TemporaryToken {
0151     Temporary
0152   };
0153 
0154   using iterator = UnresolvedSetImpl::iterator;
0155 
0156   LookupResult(
0157       Sema &SemaRef, const DeclarationNameInfo &NameInfo,
0158       Sema::LookupNameKind LookupKind,
0159       RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration)
0160       : SemaPtr(&SemaRef), NameInfo(NameInfo), LookupKind(LookupKind),
0161         Redecl(Redecl != RedeclarationKind::NotForRedeclaration),
0162         ExternalRedecl(Redecl == RedeclarationKind::ForExternalRedeclaration),
0163         DiagnoseAccess(Redecl == RedeclarationKind::NotForRedeclaration),
0164         DiagnoseAmbiguous(Redecl == RedeclarationKind::NotForRedeclaration) {
0165     configure();
0166   }
0167 
0168   // TODO: consider whether this constructor should be restricted to take
0169   // as input a const IdentifierInfo* (instead of Name),
0170   // forcing other cases towards the constructor taking a DNInfo.
0171   LookupResult(
0172       Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
0173       Sema::LookupNameKind LookupKind,
0174       RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration)
0175       : SemaPtr(&SemaRef), NameInfo(Name, NameLoc), LookupKind(LookupKind),
0176         Redecl(Redecl != RedeclarationKind::NotForRedeclaration),
0177         ExternalRedecl(Redecl == RedeclarationKind::ForExternalRedeclaration),
0178         DiagnoseAccess(Redecl == RedeclarationKind::NotForRedeclaration),
0179         DiagnoseAmbiguous(Redecl == RedeclarationKind::NotForRedeclaration) {
0180     configure();
0181   }
0182 
0183   /// Creates a temporary lookup result, initializing its core data
0184   /// using the information from another result.  Diagnostics are always
0185   /// disabled.
0186   LookupResult(TemporaryToken _, const LookupResult &Other)
0187       : SemaPtr(Other.SemaPtr), NameInfo(Other.NameInfo),
0188         LookupKind(Other.LookupKind), IDNS(Other.IDNS), Redecl(Other.Redecl),
0189         ExternalRedecl(Other.ExternalRedecl), HideTags(Other.HideTags),
0190         AllowHidden(Other.AllowHidden),
0191         TemplateNameLookup(Other.TemplateNameLookup) {}
0192 
0193   // FIXME: Remove these deleted methods once the default build includes
0194   // -Wdeprecated.
0195   LookupResult(const LookupResult &) = delete;
0196   LookupResult &operator=(const LookupResult &) = delete;
0197 
0198   LookupResult(LookupResult &&Other)
0199       : ResultKind(std::move(Other.ResultKind)),
0200         Ambiguity(std::move(Other.Ambiguity)), Decls(std::move(Other.Decls)),
0201         Paths(std::move(Other.Paths)),
0202         NamingClass(std::move(Other.NamingClass)),
0203         BaseObjectType(std::move(Other.BaseObjectType)),
0204         SemaPtr(std::move(Other.SemaPtr)), NameInfo(std::move(Other.NameInfo)),
0205         NameContextRange(std::move(Other.NameContextRange)),
0206         LookupKind(std::move(Other.LookupKind)), IDNS(std::move(Other.IDNS)),
0207         Redecl(std::move(Other.Redecl)),
0208         ExternalRedecl(std::move(Other.ExternalRedecl)),
0209         HideTags(std::move(Other.HideTags)),
0210         DiagnoseAccess(std::move(Other.DiagnoseAccess)),
0211         DiagnoseAmbiguous(std::move(Other.DiagnoseAmbiguous)),
0212         AllowHidden(std::move(Other.AllowHidden)),
0213         Shadowed(std::move(Other.Shadowed)),
0214         TemplateNameLookup(std::move(Other.TemplateNameLookup)) {
0215     Other.Paths = nullptr;
0216     Other.DiagnoseAccess = false;
0217     Other.DiagnoseAmbiguous = false;
0218   }
0219 
0220   LookupResult &operator=(LookupResult &&Other) {
0221     ResultKind = std::move(Other.ResultKind);
0222     Ambiguity = std::move(Other.Ambiguity);
0223     Decls = std::move(Other.Decls);
0224     Paths = std::move(Other.Paths);
0225     NamingClass = std::move(Other.NamingClass);
0226     BaseObjectType = std::move(Other.BaseObjectType);
0227     SemaPtr = std::move(Other.SemaPtr);
0228     NameInfo = std::move(Other.NameInfo);
0229     NameContextRange = std::move(Other.NameContextRange);
0230     LookupKind = std::move(Other.LookupKind);
0231     IDNS = std::move(Other.IDNS);
0232     Redecl = std::move(Other.Redecl);
0233     ExternalRedecl = std::move(Other.ExternalRedecl);
0234     HideTags = std::move(Other.HideTags);
0235     DiagnoseAccess = std::move(Other.DiagnoseAccess);
0236     DiagnoseAmbiguous = std::move(Other.DiagnoseAmbiguous);
0237     AllowHidden = std::move(Other.AllowHidden);
0238     Shadowed = std::move(Other.Shadowed);
0239     TemplateNameLookup = std::move(Other.TemplateNameLookup);
0240     Other.Paths = nullptr;
0241     Other.DiagnoseAccess = false;
0242     Other.DiagnoseAmbiguous = false;
0243     return *this;
0244   }
0245 
0246   ~LookupResult() {
0247     if (DiagnoseAccess)
0248       diagnoseAccess();
0249     if (DiagnoseAmbiguous)
0250       diagnoseAmbiguous();
0251     if (Paths) deletePaths(Paths);
0252   }
0253 
0254   /// Gets the name info to look up.
0255   const DeclarationNameInfo &getLookupNameInfo() const {
0256     return NameInfo;
0257   }
0258 
0259   /// Sets the name info to look up.
0260   void setLookupNameInfo(const DeclarationNameInfo &NameInfo) {
0261     this->NameInfo = NameInfo;
0262   }
0263 
0264   /// Gets the name to look up.
0265   DeclarationName getLookupName() const {
0266     return NameInfo.getName();
0267   }
0268 
0269   /// Sets the name to look up.
0270   void setLookupName(DeclarationName Name) {
0271     NameInfo.setName(Name);
0272   }
0273 
0274   /// Gets the kind of lookup to perform.
0275   Sema::LookupNameKind getLookupKind() const {
0276     return LookupKind;
0277   }
0278 
0279   /// True if this lookup is just looking for an existing declaration.
0280   bool isForRedeclaration() const {
0281     return Redecl;
0282   }
0283 
0284   /// True if this lookup is just looking for an existing declaration to link
0285   /// against a declaration with external linkage.
0286   bool isForExternalRedeclaration() const {
0287     return ExternalRedecl;
0288   }
0289 
0290   RedeclarationKind redeclarationKind() const {
0291     return ExternalRedecl ? RedeclarationKind::ForExternalRedeclaration
0292            : Redecl       ? RedeclarationKind::ForVisibleRedeclaration
0293                           : RedeclarationKind::NotForRedeclaration;
0294   }
0295 
0296   /// Specify whether hidden declarations are visible, e.g.,
0297   /// for recovery reasons.
0298   void setAllowHidden(bool AH) {
0299     AllowHidden = AH;
0300   }
0301 
0302   /// Determine whether this lookup is permitted to see hidden
0303   /// declarations, such as those in modules that have not yet been imported.
0304   bool isHiddenDeclarationVisible(NamedDecl *ND) const {
0305     return AllowHidden ||
0306            (isForExternalRedeclaration() && ND->isExternallyDeclarable());
0307   }
0308 
0309   /// Sets whether tag declarations should be hidden by non-tag
0310   /// declarations during resolution.  The default is true.
0311   void setHideTags(bool Hide) {
0312     HideTags = Hide;
0313   }
0314 
0315   /// Sets whether this is a template-name lookup. For template-name lookups,
0316   /// injected-class-names are treated as naming a template rather than a
0317   /// template specialization.
0318   void setTemplateNameLookup(bool TemplateName) {
0319     TemplateNameLookup = TemplateName;
0320   }
0321 
0322   bool isTemplateNameLookup() const { return TemplateNameLookup; }
0323 
0324   bool isAmbiguous() const {
0325     return getResultKind() == Ambiguous;
0326   }
0327 
0328   /// Determines if this names a single result which is not an
0329   /// unresolved value using decl.  If so, it is safe to call
0330   /// getFoundDecl().
0331   bool isSingleResult() const {
0332     return getResultKind() == Found;
0333   }
0334 
0335   /// Determines if the results are overloaded.
0336   bool isOverloadedResult() const {
0337     return getResultKind() == FoundOverloaded;
0338   }
0339 
0340   bool isUnresolvableResult() const {
0341     return getResultKind() == FoundUnresolvedValue;
0342   }
0343 
0344   LookupResultKind getResultKind() const {
0345     assert(checkDebugAssumptions());
0346     return ResultKind;
0347   }
0348 
0349   AmbiguityKind getAmbiguityKind() const {
0350     assert(isAmbiguous());
0351     return Ambiguity;
0352   }
0353 
0354   const UnresolvedSetImpl &asUnresolvedSet() const {
0355     return Decls;
0356   }
0357 
0358   iterator begin() const { return iterator(Decls.begin()); }
0359   iterator end() const { return iterator(Decls.end()); }
0360 
0361   /// Return true if no decls were found
0362   bool empty() const { return Decls.empty(); }
0363 
0364   /// Return the base paths structure that's associated with
0365   /// these results, or null if none is.
0366   CXXBasePaths *getBasePaths() const {
0367     return Paths;
0368   }
0369 
0370   /// Determine whether the given declaration is visible to the
0371   /// program.
0372   static bool isVisible(Sema &SemaRef, NamedDecl *D);
0373 
0374   static bool isReachable(Sema &SemaRef, NamedDecl *D);
0375 
0376   static bool isAcceptable(Sema &SemaRef, NamedDecl *D,
0377                            Sema::AcceptableKind Kind) {
0378     return Kind == Sema::AcceptableKind::Visible ? isVisible(SemaRef, D)
0379                                                  : isReachable(SemaRef, D);
0380   }
0381 
0382   /// Determine whether this lookup is permitted to see the declaration.
0383   /// Note that a reachable but not visible declaration inhabiting a namespace
0384   /// is not allowed to be seen during name lookup.
0385   ///
0386   /// For example:
0387   /// ```
0388   /// // m.cppm
0389   /// export module m;
0390   /// struct reachable { int v; }
0391   /// export auto func() { return reachable{43}; }
0392   /// // Use.cpp
0393   /// import m;
0394   /// auto Use() {
0395   ///   // Not valid. We couldn't see reachable here.
0396   ///   // So isAvailableForLookup would return false when we look
0397   ///   up 'reachable' here.
0398   ///   // return reachable(43).v;
0399   ///   // Valid. The field name 'v' is allowed during name lookup.
0400   ///   // So isAvailableForLookup would return true when we look up 'v' here.
0401   ///   return func().v;
0402   /// }
0403   /// ```
0404   static bool isAvailableForLookup(Sema &SemaRef, NamedDecl *ND);
0405 
0406   /// Retrieve the accepted (re)declaration of the given declaration,
0407   /// if there is one.
0408   NamedDecl *getAcceptableDecl(NamedDecl *D) const {
0409     if (!D->isInIdentifierNamespace(IDNS))
0410       return nullptr;
0411 
0412     if (isAvailableForLookup(getSema(), D) || isHiddenDeclarationVisible(D))
0413       return D;
0414 
0415     return getAcceptableDeclSlow(D);
0416   }
0417 
0418 private:
0419   static bool isAcceptableSlow(Sema &SemaRef, NamedDecl *D,
0420                                Sema::AcceptableKind Kind);
0421   static bool isReachableSlow(Sema &SemaRef, NamedDecl *D);
0422   NamedDecl *getAcceptableDeclSlow(NamedDecl *D) const;
0423 
0424 public:
0425   /// Returns the identifier namespace mask for this lookup.
0426   unsigned getIdentifierNamespace() const {
0427     return IDNS;
0428   }
0429 
0430   /// Returns whether these results arose from performing a
0431   /// lookup into a class.
0432   bool isClassLookup() const {
0433     return NamingClass != nullptr;
0434   }
0435 
0436   /// Returns the 'naming class' for this lookup, i.e. the
0437   /// class which was looked into to find these results.
0438   ///
0439   /// C++0x [class.access.base]p5:
0440   ///   The access to a member is affected by the class in which the
0441   ///   member is named. This naming class is the class in which the
0442   ///   member name was looked up and found. [Note: this class can be
0443   ///   explicit, e.g., when a qualified-id is used, or implicit,
0444   ///   e.g., when a class member access operator (5.2.5) is used
0445   ///   (including cases where an implicit "this->" is added). If both
0446   ///   a class member access operator and a qualified-id are used to
0447   ///   name the member (as in p->T::m), the class naming the member
0448   ///   is the class named by the nested-name-specifier of the
0449   ///   qualified-id (that is, T). -- end note ]
0450   ///
0451   /// This is set by the lookup routines when they find results in a class.
0452   CXXRecordDecl *getNamingClass() const {
0453     return NamingClass;
0454   }
0455 
0456   /// Sets the 'naming class' for this lookup.
0457   void setNamingClass(CXXRecordDecl *Record) {
0458     NamingClass = Record;
0459   }
0460 
0461   /// Returns the base object type associated with this lookup;
0462   /// important for [class.protected].  Most lookups do not have an
0463   /// associated base object.
0464   QualType getBaseObjectType() const {
0465     return BaseObjectType;
0466   }
0467 
0468   /// Sets the base object type for this lookup.
0469   void setBaseObjectType(QualType T) {
0470     BaseObjectType = T;
0471   }
0472 
0473   /// Add a declaration to these results with its natural access.
0474   /// Does not test the acceptance criteria.
0475   void addDecl(NamedDecl *D) {
0476     addDecl(D, D->getAccess());
0477   }
0478 
0479   /// Add a declaration to these results with the given access.
0480   /// Does not test the acceptance criteria.
0481   void addDecl(NamedDecl *D, AccessSpecifier AS) {
0482     Decls.addDecl(D, AS);
0483     ResultKind = Found;
0484   }
0485 
0486   /// Add all the declarations from another set of lookup
0487   /// results.
0488   void addAllDecls(const LookupResult &Other) {
0489     Decls.append(Other.Decls.begin(), Other.Decls.end());
0490     ResultKind = Found;
0491   }
0492 
0493   /// Determine whether no result was found because we could not
0494   /// search into dependent base classes of the current instantiation.
0495   bool wasNotFoundInCurrentInstantiation() const {
0496     return ResultKind == NotFoundInCurrentInstantiation;
0497   }
0498 
0499   /// Note that while no result was found in the current instantiation,
0500   /// there were dependent base classes that could not be searched.
0501   void setNotFoundInCurrentInstantiation() {
0502     assert((ResultKind == NotFound ||
0503             ResultKind == NotFoundInCurrentInstantiation) &&
0504            Decls.empty());
0505     ResultKind = NotFoundInCurrentInstantiation;
0506   }
0507 
0508   /// Determine whether the lookup result was shadowed by some other
0509   /// declaration that lookup ignored.
0510   bool isShadowed() const { return Shadowed; }
0511 
0512   /// Note that we found and ignored a declaration while performing
0513   /// lookup.
0514   void setShadowed() { Shadowed = true; }
0515 
0516   /// Resolves the result kind of the lookup, possibly hiding
0517   /// decls.
0518   ///
0519   /// This should be called in any environment where lookup might
0520   /// generate multiple lookup results.
0521   void resolveKind();
0522 
0523   /// Re-resolves the result kind of the lookup after a set of
0524   /// removals has been performed.
0525   void resolveKindAfterFilter() {
0526     if (Decls.empty()) {
0527       if (ResultKind != NotFoundInCurrentInstantiation)
0528         ResultKind = NotFound;
0529 
0530       if (Paths) {
0531         deletePaths(Paths);
0532         Paths = nullptr;
0533       }
0534     } else {
0535       std::optional<AmbiguityKind> SavedAK;
0536       bool WasAmbiguous = false;
0537       if (ResultKind == Ambiguous) {
0538         SavedAK = Ambiguity;
0539         WasAmbiguous = true;
0540       }
0541       ResultKind = Found;
0542       resolveKind();
0543 
0544       // If we didn't make the lookup unambiguous, restore the old
0545       // ambiguity kind.
0546       if (ResultKind == Ambiguous) {
0547         (void)WasAmbiguous;
0548         assert(WasAmbiguous);
0549         Ambiguity = *SavedAK;
0550       } else if (Paths) {
0551         deletePaths(Paths);
0552         Paths = nullptr;
0553       }
0554     }
0555   }
0556 
0557   template <class DeclClass>
0558   DeclClass *getAsSingle() const {
0559     if (getResultKind() != Found) return nullptr;
0560     return dyn_cast<DeclClass>(getFoundDecl());
0561   }
0562 
0563   /// Fetch the unique decl found by this lookup.  Asserts
0564   /// that one was found.
0565   ///
0566   /// This is intended for users who have examined the result kind
0567   /// and are certain that there is only one result.
0568   NamedDecl *getFoundDecl() const {
0569     assert(getResultKind() == Found
0570            && "getFoundDecl called on non-unique result");
0571     return (*begin())->getUnderlyingDecl();
0572   }
0573 
0574   /// Fetches a representative decl.  Useful for lazy diagnostics.
0575   NamedDecl *getRepresentativeDecl() const {
0576     assert(!Decls.empty() && "cannot get representative of empty set");
0577     return *begin();
0578   }
0579 
0580   /// Asks if the result is a single tag decl.
0581   bool isSingleTagDecl() const {
0582     return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
0583   }
0584 
0585   /// Make these results show that the name was found in
0586   /// base classes of different types.
0587   ///
0588   /// The given paths object is copied and invalidated.
0589   void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
0590 
0591   /// Make these results show that the name was found in
0592   /// distinct base classes of the same type.
0593   ///
0594   /// The given paths object is copied and invalidated.
0595   void setAmbiguousBaseSubobjects(CXXBasePaths &P);
0596 
0597   /// Make these results show that the name was found in
0598   /// different contexts and a tag decl was hidden by an ordinary
0599   /// decl in a different context.
0600   void setAmbiguousQualifiedTagHiding() {
0601     setAmbiguous(AmbiguousTagHiding);
0602   }
0603 
0604   /// Clears out any current state.
0605   LLVM_ATTRIBUTE_REINITIALIZES void clear() {
0606     ResultKind = NotFound;
0607     Decls.clear();
0608     if (Paths) deletePaths(Paths);
0609     Paths = nullptr;
0610     NamingClass = nullptr;
0611     Shadowed = false;
0612   }
0613 
0614   /// Clears out any current state and re-initializes for a
0615   /// different kind of lookup.
0616   void clear(Sema::LookupNameKind Kind) {
0617     clear();
0618     LookupKind = Kind;
0619     configure();
0620   }
0621 
0622   /// Change this lookup's redeclaration kind.
0623   void setRedeclarationKind(RedeclarationKind RK) {
0624     Redecl = (RK != RedeclarationKind::NotForRedeclaration);
0625     ExternalRedecl = (RK == RedeclarationKind::ForExternalRedeclaration);
0626     configure();
0627   }
0628 
0629   void dump();
0630   void print(raw_ostream &);
0631 
0632   /// Suppress the diagnostics that would normally fire because of this
0633   /// lookup.  This happens during (e.g.) redeclaration lookups.
0634   void suppressDiagnostics() {
0635     DiagnoseAccess = false;
0636     DiagnoseAmbiguous = false;
0637   }
0638 
0639   /// Suppress the diagnostics that would normally fire because of this
0640   /// lookup due to access control violations.
0641   void suppressAccessDiagnostics() { DiagnoseAccess = false; }
0642 
0643   /// Determines whether this lookup is suppressing access control diagnostics.
0644   bool isSuppressingAccessDiagnostics() const { return !DiagnoseAccess; }
0645 
0646   /// Determines whether this lookup is suppressing ambiguous lookup
0647   /// diagnostics.
0648   bool isSuppressingAmbiguousDiagnostics() const { return !DiagnoseAmbiguous; }
0649 
0650   /// Sets a 'context' source range.
0651   void setContextRange(SourceRange SR) {
0652     NameContextRange = SR;
0653   }
0654 
0655   /// Gets the source range of the context of this name; for C++
0656   /// qualified lookups, this is the source range of the scope
0657   /// specifier.
0658   SourceRange getContextRange() const {
0659     return NameContextRange;
0660   }
0661 
0662   /// Gets the location of the identifier.  This isn't always defined:
0663   /// sometimes we're doing lookups on synthesized names.
0664   SourceLocation getNameLoc() const {
0665     return NameInfo.getLoc();
0666   }
0667 
0668   /// Get the Sema object that this lookup result is searching
0669   /// with.
0670   Sema &getSema() const { return *SemaPtr; }
0671 
0672   /// A class for iterating through a result set and possibly
0673   /// filtering out results.  The results returned are possibly
0674   /// sugared.
0675   class Filter {
0676     friend class LookupResult;
0677 
0678     LookupResult &Results;
0679     LookupResult::iterator I;
0680     bool Changed = false;
0681     bool CalledDone = false;
0682 
0683     Filter(LookupResult &Results) : Results(Results), I(Results.begin()) {}
0684 
0685   public:
0686     Filter(Filter &&F)
0687         : Results(F.Results), I(F.I), Changed(F.Changed),
0688           CalledDone(F.CalledDone) {
0689       F.CalledDone = true;
0690     }
0691 
0692     // The move assignment operator is defined as deleted pending
0693     // further motivation.
0694     Filter &operator=(Filter &&) = delete;
0695 
0696     // The copy constrcutor and copy assignment operator is defined as deleted
0697     // pending further motivation.
0698     Filter(const Filter &) = delete;
0699     Filter &operator=(const Filter &) = delete;
0700 
0701     ~Filter() {
0702       assert(CalledDone &&
0703              "LookupResult::Filter destroyed without done() call");
0704     }
0705 
0706     bool hasNext() const {
0707       return I != Results.end();
0708     }
0709 
0710     NamedDecl *next() {
0711       assert(I != Results.end() && "next() called on empty filter");
0712       return *I++;
0713     }
0714 
0715     /// Restart the iteration.
0716     void restart() {
0717       I = Results.begin();
0718     }
0719 
0720     /// Erase the last element returned from this iterator.
0721     void erase() {
0722       Results.Decls.erase(--I);
0723       Changed = true;
0724     }
0725 
0726     /// Replaces the current entry with the given one, preserving the
0727     /// access bits.
0728     void replace(NamedDecl *D) {
0729       Results.Decls.replace(I-1, D);
0730       Changed = true;
0731     }
0732 
0733     /// Replaces the current entry with the given one.
0734     void replace(NamedDecl *D, AccessSpecifier AS) {
0735       Results.Decls.replace(I-1, D, AS);
0736       Changed = true;
0737     }
0738 
0739     void done() {
0740       assert(!CalledDone && "done() called twice");
0741       CalledDone = true;
0742 
0743       if (Changed)
0744         Results.resolveKindAfterFilter();
0745     }
0746   };
0747 
0748   /// Create a filter for this result set.
0749   Filter makeFilter() {
0750     return Filter(*this);
0751   }
0752 
0753   void setFindLocalExtern(bool FindLocalExtern) {
0754     if (FindLocalExtern)
0755       IDNS |= Decl::IDNS_LocalExtern;
0756     else
0757       IDNS &= ~Decl::IDNS_LocalExtern;
0758   }
0759 
0760 private:
0761   void diagnoseAccess() {
0762     if (!isAmbiguous() && isClassLookup() &&
0763         getSema().getLangOpts().AccessControl)
0764       getSema().CheckLookupAccess(*this);
0765   }
0766 
0767   void diagnoseAmbiguous() {
0768     if (isAmbiguous())
0769       getSema().DiagnoseAmbiguousLookup(*this);
0770   }
0771 
0772   void setAmbiguous(AmbiguityKind AK) {
0773     ResultKind = Ambiguous;
0774     Ambiguity = AK;
0775   }
0776 
0777   void addDeclsFromBasePaths(const CXXBasePaths &P);
0778   void configure();
0779 
0780   bool checkDebugAssumptions() const;
0781 
0782   bool checkUnresolved() const {
0783     for (iterator I = begin(), E = end(); I != E; ++I)
0784       if (isa<UnresolvedUsingValueDecl>((*I)->getUnderlyingDecl()))
0785         return true;
0786     return false;
0787   }
0788 
0789   static void deletePaths(CXXBasePaths *);
0790 
0791   // Results.
0792   LookupResultKind ResultKind = NotFound;
0793   // ill-defined unless ambiguous. Still need to be initialized it will be
0794   // copied/moved.
0795   AmbiguityKind Ambiguity = {};
0796   UnresolvedSet<8> Decls;
0797   CXXBasePaths *Paths = nullptr;
0798   CXXRecordDecl *NamingClass = nullptr;
0799   QualType BaseObjectType;
0800 
0801   // Parameters.
0802   Sema *SemaPtr;
0803   DeclarationNameInfo NameInfo;
0804   SourceRange NameContextRange;
0805   Sema::LookupNameKind LookupKind;
0806   unsigned IDNS = 0; // set by configure()
0807 
0808   bool Redecl;
0809   bool ExternalRedecl;
0810 
0811   /// True if tag declarations should be hidden if non-tags
0812   ///   are present
0813   bool HideTags = true;
0814 
0815   bool DiagnoseAccess = false;
0816   bool DiagnoseAmbiguous = false;
0817 
0818   /// True if we should allow hidden declarations to be 'visible'.
0819   bool AllowHidden = false;
0820 
0821   /// True if the found declarations were shadowed by some other
0822   /// declaration that we skipped. This only happens when \c LookupKind
0823   /// is \c LookupRedeclarationWithLinkage.
0824   bool Shadowed = false;
0825 
0826   /// True if we're looking up a template-name.
0827   bool TemplateNameLookup = false;
0828 };
0829 
0830 /// Consumes visible declarations found when searching for
0831 /// all visible names within a given scope or context.
0832 ///
0833 /// This abstract class is meant to be subclassed by clients of \c
0834 /// Sema::LookupVisibleDecls(), each of which should override the \c
0835 /// FoundDecl() function to process declarations as they are found.
0836 class VisibleDeclConsumer {
0837 public:
0838   /// Destroys the visible declaration consumer.
0839   virtual ~VisibleDeclConsumer();
0840 
0841   /// Determine whether hidden declarations (from unimported
0842   /// modules) should be given to this consumer. By default, they
0843   /// are not included.
0844   virtual bool includeHiddenDecls() const;
0845 
0846   /// Invoked each time \p Sema::LookupVisibleDecls() finds a
0847   /// declaration visible from the current scope or context.
0848   ///
0849   /// \param ND the declaration found.
0850   ///
0851   /// \param Hiding a declaration that hides the declaration \p ND,
0852   /// or NULL if no such declaration exists.
0853   ///
0854   /// \param Ctx the original context from which the lookup started.
0855   ///
0856   /// \param InBaseClass whether this declaration was found in base
0857   /// class of the context we searched.
0858   virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
0859                          bool InBaseClass) = 0;
0860 
0861   /// Callback to inform the client that Sema entered into a new context
0862   /// to find a visible declaration.
0863   //
0864   /// \param Ctx the context which Sema entered.
0865   virtual void EnteredContext(DeclContext *Ctx) {}
0866 };
0867 
0868 /// A class for storing results from argument-dependent lookup.
0869 class ADLResult {
0870 private:
0871   /// A map from canonical decls to the 'most recent' decl.
0872   llvm::MapVector<NamedDecl*, NamedDecl*> Decls;
0873 
0874   struct select_second {
0875     NamedDecl *operator()(std::pair<NamedDecl*, NamedDecl*> P) const {
0876       return P.second;
0877     }
0878   };
0879 
0880 public:
0881   /// Adds a new ADL candidate to this map.
0882   void insert(NamedDecl *D);
0883 
0884   /// Removes any data associated with a given decl.
0885   void erase(NamedDecl *D) {
0886     Decls.erase(cast<NamedDecl>(D->getCanonicalDecl()));
0887   }
0888 
0889   using iterator =
0890       llvm::mapped_iterator<decltype(Decls)::iterator, select_second>;
0891 
0892   iterator begin() { return iterator(Decls.begin(), select_second()); }
0893   iterator end() { return iterator(Decls.end(), select_second()); }
0894 };
0895 
0896 } // namespace clang
0897 
0898 #endif // LLVM_CLANG_SEMA_LOOKUP_H