Example #1
0
        public static ITranslation For(MethodCallExpression methodCall, ITranslationContext context)
        {
            var method     = new BclMethodWrapper(methodCall.Method);
            var parameters = new ParameterSetTranslation(method, methodCall.Arguments, context);

            if (context.Settings.ConvertPropertyMethodsToSimpleSyntax && IsDirectPropertyMethodCall(methodCall, out var propertyInfo))
            {
                var objTranslation = context.GetTranslationFor(methodCall.Object);
                var propertyAccess = new MemberAccessTranslation(objTranslation, propertyInfo.Name, propertyInfo.PropertyType);

                var isAssignment = methodCall.Method.ReturnType == typeof(void);
                if (isAssignment)
                {
                    var valueExpr = methodCall.Arguments.First();
                    return(new AssignmentTranslation(Assign, propertyAccess, valueExpr, context));
                }
                else
                {
                    return(propertyAccess);
                }
            }

            if (IsStringConcatCall(methodCall))
            {
                return(new StringConcatenationTranslation(Call, methodCall.Arguments, context));
            }

            if (methodCall.Method.IsImplicitOperator())
            {
                return(new CodeBlockTranslation(parameters[0]).WithNodeType(Call));
            }

            var subject = GetSubjectTranslation(methodCall, context);

            if (IsIndexedPropertyAccess(methodCall))
            {
                return(new IndexAccessTranslation(subject, parameters, methodCall.Type));
            }

            parameters = parameters.WithParentheses();

            if (methodCall.Method.IsExplicitOperator())
            {
                return(CastTranslation.ForExplicitOperator(
                           parameters[0],
                           context.GetTranslationFor(methodCall.Method.ReturnType)));
            }

            var methodCallTranslation = new StandardMethodCallTranslation(Call, subject, method, parameters, context);

            if (context.IsPartOfMethodCallChain(methodCall))
            {
                methodCallTranslation.AsPartOfMethodCallChain();
            }

            return(methodCallTranslation);
        }
Example #2
0
        public static ITranslation For(MethodCallExpression methodCall, ITranslationContext context)
        {
            if (methodCall.Method.IsPropertyGetterOrSetterCall(out var property))
            {
                var getterTranslation = new PropertyGetterTranslation(methodCall, property, context);

                if (methodCall.Method.ReturnType != typeof(void))
                {
                    return(getterTranslation);
                }

                return(new PropertySetterTranslation(methodCall, getterTranslation, context));
            }

            if (IsStringConcatCall(methodCall))
            {
                return(new StringConcatenationTranslation(Call, methodCall.Arguments, context));
            }

            var method     = new BclMethodWrapper(methodCall.Method);
            var parameters = new ParameterSetTranslation(method, methodCall.Arguments, context);

            if (methodCall.Method.IsImplicitOperator())
            {
                return(new CodeBlockTranslation(parameters[0]).WithNodeType(Call));
            }

            var subject = GetSubjectTranslation(methodCall, context);

            if (IsIndexedPropertyAccess(methodCall))
            {
                return(new IndexAccessTranslation(subject, parameters, methodCall.Type));
            }

            parameters = parameters.WithParentheses();

            if (methodCall.Method.IsExplicitOperator())
            {
                return(CastTranslation.ForExplicitOperator(
                           parameters[0],
                           context.GetTranslationFor(methodCall.Method.ReturnType)));
            }

            var methodCallTranslation = new StandardMethodCallTranslation(Call, subject, method, parameters, context);

            if (context.IsPartOfMethodCallChain(methodCall))
            {
                methodCallTranslation.AsPartOfMethodCallChain();
            }

            return(methodCallTranslation);
        }
        public static ITranslation For(TypeBinaryExpression typeBinary, ITranslationContext context)
        {
            ITranslation operandTranslation;

            if (_reduceTypeEqualMethod != null)
            {
                try
                {
                    // TypeEqual '123 TypeEqual int' is reduced to a Block with the Expressions '123' and 'true',
                    // 'o TypeEqual string' is reduced to (o != null) && (o is string):
                    var reducedTypeBinary = (Expression)_reduceTypeEqualMethod.Invoke(typeBinary, null);

                    operandTranslation = context.GetTranslationFor(reducedTypeBinary);

                    if (operandTranslation.NodeType == ExpressionType.Block)
                    {
                        operandTranslation = ((BlockTranslation)operandTranslation).WithoutTermination();
                    }

                    return(operandTranslation.WithTypes(ExpressionType.TypeEqual, typeof(bool)));
                }
                catch
                {
                    // Unable to invoke the non-public ReduceTypeEqual method - ignore
                }
            }

            operandTranslation = context.GetTranslationFor(typeBinary.Expression);
            var typeNameTranslation = context.GetTranslationFor(typeBinary.TypeOperand);

            if (typeBinary.TypeOperand.IsClass())
            {
                return(CastTranslation.For(typeBinary, context));
            }

            return(new TypeOfTranslation(operandTranslation, typeNameTranslation, context));
        }
Example #4
0
 public static bool ShouldWriteInParentheses(this ITranslation translation)
 {
     return((translation.NodeType == ExpressionType.Conditional) ||
            translation.IsBinary() || translation.IsAssignment() ||
            CastTranslation.IsCast(translation.NodeType));
 }
        public ITranslation GetTranslationFor(Expression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            switch (expression.NodeType)
            {
            case Decrement:
            case Increment:
            case IsFalse:
            case IsTrue:
            case OnesComplement:
            case PostDecrementAssign:
            case PostIncrementAssign:
            case PreDecrementAssign:
            case PreIncrementAssign:
            case UnaryPlus:
                return(new UnaryTranslation((UnaryExpression)expression, this));

            case Add:
            case AddChecked:
            case And:
            case AndAlso:
            case Coalesce:
            case Divide:
            case Equal:
            case ExclusiveOr:
            case GreaterThan:
            case GreaterThanOrEqual:
            case LeftShift:
            case LessThan:
            case LessThanOrEqual:
            case Modulo:
            case Multiply:
            case MultiplyChecked:
            case NotEqual:
            case Or:
            case OrElse:
            case Power:
            case RightShift:
            case Subtract:
            case SubtractChecked:
                return(BinaryTranslation.For((BinaryExpression)expression, this));

            case AddAssign:
            case AddAssignChecked:
            case AndAssign:
            case Assign:
            case DivideAssign:
            case ExclusiveOrAssign:
            case LeftShiftAssign:
            case ModuloAssign:
            case MultiplyAssign:
            case MultiplyAssignChecked:
            case OrAssign:
            case PowerAssign:
            case RightShiftAssign:
            case SubtractAssign:
            case SubtractAssignChecked:
                return(new AssignmentTranslation((BinaryExpression)expression, this));

            case ArrayIndex:
                return(new IndexAccessTranslation((BinaryExpression)expression, this));

            case ArrayLength:
                return(new ArrayLengthTranslation((UnaryExpression)expression, this));

            case Block:
                return(new BlockTranslation((BlockExpression)expression, this));

            case Call:
                return(MethodCallTranslation.For((MethodCallExpression)expression, this));

            case Conditional:
                return(ConditionalTranslation.For((ConditionalExpression)expression, this));

            case Constant:
                return(ConstantTranslation.For((ConstantExpression)expression, this));

            case ExpressionType.Convert:
            case ConvertChecked:
            case TypeAs:
            case Unbox:
                return(CastTranslation.For((UnaryExpression)expression, this));

            case DebugInfo:
                return(DebugInfoTranslation.For((DebugInfoExpression)expression, this));

            case Default:
                return(new DefaultValueTranslation(expression, this));

            case Dynamic:
                return(DynamicTranslation.For((DynamicExpression)expression, this));

            case Extension:
                return(new FixedValueTranslation(expression, this));

            case Goto:
                return(GotoTranslation.For((GotoExpression)expression, this));

            case Index:
                return(new IndexAccessTranslation((IndexExpression)expression, this));

            case Invoke:
                return(MethodCallTranslation.For((InvocationExpression)expression, this));

            case Label:
                return(new LabelTranslation((LabelExpression)expression, this));

            case Lambda:
                return(new LambdaTranslation((LambdaExpression)expression, this));

            case ListInit:
                return(ListInitialisationTranslation.For((ListInitExpression)expression, this));

            case Loop:
                return(new LoopTranslation((LoopExpression)expression, this));

            case MemberAccess:
                return(new MemberAccessTranslation((MemberExpression)expression, this));

            case MemberInit:
                return(MemberInitialisationTranslation.For((MemberInitExpression)expression, this));

            case Negate:
            case NegateChecked:
            case Not:
                return(new NegationTranslation((UnaryExpression)expression, this));

            case New:
                return(NewingTranslation.For((NewExpression)expression, this));

            case NewArrayBounds:
                return(new NewArrayTranslation((NewArrayExpression)expression, this));

            case NewArrayInit:
                return(ArrayInitialisationTranslation.For((NewArrayExpression)expression, this));

            case Parameter:
                return(ParameterTranslation.For((ParameterExpression)expression, this));

            case Quote:
                return(QuotedLambdaTranslation.For((UnaryExpression)expression, this));

            case RuntimeVariables:
                return(RuntimeVariablesTranslation.For((RuntimeVariablesExpression)expression, this));

            case Switch:
                return(new SwitchTranslation((SwitchExpression)expression, this));

            case Throw:
                return(new ThrowTranslation((UnaryExpression)expression, this));

            case Try:
                return(new TryCatchTranslation((TryExpression)expression, this));

            case TypeEqual:
                return(TypeEqualTranslation.For((TypeBinaryExpression)expression, this));

            case TypeIs:
                return(CastTranslation.For((TypeBinaryExpression)expression, this));
            }

            return(new FixedValueTranslation(expression, this));
        }