Home | History | Annotate | Download | only in MC
      1 //===- SymbolCategory.h ---------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #ifndef MCLD_SYMBOL_CATEGORY_H
     10 #define MCLD_SYMBOL_CATEGORY_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 #include <mcld/ADT/TypeTraits.h>
     15 #include <vector>
     16 
     17 namespace mcld
     18 {
     19 
     20 class LDSymbol;
     21 class ResolveInfo;
     22 /** \class SymbolCategory
     23  *  \brief SymbolCategory groups output LDSymbol into different categories.
     24  */
     25 class SymbolCategory
     26 {
     27 private:
     28   typedef std::vector<LDSymbol*> OutputSymbols;
     29 
     30 public:
     31   typedef OutputSymbols::iterator iterator;
     32   typedef OutputSymbols::const_iterator const_iterator;
     33 
     34 public:
     35   SymbolCategory();
     36 
     37   ~SymbolCategory();
     38 
     39   // -----  modifiers  ----- //
     40   SymbolCategory& add(LDSymbol& pSymbol);
     41 
     42   SymbolCategory& forceLocal(LDSymbol& pSymbol);
     43 
     44   SymbolCategory& arrange(LDSymbol& pSymbol, const ResolveInfo& pSourceInfo);
     45 
     46   SymbolCategory& changeCommonsToGlobal();
     47 
     48   // -----  access  ----- //
     49   LDSymbol& at(size_t pPosition)
     50   { return *m_OutputSymbols.at(pPosition); }
     51 
     52   const LDSymbol& at(size_t pPosition) const
     53   { return *m_OutputSymbols.at(pPosition); }
     54 
     55   LDSymbol& operator[](size_t pPosition)
     56   { return *m_OutputSymbols[pPosition]; }
     57 
     58   const LDSymbol& operator[](size_t pPosition) const
     59   { return *m_OutputSymbols[pPosition]; }
     60 
     61   // -----  observers  ----- //
     62   size_t numOfSymbols() const;
     63 
     64   size_t numOfLocals() const;
     65 
     66   size_t numOfCommons() const;
     67 
     68   size_t numOfRegulars() const;
     69 
     70   bool empty() const;
     71 
     72   bool emptyLocals() const;
     73 
     74   bool emptyCommons() const;
     75 
     76   bool emptyRegulars() const;
     77 
     78   // -----  iterators  ----- //
     79   iterator begin();
     80   iterator end();
     81   const_iterator begin() const;
     82   const_iterator end() const;
     83 
     84   iterator localBegin();
     85   iterator localEnd();
     86   const_iterator localBegin() const;
     87   const_iterator localEnd() const;
     88 
     89   iterator commonBegin();
     90   iterator commonEnd();
     91   const_iterator commonBegin() const;
     92   const_iterator commonEnd() const;
     93 
     94   iterator regularBegin();
     95   iterator regularEnd();
     96   const_iterator regularBegin() const;
     97   const_iterator regularEnd() const;
     98 
     99 private:
    100   class Category
    101   {
    102   public:
    103     enum Type {
    104       File,
    105       Local,
    106       Common,
    107       Weak,
    108       Global
    109     };
    110 
    111   public:
    112     Type type;
    113 
    114     size_t begin;
    115     size_t end;
    116 
    117     Category* prev;
    118     Category* next;
    119 
    120   public:
    121     Category(Type pType)
    122       : type(pType),
    123         begin(0),
    124         end(0),
    125         prev(NULL),
    126         next(NULL) {
    127     }
    128 
    129     size_t size() const
    130     { return (end - begin); }
    131 
    132     bool empty() const
    133     { return (begin == end); }
    134 
    135     bool isFirst() const
    136     { return (NULL == prev); }
    137 
    138     bool isLast() const
    139     { return (NULL == next); }
    140 
    141     static Type categorize(const ResolveInfo& pInfo);
    142 
    143   };
    144 
    145 private:
    146   OutputSymbols m_OutputSymbols;
    147 
    148   Category* m_pFile;
    149   Category* m_pLocal;
    150   Category* m_pCommon;
    151   Category* m_pWeak;
    152   Category* m_pGlobal;
    153 };
    154 
    155 } // namespace of mcld
    156 
    157 #endif
    158 
    159