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