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 "mcld/LD/NamePool.h"
10
11#include "mcld/LD/StaticResolver.h"
12
13#include <llvm/Support/raw_ostream.h>
14
15namespace mcld {
16
17//===----------------------------------------------------------------------===//
18// NamePool
19//===----------------------------------------------------------------------===//
20NamePool::NamePool(NamePool::size_type pSize)
21    : m_pResolver(new StaticResolver()), m_Table(pSize) {
22}
23
24NamePool::~NamePool() {
25  delete m_pResolver;
26
27  FreeInfoSet::iterator info, iEnd = m_FreeInfoSet.end();
28  for (info = m_FreeInfoSet.begin(); info != iEnd; ++info) {
29    ResolveInfo::Destroy(*info);
30  }
31}
32
33/// createSymbol - create a symbol
34ResolveInfo* NamePool::createSymbol(const llvm::StringRef& pName,
35                                    bool pIsDyn,
36                                    ResolveInfo::Type pType,
37                                    ResolveInfo::Desc pDesc,
38                                    ResolveInfo::Binding pBinding,
39                                    ResolveInfo::SizeType pSize,
40                                    ResolveInfo::Visibility pVisibility) {
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                            LDSymbol::ValueType pValue,
63                            ResolveInfo::Visibility pVisibility,
64                            ResolveInfo* pOldInfo,
65                            Resolver::Result& pResult) {
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  } else {
76    exist = false;
77    new_symbol = old_symbol;
78  }
79
80  new_symbol->setIsSymbol(true);
81  new_symbol->setSource(pIsDyn);
82  new_symbol->setType(pType);
83  new_symbol->setDesc(pDesc);
84  new_symbol->setBinding(pBinding);
85  new_symbol->setVisibility(pVisibility);
86  new_symbol->setSize(pSize);
87
88  if (!exist) {
89    // old_symbol is neither existed nor a symbol.
90    pResult.info = new_symbol;
91    pResult.existent = false;
92    pResult.overriden = true;
93    return;
94  } else if (pOldInfo != NULL) {
95    // existent, remember its attribute
96    pOldInfo->override(*old_symbol);
97  }
98
99  // exist and is a symbol
100  // symbol resolution
101  bool override = false;
102  unsigned int action = Resolver::LastAction;
103  if (m_pResolver->resolve(*old_symbol, *new_symbol, override, pValue)) {
104    pResult.info = old_symbol;
105    pResult.existent = true;
106    pResult.overriden = override;
107  } else {
108    m_pResolver->resolveAgain(*this, action, *old_symbol, *new_symbol, pResult);
109  }
110
111  m_Table.getEntryFactory().destroy(new_symbol);
112  return;
113}
114
115llvm::StringRef NamePool::insertString(const llvm::StringRef& pString) {
116  bool exist = false;
117  ResolveInfo* resolve_info = m_Table.insert(pString, exist);
118  return llvm::StringRef(resolve_info->name(), resolve_info->nameSize());
119}
120
121void NamePool::reserve(NamePool::size_type pSize) {
122  m_Table.rehash(pSize);
123}
124
125NamePool::size_type NamePool::capacity() const {
126  return (m_Table.numOfBuckets() - m_Table.numOfEntries());
127}
128
129/// findInfo - find the resolved ResolveInfo
130ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName) {
131  Table::iterator iter = m_Table.find(pName);
132  return iter.getEntry();
133}
134
135/// findInfo - find the resolved ResolveInfo
136const ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName) const {
137  Table::const_iterator iter = m_Table.find(pName);
138  return iter.getEntry();
139}
140
141/// findSymbol - find the resolved output LDSymbol
142LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName) {
143  ResolveInfo* info = findInfo(pName);
144  if (info == NULL)
145    return NULL;
146  return info->outSymbol();
147}
148
149/// findSymbol - find the resolved output LDSymbol
150const LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName) const {
151  const ResolveInfo* info = findInfo(pName);
152  if (info == NULL)
153    return NULL;
154  return info->outSymbol();
155}
156
157}  // namespace mcld
158