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 <cstddef> 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 SymbolCategory& changeLocalToDynamic(const LDSymbol& pSymbol); 49 50 // ----- access ----- // at(size_t pPosition)51 LDSymbol& at(size_t pPosition) 52 { return *m_OutputSymbols.at(pPosition); } 53 at(size_t pPosition)54 const LDSymbol& at(size_t pPosition) const 55 { return *m_OutputSymbols.at(pPosition); } 56 57 LDSymbol& operator[](size_t pPosition) 58 { return *m_OutputSymbols[pPosition]; } 59 60 const LDSymbol& operator[](size_t pPosition) const 61 { return *m_OutputSymbols[pPosition]; } 62 63 // ----- observers ----- // 64 size_t numOfSymbols() const; 65 66 size_t numOfFiles() const; 67 68 size_t numOfLocals() const; 69 70 size_t numOfLocalDyns() const; 71 72 size_t numOfCommons() const; 73 74 size_t numOfDynamics() const; 75 76 size_t numOfRegulars() const; 77 78 bool empty() const; 79 80 bool emptyFiles() const; 81 82 bool emptyLocals() const; 83 84 bool emptyLocalDyns() const; 85 86 bool emptyCommons() const; 87 88 bool emptyDynamics() const; 89 90 bool emptyRegulars() const; 91 92 // ----- iterators ----- // 93 iterator begin(); 94 iterator end(); 95 const_iterator begin() const; 96 const_iterator end() const; 97 98 iterator fileBegin(); 99 iterator fileEnd(); 100 const_iterator fileBegin() const; 101 const_iterator fileEnd() const; 102 103 iterator localBegin(); 104 iterator localEnd(); 105 const_iterator localBegin() const; 106 const_iterator localEnd() const; 107 108 iterator localDynBegin(); 109 iterator localDynEnd(); 110 const_iterator localDynBegin() const; 111 const_iterator localDynEnd() const; 112 113 iterator commonBegin(); 114 iterator commonEnd(); 115 const_iterator commonBegin() const; 116 const_iterator commonEnd() const; 117 118 iterator dynamicBegin(); 119 iterator dynamicEnd(); 120 const_iterator dynamicBegin() const; 121 const_iterator dynamicEnd() const; 122 123 iterator regularBegin(); 124 iterator regularEnd(); 125 const_iterator regularBegin() const; 126 const_iterator regularEnd() const; 127 128 private: 129 class Category 130 { 131 public: 132 enum Type { 133 File, 134 Local, 135 LocalDyn, 136 Common, 137 Dynamic, 138 Regular 139 }; 140 141 public: 142 Type type; 143 144 size_t begin; 145 size_t end; 146 147 Category* prev; 148 Category* next; 149 150 public: Category(Type pType)151 Category(Type pType) 152 : type(pType), 153 begin(0), 154 end(0), 155 prev(NULL), 156 next(NULL) { 157 } 158 size()159 size_t size() const 160 { return (end - begin); } 161 empty()162 bool empty() const 163 { return (begin == end); } 164 isFirst()165 bool isFirst() const 166 { return (NULL == prev); } 167 isLast()168 bool isLast() const 169 { return (NULL == next); } 170 171 static Type categorize(const ResolveInfo& pInfo); 172 }; 173 174 private: 175 SymbolCategory& add(LDSymbol& pSymbol, Category::Type pTarget); 176 177 private: 178 OutputSymbols m_OutputSymbols; 179 180 Category* m_pFile; 181 Category* m_pLocal; 182 Category* m_pLocalDyn; 183 Category* m_pCommon; 184 Category* m_pDynamic; 185 Category* m_pRegular; 186 }; 187 188 } // namespace of mcld 189 190 #endif 191 192