forked from xoofx/CppAst.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppPrimitiveType.cs
More file actions
278 lines (244 loc) · 10.4 KB
/
Copy pathCppPrimitiveType.cs
File metadata and controls
278 lines (244 loc) · 10.4 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
namespace CppAst
{
/// <summary>
/// A C++ primitive type (e.g `int`, `void`, `bool`...)
/// </summary>
public sealed class CppPrimitiveType : CppType
{
/// <summary>
/// Singleton instance of the `void` type.
/// </summary>
public static readonly CppPrimitiveType Void = new CppPrimitiveType(CppPrimitiveKind.Void);
/// <summary>
/// Singleton instance of the `bool` type.
/// </summary>
public static readonly CppPrimitiveType Bool = new CppPrimitiveType(CppPrimitiveKind.Bool);
/// <summary>
/// Singleton instance of the `wchar` type.
/// </summary>
public static readonly CppPrimitiveType WChar = new CppPrimitiveType(CppPrimitiveKind.WChar);
/// <summary>
/// Singleton instance of the `char` type.
/// </summary>
public static readonly CppPrimitiveType Char = new CppPrimitiveType(CppPrimitiveKind.Char);
/// <summary>
/// Singleton instance of the `short` type.
/// </summary>
public static readonly CppPrimitiveType Short = new CppPrimitiveType(CppPrimitiveKind.Short);
/// <summary>
/// Singleton instance of the `int` type.
/// </summary>
public static readonly CppPrimitiveType Int = new CppPrimitiveType(CppPrimitiveKind.Int);
/// <summary>
/// Singleton instance of the `long` type.
/// </summary>
public static readonly CppPrimitiveType Long = new CppPrimitiveType(CppPrimitiveKind.Long);
/// <summary>
/// Singleton instance of the `long long` type.
/// </summary>
public static readonly CppPrimitiveType LongLong = new CppPrimitiveType(CppPrimitiveKind.LongLong);
/// <summary>
/// Singleton instance of the `unsigned char` type.
/// </summary>
public static readonly CppPrimitiveType UnsignedChar = new CppPrimitiveType(CppPrimitiveKind.UnsignedChar);
/// <summary>
/// Singleton instance of the `unsigned short` type.
/// </summary>
public static readonly CppPrimitiveType UnsignedShort = new CppPrimitiveType(CppPrimitiveKind.UnsignedShort);
/// <summary>
/// Singleton instance of the `unsigned int` type.
/// </summary>
public static readonly CppPrimitiveType UnsignedInt = new CppPrimitiveType(CppPrimitiveKind.UnsignedInt);
/// <summary>
/// Singleton instance of the `unsigned long` type.
/// </summary>
public static readonly CppPrimitiveType UnsignedLong = new CppPrimitiveType(CppPrimitiveKind.UnsignedLong);
/// <summary>
/// Singleton instance of the `unsigned long long` type.
/// </summary>
public static readonly CppPrimitiveType UnsignedLongLong = new CppPrimitiveType(CppPrimitiveKind.UnsignedLongLong);
/// <summary>
/// Singleton instance of the `float` type.
/// </summary>
public static readonly CppPrimitiveType Float = new CppPrimitiveType(CppPrimitiveKind.Float);
/// <summary>
/// Singleton instance of the `float` type.
/// </summary>
public static readonly CppPrimitiveType Double = new CppPrimitiveType(CppPrimitiveKind.Double);
/// <summary>
/// Singleton instance of the `long double` type.
/// </summary>
public static readonly CppPrimitiveType LongDouble = new CppPrimitiveType(CppPrimitiveKind.LongDouble);
/// <summary>
/// ObjC `id` type.
/// </summary>
public static readonly CppPrimitiveType ObjCObject = new CppPrimitiveType(CppPrimitiveKind.ObjCObject);
/// <summary>
/// ObjC `SEL` type.
/// </summary>
public static readonly CppPrimitiveType ObjCSel = new CppPrimitiveType(CppPrimitiveKind.ObjCSel);
/// <summary>
/// ObjC `Class` type.
/// </summary>
public static readonly CppPrimitiveType ObjCClass = new CppPrimitiveType(CppPrimitiveKind.ObjCClass);
/// <summary>
/// Unsigned 128 bits integer type.
/// </summary>
public static readonly CppPrimitiveType UInt128 = new CppPrimitiveType(CppPrimitiveKind.UInt128);
/// <summary>
/// 128 bits integer type.
/// </summary>
public static readonly CppPrimitiveType Int128 = new CppPrimitiveType(CppPrimitiveKind.Int128);
/// <summary>
/// Float16 type.
/// </summary>
public static readonly CppPrimitiveType Float16 = new CppPrimitiveType(CppPrimitiveKind.Float16);
/// <summary>
/// BFloat16 type.
/// </summary>
public static readonly CppPrimitiveType BFloat16 = new CppPrimitiveType(CppPrimitiveKind.BFloat16);
/// <summary>
/// A IntPtr type (Not directly a C++ type, but used during remapping/codegen).
/// </summary>
public static readonly CppPrimitiveType IntPtr = new CppPrimitiveType(CppPrimitiveKind.IntPtr);
/// <summary>
/// A UIntPtr type (Not directly a C++ type, but used during remapping/codegen).
/// </summary>
public static readonly CppPrimitiveType UIntPtr = new CppPrimitiveType(CppPrimitiveKind.UIntPtr);
private readonly int _sizeOf;
/// <summary>
/// Base constructor of a primitive
/// </summary>
/// <param name="kind"></param>
private CppPrimitiveType(CppPrimitiveKind kind) : base(CppTypeKind.Primitive)
{
Kind = kind;
UpdateSize(out _sizeOf);
}
/// <summary>
/// The kind of primitive.
/// </summary>
public CppPrimitiveKind Kind { get; }
private void UpdateSize(out int sizeOf)
{
switch (Kind)
{
case CppPrimitiveKind.Void:
sizeOf = 0;
break;
case CppPrimitiveKind.Bool:
sizeOf = 1;
break;
case CppPrimitiveKind.WChar:
sizeOf = 2;
break;
case CppPrimitiveKind.Char:
sizeOf = 1;
break;
case CppPrimitiveKind.Short:
sizeOf = 2;
break;
case CppPrimitiveKind.Int:
sizeOf = 4;
break;
case CppPrimitiveKind.Long:
case CppPrimitiveKind.UnsignedLong:
sizeOf = 4; // This is incorrect
break;
case CppPrimitiveKind.LongLong:
sizeOf = 8;
break;
case CppPrimitiveKind.UnsignedChar:
sizeOf = 1;
break;
case CppPrimitiveKind.UnsignedShort:
sizeOf = 2;
break;
case CppPrimitiveKind.UnsignedInt:
sizeOf = 4;
break;
case CppPrimitiveKind.UnsignedLongLong:
sizeOf = 8;
break;
case CppPrimitiveKind.Float:
sizeOf = 4;
break;
case CppPrimitiveKind.Double:
sizeOf = 8;
break;
case CppPrimitiveKind.LongDouble:
sizeOf = 8;
break;
case CppPrimitiveKind.ObjCObject:
case CppPrimitiveKind.ObjCSel:
case CppPrimitiveKind.ObjCClass:
case CppPrimitiveKind.IntPtr:
case CppPrimitiveKind.UIntPtr:
sizeOf = 8; // Valid only for 64 bits
break;
case CppPrimitiveKind.UInt128:
case CppPrimitiveKind.Int128:
sizeOf = 16;
break;
case CppPrimitiveKind.Float16:
case CppPrimitiveKind.BFloat16:
sizeOf = 2;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <inheritdoc />
public override string ToString()
{
return Kind switch
{
CppPrimitiveKind.Void => "void",
CppPrimitiveKind.WChar => "wchar_t",
CppPrimitiveKind.Char => "char",
CppPrimitiveKind.Short => "short",
CppPrimitiveKind.Int => "int",
CppPrimitiveKind.Long => "long",
CppPrimitiveKind.UnsignedLong => "unsigned long",
CppPrimitiveKind.LongLong => "long long",
CppPrimitiveKind.UnsignedChar => "unsigned char",
CppPrimitiveKind.UnsignedShort => "unsigned short",
CppPrimitiveKind.UnsignedInt => "unsigned int",
CppPrimitiveKind.UnsignedLongLong => "unsigned long long",
CppPrimitiveKind.Float => "float",
CppPrimitiveKind.Double => "double",
CppPrimitiveKind.LongDouble => "long double",
CppPrimitiveKind.Bool => "bool",
CppPrimitiveKind.IntPtr => "intptr_t",
CppPrimitiveKind.UIntPtr => "uintptr_t",
CppPrimitiveKind.Int128 => "System.Int128",
CppPrimitiveKind.UInt128 => "System.UInt128",
CppPrimitiveKind.ObjCObject => "ObjCObject",
CppPrimitiveKind.ObjCSel => "ObjCSel",
CppPrimitiveKind.ObjCClass => "ObjCClass",
CppPrimitiveKind.Float16 => "System.Half",
CppPrimitiveKind.BFloat16 => "BFloat16",
_ => throw new InvalidOperationException($"Unhandled PrimitiveKind: {Kind}")
};
}
private bool Equals(CppPrimitiveType other)
{
return base.Equals(other) && Kind == other.Kind;
}
/// <inheritdoc />
public override int SizeOf
{
get => _sizeOf;
set => throw new InvalidOperationException("Cannot set the SizeOf of a primitive type");
}
/// <inheritdoc />
public override CppType GetCanonicalType()
{
return this;
}
}
}