-
Notifications
You must be signed in to change notification settings - Fork 57
/
GenTyped.fu
259 lines (239 loc) · 6.69 KB
/
GenTyped.fu
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
// GenTyped.fu - C/C++/C#/D/Java code generator
//
// Copyright (C) 2011-2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fusion Transpiler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
public abstract class GenTyped : GenBase
{
protected abstract void WriteType!(FuType type, bool promote);
protected override void WriteCoercedLiteral!(FuType? type, FuExpr expr)
{
expr.Accept(this, FuPriority.Argument);
if (type != null && type.Id == FuId.FloatType && expr is FuLiteralDouble)
WriteChar('f');
}
protected override void WriteTypeAndName!(FuNamedValue value)
{
WriteType(value.Type, true);
WriteChar(' ');
WriteName(value);
}
internal override void VisitAggregateInitializer!(FuAggregateInitializer expr)
{
Write("{ ");
WriteCoercedLiterals(expr.Type.AsClassType().GetElementType(), expr.Items);
Write(" }");
}
protected void WriteArrayStorageLength!(FuExpr expr)
{
assert expr.Type is FuArrayStorageType array;
VisitLiteralLong(array.Length);
}
protected override void WriteNewArray!(FuType elementType, FuExpr lengthExpr, FuPriority parent)
{
Write("new ");
WriteType(elementType.GetBaseType(), false);
WriteChar('[');
lengthExpr.Accept(this, FuPriority.Argument);
WriteChar(']');
while (elementType.IsArray()) {
WriteChar('[');
if (elementType is FuArrayStorageType arrayStorage)
arrayStorage.LengthExpr.Accept(this, FuPriority.Argument);
WriteChar(']');
elementType = elementType.AsClassType().GetElementType();
}
}
protected int GetOneAscii(FuExpr expr) => expr is FuLiteralString literal ? literal.GetOneAscii() : -1;
protected void WriteCharMethodCall!(FuExpr obj, string method, FuExpr arg)
{
obj.Accept(this, FuPriority.Primary);
WriteChar('.');
Write(method);
WriteChar('(');
if (!(arg is FuLiteralChar))
Write("(char) ");
arg.Accept(this, FuPriority.Primary);
WriteChar(')');
}
protected static bool IsNarrower(FuId left, FuId right)
{
switch (left) {
case FuId.SByteRange:
switch (right) {
case FuId.ByteRange:
case FuId.ShortRange:
case FuId.UShortRange:
case FuId.IntType:
case FuId.NIntType:
case FuId.LongType:
return true;
default:
return false;
}
case FuId.ByteRange:
switch (right) {
case FuId.SByteRange:
case FuId.ShortRange:
case FuId.UShortRange:
case FuId.IntType:
case FuId.NIntType:
case FuId.LongType:
return true;
default:
return false;
}
case FuId.ShortRange:
switch (right) {
case FuId.UShortRange:
case FuId.IntType:
case FuId.NIntType:
case FuId.LongType:
return true;
default:
return false;
}
case FuId.UShortRange:
switch (right) {
case FuId.ShortRange:
case FuId.IntType:
case FuId.NIntType:
case FuId.LongType:
return true;
default:
return false;
}
case FuId.IntType:
return right == FuId.LongType;
default:
return false;
}
}
protected FuExpr GetStaticCastInner(FuType type, FuExpr expr)
{
if (expr is FuBinaryExpr binary && binary.Op == FuToken.And && binary.Right is FuLiteralLong rightMask
&& type is FuIntegerType) {
long mask;
switch (type.Id) {
case FuId.ByteRange:
case FuId.SByteRange:
mask = 0xff;
break;
case FuId.ShortRange:
case FuId.UShortRange:
mask = 0xffff;
break;
case FuId.IntType:
mask = 0xffffffff;
break;
default:
return expr;
}
if ((rightMask.Value & mask) == mask)
return binary.Left;
}
return expr;
}
protected void WriteStaticCastType!(FuType type)
{
WriteChar('(');
WriteType(type, false);
Write(") ");
}
protected virtual void WriteStaticCast!(FuType type, FuExpr expr)
{
WriteStaticCastType(type);
GetStaticCastInner(type, expr).Accept(this, FuPriority.Primary);
}
protected override void WriteNotPromoted!(FuType type, FuExpr expr)
{
if (type is FuIntegerType
&& IsNarrower(type.Id, GetTypeId(expr.Type, true)))
WriteStaticCast(type, expr);
else
WriteCoercedLiteral(type, expr);
}
protected virtual bool IsPromoted(FuExpr expr) => !(expr is FuBinaryExpr binary && (binary.Op == FuToken.LeftBracket || binary.IsAssign()));
protected override void WriteAssignRight!(FuBinaryExpr expr)
{
if (expr.Left.IsIndexing()) {
if (expr.Right is FuLiteralLong) {
WriteCoercedLiteral(expr.Left.Type, expr.Right);
return;
}
FuId leftTypeId = expr.Left.Type.Id;
FuId rightTypeId = GetTypeId(expr.Right.Type, IsPromoted(expr.Right));
if (leftTypeId == FuId.SByteRange && rightTypeId == FuId.SByteRange) {
expr.Right.Accept(this, FuPriority.Assign); // omit Java "& 0xff"
return;
}
if (IsNarrower(leftTypeId, rightTypeId)) {
WriteStaticCast(expr.Left.Type, expr.Right);
return;
}
}
base.WriteAssignRight(expr);
}
protected override void WriteCoercedInternal!(FuType type, FuExpr expr, FuPriority parent)
{
if (type is FuIntegerType && type.Id != FuId.LongType && expr.Type.Id == FuId.LongType)
WriteStaticCast(type, expr);
else if (type.Id == FuId.FloatType && expr.Type.Id == FuId.DoubleType) {
if (expr is FuLiteralDouble literal) {
VisitLiteralDouble(literal.Value);
WriteChar('f');
}
else
WriteStaticCast(type, expr);
}
else if (type is FuIntegerType && expr.Type.Id == FuId.FloatIntType) {
if (expr is FuCallExpr call && call.Method.Symbol.Id == FuId.MathTruncate) {
expr = call.Arguments[0];
if (expr is FuLiteralDouble literal) {
VisitLiteralLong(Math.Truncate(literal.Value)); // TODO: range check
return;
}
}
WriteStaticCast(type, expr);
}
else
base.WriteCoercedInternal(type, expr, parent);
}
protected override void WriteCharAt!(FuBinaryExpr expr)
{
WriteIndexing(expr.Left, expr.Right);
}
protected override void StartTemporaryVar!(FuType type)
{
WriteType(type, true);
WriteChar(' ');
}
protected override void WriteAssertCast!(FuBinaryExpr expr)
{
assert expr.Right is FuVar def;
WriteTypeAndName(def);
Write(" = ");
WriteStaticCast(def.Type, expr.Left);
WriteCharLine(';');
}
protected void WriteExceptionConstructor!(FuClass klass, string s)
{
Write("public ");
Write(klass.Name);
WriteLine(s);
}
}