clang  6.0.0svn
UnresolvedSet.h
Go to the documentation of this file.
1 //===-- UnresolvedSet.h - Unresolved sets of declarations ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the UnresolvedSet class, which is used to store
11 // collections of declarations in the AST.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
16 #define LLVM_CLANG_AST_UNRESOLVEDSET_H
17 
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator.h"
22 
23 namespace clang {
24 
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;
34 
36  : iterator_adaptor_base(Iter) {}
37  explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
38  : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
39 
40 public:
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) {}
44 
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; }
50 
51  NamedDecl *operator*() const { return getDecl(); }
52  NamedDecl *operator->() const { return **this; }
53 };
54 
55 /// \brief A set of unresolved declarations.
58 
59  // Don't allow direct construction, and only permit subclassing by
60  // UnresolvedSet.
61 private:
62  template <unsigned N> friend class UnresolvedSet;
63  UnresolvedSetImpl() = default;
64  UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
65  UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
66 
67  // FIXME: Switch these to "= default" once MSVC supports generating move ops
69  UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
70 
71 public:
72  // We don't currently support assignment through this iterator, so we might
73  // as well use the same implementation twice.
76 
77  iterator begin() { return iterator(decls().begin()); }
78  iterator end() { return iterator(decls().end()); }
79 
80  const_iterator begin() const { return const_iterator(decls().begin()); }
81  const_iterator end() const { return const_iterator(decls().end()); }
82 
83  void addDecl(NamedDecl *D) {
84  addDecl(D, AS_none);
85  }
86 
88  decls().push_back(DeclAccessPair::make(D, AS));
89  }
90 
91  /// Replaces the given declaration with the new one, once.
92  ///
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);
98  return false;
99  }
100 
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); }
104 
105  void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
106  I.I->set(New, AS);
107  }
108 
109  void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
110 
111  void erase(iterator I) { *I.I = decls().pop_back_val(); }
112 
113  void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
114 
115  void clear() { decls().clear(); }
116  void set_size(unsigned N) { decls().set_size(N); }
117 
118  bool empty() const { return decls().empty(); }
119  unsigned size() const { return decls().size(); }
120 
121  void append(iterator I, iterator E) { decls().append(I.I, E.I); }
122 
123  DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
124  const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
125 
126 private:
127  // These work because the only permitted subclass is UnresolvedSetImpl
128 
129  DeclsTy &decls() {
130  return *reinterpret_cast<DeclsTy*>(this);
131  }
132  const DeclsTy &decls() const {
133  return *reinterpret_cast<const DeclsTy*>(this);
134  }
135 };
136 
137 /// \brief A set of unresolved declarations.
138 template <unsigned InlineCapacity> class UnresolvedSet :
139  public UnresolvedSetImpl {
141 };
142 
143 
144 } // namespace clang
145 
146 #endif
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:80
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:95
void setAccess(AccessSpecifier AS)
Definition: UnresolvedSet.h:48
const DeclAccessPair & getPair() const
Definition: UnresolvedSet.h:49
UnresolvedSetIterator const_iterator
Definition: UnresolvedSet.h:75
void addDecl(NamedDecl *D, AccessSpecifier AS)
Definition: UnresolvedSet.h:87
The iterator over UnresolvedSets.
Definition: UnresolvedSet.h:27
NamedDecl * operator->() const
Definition: UnresolvedSet.h:52
void setDecl(NamedDecl *ND) const
Definition: UnresolvedSet.h:46
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:47
A set of unresolved declarations.
Definition: UnresolvedSet.h:56
void append(iterator I, iterator E)
NamedDecl * operator*() const
Definition: UnresolvedSet.h:51
void erase(unsigned I)
bool replace(const NamedDecl *Old, NamedDecl *New)
Replaces the given declaration with the new one, once.
Definition: UnresolvedSet.h:94
A reference to an overloaded function set, either an UnresolvedLookupExpr or an UnresolvedMemberExpr...
Definition: ExprCXX.h:2467
void addDecl(NamedDecl *D)
Definition: UnresolvedSet.h:83
NamedDecl * getDecl() const
Definition: UnresolvedSet.h:45
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:81
void erase(iterator I)
unsigned size() const
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
void setAccess(AccessSpecifier AS)
Definition: DeclBase.h:446
DeclAccessPair & operator[](unsigned I)
UnresolvedSetIterator iterator
Definition: UnresolvedSet.h:74