Пример #1
0
        public override void OnBinaryExpression(BinaryExpression node)
        {
            if (node.Operator == BinaryOperatorType.Assign && node.Left.ExpressionType != node.Right.ExpressionType && node.Right.ExpressionType != null)
            {
                if (NeedsCastWithPotentialDataLoss(node.Left.ExpressionType.Type, node.Right.ExpressionType.Type))
                {
                    node.Replace(node.Right, CodeBuilder.CreateCast(node.Left.ExpressionType.Type, node.Right));
                }
            }
            else if (AstUtil.GetBinaryOperatorKind(node) == BinaryOperatorKind.Arithmetic && node.ExpressionType.ElementType == TypeSystemServices.ObjectType)
            {
                if (TypeSystemServices.IsNumber(node.Left.ExpressionType))
                {
                    node.ExpressionType = node.Left.ExpressionType;
                    node.Replace(node.Right, CodeBuilder.CreateCast(node.ExpressionType, node.Right));
                }
                else
                {
                    node.ExpressionType = node.Right.ExpressionType;
                    node.Replace(node.Left, CodeBuilder.CreateCast(node.ExpressionType, node.Left));
                }
            }

            base.OnBinaryExpression(node);
        }
Пример #2
0
        private bool FixMixedBooleanExpressionOperand(BinaryExpression node, Expression booleanSide, Expression nonBooleanSide)
        {
            if (booleanSide.ExpressionType == TypeSystemServices.BoolType && TypeSystemServices.IsNumber(nonBooleanSide.ExpressionType))
            {
                var        literalExpression = nonBooleanSide as LiteralExpression;
                Expression replacementNode   = literalExpression != null
                                                ? (Expression)CodeBuilder.CreateBoolLiteral(Convert.ToInt32(literalExpression.ValueObject) != 0)
                                                : CodeBuilder.CreateBoundBinaryExpression(booleanSide.ExpressionType, BinaryOperatorType.Inequality, nonBooleanSide, CodeBuilder.CreateIntegerLiteral(0));

                node.Replace(nonBooleanSide, replacementNode);
                return(true);
            }

            return(false);
        }
Пример #3
0
        private bool NeedsCastWithPotentialDataLoss(IType targetType, IType sourceType)
        {
            if (targetType == sourceType)
            {
                return(false);
            }

            if (sourceType == TypeSystemServices.ObjectType)
            {
                return(true);
            }

            if (targetType != TypeSystemServices.ObjectType && !sourceType.IsNull() && !targetType.IsAssignableFrom(sourceType))
            {
                if (TypeSystemServices.IsNumber(targetType) && TypeSystemServices.IsNumber(sourceType))
                {
                    return(!IsWideningPromotion(targetType, sourceType));
                }

                return(TypeSystemServices.IsNumber(targetType) ^ TypeSystemServices.IsNumber(sourceType));
            }

            return(false);
        }