public NewArrayTranslation(NewArrayExpression newArray, ITranslationContext context)
        {
            Type = newArray.Type;
            _typeNameTranslation = context.GetTranslationFor(newArray.Type.GetElementType());

            var translationSize = _typeNameTranslation.TranslationSize + 6;
            var formattingSize  = _typeNameTranslation.FormattingSize;

            if (newArray.Expressions.Count == 0)
            {
                _boundTranslations = Enumerable <ITranslation> .EmptyArray;
            }
            else
            {
                _boundTranslationCount = newArray.Expressions.Count;
                _boundTranslations     = new ITranslation[_boundTranslationCount];

                for (var i = 0; i < _boundTranslationCount; ++i)
                {
                    var boundTranslation = context.GetTranslationFor(newArray.Expressions[i]);

                    _boundTranslations[i] = boundTranslation;
                    translationSize      += boundTranslation.TranslationSize + 2;
                    formattingSize       += boundTranslation.FormattingSize;
                }
            }

            TranslationSize = translationSize;
            FormattingSize  = formattingSize;
        }
Example #2
0
 public static ITranslation GetSubjectTranslation(
     this MethodCallExpression methodCall,
     ITranslationContext context)
 {
     return(context.GetTranslationFor(methodCall.GetSubject()) ??
            context.GetTranslationFor(methodCall.Method.DeclaringType));
 }
Example #3
0
        public NewArrayTranslation(NewArrayExpression newArray, ITranslationContext context)
        {
            Type = newArray.Type;
            _typeNameTranslation = context.GetTranslationFor(newArray.Type.GetElementType());

            var estimatedSize = _typeNameTranslation.EstimatedSize + 6;

            if (newArray.Expressions.Count == 0)
            {
                _boundTranslations = Enumerable <ITranslation> .EmptyArray;
            }
            else
            {
                _boundTranslations = new ITranslation[newArray.Expressions.Count];

                for (var i = 0; ;)
                {
                    var boundTranslation = context.GetTranslationFor(newArray.Expressions[i]);

                    _boundTranslations[i] = boundTranslation;
                    estimatedSize        += boundTranslation.EstimatedSize + 2;

                    if (++i == _boundTranslations.Length)
                    {
                        break;
                    }
                }
            }

            EstimatedSize = estimatedSize;
        }
Example #4
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 #5
0
 private BinaryTranslation(BinaryExpression binary, ITranslationContext context)
     : base(IsCheckedBinary(binary.NodeType), "(", ")")
 {
     NodeType = binary.NodeType;
     Type     = binary.Type;
     _leftOperandTranslation = context.GetTranslationFor(binary.Left);
     _operator = GetOperator(binary);
     _rightOperandTranslation = context.GetTranslationFor(binary.Right);
     EstimatedSize            = GetEstimatedSize();
 }
 private BinaryTranslation(BinaryExpression binary, ITranslationContext context)
     : base(IsCheckedBinary(binary.NodeType), "(", ")")
 {
     _context = context;
     NodeType = binary.NodeType;
     Type     = binary.Type;
     _leftOperandTranslation = context.GetTranslationFor(binary.Left);
     _operator = GetOperator(binary);
     _rightOperandTranslation = context.GetTranslationFor(binary.Right);
     TranslationSize          = GetTranslationSize();
     FormattingSize           = _leftOperandTranslation.FormattingSize + _rightOperandTranslation.FormattingSize;
 }
Example #7
0
        private static ITranslation GetSubjectOrNull(MemberExpression memberAccess, ITranslationContext context)
        {
            if (memberAccess.Expression == null)
            {
                return(context.GetTranslationFor(memberAccess.Member.DeclaringType));
            }

            if (SubjectIsCapturedInstance(memberAccess))
            {
                return(null);
            }

            return(context.GetTranslationFor(memberAccess.Expression));
        }
Example #8
0
        public static ITranslation For(UnaryExpression cast, ITranslationContext context)
        {
            var castValueTranslation = context.GetTranslationFor(cast.Operand);

            switch (cast.NodeType)
            {
            case ExpressionType.Convert:
            case ConvertChecked:
                if (cast.Type == typeof(object))
                {
                    // Don't bother to show a boxing cast:
                    return(castValueTranslation);
                }

                if (cast.Method != null)
                {
                    var isImplicitOperator = cast.Method.IsImplicitOperator();

                    if (isImplicitOperator)
                    {
                        return(castValueTranslation.ShouldWriteInParentheses()
                                ? castValueTranslation.WithParentheses()
                                : castValueTranslation);
                    }

                    if (cast.Method.IsExplicitOperator())
                    {
                        break;
                    }

                    return(MethodCallTranslation.ForCustomMethodCast(
                               context.GetTranslationFor(cast.Type),
                               new BclMethodWrapper(cast.Method),
                               castValueTranslation,
                               context));
                }

                if (IsDelegateCast(cast, out var createDelegateCall))
                {
                    return(MethodGroupTranslation.ForCreateDelegateCall(cast.NodeType, createDelegateCall, context));
                }

                break;

            case TypeAs:
                return(new TypeTestedTranslation(TypeAs, castValueTranslation, " as ", cast.Type, context));
            }

            return(new StandardCastTranslation(cast, castValueTranslation, context));
        }
        public SwitchTranslation(SwitchExpression switchStatement, ITranslationContext context)
        {
            Type = switchStatement.Type;
            _valueTranslation = context.GetTranslationFor(switchStatement.SwitchValue);

            var estimatedSize = _valueTranslation.EstimatedSize;
            var caseCount     = switchStatement.Cases.Count;

            _caseTestValueTranslations = new ITranslation[caseCount][];
            _caseTranslations          = new ITranslation[caseCount];

            for (var i = 0; ;)
            {
                var @case          = switchStatement.Cases[i];
                var testValueCount = @case.TestValues.Count;

                var caseTestValueTranslations = new ITranslation[testValueCount];

                for (var j = 0; ;)
                {
                    var caseTestValueTranslation = context.GetTranslationFor(@case.TestValues[j]);
                    caseTestValueTranslations[j] = caseTestValueTranslation;
                    estimatedSize += caseTestValueTranslation.EstimatedSize;

                    if (++j == testValueCount)
                    {
                        break;
                    }
                }

                _caseTestValueTranslations[i] = caseTestValueTranslations;
                _caseTranslations[i]          = GetCaseBodyTranslationOrNull(@case.Body, context);

                if (++i == caseCount)
                {
                    break;
                }
            }

            _defaultCaseTranslation = GetCaseBodyTranslationOrNull(switchStatement.DefaultBody, context);

            if (_defaultCaseTranslation != null)
            {
                estimatedSize += _defaultCaseTranslation.EstimatedSize;
            }

            EstimatedSize = estimatedSize;
        }
        private NewingTranslation(
            NewExpression newing,
            ITranslationContext context,
            bool omitParenthesesIfParameterless)
            : base(newing, context)
        {
            _typeNameTranslation = context.GetTranslationFor(newing.Type).WithObjectTypeName();

            if (omitParenthesesIfParameterless && Parameters.None)
            {
                Parameters.WithoutParentheses();
            }
            else
            {
                Parameters.WithParentheses();
            }

            TranslationSize =
                "new ".Length +
                _typeNameTranslation.TranslationSize +
                Parameters.TranslationSize;

            FormattingSize =
                context.GetKeywordFormattingSize() +
                _typeNameTranslation.FormattingSize +
                Parameters.FormattingSize;
        }
        public static ITranslation For(ConstantExpression constant, ITranslationContext context)
        {
            if (constant.Value == null)
            {
                return(FixedValueTranslation("null", constant.Type));
            }

            if (constant.Type.IsEnum())
            {
                return(new EnumConstantTranslation(constant, context));
            }

            if (TryTranslateFromTypeCode(constant, context, out var translation))
            {
                return(translation);
            }

            var valueType = constant.Value.GetType();

            if (valueType.IsPrimitive() || valueType.IsValueType())
            {
                return(FixedValueTranslation(constant.Value, valueType));
            }

            return(context.GetTranslationFor(valueType).WithNodeType(Constant));
        }
 public NamedVariableExceptionClause(CatchBlock catchBlock, ITranslationContext context)
 {
     _exceptionTypeTranslation = context.GetTranslationFor(catchBlock.Test);
     _variableName             = catchBlock.Variable.Name;
     TranslationSize           = _exceptionTypeTranslation.TranslationSize + _variableName.Length + 5;
     FormattingSize            = _exceptionTypeTranslation.FormattingSize;
 }
 public NegationTranslation(UnaryExpression negation, ITranslationContext context)
     : this(
         negation.NodeType,
         (negation.NodeType == ExpressionType.Not) ? _bang : '-',
         context.GetTranslationFor(negation.Operand))
 {
 }
Example #14
0
 public NewEmptyBoundedArrayTranslation(Expression arrayInit, ITranslationContext context)
     : base(arrayInit)
 {
     _emptyArrayNewing = context.GetTranslationFor(arrayInit.Type.GetElementType());
     TranslationSize   = "new ".Length + _emptyArrayNewing.TranslationSize + "[0]".Length;
     FormattingSize    = context.GetKeywordFormattingSize() + _emptyArrayNewing.FormattingSize;
 }
Example #15
0
            public void WithTypeNames(ITranslationContext context)
            {
                _typeNameTranslation = context.GetTranslationFor(Type);

                TranslationSize += _typeNameTranslation.TranslationSize;
                FormattingSize  += _typeNameTranslation.FormattingSize;
            }
        private static IDictionary <ITranslation, ParameterSetTranslation> GetVariableDeclarations(
            BlockExpression block,
            ITranslationContext context)
        {
            if (block.Variables.Count == 0)
            {
                return(EmptyDictionary <ITranslation, ParameterSetTranslation> .Instance);
            }

            var variablesByType = block
                                  .Variables
                                  .Except(context.InlineOutputVariables)
                                  .Except(context.JoinedAssignmentVariables)
                                  .GroupBy(v => v.Type)
                                  .ToArray();

            if (variablesByType.Length == 0)
            {
                return(EmptyDictionary <ITranslation, ParameterSetTranslation> .Instance);
            }

            return(variablesByType.ToDictionary(
                       grp => (ITranslation)context.GetTranslationFor(grp.Key),
                       grp => ParameterSetTranslation.For(grp.ToList(), context)
                       .WithoutParentheses()
                       .WithoutTypeNames(context)));
        }
Example #17
0
 public StandardCastTranslation(Expression cast, ITranslation castValueTranslation, ITranslationContext context)
     : this(
         cast.NodeType,
         context.GetTranslationFor(cast.Type),
         castValueTranslation)
 {
 }
Example #18
0
        public DefaultValueTranslation(
            Expression defaultExpression,
            ITranslationContext context,
            bool allowNullKeyword = true)
        {
            Type = defaultExpression.Type;

            if (Type == typeof(void))
            {
                IsEmpty = true;
                return;
            }

            if (allowNullKeyword)
            {
                _typeCanBeNull = Type.CanBeNull();

                if (_typeCanBeNull)
                {
                    TranslationSize = _null.Length;
                    return;
                }
            }

            _typeNameTranslation = context.GetTranslationFor(Type);
            TranslationSize      = _default.Length + _typeNameTranslation.TranslationSize + "()".Length;
            FormattingSize       = context.GetKeywordFormattingSize() + _typeNameTranslation.FormattingSize;
        }
 public FilteredExceptionClause(CatchBlock catchBlock, ITranslationContext context)
     : base(catchBlock, context)
 {
     _filterTranslation = context.GetTranslationFor(catchBlock.Filter);
     TranslationSize    = base.TranslationSize + _filterTranslation.TranslationSize + 6;
     FormattingSize     = base.FormattingSize + _filterTranslation.FormattingSize;
 }
        private QuotedLambdaTranslation(UnaryExpression quotedLambda, ITranslationContext context)
        {
            var comment           = ReadableExpression.Comment("Quoted to induce a closure:");
            var quotedLambdaBlock = Expression.Block(comment, quotedLambda.Operand);

            _quotedLambdaTranslation = context.GetTranslationFor(quotedLambdaBlock);
        }
            public IfElseTranslation(ConditionalExpression conditional, ITranslationContext context)
                : base(
                    conditional,
                    GetCodeBlockTranslation(
                        context.GetTranslationFor(conditional.IfTrue),
                        withReturnKeyword: false),
                    context.GetTranslationFor(conditional.IfFalse),
                    context)
            {
                _isElseIf = IsElseIf(conditional);

                if (_isElseIf == false)
                {
                    IfFalseTranslation = GetCodeBlockTranslation(IfFalseTranslation, conditional.IfFalse.IsReturnable());
                }
            }
        public static ITranslation For(ConstantExpression constant, ITranslationContext context)
        {
            if (context.Settings.ConstantExpressionValueFactory != null)
            {
                var userTranslation = context.Settings.ConstantExpressionValueFactory(constant.Type, constant.Value);

                return((userTranslation == null)
                    ? NullTranslation(constant.Type, context)
                    : FixedValueTranslation(userTranslation, constant.Type, context));
            }

            if (constant.Value == null)
            {
                return(NullTranslation(constant.Type, context));
            }

            if (constant.Type.IsEnum())
            {
                return(new EnumConstantTranslation(constant, context));
            }

            if (TryTranslateFromTypeCode(constant, context, out var translation))
            {
                return(translation);
            }

            var valueType = constant.Value.GetType();

            if (valueType.IsPrimitive() || valueType.IsValueType())
            {
                return(FixedValueTranslation(constant.Value, valueType, context));
            }

            return(context.GetTranslationFor(valueType).WithNodeType(Constant));
        }
        public static ITranslation ForCreateDelegateCall(
            ExpressionType nodeType,
            MethodCallExpression createDelegateCall,
            ITranslationContext context)
        {
#if NET35
            var subjectMethod = (MethodInfo)((ConstantExpression)createDelegateCall.Arguments.Last()).Value;
#else
            // ReSharper disable once PossibleNullReferenceException
            var subjectMethod = (MethodInfo)((ConstantExpression)createDelegateCall.Object).Value;
#endif
            var subjectTranslation = subjectMethod.IsStatic
                ? context.GetTranslationFor(subjectMethod.DeclaringType)
                : context.GetTranslationFor(createDelegateCall.Arguments.ElementAtOrDefault(1));

            return new MethodGroupTranslation(nodeType, subjectTranslation, subjectMethod, context);
        }
        protected static ITranslation GetSubjectOrNull(
            Expression subject,
            MemberInfo member,
            ITranslationContext context)
        {
            if (subject == null)
            {
                return(context.GetTranslationFor(member.DeclaringType));
            }

            if (SubjectIsCapturedInstance(subject, member))
            {
                return(null);
            }

            return(context.GetTranslationFor(subject));
        }
Example #25
0
 private IndexAccessTranslation(
     Expression subject,
     ICollection <Expression> arguments,
     ITranslationContext context)
 {
     _subject      = context.GetTranslationFor(subject);
     _parameters   = new ParameterSetTranslation(arguments, context);
     EstimatedSize = GetEstimatedSize();
 }
Example #26
0
 public BlockStatementTranslation(Expression expression, ITranslationContext context)
 {
     NodeType                 = expression.NodeType;
     Expression               = expression;
     _statementTranslation    = context.GetTranslationFor(expression);
     _statementIsUnterminated = StatementIsUnterminated(expression);
     _writeBlankLineBefore    = WriteBlankLineBefore();
     EstimatedSize            = _statementTranslation.EstimatedSize + 1;
 }
 public IfStatementTranslation(ConditionalExpression conditional, ITranslationContext context)
     : base(
         conditional,
         GetCodeBlockTranslation(
             context.GetTranslationFor(conditional.IfTrue),
             withReturnKeyword: conditional.IfTrue.IsReturnable()),
         context)
 {
 }
Example #28
0
 public static ITranslation For(TypeBinaryExpression typeIs, ITranslationContext context)
 {
     return(new TypeTestedTranslation(
                TypeIs,
                context.GetTranslationFor(typeIs.Expression),
                " is ",
                typeIs.TypeOperand,
                context));
 }
 private IndexAccessTranslation(
     Expression subject,
     ICollection <Expression> arguments,
     ITranslationContext context)
     : this(
         context.GetTranslationFor(subject),
         ParameterSetTranslation.For(arguments, context))
 {
 }
 public ShortCircuitingIfTranslation(ConditionalExpression conditional, ITranslationContext context)
     : base(
         conditional,
         GetCodeBlockTranslation(
             context.GetTranslationFor(conditional.IfTrue),
             withReturnKeyword: true),
         context.GetCodeBlockTranslationFor(conditional.IfFalse).WithoutBraces(),
         context)
 {
 }