clang  9.0.0svn
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 
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/Specifiers.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator.h"
22 #include <cstddef>
23 #include <iterator>
24 
25 namespace clang {
26 
27 class NamedDecl;
28 
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;
38 
40  : iterator_adaptor_base(Iter) {}
41  explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
42  : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
43 
44 public:
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) {}
48 
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; }
54 
55  NamedDecl *operator*() const { return getDecl(); }
56  NamedDecl *operator->() const { return **this; }
57 };
58 
59 /// A set of unresolved declarations.
62 
63  // Don't allow direct construction, and only permit subclassing by
64  // UnresolvedSet.
65 private:
66  template <unsigned N> friend class UnresolvedSet;
67 
68  UnresolvedSetImpl() = default;
69  UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
70  UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
71 
72  // FIXME: Switch these to "= default" once MSVC supports generating move ops
74  UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
75 
76 public:
77  // We don't currently support assignment through this iterator, so we might
78  // as well use the same implementation twice.
81 
82  iterator begin() { return iterator(decls().begin()); }
83  iterator end() { return iterator(decls().end()); }
84 
85  const_iterator begin() const { return const_iterator(decls().begin()); }
86  const_iterator end() const { return const_iterator(decls().end()); }
87 
88  void addDecl(NamedDecl *D) {
89  addDecl(D, AS_none);
90  }
91 
93  decls().push_back(DeclAccessPair::make(D, AS));
94  }
95 
96  /// Replaces the given declaration with the new one, once.
97  ///
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);
103  return false;
104  }
105 
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); }
109 
111  I.I->set(New, AS);
112  }
113 
114  void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
115 
116  void erase(iterator I) { *I.I = decls().pop_back_val(); }
117 
118  void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
119 
120  void clear() { decls().clear(); }
121  void set_size(unsigned N) { decls().set_size(N); }
122 
123  bool empty() const { return decls().empty(); }
124  unsigned size() const { return decls().size(); }
125 
126  void append(iterator I, iterator E) { decls().append(I.I, E.I); }
127 
128  DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
129  const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
130 
131 private:
132  // These work because the only permitted subclass is UnresolvedSetImpl
133 
134  DeclsTy &decls() {
135  return *reinterpret_cast<DeclsTy*>(this);
136  }
137  const DeclsTy &decls() const {
138  return *reinterpret_cast<const DeclsTy*>(this);
139  }
140 };
141 
142 /// A set of unresolved declarations.
143 template <unsigned InlineCapacity> class UnresolvedSet :
144  public UnresolvedSetImpl {
146 };
147 
148 
149 } // namespace clang
150 
151 #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H
void replace(iterator I, NamedDecl *New)
Replaces the declaration at the given iterator with the new one, preserving the original access bits...
const DeclAccessPair & operator[](unsigned I) const
const_iterator begin() const
Definition: UnresolvedSet.h:85
void setAccess(iterator I, AccessSpecifier AS)
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:105
void setAccess(AccessSpecifier AS)
Definition: UnresolvedSet.h:52
const DeclAccessPair & getPair() const
Definition: UnresolvedSet.h:53
void addDecl(NamedDecl *D, AccessSpecifier AS)
Definition: UnresolvedSet.h:92
The iterator over UnresolvedSets.
Definition: UnresolvedSet.h:31
NamedDecl * operator->() const
Definition: UnresolvedSet.h:56
void setDecl(NamedDecl *ND) const
Definition: UnresolvedSet.h:50
void replace(iterator I, NamedDecl *New, AccessSpecifier AS)
static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS)
void set_size(unsigned N)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
AccessSpecifier getAccess() const
Definition: UnresolvedSet.h:51
A set of unresolved declarations.
Definition: UnresolvedSet.h:60
void append(iterator I, iterator E)
NamedDecl * operator*() const
Definition: UnresolvedSet.h:55
void erase(unsigned I)
bool replace(const NamedDecl *Old, NamedDecl *New)
Replaces the given declaration with the new one, once.
Definition: UnresolvedSet.h:99
A reference to an overloaded function set, either an UnresolvedLookupExpr or an UnresolvedMemberExpr...
Definition: ExprCXX.h:2757
void addDecl(NamedDecl *D)
Definition: UnresolvedSet.h:88
NamedDecl * getDecl() const
Definition: UnresolvedSet.h:49
Defines various enumerations that describe declaration and type specifiers.
A POD class for pairing a NamedDecl* with an access specifier.
Dataflow Directional Tag Classes.
A set of unresolved declarations.
An UnresolvedSet-like class which uses the ASTContext&#39;s allocator.
const_iterator end() const
Definition: UnresolvedSet.h:86
void erase(iterator I)
unsigned size() const
This represents a decl that may have a name.
Definition: Decl.h:248
void setAccess(AccessSpecifier AS)
Definition: DeclBase.h:468
DeclAccessPair & operator[](unsigned I)