-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
CppPrimitiveType.cs
226 lines (201 loc) · 7.98 KB
/
CppPrimitiveType.cs
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
// 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);
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;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <inheritdoc />
public override string ToString()
{
switch (Kind)
{
case CppPrimitiveKind.Void:
return "void";
case CppPrimitiveKind.WChar:
return "wchar_t";
case CppPrimitiveKind.Char:
return "char";
case CppPrimitiveKind.Short:
return "short";
case CppPrimitiveKind.Int:
return "int";
case CppPrimitiveKind.Long:
return "long";
case CppPrimitiveKind.UnsignedLong:
return "unsigned long";
case CppPrimitiveKind.LongLong:
return "long long";
case CppPrimitiveKind.UnsignedChar:
return "unsigned char";
case CppPrimitiveKind.UnsignedShort:
return "unsigned short";
case CppPrimitiveKind.UnsignedInt:
return "unsigned int";
case CppPrimitiveKind.UnsignedLongLong:
return "unsigned long long";
case CppPrimitiveKind.Float:
return "float";
case CppPrimitiveKind.Double:
return "double";
case CppPrimitiveKind.LongDouble:
return "long double";
case CppPrimitiveKind.Bool:
return "bool";
default:
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;
}
}
}