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/ArrayRef.h"
     21 #include "llvm/ADT/SmallVector.h"
     22 #include <iterator>
     23 
     24 namespace clang {
     25 
     26 /// The iterator over UnresolvedSets.  Serves as both the const and
     27 /// non-const iterator.
     28 class UnresolvedSetIterator {
     29 private:
     30   typedef MutableArrayRef<DeclAccessPair> DeclsTy;
     31   typedef DeclsTy::iterator IteratorTy;
     32 
     33   IteratorTy ir;
     34 
     35   friend class UnresolvedSetImpl;
     36   friend class ASTUnresolvedSet;
     37   friend class OverloadExpr;
     38   explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
     39   explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
     40     ir(const_cast<DeclsTy::iterator>(ir)) {}
     41 
     42   IteratorTy getIterator() const { return ir; }
     43 
     44 public:
     45   UnresolvedSetIterator() {}
     46 
     47   typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
     48   typedef NamedDecl *value_type;
     49   typedef NamedDecl **pointer;
     50   typedef NamedDecl *reference;
     51   typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
     52 
     53   NamedDecl *getDecl() const { return ir->getDecl(); }
     54   void setDecl(NamedDecl *ND) const { return ir->setDecl(ND); }
     55   AccessSpecifier getAccess() const { return ir->getAccess(); }
     56   void setAccess(AccessSpecifier AS) { ir->setAccess(AS); }
     57   DeclAccessPair getPair() const { return *ir; }
     58 
     59   NamedDecl *operator*() const { return getDecl(); }
     60 
     61   UnresolvedSetIterator &operator++() { ++ir; return *this; }
     62   UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
     63   UnresolvedSetIterator &operator--() { --ir; return *this; }
     64   UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
     65 
     66   UnresolvedSetIterator &operator+=(difference_type d) {
     67     ir += d; return *this;
     68   }
     69   UnresolvedSetIterator operator+(difference_type d) const {
     70     return UnresolvedSetIterator(ir + d);
     71   }
     72   UnresolvedSetIterator &operator-=(difference_type d) {
     73     ir -= d; return *this;
     74   }
     75   UnresolvedSetIterator operator-(difference_type d) const {
     76     return UnresolvedSetIterator(ir - d);
     77   }
     78   value_type operator[](difference_type d) const { return *(*this + d); }
     79 
     80   difference_type operator-(const UnresolvedSetIterator &o) const {
     81     return ir - o.ir;
     82   }
     83 
     84   bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
     85   bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
     86   bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
     87   bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
     88   bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
     89   bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
     90 };
     91 
     92 /// \brief A set of unresolved declarations.
     93 class UnresolvedSetImpl {
     94   typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
     95 
     96   // Don't allow direct construction, and only permit subclassing by
     97   // UnresolvedSet.
     98 private:
     99   template <unsigned N> friend class UnresolvedSet;
    100   UnresolvedSetImpl() {}
    101   UnresolvedSetImpl(const UnresolvedSetImpl &) LLVM_DELETED_FUNCTION;
    102 
    103 public:
    104   // We don't currently support assignment through this iterator, so we might
    105   // as well use the same implementation twice.
    106   typedef UnresolvedSetIterator iterator;
    107   typedef UnresolvedSetIterator const_iterator;
    108 
    109   iterator begin() { return iterator(decls().begin()); }
    110   iterator end() { return iterator(decls().end()); }
    111 
    112   const_iterator begin() const { return const_iterator(decls().begin()); }
    113   const_iterator end() const { return const_iterator(decls().end()); }
    114 
    115   void addDecl(NamedDecl *D) {
    116     addDecl(D, AS_none);
    117   }
    118 
    119   void addDecl(NamedDecl *D, AccessSpecifier AS) {
    120     decls().push_back(DeclAccessPair::make(D, AS));
    121   }
    122 
    123   /// Replaces the given declaration with the new one, once.
    124   ///
    125   /// \return true if the set changed
    126   bool replace(const NamedDecl* Old, NamedDecl *New) {
    127     for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
    128       if (I->getDecl() == Old)
    129         return (I->setDecl(New), true);
    130     return false;
    131   }
    132 
    133   /// Replaces the declaration at the given iterator with the new one,
    134   /// preserving the original access bits.
    135   void replace(iterator I, NamedDecl *New) {
    136     I.ir->setDecl(New);
    137   }
    138 
    139   void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
    140     I.ir->set(New, AS);
    141   }
    142 
    143   void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
    144 
    145   void erase(iterator I) { *I.ir = decls().pop_back_val(); }
    146 
    147   void setAccess(iterator I, AccessSpecifier AS) {
    148     I.ir->setAccess(AS);
    149   }
    150 
    151   void clear() { decls().clear(); }
    152   void set_size(unsigned N) { decls().set_size(N); }
    153 
    154   bool empty() const { return decls().empty(); }
    155   unsigned size() const { return decls().size(); }
    156 
    157   void append(iterator I, iterator E) {
    158     decls().append(I.ir, E.ir);
    159   }
    160 
    161   DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
    162   const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
    163 
    164 private:
    165   // These work because the only permitted subclass is UnresolvedSetImpl
    166 
    167   DeclsTy &decls() {
    168     return *reinterpret_cast<DeclsTy*>(this);
    169   }
    170   const DeclsTy &decls() const {
    171     return *reinterpret_cast<const DeclsTy*>(this);
    172   }
    173 };
    174 
    175 /// \brief A set of unresolved declarations.
    176 template <unsigned InlineCapacity> class UnresolvedSet :
    177     public UnresolvedSetImpl {
    178   SmallVector<DeclAccessPair, InlineCapacity> Decls;
    179 };
    180 
    181 
    182 } // namespace clang
    183 
    184 #endif
    185