| | 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.Collections.ObjectModel; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Linq.Expressions; |
| | 9 | |
|
| | 10 | | namespace Azure.Data.Tables.Queryable |
| | 11 | | { |
| | 12 | | internal abstract class LinqExpressionVisitor |
| | 13 | | { |
| | 14 | | internal virtual Expression Visit(Expression exp) |
| | 15 | | { |
| 18108 | 16 | | if (exp == null) |
| | 17 | | { |
| 1148 | 18 | | return exp; |
| | 19 | | } |
| | 20 | |
|
| 16960 | 21 | | switch (exp.NodeType) |
| | 22 | | { |
| | 23 | | case ExpressionType.UnaryPlus: |
| | 24 | | case ExpressionType.Negate: |
| | 25 | | case ExpressionType.NegateChecked: |
| | 26 | | case ExpressionType.Not: |
| | 27 | | case ExpressionType.Convert: |
| | 28 | | case ExpressionType.ConvertChecked: |
| | 29 | | case ExpressionType.ArrayLength: |
| | 30 | | case ExpressionType.Quote: |
| | 31 | | case ExpressionType.TypeAs: |
| 300 | 32 | | return VisitUnary((UnaryExpression)exp); |
| | 33 | | case ExpressionType.Add: |
| | 34 | | case ExpressionType.AddChecked: |
| | 35 | | case ExpressionType.Subtract: |
| | 36 | | case ExpressionType.SubtractChecked: |
| | 37 | | case ExpressionType.Multiply: |
| | 38 | | case ExpressionType.MultiplyChecked: |
| | 39 | | case ExpressionType.Divide: |
| | 40 | | case ExpressionType.Modulo: |
| | 41 | | case ExpressionType.Power: |
| | 42 | | case ExpressionType.And: |
| | 43 | | case ExpressionType.AndAlso: |
| | 44 | | case ExpressionType.Or: |
| | 45 | | case ExpressionType.OrElse: |
| | 46 | | case ExpressionType.LessThan: |
| | 47 | | case ExpressionType.LessThanOrEqual: |
| | 48 | | case ExpressionType.GreaterThan: |
| | 49 | | case ExpressionType.GreaterThanOrEqual: |
| | 50 | | case ExpressionType.Equal: |
| | 51 | | case ExpressionType.NotEqual: |
| | 52 | | case ExpressionType.Coalesce: |
| | 53 | | case ExpressionType.ArrayIndex: |
| | 54 | | case ExpressionType.RightShift: |
| | 55 | | case ExpressionType.LeftShift: |
| | 56 | | case ExpressionType.ExclusiveOr: |
| 4592 | 57 | | return VisitBinary((BinaryExpression)exp); |
| | 58 | | case ExpressionType.Constant: |
| 2988 | 59 | | return VisitConstant((ConstantExpression)exp); |
| | 60 | | case ExpressionType.Parameter: |
| 2988 | 61 | | return VisitParameter((ParameterExpression)exp); |
| | 62 | | case ExpressionType.MemberAccess: |
| 3180 | 63 | | return VisitMemberAccess((MemberExpression)exp); |
| | 64 | | case ExpressionType.Call: |
| 1064 | 65 | | return VisitMethodCall((MethodCallExpression)exp); |
| | 66 | | case ExpressionType.Lambda: |
| 1840 | 67 | | return VisitLambda((LambdaExpression)exp); |
| | 68 | | case ExpressionType.Invoke: |
| 8 | 69 | | return VisitInvocation((InvocationExpression)exp); |
| | 70 | | default: |
| 0 | 71 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "The expression type {0} i |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | internal virtual Expression VisitUnary(UnaryExpression u) |
| | 76 | | { |
| 284 | 77 | | Expression operand = Visit(u.Operand); |
| 284 | 78 | | if (operand != u.Operand) |
| | 79 | | { |
| 8 | 80 | | return Expression.MakeUnary(u.NodeType, operand, u.Type, u.Method); |
| | 81 | | } |
| | 82 | |
|
| 276 | 83 | | return u; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | internal virtual Expression VisitBinary(BinaryExpression b) |
| | 87 | | { |
| 3444 | 88 | | Expression left = Visit(b.Left); |
| 3444 | 89 | | Expression right = Visit(b.Right); |
| 3444 | 90 | | Expression conversion = Visit(b.Conversion); |
| 3444 | 91 | | if (left != b.Left || right != b.Right || conversion != b.Conversion) |
| | 92 | | { |
| 830 | 93 | | if (b.NodeType == ExpressionType.Coalesce && b.Conversion != null) |
| | 94 | | { |
| 0 | 95 | | return Expression.Coalesce(left, right, conversion as LambdaExpression); |
| | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 830 | 99 | | return Expression.MakeBinary(b.NodeType, left, right, b.IsLiftedToNull, b.Method); |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 2614 | 103 | | return b; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | internal virtual Expression VisitConstant(ConstantExpression c) |
| | 107 | | { |
| 2180 | 108 | | return c; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | internal virtual Expression VisitParameter(ParameterExpression p) |
| | 112 | | { |
| 2400 | 113 | | return p; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | internal virtual Expression VisitMemberAccess(MemberExpression m) |
| | 117 | | { |
| 2592 | 118 | | Expression exp = Visit(m.Expression); |
| 2592 | 119 | | if (exp != m.Expression) |
| | 120 | | { |
| 0 | 121 | | return Expression.MakeMemberAccess(exp, m.Member); |
| | 122 | | } |
| | 123 | |
|
| 2592 | 124 | | return m; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | internal virtual Expression VisitMethodCall(MethodCallExpression m) |
| | 128 | | { |
| 852 | 129 | | Expression obj = Visit(m.Object); |
| | 130 | |
|
| 852 | 131 | | IEnumerable<Expression> args = VisitExpressionList(m.Arguments); |
| 852 | 132 | | if (obj != m.Object || args != m.Arguments) |
| | 133 | | { |
| 40 | 134 | | return Expression.Call(obj, m.Method, args); |
| | 135 | | } |
| | 136 | |
|
| 812 | 137 | | return m; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | internal virtual ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCollection<Expression> original) |
| | 141 | | { |
| 860 | 142 | | List<Expression> list = null; |
| 4300 | 143 | | for (int i = 0, n = original.Count; i < n; i++) |
| | 144 | | { |
| 860 | 145 | | Expression p = Visit(original[i]); |
| 860 | 146 | | if (list != null) |
| | 147 | | { |
| 0 | 148 | | list.Add(p); |
| | 149 | | } |
| 860 | 150 | | else if (p != original[i]) |
| | 151 | | { |
| 40 | 152 | | list = new List<Expression>(n); |
| 0 | 153 | | for (int j = 0; j < i; j++) |
| | 154 | | { |
| 0 | 155 | | list.Add(original[j]); |
| | 156 | | } |
| | 157 | |
|
| 40 | 158 | | list.Add(p); |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 860 | 162 | | if (list != null) |
| | 163 | | { |
| 40 | 164 | | return new ReadOnlyCollection<Expression>(list); |
| | 165 | | } |
| | 166 | |
|
| 820 | 167 | | return original; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | internal virtual Expression VisitLambda(LambdaExpression lambda) |
| | 171 | | { |
| 1380 | 172 | | Expression body = Visit(lambda.Body); |
| 1380 | 173 | | if (body != lambda.Body) |
| | 174 | | { |
| 424 | 175 | | return Expression.Lambda(lambda.Type, body, lambda.Parameters); |
| | 176 | | } |
| | 177 | |
|
| 956 | 178 | | return lambda; |
| | 179 | | } |
| | 180 | |
|
| | 181 | | internal virtual Expression VisitInvocation(InvocationExpression iv) |
| | 182 | | { |
| 8 | 183 | | IEnumerable<Expression> args = VisitExpressionList(iv.Arguments); |
| 8 | 184 | | Expression expr = Visit(iv.Expression); |
| 8 | 185 | | if (args != iv.Arguments || expr != iv.Expression) |
| | 186 | | { |
| 0 | 187 | | return Expression.Invoke(expr, args); |
| | 188 | | } |
| | 189 | |
|
| 8 | 190 | | return iv; |
| | 191 | | } |
| | 192 | | } |
| | 193 | | } |