|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace FastCloner.SourceGenerator; |
| 4 | + |
| 5 | +internal static class BridgeProxyEmitter |
| 6 | +{ |
| 7 | + public const string HintName = "FastCloner_SGBridgeProxy.g.cs"; |
| 8 | + |
| 9 | + public static bool ShouldEmit(TargetFramework targetFramework, BridgeContract contract) |
| 10 | + { |
| 11 | + if (targetFramework >= TargetFramework.Net8) |
| 12 | + return false; |
| 13 | + |
| 14 | + return contract is { IsAvailable: true, Methods.Count: > 0 }; |
| 15 | + } |
| 16 | + |
| 17 | + public static string Emit(BridgeContract contract) |
| 18 | + { |
| 19 | + string proxyFqn = contract.ProxyTypeFullName; |
| 20 | + int lastDot = proxyFqn.LastIndexOf('.'); |
| 21 | + string proxyNamespace = lastDot >= 0 ? proxyFqn.Substring(0, lastDot) : string.Empty; |
| 22 | + string proxyTypeName = lastDot >= 0 ? proxyFqn.Substring(lastDot + 1) : proxyFqn; |
| 23 | + |
| 24 | + StringBuilder sb = new StringBuilder(2048); |
| 25 | + |
| 26 | + sb.AppendLine("#nullable disable"); |
| 27 | + sb.AppendLine("using System;"); |
| 28 | + sb.AppendLine("using System.Reflection;"); |
| 29 | + sb.AppendLine(); |
| 30 | + |
| 31 | + bool hasNamespace = proxyNamespace.Length > 0; |
| 32 | + if (hasNamespace) |
| 33 | + { |
| 34 | + sb.Append("namespace ").Append(proxyNamespace).AppendLine(); |
| 35 | + sb.AppendLine("{"); |
| 36 | + } |
| 37 | + |
| 38 | + string indent = hasNamespace ? " " : string.Empty; |
| 39 | + |
| 40 | + sb.Append(indent).AppendLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]"); |
| 41 | + sb.Append(indent).AppendLine("[global::System.Runtime.CompilerServices.CompilerGenerated]"); |
| 42 | + sb.Append(indent).Append("internal static class ").AppendLine(proxyTypeName); |
| 43 | + sb.Append(indent).AppendLine("{"); |
| 44 | + |
| 45 | + string body = hasNamespace ? indent + " " : " "; |
| 46 | + |
| 47 | + sb.Append(body).Append("private const string BridgeTypeName = \"").Append(contract.BridgeTypeMetadataName).AppendLine("\";"); |
| 48 | + sb.AppendLine(); |
| 49 | + sb.Append(body).AppendLine("private static readonly Type BridgeType = ResolveBridgeType();"); |
| 50 | + |
| 51 | + foreach (BridgeMethodSpec method in contract.Methods) |
| 52 | + { |
| 53 | + sb.AppendLine(); |
| 54 | + string delegateType = BuildDelegateType(method); |
| 55 | + string parameterTypeofList = BuildTypeofList(method.ParameterTypeFqns); |
| 56 | + |
| 57 | + sb.Append(body).Append("internal static readonly ").Append(delegateType).Append(' ').Append(method.Name).AppendLine(" ="); |
| 58 | + sb.Append(body).Append(" (").Append(delegateType).AppendLine(")Delegate.CreateDelegate("); |
| 59 | + sb.Append(body).Append(" typeof(").Append(delegateType).AppendLine("),"); |
| 60 | + sb.Append(body).Append(" ResolveStaticMethod(\"").Append(method.Name).Append('\"'); |
| 61 | + if (parameterTypeofList.Length > 0) |
| 62 | + { |
| 63 | + sb.Append(", ").Append(parameterTypeofList); |
| 64 | + } |
| 65 | + sb.AppendLine("));"); |
| 66 | + } |
| 67 | + |
| 68 | + sb.AppendLine(); |
| 69 | + sb.Append(body).AppendLine("private static Type ResolveBridgeType()"); |
| 70 | + sb.Append(body).AppendLine("{"); |
| 71 | + sb.Append(body).AppendLine(" Assembly runtimeAssembly = typeof(global::FastCloner.Code.FastClonerGenerator).Assembly;"); |
| 72 | + sb.Append(body).AppendLine(" Type type = runtimeAssembly.GetType(BridgeTypeName, throwOnError: false);"); |
| 73 | + sb.Append(body).AppendLine(" if (type == null)"); |
| 74 | + sb.Append(body).AppendLine(" {"); |
| 75 | + sb.Append(body).AppendLine(" throw new InvalidOperationException("); |
| 76 | + sb.Append(body).AppendLine(" \"FastCloner source generator: the runtime bridge type '\" + BridgeTypeName +"); |
| 77 | + sb.Append(body).AppendLine(" \"' was not found in assembly '\" + runtimeAssembly.FullName +"); |
| 78 | + sb.Append(body).AppendLine(" \"'. The FastCloner runtime version may be incompatible with the source generator. \" +"); |
| 79 | + sb.Append(body).AppendLine(" \"Update the FastCloner package to a matching version.\");"); |
| 80 | + sb.Append(body).AppendLine(" }"); |
| 81 | + sb.Append(body).AppendLine(" return type;"); |
| 82 | + sb.Append(body).AppendLine("}"); |
| 83 | + |
| 84 | + sb.AppendLine(); |
| 85 | + sb.Append(body).AppendLine("private static MethodInfo ResolveStaticMethod(string name, params Type[] parameterTypes)"); |
| 86 | + sb.Append(body).AppendLine("{"); |
| 87 | + sb.Append(body).AppendLine(" const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;"); |
| 88 | + sb.Append(body).AppendLine(" MethodInfo method = BridgeType.GetMethod(name, flags, binder: null, types: parameterTypes, modifiers: null);"); |
| 89 | + sb.Append(body).AppendLine(" if (method == null)"); |
| 90 | + sb.Append(body).AppendLine(" {"); |
| 91 | + sb.Append(body).AppendLine(" throw new InvalidOperationException("); |
| 92 | + sb.Append(body).AppendLine(" \"FastCloner source generator: bridge method '\" + name +"); |
| 93 | + sb.Append(body).AppendLine(" \"' was not found on '\" + BridgeType.FullName +"); |
| 94 | + sb.Append(body).AppendLine(" \"'. The FastCloner runtime version may be incompatible with the source generator.\");"); |
| 95 | + sb.Append(body).AppendLine(" }"); |
| 96 | + sb.Append(body).AppendLine(" return method;"); |
| 97 | + sb.Append(body).AppendLine("}"); |
| 98 | + |
| 99 | + sb.Append(indent).AppendLine("}"); |
| 100 | + |
| 101 | + if (hasNamespace) |
| 102 | + { |
| 103 | + sb.AppendLine("}"); |
| 104 | + } |
| 105 | + |
| 106 | + return sb.ToString(); |
| 107 | + } |
| 108 | + |
| 109 | + private static string BuildDelegateType(BridgeMethodSpec method) |
| 110 | + { |
| 111 | + if (method.IsVoid) |
| 112 | + { |
| 113 | + if (method.ParameterTypeFqns.Count == 0) |
| 114 | + { |
| 115 | + return "global::System.Action"; |
| 116 | + } |
| 117 | + |
| 118 | + return "global::System.Action<" + Join(method.ParameterTypeFqns) + ">"; |
| 119 | + } |
| 120 | + |
| 121 | + if (method.ParameterTypeFqns.Count == 0) |
| 122 | + { |
| 123 | + return "global::System.Func<" + method.ReturnTypeFqn + ">"; |
| 124 | + } |
| 125 | + |
| 126 | + return "global::System.Func<" + Join(method.ParameterTypeFqns) + ", " + method.ReturnTypeFqn + ">"; |
| 127 | + } |
| 128 | + |
| 129 | + private static string BuildTypeofList(EquatableArray<string> parameterTypeFqns) |
| 130 | + { |
| 131 | + string[]? items = parameterTypeFqns.GetArray(); |
| 132 | + if (items is null || items.Length == 0) |
| 133 | + return string.Empty; |
| 134 | + |
| 135 | + StringBuilder sb = new StringBuilder(); |
| 136 | + for (int i = 0; i < items.Length; i++) |
| 137 | + { |
| 138 | + if (i > 0) sb.Append(", "); |
| 139 | + sb.Append("typeof(").Append(items[i]).Append(')'); |
| 140 | + } |
| 141 | + return sb.ToString(); |
| 142 | + } |
| 143 | + |
| 144 | + private static string Join(EquatableArray<string> items) |
| 145 | + { |
| 146 | + string[]? array = items.GetArray(); |
| 147 | + if (array is null || array.Length == 0) |
| 148 | + return string.Empty; |
| 149 | + |
| 150 | + StringBuilder sb = new StringBuilder(); |
| 151 | + for (int i = 0; i < array.Length; i++) |
| 152 | + { |
| 153 | + if (i > 0) sb.Append(", "); |
| 154 | + sb.Append(array[i]); |
| 155 | + } |
| 156 | + return sb.ToString(); |
| 157 | + } |
| 158 | +} |
0 commit comments