NamePool.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
1//===- NamePool.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_LD_NAMEPOOL_H 10#define MCLD_LD_NAMEPOOL_H 11#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14 15#include <mcld/Config/Config.h> 16#include <mcld/ADT/HashTable.h> 17#include <mcld/ADT/StringHash.h> 18#include <mcld/ADT/Uncopyable.h> 19#include <mcld/LD/Resolver.h> 20#include <mcld/LD/ResolveInfo.h> 21#include <mcld/Support/GCFactory.h> 22 23#include <utility> 24 25#include <llvm/ADT/StringRef.h> 26 27namespace mcld { 28 29class StringTable; 30class SymbolTableIF; 31class SectionData; 32 33/** \class NamePool 34 * \brief Store symbol and search symbol by name. Can help symbol resolution. 35 * 36 * - MCLinker is responsed for creating NamePool. 37 */ 38class NamePool : private Uncopyable 39{ 40public: 41 typedef HashTable<ResolveInfo, hash::StringHash<hash::DJB> > Table; 42 typedef Table::iterator syminfo_iterator; 43 typedef Table::const_iterator const_syminfo_iterator; 44 45 typedef GCFactory<ResolveInfo*, 128> FreeInfoSet; 46 typedef FreeInfoSet::iterator freeinfo_iterator; 47 typedef FreeInfoSet::const_iterator const_freeinfo_iterator; 48 49 typedef size_t size_type; 50 51public: 52 explicit NamePool(size_type pSize = 3); 53 54 ~NamePool(); 55 56 // ----- modifiers ----- // 57 /// createSymbol - create a symbol but do not insert into the pool. 58 /// The created symbol did not go through the path of symbol resolution. 59 ResolveInfo* createSymbol(const llvm::StringRef& pName, 60 bool pIsDyn, 61 ResolveInfo::Type pType, 62 ResolveInfo::Desc pDesc, 63 ResolveInfo::Binding pBinding, 64 ResolveInfo::SizeType pSize, 65 ResolveInfo::Visibility pVisibility = ResolveInfo::Default); 66 67 /// insertSymbol - insert a symbol and resolve the symbol immediately 68 /// @param pOldInfo - if pOldInfo is not NULL, the old ResolveInfo being 69 /// overriden is kept in pOldInfo. 70 /// @param pResult the result of symbol resultion. 71 /// @note pResult.override is true if the output LDSymbol also need to be 72 /// overriden 73 void insertSymbol(const llvm::StringRef& pName, 74 bool pIsDyn, 75 ResolveInfo::Type pType, 76 ResolveInfo::Desc pDesc, 77 ResolveInfo::Binding pBinding, 78 ResolveInfo::SizeType pSize, 79 LDSymbol::ValueType pValue, 80 ResolveInfo::Visibility pVisibility, 81 ResolveInfo* pOldInfo, 82 Resolver::Result& pResult); 83 84 /// findSymbol - find the resolved output LDSymbol 85 const LDSymbol* findSymbol(const llvm::StringRef& pName) const; 86 LDSymbol* findSymbol(const llvm::StringRef& pName); 87 88 /// findInfo - find the resolved ResolveInfo 89 const ResolveInfo* findInfo(const llvm::StringRef& pName) const; 90 ResolveInfo* findInfo(const llvm::StringRef& pName); 91 92 /// insertString - insert a string 93 /// if the string has existed, modify pString to the existing string 94 /// @return the StringRef points to the hash table 95 llvm::StringRef insertString(const llvm::StringRef& pString); 96 97 // ----- observers ----- // 98 size_type size() const 99 { return m_Table.numOfEntries(); } 100 101 bool empty() const 102 { return m_Table.empty(); } 103 104 // syminfo_iterator - traverse the ResolveInfo in the resolved HashTable 105 syminfo_iterator syminfo_begin() 106 { return m_Table.begin(); } 107 108 syminfo_iterator syminfo_end() 109 { return m_Table.end(); } 110 111 const_syminfo_iterator syminfo_begin() const 112 { return m_Table.begin(); } 113 114 const_syminfo_iterator syminfo_end() const 115 { return m_Table.end(); } 116 117 // freeinfo_iterator - traverse the ResolveInfo those do not need to be 118 // resolved, for example, local symbols 119 freeinfo_iterator freeinfo_begin() 120 { return m_FreeInfoSet.begin(); } 121 122 freeinfo_iterator freeinfo_end() 123 { return m_FreeInfoSet.end(); } 124 125 const_freeinfo_iterator freeinfo_begin() const 126 { return m_FreeInfoSet.begin(); } 127 128 const_freeinfo_iterator freeinfo_end() const 129 { return m_FreeInfoSet.end(); } 130 131 // ----- capacity ----- // 132 void reserve(size_type pN); 133 134 size_type capacity() const; 135 136private: 137 Resolver* m_pResolver; 138 Table m_Table; 139 FreeInfoSet m_FreeInfoSet; 140}; 141 142} // namespace of mcld 143 144#endif 145 146