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 UnresolvedSetIterator(DeclAccessPair * Iter)40 explicit UnresolvedSetIterator(DeclAccessPair *Iter) 41 : iterator_adaptor_base(Iter) {} UnresolvedSetIterator(const DeclAccessPair * 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. UnresolvedSetIterator()48 UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {} 49 getDeclID()50 uint64_t getDeclID() const { return I->getDeclID(); } getDecl()51 NamedDecl *getDecl() const { return I->getDecl(); } setDecl(NamedDecl * ND)52 void setDecl(NamedDecl *ND) const { return I->setDecl(ND); } getAccess()53 AccessSpecifier getAccess() const { return I->getAccess(); } setAccess(AccessSpecifier AS)54 void setAccess(AccessSpecifier AS) { I->setAccess(AS); } getPair()55 const DeclAccessPair &getPair() const { return *I; } 56 57 NamedDecl *operator*() const { return getDecl(); } 58 NamedDecl *operator->() const { return **this; } 59 }; 60 61 /// A set of unresolved declarations. 62 class UnresolvedSetImpl { 63 using DeclsTy = SmallVectorImpl<DeclAccessPair>; 64 65 // Don't allow direct construction, and only permit subclassing by 66 // UnresolvedSet. 67 private: 68 template <unsigned N> friend class UnresolvedSet; 69 70 UnresolvedSetImpl() = default; 71 UnresolvedSetImpl(const UnresolvedSetImpl &) = default; 72 UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default; 73 74 // FIXME: Switch these to "= default" once MSVC supports generating move ops UnresolvedSetImpl(UnresolvedSetImpl &&)75 UnresolvedSetImpl(UnresolvedSetImpl &&) {} 76 UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; } 77 78 public: 79 // We don't currently support assignment through this iterator, so we might 80 // as well use the same implementation twice. 81 using iterator = UnresolvedSetIterator; 82 using const_iterator = UnresolvedSetIterator; 83 begin()84 iterator begin() { return iterator(decls().begin()); } end()85 iterator end() { return iterator(decls().end()); } 86 begin()87 const_iterator begin() const { return const_iterator(decls().begin()); } end()88 const_iterator end() const { return const_iterator(decls().end()); } 89 pairs()90 ArrayRef<DeclAccessPair> pairs() const { return decls(); } 91 addDecl(NamedDecl * D)92 void addDecl(NamedDecl *D) { 93 addDecl(D, AS_none); 94 } 95 addDecl(NamedDecl * D,AccessSpecifier AS)96 void addDecl(NamedDecl *D, AccessSpecifier AS) { 97 decls().push_back(DeclAccessPair::make(D, AS)); 98 } 99 100 /// Replaces the given declaration with the new one, once. 101 /// 102 /// \return true if the set changed replace(const NamedDecl * Old,NamedDecl * New)103 bool replace(const NamedDecl* Old, NamedDecl *New) { 104 for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I) 105 if (I->getDecl() == Old) 106 return (I->setDecl(New), true); 107 return false; 108 } 109 110 /// Replaces the declaration at the given iterator with the new one, 111 /// preserving the original access bits. replace(iterator I,NamedDecl * New)112 void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); } 113 replace(iterator I,NamedDecl * New,AccessSpecifier AS)114 void replace(iterator I, NamedDecl *New, AccessSpecifier AS) { 115 I.I->set(New, AS); 116 } 117 erase(unsigned I)118 void erase(unsigned I) { 119 auto val = decls().pop_back_val(); 120 if (I < size()) 121 decls()[I] = val; 122 } 123 erase(iterator I)124 void erase(iterator I) { 125 auto val = decls().pop_back_val(); 126 if (I != end()) 127 *I.I = val; 128 } 129 setAccess(iterator I,AccessSpecifier AS)130 void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); } 131 clear()132 void clear() { decls().clear(); } truncate(unsigned N)133 void truncate(unsigned N) { decls().truncate(N); } 134 empty()135 bool empty() const { return decls().empty(); } size()136 unsigned size() const { return decls().size(); } 137 append(iterator I,iterator E)138 void append(iterator I, iterator E) { decls().append(I.I, E.I); } 139 assign(Iter I,Iter E)140 template<typename Iter> void assign(Iter I, Iter E) { decls().assign(I, E); } 141 142 DeclAccessPair &operator[](unsigned I) { return decls()[I]; } 143 const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; } 144 145 private: 146 // These work because the only permitted subclass is UnresolvedSetImpl 147 decls()148 DeclsTy &decls() { 149 return *reinterpret_cast<DeclsTy*>(this); 150 } decls()151 const DeclsTy &decls() const { 152 return *reinterpret_cast<const DeclsTy*>(this); 153 } 154 }; 155 156 /// A set of unresolved declarations. 157 template <unsigned InlineCapacity> class UnresolvedSet : 158 public UnresolvedSetImpl { 159 SmallVector<DeclAccessPair, InlineCapacity> Decls; 160 }; 161 162 163 } // namespace clang 164 165 #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H 166