1 //===-- UnresolvedSet.h - Unresolved sets of declarations ------*- 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 the UnresolvedSet class, which is used to store
11 // collections of declarations in the AST.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
16 #define LLVM_CLANG_AST_UNRESOLVEDSET_H
19 #include "llvm/ADT/SmallVector.h"
20 #include "clang/AST/DeclAccessPair.h"
24 /// The iterator over UnresolvedSets. Serves as both the const and
25 /// non-const iterator.
26 class UnresolvedSetIterator {
28 typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
29 typedef DeclsTy::iterator IteratorTy;
33 friend class UnresolvedSetImpl;
34 friend class OverloadExpr;
35 explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
36 explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
37 ir(const_cast<DeclsTy::iterator>(ir)) {}
39 IteratorTy getIterator() const { return ir; }
42 UnresolvedSetIterator() {}
44 typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
45 typedef NamedDecl *value_type;
46 typedef NamedDecl **pointer;
47 typedef NamedDecl *reference;
48 typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
50 NamedDecl *getDecl() const { return ir->getDecl(); }
51 AccessSpecifier getAccess() const { return ir->getAccess(); }
52 void setAccess(AccessSpecifier AS) { ir->setAccess(AS); }
53 DeclAccessPair getPair() const { return *ir; }
55 NamedDecl *operator*() const { return getDecl(); }
57 UnresolvedSetIterator &operator++() { ++ir; return *this; }
58 UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
59 UnresolvedSetIterator &operator--() { --ir; return *this; }
60 UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
62 UnresolvedSetIterator &operator+=(difference_type d) {
63 ir += d; return *this;
65 UnresolvedSetIterator operator+(difference_type d) const {
66 return UnresolvedSetIterator(ir + d);
68 UnresolvedSetIterator &operator-=(difference_type d) {
69 ir -= d; return *this;
71 UnresolvedSetIterator operator-(difference_type d) const {
72 return UnresolvedSetIterator(ir - d);
74 value_type operator[](difference_type d) const { return *(*this + d); }
76 difference_type operator-(const UnresolvedSetIterator &o) const {
80 bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
81 bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
82 bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
83 bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
84 bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
85 bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
88 /// UnresolvedSet - A set of unresolved declarations.
89 class UnresolvedSetImpl {
90 typedef UnresolvedSetIterator::DeclsTy DeclsTy;
92 // Don't allow direct construction, and only permit subclassing by
95 template <unsigned N> friend class UnresolvedSet;
96 UnresolvedSetImpl() {}
97 UnresolvedSetImpl(const UnresolvedSetImpl &) {}
100 // We don't currently support assignment through this iterator, so we might
101 // as well use the same implementation twice.
102 typedef UnresolvedSetIterator iterator;
103 typedef UnresolvedSetIterator const_iterator;
105 iterator begin() { return iterator(decls().begin()); }
106 iterator end() { return iterator(decls().end()); }
108 const_iterator begin() const { return const_iterator(decls().begin()); }
109 const_iterator end() const { return const_iterator(decls().end()); }
111 void addDecl(NamedDecl *D) {
115 void addDecl(NamedDecl *D, AccessSpecifier AS) {
116 decls().push_back(DeclAccessPair::make(D, AS));
119 /// Replaces the given declaration with the new one, once.
121 /// \return true if the set changed
122 bool replace(const NamedDecl* Old, NamedDecl *New) {
123 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
124 if (I->getDecl() == Old)
125 return (I->setDecl(New), true);
129 /// Replaces the declaration at the given iterator with the new one,
130 /// preserving the original access bits.
131 void replace(iterator I, NamedDecl *New) {
135 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
139 void erase(unsigned I) {
140 decls()[I] = decls().back();
144 void erase(iterator I) {
145 *I.ir = decls().back();
149 void setAccess(iterator I, AccessSpecifier AS) {
153 void clear() { decls().clear(); }
154 void set_size(unsigned N) { decls().set_size(N); }
156 bool empty() const { return decls().empty(); }
157 unsigned size() const { return decls().size(); }
159 void append(iterator I, iterator E) {
160 decls().append(I.ir, E.ir);
163 DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
164 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
167 // These work because the only permitted subclass is UnresolvedSetImpl
170 return *reinterpret_cast<DeclsTy*>(this);
172 const DeclsTy &decls() const {
173 return *reinterpret_cast<const DeclsTy*>(this);
177 /// A set of unresolved declarations
178 template <unsigned InlineCapacity> class UnresolvedSet :
179 public UnresolvedSetImpl {
180 SmallVector<DeclAccessPair, InlineCapacity> Decls;