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_MC_SYMBOLCATEGORY_H_
10#define MCLD_MC_SYMBOLCATEGORY_H_
11#include <cstddef>
12#include <vector>
13
14namespace mcld {
15
16class LDSymbol;
17class ResolveInfo;
18/** \class SymbolCategory
19 *  \brief SymbolCategory groups output LDSymbol into different categories.
20 */
21class SymbolCategory {
22 private:
23  typedef std::vector<LDSymbol*> OutputSymbols;
24
25 public:
26  typedef OutputSymbols::iterator iterator;
27  typedef OutputSymbols::const_iterator const_iterator;
28
29 public:
30  SymbolCategory();
31
32  ~SymbolCategory();
33
34  // -----  modifiers  ----- //
35  SymbolCategory& add(LDSymbol& pSymbol);
36
37  SymbolCategory& forceLocal(LDSymbol& pSymbol);
38
39  SymbolCategory& arrange(LDSymbol& pSymbol, const ResolveInfo& pSourceInfo);
40
41  SymbolCategory& changeCommonsToGlobal();
42
43  SymbolCategory& changeToDynamic(LDSymbol& pSymbol);
44
45  // -----  access  ----- //
46  LDSymbol& at(size_t pPosition) { return *m_OutputSymbols.at(pPosition); }
47
48  const LDSymbol& at(size_t pPosition) const {
49    return *m_OutputSymbols.at(pPosition);
50  }
51
52  LDSymbol& operator[](size_t pPosition) { return *m_OutputSymbols[pPosition]; }
53
54  const LDSymbol& operator[](size_t pPosition) const {
55    return *m_OutputSymbols[pPosition];
56  }
57
58  // -----  observers  ----- //
59  size_t numOfSymbols() const;
60
61  size_t numOfFiles() const;
62
63  size_t numOfLocals() const;
64
65  size_t numOfLocalDyns() const;
66
67  size_t numOfCommons() const;
68
69  size_t numOfDynamics() const;
70
71  size_t numOfRegulars() const;
72
73  bool empty() const;
74
75  bool emptyFiles() const;
76
77  bool emptyLocals() const;
78
79  bool emptyLocalDyns() const;
80
81  bool emptyCommons() const;
82
83  bool emptyDynamics() const;
84
85  bool emptyRegulars() const;
86
87  // -----  iterators  ----- //
88  iterator begin();
89  iterator end();
90  const_iterator begin() const;
91  const_iterator end() const;
92
93  iterator fileBegin();
94  iterator fileEnd();
95  const_iterator fileBegin() const;
96  const_iterator fileEnd() const;
97
98  iterator localBegin();
99  iterator localEnd();
100  const_iterator localBegin() const;
101  const_iterator localEnd() const;
102
103  iterator localDynBegin();
104  iterator localDynEnd();
105  const_iterator localDynBegin() const;
106  const_iterator localDynEnd() const;
107
108  iterator commonBegin();
109  iterator commonEnd();
110  const_iterator commonBegin() const;
111  const_iterator commonEnd() const;
112
113  iterator dynamicBegin();
114  iterator dynamicEnd();
115  const_iterator dynamicBegin() const;
116  const_iterator dynamicEnd() const;
117
118  iterator regularBegin();
119  iterator regularEnd();
120  const_iterator regularBegin() const;
121  const_iterator regularEnd() const;
122
123 private:
124  class Category {
125   public:
126    enum Type { File, Local, LocalDyn, Common, Dynamic, Regular };
127
128   public:
129    Type type;
130
131    size_t begin;
132    size_t end;
133
134    Category* prev;
135    Category* next;
136
137   public:
138    explicit Category(Type pType)
139        : type(pType), begin(0), end(0), prev(NULL), next(NULL) {}
140
141    size_t size() const { return (end - begin); }
142
143    bool empty() const { return (begin == end); }
144
145    bool isFirst() const { return (prev == NULL); }
146
147    bool isLast() const { return (next == NULL); }
148
149    static Type categorize(const ResolveInfo& pInfo);
150  };
151
152 private:
153  SymbolCategory& add(LDSymbol& pSymbol, Category::Type pTarget);
154
155  SymbolCategory& arrange(LDSymbol& pSymbol,
156                          Category::Type pSource,
157                          Category::Type pTarget);
158
159 private:
160  OutputSymbols m_OutputSymbols;
161
162  Category* m_pFile;
163  Category* m_pLocal;
164  Category* m_pLocalDyn;
165  Category* m_pCommon;
166  Category* m_pDynamic;
167  Category* m_pRegular;
168};
169
170}  // namespace mcld
171
172#endif  // MCLD_MC_SYMBOLCATEGORY_H_
173