summaryrefslogtreecommitdiff
path: root/include/mcld/MC/Attribute.h
blob: 1c02505c58e6d88369f94d66f3acadec2cbc5560 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===- Attribute.h --------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_MC_ATTRIBUTE_H
#define MCLD_MC_ATTRIBUTE_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

namespace mcld {

class AttributeSet;

/** \class AttributeBase
 *  \brief AttributeBase provides the real storage for attributes of options.
 *
 *  Attributes are options affecting the link editing of input files.
 *  Some options affects the input files mentioned on the command line after
 *  them. For example, --whole-archive option affects archives mentioned on
 *  the command line after the --whole-archve option. We call such options
 *  "attributes of input files"
 *
 *  AttributeBase is the storage for attributes of input files. Each input
 *  file (@see mcld::Input in MCLinker) has a pointer of an attribute. Since
 *  most attributes of input files are identical, our design lets input files
 *  which have identical attributes share common attribute. AttributeBase is
 *  the shared storage for attribute.
 */
class AttributeBase
{
public:
  AttributeBase()
  : m_WholeArchive(false),
    m_AsNeeded(false),
    m_AddNeeded(true),
    m_Static(false)
  { }

  AttributeBase(const AttributeBase& pBase)
  : m_WholeArchive(pBase.m_WholeArchive),
    m_AsNeeded(pBase.m_AsNeeded),
    m_AddNeeded(pBase.m_AddNeeded),
    m_Static(pBase.m_Static)
  { }

  virtual ~AttributeBase()
  { }

  // ----- observers  ----- //
  // represent GNU ld --whole-archive/--no-whole-archive options
  bool isWholeArchive() const
  { return m_WholeArchive; }

  // represent GNU ld --as-needed/--no-as-needed options
  bool isAsNeeded() const
  { return m_AsNeeded; }

  // represent GNU ld --add-needed/--no-add-needed options
  bool isAddNeeded() const
  { return m_AddNeeded; }

  // represent GNU ld -static option
  bool isStatic() const
  { return m_Static; }

  // represent GNU ld -call_shared option
  bool isDynamic() const
  { return !m_Static; }

public:
  bool m_WholeArchive : 1;
  bool m_AsNeeded : 1;
  bool m_AddNeeded : 1;
  bool m_Static : 1;
};

/** \class Attribute
 *  \brief The base class of attributes. Providing the raw operations of an
 *  attributes
 *
 *  For conventience and producing less bugs, we move the stoarges of attributes
 *  onto AttributeBase, and modifiers remains with the class Attribute.
 */
class Attribute : public AttributeBase
{
public:
  // -----  modifiers  ----- //
  void setWholeArchive()
  { m_WholeArchive = true; }

  void unsetWholeArchive()
  { m_WholeArchive = false; }

  void setAsNeeded()
  { m_AsNeeded = true; }

  void unsetAsNeeded()
  { m_AsNeeded = false; }

  void setAddNeeded()
  { m_AddNeeded = true; }

  void unsetAddNeeded()
  { m_AddNeeded = false; }

  void setStatic()
  { m_Static = true; }

  void setDynamic()
  { m_Static = false; }
};

/** \class AttrConstraint
 *  \brief AttrConstarint is the constraint of a system.
 *
 *  Some systems can not enable certain attributes of a input file.
 *  For example, systems which have no shared libraries can not enable
 *  --call_shared options. We call the ability of enabling attributes
 *  as the constraint of attributes of a system.
 *
 *  Systems enable attributes at the target implementation of SectLinker.
 *
 *  @see SectLinker
 */
class AttrConstraint : public AttributeBase
{
public:
  void enableWholeArchive()
  { m_WholeArchive = true; }

  void disableWholeArchive()
  { m_WholeArchive = false; }

  void enableAsNeeded()
  { m_AsNeeded = true; }

  void disableAsNeeded()
  { m_AsNeeded = false; }

  void enableAddNeeded()
  { m_AddNeeded = true; }

  void disableAddNeeded()
  { m_AddNeeded = false; }

  void setSharedSystem()
  { m_Static = false; }

  void setStaticSystem()
  { m_Static = true; }

  bool isSharedSystem() const
  { return !m_Static; }

  bool isStaticSystem() const
  { return m_Static; }

  bool isLegal(const Attribute& pAttr) const;
};

/** \class AttributeProxy
 *  \brief AttributeProxys is the illusion of private attribute of each
 *  input file.
 *
 *  We designers want to hide the details of sharing common attributes
 *  between input files. We want input files under the illusion that they
 *  have their own private attributes to simplify the linking algorithms.
 *
 *  AttributeProxy hides the reality of sharing. An input file can change
 *  its attribute without explicit searching of existing attributes
 *  as it has a private ownership of the attribute. AttributeProxy does
 *  the searching in the AttributeSet and changes the pointer of
 *  the attribute of the input file. If the searching fails, AttributeProxy
 *  requests a new attribute from the AttributeSet.
 */
class AttributeProxy
{
public:
  AttributeProxy(AttributeSet& pParent,
                 const Attribute& pBase,
                 const AttrConstraint& pConstraint);

  ~AttributeProxy();

  // ----- observers  ----- //
  bool isWholeArchive() const;

  bool isAsNeeded() const;

  bool isAddNeeded() const;

  bool isStatic() const;

  bool isDynamic() const;

  const Attribute* attr() const
  { return m_pBase; }

  // -----  modifiers  ----- //
  void setWholeArchive();
  void unsetWholeArchive();
  void setAsNeeded();
  void unsetAsNeeded();
  void setAddNeeded();
  void unsetAddNeeded();
  void setStatic();
  void setDynamic();

  AttributeProxy& assign(Attribute* pBase);

private:
  AttributeSet &m_AttrPool;
  const Attribute *m_pBase;
  const AttrConstraint& m_Constraint;
};


// -----  comparisons  ----- //
inline bool operator== (const Attribute& pLHS, const Attribute& pRHS)
{
  return ((pLHS.isWholeArchive() == pRHS.isWholeArchive()) &&
    (pLHS.isAsNeeded() == pRHS.isAsNeeded()) &&
    (pLHS.isAddNeeded() == pRHS.isAddNeeded()) &&
    (pLHS.isStatic() == pRHS.isStatic()));
}

inline bool operator!= (const Attribute& pLHS, const Attribute& pRHS)
{
  return !(pLHS == pRHS);
}

} // namespace of mcld

#endif