You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
17 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. namespace Net3_Proxy
  8. {
  9. public static class ExpressionEx
  10. {
  11. internal static ExpressionType ExprT(this ExpressionTypeEx ex)
  12. => (ExpressionType)ex;
  13. internal static ExpressionTypeEx ExprTEx(this ExpressionType ex)
  14. => (ExpressionTypeEx)ex;
  15. private static void Validate(Type type, bool allowByRef)
  16. {
  17. if (type == null)
  18. throw new ArgumentNullException(nameof(type));
  19. TypeUtils.ValidateType(type, nameof(type), allowByRef, false);
  20. if (type == typeof(void))
  21. throw new ArgumentException("Argument cannot be of type void", nameof(type));
  22. }
  23. private static void RequiresCanWrite(Expression expression, string paramName)
  24. {
  25. if (expression == null)
  26. {
  27. throw new ArgumentNullException(paramName);
  28. }
  29. ExpressionType nodeType = expression.NodeType;
  30. if (nodeType != ExpressionType.MemberAccess)
  31. {
  32. if (nodeType == ExpressionType.Parameter)
  33. return;
  34. }
  35. else
  36. {
  37. MemberInfo member = ((MemberExpression)expression).Member;
  38. if (member is PropertyInfo propertyInfo)
  39. {
  40. if (propertyInfo.CanWrite)
  41. {
  42. return;
  43. }
  44. }
  45. else
  46. {
  47. FieldInfo fieldInfo = (FieldInfo)member;
  48. if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral)
  49. {
  50. return;
  51. }
  52. }
  53. }
  54. throw new ArgumentException("Expression must be writeable", paramName);
  55. }
  56. public static void RequiresCanRead(Expression expression, string paramName, int idx = -1)
  57. {
  58. if (expression == null)
  59. throw new ArgumentNullException(TypeUtils.GetParamName(paramName, idx));
  60. ExpressionType nodeType = expression.NodeType;
  61. if (nodeType == ExpressionType.MemberAccess)
  62. {
  63. if (((MemberExpression)expression).Member is PropertyInfo propertyInfo && !propertyInfo.CanRead)
  64. {
  65. throw new ArgumentException("Expression must be readable", TypeUtils.GetParamName(paramName, idx));
  66. }
  67. }
  68. }
  69. public static VariableExpression Variable(Type type, string name = null)
  70. {
  71. Validate(type, false);
  72. return new VariableExpression(type, name);
  73. }
  74. public static AssignExpression Assign(Expression left, Expression right)
  75. {
  76. RequiresCanWrite(left, nameof(left));
  77. RequiresCanRead(right, nameof(right));
  78. TypeUtils.ValidateType(left.Type, nameof(left), true, true);
  79. TypeUtils.ValidateType(right.Type, nameof(right), true, true);
  80. if (!TypeUtils.AreReferenceAssignable(left.Type, right.Type))
  81. throw new ArgumentException($"Expression of type '{left.Type}' cannot be used for assignment to type '{right.Type}'");
  82. if (left.NodeType.ExprTEx() != ExpressionTypeEx.Variable)
  83. throw new NotSupportedException("Non-variable left hand operands to assignment is currently not supported");
  84. return new AssignExpression(left, right);
  85. }
  86. public static Expression Block(IEnumerable<VariableExpression> vars, params Expression[] body) => Block(vars, body);
  87. public static Expression Block(IEnumerable<VariableExpression> vars, IEnumerable<Expression> body)
  88. { // this is probably terrible performance-wise when executing, but i'm not giving up BlockExpression damnit!
  89. var varSet = new HashSet<VariableExpression>(vars);
  90. var bodyArr = body.ToArray();
  91. var finalExprList = new Stack<(List<Expression> list, BlockParseStackInfo info)>();
  92. var remaining = bodyArr.Length;
  93. var varParams = new Dictionary<VariableExpression, ParameterExpression>(varSet.Count);
  94. finalExprList.Push((new List<Expression>(remaining), default(BlockParseStackInfo)));
  95. while (remaining > 0)
  96. {
  97. var (list, info) = finalExprList.Pop();
  98. var targetExpr = bodyArr[bodyArr.Length - remaining--];
  99. if (targetExpr.NodeType.ExprTEx() == ExpressionTypeEx.Assign)
  100. {
  101. var assign = (AssignExpression)targetExpr;
  102. var left = (VariableExpression)assign.Left;
  103. var right = assign.Right;
  104. var param = Expression.Parameter(left.Type, left.Name);
  105. finalExprList.Push((list, info));
  106. finalExprList.Push((new List<Expression>(remaining), new BlockParseStackInfo
  107. {
  108. ValueExpr = BlockVisitReplaceVariables(right, varParams), Param = param
  109. }));
  110. varParams.Add(left, param);
  111. continue;
  112. }
  113. list.Add(BlockVisitReplaceVariables(targetExpr, varParams));
  114. finalExprList.Push((list, info));
  115. }
  116. var funcType = typeof(Func<>);
  117. Expression topExpr = null;
  118. while (finalExprList.Count > 0)
  119. {
  120. var (list, info) = finalExprList.Pop();
  121. // there is an optimization opportunity for consecutive assignments, but it needs to make sure they don't depend on each other
  122. if (topExpr != null) list.Add(topExpr);
  123. Expression last = null;
  124. Type lastType = null;
  125. var rest = new List<Expression<Action>>(list.Count);
  126. for (int i = 0; i < list.Count; i++)
  127. {
  128. var expr = list[i];
  129. if (i + 1 == list.Count)
  130. {
  131. var ty = expr.Type;
  132. if (ty == typeof(void))
  133. rest.Add(Expression.Lambda<Action>(expr));
  134. else
  135. {
  136. lastType = ty;
  137. var func = funcType.MakeGenericType(ty);
  138. last = Expression.Lambda(func, expr);
  139. }
  140. }
  141. else
  142. rest.Add(Expression.Lambda<Action>(expr));
  143. }
  144. Expression topBody;
  145. if (lastType != null)
  146. {
  147. var execSeq = ExecuteSequenceTyped.MakeGenericMethod(lastType);
  148. topBody = Expression.Call(null, execSeq, last, Expression.NewArrayInit(typeof(Action), rest.Cast<Expression>()));
  149. }
  150. else
  151. topBody = Expression.Call(null, ExecuteSequenceVoid, Expression.NewArrayInit(typeof(Action), rest.Cast<Expression>()));
  152. if (info.Param != null && info.ValueExpr != null)
  153. topExpr = Expression.Invoke(Expression.Lambda(topBody, info.Param), info.ValueExpr);
  154. else
  155. topExpr = topBody;
  156. }
  157. return topExpr;
  158. }
  159. /*
  160. * {
  161. * Console.WriteLine("ho");
  162. * int i = 3;
  163. * Console.WriteLine(i);
  164. * i // return last
  165. * }
  166. *
  167. * ExecuteSequence<int>(
  168. * () => (i =>
  169. * ExecuteSequence<int>(
  170. * () => i,
  171. * () => Console.WriteLine(i)
  172. * )
  173. * )(3),
  174. * () => Console.WriteLine("ho")
  175. * )
  176. */
  177. private struct BlockParseStackInfo
  178. {
  179. public Expression ValueExpr;
  180. public ParameterExpression Param;
  181. }
  182. private static Expression BlockVisitReplaceVariables(Expression expr, Dictionary<VariableExpression, ParameterExpression> mapping)
  183. {
  184. var binary = expr as BinaryExpression;
  185. var unary = expr as UnaryExpression;
  186. switch (expr.NodeType)
  187. {
  188. case ExpressionType.Add:
  189. return Expression.Add(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  190. case ExpressionType.AddChecked:
  191. return Expression.AddChecked(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  192. case ExpressionType.And:
  193. return Expression.And(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  194. case ExpressionType.AndAlso:
  195. return Expression.AndAlso(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  196. case ExpressionType.ArrayIndex:
  197. return Expression.ArrayIndex(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  198. case ExpressionType.Coalesce:
  199. return Expression.Coalesce(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  200. case ExpressionType.Divide:
  201. return Expression.Divide(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  202. case ExpressionType.Equal:
  203. return Expression.Equal(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  204. case ExpressionType.ExclusiveOr:
  205. return Expression.ExclusiveOr(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  206. case ExpressionType.GreaterThan:
  207. return Expression.GreaterThan(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  208. case ExpressionType.GreaterThanOrEqual:
  209. return Expression.GreaterThanOrEqual(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  210. case ExpressionType.LeftShift:
  211. return Expression.LeftShift(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  212. case ExpressionType.LessThan:
  213. return Expression.LessThan(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  214. case ExpressionType.LessThanOrEqual:
  215. return Expression.LessThanOrEqual(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  216. case ExpressionType.Modulo:
  217. return Expression.Modulo(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  218. case ExpressionType.Multiply:
  219. return Expression.Multiply(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  220. case ExpressionType.MultiplyChecked:
  221. return Expression.MultiplyChecked(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  222. case ExpressionType.NotEqual:
  223. return Expression.NotEqual(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  224. case ExpressionType.Or:
  225. return Expression.Or(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  226. case ExpressionType.OrElse:
  227. return Expression.OrElse(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  228. case ExpressionType.Power:
  229. return Expression.Power(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  230. case ExpressionType.RightShift:
  231. return Expression.RightShift(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  232. case ExpressionType.Subtract:
  233. return Expression.Subtract(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  234. case ExpressionType.SubtractChecked:
  235. return Expression.SubtractChecked(BlockVisitReplaceVariables(binary.Left, mapping), BlockVisitReplaceVariables(binary.Right, mapping));
  236. case ExpressionType.ArrayLength:
  237. return Expression.ArrayLength(BlockVisitReplaceVariables(unary.Operand, mapping));
  238. case ExpressionType.Negate:
  239. return Expression.Negate(BlockVisitReplaceVariables(unary.Operand, mapping));
  240. case ExpressionType.UnaryPlus:
  241. return Expression.UnaryPlus(BlockVisitReplaceVariables(unary.Operand, mapping));
  242. case ExpressionType.NegateChecked:
  243. return Expression.NegateChecked(BlockVisitReplaceVariables(unary.Operand, mapping));
  244. case ExpressionType.Not:
  245. return Expression.Not(BlockVisitReplaceVariables(unary.Operand, mapping));
  246. case ExpressionType.TypeAs:
  247. return Expression.TypeAs(BlockVisitReplaceVariables(unary.Operand, mapping), unary.Type);
  248. case ExpressionType.Call:
  249. var callExpr = expr as MethodCallExpression;
  250. return Expression.Call(BlockVisitReplaceVariables(callExpr.Object, mapping),
  251. callExpr.Method, callExpr.Arguments.Select(a => BlockVisitReplaceVariables(a, mapping)));
  252. case ExpressionType.Conditional:
  253. var condExpr = expr as ConditionalExpression;
  254. return Expression.Condition(BlockVisitReplaceVariables(condExpr.Test, mapping),
  255. BlockVisitReplaceVariables(condExpr.IfTrue, mapping), BlockVisitReplaceVariables(condExpr.IfFalse, mapping));
  256. case ExpressionType.Constant: return expr; // constants should be unchanged
  257. case ExpressionType.Convert:
  258. return Expression.Convert(BlockVisitReplaceVariables(unary.Operand, mapping), unary.Type, unary.Method);
  259. case ExpressionType.ConvertChecked:
  260. return Expression.ConvertChecked(BlockVisitReplaceVariables(unary.Operand, mapping), unary.Type, unary.Method);
  261. case ExpressionType.Invoke:
  262. var invokeExpr = expr as InvocationExpression;
  263. return Expression.Invoke(BlockVisitReplaceVariables(invokeExpr.Expression, mapping),
  264. invokeExpr.Arguments.Select(e => BlockVisitReplaceVariables(e, mapping)));
  265. case ExpressionType.Lambda:
  266. var lambdaExpr = expr as LambdaExpression;
  267. return Expression.Lambda(lambdaExpr.Type, BlockVisitReplaceVariables(lambdaExpr.Body, mapping), lambdaExpr.Parameters);
  268. case ExpressionType.ListInit:
  269. var listInitExpr = expr as ListInitExpression;
  270. return Expression.ListInit((NewExpression)BlockVisitReplaceVariables(listInitExpr.NewExpression, mapping),
  271. listInitExpr.Initializers.Select(i => i.AddMethod).First(),
  272. listInitExpr.Initializers.SelectMany(i => i.Arguments)
  273. .Select(e => BlockVisitReplaceVariables(e, mapping)));
  274. case ExpressionType.MemberAccess:
  275. var memberExpr = expr as MemberExpression;
  276. return Expression.MakeMemberAccess(BlockVisitReplaceVariables(memberExpr.Expression, mapping), memberExpr.Member);
  277. case ExpressionType.MemberInit:
  278. var memberInitExpr = expr as MemberInitExpression;
  279. return Expression.MemberInit((NewExpression)BlockVisitReplaceVariables(memberInitExpr.NewExpression, mapping),
  280. memberInitExpr.Bindings);
  281. case ExpressionType.New:
  282. var newExpr = expr as NewExpression;
  283. return Expression.New(newExpr.Constructor,
  284. newExpr.Arguments.Select(e => BlockVisitReplaceVariables(e, mapping)),
  285. newExpr.Members);
  286. case ExpressionType.NewArrayInit:
  287. var newArrayInitExpr = expr as NewArrayExpression;
  288. return Expression.NewArrayInit(newArrayInitExpr.Type,
  289. newArrayInitExpr.Expressions.Select(e => BlockVisitReplaceVariables(e, mapping)));
  290. case ExpressionType.NewArrayBounds:
  291. var newArrayBoundsExpr = expr as NewArrayExpression;
  292. return Expression.NewArrayBounds(newArrayBoundsExpr.Type,
  293. newArrayBoundsExpr.Expressions.Select(e => BlockVisitReplaceVariables(e, mapping)));
  294. case ExpressionType.Parameter: return expr; // like constant
  295. case ExpressionType.Quote:
  296. return Expression.Quote(BlockVisitReplaceVariables(unary.Operand, mapping));
  297. case ExpressionType.TypeIs:
  298. var typeIsExpr = expr as TypeBinaryExpression;
  299. return Expression.TypeIs(BlockVisitReplaceVariables(typeIsExpr.Expression, mapping), typeIsExpr.TypeOperand);
  300. default:
  301. switch (expr.NodeType.ExprTEx())
  302. {
  303. case ExpressionTypeEx.Variable:
  304. var varExpr = expr as VariableExpression;
  305. if (mapping.TryGetValue(varExpr, out var paramExpr))
  306. return paramExpr;
  307. else
  308. return varExpr; // not in scope in the current context, might be later
  309. case ExpressionTypeEx.Assign:
  310. throw new InvalidOperationException("Assign expression must appear directly inside a block expression");
  311. default:
  312. throw new ArgumentException($"Unhandled expression type '{expr.NodeType}'");
  313. }
  314. }
  315. }
  316. private static readonly MethodInfo ExecuteSequenceVoid = typeof(ExpressionEx).GetMethod(nameof(ExecuteSequence),
  317. BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(Action[]) }, null);
  318. private static void ExecuteSequence(params Action[] exec)
  319. {
  320. foreach (var act in exec) act();
  321. }
  322. private static readonly MethodInfo ExecuteSequenceTyped = typeof(ExpressionEx).GetMethod(nameof(ExecuteSequence),
  323. BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(Func<>), typeof(Action[]) }, null);
  324. private static T ExecuteSequence<T>(Func<T> last, params Action[] first)
  325. {
  326. ExecuteSequence(first);
  327. return last();
  328. }
  329. }
  330. internal enum ExpressionTypeEx
  331. {
  332. Variable = 46,
  333. Assign = 47
  334. }
  335. public class VariableExpression : Expression, IEquatable<VariableExpression>
  336. {
  337. public string Name { get; }
  338. internal VariableExpression(Type varType, string name) : base(ExpressionTypeEx.Variable.ExprT(), varType)
  339. => Name = name;
  340. public bool Equals(VariableExpression other)
  341. => Name == other.Name && Type == other.Type;
  342. }
  343. public class AssignExpression : Expression
  344. {
  345. public Expression Left { get; }
  346. public Expression Right { get; }
  347. internal AssignExpression(Expression left, Expression right) : base(ExpressionTypeEx.Assign.ExprT(), left.Type)
  348. {
  349. Left = left;
  350. Right = right;
  351. }
  352. }
  353. }