clang API Documentation

UnresolvedSet.h
Go to the documentation of this file.
00001 //===-- UnresolvedSet.h - Unresolved sets of declarations  ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the UnresolvedSet class, which is used to store
00011 //  collections of declarations in the AST.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_AST_UNRESOLVEDSET_H
00016 #define LLVM_CLANG_AST_UNRESOLVEDSET_H
00017 
00018 #include "clang/AST/DeclAccessPair.h"
00019 #include "clang/Basic/LLVM.h"
00020 #include "llvm/ADT/ArrayRef.h"
00021 #include "llvm/ADT/SmallVector.h"
00022 #include <iterator>
00023 
00024 namespace clang {
00025 
00026 /// The iterator over UnresolvedSets.  Serves as both the const and
00027 /// non-const iterator.
00028 class UnresolvedSetIterator {
00029 private:
00030   typedef MutableArrayRef<DeclAccessPair> DeclsTy;
00031   typedef DeclsTy::iterator IteratorTy;
00032 
00033   IteratorTy ir;
00034 
00035   friend class UnresolvedSetImpl;
00036   friend class ASTUnresolvedSet;
00037   friend class OverloadExpr;
00038   explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
00039   explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
00040     ir(const_cast<DeclsTy::iterator>(ir)) {}
00041   
00042   IteratorTy getIterator() const { return ir; }
00043   
00044 public:
00045   UnresolvedSetIterator() {}
00046 
00047   typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
00048   typedef NamedDecl *value_type;
00049   typedef NamedDecl **pointer;
00050   typedef NamedDecl *reference;
00051   typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
00052 
00053   NamedDecl *getDecl() const { return ir->getDecl(); }
00054   void setDecl(NamedDecl *ND) const { return ir->setDecl(ND); }
00055   AccessSpecifier getAccess() const { return ir->getAccess(); }
00056   void setAccess(AccessSpecifier AS) { ir->setAccess(AS); }
00057   DeclAccessPair getPair() const { return *ir; }
00058 
00059   NamedDecl *operator*() const { return getDecl(); }
00060   
00061   UnresolvedSetIterator &operator++() { ++ir; return *this; }
00062   UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
00063   UnresolvedSetIterator &operator--() { --ir; return *this; }
00064   UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
00065 
00066   UnresolvedSetIterator &operator+=(difference_type d) {
00067     ir += d; return *this;
00068   }
00069   UnresolvedSetIterator operator+(difference_type d) const {
00070     return UnresolvedSetIterator(ir + d);
00071   }
00072   UnresolvedSetIterator &operator-=(difference_type d) {
00073     ir -= d; return *this;
00074   }
00075   UnresolvedSetIterator operator-(difference_type d) const {
00076     return UnresolvedSetIterator(ir - d);
00077   }
00078   value_type operator[](difference_type d) const { return *(*this + d); }
00079 
00080   difference_type operator-(const UnresolvedSetIterator &o) const {
00081     return ir - o.ir;
00082   }
00083 
00084   bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
00085   bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
00086   bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
00087   bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
00088   bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
00089   bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
00090 };
00091 
00092 /// \brief A set of unresolved declarations.
00093 class UnresolvedSetImpl {
00094   typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
00095 
00096   // Don't allow direct construction, and only permit subclassing by
00097   // UnresolvedSet.
00098 private:
00099   template <unsigned N> friend class UnresolvedSet;
00100   UnresolvedSetImpl() {}
00101   UnresolvedSetImpl(const UnresolvedSetImpl &) {}
00102 
00103 public:
00104   // We don't currently support assignment through this iterator, so we might
00105   // as well use the same implementation twice.
00106   typedef UnresolvedSetIterator iterator;
00107   typedef UnresolvedSetIterator const_iterator;
00108 
00109   iterator begin() { return iterator(decls().begin()); }
00110   iterator end() { return iterator(decls().end()); }
00111 
00112   const_iterator begin() const { return const_iterator(decls().begin()); }
00113   const_iterator end() const { return const_iterator(decls().end()); }
00114 
00115   void addDecl(NamedDecl *D) {
00116     addDecl(D, AS_none);
00117   }
00118 
00119   void addDecl(NamedDecl *D, AccessSpecifier AS) {
00120     decls().push_back(DeclAccessPair::make(D, AS));
00121   }
00122 
00123   /// Replaces the given declaration with the new one, once.
00124   ///
00125   /// \return true if the set changed
00126   bool replace(const NamedDecl* Old, NamedDecl *New) {
00127     for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
00128       if (I->getDecl() == Old)
00129         return (I->setDecl(New), true);
00130     return false;
00131   }
00132 
00133   /// Replaces the declaration at the given iterator with the new one,
00134   /// preserving the original access bits.
00135   void replace(iterator I, NamedDecl *New) {
00136     I.ir->setDecl(New);
00137   }
00138 
00139   void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
00140     I.ir->set(New, AS);
00141   }
00142 
00143   void erase(unsigned I) { decls()[I] = decls().pop_back_val(); }
00144 
00145   void erase(iterator I) { *I.ir = decls().pop_back_val(); }
00146 
00147   void setAccess(iterator I, AccessSpecifier AS) {
00148     I.ir->setAccess(AS);
00149   }
00150 
00151   void clear() { decls().clear(); }
00152   void set_size(unsigned N) { decls().set_size(N); }
00153 
00154   bool empty() const { return decls().empty(); }
00155   unsigned size() const { return decls().size(); }
00156 
00157   void append(iterator I, iterator E) {
00158     decls().append(I.ir, E.ir);
00159   }
00160 
00161   DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
00162   const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
00163 
00164 private:
00165   // These work because the only permitted subclass is UnresolvedSetImpl
00166 
00167   DeclsTy &decls() {
00168     return *reinterpret_cast<DeclsTy*>(this);
00169   }
00170   const DeclsTy &decls() const {
00171     return *reinterpret_cast<const DeclsTy*>(this);
00172   }
00173 };
00174 
00175 /// \brief A set of unresolved declarations.
00176 template <unsigned InlineCapacity> class UnresolvedSet :
00177     public UnresolvedSetImpl {
00178   SmallVector<DeclAccessPair, InlineCapacity> Decls;
00179 };
00180 
00181   
00182 } // namespace clang
00183 
00184 #endif