retdec
dotnet_data_types.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_FILEFORMAT_TYPES_DOTNET_TYPES_DOTNET_DATA_TYPES_H
8 #define RETDEC_FILEFORMAT_TYPES_DOTNET_TYPES_DOTNET_DATA_TYPES_H
9 
10 #include <memory>
11 #include <unordered_map>
12 #include <vector>
13 
15 
16 namespace retdec {
17 namespace fileformat {
18 
19 class DotnetClass;
20 
21 enum class ElementType
22 {
23  End = 0x00,
24  Void = 0x01,
25  Boolean = 0x02,
26  Char = 0x03,
27  Int8 = 0x04,
28  UInt8 = 0x05,
29  Int16 = 0x06,
30  UInt16 = 0x07,
31  Int32 = 0x08,
32  UInt32 = 0x09,
33  Int64 = 0x0A,
34  UInt64 = 0x0B,
35  Float32 = 0x0C,
36  Float64 = 0x0D,
37  String = 0x0E,
38  Ptr = 0x0F,
39  ByRef = 0x10,
40  ValueType = 0x11,
41  Class = 0x12,
42  GenericVar = 0x13,
43  Array = 0x14,
44  GenericInst = 0x15,
45  TypedByRef = 0x16,
46  IntPtr = 0x18,
47  UIntPtr = 0x19,
48  FnPtr = 0x1B,
49  Object = 0x1C,
50  SzArray = 0x1D,
51  GenericMVar = 0x1E,
52  CModRequired = 0x1F,
53  CModOptional = 0x20,
54  Internal = 0x21,
55  Modifier = 0x40,
56  Sentinel = 0x41,
57  Pinned = 0x45,
58  MetaType = 0x50,
59  BoxedObject = 0x51,
60  CustomField = 0x53,
61  CustomProperty = 0x54,
62  CustomEnum = 0x55
63 };
64 
66 {
67  protected:
69  public:
70  DotnetDataTypeBase(ElementType elementType) : type(elementType) {}
71  virtual ~DotnetDataTypeBase() = default;
72 
75  virtual std::string getText() const = 0;
77 
80  ElementType getElementType() const { return type; }
82 };
83 
84 template <ElementType Type>
86 {
87  public:
89 
90  virtual std::string getText() const override
91  {
92  static const std::unordered_map<ElementType, std::string, retdec::utils::EnumClassKeyHash> typeNames =
93  {
94  { ElementType::Void, "void" },
95  { ElementType::Boolean, "bool" },
96  { ElementType::Char, "char" },
97  { ElementType::Int8, "sbyte" },
98  { ElementType::UInt8, "byte" },
99  { ElementType::Int16, "short" },
100  { ElementType::UInt16, "ushort" },
101  { ElementType::Int32, "int" },
102  { ElementType::UInt32, "uint" },
103  { ElementType::Int64, "long" },
104  { ElementType::UInt64, "ulong" },
105  { ElementType::Float32, "float" },
106  { ElementType::Float64, "double" },
107  { ElementType::String, "string" },
108  { ElementType::IntPtr, "IntPtr" },
109  { ElementType::UIntPtr, "UIntPtr" },
110  { ElementType::Object, "object" },
111  { ElementType::TypedByRef, "TypedReference" }
112  };
113 
114  auto itr = typeNames.find(Type);
115  if (itr == typeNames.end())
116  return {};
117 
118  return itr->second;
119  }
120 };
121 
122 template <>
124 {
125  private:
126  std::unique_ptr<DotnetDataTypeBase> pointed;
127  public:
128  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& pointedType)
129  : DotnetDataTypeBase(ElementType::Ptr), pointed(std::move(pointedType)) {}
130 
131  virtual std::string getText() const override;
132 
133  const DotnetDataTypeBase* getPointedType() const { return pointed.get(); }
134 };
135 
136 template <>
138 {
139  private:
140  std::unique_ptr<DotnetDataTypeBase> referred;
141  public:
142  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& referredType)
143  : DotnetDataTypeBase(ElementType::ByRef), referred(std::move(referredType)) {}
144 
145  virtual std::string getText() const override;
146 
147  const DotnetDataTypeBase* getReferredType() const { return referred.get(); }
148 };
149 
150 template <>
152 {
153  private:
155  public:
156  DotnetDataType(const DotnetClass* classType)
157  : DotnetDataTypeBase(ElementType::ValueType), type(classType) {}
158 
159  virtual std::string getText() const override;
160 
161  const DotnetClass* getType() const { return type; }
162 };
163 
164 template <>
166 {
167  private:
169  public:
170  DotnetDataType(const DotnetClass* classType)
171  : DotnetDataTypeBase(ElementType::Class), type(classType) {}
172 
173  virtual std::string getText() const override;
174 
175  const DotnetClass* getType() const { return type; }
176 };
177 
178 template <>
180 {
181  private:
182  const std::string* genericVar;
183  public:
184  DotnetDataType(const std::string* varGenericVar)
185  : DotnetDataTypeBase(ElementType::GenericVar), genericVar(varGenericVar) {}
186 
187  virtual std::string getText() const override;
188 
189  const std::string& getGenericVariable() const { return *genericVar; }
190 };
191 
192 template <>
194 {
195  private:
196  std::unique_ptr<DotnetDataTypeBase> underlyingType;
197  std::vector<std::pair<std::int64_t, std::int64_t>> dimensions;
198  public:
199  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& arrayUnderlyingType, std::vector<std::pair<std::int64_t, std::int64_t>>&& arrayDimensions)
200  : DotnetDataTypeBase(ElementType::Array), underlyingType(std::move(arrayUnderlyingType)), dimensions(std::move(arrayDimensions)) {}
201 
202  virtual std::string getText() const override;
203 
204  const DotnetDataTypeBase* getUnderlyingType() const { return underlyingType.get(); }
205  const std::vector<std::pair<std::int64_t, std::int64_t>>& getDimensions() const { return dimensions; }
206 };
207 
208 template <>
210 {
211  private:
212  std::unique_ptr<DotnetDataTypeBase> type;
213  std::vector<std::unique_ptr<DotnetDataTypeBase>> genericTypes;
214  public:
215  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& instType, std::vector<std::unique_ptr<DotnetDataTypeBase>>&& instGenericTypes)
216  : DotnetDataTypeBase(ElementType::GenericInst), type(std::move(instType)), genericTypes(std::move(instGenericTypes)) {}
217 
218  virtual std::string getText() const override;
219 
220  const DotnetDataTypeBase* getType() const { return type.get(); }
221  std::size_t getGenericCount() const { return genericTypes.size(); }
222  const std::vector<std::unique_ptr<DotnetDataTypeBase>>& getGenericTypes() const { return genericTypes; }
223 };
224 
225 template <>
227 {
228  private:
229  std::unique_ptr<DotnetDataTypeBase> returnType;
230  std::vector<std::unique_ptr<DotnetDataTypeBase>> paramTypes;
231  public:
232  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& fnReturnType, std::vector<std::unique_ptr<DotnetDataTypeBase>>&& fnParamTypes)
233  : DotnetDataTypeBase(ElementType::FnPtr), returnType(std::move(fnReturnType)), paramTypes(std::move(fnParamTypes)) {}
234 
235  virtual std::string getText() const override;
236 
237  const DotnetDataTypeBase* getReturnType() const { return returnType.get(); }
238  const std::vector<std::unique_ptr<DotnetDataTypeBase>>& getParameterTypes() const { return paramTypes; }
239 };
240 
241 template <>
243 {
244  private:
245  std::unique_ptr<DotnetDataTypeBase> underlyingType;
246  public:
247  DotnetDataType(std::unique_ptr<DotnetDataTypeBase>&& arrayUnderlyingType)
248  : DotnetDataTypeBase(ElementType::SzArray), underlyingType(std::move(arrayUnderlyingType)) {}
249 
250  virtual std::string getText() const override;
251 
252  const DotnetDataTypeBase* getUnderlyingType() const { return underlyingType.get(); }
253 };
254 
255 template <>
257 {
258  private:
259  const std::string* genericVar;
260  public:
261  DotnetDataType(const std::string* mvarGenericVar)
262  : DotnetDataTypeBase(ElementType::GenericMVar), genericVar(mvarGenericVar) {}
263 
264  virtual std::string getText() const override;
265 
266  const std::string& getGenericVariable() const { return *genericVar; }
267 };
268 
269 template <>
271 {
272  private:
274  std::unique_ptr<DotnetDataTypeBase> type;
275  public:
276  DotnetDataType(const DotnetClass* typeModifier, std::unique_ptr<DotnetDataTypeBase>&& modifierType)
277  : DotnetDataTypeBase(ElementType::CModRequired), modifier(typeModifier), type(std::move(modifierType)) {}
278 
279  virtual std::string getText() const override;
280 
281  const DotnetClass* getModifier() const { return modifier; }
282  const DotnetDataTypeBase* getType() const { return type.get(); }
283 };
284 
285 template <>
287 {
288  private:
290  std::unique_ptr<DotnetDataTypeBase> type;
291  public:
292  DotnetDataType(const DotnetClass* typeModifier, std::unique_ptr<DotnetDataTypeBase>&& modifierType)
293  : DotnetDataTypeBase(ElementType::CModRequired), modifier(typeModifier), type(std::move(modifierType)) {}
294 
295  virtual std::string getText() const override;
296 
297  const DotnetClass* getModifier() const { return modifier; }
298  const DotnetDataTypeBase* getType() const { return type.get(); }
299 };
300 
332 //using DotnetDataTypeInternal = DotnetDataType<ElementType::Internal>;
333 //using DotnetDataTypeModifier = DotnetDataType<ElementType::Modifier>;
334 //using DotnetDataTypeSentinel = DotnetDataType<ElementType::Sentinel>;
335 //using DotnetDataTypePinned = DotnetDataType<ElementType::Pinned>;
336 //using DotnetDataTypeMetaType = DotnetDataType<ElementType::MetaType>;
337 //using DotnetDataTypeBoxedObject = DotnetDataType<ElementType::BoxedObject>;
338 //using DotnetDataTypeCustomField = DotnetDataType<ElementType::CustomField>;
339 //using DotnetDataTypeCustomProperty = DotnetDataType<ElementType::CustomProperty>;
340 //using DotnetDataTypeCustomEnum = DotnetDataType<ElementType::CustomEnum>;
341 
342 } // namespace fileformat
343 } // namespace retdec
344 
345 #endif
Definition: dotnet_class.h:27
Definition: dotnet_data_types.h:66
ElementType getElementType() const
Definition: dotnet_data_types.h:80
DotnetDataTypeBase(ElementType elementType)
Definition: dotnet_data_types.h:70
virtual std::string getText() const =0
ElementType type
Definition: dotnet_data_types.h:68
std::vector< std::pair< std::int64_t, std::int64_t > > dimensions
Definition: dotnet_data_types.h:197
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&arrayUnderlyingType, std::vector< std::pair< std::int64_t, std::int64_t >> &&arrayDimensions)
Definition: dotnet_data_types.h:199
const DotnetDataTypeBase * getUnderlyingType() const
Definition: dotnet_data_types.h:204
std::unique_ptr< DotnetDataTypeBase > underlyingType
Definition: dotnet_data_types.h:196
const std::vector< std::pair< std::int64_t, std::int64_t > > & getDimensions() const
Definition: dotnet_data_types.h:205
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&referredType)
Definition: dotnet_data_types.h:142
std::unique_ptr< DotnetDataTypeBase > referred
Definition: dotnet_data_types.h:140
const DotnetDataTypeBase * getReferredType() const
Definition: dotnet_data_types.h:147
const DotnetClass * getModifier() const
Definition: dotnet_data_types.h:297
const DotnetClass * modifier
Definition: dotnet_data_types.h:289
const DotnetDataTypeBase * getType() const
Definition: dotnet_data_types.h:298
DotnetDataType(const DotnetClass *typeModifier, std::unique_ptr< DotnetDataTypeBase > &&modifierType)
Definition: dotnet_data_types.h:292
std::unique_ptr< DotnetDataTypeBase > type
Definition: dotnet_data_types.h:290
const DotnetClass * modifier
Definition: dotnet_data_types.h:273
const DotnetClass * getModifier() const
Definition: dotnet_data_types.h:281
const DotnetDataTypeBase * getType() const
Definition: dotnet_data_types.h:282
DotnetDataType(const DotnetClass *typeModifier, std::unique_ptr< DotnetDataTypeBase > &&modifierType)
Definition: dotnet_data_types.h:276
std::unique_ptr< DotnetDataTypeBase > type
Definition: dotnet_data_types.h:274
const DotnetClass * type
Definition: dotnet_data_types.h:168
const DotnetClass * getType() const
Definition: dotnet_data_types.h:175
DotnetDataType(const DotnetClass *classType)
Definition: dotnet_data_types.h:170
const std::vector< std::unique_ptr< DotnetDataTypeBase > > & getParameterTypes() const
Definition: dotnet_data_types.h:238
std::vector< std::unique_ptr< DotnetDataTypeBase > > paramTypes
Definition: dotnet_data_types.h:230
const DotnetDataTypeBase * getReturnType() const
Definition: dotnet_data_types.h:237
std::unique_ptr< DotnetDataTypeBase > returnType
Definition: dotnet_data_types.h:229
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&fnReturnType, std::vector< std::unique_ptr< DotnetDataTypeBase >> &&fnParamTypes)
Definition: dotnet_data_types.h:232
std::vector< std::unique_ptr< DotnetDataTypeBase > > genericTypes
Definition: dotnet_data_types.h:213
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&instType, std::vector< std::unique_ptr< DotnetDataTypeBase >> &&instGenericTypes)
Definition: dotnet_data_types.h:215
const std::vector< std::unique_ptr< DotnetDataTypeBase > > & getGenericTypes() const
Definition: dotnet_data_types.h:222
std::unique_ptr< DotnetDataTypeBase > type
Definition: dotnet_data_types.h:212
std::size_t getGenericCount() const
Definition: dotnet_data_types.h:221
const DotnetDataTypeBase * getType() const
Definition: dotnet_data_types.h:220
const std::string * genericVar
Definition: dotnet_data_types.h:259
const std::string & getGenericVariable() const
Definition: dotnet_data_types.h:266
DotnetDataType(const std::string *mvarGenericVar)
Definition: dotnet_data_types.h:261
const std::string & getGenericVariable() const
Definition: dotnet_data_types.h:189
const std::string * genericVar
Definition: dotnet_data_types.h:182
DotnetDataType(const std::string *varGenericVar)
Definition: dotnet_data_types.h:184
std::unique_ptr< DotnetDataTypeBase > pointed
Definition: dotnet_data_types.h:126
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&pointedType)
Definition: dotnet_data_types.h:128
const DotnetDataTypeBase * getPointedType() const
Definition: dotnet_data_types.h:133
DotnetDataType(std::unique_ptr< DotnetDataTypeBase > &&arrayUnderlyingType)
Definition: dotnet_data_types.h:247
const DotnetDataTypeBase * getUnderlyingType() const
Definition: dotnet_data_types.h:252
std::unique_ptr< DotnetDataTypeBase > underlyingType
Definition: dotnet_data_types.h:245
const DotnetClass * getType() const
Definition: dotnet_data_types.h:161
const DotnetClass * type
Definition: dotnet_data_types.h:154
DotnetDataType(const DotnetClass *classType)
Definition: dotnet_data_types.h:156
Definition: dotnet_data_types.h:86
virtual std::string getText() const override
Definition: dotnet_data_types.h:90
DotnetDataType()
Definition: dotnet_data_types.h:88
Definition: string.h:22
Classes for metadata tables.
ElementType
Definition: dotnet_data_types.h:22
Definition: archive_wrapper.h:19