Esempio n. 1
0
        public static void TryToCastConstantExpression(ConstantExpression constant, InnerType toType)
        {
            SyntaxTreeNode parent = constant.Parent;

            if (CanCast(constant.Type, toType, false))
            {
                CastCase   castCase   = DefineCastCase(constant.Type, toType);
                Expression castMethod = CreateCastMethod(parent, toType, new ParameterDeclaration[] { new ParameterDeclaration(constant) }, castCase);
                Replace(parent, constant, castMethod);
            }
        }
Esempio n. 2
0
        public static void TryToCastIdentifierExpression(IdentifierExpression identifier, InnerType toType)
        {
            SyntaxTreeNode parent = identifier.Parent;

            if (CanCast(identifier.Type, toType, false))
            {
                CastCase   castCase   = DefineCastCase(identifier.Type, toType);
                Expression castMethod = CreateCastMethod(parent, toType, new ParameterDeclaration[] { new ParameterDeclaration(identifier) }, castCase);
                Replace(parent, identifier, castMethod);
            }
        }
Esempio n. 3
0
        public static void TryToCastMethodInvokation(MethodInvokationExpression method, InnerType toType)
        {
            SyntaxTreeNode parent = method.Parent;

            TryToCastMethodParameters(method);
            if (CanCast(method.ReturnType, toType, false))
            {
                CastCase   castCase   = DefineCastCase(method.ReturnType, toType);
                Expression castMethod = CreateCastMethod(parent, toType, new ParameterDeclaration[] { new ParameterDeclaration(method) }, castCase);
                Replace(parent, method, castMethod);
            }
        }
Esempio n. 4
0
        public static void TryToCastUnaryArithExpression(UnaryArithExpression unaryArith, InnerType toType)
        {
            SyntaxTreeNode parent      = unaryArith;
            InnerType      operandType = TypeResolver.ResolveExpressionType(unaryArith.Operand);

            if (CanCast(operandType, toType, false))
            {
                CastCase   castCase   = DefineCastCase(operandType, toType);
                Expression castMethod = CreateCastMethod(parent, toType, new ParameterDeclaration[] { new ParameterDeclaration(unaryArith.Operand) }, castCase);
                Replace(parent, unaryArith.Operand, castMethod);
            }
        }
Esempio n. 5
0
        public static void TryToCastBinaryArithExpression(BinaryArithExpression binaryArith, InnerType toType, bool inCastedBinary = false)
        {
            SyntaxTreeNode parent       = binaryArith.Parent;
            InnerType      LOperandType = TypeResolver.ResolveExpressionType(binaryArith.LeftOperand);
            InnerType      ROperandType = TypeResolver.ResolveExpressionType(binaryArith.RightOperand);

            switch (binaryArith.OperatorKind)
            {
            case BinaryExpression.BinaryOperator.LShift:
            case BinaryExpression.BinaryOperator.RShift:
            case BinaryExpression.BinaryOperator.BitwiseOr:
            case BinaryExpression.BinaryOperator.BitwiseAnd:
            case BinaryExpression.BinaryOperator.BitwiseXor:
                if (DefineCastCase(LOperandType, toType) != CastCase.Undefined)
                {
                    if (binaryArith.LeftOperand.NodeKind == NodeType.BinaryArithExpression)
                    {
                        TryToCastBinaryArithExpression((BinaryArithExpression)binaryArith.LeftOperand, new Int32(), true);
                    }
                    else
                    {
                        TryToCastExpression(binaryArith.LeftOperand, new Int32());
                    }
                }
                if (DefineCastCase(ROperandType, toType) != CastCase.Undefined)
                {
                    if (binaryArith.RightOperand.NodeKind == NodeType.BinaryArithExpression)
                    {
                        TryToCastBinaryArithExpression((BinaryArithExpression)binaryArith.RightOperand, new Int32(), true);
                    }
                    else
                    {
                        TryToCastExpression(binaryArith.RightOperand, new Int32());
                    }
                }
                if (!inCastedBinary)
                {
                    if (CanCast(TypeResolver.ResolveBinaryArithExpressionType(binaryArith), toType, false))
                    {
                        CastCase   castCase   = DefineCastCase(TypeResolver.ResolveBinaryArithExpressionType(binaryArith), toType);
                        Expression castMethod = CreateCastMethod(parent, toType, new ParameterDeclaration[] { new ParameterDeclaration(binaryArith) }, castCase);
                        Replace(parent, binaryArith, castMethod);
                    }
                }
                break;

            default:
                TryToCastExpression(binaryArith.LeftOperand, toType);
                TryToCastExpression(binaryArith.RightOperand, toType);
                break;
            }
        }
Esempio n. 6
0
        public static string GetCastMethodName(CastCase castCase)
        {
            switch (castCase)
            {
            case CastCase.LongToFloat:
            case CastCase.IntegerToFloat:
                return("ToSingle");

            case CastCase.CharToLong:
            case CastCase.IntegerToLong:
                return("ToInt64");

            case CastCase.CharToInteger:
                return("ToInt32");

            default:
                throw new System.Exception();
            }
        }
Esempio n. 7
0
        private static Expression CreateCastMethod(SyntaxTreeNode parent, InnerType returnType, ParameterDeclaration[] parameters, CastCase castCase)
        {
            Expression castMethod;

            if (castCase != CastCase.NoNeedToCast && castCase != CastCase.Undefined)
            {
                castMethod = new MethodInvokationExpression(GetCastMethodName(castCase), parameters, parameters[0].SourceContext)
                {
                    ReturnType = returnType
                }
            }
            ;
            else
            {
                castMethod = parameters[0].ParameterInstance;
            }
            castMethod.Parent = parent;
            return(castMethod);
        }