private Expression Convert(LinqExp.ConditionalExpression linqConditional) { return(Expression.Condition( ConvertExp(linqConditional.Test), ConvertExp(linqConditional.IfTrue), ConvertExp(linqConditional.IfFalse))); }
private Expression TryToCoalesceAsNullable(ConditionalExpression c) { var hasValueTarget = (Expression)null; var getValueTarget = (Expression)null; var matched = Matcher .For(c.Test).AsPropertyOrField() .Property(property => property.Name == "HasValue" && property.DeclaringType.IsGenericTypeDefinedAs(typeof(Nullable<>))) .Do(p => hasValueTarget = p.Expression) .For(c.IfTrue).AsConvert() .Type(typeof(Nullable<>)) .Operand().AsMethodCall() .Method(method => method.Name == "GetValueOrDefault" && method.DeclaringType.IsGenericTypeDefinedAs(typeof(Nullable<>))) .Do(call => getValueTarget = call.Object) .Matched; if (!matched) return null; if (hasValueTarget != getValueTarget) return null; return Expression.Coalesce(getValueTarget, c.IfFalse); }
protected override Expression VisitConditional(ConditionalExpression expression) { var test = this.Visit(expression.Test); var ifFalse = this.Visit(expression.IfFalse); var ifTrue = this.Visit(expression.IfTrue); if (test is BitBooleanExpression) { test = Expression.Equal(test, Expression.Constant(true)); } if (ifFalse.Type.GetUnwrappedNullableType() == typeof(bool) && !(ifFalse is BitBooleanExpression)) { ifFalse = BitBooleanExpression.Coerce(ifFalse); } if (ifTrue.Type.GetUnwrappedNullableType() == typeof(bool) && !(ifTrue is BitBooleanExpression)) { ifTrue = BitBooleanExpression.Coerce(ifTrue); } if (test != expression.Test || ifFalse != expression.IfFalse || ifTrue != expression.IfTrue) { return new BitBooleanExpression(Expression.Condition(test, ifTrue, ifFalse)); } else { return base.VisitConditional(expression); } }
protected ConditionalExpression UpdateConditional(ConditionalExpression c, Expression test, Expression ifTrue, Expression ifFalse) { if (test != c.Test || ifTrue != c.IfTrue || ifFalse != c.IfFalse) { return Expression.Condition(test, ifTrue, ifFalse); } return c; }
protected override Expression VisitConditional(ConditionalExpression node) { return Expression.Condition( Visit(node.Test), Visit(node.IfTrue), Visit(node.IfFalse)); }
private bool TryRemoveNullPropagation(ConditionalExpression node, out Expression condition) { condition = null; if (node.IfTrue.NodeType != ExpressionType.Constant) { return false; } if (node.Test.NodeType != ExpressionType.Equal) { return false; } var test = (BinaryExpression)node.Test; var constantExpr = (ConstantExpression)node.IfTrue; if (node.IfFalse.NodeType != ExpressionType.Call) { return false; } var memberExpr = (MethodCallExpression)node.IfFalse; _yankingNull = true; condition = Visit(memberExpr); _yankingNull = false; return true; }
public override Expression VisitConditional(ConditionalExpression c) { _Builder.Append("("); var conditional = base.VisitConditional(c); _Builder.Append(")"); return conditional; }
/// <summary> /// Constructor with an <see cref="ConditionalExpression"/> and an <see cref="ExpressionConverter"/>. /// </summary> /// <param name="expression">The original, not serializable <see cref="Expression"/>.</param> /// <param name="expConverter">The <see cref="ExpressionConverter"/> to convert contained <see cref="Expression">Expressions</see>.</param> public SerializableConditionalExpression(ConditionalExpression expression, ExpressionConverter expConverter) : base(expression, expConverter) { IfTrue = expression.IfTrue.MakeSerializable(expConverter); IfFalse = expression.IfFalse.MakeSerializable(expConverter); Test = expression.Test.MakeSerializable(expConverter); }
private bool TryRemoveNullPropagation(ConditionalExpression node, out Expression condition) { condition = null; if (!typeof(IEnumerable).IsAssignableFrom(node.Type)) { return false; } if (node.Test.NodeType != ExpressionType.Equal) { return false; } if (node.IfTrue.NodeType != ExpressionType.Call) { return false; } if (node.IfFalse.NodeType != ExpressionType.MemberAccess) { return false; } if (!node.Type.IsAssignableFrom(node.IfFalse.Type)) { return false; } _yankingNull = true; condition = node.IfFalse; _yankingNull = false; return true; }
protected override Expression VisitConditional(ConditionalExpression c) { c = (ConditionalExpression)base.VisitConditional(c); return TryToCoalesceAsObjects(c) ?? TryToCoalesceAsNullable(c) ?? c; }
protected static ConditionalExpression UpdateConditional(ConditionalExpression node, Expression test, Expression ifTrue, Expression ifFalse) { if (node.Test != test || node.IfTrue != ifTrue || node.IfFalse != ifFalse) { return Expression.Condition(test, ifTrue, ifFalse); } return node; }
/********* ** Protected methods *********/ /// <summary>Handles the conditional expression (equivalent to <c>.If {} .Else {}</c> in the sample expression tree in the <see cref="FixStringMethodsVisitor"/> remarks).</summary> /// <param name="original">The original expression.</param> /// <param name="ifElse">The conditional expression.</param> /// <returns>A reduced if/else statement if it contains any of the matched methods. Otherwise, the original expression.</returns> private Expression HandleConditionalExpression(Expression original, ConditionalExpression ifElse) { var elseExpression = ifElse.IfFalse as UnaryExpression; if (elseExpression != null) { var methodCallExpression = elseExpression.Operand as MethodCallExpression; if (methodCallExpression != null) { if (this.BooleanReturnStringMethods.Contains(methodCallExpression.Method)) { var methodCallReplacement = Expression.Call( methodCallExpression.Object, methodCallExpression.Method, methodCallExpression.Arguments); // Convert the result to a nullable boolean so the Expression.Equal works. var result = Expression.Convert(methodCallReplacement, typeof(bool?)); return result; } if (this.IntegerStringMethods.Contains(methodCallExpression.Method)) { var methodCallReplacement = Expression.Call( methodCallExpression.Object, methodCallExpression.Method, methodCallExpression.Arguments); var result = Expression.Convert(methodCallReplacement, typeof(int?)); return result; } } } var firstLevelMethodCallExpression = ifElse.IfFalse as MethodCallExpression; if (firstLevelMethodCallExpression != null) { // Using the method name and declaring type as strings because I don't want to add a dependency to the project for a simple check like that. if (firstLevelMethodCallExpression.Method.DeclaringType != null && firstLevelMethodCallExpression.Method.DeclaringType.FullName == "System.Web.Http.OData.Query.Expressions.ClrSafeFunctions" && (firstLevelMethodCallExpression.Method.Name == "SubstringStartAndLength" || firstLevelMethodCallExpression.Method.Name == "SubstringStart")) { var arguments = firstLevelMethodCallExpression.Arguments.Skip(1).ToArray(); return Expression.Call( firstLevelMethodCallExpression.Arguments[0], typeof(string).GetMethod("Substring", arguments.Select(x => typeof(int)).ToArray()), arguments); } if (this.ConcatStringMethods.Contains(firstLevelMethodCallExpression.Method)) { return Expression.Add(firstLevelMethodCallExpression.Arguments.First(), firstLevelMethodCallExpression.Arguments.Last(), firstLevelMethodCallExpression.Method); } } return original; }
void Conditional(ConditionalExpression expression) { Write("Conditional:"); Write("Test:"); Expression(expression.Test); Write("IfTrue:"); Expression(expression.IfTrue); Write("IfFalse:"); Expression(expression.IfFalse); }
protected override Expression VisitConditional(ConditionalExpression node) { DefaultExpression defaultExpression = node.IfFalse as DefaultExpression; if (defaultExpression == null || defaultExpression.Type != typeof (void) || !_canReplace) return base.VisitConditional(node); _canReplace = false; return Visit(Expression.IfThenElse(node.Test, node.IfTrue, Replacement)); }
protected override Expression VisitConditional(ConditionalExpression node) { var cond = Visit(node.Test); AddParameter(cond); var lhs = Visit(node.IfTrue); AddParameter(lhs); var rhs = Visit(node.IfFalse); AddParameter(rhs); return null; }
protected override Expression VisitConditional(ConditionalExpression expression) { var constantExpression = expression.Test as ConstantExpression; if (constantExpression != null) { return Convert.ToBoolean(constantExpression.Value) ? expression.IfTrue : expression.IfFalse; } return base.VisitConditional(expression); }
protected override Expression VisitConditionalExpression(ConditionalExpression expression) { var oldRequiresJoinForNonIdentifier = _requiresJoinForNonIdentifier; _requiresJoinForNonIdentifier = !_preventJoinsInConditionalTest && _requiresJoinForNonIdentifier; var newTest = VisitExpression(expression.Test); _requiresJoinForNonIdentifier = oldRequiresJoinForNonIdentifier; var newFalse = VisitExpression(expression.IfFalse); var newTrue = VisitExpression(expression.IfTrue); if ((newTest != expression.Test) || (newFalse != expression.IfFalse) || (newTrue != expression.IfTrue)) return Expression.Condition(newTest, newTrue, newFalse); return expression; }
internal static Condition Condition(ConditionalExpression expression) { return new Condition() { Type = expression.Type != expression.IfTrue.Type ? TypeRef.Serialize(expression.Type) : null, Test = Serialize(expression.Test), IfTrue = Serialize(expression.IfTrue), IfFalse = Serialize(expression.IfFalse), }.If(n => n.Type == null, n => n.TypeHint = TypeRef.Serialize(expression.Type)); }
protected override Expression VisitConditional(ConditionalExpression node) { if (node.IfFalse.NodeType != ExpressionType.Throw) { return base.VisitConditional(node); } Console.WriteLine(node.ToString()); Expression<Func<Object>> dummyFalseResult = () => DUMMY_RESULT; var invokeDummyFalseResult = Expression.Invoke(dummyFalseResult, null); return Expression.Condition(node.Test, node.IfTrue, invokeDummyFalseResult); }
protected override Expression VisitConditional(ConditionalExpression node) { Expression expression; if (TryRemoveNullPropagation(node, out expression)) { return expression; } if (_yankingNull && IsNullCheck(node.Test)) { return Visit(node.IfFalse); } return base.VisitConditional(node); }
protected override Expression VisitConditional(ConditionalExpression node) { var newExpr = base.VisitConditional(node); if (newExpr.NodeType == ExpressionType.Conditional) { var cond = (ConditionalExpression) newExpr; if (cond.Test.NodeType == ExpressionType.Constant) { var testResult = (bool) ((ConstantExpression) cond.Test).Value; return testResult ? cond.IfTrue : cond.IfFalse; } } return newExpr; }
/********* ** Protected methods *********/ /// <summary>Handles the conditional expression (equivalent to <c>.If {} .Else {}</c> in the sample expression tree in the <see cref="FixStringMethodsVisitor"/> remarks).</summary> /// <param name="original">The original expression.</param> /// <param name="ifElse">The conditional expression.</param> /// <returns>A reduced if/else statement if it contains any of the matched methods. Otherwise, the original expression.</returns> private Expression HandleConditionalExpression(Expression original, ConditionalExpression ifElse) { var binaryExpression = ifElse.Test as BinaryExpression; if (binaryExpression != null && binaryExpression.Right is ConstantExpression) { if (((ConstantExpression)binaryExpression.Right).Value == null) { // Ignore the null check and always return the value. return ifElse.IfFalse; } } Console.WriteLine(ifElse); return original; }
protected override Expression VisitConditionalExpression(ConditionalExpression expression) { var testExpression = VisitExpression(expression.Test); bool testExprResult; if (VisitorUtil.IsBooleanConstant(testExpression, out testExprResult)) { if (testExprResult) return VisitExpression(expression.IfTrue); return VisitExpression(expression.IfFalse); } return base.VisitConditionalExpression(expression); }
protected override Expression VisitConditional(ConditionalExpression c) { c = (ConditionalExpression)base.VisitConditional(c); if (c.Type != typeof(bool)) return c; var ifTrueAsConstant = c.IfTrue as ConstantExpression; var ifFalseAsConstant = c.IfFalse as ConstantExpression; if (ifTrueAsConstant == null && ifFalseAsConstant == null) return c; return table[Pair(Boolean(ifTrueAsConstant), Boolean(ifFalseAsConstant))].Invoke( c.Test, c.IfTrue, c.IfFalse ); }
public override Expression VisitConditional(ConditionalExpression c) { if (ExpressionUtils.IsRedundantEqualityTest(c)) { if (ExpressionUtils.IsConvertWithMethod(c.IfFalse, "Contains")) { return Visit((c.IfFalse as UnaryExpression).Operand); } else { return Visit(c.IfFalse); } } return base.VisitConditional(c); }
protected override Linq.Expression VisitConditional(Linq.ConditionalExpression node) { Condition condition; if (node.Type == typeof(void)) { if (node.IfFalse is Linq.DefaultExpression defaultExpression && defaultExpression.Type == typeof(void)) { condition = new IfThen(this.Current); } else { condition = new IfThenElse(this.Current) { IfFalse = this.VisitCacheParse(node.IfFalse), }; } }
private bool TryRemoveNullPropagation(ConditionalExpression node, out Expression condition) { condition = null; if (node.IfTrue.NodeType != ExpressionType.Constant) { return false; } if (node.Test.NodeType != ExpressionType.Equal) { return false; } var test = (BinaryExpression)node.Test; var constantExpr = (ConstantExpression)node.IfTrue; if (constantExpr.Type != typeof(bool)) { return false; } if ((bool)constantExpr.Value == true) { return false; } if (node.IfFalse.NodeType != ExpressionType.MemberAccess) { return false; } var memberExpr = (MemberExpression)node.IfFalse; if (!memberExpr.Member.DeclaringType.IsGenericType || memberExpr.Member.DeclaringType.GetGenericTypeDefinition() != typeof(Nullable<>)) { return false; } if (memberExpr.Expression != test.Left) { return false; } // After detecting the null propagation expression, proceed to remove the null guards _yankingNull = true; condition = Visit(memberExpr.Expression); _yankingNull = false; return true; }
public static ConditionalExpressionFingerprint Create(ConditionalExpression expression, ParserContext parserContext) { ExpressionFingerprint fingerprint = ExpressionFingerprint.Create(expression.Test, parserContext); if ((fingerprint == null) && (expression.Test != null)) { return null; } ExpressionFingerprint fingerprint2 = ExpressionFingerprint.Create(expression.IfTrue, parserContext); if ((fingerprint2 == null) && (expression.IfTrue != null)) { return null; } ExpressionFingerprint fingerprint3 = ExpressionFingerprint.Create(expression.IfFalse, parserContext); if ((fingerprint3 == null) && (expression.IfFalse != null)) { return null; } return new ConditionalExpressionFingerprint(expression) { Test = fingerprint, IfTrue = fingerprint2, IfFalse = fingerprint3 }; }
public static ConditionalExpressionFingerprint Create( ConditionalExpression expression, ParserContext context) { ExpressionFingerprint test = Create(expression.Test, context); if (test == null && expression.Test != null) return null; ExpressionFingerprint ifTrue = Create(expression.IfTrue, context); if (ifTrue == null && expression.IfTrue != null) return null; ExpressionFingerprint ifFalse = Create(expression.IfFalse, context); if (ifFalse == null && expression.IfFalse != null) return null; return new ConditionalExpressionFingerprint(expression) { _test = test, _ifTrue = ifTrue, _ifFalse = ifFalse }; }
protected override Expression VisitConditionalExpression(ConditionalExpression expression) { var result = base.VisitConditionalExpression(expression); if (!(result is ConditionalExpression)) return result; expression = (ConditionalExpression) result; if (!(expression.Test is BinaryExpression)) return expression; var test = (BinaryExpression)expression.Test; var testExpression = GetNonNullSide(test.Left, test.Right); var nonNullResult = GetNonNullSide(expression.IfFalse, expression.IfTrue); if (testExpression == null || nonNullResult == null) return expression; return nonNullResult; }
public static bool IsRedundantEqualityTest(ConditionalExpression c) { var constantExpression = c.IfTrue as ConstantExpression; if (constantExpression == null) return false; if (constantExpression.Value == null || constantExpression.Value.Equals(false)) { var binaryExpression = c.Test as BinaryExpression; if (binaryExpression == null) return false; if (binaryExpression.NodeType == ExpressionType.Equal || binaryExpression.Method != null && binaryExpression.Method.Name == "op_Equality") { if (binaryExpression.Right is ConstantExpression && (binaryExpression.Right as ConstantExpression).Value == null) { return true; } } } return false; }
protected override MSAst.Expression VisitConditional(MSAst.ConditionalExpression node) { MSAst.Expression t = Visit(node.Test); MSAst.Expression l; MSAst.Expression r; _insideConditionalBlock = true; try { l = Visit(node.IfTrue); r = Visit(node.IfFalse); } finally { _insideConditionalBlock = false; } if (t == node.Test && l == node.IfTrue && r == node.IfFalse) { return(node); } return(Ast.Condition(t, l, r, node.Type)); }
protected override Expression VisitConditional(ConditionalExpression expression) { Expression test; using (this.AcquireDisableCompareContext()) { test = this.Visit(expression.Test); } var ifTrue = this.Visit(expression.IfTrue); var ifFalse = this.Visit(expression.IfFalse); if (test != expression.Test || ifTrue != expression.IfTrue || ifFalse != expression.IfFalse) { return Expression.Condition(test, ifTrue, ifFalse); } else { return expression; } }
protected override Expression VisitConditional(ConditionalExpression c) => InvokeEvent(ConditionalVisited, c, base.VisitConditional);
#pragma warning restore CA1810 #pragma warning restore IDE0079 /// <summary> /// Visits the conditional expression and avoids inadvertening cloning of the node when unchanged due to a bug in the .NET Framework. /// </summary> /// <param name="node">The expression to visit.</param> /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns> /// <remarks> /// Works around a bug in ConditionalExpression.Update that causes inadvertent cloning of `IfThen` nodes. /// See https://github.com/dotnet/corefx/issues/3223 for more information. /// </remarks> protected override Expression VisitConditional(ConditionalExpression node) { // // No need to do some workaround if the quirk has been fixed. // if (s_hasIfThenQuirk) { // // The quirk exists, so only access IfFalse once to avoid triggering allocations // of spurious Default(typeof(void)) nodes. // var oldIfFalse = node.IfFalse; // // See ConditionalExpression.Make for the logic that leads to special-casing nodes // whose IfFalse child is a DefaultExpression of type void. // if (node.Type == typeof(void) && IsEmpty(oldIfFalse)) { var oldTest = node.Test; var oldIfTrue = node.IfTrue; // // Visit in the same order as the base class would do. We still visit IfFalse, // which always returns a new instance of Default(typeof(void)) when the quirk // is present. This is needed because a visitor subtype may want to do override // VisitDefault and change the IfFalse node. // var newTest = Visit(oldTest); var newIfTrue = Visit(oldIfTrue); var newIfFalse = Visit(oldIfFalse); // // Check child reference equality to avoid cloning, as we normally would do, // but check the case where the new IfFalse child node is Default(typeof(void)) // which we will classify as no change either. // // NB: In case the user rewrites the expression tree to ensure uniqueness of // references of nodes (i.e. intentional cloning), it's fine to keep the // node as-is, because ConditionalExpression's behavior of returning a new // instance of Default(typeof(void)) for each access to IfFalse ensures // the same uniqueness property. Stated otherwise, the unique Default node // handed to us by such a user doesn't get stored in ConditionalExpression // anyway, in favor of GetFalse() returning new Default(typeof(void)) nodes // for each access to IfFalse. // if (newTest == oldTest && newIfTrue == oldIfTrue && (newIfFalse == oldIfFalse || IsEmpty(newIfFalse))) { return(node); } // // If anything changed for real, call Update as usual. // return(node.Update(newTest, newIfTrue, newIfFalse)); } } return(base.VisitConditional(node)); }
protected internal virtual Expression VisitConditional(ConditionalExpression node) { return(node.Update(this.Visit(node.Test), this.Visit(node.IfTrue), this.Visit(node.IfFalse))); }
public ConditionalExpressionProxy(ConditionalExpression node) { ContractUtils.RequiresNotNull(node, nameof(node)); _node = node; }
protected virtual Expression VisitConditional(ConditionalExpression node) { throw new NotImplementedException(); }
protected internal virtual new Expression VisitConditional(ConditionalExpression node) { return(default(Expression)); }
protected virtual void VisitConditional(ConditionalExpression conditional) { Visit(conditional.Test); Visit(conditional.IfTrue); Visit(conditional.IfFalse); }
protected override ExpressionTree MakeConditional(ConditionalExpression node, ExpressionTree test, ExpressionTree ifTrue, ExpressionTree ifFalse) => new ExpressionTree <ConditionalExpression>(node, test, ifTrue, ifFalse);
protected override Expression VisitConditional(System.Linq.Expressions.ConditionalExpression node) { return(base.VisitConditional(node)); }