SymbolCategory.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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 17namespace mcld 18{ 19 20class LDSymbol; 21class ResolveInfo; 22/** \class SymbolCategory 23 * \brief SymbolCategory groups output LDSymbol into different categories. 24 */ 25class SymbolCategory 26{ 27private: 28 typedef std::vector<LDSymbol*> OutputSymbols; 29 30public: 31 typedef OutputSymbols::iterator iterator; 32 typedef OutputSymbols::const_iterator const_iterator; 33 34public: 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& changeLocalToTLS(const LDSymbol& pSymbol); 49 50 // ----- access ----- // 51 LDSymbol& at(size_t pPosition) 52 { return *m_OutputSymbols.at(pPosition); } 53 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 numOfLocals() const; 67 68 size_t numOfCommons() const; 69 70 size_t numOfRegulars() const; 71 72 bool empty() const; 73 74 bool emptyLocals() const; 75 76 bool emptyCommons() const; 77 78 bool emptyRegulars() const; 79 80 // ----- iterators ----- // 81 iterator begin(); 82 iterator end(); 83 const_iterator begin() const; 84 const_iterator end() const; 85 86 iterator localBegin(); 87 iterator localEnd(); 88 const_iterator localBegin() const; 89 const_iterator localEnd() const; 90 91 iterator tlsBegin(); 92 iterator tlsEnd(); 93 const_iterator tlsBegin() const; 94 const_iterator tlsEnd() const; 95 96 iterator commonBegin(); 97 iterator commonEnd(); 98 const_iterator commonBegin() const; 99 const_iterator commonEnd() const; 100 101 iterator regularBegin(); 102 iterator regularEnd(); 103 const_iterator regularBegin() const; 104 const_iterator regularEnd() const; 105 106private: 107 class Category 108 { 109 public: 110 enum Type { 111 File, 112 Local, 113 TLS, 114 Common, 115 Weak, 116 Global 117 }; 118 119 public: 120 Type type; 121 122 size_t begin; 123 size_t end; 124 125 Category* prev; 126 Category* next; 127 128 public: 129 Category(Type pType) 130 : type(pType), 131 begin(0), 132 end(0), 133 prev(NULL), 134 next(NULL) { 135 } 136 137 size_t size() const 138 { return (end - begin); } 139 140 bool empty() const 141 { return (begin == end); } 142 143 bool isFirst() const 144 { return (NULL == prev); } 145 146 bool isLast() const 147 { return (NULL == next); } 148 149 static Type categorize(const ResolveInfo& pInfo); 150 151 }; 152 153private: 154 OutputSymbols m_OutputSymbols; 155 156 Category* m_pFile; 157 Category* m_pLocal; 158 Category* m_pTLS; 159 Category* m_pCommon; 160 Category* m_pWeak; 161 Category* m_pGlobal; 162}; 163 164} // namespace of mcld 165 166#endif 167 168