Clang Project

clang_source_code/include/clang/AST/UnresolvedSet.h
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/SmallVector.h"
21#include "llvm/ADT/iterator.h"
22#include <cstddef>
23#include <iterator>
24
25namespace clang {
26
27class NamedDecl;
28
29/// The iterator over UnresolvedSets.  Serves as both the const and
30/// non-const iterator.
31class 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
39  explicit UnresolvedSetIterator(DeclAccessPair *Iter)
40      : iterator_adaptor_base(Iter) {}
41  explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
42      : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
43
44public:
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 *NDconst { 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.
60class UnresolvedSetImpl {
61  using DeclsTy = SmallVectorImpl<DeclAccessPair>;
62
63  // Don't allow direct construction, and only permit subclassing by
64  // UnresolvedSet.
65private:
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
73  UnresolvedSetImpl(UnresolvedSetImpl &&) {}
74  UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
75
76public:
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;
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(DAS_none);
90  }
91
92  void addDecl(NamedDecl *DAccessSpecifier AS) {
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 NamedDeclOldNamedDecl *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 INamedDecl *New) { I.I->setDecl(New); }
109
110  void replace(iterator INamedDecl *NewAccessSpecifier AS) {
111    I.I->set(NewAS);
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 IAccessSpecifier 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 Iiterator E) { decls().append(I.I, E.I); }
127
128  DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
129  const DeclAccessPair &operator[](unsigned Iconst { return decls()[I]; }
130
131private:
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.
143template <unsigned InlineCapacity> class UnresolvedSet :
144    public UnresolvedSetImpl {
145  SmallVector<DeclAccessPairInlineCapacityDecls;
146};
147
148
149// namespace clang
150
151#endif // LLVM_CLANG_AST_UNRESOLVEDSET_H
152
clang::UnresolvedSetIterator::getDecl
clang::UnresolvedSetIterator::setDecl
clang::UnresolvedSetIterator::getAccess
clang::UnresolvedSetIterator::setAccess
clang::UnresolvedSetIterator::getPair
clang::UnresolvedSetImpl::begin
clang::UnresolvedSetImpl::end
clang::UnresolvedSetImpl::begin
clang::UnresolvedSetImpl::end
clang::UnresolvedSetImpl::addDecl
clang::UnresolvedSetImpl::addDecl
clang::UnresolvedSetImpl::replace
clang::UnresolvedSetImpl::replace
clang::UnresolvedSetImpl::replace
clang::UnresolvedSetImpl::erase
clang::UnresolvedSetImpl::erase
clang::UnresolvedSetImpl::setAccess
clang::UnresolvedSetImpl::clear
clang::UnresolvedSetImpl::set_size
clang::UnresolvedSetImpl::empty
clang::UnresolvedSetImpl::size
clang::UnresolvedSetImpl::append
clang::UnresolvedSetImpl::decls
clang::UnresolvedSetImpl::decls
clang::UnresolvedSet::Decls