|
| 1 | +using HashlinkNET.Bytecode; |
| 2 | +using HashlinkNET.Compiler.Data; |
| 3 | +using HashlinkNET.Compiler.Utils; |
| 4 | +using Mono.Cecil; |
| 5 | +using Mono.Cecil.Cil; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace HashlinkNET.Compiler.Steps.Class |
| 11 | +{ |
| 12 | + internal class GenerateClassExplicitCastStep : ForeachHlTypeCompileStep |
| 13 | + { |
| 14 | + public override bool Filter( HlType type ) |
| 15 | + { |
| 16 | + return type.Kind == HlTypeKind.Obj; |
| 17 | + } |
| 18 | + public override void Execute( IDataContainer container, HlCode code, GlobalData gdata, RuntimeImports rdata, HlType type ) |
| 19 | + { |
| 20 | + var info = container.GetData<ObjClassData>(type); |
| 21 | + var td = info.TypeDef; |
| 22 | + foreach (var v in info.CastExplicitTypes) |
| 23 | + { |
| 24 | + if (!v.Value) |
| 25 | + { |
| 26 | + continue; |
| 27 | + } |
| 28 | + { |
| 29 | + var to = new MethodDefinition( |
| 30 | + $"op_Explicit", |
| 31 | + MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.HideBySig, |
| 32 | + rdata.objectType |
| 33 | + ) |
| 34 | + { |
| 35 | + Parameters = |
| 36 | + { |
| 37 | + new("value", ParameterAttributes.None, td) |
| 38 | + }, |
| 39 | + ReturnType = v.Key |
| 40 | + }; |
| 41 | + var il = to.Body.GetILProcessor(); |
| 42 | + |
| 43 | + il.Emit(OpCodes.Ldarg_0); |
| 44 | + |
| 45 | + if (v.Value) //Is Virtual |
| 46 | + { |
| 47 | + il.Emit(OpCodes.Call, rdata.hToVirtual.MakeInstance(v.Key)); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + il.Emit(OpCodes.Call, rdata.hToObject.MakeInstance(v.Key)); |
| 52 | + } |
| 53 | + |
| 54 | + il.Emit(OpCodes.Ret); |
| 55 | + |
| 56 | + |
| 57 | + td.Methods.Add(to); |
| 58 | + } |
| 59 | + { |
| 60 | + var from = new MethodDefinition( |
| 61 | + $"op_Explicit", |
| 62 | + MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.HideBySig, |
| 63 | + rdata.objectType |
| 64 | + ) |
| 65 | + { |
| 66 | + Parameters = |
| 67 | + { |
| 68 | + new("value", ParameterAttributes.None, v.Key) |
| 69 | + }, |
| 70 | + ReturnType = td |
| 71 | + }; |
| 72 | + |
| 73 | + var il = from.Body.GetILProcessor(); |
| 74 | + |
| 75 | + il.Emit(OpCodes.Ldarg_0); |
| 76 | + il.Emit(OpCodes.Call, rdata.hToObject.MakeInstance(td)); |
| 77 | + il.Emit(OpCodes.Ret); |
| 78 | + |
| 79 | + td.Methods.Add(from); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments