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