1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Dynamic;
using Microsoft.Scripting.Utils;
using AstUtils = Microsoft.Scripting.Ast.Utils;
namespace Microsoft.Scripting.Ast {
public static partial class Utils {
/// <summary>
/// Null coalescing expression
/// {result} ::= ((tmp = {_left}) == null) ? {right} : tmp
/// '??' operator in C#.
/// </summary>
public static Expression Coalesce(Expression left, Expression right, out ParameterExpression temp) {
return CoalesceInternal(left, right, null, false, out temp);
}
/// <summary>
/// True coalescing expression.
/// {result} ::= IsTrue(tmp = {left}) ? {right} : tmp
/// Generalized AND semantics.
/// </summary>
public static Expression CoalesceTrue(Expression left, Expression right, MethodInfo isTrue, out ParameterExpression temp) {
ContractUtils.RequiresNotNull(isTrue, "isTrue");
return CoalesceInternal(left, right, isTrue, false, out temp);
}
/// <summary>
/// False coalescing expression.
/// {result} ::= IsTrue(tmp = {left}) ? tmp : {right}
/// Generalized OR semantics.
/// </summary>
public static Expression CoalesceFalse(Expression left, Expression right, MethodInfo isTrue, out ParameterExpression temp) {
ContractUtils.RequiresNotNull(isTrue, "isTrue");
return CoalesceInternal(left, right, isTrue, true, out temp);
}
private static Expression CoalesceInternal(Expression left, Expression right, MethodInfo isTrue, bool isReverse, out ParameterExpression temp) {
ContractUtils.RequiresNotNull(left, "left");
ContractUtils.RequiresNotNull(right, "right");
// A bit too strict, but on a safe side.
ContractUtils.Requires(left.Type == right.Type, "Expression types must match");
temp = Expression.Variable(left.Type, "tmp_left");
Expression condition;
if (isTrue != null) {
ContractUtils.Requires(isTrue.ReturnType == typeof(bool), "isTrue", "Predicate must return bool.");
ParameterInfo[] parameters = isTrue.GetParameters();
ContractUtils.Requires(parameters.Length == 1, "isTrue", "Predicate must take one parameter.");
ContractUtils.Requires(isTrue.IsStatic && isTrue.IsPublic, "isTrue", "Predicate must be public and static.");
Type pt = parameters[0].ParameterType;
ContractUtils.Requires(TypeUtils.CanAssign(pt, left.Type), "left", "Incorrect left expression type");
condition = Expression.Call(isTrue, Expression.Assign(temp, left));
} else {
ContractUtils.Requires(TypeUtils.CanCompareToNull(left.Type), "left", "Incorrect left expression type");
condition = Expression.Equal(Expression.Assign(temp, left), AstUtils.Constant(null, left.Type));
}
Expression t, f;
if (isReverse) {
t = temp;
f = right;
} else {
t = right;
f = temp;
}
return Expression.Condition(condition, t, f);
}
public static Expression Coalesce(LambdaBuilder builder, Expression left, Expression right) {
ParameterExpression temp;
Expression result = Coalesce(left, right, out temp);
builder.AddHiddenVariable(temp);
return result;
}
/// <summary>
/// True coalescing expression.
/// {result} ::= IsTrue(tmp = {left}) ? {right} : tmp
/// Generalized AND semantics.
/// </summary>
public static Expression CoalesceTrue(LambdaBuilder builder, Expression left, Expression right, MethodInfo isTrue) {
ContractUtils.RequiresNotNull(isTrue, "isTrue");
ParameterExpression temp;
Expression result = CoalesceTrue(left, right, isTrue, out temp);
builder.AddHiddenVariable(temp);
return result;
}
/// <summary>
/// False coalescing expression.
/// {result} ::= IsTrue(tmp = {left}) ? tmp : {right}
/// Generalized OR semantics.
/// </summary>
public static Expression CoalesceFalse(LambdaBuilder builder, Expression left, Expression right, MethodInfo isTrue) {
ContractUtils.RequiresNotNull(isTrue, "isTrue");
ParameterExpression temp;
Expression result = CoalesceFalse(left, right, isTrue, out temp);
builder.AddHiddenVariable(temp);
return result;
}
}
}
|