-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeBase.cs
More file actions
463 lines (384 loc) · 17.5 KB
/
Copy pathTypeBase.cs
File metadata and controls
463 lines (384 loc) · 17.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
namespace NodeDev.Core.Types;
public abstract class TypeBase
{
public abstract string Name { get; }
public abstract string FullName { get; }
public virtual bool IsClass => true;
public abstract TypeBase[] Generics { get; }
public abstract TypeBase? BaseType { get; }
public abstract TypeBase[] Interfaces { get; }
public abstract bool IsArray { get; }
public abstract TypeBase ArrayType { get; }
public abstract TypeBase ArrayInnerType { get; }
public bool HasUndefinedGenerics => Generics.Any(x => x is UndefinedGenericType || x.HasUndefinedGenerics);
public virtual bool IsExec => false;
public virtual bool AllowTextboxEdit => false;
public virtual string? DefaultTextboxValue => null;
public abstract string FriendlyName { get; }
internal protected abstract string Serialize();
public abstract Type MakeRealType();
public abstract TypeBase CloneWithGenerics(TypeBase[] newGenerics);
public virtual object? ParseTextboxEdit(string text) => throw new NotImplementedException();
public abstract IEnumerable<IMemberInfo> GetMembers();
public record class SerializedType(string TypeFullName, string SerializedTypeCustom);
public SerializedType SerializeWithFullTypeName()
{
var serializedType = new SerializedType(GetType().FullName!, Serialize());
return serializedType;
}
public string SerializeWithFullTypeNameString() => JsonSerializer.Serialize(SerializeWithFullTypeName());
public IEnumerable<string> GetUndefinedGenericTypes()
{
IEnumerable<string> undefinedGenericTypes = this is UndefinedGenericType undefinedGenericType ? [undefinedGenericType.UndefinedGenericTypeName] : [];
return undefinedGenericTypes.Concat(Generics.SelectMany(x => x.GetUndefinedGenericTypes())).Distinct();
}
public TypeBase ReplaceUndefinedGeneric(IReadOnlyDictionary<string, TypeBase> genericTypes)
{
if (this is UndefinedGenericType undefinedGeneric)
{
if (!genericTypes.TryGetValue(undefinedGeneric.UndefinedGenericTypeName, out var newType))
return undefinedGeneric; // put back the undefined generic if we didn't find a replacement
// We know what 'T' is matched with. Now if we have T[], we have to increase the array level of the matched type too.
for (int i = 0; i < undefinedGeneric.NbArrayLevels; ++i)
newType = newType.ArrayType;
return newType;
}
var generics = new TypeBase[Generics.Length];
for (int i = 0; i < Generics.Length; ++i)
generics[i] = Generics[i].ReplaceUndefinedGeneric(genericTypes); // ask a more complex type to replace its own undefined generics
return CloneWithGenerics(generics);
}
#region Assignation checks
/// <summary>
/// Returns in the backend type is the same, ignoring generics
/// For RealType, that means the actual 'Type' Backend
/// </summary>
/// <param name="typeBase"></param>
/// <returns></returns>
public abstract bool IsSameBackend(TypeBase typeBase);
/// <summary>
/// Validate if 'this' is directly assignable to 'other' without checkout the entire hierarchy of inheritance and interface implementations
/// </summary>
/// <param name="other">Other type we are trying to plug into</param>
/// <param name="allowInOutGenerics">Check for covariant and contravariant generics</param>
/// <param name="changedGenericsLeft">Generics that needs to be updated in 'this' in order for the assignation to work</param>
/// <param name="changedGenericsRight">Generics that needs to be updated in 'other' in order for the assignation to work</param>
/// <returns></returns>
internal bool IsDirectlyAssignableTo(TypeBase other, bool allowInOutGenerics, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsLeft, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsRight, out int totalDepths)
{
if (this is UndefinedGenericType thisUndefinedGenericType)
{
if (other is UndefinedGenericType)
{
changedGenericsLeft = []; // nothing to change, we're plugging a generic into another generic
changedGenericsRight = [];
totalDepths = 0;
return true;
}
else if (!IsArray || other.IsArray) // we can change 'this' to the same type as 'other' and plug into it
{
// We are either plugging:
// T into string[], T into string or T[] into string[]
changedGenericsLeft = new()
{
[thisUndefinedGenericType.UndefinedGenericTypeName] = thisUndefinedGenericType.SimplifyToMatchWith(other)
};
changedGenericsRight = [];
totalDepths = 0;
return true;
}
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
else if (other is UndefinedGenericType otherUndefinedGenericType) // we can change the other generic into the current type
{
if (IsArray || !other.IsArray)
{
// We are either plugging : string[] into T[] or string[] into T
// OR (the 'if' OR...)
// We are plugging string string into T
// In both cases we want to update the generic to the array type and let the system handle the rest later on
changedGenericsLeft = [];
changedGenericsRight = new()
{
[otherUndefinedGenericType.UndefinedGenericTypeName] = otherUndefinedGenericType.SimplifyToMatchWith(this)
};
totalDepths = 0;
return true;
}
// string into T[] <----- this is not allowed
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
if (IsSameBackend(other)) // same backend, List<int> would be the same backend as List<string> or List<T>
{
// check all the generics, they have to either be undefined, the same or covariant
changedGenericsLeft = [];
changedGenericsRight = [];
totalDepths = 0;
for (int i = 0; i < Generics.Length; ++i)
{
if (allowInOutGenerics && other.IsOut(i)) // we can plug a less derived type, like IEnumerable<Child> to IEnumerable<Parent>
{
if (Generics[i].IsAssignableTo(other.Generics[i], out var changedGenericsLeftLocal, out var changedGenericsRightLocal, out var totalSubDepths))
{
totalDepths += totalSubDepths;
foreach (var changed in changedGenericsLeftLocal)
changedGenericsLeft[changed.Key] = changed.Value;
foreach (var changed in changedGenericsRightLocal)
changedGenericsRight[changed.Key] = changed.Value;
continue; // it worked, check the next generic
}
}
else if (allowInOutGenerics && other.IsIn(i)) // We can plug a more derived type, like IComparable<Parent> to IComparable<Child>
{
// Invert the 'changedGenerics' left and right here, since we're plugging 'other' into 'this' this time
if (other.Generics[i].IsAssignableTo(Generics[i], out var changedGenericsRightLocal, out var changedGenericsLeftLocal, out var totalSubDepths))
{
totalDepths += totalSubDepths;
foreach (var changed in changedGenericsLeftLocal)
changedGenericsLeft[changed.Key] = changed.Value;
foreach (var changed in changedGenericsRightLocal)
changedGenericsRight[changed.Key] = changed.Value;
continue; // it worked, check the next generic
}
}
else
{
// the generic is not covariant, so it has to be the same
if (Generics[i].IsDirectlyAssignableTo(other.Generics[i], allowInOutGenerics, out var changedGenericsLeftLocal, out var changedGenericsRightLocal, out var totalSubDepths))
{
totalDepths += totalSubDepths;
foreach (var changed in changedGenericsLeftLocal)
changedGenericsLeft[changed.Key] = changed.Value;
foreach (var changed in changedGenericsRightLocal)
changedGenericsRight[changed.Key] = changed.Value;
continue; // it worked, check the next generic
}
}
// one of the generics is not assignable, so we're not assignable
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
return true;
}
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
public bool IsAssignableTo(TypeBase other, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsLeft, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsRight)
{
return IsAssignableTo(other, out changedGenericsLeft, out changedGenericsRight, out _);
}
/// <summary>
/// Check if 'this' is assignable to 'other' and return the depth of the assignation.
/// Also return the generics that needs to be updated in order for the assignation to work.
/// </summary>
/// <param name="totalDepths">
/// Total depths of the assignation. That is, how far up the inheritance tree did we need to go to make this assignation work.
/// It also sums up the depths of the generics assignations.
/// This is use to prioritize assignations of lower depth, such as converting List<string> to IList<string> instead of IEnumerable<string>, if possible.
/// </param>
/// <returns>True if the assignation is possible. Then <paramref name="changedGenerics"/> and <paramref name="totalDepths"/> are both set.</returns>
public bool IsAssignableTo(TypeBase other, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsLeft, [MaybeNullWhen(false)] out Dictionary<string, TypeBase> changedGenericsRight, out int totalDepths)
{
if ((this is ExecType && other is not ExecType) || (other is ExecType && this is not ExecType))
{
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
if (IsDirectlyAssignableTo(other, true, out changedGenericsLeft, out changedGenericsRight, out var totalSubDepths))
{
totalDepths = totalSubDepths;
return true; // Either plugging something easy like List<int> to List<int>, some generic or covariant like IEnumerable<Parent> to IEnumerable<Child>
}
var myAssignableTypes = GetAssignableTypes().OrderBy(x => x.Depth);
var changedGenericsLeftLocal = new Dictionary<string, TypeBase>();
var changedGenericsRightLocal = new Dictionary<string, TypeBase>();
foreach ((var myAssignableType, var assignableDepth) in myAssignableTypes)
{
if (!myAssignableType.IsSameBackend(other))
continue;
if (myAssignableType.Generics.Length != other.Generics.Length)
throw new Exception("Unable to compare types with different number of generics, this should never happen since the BackendType is identical");
var isAssignable = true;
changedGenericsLeftLocal.Clear(); // reuse the same dictionary for optimization purposes
changedGenericsRightLocal.Clear(); // reuse the same dictionary for optimization purposes
var totalDepthsLocal = 0;
for (int i = 0; i < myAssignableType.Generics.Length; i++)
{
// check if either, both or none of the current and other type are undefined generics
if (myAssignableType.Generics[i] is UndefinedGenericType currentUndefinedGeneric)
{
if (other.Generics[i] is UndefinedGenericType)
continue; // this works out fine, we're plugging a generic into another generic
else
{
// we're plugging a generic into a non generic type, we just record that and keep checking the others
// however this only works if we're either plugging :
// T into string or T[] into string[]
if (currentUndefinedGeneric.IsArray && !other.Generics[i].IsArray)
{
isAssignable = false;
break;
}
else
{
changedGenericsLeftLocal[currentUndefinedGeneric.UndefinedGenericTypeName] = currentUndefinedGeneric.SimplifyToMatchWith(other.Generics[i]);
continue;
}
}
}
else if (other.Generics[i] is UndefinedGenericType otherUndefinedGeneric)
{
// we know we're not a generic, so we're plugging a type into a generic
// However, this only works if we're either plugging :
// string into T or string[] into T[]
if (myAssignableType.IsArray && !other.Generics[i].IsArray)
{
isAssignable = false;
break;
}
changedGenericsRightLocal[otherUndefinedGeneric.UndefinedGenericTypeName] = otherUndefinedGeneric.SimplifyToMatchWith(myAssignableType.Generics[i]);
continue;
}
bool checkParents = true;
TypeBase left, right;
bool swapped = false; // true if left and right changed generics need to be swapped
// let's check if they have the same backend type, like List<T> and List<int> share the List<> backend type
// if they are 2 types that could be assigned only by looking at their BaseType or interfaces (Like List<int> to IEnumerable<int>), that will be checked later
if (!myAssignableType.Generics[i].IsSameBackend(other.Generics[i]))
{
// Check if the types are assignable even if the generic isn't the same
// By example, List<ParentClass> can be assigned to IEnumerable<ChildClass> even though ParentClass and ChildClass aren't the same
if (other.IsOut(i))
{
// we have to check if otherAssignableType.Generics[i] is less derived than myAssignableType.Generics[i]
left = myAssignableType.Generics[i];
right = other.Generics[i];
}
else if (other.IsIn(i))
{
// we have to check if otherAssignableType.Generics[i] is more derived than myAssignableType.Generics[i]
left = other.Generics[i];
right = myAssignableType.Generics[i];
swapped = true;
}
else
{
// we have to check if otherAssignableType.Generics[i] is the same as myAssignableType.Generics[i]
left = myAssignableType.Generics[i];
right = other.Generics[i];
checkParents = false;
}
}
else
{
// we have to check if otherAssignableType.Generics[i] is the same as myAssignableType.Generics[i]
left = myAssignableType.Generics[i];
right = other.Generics[i];
checkParents = false;
}
// Check parents will recursively check if the left is assignable to the right, going up the inheritance tree
if (checkParents)
{
if (left.IsAssignableTo(right, out var changedGenericsLeftLocally, out var changedGenericsRightLocally, out var totalDepths1))
{
totalDepthsLocal += totalDepths1;
if (swapped)
{
(changedGenericsRightLocally, changedGenericsLeftLocally) = (changedGenericsLeftLocally, changedGenericsRightLocally);
}
foreach (var changed in changedGenericsLeftLocally)
changedGenericsLeftLocal[changed.Key] = changed.Value;
foreach (var changed in changedGenericsRightLocally)
changedGenericsRightLocal[changed.Key] = changed.Value;
continue;
}
}
else
{
if (left.IsDirectlyAssignableTo(right, false, out var changedGenericsLeftLocally, out var changedGenericsRightLocally, out totalSubDepths))
{
totalDepthsLocal += totalSubDepths;
if (swapped)
{
(changedGenericsRightLocally, changedGenericsLeftLocally) = (changedGenericsLeftLocally, changedGenericsRightLocally);
}
foreach (var changed in changedGenericsLeftLocally)
changedGenericsLeftLocal[changed.Key] = changed.Value;
foreach (var changed in changedGenericsRightLocally)
changedGenericsRightLocal[changed.Key] = changed.Value;
continue;
}
}
isAssignable = false;
break;
}
if (isAssignable)
{
changedGenericsLeft = changedGenericsLeftLocal;
changedGenericsRight = changedGenericsRightLocal;
totalDepths = assignableDepth + 1 + totalDepthsLocal;
return true;
}
}
changedGenericsLeft = changedGenericsRight = null;
totalDepths = -1;
return false;
}
#endregion
/// <summary>
/// If true, We can plug any less derived type. Ex IComparer<Person> can be assigned to IComparer<Employee> even though a person is not necessarily an employee. This one is unusual
/// </summary>
public virtual bool IsIn(int genericIndex) => false;
/// <summary>
/// If true, we can plug any derived type. Ex List<Employee> ca be assigned to IEnumerable<Person>
/// </summary>
public virtual bool IsOut(int genericIndex) => false;
/// <summary>
/// Returns a list of types that this type can be assigned to
/// </summary>
public IEnumerable<(TypeBase Type, int Depth)> GetAssignableTypes()
{
return GetAssignableTypes(0).DistinctBy(x => x.Type);
}
private IEnumerable<(TypeBase Type, int Depth)> GetAssignableTypes(int depth)
{
yield return (this, depth);
if (BaseType != null)
{
foreach (var baseType in BaseType.GetAssignableTypes(depth + 1))
yield return baseType;
}
foreach (var @interface in Interfaces)
{
foreach (var interfaceType in @interface.GetAssignableTypes(depth + 1))
yield return interfaceType;
}
}
public abstract IEnumerable<IMethodInfo> GetMethods();
public IEnumerable<IMethodInfo> GetMethods(MethodAttributes attributes) => GetMethods().Where(x => (x.Attributes & attributes) == attributes);
public abstract IEnumerable<IMethodInfo> GetMethods(string name);
public static TypeBase DeserializeFullTypeNameString(TypeFactory typeFactory, string serializedTypeStr)
{
var serializedType = JsonSerializer.Deserialize<SerializedType>(serializedTypeStr) ?? throw new Exception("Unable to deserialize type");
return Deserialize(typeFactory, serializedType);
}
public static TypeBase Deserialize(TypeFactory typeFactory, SerializedType serializedType)
{
var type = TypeFactory.GetTypeByFullName(serializedType.TypeFullName) ?? throw new Exception($"Type not found: {serializedType.TypeFullName}");
var deserializeMethod = type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static) ?? throw new Exception($"Deserialize method not found in type: {serializedType.TypeFullName}");
var deserializedType = deserializeMethod.Invoke(null, [typeFactory, serializedType.SerializedTypeCustom]);
if (deserializedType is TypeBase typeBase)
return typeBase;
throw new Exception($"Deserialize method in type {serializedType.TypeFullName} returned invalid type");
}
}