10b57cec5SDimitry Andric //===- UnresolvedSet.h - Unresolved sets of declarations --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines the UnresolvedSet class, which is used to store
100b57cec5SDimitry Andric //  collections of declarations in the AST.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
150b57cec5SDimitry Andric #define LLVM_CLANG_AST_UNRESOLVEDSET_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "clang/AST/DeclAccessPair.h"
180b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
190b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h"
20480093f4SDimitry Andric #include "llvm/ADT/ArrayRef.h"
210b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
230b57cec5SDimitry Andric #include <cstddef>
240b57cec5SDimitry Andric #include <iterator>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace clang {
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class NamedDecl;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric /// The iterator over UnresolvedSets.  Serves as both the const and
310b57cec5SDimitry Andric /// non-const iterator.
320b57cec5SDimitry Andric class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
330b57cec5SDimitry Andric                                   UnresolvedSetIterator, DeclAccessPair *,
340b57cec5SDimitry Andric                                   std::random_access_iterator_tag, NamedDecl *,
350b57cec5SDimitry Andric                                   std::ptrdiff_t, NamedDecl *, NamedDecl *> {
360b57cec5SDimitry Andric   friend class ASTUnresolvedSet;
370b57cec5SDimitry Andric   friend class OverloadExpr;
380b57cec5SDimitry Andric   friend class UnresolvedSetImpl;
390b57cec5SDimitry Andric 
UnresolvedSetIterator(DeclAccessPair * Iter)400b57cec5SDimitry Andric   explicit UnresolvedSetIterator(DeclAccessPair *Iter)
410b57cec5SDimitry Andric       : iterator_adaptor_base(Iter) {}
UnresolvedSetIterator(const DeclAccessPair * Iter)420b57cec5SDimitry Andric   explicit UnresolvedSetIterator(const DeclAccessPair *Iter)
430b57cec5SDimitry Andric       : iterator_adaptor_base(const_cast<DeclAccessPair *>(Iter)) {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric public:
460b57cec5SDimitry Andric   // Work around a bug in MSVC 2013 where explicitly default constructed
470b57cec5SDimitry Andric   // temporaries with defaulted ctors are not zero initialized.
UnresolvedSetIterator()480b57cec5SDimitry Andric   UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
490b57cec5SDimitry Andric 
getDecl()500b57cec5SDimitry Andric   NamedDecl *getDecl() const { return I->getDecl(); }
setDecl(NamedDecl * ND)510b57cec5SDimitry Andric   void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
getAccess()520b57cec5SDimitry Andric   AccessSpecifier getAccess() const { return I->getAccess(); }
setAccess(AccessSpecifier AS)530b57cec5SDimitry Andric   void setAccess(AccessSpecifier AS) { I->setAccess(AS); }
getPair()540b57cec5SDimitry Andric   const DeclAccessPair &getPair() const { return *I; }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   NamedDecl *operator*() const { return getDecl(); }
570b57cec5SDimitry Andric   NamedDecl *operator->() const { return **this; }
580b57cec5SDimitry Andric };
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric /// A set of unresolved declarations.
610b57cec5SDimitry Andric class UnresolvedSetImpl {
620b57cec5SDimitry Andric   using DeclsTy = SmallVectorImpl<DeclAccessPair>;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // Don't allow direct construction, and only permit subclassing by
650b57cec5SDimitry Andric   // UnresolvedSet.
660b57cec5SDimitry Andric private:
670b57cec5SDimitry Andric   template <unsigned N> friend class UnresolvedSet;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   UnresolvedSetImpl() = default;
700b57cec5SDimitry Andric   UnresolvedSetImpl(const UnresolvedSetImpl &) = default;
710b57cec5SDimitry Andric   UnresolvedSetImpl &operator=(const UnresolvedSetImpl &) = default;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // FIXME: Switch these to "= default" once MSVC supports generating move ops
UnresolvedSetImpl(UnresolvedSetImpl &&)740b57cec5SDimitry Andric   UnresolvedSetImpl(UnresolvedSetImpl &&) {}
750b57cec5SDimitry Andric   UnresolvedSetImpl &operator=(UnresolvedSetImpl &&) { return *this; }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric public:
780b57cec5SDimitry Andric   // We don't currently support assignment through this iterator, so we might
790b57cec5SDimitry Andric   // as well use the same implementation twice.
800b57cec5SDimitry Andric   using iterator = UnresolvedSetIterator;
810b57cec5SDimitry Andric   using const_iterator = UnresolvedSetIterator;
820b57cec5SDimitry Andric 
begin()830b57cec5SDimitry Andric   iterator begin() { return iterator(decls().begin()); }
end()840b57cec5SDimitry Andric   iterator end() { return iterator(decls().end()); }
850b57cec5SDimitry Andric 
begin()860b57cec5SDimitry Andric   const_iterator begin() const { return const_iterator(decls().begin()); }
end()870b57cec5SDimitry Andric   const_iterator end() const { return const_iterator(decls().end()); }
880b57cec5SDimitry Andric 
pairs()89480093f4SDimitry Andric   ArrayRef<DeclAccessPair> pairs() const { return decls(); }
90480093f4SDimitry Andric 
addDecl(NamedDecl * D)910b57cec5SDimitry Andric   void addDecl(NamedDecl *D) {
920b57cec5SDimitry Andric     addDecl(D, AS_none);
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric 
addDecl(NamedDecl * D,AccessSpecifier AS)950b57cec5SDimitry Andric   void addDecl(NamedDecl *D, AccessSpecifier AS) {
960b57cec5SDimitry Andric     decls().push_back(DeclAccessPair::make(D, AS));
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   /// Replaces the given declaration with the new one, once.
1000b57cec5SDimitry Andric   ///
1010b57cec5SDimitry Andric   /// \return true if the set changed
replace(const NamedDecl * Old,NamedDecl * New)1020b57cec5SDimitry Andric   bool replace(const NamedDecl* Old, NamedDecl *New) {
1030b57cec5SDimitry Andric     for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
1040b57cec5SDimitry Andric       if (I->getDecl() == Old)
1050b57cec5SDimitry Andric         return (I->setDecl(New), true);
1060b57cec5SDimitry Andric     return false;
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   /// Replaces the declaration at the given iterator with the new one,
1100b57cec5SDimitry Andric   /// preserving the original access bits.
replace(iterator I,NamedDecl * New)1110b57cec5SDimitry Andric   void replace(iterator I, NamedDecl *New) { I.I->setDecl(New); }
1120b57cec5SDimitry Andric 
replace(iterator I,NamedDecl * New,AccessSpecifier AS)1130b57cec5SDimitry Andric   void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
1140b57cec5SDimitry Andric     I.I->set(New, AS);
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric 
erase(unsigned I)11706c3fb27SDimitry Andric   void erase(unsigned I) {
11806c3fb27SDimitry Andric     auto val = decls().pop_back_val();
11906c3fb27SDimitry Andric     if (I < size())
12006c3fb27SDimitry Andric       decls()[I] = val;
12106c3fb27SDimitry Andric   }
1220b57cec5SDimitry Andric 
erase(iterator I)12306c3fb27SDimitry Andric   void erase(iterator I) {
12406c3fb27SDimitry Andric     auto val = decls().pop_back_val();
12506c3fb27SDimitry Andric     if (I != end())
12606c3fb27SDimitry Andric       *I.I = val;
12706c3fb27SDimitry Andric   }
1280b57cec5SDimitry Andric 
setAccess(iterator I,AccessSpecifier AS)1290b57cec5SDimitry Andric   void setAccess(iterator I, AccessSpecifier AS) { I.I->setAccess(AS); }
1300b57cec5SDimitry Andric 
clear()1310b57cec5SDimitry Andric   void clear() { decls().clear(); }
truncate(unsigned N)13204eeddc0SDimitry Andric   void truncate(unsigned N) { decls().truncate(N); }
1330b57cec5SDimitry Andric 
empty()1340b57cec5SDimitry Andric   bool empty() const { return decls().empty(); }
size()1350b57cec5SDimitry Andric   unsigned size() const { return decls().size(); }
1360b57cec5SDimitry Andric 
append(iterator I,iterator E)1370b57cec5SDimitry Andric   void append(iterator I, iterator E) { decls().append(I.I, E.I); }
1380b57cec5SDimitry Andric 
assign(Iter I,Iter E)139480093f4SDimitry Andric   template<typename Iter> void assign(Iter I, Iter E) { decls().assign(I, E); }
140480093f4SDimitry Andric 
1410b57cec5SDimitry Andric   DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
1420b57cec5SDimitry Andric   const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric private:
1450b57cec5SDimitry Andric   // These work because the only permitted subclass is UnresolvedSetImpl
1460b57cec5SDimitry Andric 
decls()1470b57cec5SDimitry Andric   DeclsTy &decls() {
1480b57cec5SDimitry Andric     return *reinterpret_cast<DeclsTy*>(this);
1490b57cec5SDimitry Andric   }
decls()1500b57cec5SDimitry Andric   const DeclsTy &decls() const {
1510b57cec5SDimitry Andric     return *reinterpret_cast<const DeclsTy*>(this);
1520b57cec5SDimitry Andric   }
1530b57cec5SDimitry Andric };
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric /// A set of unresolved declarations.
1560b57cec5SDimitry Andric template <unsigned InlineCapacity> class UnresolvedSet :
1570b57cec5SDimitry Andric     public UnresolvedSetImpl {
1580b57cec5SDimitry Andric   SmallVector<DeclAccessPair, InlineCapacity> Decls;
1590b57cec5SDimitry Andric };
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric } // namespace clang
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric #endif // LLVM_CLANG_AST_UNRESOLVEDSET_H
165