zcov: / include/clang/AST/UnresolvedSet.h


Files: 1 Branches Taken: 75.0% 3 / 4
Generated: 2010-02-10 01:31 Branches Executed: 100.0% 4 / 4
Line Coverage: 98.7% 77 / 78


Programs: 40 Runs 60360


       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 <iterator>
      19                 : #include "llvm/ADT/SmallVector.h"
      20                 : #include "clang/Basic/Specifiers.h"
      21                 : 
      22                 : namespace clang {
      23                 : 
      24                 : class NamedDecl;
      25                 : 
      26                 : /// A POD class for pairing a NamedDecl* with an access specifier.
      27                 : /// Can be put into unions.
      28              891: class DeclAccessPair {
      29                 :   NamedDecl *Ptr; // we'd use llvm::PointerUnion, but it isn't trivial
      30                 : 
      31                 :   enum { Mask = 0x3 };
      32                 : 
      33                 : public:
      34            98065:   static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS) {
      35                 :     DeclAccessPair p;
      36            98065:     p.set(D, AS);
      37                 :     return p;
      38                 :   }
      39                 : 
      40           415550:   NamedDecl *getDecl() const {
      41           415550:     return (NamedDecl*) (~Mask & (uintptr_t) Ptr);
      42                 :   }
      43            14803:   AccessSpecifier getAccess() const {
      44            14803:     return AccessSpecifier(Mask & (uintptr_t) Ptr);
      45                 :   }
      46                 : 
      47              118:   void setDecl(NamedDecl *D) {
      48              118:     set(D, getAccess());
      49              118:   }
      50              247:   void setAccess(AccessSpecifier AS) {
      51              247:     set(getDecl(), AS);
      52              247:   }
      53            98430:   void set(NamedDecl *D, AccessSpecifier AS) {
      54                 :     Ptr = reinterpret_cast<NamedDecl*>(uintptr_t(AS) |
      55            98430:                                        reinterpret_cast<uintptr_t>(D));
      56            98430:   }
      57                 : 
      58                 :   operator NamedDecl*() const { return getDecl(); }
      59            32230:   NamedDecl *operator->() const { return getDecl(); }
      60                 : };
      61                 : }
      62                 : 
      63                 : // Take a moment to tell SmallVector that this is POD.
      64                 : namespace llvm {
      65                 : template<typename> struct isPodLike;
      66                 : template<> struct isPodLike<clang::DeclAccessPair> {
      67                 :    static const bool value = true;
      68                 : };
      69                 : }
      70                 : 
      71                 : namespace clang {
      72                 : 
      73                 : /// The iterator over UnresolvedSets.  Serves as both the const and
      74                 : /// non-const iterator.
      75                 : class UnresolvedSetIterator {
      76                 : private:
      77                 :   typedef llvm::SmallVectorImpl<DeclAccessPair> DeclsTy;
      78                 :   typedef DeclsTy::iterator IteratorTy;
      79                 : 
      80                 :   IteratorTy ir;
      81                 : 
      82                 :   friend class UnresolvedSetImpl;
      83           171428:   explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
      84           327389:   explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
      85           327389:     ir(const_cast<DeclsTy::iterator>(ir)) {}
      86                 : public:
      87                1:   UnresolvedSetIterator() {}
      88                 : 
      89                 :   typedef std::iterator_traits<IteratorTy>::difference_type difference_type;
      90                 :   typedef NamedDecl *value_type;
      91                 :   typedef NamedDecl **pointer;
      92                 :   typedef NamedDecl *reference;
      93                 :   typedef std::iterator_traits<IteratorTy>::iterator_category iterator_category;
      94                 : 
      95           380984:   NamedDecl *getDecl() const { return ir->getDecl(); }
      96            12777:   AccessSpecifier getAccess() const { return ir->getAccess(); }
      97                 : 
      98           380984:   NamedDecl *operator*() const { return getDecl(); }
      99                 :   
     100            22149:   UnresolvedSetIterator &operator++() { ++ir; return *this; }
     101             6466:   UnresolvedSetIterator operator++(int) { return UnresolvedSetIterator(ir++); }
     102              818:   UnresolvedSetIterator &operator--() { --ir; return *this; }
     103                 :   UnresolvedSetIterator operator--(int) { return UnresolvedSetIterator(ir--); }
     104                 : 
     105                 :   UnresolvedSetIterator &operator+=(difference_type d) {
     106                 :     ir += d; return *this;
     107                 :   }
     108             1389:   UnresolvedSetIterator operator+(difference_type d) const {
     109             1389:     return UnresolvedSetIterator(ir + d);
     110                 :   }
     111                 :   UnresolvedSetIterator &operator-=(difference_type d) {
     112                 :     ir -= d; return *this;
     113                 :   }
     114              107:   UnresolvedSetIterator operator-(difference_type d) const {
     115              107:     return UnresolvedSetIterator(ir - d);
     116                 :   }
     117                 :   value_type operator[](difference_type d) const { return *(*this + d); }
     118                 : 
     119             2694:   difference_type operator-(const UnresolvedSetIterator &o) const {
     120             2694:     return ir - o.ir;
     121                 :   }
     122                 : 
     123             1695:   bool operator==(const UnresolvedSetIterator &o) const { return ir == o.ir; }
     124           103523:   bool operator!=(const UnresolvedSetIterator &o) const { return ir != o.ir; }
     125                 :   bool operator<(const UnresolvedSetIterator &o) const { return ir < o.ir; }
     126                 :   bool operator<=(const UnresolvedSetIterator &o) const { return ir <= o.ir; }
     127                 :   bool operator>=(const UnresolvedSetIterator &o) const { return ir >= o.ir; }
     128                 :   bool operator>(const UnresolvedSetIterator &o) const { return ir > o.ir; }
     129                 : };
     130                 : 
     131                 : /// UnresolvedSet - A set of unresolved declarations.  This is needed
     132                 : /// in a lot of places, but isn't really worth breaking into its own
     133                 : /// header right now.
     134                 : class UnresolvedSetImpl {
     135                 :   typedef UnresolvedSetIterator::DeclsTy DeclsTy;
     136                 : 
     137                 :   // Don't allow direct construction, and only permit subclassing by
     138                 :   // UnresolvedSet.
     139                 : private:
     140                 :   template <unsigned N> friend class UnresolvedSet;
     141           153048:   UnresolvedSetImpl() {}
     142                 :   UnresolvedSetImpl(const UnresolvedSetImpl &) {}
     143                 : 
     144                 : public:
     145                 :   // We don't currently support assignment through this iterator, so we might
     146                 :   // as well use the same implementation twice.
     147                 :   typedef UnresolvedSetIterator iterator;
     148                 :   typedef UnresolvedSetIterator const_iterator;
     149                 : 
     150           158360:   iterator begin() { return iterator(decls().begin()); }
     151             5106:   iterator end() { return iterator(decls().end()); }
     152                 : 
     153           245116:   const_iterator begin() const { return const_iterator(decls().begin()); }
     154            82273:   const_iterator end() const { return const_iterator(decls().end()); }
     155                 : 
     156              295:   void addDecl(NamedDecl *D) {
     157              295:     addDecl(D, AS_none);
     158              295:   }
     159                 : 
     160            96137:   void addDecl(NamedDecl *D, AccessSpecifier AS) {
     161            96137:     decls().push_back(DeclAccessPair::make(D, AS));
     162            96137:   }
     163                 : 
     164                 :   /// Replaces the given declaration with the new one, once.
     165                 :   ///
     166                 :   /// \return true if the set changed
     167               11:   bool replace(const NamedDecl* Old, NamedDecl *New) {
                       14: branch 4 taken
                        0: branch 5 not taken
     168               14:     for (DeclsTy::iterator I = decls().begin(), E = decls().end(); I != E; ++I)
                       11: branch 1 taken
                        3: branch 2 taken
     169               14:       if (I->getDecl() == Old)
     170               11:         return (I->setDecl(New), true);
     171                0:     return false;
     172                 :   }
     173                 : 
     174                 :   /// Replaces the declaration at the given iterator with the new one,
     175                 :   /// preserving the original access bits.
     176              107:   void replace(iterator I, NamedDecl *New) {
     177              107:     I.ir->setDecl(New);
     178              107:   }
     179                 : 
     180                 :   void replace(iterator I, NamedDecl *New, AccessSpecifier AS) {
     181                 :     I.ir->set(New, AS);
     182                 :   }
     183                 : 
     184               73:   void erase(unsigned I) {
     185               73:     decls()[I] = decls().back();
     186               73:     decls().pop_back();
     187               73:   }
     188                 : 
     189              818:   void erase(iterator I) {
     190              818:     *I.ir = decls().back();
     191              818:     decls().pop_back();
     192              818:   }
     193                 : 
     194              247:   void setAccess(iterator I, AccessSpecifier AS) {
     195              247:     I.ir->setAccess(AS);
     196              247:   }
     197                 : 
     198            15548:   void clear() { decls().clear(); }
     199            14142:   void set_size(unsigned N) { decls().set_size(N); }
     200                 : 
     201           191015:   bool empty() const { return decls().empty(); }
     202           634979:   unsigned size() const { return decls().size(); }
     203                 : 
     204             1796:   void append(iterator I, iterator E) {
     205             1796:     decls().append(I.ir, E.ir);
     206             1796:   }
     207                 : 
     208            60851:   DeclAccessPair &operator[](unsigned I) { return decls()[I]; }
     209                 :   const DeclAccessPair &operator[](unsigned I) const { return decls()[I]; }
     210                 : 
     211                 : private:
     212                 :   // These work because the only permitted subclass is UnresolvedSetImpl
     213                 : 
     214           353817:   DeclsTy &decls() {
     215           353817:     return *reinterpret_cast<DeclsTy*>(this);
     216                 :   }
     217          1153383:   const DeclsTy &decls() const {
     218          1153383:     return *reinterpret_cast<const DeclsTy*>(this);
     219                 :   }
     220                 : };
     221                 : 
     222                 : /// A set of unresolved declarations 
     223                 : template <unsigned InlineCapacity> class UnresolvedSet :
     224           294832:     public UnresolvedSetImpl {
     225                 :   llvm::SmallVector<DeclAccessPair, InlineCapacity> Decls;
     226                 : };
     227                 : 
     228                 :   
     229                 : } // namespace clang
     230                 : 
     231                 : #endif

Generated: 2010-02-10 01:31 by zcov