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