summaryrefslogtreecommitdiff
path: root/GenTyped.cs
blob: d4e802a94eb25c9162070dbff081fdc38212003f (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
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
// GenTyped.cs - C/C++/C#/Java code generator
//
// Copyright (C) 2011-2021  Piotr Fusik
//
// This file is part of CiTo, see https://github.com/pfusik/cito
//
// CiTo 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.
//
// CiTo 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 CiTo.  If not, see http://www.gnu.org/licenses/

using System;
using System.Collections.Generic;
using System.Linq;

namespace Foxoft.Ci
{

public abstract class GenTyped : GenBase
{
	protected abstract void Write(TypeCode typeCode);

	protected abstract void Write(CiType type, bool promote);

	protected override void WriteTypeAndName(CiNamedValue value)
	{
		Write(value.Type, true);
		Write(' ');
		WriteName(value);
	}

	public override CiExpr Visit(CiCollection expr, CiPriority parent)
	{
		CiType type = ((CiArrayStorageType) expr.Type).ElementType;
		Write("{ ");
		WriteCoercedLiterals(type, expr.Items);
		Write(" }");
		return expr;
	}

	protected override void WriteNewArray(CiType elementType, CiExpr lengthExpr, CiPriority parent)
	{
		Write("new ");
		Write(elementType.BaseType, false);
		Write('[');
		lengthExpr.Accept(this, CiPriority.Argument);
		Write(']');
		while (elementType is CiArrayType array) {
			Write('[');
			if (array is CiArrayStorageType arrayStorage)
				arrayStorage.LengthExpr.Accept(this, CiPriority.Argument);
			Write(']');
			elementType = array.ElementType;
		}
	}

	static bool IsAscii(long c)
	{
		if (c >= ' ' && c <= 0x7e)
			return true;
		switch (c) {
		case '\a':
		case '\b':
		case '\f':
		case '\n':
		case '\r':
		case '\t':
		case '\v':
			return true;
		default:
			return false;
		}
	}

	protected bool IsOneAsciiString(CiExpr expr, out char c)
	{
		if (expr is CiLiteral literal && literal.Value is string s && s.Length == 1 && IsAscii(s[0])) {
			c = s[0];
			return true;
		}
		c = '\0';
		return false;
	}

	protected void WriteCharLiteral(char c)
	{
		Write('\'');
		if (c == '\'')
			Write('\\');
		WriteEscapedChar(c);
		Write('\'');
	}

	protected override void WriteComparison(CiBinaryExpr expr, CiPriority parent, CiPriority child, string op)
	{
		if (expr.Left.IsIndexing && expr.Left is CiBinaryExpr indexing && indexing.Left.Type is CiStringType
		 && expr.Right is CiLiteral literal && literal.Value is long c && IsAscii(c)) {
			if (parent > child)
				Write('(');
			expr.Left.Accept(this, child);
			Write(op);
			WriteCharLiteral((char) c);
			if (parent > child)
				Write(')');
		}
		else
			base.WriteComparison(expr, parent, child, op);
	}

	protected static bool IsNarrower(TypeCode left, TypeCode right)
	{
		switch (left) {
		case TypeCode.SByte:
			switch (right) {
			case TypeCode.Byte:
			case TypeCode.Int16:
			case TypeCode.UInt16:
			case TypeCode.Int32:
			case TypeCode.Int64:
				return true;
			default:
				return false;
			}
		case TypeCode.Byte:
			switch (right) {
			case TypeCode.SByte:
			case TypeCode.Int16:
			case TypeCode.UInt16:
			case TypeCode.Int32:
			case TypeCode.Int64:
				return true;
			default:
				return false;
			}
		case TypeCode.Int16:
			switch (right) {
			case TypeCode.UInt16:
			case TypeCode.Int32:
			case TypeCode.Int64:
				return true;
			default:
				return false;
			}
		case TypeCode.UInt16:
			switch (right) {
			case TypeCode.Int16:
			case TypeCode.Int32:
			case TypeCode.Int64:
				return true;
			default:
				return false;
			}
		case TypeCode.Int32:
			return right == TypeCode.Int64;
		default:
			return false;
		}
	}

	protected CiExpr GetStaticCastInner(CiType type, CiExpr expr)
	{
		if (expr is CiBinaryExpr binary && binary.Op == CiToken.And && binary.Right is CiLiteral rightMask
		 && type is CiIntegerType integer) {
			long mask;
			switch (GetIntegerTypeCode(integer, false)) {
			case TypeCode.Byte:
			case TypeCode.SByte:
				mask = 0xff;
				break;
			case TypeCode.Int16:
			case TypeCode.UInt16:
				mask = 0xffff;
				break;
			case TypeCode.Int32:
			case TypeCode.UInt32:
				mask = 0xffffffff;
				break;
			default:
				return expr;
			}
			if (((long) rightMask.Value & mask) == mask)
				return binary.Left;
		}
		return expr;
	}

	protected virtual void WriteStaticCast(CiType type, CiExpr expr)
	{
		Write('(');
		Write(type, false);
		Write(") ");
		GetStaticCastInner(type, expr).Accept(this, CiPriority.Primary);
	}

	protected override void WriteNotPromoted(CiType type, CiExpr expr)
	{
		if (type is CiIntegerType elementType
		 && IsNarrower(GetIntegerTypeCode(elementType, false), GetIntegerTypeCode((CiIntegerType) expr.Type, true)))
			WriteStaticCast(elementType, expr);
		else
			expr.Accept(this, CiPriority.Argument);
	}

	protected virtual bool IsNotPromotedIndexing(CiBinaryExpr expr) => expr.Op == CiToken.LeftBracket;

	protected override void WriteAssignRight(CiBinaryExpr expr)
	{
		if (expr.Left.IsIndexing) {
			TypeCode leftTypeCode = GetTypeCode(expr.Left.Type, false);
			bool promote;
			switch (expr.Right) {
			case CiLiteral _:
			case CiBinaryExpr rightBinary when IsNotPromotedIndexing(rightBinary) || rightBinary.IsAssign:
				promote = false;
				break;
			default:
				promote = true;
				break;
			}
			TypeCode rightTypeCode = GetTypeCode(expr.Right.Type, promote);
			if (leftTypeCode == TypeCode.SByte && rightTypeCode == TypeCode.SByte) {
				expr.Right.Accept(this, CiPriority.Assign); // omit Java "& 0xff"
				return;
			}
			if (IsNarrower(leftTypeCode, rightTypeCode)) {
				WriteStaticCast(expr.Left.Type, expr.Right);
				return;
			}
		}
		WriteCoerced(expr.Left.Type, expr.Right, CiPriority.Argument);
	}

	protected override void WriteCoercedInternal(CiType type, CiExpr expr, CiPriority parent)
	{
		if (type is CiIntegerType && type != CiSystem.LongType && expr.Type == CiSystem.LongType)
			WriteStaticCast(type, expr);
		else if (type == CiSystem.FloatType && expr.Type == CiSystem.DoubleType) {
			if (expr is CiLiteral) {
				expr.Accept(this, CiPriority.Argument);
				Write('f');
			}
			else
				WriteStaticCast(type, expr);
		}
		else if (type is CiIntegerType && expr.Type == CiSystem.FloatIntType) {
			if (expr is CiCallExpr call && call.Method.IsReferenceTo(CiSystem.MathTruncate))
				WriteStaticCast(type, call.Arguments[0]);
			else
				WriteStaticCast(type, expr);
		}
		else
			base.WriteCoercedInternal(type, expr, parent);
	}

	protected override void WriteCharAt(CiBinaryExpr expr)
	{
		WriteIndexing(expr, CiPriority.Argument);
	}

	protected abstract bool HasInitCode(CiNamedValue def);

	protected virtual bool NeedsConstructor(CiClass klass)
	{
		return klass.Constructor != null
			|| klass.Fields.Any(field => HasInitCode(field));
	}

}

}