-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCompoundObjectInitializationExpression.cs
More file actions
291 lines (256 loc) · 10.9 KB
/
CompoundObjectInitializationExpression.cs
File metadata and controls
291 lines (256 loc) · 10.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT
using System.Collections.Immutable;
using Cesium.Ast;
using Cesium.CodeGen.Contexts;
using Cesium.CodeGen.Extensions;
using Cesium.CodeGen.Ir.Declarations;
using Cesium.CodeGen.Ir.Types;
using Cesium.Core;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Cesium.CodeGen.Ir.Expressions;
internal sealed class CompoundObjectInitializationExpression : IExpression
{
private readonly IType? _type;
private FieldDefinition? _typeDef;
private Action? _prefixAction;
private Action? _postfixAction;
internal ImmutableArray<IExpression?> Initializers { get; }
public CompoundObjectInitializationExpression(IType? type, ImmutableArray<IExpression?> initializers)
{
_type = type;
Initializers = initializers;
}
public CompoundObjectInitializationExpression(ImmutableArray<IExpression?> initializers)
: this(null, initializers)
{
}
public CompoundObjectInitializationExpression(CompoundLiteralExpression expression, IDeclarationScope scope)
{
var (type, _) = LocalDeclarationInfo.ProcessSpecifiers(expression.TypeName.SpecifierQualifierList, scope);
_type = type;
Initializers = expression.Initializers.Select(initializer => IScopedDeclarationInfo.ConvertInitializer(_type, initializer, scope)).ToImmutableArray();
}
public void Hint(FieldDefinition type, Action prefixAction, Action postfixAction)
{
_typeDef = type;
_prefixAction = prefixAction;
_postfixAction = postfixAction;
}
public void EmitTo(IEmitScope scope)
{
if (_type == null && _typeDef == null)
throw new Exception("_type is null!");
var instructions = scope.Method.Body.Instructions;
TypeDefinition typeDef = _type != null ? ((StructType)_type).Resolve(scope.Context).Resolve() : _typeDef!.FieldType.Resolve();
var fieldsDefs = typeDef.Fields;
var initializers = Initializers;
if (typeDef.IsCArray())
{
var element = typeDef.Fields[0].FieldType;
for (int i = 0; i < initializers.Length; i++)
{
IExpression? expr = initializers[i];
if (expr == null)
throw new CompilationException($"Retrieved null initializer!");
if (_prefixAction is not null)
{
_prefixAction();
instructions.Add(Instruction.Create(OpCodes.Ldflda, _typeDef));
}
if (i != 0)
{
instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i));
instructions.Add(Instruction.Create(OpCodes.Sizeof, element)); // size = sizeof(array element)
instructions.Add(Instruction.Create(OpCodes.Mul)); // offset = id * size
instructions.Add(Instruction.Create(OpCodes.Add)); // parent_struct_field_address + offset
}
expr.EmitTo(scope);
instructions.Add(GetWriteInstruction(element.MetadataType));
}
return;
}
_prefixAction?.Invoke();
var newobj = new VariableDefinition(typeDef);
scope.Method.Body.Variables.Add(newobj);
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
instructions.Add(Instruction.Create(OpCodes.Initobj, typeDef));
if (initializers.Length == 0) // zero init like SomeType name = { };
{
instructions.Add(Instruction.Create(OpCodes.Ldloc, newobj)); // push new object
if (_postfixAction is not null)
{
_postfixAction();
instructions.Add(Instruction.Create(OpCodes.Ldflda, _typeDef));
}
return;
}
for (int i = 0; i < initializers.Length; i++)
{
var init = initializers[i];
if (init == null)
throw new CompilationException($"Retrieved null initializer!");
if (init is ConstantLiteralExpression or GetValueExpression or TypeCastExpression)
{
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
init.EmitTo(scope);
instructions.Add(Instruction.Create(OpCodes.Stfld, fieldsDefs[i]));
}
else if (init is CompoundObjectFieldInitializer f)
{
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
EmitPathToField(scope, typeDef, f);
}
else if (init is CompoundObjectInitializationExpression objInit)
{
// UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE
var index = i;
objInit.Hint(
fieldsDefs[i],
() =>
{
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
},
() =>
{
instructions.Add(Instruction.Create(OpCodes.Stfld, fieldsDefs[index]));
});
objInit.EmitTo(scope);
}
else if (init is AssignmentExpression assignment)
{
throw new CompilationException($"You cant use {assignment} in the object initialization!");
}
else
throw new NotImplementedException($"Unsupported or unknown Initializer or Expression: {init.GetType().FullName}");
}
instructions.Add(Instruction.Create(OpCodes.Ldloc, newobj)); // push new object
_postfixAction?.Invoke();
}
private static void EmitPathToField(IEmitScope scope, TypeDefinition type, CompoundObjectFieldInitializer initializer)
{
FieldDefinition? field = null;
var path = initializer.Designation.Designators;
var last = path.Length - 1;
for (int i = 0; i < path.Length; i++)
{
var p = path[i];
if (p is IdentifierDesignator id)
{
field = type.Fields.FirstOrDefault(d => d.Name == id.FieldName)!;
if (field == null) // maybe anon?
{
List<FieldDefinition> list = new(1);
foreach(var f in type.Fields)
{
if (ResolveAnon(id.FieldName, f, list))
{
scope.LdFldA(f);
var start = list.Count - 1;
for (int x = start; x >= 1; x--)
{
var f2 = list[x];
scope.LdFldA(f2);
}
field = list[0];
break;
}
}
}
}
else if (p is BracketsDesignator b)
{
var instructions = scope.Method.Body.Instructions;
var arrayType = field!.FieldType.Resolve();
var element = arrayType.Fields[0].FieldType;
b.Expression.ToIntermediate((IDeclarationScope)scope).EmitTo(scope); // element id
instructions.Add(Instruction.Create(OpCodes.Sizeof, element)); // size = sizeof(array element)
instructions.Add(Instruction.Create(OpCodes.Mul)); // offset = id * size
instructions.Add(Instruction.Create(OpCodes.Add)); // ref result = ref previousField[offset]; or ref result = ref Unsafe.AddByteOffset(ref previoutsField, offset)
if (i == last)
{
initializer.Inner.EmitTo(scope); // push data
instructions.Add(GetWriteInstruction(element.MetadataType));
return;
}
continue;
}
else
throw new NotImplementedException();
if (field == null)
throw new NullReferenceException("field"); // unexpected
if (i != last)
{
scope.LdFldA(field);
type = field.FieldType.Resolve();
}
else
{
initializer.Inner.EmitTo(scope);
scope.StFld(field);
}
}
}
static bool ResolveAnon(string fieldName, FieldDefinition type, List<FieldDefinition> list)
{
var fieldType = type.FieldType.Resolve();
if (fieldType == null) return false;
if (fieldType.IsPrimitive) return false; // skip primitives (int, long and etc)
foreach (var field in fieldType.Fields)
{
if (field.Name == fieldName)
{
list.Add(field);
return true;
}
var resolved = field.FieldType.Resolve();
if (resolved == null) continue;
if (resolved.IsPrimitive) continue; // They don't have fields, so skip them
if ((resolved.Name.StartsWith("_Union_") || resolved.Name.StartsWith("_Anon_")) && ResolveAnon(fieldName, field, list))
{
list.Add(field);
return true;
}
}
return false;
}
static Instruction GetWriteInstruction(MetadataType type)
{
switch (type)
{
case MetadataType.Boolean:
case MetadataType.Byte:
case MetadataType.SByte:
return Instruction.Create(OpCodes.Stind_I1);
case MetadataType.UInt16:
case MetadataType.Int16:
return Instruction.Create(OpCodes.Stind_I2);
case MetadataType.Int32:
case MetadataType.UInt32:
return Instruction.Create(OpCodes.Stind_I4);
case MetadataType.Int64:
case MetadataType.UInt64:
return Instruction.Create(OpCodes.Stind_I8);
case MetadataType.Single:
return Instruction.Create(OpCodes.Stind_R4);
case MetadataType.Double:
return Instruction.Create(OpCodes.Stind_R8);
case MetadataType.Pointer:
case MetadataType.IntPtr:
case MetadataType.UIntPtr:
case MetadataType.ByReference:
return Instruction.Create(OpCodes.Stind_I);
default:
throw new CompilationException($"This array type isn't supported: {type}");
}
}
public IType GetExpressionType(IDeclarationScope scope) => _type!;
public IExpression Lower(IDeclarationScope scope)
{
var resolvedType = _type?.TypeKind == TypeKind.Unresolved ? scope.ResolveType(_type) : _type;
var initializers = Initializers.Select(e => e?.Lower(scope)).ToImmutableArray();
return resolvedType == null ? new CompoundObjectInitializationExpression(initializers) : new CompoundObjectInitializationExpression(resolvedType, initializers);
}
}