Ejemplo n.º 1
0
        public AnonymousTypeNewingTranslation(NewExpression newing, ITranslationContext context)
            : base(newing, context)
        {
            Type      = newing.Type;
            _typeName = context.Settings.AnonymousTypeNameFactory?.Invoke(Type) ?? string.Empty;

            var ctorParameters     = newing.Constructor.GetParameters();
            var ctorParameterCount = ctorParameters.Length;

            var initializers    = new ITranslation[ctorParameterCount];
            var translationSize = _typeName.Length + "new ".Length;

            for (var i = 0; ;)
            {
                initializers[i] = new AnonymousTypeInitializerTranslation(
                    ctorParameters[i],
                    Parameters[i]);

                ++i;

                if (i == ctorParameterCount)
                {
                    break;
                }
            }

            _initializers = new AnonymousTypeInitializerTranslationSet(initializers, context);

            TranslationSize = translationSize + _initializers.TranslationSize;

            FormattingSize =
                context.GetKeywordFormattingSize() + // <- For 'new'
                _initializers.FormattingSize;
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
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;
 }
        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 LoopTranslation(LoopExpression loop, ITranslationContext context)
        {
            Type = loop.Type;
            _loopBodyTranslation = context.GetCodeBlockTranslationFor(loop.Body).WithTermination().WithBraces();
            TranslationSize      = _loopBodyTranslation.TranslationSize + 10;

            FormattingSize =
                context.GetControlStatementFormattingSize() + // <- for 'while'
                context.GetKeywordFormattingSize() +          // <- for 'true'
                _loopBodyTranslation.FormattingSize;
        }
            public BlockAssignmentStatementTranslation(BinaryExpression assignment, ITranslationContext context)
                : base(assignment, context)
            {
                if (UseFullTypeName(assignment, context))
                {
                    _typeNameTranslation = context.GetTranslationFor(assignment.Left.Type);
                    TranslationSize     += _typeNameTranslation.TranslationSize + 2;
                    FormattingSize      += _typeNameTranslation.FormattingSize;
                    return;
                }

                TranslationSize += _var.Length;
                FormattingSize  += context.GetKeywordFormattingSize();
            }
Ejemplo n.º 7
0
            public OutParameterTranslation(Expression parameter, ITranslationContext context)
            {
                _parameterTranslation = context.GetTranslationFor(parameter);
                TranslationSize       = _parameterTranslation.TranslationSize + _out.Length;
                FormattingSize        = _parameterTranslation.FormattingSize + context.GetKeywordFormattingSize();

                if ((parameter.NodeType == Parameter) &&
                    context.Settings.DeclareOutParamsInline &&
                    context.ShouldBeDeclaredInline((ParameterExpression)parameter))
                {
                    _declareParameterInline = true;

                    if (context.Settings.UseImplicitTypeNames)
                    {
                        TranslationSize += _var.Length;
                        FormattingSize  += context.GetKeywordFormattingSize();
                        return;
                    }

                    _typeNameTranslation = context.GetTranslationFor(parameter.Type);
                    TranslationSize     += _typeNameTranslation.TranslationSize + 1;
                    FormattingSize      += _typeNameTranslation.FormattingSize;
                }
            }
Ejemplo n.º 8
0
        public ThrowTranslation(UnaryExpression throwExpression, ITranslationContext context)
        {
            Type            = throwExpression.Type;
            TranslationSize = _throw.Length;
            FormattingSize  = context.GetKeywordFormattingSize();

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            // unary.Operand is null when using Expression.Rethrow():
            if ((throwExpression.Operand == null) || context.IsCatchBlockVariable(throwExpression.Operand))
            {
                return;
            }

            _thrownItemTranslation = context.GetTranslationFor(throwExpression.Operand);
            TranslationSize        = _thrownItemTranslation.TranslationSize;
            FormattingSize        += _thrownItemTranslation.FormattingSize;
        }
            public TypeOfTranslation(
                ITranslation operandTranslation,
                ITranslation typeNameTranslation,
                ITranslationContext context)
            {
                _operandTranslation  = operandTranslation;
                _typeNameTranslation = typeNameTranslation;

                TranslationSize =
                    operandTranslation.TranslationSize +
                    typeNameTranslation.TranslationSize +
                    _typeOf.Length + "()".Length;

                FormattingSize =
                    operandTranslation.FormattingSize +
                    typeNameTranslation.FormattingSize +
                    context.GetKeywordFormattingSize();
            }
        public TryCatchTranslation(TryExpression tryCatchFinally, ITranslationContext context)
        {
            Type = tryCatchFinally.Type;
            _isNonVoidTryCatch = tryCatchFinally.HasReturnType();

            _bodyTranslation = GetReturnableBlockTranslation(tryCatchFinally.Body, context);

            _catchBlockTranslations = GetCatchBlockTranslations(
                tryCatchFinally.Handlers,
                out _catchBlockCount,
                out var catchBlockTranslationsSize,
                out var catchBlocksFormattingSize,
                context);

            var translationSize       = _bodyTranslation.TranslationSize + catchBlockTranslationsSize;
            var keywordFormattingSize = context.GetKeywordFormattingSize();

            var formattingSize =
                keywordFormattingSize + // <- for the 'try'
                _bodyTranslation.FormattingSize +
                catchBlocksFormattingSize;

            _hasFault = tryCatchFinally.Fault != null;

            if (_hasFault)
            {
                _faultTranslation = GetReturnableBlockTranslation(tryCatchFinally.Fault, context);
                translationSize  += _faultTranslation.TranslationSize;
                formattingSize   += keywordFormattingSize;
            }

            _hasFinally = tryCatchFinally.Finally != null;

            if (_hasFinally)
            {
                _finallyTranslation = GetReturnableBlockTranslation(tryCatchFinally.Finally, context);
                translationSize    += _finallyTranslation.TranslationSize;
                formattingSize     += keywordFormattingSize;
            }

            TranslationSize = translationSize;
            FormattingSize  = formattingSize;
        }
            public CatchBlockTranslation(CatchBlock catchBlock, ITranslationContext context)
            {
                _catchBodyTranslation = GetBlockTranslation(catchBlock.Body, context);
                _exceptionClause      = GetExceptionClauseOrNullFor(catchBlock, context);

                if ((_catchBodyTranslation.NodeType != ExpressionType.Throw) && catchBlock.Body.IsReturnable())
                {
                    _catchBodyTranslation.WithReturnKeyword();
                }

                var keywordFormattingSize = context.GetKeywordFormattingSize();

                TranslationSize = _catchBodyTranslation.TranslationSize;
                FormattingSize  = keywordFormattingSize + _catchBodyTranslation.FormattingSize;

                if (_exceptionClause != null)
                {
                    TranslationSize += _exceptionClause.TranslationSize;
                    FormattingSize  += keywordFormattingSize + _exceptionClause.FormattingSize;
                }
            }
            public TypeTestedTranslation(
                ExpressionType nodeType,
                ITranslation testedValueTranslation,
                string test,
                Type testedType,
                ITranslationContext context)
            {
                NodeType = nodeType;
                _testedValueTranslation = testedValueTranslation;
                _test = test;
                _testedTypeNameTranslation = context.GetTranslationFor(testedType);

                TranslationSize =
                    _testedValueTranslation.TranslationSize +
                    _test.Length +
                    _testedTypeNameTranslation.TranslationSize;

                FormattingSize =
                    _testedValueTranslation.FormattingSize +
                    context.GetKeywordFormattingSize() +
                    _testedTypeNameTranslation.FormattingSize;
            }
Ejemplo n.º 13
0
        public SwitchTranslation(SwitchExpression switchStatement, ITranslationContext context)
        {
            _settings         = context.Settings;
            Type              = switchStatement.Type;
            _valueTranslation = context.GetTranslationFor(switchStatement.SwitchValue);

            var keywordFormattingSize = context.GetKeywordFormattingSize();
            var translationSize       = _switch.Length + _valueTranslation.TranslationSize + 4;

            var formattingSize =
                keywordFormattingSize +  // <- for 'switch'
                _valueTranslation.FormattingSize;

            _casesCount = switchStatement.Cases.Count;

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

            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;

                    translationSize += _case.Length + caseTestValueTranslation.TranslationSize + 3;

                    formattingSize +=
                        keywordFormattingSize + // <- for 'case'
                        caseTestValueTranslation.FormattingSize;

                    ++j;

                    if (j == testValueCount)
                    {
                        break;
                    }

                    translationSize += 3;
                }

                _caseTestValueTranslations[i] = caseTestValueTranslations;

                var caseTranslation = GetCaseBodyTranslationOrNull(@case.Body, context);
                _caseTranslations[i] = caseTranslation;
                translationSize     += caseTranslation.TranslationSize;
                formattingSize      += caseTranslation.FormattingSize;

                if (WriteBreak(caseTranslation))
                {
                    translationSize += "break;".Length;
                    formattingSize  += keywordFormattingSize;
                }

                ++i;

                if (i == _casesCount)
                {
                    break;
                }
            }

            _defaultCaseTranslation = GetCaseBodyTranslationOrNull(switchStatement.DefaultBody, context);

            if (_defaultCaseTranslation != null)
            {
                translationSize += _defaultCaseTranslation.TranslationSize;
                formattingSize  += _defaultCaseTranslation.FormattingSize;

                if (WriteBreak(_defaultCaseTranslation))
                {
                    translationSize += "break;".Length;
                    formattingSize  += keywordFormattingSize;
                }
            }

            TranslationSize = translationSize;
            FormattingSize  = formattingSize;
        }
Ejemplo n.º 14
0
 public RefParameterTranslation(Expression parameter, ITranslationContext context)
 {
     _parameterTranslation = context.GetTranslationFor(parameter);
     TranslationSize       = _parameterTranslation.TranslationSize + _ref.Length;
     FormattingSize        = _parameterTranslation.FormattingSize + context.GetKeywordFormattingSize();
 }
 public TypeofOperatorTranslation(Type type, ITranslationContext context)
 {
     _typeNameTranslation = context.GetTranslationFor(type);
     TranslationSize      = _typeNameTranslation.TranslationSize + "typeof()".Length;
     FormattingSize       = _typeNameTranslation.FormattingSize + context.GetKeywordFormattingSize();
 }
Ejemplo n.º 16
0
 public NewImplicitlyTypedArrayTranslation(Expression arrayInit, ITranslationContext context)
     : base(arrayInit)
 {
     TranslationSize = "new[]".Length;
     FormattingSize  = context.GetKeywordFormattingSize();
 }