summaryrefslogtreecommitdiff
path: root/include/mcld/Target/ELFAttributeValue.h
blob: 0af3c976cc0ff771a3c6ff592dbd39dbee6630b4 (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
//===- ELFAttributeValue.h ------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_TARGET_ELFATTRIBUTEVALUE_H
#define MCLD_TARGET_ELFATTRIBUTEVALUE_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

#include <string>

namespace mcld
{

/** \class ELFAttributeValue
 *  \brief ELFAttributeValue stroes the value of an attribute tag. The attribtue
 *  tag itself is not stored in this object.
 */
class ELFAttributeValue
{
public:
  // Type of value that an attribute tag holds.
  enum Type {
    // The value contains no data and has unknown type.
    Uninitialized = 0,

    // The value contains integer data.
    Int           = 1L << 0,

    // The value contains string data.
    String        = 1L << 1,

    // This is for attribute in which "default value" (0 for int type and empty
    // string for string type) has special meaning for them. That is, the
    // default value is "disabled" and meaningful for those attribute.
    NoDefault     = 1L << 2,
  };

public:
  ELFAttributeValue()
    : m_Type(Uninitialized), m_IntValue(0), m_StringValue() { }

  ~ELFAttributeValue() { }

public:
  unsigned int type() const
  { return m_Type; }

  void setType(unsigned int pType)
  { m_Type = pType; }

  unsigned int getIntValue() const
  { return m_IntValue; }

  void setIntValue(unsigned int pIntValue)
  { m_IntValue = pIntValue; }

  const std::string &getStringValue() const
  { return m_StringValue; }

  void setStringValue(const std::string &pStringValue)
  { m_StringValue = pStringValue; }

  void setStringValue(const char *pStringValue, size_t pSize)
  { m_StringValue.assign(pStringValue, pSize); }

  void setStringValue(const char *pStringValue)
  { m_StringValue.assign(pStringValue); }

  size_t getSize() const;

  inline bool isUninitialized() const
  { return (m_Type == Uninitialized); }

  inline bool isInitialized() const
  { return !isUninitialized(); }

  inline bool isIntValue() const
  { return (m_Type & Int); }

  inline bool isStringValue() const
  { return (m_Type & String); }

  inline bool hasNoDefault() const
  { return (m_Type & NoDefault); }

  bool isDefaultValue() const;

  // Returns true if this attribute value should be emitted to the output.
  inline bool shouldEmit() const {
    // Attribute with non-default value should be emitted.
    return !isDefaultValue();
  }

  bool equals(const ELFAttributeValue& pValue) const;

  bool operator==(const ELFAttributeValue& pValue) const
  { return equals(pValue); }
  bool operator!=(const ELFAttributeValue& pValue) const
  { return !equals(pValue); }

  /// reset - reset this value to the uninitialized state
  void reset()
  {
    m_Type = Uninitialized;
    m_IntValue = 0;
    m_StringValue.clear();
    return;
  }

private:
  unsigned int m_Type;

  unsigned int m_IntValue;
  std::string m_StringValue;
};

} // namespace of mcld

#endif