| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Linq.Expressions; |
| | 8 | |
|
| | 9 | | namespace Azure.Data.Tables.Queryable |
| | 10 | | { |
| | 11 | | internal static class Evaluator |
| | 12 | | { |
| | 13 | | internal static Expression PartialEval(Expression expression, Func<Expression, bool> canBeEvaluated) |
| | 14 | | { |
| 460 | 15 | | Nominator nominator = new Nominator(canBeEvaluated); |
| 460 | 16 | | HashSet<Expression> candidates = nominator.Nominate(expression); |
| 460 | 17 | | return new SubtreeEvaluator(candidates).Eval(expression); |
| | 18 | | } |
| | 19 | |
|
| | 20 | | internal static Expression PartialEval(Expression expression) |
| | 21 | | { |
| 460 | 22 | | return PartialEval(expression, Evaluator.CanBeEvaluatedLocally); |
| | 23 | | } |
| | 24 | |
|
| | 25 | | private static bool CanBeEvaluatedLocally(Expression expression) |
| | 26 | | { |
| 2898 | 27 | | return expression.NodeType != ExpressionType.Parameter && |
| 2898 | 28 | | expression.NodeType != ExpressionType.Lambda |
| 2898 | 29 | | && expression.NodeType != (ExpressionType)ResourceExpressionType.RootResourceSet; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | internal class SubtreeEvaluator : LinqExpressionVisitor |
| | 33 | | { |
| | 34 | | private HashSet<Expression> candidates; |
| | 35 | |
|
| 460 | 36 | | internal SubtreeEvaluator(HashSet<Expression> candidates) |
| | 37 | | { |
| 460 | 38 | | this.candidates = candidates; |
| 460 | 39 | | } |
| | 40 | |
|
| | 41 | | internal Expression Eval(Expression exp) |
| | 42 | | { |
| 460 | 43 | | return Visit(exp); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | internal override Expression Visit(Expression exp) |
| | 47 | | { |
| 5554 | 48 | | if (exp == null) |
| | 49 | | { |
| 1148 | 50 | | return null; |
| | 51 | | } |
| | 52 | |
|
| 4406 | 53 | | if (candidates.Contains(exp)) |
| | 54 | | { |
| 1084 | 55 | | return Evaluate(exp); |
| | 56 | | } |
| | 57 | |
|
| 3322 | 58 | | return base.Visit(exp); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | private static Expression Evaluate(Expression e) |
| | 62 | | { |
| 1084 | 63 | | if (e.NodeType == ExpressionType.Constant) |
| | 64 | | { |
| 626 | 65 | | return e; |
| | 66 | | } |
| | 67 | |
|
| 458 | 68 | | LambdaExpression lambda = Expression.Lambda(e); |
| 458 | 69 | | Delegate fn = lambda.Compile(); |
| 458 | 70 | | object constantValue = fn.DynamicInvoke(null); |
| | 71 | | Debug.Assert(!(constantValue is Expression), "!(constantValue is Expression)"); |
| | 72 | |
|
| 458 | 73 | | Type constantType = e.Type; |
| 458 | 74 | | if (constantValue != null && constantType.IsArray && constantType.GetElementType() == constantValue.GetT |
| | 75 | | { |
| 36 | 76 | | constantType = constantValue.GetType(); |
| | 77 | | } |
| | 78 | |
|
| 458 | 79 | | return Expression.Constant(constantValue, constantType); |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| | 83 | | internal class Nominator : LinqExpressionVisitor |
| | 84 | | { |
| | 85 | | private Func<Expression, bool> functionCanBeEvaluated; |
| | 86 | |
|
| | 87 | | private HashSet<Expression> candidates; |
| | 88 | |
|
| | 89 | | private bool cannotBeEvaluated; |
| | 90 | |
|
| 460 | 91 | | internal Nominator(Func<Expression, bool> functionCanBeEvaluated) |
| | 92 | | { |
| 460 | 93 | | this.functionCanBeEvaluated = functionCanBeEvaluated; |
| 460 | 94 | | } |
| | 95 | |
|
| | 96 | | internal HashSet<Expression> Nominate(Expression expression) |
| | 97 | | { |
| 460 | 98 | | candidates = new HashSet<Expression>(EqualityComparer<Expression>.Default); |
| 460 | 99 | | Visit(expression); |
| 460 | 100 | | return candidates; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | internal override Expression Visit(Expression expression) |
| | 104 | | { |
| 6580 | 105 | | if (expression != null) |
| | 106 | | { |
| 5412 | 107 | | bool saveCannotBeEvaluated = cannotBeEvaluated; |
| 5412 | 108 | | cannotBeEvaluated = false; |
| | 109 | |
|
| 5412 | 110 | | base.Visit(expression); |
| | 111 | |
|
| 5412 | 112 | | if (!cannotBeEvaluated) |
| | 113 | | { |
| 2898 | 114 | | if (functionCanBeEvaluated(expression)) |
| | 115 | | { |
| 2090 | 116 | | candidates.Add(expression); |
| | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 808 | 120 | | cannotBeEvaluated = true; |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| 5412 | 124 | | cannotBeEvaluated |= saveCannotBeEvaluated; |
| | 125 | | } |
| | 126 | |
|
| 6580 | 127 | | return expression; |
| | 128 | | } |
| | 129 | | } |
| | 130 | | } |
| | 131 | | } |