NamePool.cpp revision 6f75755c9204b1d8817ae5a65a2f7e5af0ec3f70
1//===- NamePool.cpp -------------------------------------------------------===//
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#include <llvm/Support/raw_ostream.h>
10#include <mcld/LD/NamePool.h>
11#include <mcld/LD/StaticResolver.h>
12
13using namespace mcld;
14
15//===----------------------------------------------------------------------===//
16// NamePool
17//===----------------------------------------------------------------------===//
18NamePool::NamePool(NamePool::size_type pSize)
19  : m_pResolver(new StaticResolver()), m_Table(pSize) {
20}
21
22NamePool::~NamePool()
23{
24  delete m_pResolver;
25
26  FreeInfoSet::iterator info, iEnd = m_FreeInfoSet.end();
27  for (info = m_FreeInfoSet.begin(); info != iEnd; ++info) {
28    ResolveInfo::Destroy(*info);
29  }
30}
31
32/// createSymbol - create a symbol
33ResolveInfo* NamePool::createSymbol(const llvm::StringRef& pName,
34                                    bool pIsDyn,
35                                    ResolveInfo::Type pType,
36                                    ResolveInfo::Desc pDesc,
37                                    ResolveInfo::Binding pBinding,
38                                    ResolveInfo::SizeType pSize,
39                                    ResolveInfo::Visibility pVisibility)
40{
41  ResolveInfo** result = m_FreeInfoSet.allocate();
42  (*result) = ResolveInfo::Create(pName);
43  (*result)->setIsSymbol(true);
44  (*result)->setSource(pIsDyn);
45  (*result)->setType(pType);
46  (*result)->setDesc(pDesc);
47  (*result)->setBinding(pBinding);
48  (*result)->setVisibility(pVisibility);
49  (*result)->setSize(pSize);
50  return *result;
51}
52
53/// insertSymbol - insert a symbol and resolve it immediately
54/// @return the pointer of resolved ResolveInfo
55/// @return is the symbol existent?
56void NamePool::insertSymbol(const llvm::StringRef& pName,
57                              bool pIsDyn,
58                              ResolveInfo::Type pType,
59                              ResolveInfo::Desc pDesc,
60                              ResolveInfo::Binding pBinding,
61                              ResolveInfo::SizeType pSize,
62                              ResolveInfo::Visibility pVisibility,
63                              ResolveInfo* pOldInfo,
64                              Resolver::Result& pResult)
65{
66  // We should check if there is any symbol with the same name existed.
67  // If it already exists, we should use resolver to decide which symbol
68  // should be reserved. Otherwise, we insert the symbol and set up its
69  // attributes.
70  bool exist = false;
71  ResolveInfo* old_symbol = m_Table.insert(pName, exist);
72  ResolveInfo* new_symbol = NULL;
73  if (exist && old_symbol->isSymbol()) {
74    new_symbol = m_Table.getEntryFactory().produce(pName);
75  }
76  else {
77    exist = false;
78    new_symbol = old_symbol;
79  }
80
81  new_symbol->setIsSymbol(true);
82  new_symbol->setSource(pIsDyn);
83  new_symbol->setType(pType);
84  new_symbol->setDesc(pDesc);
85  new_symbol->setBinding(pBinding);
86  new_symbol->setVisibility(pVisibility);
87  new_symbol->setSize(pSize);
88
89  if (!exist) {
90    // old_symbol is neither existed nor a symbol.
91    pResult.info      = new_symbol;
92    pResult.existent  = false;
93    pResult.overriden = true;
94    return;
95  }
96  else if (NULL != pOldInfo) {
97    // existent, remember its attribute
98    pOldInfo->override(*old_symbol);
99  }
100
101  // exist and is a symbol
102  // symbol resolution
103  bool override = false;
104  unsigned int action = Resolver::LastAction;
105  if (m_pResolver->resolve(*old_symbol, *new_symbol, override)) {
106    pResult.info      = old_symbol;
107    pResult.existent  = true;
108    pResult.overriden = override;
109  }
110  else {
111      m_pResolver->resolveAgain(*this, action, *old_symbol, *new_symbol, pResult);
112  }
113
114  m_Table.getEntryFactory().destroy(new_symbol);
115  return;
116}
117
118llvm::StringRef NamePool::insertString(const llvm::StringRef& pString)
119{
120  bool exist = false;
121  ResolveInfo* resolve_info = m_Table.insert(pString, exist);
122  return llvm::StringRef(resolve_info->name(), resolve_info->nameSize());
123}
124
125void NamePool::reserve(NamePool::size_type pSize)
126{
127  m_Table.rehash(pSize);
128}
129
130NamePool::size_type NamePool::capacity() const
131{
132  return (m_Table.numOfBuckets() - m_Table.numOfEntries());
133}
134
135/// findInfo - find the resolved ResolveInfo
136ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName)
137{
138  Table::iterator iter = m_Table.find(pName);
139  return iter.getEntry();
140}
141
142/// findInfo - find the resolved ResolveInfo
143const ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName) const
144{
145  Table::const_iterator iter = m_Table.find(pName);
146  return iter.getEntry();
147}
148
149/// findSymbol - find the resolved output LDSymbol
150LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName)
151{
152  ResolveInfo* info = findInfo(pName);
153  if (NULL == info)
154    return NULL;
155  return info->outSymbol();
156}
157
158/// findSymbol - find the resolved output LDSymbol
159const LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName) const
160{
161  const ResolveInfo* info = findInfo(pName);
162  if (NULL == info)
163    return NULL;
164  return info->outSymbol();
165}
166
167