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