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
18 #include "clang/AST/DeclAccessPair.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/SmallVector.h"
26 /// The iterator over UnresolvedSets. Serves as both the const and
27 /// non-const iterator.
28 class UnresolvedSetIterator {
30 typedef llvm::MutableArrayRef<DeclAccessPair> DeclsTy;
31 typedef DeclsTy::iterator IteratorTy;
35 friend class UnresolvedSetImpl;
36 friend class ASTUnresolvedSet;
37 friend class OverloadExpr;
38 explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
39 explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
40 ir(const_cast<DeclsTy::iterator>(ir)) {}
42 IteratorTy getIterator() const { return ir; }
45 UnresolvedSetIterator() {}
47 typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
48 typedef NamedDecl *value_type;
49 typedef NamedDecl **pointer;
50 typedef NamedDecl *reference;
51 typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
53 NamedDecl *getDecl() const { return ir->getDecl(); }
54 void setDecl(NamedDecl *ND) const { return ir->setDecl(ND); }
55 AccessSpecifier getAccess() const { return ir->getAccess(); }
56 void setAccess(AccessSpecifier AS) { ir->setAccess(AS); }
57 DeclAccessPair getPair() const { return *ir; }
59 NamedDecl *operator*() const { return getDecl(); }
61 UnresolvedSetIterator &operator++() { ++ir; return *this; }
62 UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
63 UnresolvedSetIterator &operator--() { --ir; return *this; }
64 UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
66 UnresolvedSetIterator &operator+=(difference_type d) {
67 ir += d; return *this;
69 UnresolvedSetIterator operator+(difference_type d) const {
70 return UnresolvedSetIterator(ir + d);
72 UnresolvedSetIterator &operator-=(difference_type d) {
73 ir -= d; return *this;
75 UnresolvedSetIterator operator-(difference_type d) const {
76 return UnresolvedSetIterator(ir - d);
78 value_type operator[](difference_type d) const { return *(*this + d); }
80 difference_type operator-(const UnresolvedSetIterator &o) const {
84 bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
85 bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
86 bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
87 bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
88 bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
89 bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
92 /// \brief A set of unresolved declarations.
93 class UnresolvedSetImpl {
94 typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
96 // Don't allow direct construction, and only permit subclassing by
99 template <unsigned N> friend class UnresolvedSet;
100 UnresolvedSetImpl() {}
101 UnresolvedSetImpl(const UnresolvedSetImpl &) LLVM_DELETED_FUNCTION;
104 // We don't currently support assignment through this iterator, so we might
105 // as well use the same implementation twice.
106 typedef UnresolvedSetIterator iterator;
107 typedef UnresolvedSetIterator const_iterator;
109 iterator begin() { return iterator(decls().begin()); }
110 iterator end() { return iterator(decls().end()); }
112 const_iterator begin() const { return const_iterator(decls().begin()); }
113 const_iterator end() const { return const_iterator(decls().end()); }
115 void addDecl(NamedDecl *D) {
119 void addDecl(NamedDecl *D, AccessSpecifier AS) {
120 decls().push_back(DeclAccessPair::make(D, AS));
123 /// Replaces the given declaration with the new one, once.
125 /// \return true if the set changed
126 bool replace(const NamedDecl* Old, NamedDecl *New) {
127 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
128 if (I->getDecl() == Old)
129 return (I->setDecl(New), true);
133 /// Replaces the declaration at the given iterator with the new one,
134 /// preserving the original access bits.
135 void replace(iterator I, NamedDecl *New) {
139 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
143 void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
145 void erase(iterator I) { *I.ir = decls().pop_back_val(); }
147 void setAccess(iterator I, AccessSpecifier AS) {
151 void clear() { decls().clear(); }
152 void set_size(unsigned N) { decls().set_size(N); }
154 bool empty() const { return decls().empty(); }
155 unsigned size() const { return decls().size(); }
157 void append(iterator I, iterator E) {
158 decls().append(I.ir, E.ir);
161 DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
162 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
165 // These work because the only permitted subclass is UnresolvedSetImpl
168 return *reinterpret_cast<DeclsTy*>(this);
170 const DeclsTy &decls() const {
171 return *reinterpret_cast<const DeclsTy*>(this);
175 /// \brief A set of unresolved declarations.
176 template <unsigned InlineCapacity> class UnresolvedSet :
177 public UnresolvedSetImpl {
178 SmallVector<DeclAccessPair, InlineCapacity> Decls;