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/SmallVector.h"
21 #include "llvm/ADT/iterator.h"
25 /// The iterator over UnresolvedSets. Serves as both the const and
26 /// non-const iterator.
27 class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
28 UnresolvedSetIterator, DeclAccessPair *,
29 std::random_access_iterator_tag, NamedDecl *,
30 std::ptrdiff_t, NamedDecl *, NamedDecl *> {
31 friend class UnresolvedSetImpl;
32 friend class ASTUnresolvedSet;
33 friend class OverloadExpr;
35 explicit UnresolvedSetIterator(DeclAccessPair *Iter)
36 : iterator_adaptor_base(Iter) {}
37 explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
38 : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
41 // Work around a bug in MSVC 2013 where explicitly default constructed
42 // temporaries with defaulted ctors are not zero initialized.
43 UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
45 NamedDecl *getDecl() const { return I->getDecl(); }
46 void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
47 AccessSpecifier getAccess() const { return I->getAccess(); }
48 void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
49 const DeclAccessPair &getPair() const { return *I; }
51 NamedDecl *operator*() const { return getDecl(); }
52 NamedDecl *operator->() const { return **this; }
55 /// \brief A set of unresolved declarations.
56 class UnresolvedSetImpl {
57 typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
59 // Don't allow direct construction, and only permit subclassing by
62 template <unsigned N> friend class UnresolvedSet;
63 UnresolvedSetImpl() = default;
64 UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
65 UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
67 // FIXME: Switch these to "= default" once MSVC supports generating move ops
68 UnresolvedSetImpl(UnresolvedSetImpl &&) {}
69 UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
72 // We don't currently support assignment through this iterator, so we might
73 // as well use the same implementation twice.
74 typedef UnresolvedSetIterator iterator;
75 typedef UnresolvedSetIterator const_iterator;
77 iterator begin() { return iterator(decls().begin()); }
78 iterator end() { return iterator(decls().end()); }
80 const_iterator begin() const { return const_iterator(decls().begin()); }
81 const_iterator end() const { return const_iterator(decls().end()); }
83 void addDecl(NamedDecl *D) {
87 void addDecl(NamedDecl *D, AccessSpecifier AS) {
88 decls().push_back(DeclAccessPair::make(D, AS));
91 /// Replaces the given declaration with the new one, once.
93 /// \return true if the set changed
94 bool replace(const NamedDecl* Old, NamedDecl *New) {
95 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
96 if (I->getDecl() == Old)
97 return (I->setDecl(New), true);
101 /// Replaces the declaration at the given iterator with the new one,
102 /// preserving the original access bits.
103 void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
105 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
109 void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
111 void erase(iterator I) { *I.I = decls().pop_back_val(); }
113 void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
115 void clear() { decls().clear(); }
116 void set_size(unsigned N) { decls().set_size(N); }
118 bool empty() const { return decls().empty(); }
119 unsigned size() const { return decls().size(); }
121 void append(iterator I, iterator E) { decls().append(I.I, E.I); }
123 DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
124 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
127 // These work because the only permitted subclass is UnresolvedSetImpl
130 return *reinterpret_cast<DeclsTy*>(this);
132 const DeclsTy &decls() const {
133 return *reinterpret_cast<const DeclsTy*>(this);
137 /// \brief A set of unresolved declarations.
138 template <unsigned InlineCapacity> class UnresolvedSet :
139 public UnresolvedSetImpl {
140 SmallVector<DeclAccessPair, InlineCapacity> Decls;