UnresolvedSet.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- UnresolvedSet.h - Unresolved sets of declarations --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the UnresolvedSet class, which is used to store
  15. // collections of declarations in the AST.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
  19. #define LLVM_CLANG_AST_UNRESOLVEDSET_H
  20. #include "clang/AST/DeclAccessPair.h"
  21. #include "clang/Basic/LLVM.h"
  22. #include "clang/Basic/Specifiers.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/iterator.h"
  26. #include <cstddef>
  27. #include <iterator>
  28. namespace clang {
  29. class NamedDecl;
  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. explicit UnresolvedSetIterator(DeclAccessPair *Iter)
  40. : iterator_adaptor_base(Iter) {}
  41. explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
  42. : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
  43. public:
  44. // Work around a bug in MSVC 2013 where explicitly default constructed
  45. // temporaries with defaulted ctors are not zero initialized.
  46. UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
  47. NamedDecl *getDecl() const { return I->getDecl(); }
  48. void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
  49. AccessSpecifier getAccess() const { return I->getAccess(); }
  50. void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
  51. const DeclAccessPair &getPair() const { return *I; }
  52. NamedDecl *operator*() const { return getDecl(); }
  53. NamedDecl *operator->() const { return **this; }
  54. };
  55. /// A set of unresolved declarations.
  56. class UnresolvedSetImpl {
  57. using DeclsTy = SmallVectorImpl<DeclAccessPair>;
  58. // Don't allow direct construction, and only permit subclassing by
  59. // UnresolvedSet.
  60. private:
  61. template <unsigned N> friend class UnresolvedSet;
  62. UnresolvedSetImpl() = default;
  63. UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
  64. UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
  65. // FIXME: Switch these to "= default" once MSVC supports generating move ops
  66. UnresolvedSetImpl(UnresolvedSetImpl &&) {}
  67. UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
  68. public:
  69. // We don't currently support assignment through this iterator, so we might
  70. // as well use the same implementation twice.
  71. using iterator = UnresolvedSetIterator;
  72. using const_iterator = UnresolvedSetIterator;
  73. iterator begin() { return iterator(decls().begin()); }
  74. iterator end() { return iterator(decls().end()); }
  75. const_iterator begin() const { return const_iterator(decls().begin()); }
  76. const_iterator end() const { return const_iterator(decls().end()); }
  77. ArrayRef<DeclAccessPair> pairs() const { return decls(); }
  78. void addDecl(NamedDecl *D) {
  79. addDecl(D, AS_none);
  80. }
  81. void addDecl(NamedDecl *D, AccessSpecifier AS) {
  82. decls().push_back(DeclAccessPair::make(D, AS));
  83. }
  84. /// Replaces the given declaration with the new one, once.
  85. ///
  86. /// \return true if the set changed
  87. bool replace(const NamedDecl* Old, NamedDecl *New) {
  88. for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
  89. if (I->getDecl() == Old)
  90. return (I->setDecl(New), true);
  91. return false;
  92. }
  93. /// Replaces the declaration at the given iterator with the new one,
  94. /// preserving the original access bits.
  95. void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
  96. void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
  97. I.I->set(New, AS);
  98. }
  99. void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
  100. void erase(iterator I) { *I.I = decls().pop_back_val(); }
  101. void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
  102. void clear() { decls().clear(); }
  103. void truncate(unsigned N) { decls().truncate(N); }
  104. bool empty() const { return decls().empty(); }
  105. unsigned size() const { return decls().size(); }
  106. void append(iterator I, iterator E) { decls().append(I.I, E.I); }
  107. template<typename Iter> void assign(Iter I, Iter E) { decls().assign(I, E); }
  108. DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
  109. const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
  110. private:
  111. // These work because the only permitted subclass is UnresolvedSetImpl
  112. DeclsTy &decls() {
  113. return *reinterpret_cast<DeclsTy*>(this);
  114. }
  115. const DeclsTy &decls() const {
  116. return *reinterpret_cast<const DeclsTy*>(this);
  117. }
  118. };
  119. /// A set of unresolved declarations.
  120. template <unsigned InlineCapacity> class UnresolvedSet :
  121. public UnresolvedSetImpl {
  122. SmallVector<DeclAccessPair, InlineCapacity> Decls;
  123. };
  124. } // namespace clang
  125. #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H
  126. #ifdef __GNUC__
  127. #pragma GCC diagnostic pop
  128. #endif