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