1 //===- DeclLookups.h - Low-level interface to all names in a DC -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines DeclContext::all_lookups_iterator.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLLOOKUPS_H
15 #define LLVM_CLANG_AST_DECLLOOKUPS_H
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclContextInternals.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "clang/AST/ExternalASTSource.h"
27 /// all_lookups_iterator - An iterator that provides a view over the results
28 /// of looking up every possible name.
29 class DeclContext::all_lookups_iterator {
30 StoredDeclsMap::iterator It, End;
33 using value_type = lookup_result;
34 using reference = lookup_result;
35 using pointer = lookup_result;
36 using iterator_category = std::forward_iterator_tag;
37 using difference_type = std::ptrdiff_t;
39 all_lookups_iterator() = default;
40 all_lookups_iterator(StoredDeclsMap::iterator It,
41 StoredDeclsMap::iterator End)
44 DeclarationName getLookupName() const { return It->first; }
46 reference operator*() const { return It->second.getLookupResult(); }
47 pointer operator->() const { return It->second.getLookupResult(); }
49 all_lookups_iterator& operator++() {
50 // Filter out using directives. They don't belong as results from name
51 // lookup anyways, except as an implementation detail. Users of the API
52 // should not expect to get them (or worse, rely on it).
56 It->first == DeclarationName::getUsingDirectiveName());
61 all_lookups_iterator operator++(int) {
62 all_lookups_iterator tmp(*this);
67 friend bool operator==(all_lookups_iterator x, all_lookups_iterator y) {
71 friend bool operator!=(all_lookups_iterator x, all_lookups_iterator y) {
76 inline DeclContext::lookups_range DeclContext::lookups() const {
77 DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
78 if (Primary->hasExternalVisibleStorage())
79 getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
80 if (StoredDeclsMap *Map = Primary->buildLookup())
81 return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
82 all_lookups_iterator(Map->end(), Map->end()));
84 // Synthesize an empty range. This requires that two default constructed
85 // versions of these iterators form a valid empty range.
86 return lookups_range(all_lookups_iterator(), all_lookups_iterator());
89 inline DeclContext::lookups_range
90 DeclContext::noload_lookups(bool PreserveInternalState) const {
91 DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
92 if (!PreserveInternalState)
93 Primary->loadLazyLocalLexicalLookups();
94 if (StoredDeclsMap *Map = Primary->getLookupPtr())
95 return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
96 all_lookups_iterator(Map->end(), Map->end()));
98 // Synthesize an empty range. This requires that two default constructed
99 // versions of these iterators form a valid empty range.
100 return lookups_range(all_lookups_iterator(), all_lookups_iterator());
105 #endif // LLVM_CLANG_AST_DECLLOOKUPS_H