1 //===- UnresolvedSet.h - Unresolved sets of declarations --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the UnresolvedSet class, which is used to store
10 // collections of declarations in the AST.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
15 #define LLVM_CLANG_AST_UNRESOLVEDSET_H
17 #include "clang/AST/DeclAccessPair.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/Specifiers.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator.h"
29 /// The iterator over UnresolvedSets. Serves as both the const and
30 /// non-const iterator.
31 class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
32 UnresolvedSetIterator, DeclAccessPair *,
33 std::random_access_iterator_tag, NamedDecl *,
34 std::ptrdiff_t, NamedDecl *, NamedDecl *> {
35 friend class ASTUnresolvedSet;
36 friend class OverloadExpr;
37 friend class UnresolvedSetImpl;
39 explicit UnresolvedSetIterator(DeclAccessPair *Iter)
40 : iterator_adaptor_base(Iter) {}
41 explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
42 : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
45 // Work around a bug in MSVC 2013 where explicitly default constructed
46 // temporaries with defaulted ctors are not zero initialized.
47 UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
49 NamedDecl *getDecl() const { return I->getDecl(); }
50 void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
51 AccessSpecifier getAccess() const { return I->getAccess(); }
52 void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
53 const DeclAccessPair &getPair() const { return *I; }
55 NamedDecl *operator*() const { return getDecl(); }
56 NamedDecl *operator->() const { return **this; }
59 /// A set of unresolved declarations.
60 class UnresolvedSetImpl {
61 using DeclsTy = SmallVectorImpl<DeclAccessPair>;
63 // Don't allow direct construction, and only permit subclassing by
66 template <unsigned N> friend class UnresolvedSet;
68 UnresolvedSetImpl() = default;
69 UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
70 UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
72 // FIXME: Switch these to "= default" once MSVC supports generating move ops
73 UnresolvedSetImpl(UnresolvedSetImpl &&) {}
74 UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
77 // We don't currently support assignment through this iterator, so we might
78 // as well use the same implementation twice.
79 using iterator = UnresolvedSetIterator;
80 using const_iterator = UnresolvedSetIterator;
82 iterator begin() { return iterator(decls().begin()); }
83 iterator end() { return iterator(decls().end()); }
85 const_iterator begin() const { return const_iterator(decls().begin()); }
86 const_iterator end() const { return const_iterator(decls().end()); }
88 void addDecl(NamedDecl *D) {
92 void addDecl(NamedDecl *D, AccessSpecifier AS) {
93 decls().push_back(DeclAccessPair::make(D, AS));
96 /// Replaces the given declaration with the new one, once.
98 /// \return true if the set changed
99 bool replace(const NamedDecl* Old, NamedDecl *New) {
100 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
101 if (I->getDecl() == Old)
102 return (I->setDecl(New), true);
106 /// Replaces the declaration at the given iterator with the new one,
107 /// preserving the original access bits.
108 void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
110 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
114 void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
116 void erase(iterator I) { *I.I = decls().pop_back_val(); }
118 void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
120 void clear() { decls().clear(); }
121 void set_size(unsigned N) { decls().set_size(N); }
123 bool empty() const { return decls().empty(); }
124 unsigned size() const { return decls().size(); }
126 void append(iterator I, iterator E) { decls().append(I.I, E.I); }
128 DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
129 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
132 // These work because the only permitted subclass is UnresolvedSetImpl
135 return *reinterpret_cast<DeclsTy*>(this);
137 const DeclsTy &decls() const {
138 return *reinterpret_cast<const DeclsTy*>(this);
142 /// A set of unresolved declarations.
143 template <unsigned InlineCapacity> class UnresolvedSet :
144 public UnresolvedSetImpl {
145 SmallVector<DeclAccessPair, InlineCapacity> Decls;
151 #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H