Lookup.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Lookup.h - Classes for name lookup -----------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the LookupResult class, which is integral to
  15. // Sema's name-lookup subsystem.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SEMA_LOOKUP_H
  19. #define LLVM_CLANG_SEMA_LOOKUP_H
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/DeclBase.h"
  22. #include "clang/AST/DeclCXX.h"
  23. #include "clang/AST/DeclarationName.h"
  24. #include "clang/AST/Type.h"
  25. #include "clang/AST/UnresolvedSet.h"
  26. #include "clang/Basic/LLVM.h"
  27. #include "clang/Basic/LangOptions.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "clang/Basic/Specifiers.h"
  30. #include "clang/Sema/Sema.h"
  31. #include "llvm/ADT/MapVector.h"
  32. #include "llvm/ADT/STLExtras.h"
  33. #include "llvm/Support/Casting.h"
  34. #include <cassert>
  35. #include <optional>
  36. #include <utility>
  37. namespace clang {
  38. class CXXBasePaths;
  39. /// Represents the results of name lookup.
  40. ///
  41. /// An instance of the LookupResult class captures the results of a
  42. /// single name lookup, which can return no result (nothing found),
  43. /// a single declaration, a set of overloaded functions, or an
  44. /// ambiguity. Use the getKind() method to determine which of these
  45. /// results occurred for a given lookup.
  46. class LookupResult {
  47. public:
  48. enum LookupResultKind {
  49. /// No entity found met the criteria.
  50. NotFound = 0,
  51. /// No entity found met the criteria within the current
  52. /// instantiation,, but there were dependent base classes of the
  53. /// current instantiation that could not be searched.
  54. NotFoundInCurrentInstantiation,
  55. /// Name lookup found a single declaration that met the
  56. /// criteria. getFoundDecl() will return this declaration.
  57. Found,
  58. /// Name lookup found a set of overloaded functions that
  59. /// met the criteria.
  60. FoundOverloaded,
  61. /// Name lookup found an unresolvable value declaration
  62. /// and cannot yet complete. This only happens in C++ dependent
  63. /// contexts with dependent using declarations.
  64. FoundUnresolvedValue,
  65. /// Name lookup results in an ambiguity; use
  66. /// getAmbiguityKind to figure out what kind of ambiguity
  67. /// we have.
  68. Ambiguous
  69. };
  70. enum AmbiguityKind {
  71. /// Name lookup results in an ambiguity because multiple
  72. /// entities that meet the lookup criteria were found in
  73. /// subobjects of different types. For example:
  74. /// @code
  75. /// struct A { void f(int); }
  76. /// struct B { void f(double); }
  77. /// struct C : A, B { };
  78. /// void test(C c) {
  79. /// c.f(0); // error: A::f and B::f come from subobjects of different
  80. /// // types. overload resolution is not performed.
  81. /// }
  82. /// @endcode
  83. AmbiguousBaseSubobjectTypes,
  84. /// Name lookup results in an ambiguity because multiple
  85. /// nonstatic entities that meet the lookup criteria were found
  86. /// in different subobjects of the same type. For example:
  87. /// @code
  88. /// struct A { int x; };
  89. /// struct B : A { };
  90. /// struct C : A { };
  91. /// struct D : B, C { };
  92. /// int test(D d) {
  93. /// return d.x; // error: 'x' is found in two A subobjects (of B and C)
  94. /// }
  95. /// @endcode
  96. AmbiguousBaseSubobjects,
  97. /// Name lookup results in an ambiguity because multiple definitions
  98. /// of entity that meet the lookup criteria were found in different
  99. /// declaration contexts.
  100. /// @code
  101. /// namespace A {
  102. /// int i;
  103. /// namespace B { int i; }
  104. /// int test() {
  105. /// using namespace B;
  106. /// return i; // error 'i' is found in namespace A and A::B
  107. /// }
  108. /// }
  109. /// @endcode
  110. AmbiguousReference,
  111. /// Name lookup results in an ambiguity because an entity with a
  112. /// tag name was hidden by an entity with an ordinary name from
  113. /// a different context.
  114. /// @code
  115. /// namespace A { struct Foo {}; }
  116. /// namespace B { void Foo(); }
  117. /// namespace C {
  118. /// using namespace A;
  119. /// using namespace B;
  120. /// }
  121. /// void test() {
  122. /// C::Foo(); // error: tag 'A::Foo' is hidden by an object in a
  123. /// // different namespace
  124. /// }
  125. /// @endcode
  126. AmbiguousTagHiding
  127. };
  128. /// A little identifier for flagging temporary lookup results.
  129. enum TemporaryToken {
  130. Temporary
  131. };
  132. using iterator = UnresolvedSetImpl::iterator;
  133. LookupResult(Sema &SemaRef, const DeclarationNameInfo &NameInfo,
  134. Sema::LookupNameKind LookupKind,
  135. Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
  136. : SemaPtr(&SemaRef), NameInfo(NameInfo), LookupKind(LookupKind),
  137. Redecl(Redecl != Sema::NotForRedeclaration),
  138. ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
  139. Diagnose(Redecl == Sema::NotForRedeclaration) {
  140. configure();
  141. }
  142. // TODO: consider whether this constructor should be restricted to take
  143. // as input a const IdentifierInfo* (instead of Name),
  144. // forcing other cases towards the constructor taking a DNInfo.
  145. LookupResult(Sema &SemaRef, DeclarationName Name,
  146. SourceLocation NameLoc, Sema::LookupNameKind LookupKind,
  147. Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
  148. : SemaPtr(&SemaRef), NameInfo(Name, NameLoc), LookupKind(LookupKind),
  149. Redecl(Redecl != Sema::NotForRedeclaration),
  150. ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
  151. Diagnose(Redecl == Sema::NotForRedeclaration) {
  152. configure();
  153. }
  154. /// Creates a temporary lookup result, initializing its core data
  155. /// using the information from another result. Diagnostics are always
  156. /// disabled.
  157. LookupResult(TemporaryToken _, const LookupResult &Other)
  158. : SemaPtr(Other.SemaPtr), NameInfo(Other.NameInfo),
  159. LookupKind(Other.LookupKind), IDNS(Other.IDNS), Redecl(Other.Redecl),
  160. ExternalRedecl(Other.ExternalRedecl), HideTags(Other.HideTags),
  161. AllowHidden(Other.AllowHidden),
  162. TemplateNameLookup(Other.TemplateNameLookup) {}
  163. // FIXME: Remove these deleted methods once the default build includes
  164. // -Wdeprecated.
  165. LookupResult(const LookupResult &) = delete;
  166. LookupResult &operator=(const LookupResult &) = delete;
  167. LookupResult(LookupResult &&Other)
  168. : ResultKind(std::move(Other.ResultKind)),
  169. Ambiguity(std::move(Other.Ambiguity)), Decls(std::move(Other.Decls)),
  170. Paths(std::move(Other.Paths)),
  171. NamingClass(std::move(Other.NamingClass)),
  172. BaseObjectType(std::move(Other.BaseObjectType)),
  173. SemaPtr(std::move(Other.SemaPtr)), NameInfo(std::move(Other.NameInfo)),
  174. NameContextRange(std::move(Other.NameContextRange)),
  175. LookupKind(std::move(Other.LookupKind)), IDNS(std::move(Other.IDNS)),
  176. Redecl(std::move(Other.Redecl)),
  177. ExternalRedecl(std::move(Other.ExternalRedecl)),
  178. HideTags(std::move(Other.HideTags)),
  179. Diagnose(std::move(Other.Diagnose)),
  180. AllowHidden(std::move(Other.AllowHidden)),
  181. Shadowed(std::move(Other.Shadowed)),
  182. TemplateNameLookup(std::move(Other.TemplateNameLookup)) {
  183. Other.Paths = nullptr;
  184. Other.Diagnose = false;
  185. }
  186. LookupResult &operator=(LookupResult &&Other) {
  187. ResultKind = std::move(Other.ResultKind);
  188. Ambiguity = std::move(Other.Ambiguity);
  189. Decls = std::move(Other.Decls);
  190. Paths = std::move(Other.Paths);
  191. NamingClass = std::move(Other.NamingClass);
  192. BaseObjectType = std::move(Other.BaseObjectType);
  193. SemaPtr = std::move(Other.SemaPtr);
  194. NameInfo = std::move(Other.NameInfo);
  195. NameContextRange = std::move(Other.NameContextRange);
  196. LookupKind = std::move(Other.LookupKind);
  197. IDNS = std::move(Other.IDNS);
  198. Redecl = std::move(Other.Redecl);
  199. ExternalRedecl = std::move(Other.ExternalRedecl);
  200. HideTags = std::move(Other.HideTags);
  201. Diagnose = std::move(Other.Diagnose);
  202. AllowHidden = std::move(Other.AllowHidden);
  203. Shadowed = std::move(Other.Shadowed);
  204. TemplateNameLookup = std::move(Other.TemplateNameLookup);
  205. Other.Paths = nullptr;
  206. Other.Diagnose = false;
  207. return *this;
  208. }
  209. ~LookupResult() {
  210. if (Diagnose) diagnose();
  211. if (Paths) deletePaths(Paths);
  212. }
  213. /// Gets the name info to look up.
  214. const DeclarationNameInfo &getLookupNameInfo() const {
  215. return NameInfo;
  216. }
  217. /// Sets the name info to look up.
  218. void setLookupNameInfo(const DeclarationNameInfo &NameInfo) {
  219. this->NameInfo = NameInfo;
  220. }
  221. /// Gets the name to look up.
  222. DeclarationName getLookupName() const {
  223. return NameInfo.getName();
  224. }
  225. /// Sets the name to look up.
  226. void setLookupName(DeclarationName Name) {
  227. NameInfo.setName(Name);
  228. }
  229. /// Gets the kind of lookup to perform.
  230. Sema::LookupNameKind getLookupKind() const {
  231. return LookupKind;
  232. }
  233. /// True if this lookup is just looking for an existing declaration.
  234. bool isForRedeclaration() const {
  235. return Redecl;
  236. }
  237. /// True if this lookup is just looking for an existing declaration to link
  238. /// against a declaration with external linkage.
  239. bool isForExternalRedeclaration() const {
  240. return ExternalRedecl;
  241. }
  242. Sema::RedeclarationKind redeclarationKind() const {
  243. return ExternalRedecl ? Sema::ForExternalRedeclaration :
  244. Redecl ? Sema::ForVisibleRedeclaration : Sema::NotForRedeclaration;
  245. }
  246. /// Specify whether hidden declarations are visible, e.g.,
  247. /// for recovery reasons.
  248. void setAllowHidden(bool AH) {
  249. AllowHidden = AH;
  250. }
  251. /// Determine whether this lookup is permitted to see hidden
  252. /// declarations, such as those in modules that have not yet been imported.
  253. bool isHiddenDeclarationVisible(NamedDecl *ND) const {
  254. return AllowHidden ||
  255. (isForExternalRedeclaration() && ND->isExternallyDeclarable());
  256. }
  257. /// Sets whether tag declarations should be hidden by non-tag
  258. /// declarations during resolution. The default is true.
  259. void setHideTags(bool Hide) {
  260. HideTags = Hide;
  261. }
  262. /// Sets whether this is a template-name lookup. For template-name lookups,
  263. /// injected-class-names are treated as naming a template rather than a
  264. /// template specialization.
  265. void setTemplateNameLookup(bool TemplateName) {
  266. TemplateNameLookup = TemplateName;
  267. }
  268. bool isTemplateNameLookup() const { return TemplateNameLookup; }
  269. bool isAmbiguous() const {
  270. return getResultKind() == Ambiguous;
  271. }
  272. /// Determines if this names a single result which is not an
  273. /// unresolved value using decl. If so, it is safe to call
  274. /// getFoundDecl().
  275. bool isSingleResult() const {
  276. return getResultKind() == Found;
  277. }
  278. /// Determines if the results are overloaded.
  279. bool isOverloadedResult() const {
  280. return getResultKind() == FoundOverloaded;
  281. }
  282. bool isUnresolvableResult() const {
  283. return getResultKind() == FoundUnresolvedValue;
  284. }
  285. LookupResultKind getResultKind() const {
  286. assert(checkDebugAssumptions());
  287. return ResultKind;
  288. }
  289. AmbiguityKind getAmbiguityKind() const {
  290. assert(isAmbiguous());
  291. return Ambiguity;
  292. }
  293. const UnresolvedSetImpl &asUnresolvedSet() const {
  294. return Decls;
  295. }
  296. iterator begin() const { return iterator(Decls.begin()); }
  297. iterator end() const { return iterator(Decls.end()); }
  298. /// Return true if no decls were found
  299. bool empty() const { return Decls.empty(); }
  300. /// Return the base paths structure that's associated with
  301. /// these results, or null if none is.
  302. CXXBasePaths *getBasePaths() const {
  303. return Paths;
  304. }
  305. /// Determine whether the given declaration is visible to the
  306. /// program.
  307. static bool isVisible(Sema &SemaRef, NamedDecl *D);
  308. static bool isReachable(Sema &SemaRef, NamedDecl *D);
  309. static bool isAcceptable(Sema &SemaRef, NamedDecl *D,
  310. Sema::AcceptableKind Kind) {
  311. return Kind == Sema::AcceptableKind::Visible ? isVisible(SemaRef, D)
  312. : isReachable(SemaRef, D);
  313. }
  314. /// Determine whether this lookup is permitted to see the declaration.
  315. /// Note that a reachable but not visible declaration inhabiting a namespace
  316. /// is not allowed to be seen during name lookup.
  317. ///
  318. /// For example:
  319. /// ```
  320. /// // m.cppm
  321. /// export module m;
  322. /// struct reachable { int v; }
  323. /// export auto func() { return reachable{43}; }
  324. /// // Use.cpp
  325. /// import m;
  326. /// auto Use() {
  327. /// // Not valid. We couldn't see reachable here.
  328. /// // So isAvailableForLookup would return false when we look
  329. /// up 'reachable' here.
  330. /// // return reachable(43).v;
  331. /// // Valid. The field name 'v' is allowed during name lookup.
  332. /// // So isAvailableForLookup would return true when we look up 'v' here.
  333. /// return func().v;
  334. /// }
  335. /// ```
  336. static bool isAvailableForLookup(Sema &SemaRef, NamedDecl *ND);
  337. /// Retrieve the accepted (re)declaration of the given declaration,
  338. /// if there is one.
  339. NamedDecl *getAcceptableDecl(NamedDecl *D) const {
  340. if (!D->isInIdentifierNamespace(IDNS))
  341. return nullptr;
  342. if (isAvailableForLookup(getSema(), D) || isHiddenDeclarationVisible(D))
  343. return D;
  344. return getAcceptableDeclSlow(D);
  345. }
  346. private:
  347. static bool isAcceptableSlow(Sema &SemaRef, NamedDecl *D,
  348. Sema::AcceptableKind Kind);
  349. static bool isReachableSlow(Sema &SemaRef, NamedDecl *D);
  350. NamedDecl *getAcceptableDeclSlow(NamedDecl *D) const;
  351. public:
  352. /// Returns the identifier namespace mask for this lookup.
  353. unsigned getIdentifierNamespace() const {
  354. return IDNS;
  355. }
  356. /// Returns whether these results arose from performing a
  357. /// lookup into a class.
  358. bool isClassLookup() const {
  359. return NamingClass != nullptr;
  360. }
  361. /// Returns the 'naming class' for this lookup, i.e. the
  362. /// class which was looked into to find these results.
  363. ///
  364. /// C++0x [class.access.base]p5:
  365. /// The access to a member is affected by the class in which the
  366. /// member is named. This naming class is the class in which the
  367. /// member name was looked up and found. [Note: this class can be
  368. /// explicit, e.g., when a qualified-id is used, or implicit,
  369. /// e.g., when a class member access operator (5.2.5) is used
  370. /// (including cases where an implicit "this->" is added). If both
  371. /// a class member access operator and a qualified-id are used to
  372. /// name the member (as in p->T::m), the class naming the member
  373. /// is the class named by the nested-name-specifier of the
  374. /// qualified-id (that is, T). -- end note ]
  375. ///
  376. /// This is set by the lookup routines when they find results in a class.
  377. CXXRecordDecl *getNamingClass() const {
  378. return NamingClass;
  379. }
  380. /// Sets the 'naming class' for this lookup.
  381. void setNamingClass(CXXRecordDecl *Record) {
  382. NamingClass = Record;
  383. }
  384. /// Returns the base object type associated with this lookup;
  385. /// important for [class.protected]. Most lookups do not have an
  386. /// associated base object.
  387. QualType getBaseObjectType() const {
  388. return BaseObjectType;
  389. }
  390. /// Sets the base object type for this lookup.
  391. void setBaseObjectType(QualType T) {
  392. BaseObjectType = T;
  393. }
  394. /// Add a declaration to these results with its natural access.
  395. /// Does not test the acceptance criteria.
  396. void addDecl(NamedDecl *D) {
  397. addDecl(D, D->getAccess());
  398. }
  399. /// Add a declaration to these results with the given access.
  400. /// Does not test the acceptance criteria.
  401. void addDecl(NamedDecl *D, AccessSpecifier AS) {
  402. Decls.addDecl(D, AS);
  403. ResultKind = Found;
  404. }
  405. /// Add all the declarations from another set of lookup
  406. /// results.
  407. void addAllDecls(const LookupResult &Other) {
  408. Decls.append(Other.Decls.begin(), Other.Decls.end());
  409. ResultKind = Found;
  410. }
  411. /// Determine whether no result was found because we could not
  412. /// search into dependent base classes of the current instantiation.
  413. bool wasNotFoundInCurrentInstantiation() const {
  414. return ResultKind == NotFoundInCurrentInstantiation;
  415. }
  416. /// Note that while no result was found in the current instantiation,
  417. /// there were dependent base classes that could not be searched.
  418. void setNotFoundInCurrentInstantiation() {
  419. assert(ResultKind == NotFound && Decls.empty());
  420. ResultKind = NotFoundInCurrentInstantiation;
  421. }
  422. /// Determine whether the lookup result was shadowed by some other
  423. /// declaration that lookup ignored.
  424. bool isShadowed() const { return Shadowed; }
  425. /// Note that we found and ignored a declaration while performing
  426. /// lookup.
  427. void setShadowed() { Shadowed = true; }
  428. /// Resolves the result kind of the lookup, possibly hiding
  429. /// decls.
  430. ///
  431. /// This should be called in any environment where lookup might
  432. /// generate multiple lookup results.
  433. void resolveKind();
  434. /// Re-resolves the result kind of the lookup after a set of
  435. /// removals has been performed.
  436. void resolveKindAfterFilter() {
  437. if (Decls.empty()) {
  438. if (ResultKind != NotFoundInCurrentInstantiation)
  439. ResultKind = NotFound;
  440. if (Paths) {
  441. deletePaths(Paths);
  442. Paths = nullptr;
  443. }
  444. } else {
  445. std::optional<AmbiguityKind> SavedAK;
  446. bool WasAmbiguous = false;
  447. if (ResultKind == Ambiguous) {
  448. SavedAK = Ambiguity;
  449. WasAmbiguous = true;
  450. }
  451. ResultKind = Found;
  452. resolveKind();
  453. // If we didn't make the lookup unambiguous, restore the old
  454. // ambiguity kind.
  455. if (ResultKind == Ambiguous) {
  456. (void)WasAmbiguous;
  457. assert(WasAmbiguous);
  458. Ambiguity = *SavedAK;
  459. } else if (Paths) {
  460. deletePaths(Paths);
  461. Paths = nullptr;
  462. }
  463. }
  464. }
  465. template <class DeclClass>
  466. DeclClass *getAsSingle() const {
  467. if (getResultKind() != Found) return nullptr;
  468. return dyn_cast<DeclClass>(getFoundDecl());
  469. }
  470. /// Fetch the unique decl found by this lookup. Asserts
  471. /// that one was found.
  472. ///
  473. /// This is intended for users who have examined the result kind
  474. /// and are certain that there is only one result.
  475. NamedDecl *getFoundDecl() const {
  476. assert(getResultKind() == Found
  477. && "getFoundDecl called on non-unique result");
  478. return (*begin())->getUnderlyingDecl();
  479. }
  480. /// Fetches a representative decl. Useful for lazy diagnostics.
  481. NamedDecl *getRepresentativeDecl() const {
  482. assert(!Decls.empty() && "cannot get representative of empty set");
  483. return *begin();
  484. }
  485. /// Asks if the result is a single tag decl.
  486. bool isSingleTagDecl() const {
  487. return getResultKind() == Found && isa<TagDecl>(getFoundDecl());
  488. }
  489. /// Make these results show that the name was found in
  490. /// base classes of different types.
  491. ///
  492. /// The given paths object is copied and invalidated.
  493. void setAmbiguousBaseSubobjectTypes(CXXBasePaths &P);
  494. /// Make these results show that the name was found in
  495. /// distinct base classes of the same type.
  496. ///
  497. /// The given paths object is copied and invalidated.
  498. void setAmbiguousBaseSubobjects(CXXBasePaths &P);
  499. /// Make these results show that the name was found in
  500. /// different contexts and a tag decl was hidden by an ordinary
  501. /// decl in a different context.
  502. void setAmbiguousQualifiedTagHiding() {
  503. setAmbiguous(AmbiguousTagHiding);
  504. }
  505. /// Clears out any current state.
  506. LLVM_ATTRIBUTE_REINITIALIZES void clear() {
  507. ResultKind = NotFound;
  508. Decls.clear();
  509. if (Paths) deletePaths(Paths);
  510. Paths = nullptr;
  511. NamingClass = nullptr;
  512. Shadowed = false;
  513. }
  514. /// Clears out any current state and re-initializes for a
  515. /// different kind of lookup.
  516. void clear(Sema::LookupNameKind Kind) {
  517. clear();
  518. LookupKind = Kind;
  519. configure();
  520. }
  521. /// Change this lookup's redeclaration kind.
  522. void setRedeclarationKind(Sema::RedeclarationKind RK) {
  523. Redecl = (RK != Sema::NotForRedeclaration);
  524. ExternalRedecl = (RK == Sema::ForExternalRedeclaration);
  525. configure();
  526. }
  527. void dump();
  528. void print(raw_ostream &);
  529. /// Suppress the diagnostics that would normally fire because of this
  530. /// lookup. This happens during (e.g.) redeclaration lookups.
  531. void suppressDiagnostics() {
  532. Diagnose = false;
  533. }
  534. /// Determines whether this lookup is suppressing diagnostics.
  535. bool isSuppressingDiagnostics() const {
  536. return !Diagnose;
  537. }
  538. /// Sets a 'context' source range.
  539. void setContextRange(SourceRange SR) {
  540. NameContextRange = SR;
  541. }
  542. /// Gets the source range of the context of this name; for C++
  543. /// qualified lookups, this is the source range of the scope
  544. /// specifier.
  545. SourceRange getContextRange() const {
  546. return NameContextRange;
  547. }
  548. /// Gets the location of the identifier. This isn't always defined:
  549. /// sometimes we're doing lookups on synthesized names.
  550. SourceLocation getNameLoc() const {
  551. return NameInfo.getLoc();
  552. }
  553. /// Get the Sema object that this lookup result is searching
  554. /// with.
  555. Sema &getSema() const { return *SemaPtr; }
  556. /// A class for iterating through a result set and possibly
  557. /// filtering out results. The results returned are possibly
  558. /// sugared.
  559. class Filter {
  560. friend class LookupResult;
  561. LookupResult &Results;
  562. LookupResult::iterator I;
  563. bool Changed = false;
  564. bool CalledDone = false;
  565. Filter(LookupResult &Results) : Results(Results), I(Results.begin()) {}
  566. public:
  567. Filter(Filter &&F)
  568. : Results(F.Results), I(F.I), Changed(F.Changed),
  569. CalledDone(F.CalledDone) {
  570. F.CalledDone = true;
  571. }
  572. ~Filter() {
  573. assert(CalledDone &&
  574. "LookupResult::Filter destroyed without done() call");
  575. }
  576. bool hasNext() const {
  577. return I != Results.end();
  578. }
  579. NamedDecl *next() {
  580. assert(I != Results.end() && "next() called on empty filter");
  581. return *I++;
  582. }
  583. /// Restart the iteration.
  584. void restart() {
  585. I = Results.begin();
  586. }
  587. /// Erase the last element returned from this iterator.
  588. void erase() {
  589. Results.Decls.erase(--I);
  590. Changed = true;
  591. }
  592. /// Replaces the current entry with the given one, preserving the
  593. /// access bits.
  594. void replace(NamedDecl *D) {
  595. Results.Decls.replace(I-1, D);
  596. Changed = true;
  597. }
  598. /// Replaces the current entry with the given one.
  599. void replace(NamedDecl *D, AccessSpecifier AS) {
  600. Results.Decls.replace(I-1, D, AS);
  601. Changed = true;
  602. }
  603. void done() {
  604. assert(!CalledDone && "done() called twice");
  605. CalledDone = true;
  606. if (Changed)
  607. Results.resolveKindAfterFilter();
  608. }
  609. };
  610. /// Create a filter for this result set.
  611. Filter makeFilter() {
  612. return Filter(*this);
  613. }
  614. void setFindLocalExtern(bool FindLocalExtern) {
  615. if (FindLocalExtern)
  616. IDNS |= Decl::IDNS_LocalExtern;
  617. else
  618. IDNS &= ~Decl::IDNS_LocalExtern;
  619. }
  620. private:
  621. void diagnose() {
  622. if (isAmbiguous())
  623. getSema().DiagnoseAmbiguousLookup(*this);
  624. else if (isClassLookup() && getSema().getLangOpts().AccessControl)
  625. getSema().CheckLookupAccess(*this);
  626. }
  627. void setAmbiguous(AmbiguityKind AK) {
  628. ResultKind = Ambiguous;
  629. Ambiguity = AK;
  630. }
  631. void addDeclsFromBasePaths(const CXXBasePaths &P);
  632. void configure();
  633. bool checkDebugAssumptions() const;
  634. bool checkUnresolved() const {
  635. for (iterator I = begin(), E = end(); I != E; ++I)
  636. if (isa<UnresolvedUsingValueDecl>((*I)->getUnderlyingDecl()))
  637. return true;
  638. return false;
  639. }
  640. static void deletePaths(CXXBasePaths *);
  641. // Results.
  642. LookupResultKind ResultKind = NotFound;
  643. // ill-defined unless ambiguous. Still need to be initialized it will be
  644. // copied/moved.
  645. AmbiguityKind Ambiguity = {};
  646. UnresolvedSet<8> Decls;
  647. CXXBasePaths *Paths = nullptr;
  648. CXXRecordDecl *NamingClass = nullptr;
  649. QualType BaseObjectType;
  650. // Parameters.
  651. Sema *SemaPtr;
  652. DeclarationNameInfo NameInfo;
  653. SourceRange NameContextRange;
  654. Sema::LookupNameKind LookupKind;
  655. unsigned IDNS = 0; // set by configure()
  656. bool Redecl;
  657. bool ExternalRedecl;
  658. /// True if tag declarations should be hidden if non-tags
  659. /// are present
  660. bool HideTags = true;
  661. bool Diagnose = false;
  662. /// True if we should allow hidden declarations to be 'visible'.
  663. bool AllowHidden = false;
  664. /// True if the found declarations were shadowed by some other
  665. /// declaration that we skipped. This only happens when \c LookupKind
  666. /// is \c LookupRedeclarationWithLinkage.
  667. bool Shadowed = false;
  668. /// True if we're looking up a template-name.
  669. bool TemplateNameLookup = false;
  670. };
  671. /// Consumes visible declarations found when searching for
  672. /// all visible names within a given scope or context.
  673. ///
  674. /// This abstract class is meant to be subclassed by clients of \c
  675. /// Sema::LookupVisibleDecls(), each of which should override the \c
  676. /// FoundDecl() function to process declarations as they are found.
  677. class VisibleDeclConsumer {
  678. public:
  679. /// Destroys the visible declaration consumer.
  680. virtual ~VisibleDeclConsumer();
  681. /// Determine whether hidden declarations (from unimported
  682. /// modules) should be given to this consumer. By default, they
  683. /// are not included.
  684. virtual bool includeHiddenDecls() const;
  685. /// Invoked each time \p Sema::LookupVisibleDecls() finds a
  686. /// declaration visible from the current scope or context.
  687. ///
  688. /// \param ND the declaration found.
  689. ///
  690. /// \param Hiding a declaration that hides the declaration \p ND,
  691. /// or NULL if no such declaration exists.
  692. ///
  693. /// \param Ctx the original context from which the lookup started.
  694. ///
  695. /// \param InBaseClass whether this declaration was found in base
  696. /// class of the context we searched.
  697. virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
  698. bool InBaseClass) = 0;
  699. /// Callback to inform the client that Sema entered into a new context
  700. /// to find a visible declaration.
  701. //
  702. /// \param Ctx the context which Sema entered.
  703. virtual void EnteredContext(DeclContext *Ctx) {}
  704. };
  705. /// A class for storing results from argument-dependent lookup.
  706. class ADLResult {
  707. private:
  708. /// A map from canonical decls to the 'most recent' decl.
  709. llvm::MapVector<NamedDecl*, NamedDecl*> Decls;
  710. struct select_second {
  711. NamedDecl *operator()(std::pair<NamedDecl*, NamedDecl*> P) const {
  712. return P.second;
  713. }
  714. };
  715. public:
  716. /// Adds a new ADL candidate to this map.
  717. void insert(NamedDecl *D);
  718. /// Removes any data associated with a given decl.
  719. void erase(NamedDecl *D) {
  720. Decls.erase(cast<NamedDecl>(D->getCanonicalDecl()));
  721. }
  722. using iterator =
  723. llvm::mapped_iterator<decltype(Decls)::iterator, select_second>;
  724. iterator begin() { return iterator(Decls.begin(), select_second()); }
  725. iterator end() { return iterator(Decls.end(), select_second()); }
  726. };
  727. } // namespace clang
  728. #endif // LLVM_CLANG_SEMA_LOOKUP_H
  729. #ifdef __GNUC__
  730. #pragma GCC diagnostic pop
  731. #endif