Java Code Examples for org.codehaus.groovy.syntax.Types#DIVIDE

The following examples show how to use org.codehaus.groovy.syntax.Types#DIVIDE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Optimizes "Z = X/Y" with Z being int or long style.
 *
 * @returns null if the optimization cannot be applied, otherwise it will return the new target type
 */
private ClassNode optimizeDivWithIntOrLongTarget(final Expression rhs, final ClassNode assignmentTartgetType) {
    if (!(rhs instanceof BinaryExpression)) return null;
    BinaryExpression binExp = (BinaryExpression) rhs;
    int op = binExp.getOperation().getType();
    if (op != Types.DIVIDE && op != Types.DIVIDE_EQUAL) return null;

    ClassNode originalResultType = typeChooser.resolveType(binExp, node);
    if (!originalResultType.equals(BigDecimal_TYPE)
            || !(isLongCategory(assignmentTartgetType) || isFloatingCategory(assignmentTartgetType))) {
        return null;
    }

    ClassNode leftType = typeChooser.resolveType(binExp.getLeftExpression(), node);
    if (!isLongCategory(leftType)) return null;
    ClassNode rightType = typeChooser.resolveType(binExp.getRightExpression(), node);
    if (!isLongCategory(rightType)) return null;

    ClassNode target;
    if (isIntCategory(leftType) && isIntCategory(rightType)) {
        target = int_TYPE;
    } else if (isLongCategory(leftType) && isLongCategory(rightType)) {
        target = long_TYPE;
    } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
        target = double_TYPE;
    } else {
        return null;
    }
    addMeta(rhs).type = target;
    opt.chainInvolvedType(target);
    return target;
}
 
Example 2
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    if (expression.getNodeMetaData(StatementMeta.class) != null) return;
    super.visitBinaryExpression(expression);

    ClassNode leftType = typeChooser.resolveType(expression.getLeftExpression(), node);
    ClassNode rightType = typeChooser.resolveType(expression.getRightExpression(), node);
    ClassNode resultType = null;
    int operation = expression.getOperation().getType();

    if (operation == Types.LEFT_SQUARE_BRACKET && leftType.isArray()) {
        opt.chainShouldOptimize(true);
        resultType = leftType.getComponentType();
    } else {
        switch (operation) {
            case Types.COMPARE_EQUAL:
            case Types.COMPARE_LESS_THAN:
            case Types.COMPARE_LESS_THAN_EQUAL:
            case Types.COMPARE_GREATER_THAN:
            case Types.COMPARE_GREATER_THAN_EQUAL:
            case Types.COMPARE_NOT_EQUAL:
                if (isIntCategory(leftType) && isIntCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else {
                    opt.chainCanOptimize(true);
                }
                resultType = boolean_TYPE;
                break;
            case Types.LOGICAL_AND:
            case Types.LOGICAL_AND_EQUAL:
            case Types.LOGICAL_OR:
            case Types.LOGICAL_OR_EQUAL:
                if (boolean_TYPE.equals(leftType) && boolean_TYPE.equals(rightType)) {
                    opt.chainShouldOptimize(true);
                } else {
                    opt.chainCanOptimize(true);
                }
                expression.setType(boolean_TYPE);
                resultType = boolean_TYPE;
                break;
            case Types.DIVIDE:
            case Types.DIVIDE_EQUAL:
                if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    resultType = BigDecimal_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isBigDecCategory(leftType) && isBigDecCategory(rightType)) {
                    // no optimization for BigDecimal yet
                    //resultType = BigDecimal_TYPE;
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    resultType = double_TYPE;
                    opt.chainShouldOptimize(true);
                }
                break;
            case Types.POWER:
            case Types.POWER_EQUAL:
                // TODO: implement
                break;
            case Types.ASSIGN:
                resultType = optimizeDivWithIntOrLongTarget(expression.getRightExpression(), leftType);
                opt.chainCanOptimize(true);
                break;
            default:
                if (isIntCategory(leftType) && isIntCategory(rightType)) {
                    resultType = int_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    resultType = long_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isBigDecCategory(leftType) && isBigDecCategory(rightType)) {
                    // no optimization for BigDecimal yet
                    //resultType = BigDecimal_TYPE;
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    resultType = double_TYPE;
                    opt.chainShouldOptimize(true);
                }
        }
    }

    if (resultType != null) {
        addMeta(expression).type = resultType;
        opt.chainInvolvedType(resultType);
        opt.chainInvolvedType(rightType);
        opt.chainInvolvedType(leftType);
    }
}