summaryrefslogtreecommitdiff
path: root/include/mcld/MC/SymbolCategory.h
blob: 700fba2649ce18ab5d50c894e2ddd26ca661c762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===- SymbolCategory.h ---------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_SYMBOL_CATEGORY_H
#define MCLD_SYMBOL_CATEGORY_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif
#include <cstddef>
#include <vector>

namespace mcld
{

class LDSymbol;
class ResolveInfo;
/** \class SymbolCategory
 *  \brief SymbolCategory groups output LDSymbol into different categories.
 */
class SymbolCategory
{
private:
  typedef std::vector<LDSymbol*> OutputSymbols;

public:
  typedef OutputSymbols::iterator iterator;
  typedef OutputSymbols::const_iterator const_iterator;

public:
  SymbolCategory();

  ~SymbolCategory();

  // -----  modifiers  ----- //
  SymbolCategory& add(LDSymbol& pSymbol);

  SymbolCategory& forceLocal(LDSymbol& pSymbol);

  SymbolCategory& arrange(LDSymbol& pSymbol, const ResolveInfo& pSourceInfo);

  SymbolCategory& changeCommonsToGlobal();

  SymbolCategory& changeLocalToDynamic(const LDSymbol& pSymbol);

  // -----  access  ----- //
  LDSymbol& at(size_t pPosition)
  { return *m_OutputSymbols.at(pPosition); }

  const LDSymbol& at(size_t pPosition) const
  { return *m_OutputSymbols.at(pPosition); }

  LDSymbol& operator[](size_t pPosition)
  { return *m_OutputSymbols[pPosition]; }

  const LDSymbol& operator[](size_t pPosition) const
  { return *m_OutputSymbols[pPosition]; }

  // -----  observers  ----- //
  size_t numOfSymbols() const;

  size_t numOfFiles() const;

  size_t numOfLocals() const;

  size_t numOfLocalDyns() const;

  size_t numOfCommons() const;

  size_t numOfDynamics() const;

  size_t numOfRegulars() const;

  bool empty() const;

  bool emptyFiles() const;

  bool emptyLocals() const;

  bool emptyLocalDyns() const;

  bool emptyCommons() const;

  bool emptyDynamics() const;

  bool emptyRegulars() const;

  // -----  iterators  ----- //
  iterator begin();
  iterator end();
  const_iterator begin() const;
  const_iterator end() const;

  iterator fileBegin();
  iterator fileEnd();
  const_iterator fileBegin() const;
  const_iterator fileEnd() const;

  iterator localBegin();
  iterator localEnd();
  const_iterator localBegin() const;
  const_iterator localEnd() const;

  iterator localDynBegin();
  iterator localDynEnd();
  const_iterator localDynBegin() const;
  const_iterator localDynEnd() const;

  iterator commonBegin();
  iterator commonEnd();
  const_iterator commonBegin() const;
  const_iterator commonEnd() const;

  iterator dynamicBegin();
  iterator dynamicEnd();
  const_iterator dynamicBegin() const;
  const_iterator dynamicEnd() const;

  iterator regularBegin();
  iterator regularEnd();
  const_iterator regularBegin() const;
  const_iterator regularEnd() const;

private:
  class Category
  {
  public:
    enum Type {
      File,
      Local,
      LocalDyn,
      Common,
      Dynamic,
      Regular
    };

  public:
    Type type;

    size_t begin;
    size_t end;

    Category* prev;
    Category* next;

  public:
    Category(Type pType)
      : type(pType),
        begin(0),
        end(0),
        prev(NULL),
        next(NULL) {
    }

    size_t size() const
    { return (end - begin); }

    bool empty() const
    { return (begin == end); }

    bool isFirst() const
    { return (NULL == prev); }

    bool isLast() const
    { return (NULL == next); }

    static Type categorize(const ResolveInfo& pInfo);
  };

private:
  SymbolCategory& add(LDSymbol& pSymbol, Category::Type pTarget);

private:
  OutputSymbols m_OutputSymbols;

  Category* m_pFile;
  Category* m_pLocal;
  Category* m_pLocalDyn;
  Category* m_pCommon;
  Category* m_pDynamic;
  Category* m_pRegular;
};

} // namespace of mcld

#endif