Home | History | Annotate | Download | only in AST
      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 
     18 #include "clang/AST/DeclAccessPair.h"
     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 
     35   explicit UnresolvedSetIterator(DeclAccessPair *Iter)
     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.
     56 class UnresolvedSetImpl {
     57   typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
     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
     68   UnresolvedSetImpl(UnresolvedSetImpl &&) {}
     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.
     74   typedef UnresolvedSetIterator iterator;
     75   typedef UnresolvedSetIterator const_iterator;
     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 
     87   void addDecl(NamedDecl *D, AccessSpecifier AS) {
     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 {
    140   SmallVector<DeclAccessPair, InlineCapacity> Decls;
    141 };
    142 
    143 
    144 } // namespace clang
    145 
    146 #endif
    147