Ejemplo n.º 1
0
        override public void LeaveBinaryExpression(BinaryExpression node)
        {
            if (AstUtil.GetBinaryOperatorKind(node.Operator) == BinaryOperatorKind.Assignment ||
                node.Operator == BinaryOperatorType.ReferenceEquality ||
                node.Operator == BinaryOperatorType.ReferenceInequality)
            {
                return;
            }

            if (null == node.Left.ExpressionType || null == node.Right.ExpressionType)
            {
                return;
            }

            object lhs = GetLiteralValue(node.Left);
            object rhs = GetLiteralValue(node.Right);

            if (null == lhs || null == rhs)
            {
                return;
            }

            Expression folded  = null;
            IType      lhsType = GetExpressionType(node.Left);
            IType      rhsType = GetExpressionType(node.Right);

            if (TypeSystemServices.BoolType == lhsType && TypeSystemServices.BoolType == rhsType)
            {
                folded = GetFoldedBoolLiteral(node.Operator, Convert.ToBoolean(lhs), Convert.ToBoolean(rhs));
            }
            else if (TypeSystemServices.IsFloatingPointNumber(lhsType) || TypeSystemServices.IsFloatingPointNumber(rhsType))
            {
                folded = GetFoldedDoubleLiteral(node.Operator, Convert.ToDouble(lhs), Convert.ToDouble(rhs));
            }
            else if (TypeSystemServices.IsIntegerNumber(lhsType) || lhsType.IsEnum)
            {
                bool lhsSigned = TypeSystemServices.IsSignedNumber(lhsType);
                bool rhsSigned = TypeSystemServices.IsSignedNumber(rhsType);
                if (lhsSigned == rhsSigned)                 //mixed signed/unsigned not supported for folding
                {
                    folded = lhsSigned
                                                 ? GetFoldedIntegerLiteral(node.Operator, Convert.ToInt64(lhs), Convert.ToInt64(rhs))
                                                 : GetFoldedIntegerLiteral(node.Operator, Convert.ToUInt64(lhs), Convert.ToUInt64(rhs));
                }
            }
            else if (node.Operator == BinaryOperatorType.TypeTest && lhsType.IsValueType)
            {
                folded = GetFoldedValueTypeTest(node, lhsType, (IType)node.Right.Entity);
            }

            if (null != folded)
            {
                folded.LexicalInfo    = node.LexicalInfo;
                folded.ExpressionType = GetExpressionType(node);
                folded.Annotate(FoldedExpression, node);
                ReplaceCurrentNode(folded);
            }
        }