Beispiel #1
0
        private ExpressionSyntax GenIntegralProducingBinary(PrimitiveType type)
        {
            Debug.Assert(type.Info.IsIntegral);
            SyntaxKind op = (SyntaxKind)Options.BinaryIntegralDist.Sample(Random.Rng);

            BinOpTable table;

            if (op == SyntaxKind.LeftShiftExpression || op == SyntaxKind.RightShiftExpression)
            {
                table = BinOpTable.Shifts;
            }
            else
            {
                table = BinOpTable.Arithmetic;
            }

            PrimitiveType leftType  = Types.PickPrimitiveType(f => f.Keyword != SyntaxKind.BoolKeyword);
            PrimitiveType rightType =
                Types.PickPrimitiveType(f => table.GetResultType(leftType.Keyword, f.Keyword).HasValue);

            ExpressionSyntax left  = GenExpression(leftType);
            ExpressionSyntax right = GenExpression(rightType);

            while (left is LiteralExpressionSyntax && right is LiteralExpressionSyntax)
            {
                right = GenExpression(rightType);
            }

            if (op == SyntaxKind.ModuloExpression || op == SyntaxKind.DivideExpression)
            {
                right = CastExpression(
                    rightType.GenReferenceTo(),
                    ParenthesizedExpression(
                        BinaryExpression(
                            SyntaxKind.BitwiseOrExpression,
                            ParenthesizeIfNecessary(right),
                            LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)))));
            }

            ExpressionSyntax expr = BinaryExpression(op, ParenthesizeIfNecessary(left), ParenthesizeIfNecessary(right));

            if (table.GetResultType(leftType.Keyword, rightType.Keyword) != type.Keyword)
            {
                expr = CastExpression(type.GenReferenceTo(), ParenthesizedExpression(expr));
            }

            return(expr);
        }