• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/DeclBase.h"
18 #include "clang/AST/DeclContextInternals.h"
19 #include "clang/AST/DeclarationName.h"
20 
21 namespace clang {
22 
23 /// all_lookups_iterator - An iterator that provides a view over the results
24 /// of looking up every possible name.
25 class DeclContext::all_lookups_iterator {
26   StoredDeclsMap::iterator It, End;
27 public:
28   typedef lookup_result             value_type;
29   typedef lookup_result             reference;
30   typedef lookup_result             pointer;
31   typedef std::forward_iterator_tag iterator_category;
32   typedef std::ptrdiff_t            difference_type;
33 
all_lookups_iterator()34   all_lookups_iterator() {}
all_lookups_iterator(StoredDeclsMap::iterator It,StoredDeclsMap::iterator End)35   all_lookups_iterator(StoredDeclsMap::iterator It,
36                        StoredDeclsMap::iterator End)
37       : It(It), End(End) {}
38 
39   reference operator*() const { return It->second.getLookupResult(); }
40   pointer operator->() const { return It->second.getLookupResult(); }
41 
42   all_lookups_iterator& operator++() {
43     // Filter out using directives. They don't belong as results from name
44     // lookup anyways, except as an implementation detail. Users of the API
45     // should not expect to get them (or worse, rely on it).
46     do {
47       ++It;
48     } while (It != End &&
49              It->first == DeclarationName::getUsingDirectiveName());
50 
51     return *this;
52   }
53 
54   all_lookups_iterator operator++(int) {
55     all_lookups_iterator tmp(*this);
56     ++(*this);
57     return tmp;
58   }
59 
60   friend bool operator==(all_lookups_iterator x, all_lookups_iterator y) {
61     return x.It == y.It;
62   }
63   friend bool operator!=(all_lookups_iterator x, all_lookups_iterator y) {
64     return x.It != y.It;
65   }
66 };
67 
lookups_begin()68 DeclContext::all_lookups_iterator DeclContext::lookups_begin() const {
69   DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
70   if (Primary->hasExternalVisibleStorage())
71     getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
72   if (StoredDeclsMap *Map = Primary->buildLookup())
73     return all_lookups_iterator(Map->begin(), Map->end());
74   return all_lookups_iterator();
75 }
76 
lookups_end()77 DeclContext::all_lookups_iterator DeclContext::lookups_end() const {
78   DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
79   if (Primary->hasExternalVisibleStorage())
80     getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
81   if (StoredDeclsMap *Map = Primary->buildLookup())
82     return all_lookups_iterator(Map->end(), Map->end());
83   return all_lookups_iterator();
84 }
85 
86 } // end namespace clang
87 
88 #endif
89