clang 10.0.0git

UnresolvedSet.h
Go to the documentation of this file.
1 //===- UnresolvedSet.h - Unresolved sets of declarations --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the UnresolvedSet class, which is used to store
10 // collections of declarations in the AST.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
15 #define LLVM_CLANG_AST_UNRESOLVEDSET_H
16
17 #include "clang/AST/DeclAccessPair.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/Specifiers.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/iterator.h"
23 #include <cstddef>
24 #include <iterator>
25
26 namespace clang {
27
28 class NamedDecl;
29
30 /// The iterator over UnresolvedSets. Serves as both the const and
31 /// non-const iterator.
32 class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
33 UnresolvedSetIterator, DeclAccessPair *,
34 std::random_access_iterator_tag, NamedDecl *,
35 std::ptrdiff_t, NamedDecl *, NamedDecl *> {
36 friend class ASTUnresolvedSet;
37 friend class OverloadExpr;
38 friend class UnresolvedSetImpl;
39
40 explicit UnresolvedSetIterator(DeclAccessPair *Iter)
41 : iterator_adaptor_base(Iter) {}
42 explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
43 : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
44
45 public:
46 // Work around a bug in MSVC 2013 where explicitly default constructed
47 // temporaries with defaulted ctors are not zero initialized.
48 UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
49
50 NamedDecl *getDecl() const { return I->getDecl(); }
51 void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
52 AccessSpecifier getAccess() const { return I->getAccess(); }
53 void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
54 const DeclAccessPair &getPair() const { return *I; }
55
56 NamedDecl *operator*() const { return getDecl(); }
57 NamedDecl *operator->() const { return **this; }
58 };
59
60 /// A set of unresolved declarations.
61 class UnresolvedSetImpl {
62 using DeclsTy = SmallVectorImpl<DeclAccessPair>;
63
64 // Don't allow direct construction, and only permit subclassing by
65 // UnresolvedSet.
66 private:
67 template <unsigned N> friend class UnresolvedSet;
68
69 UnresolvedSetImpl() = default;
70 UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
71 UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
72
73 // FIXME: Switch these to "= default" once MSVC supports generating move ops
74 UnresolvedSetImpl(UnresolvedSetImpl &&) {}
75 UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
76
77 public:
78 // We don't currently support assignment through this iterator, so we might
79 // as well use the same implementation twice.
80 using iterator = UnresolvedSetIterator;
81 using const_iterator = UnresolvedSetIterator;
82
83 iterator begin() { return iterator(decls().begin()); }
84 iterator end() { return iterator(decls().end()); }
85
86 const_iterator begin() const { return const_iterator(decls().begin()); }
87 const_iterator end() const { return const_iterator(decls().end()); }
88
89 ArrayRef<DeclAccessPair> pairs() const { return decls(); }
90
91 void addDecl(NamedDecl *D) {
92 addDecl(D, AS_none);
93 }
94
95 void addDecl(NamedDecl *D, AccessSpecifier AS) {
96 decls().push_back(DeclAccessPair::make(D, AS));
97 }
98
99 /// Replaces the given declaration with the new one, once.
100 ///
101 /// \return true if the set changed
102 bool replace(const NamedDecl* Old, NamedDecl *New) {
103 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
104 if (I->getDecl() == Old)
105 return (I->setDecl(New), true);
106 return false;
107 }
108
109 /// Replaces the declaration at the given iterator with the new one,
110 /// preserving the original access bits.
111 void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
112
113 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
114 I.I->set(New, AS);
115 }
116
117 void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
118
119 void erase(iterator I) { *I.I = decls().pop_back_val(); }
120
121 void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
122
123 void clear() { decls().clear(); }
124 void set_size(unsigned N) { decls().set_size(N); }
125
126 bool empty() const { return decls().empty(); }
127 unsigned size() const { return decls().size(); }
128
129 void append(iterator I, iterator E) { decls().append(I.I, E.I); }
130
131 template<typename Iter> void assign(Iter I, Iter E) { decls().assign(I, E); }
132
133 DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
134 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
135
136 private:
137 // These work because the only permitted subclass is UnresolvedSetImpl
138
139 DeclsTy &decls() {
140 return *reinterpret_cast<DeclsTy*>(this);
141 }
142 const DeclsTy &decls() const {
143 return *reinterpret_cast<const DeclsTy*>(this);
144 }
145 };
146
147 /// A set of unresolved declarations.
148 template <unsigned InlineCapacity> class UnresolvedSet :
149 public UnresolvedSetImpl {
150 SmallVector<DeclAccessPair, InlineCapacity> Decls;
151 };
152
153
154 } // namespace clang
155
156 #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H
clang::UnresolvedSetImpl::replace
void replace(iterator I, NamedDecl *New)
Replaces the declaration at the given iterator with the new one, preserving the original access bits...
Definition: UnresolvedSet.h:111
clang::UnresolvedSetImpl::operator[]
const DeclAccessPair & operator[](unsigned I) const
Definition: UnresolvedSet.h:134
clang::UnresolvedSetImpl::begin
iterator begin()
Definition: UnresolvedSet.h:83
clang::UnresolvedSetImpl::begin
const_iterator begin() const
Definition: UnresolvedSet.h:86
clang::UnresolvedSetImpl::setAccess
void setAccess(iterator I, AccessSpecifier AS)
Definition: UnresolvedSet.h:121
clang::AccessSpecifier
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:113
clang::UnresolvedSetIterator::setAccess
void setAccess(AccessSpecifier AS)
Definition: UnresolvedSet.h:53
clang::UnresolvedSetIterator::getPair
const DeclAccessPair & getPair() const
Definition: UnresolvedSet.h:54
clang::UnresolvedSetIterator::UnresolvedSetIterator
UnresolvedSetIterator()
Definition: UnresolvedSet.h:48
clang::UnresolvedSetImpl::addDecl
void addDecl(NamedDecl *D, AccessSpecifier AS)
Definition: UnresolvedSet.h:95
clang::UnresolvedSetIterator
The iterator over UnresolvedSets.
Definition: UnresolvedSet.h:32
clang::UnresolvedSetIterator::operator->
NamedDecl * operator->() const
Definition: UnresolvedSet.h:57
llvm::SmallVectorImpl
Definition: LLVM.h:36
clang::UnresolvedSetIterator::setDecl
void setDecl(NamedDecl *ND) const
Definition: UnresolvedSet.h:51
clang::UnresolvedSetIterator::UnresolvedSetImpl
friend class UnresolvedSetImpl
Definition: UnresolvedSet.h:38
clang::UnresolvedSetImpl::replace
void replace(iterator I, NamedDecl *New, AccessSpecifier AS)
Definition: UnresolvedSet.h:113
clang::DeclAccessPair::make
static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS)
Definition: DeclAccessPair.h:35
clang::UnresolvedSetImpl::set_size
void set_size(unsigned N)
Definition: UnresolvedSet.h:124
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::UnresolvedSetIterator::getAccess
AccessSpecifier getAccess() const
Definition: UnresolvedSet.h:52
clang::UnresolvedSetImpl
A set of unresolved declarations.
Definition: UnresolvedSet.h:61
clang::UnresolvedSetImpl::append
void append(iterator I, iterator E)
Definition: UnresolvedSet.h:129
llvm::ArrayRef
Definition: LLVM.h:31
clang::UnresolvedSetIterator::operator*
NamedDecl * operator*() const
Definition: UnresolvedSet.h:56
clang::AS_none
Definition: Specifiers.h:117
DeclAccessPair.h
clang::UnresolvedSetImpl::assign
void assign(Iter I, Iter E)
Definition: UnresolvedSet.h:131
clang::UnresolvedSetImpl::erase
void erase(unsigned I)
Definition: UnresolvedSet.h:117
clang::UnresolvedSetImpl::replace
bool replace(const NamedDecl *Old, NamedDecl *New)
Replaces the given declaration with the new one, once.
Definition: UnresolvedSet.h:102
clang::OverloadExpr
A reference to an overloaded function set, either an UnresolvedLookupExpr or an UnresolvedMemberExpr...
Definition: ExprCXX.h:2844
clang::UnresolvedSetImpl::end
iterator end()
Definition: UnresolvedSet.h:84
clang::UnresolvedSetImpl::addDecl
void addDecl(NamedDecl *D)
Definition: UnresolvedSet.h:91
llvm::SmallVector
Definition: LLVM.h:35
clang::UnresolvedSetIterator::getDecl
NamedDecl * getDecl() const
Definition: UnresolvedSet.h:50
Specifiers.h
Defines various enumerations that describe declaration and type specifiers.
clang::DeclAccessPair
A POD class for pairing a NamedDecl* with an access specifier.
Definition: DeclAccessPair.h:29
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::UnresolvedSet
A set of unresolved declarations.
Definition: UnresolvedSet.h:148
clang::ASTUnresolvedSet
An UnresolvedSet-like class which uses the ASTContext&#39;s allocator.
Definition: ASTUnresolvedSet.h:29
clang::UnresolvedSetImpl::end
const_iterator end() const
Definition: UnresolvedSet.h:87
clang::UnresolvedSetImpl::erase
void erase(iterator I)
Definition: UnresolvedSet.h:119
clang::UnresolvedSetImpl::clear
void clear()
Definition: UnresolvedSet.h:123
clang::UnresolvedSetImpl::size
unsigned size() const
Definition: UnresolvedSet.h:127
clang::UnresolvedSetImpl::pairs
ArrayRef< DeclAccessPair > pairs() const
Definition: UnresolvedSet.h:89
clang::UnresolvedSetImpl::empty
bool empty() const
Definition: UnresolvedSet.h:126
iterator
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:223
clang::Decl::setAccess
void setAccess(AccessSpecifier AS)
Definition: DeclBase.h:468
clang::UnresolvedSetImpl::operator[]
DeclAccessPair & operator[](unsigned I)
Definition: UnresolvedSet.h:133