1 //===-- DeclLookups.h - Low-level interface to all names in a DC-*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines DeclContext::all_lookups_iterator.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_DECLLOOKUPS_H
15 #define LLVM_CLANG_AST_DECLLOOKUPS_H
16
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
22 namespace clang {
23
24 /// all_lookups_iterator - An iterator that provides a view over the results
25 /// of looking up every possible name.
26 class DeclContext::all_lookups_iterator {
27 StoredDeclsMap::iterator It, End;
28 public:
29 typedef lookup_result value_type;
30 typedef lookup_result reference;
31 typedef lookup_result pointer;
32 typedef std::forward_iterator_tag iterator_category;
33 typedef std::ptrdiff_t difference_type;
34
all_lookups_iterator()35 all_lookups_iterator() {}
all_lookups_iterator(StoredDeclsMap::iterator It,StoredDeclsMap::iterator End)36 all_lookups_iterator(StoredDeclsMap::iterator It,
37 StoredDeclsMap::iterator End)
38 : It(It), End(End) {}
39
40 reference operator*() const { return It->second.getLookupResult(); }
41 pointer operator->() const { return It->second.getLookupResult(); }
42
43 all_lookups_iterator& operator++() {
44 // Filter out using directives. They don't belong as results from name
45 // lookup anyways, except as an implementation detail. Users of the API
46 // should not expect to get them (or worse, rely on it).
47 do {
48 ++It;
49 } while (It != End &&
50 It->first == DeclarationName::getUsingDirectiveName());
51
52 return *this;
53 }
54
55 all_lookups_iterator operator++(int) {
56 all_lookups_iterator tmp(*this);
57 ++(*this);
58 return tmp;
59 }
60
61 friend bool operator==(all_lookups_iterator x, all_lookups_iterator y) {
62 return x.It == y.It;
63 }
64 friend bool operator!=(all_lookups_iterator x, all_lookups_iterator y) {
65 return x.It != y.It;
66 }
67 };
68
lookups_begin()69 DeclContext::all_lookups_iterator DeclContext::lookups_begin() const {
70 DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
71 if (Primary->hasExternalVisibleStorage())
72 getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
73 if (StoredDeclsMap *Map = Primary->buildLookup())
74 return all_lookups_iterator(Map->begin(), Map->end());
75 return all_lookups_iterator();
76 }
77
lookups_end()78 DeclContext::all_lookups_iterator DeclContext::lookups_end() const {
79 DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
80 if (Primary->hasExternalVisibleStorage())
81 getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
82 if (StoredDeclsMap *Map = Primary->buildLookup())
83 return all_lookups_iterator(Map->end(), Map->end());
84 return all_lookups_iterator();
85 }
86
87 } // end namespace clang
88
89 #endif
90