Search in sources :

Example 41 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy by apache.

the class PreconditionGenerator method generateDefaultPreconditionStatement.

/**
 * Generates the default precondition statement for {@link org.codehaus.groovy.ast.MethodNode} instances with
 * the {@link org.apache.groovy.contracts.annotations.meta.Precondition} annotation.
 *
 * @param type       the current {@link org.codehaus.groovy.ast.ClassNode}
 * @param methodNode the {@link org.codehaus.groovy.ast.MethodNode} with a {@link org.apache.groovy.contracts.annotations.meta.Precondition} annotation
 */
public void generateDefaultPreconditionStatement(final ClassNode type, final MethodNode methodNode) {
    // if another precondition is available we'll evaluate to false
    boolean isAnotherPreconditionAvailable = AnnotationUtils.getAnnotationNodeInHierarchyWithMetaAnnotation(type.getSuperClass(), methodNode, ClassHelper.makeWithoutCaching(Precondition.class)).size() > 0;
    if (!isAnotherPreconditionAvailable)
        return;
    // if there is another preconditio up the inheritance path, we need a default precondition with FALSE
    // e.g. C1 <no precondition> : C2 <item != null> == false || item != null
    BooleanExpression preconditionBooleanExpression = boolX(ConstantExpression.FALSE);
    preconditionBooleanExpression = addCallsToSuperMethodNodeAnnotationClosure(type, methodNode, Precondition.class, preconditionBooleanExpression, false);
    // if precondition could not be found in parent class, let's return
    if (preconditionBooleanExpression.getExpression() == ConstantExpression.FALSE)
        return;
    final BlockStatement blockStatement = wrapAssertionBooleanExpression(type, methodNode, preconditionBooleanExpression, "precondition");
    addPrecondition(methodNode, blockStatement);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) Precondition(org.apache.groovy.contracts.annotations.meta.Precondition) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 42 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy by apache.

the class Assertion method and.

public void and(T other) {
    Validate.notNull(other);
    BooleanExpression newBooleanExpression = new BooleanExpression(new BinaryExpression(booleanExpression(), Token.newSymbol(Types.LOGICAL_AND, -1, -1), other.booleanExpression()));
    newBooleanExpression.setSourcePosition(booleanExpression());
    renew(newBooleanExpression);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression)

Example 43 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project gradle by gradle.

the class ExpressionReplacingVisitorSupport method visitBooleanExpression.

@Override
public void visitBooleanExpression(BooleanExpression expr) {
    BooleanExpression result = new BooleanExpression(replaceExpr(expr.getExpression()));
    result.setType(expr.getType());
    result.setSourcePosition(expr);
    replaceVisitedExpressionWith(result);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression)

Example 44 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy-core by groovy.

the class BinaryExpressionTransformer method transformBinaryExpression.

Expression transformBinaryExpression(final BinaryExpression bin) {
    if (bin instanceof DeclarationExpression) {
        Expression optimized = transformDeclarationExpression(bin);
        if (optimized != null) {
            return optimized;
        }
    }
    Object[] list = bin.getNodeMetaData(BINARY_EXP_TARGET);
    Token operation = bin.getOperation();
    int operationType = operation.getType();
    Expression rightExpression = bin.getRightExpression();
    Expression leftExpression = bin.getLeftExpression();
    if (bin instanceof DeclarationExpression && leftExpression instanceof VariableExpression) {
        ClassNode declarationType = ((VariableExpression) leftExpression).getOriginType();
        if (rightExpression instanceof ConstantExpression) {
            ClassNode unwrapper = ClassHelper.getUnwrapper(declarationType);
            ClassNode wrapper = ClassHelper.getWrapper(declarationType);
            if (!rightExpression.getType().equals(declarationType) && wrapper.isDerivedFrom(ClassHelper.Number_TYPE) && WideningCategories.isDoubleCategory(unwrapper)) {
                ConstantExpression constant = (ConstantExpression) rightExpression;
                if (constant.getValue() != null) {
                    return optimizeConstantInitialization(bin, operation, constant, leftExpression, declarationType);
                }
            }
        }
    }
    if (operationType == Types.EQUAL && leftExpression instanceof PropertyExpression) {
        MethodNode directMCT = leftExpression.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
        if (directMCT != null) {
            return transformPropertyAssignmentToSetterCall((PropertyExpression) leftExpression, rightExpression, directMCT);
        }
    }
    if (operationType == Types.COMPARE_EQUAL || operationType == Types.COMPARE_NOT_EQUAL) {
        // let's check if one of the operands is the null constant
        CompareToNullExpression compareToNullExpression = null;
        if (isNullConstant(leftExpression)) {
            compareToNullExpression = new CompareToNullExpression(staticCompilationTransformer.transform(rightExpression), operationType == Types.COMPARE_EQUAL);
        } else if (isNullConstant(rightExpression)) {
            compareToNullExpression = new CompareToNullExpression(staticCompilationTransformer.transform(leftExpression), operationType == Types.COMPARE_EQUAL);
        }
        if (compareToNullExpression != null) {
            compareToNullExpression.setSourcePosition(bin);
            return compareToNullExpression;
        }
    } else if (operationType == Types.KEYWORD_IN) {
        return convertInOperatorToTernary(bin, rightExpression, leftExpression);
    }
    if (list != null) {
        if (operationType == Types.COMPARE_TO) {
            StaticTypesTypeChooser typeChooser = staticCompilationTransformer.getTypeChooser();
            ClassNode classNode = staticCompilationTransformer.getClassNode();
            ClassNode leftType = typeChooser.resolveType(leftExpression, classNode);
            if (leftType.implementsInterface(ClassHelper.COMPARABLE_TYPE)) {
                ClassNode rightType = typeChooser.resolveType(rightExpression, classNode);
                if (rightType.implementsInterface(ClassHelper.COMPARABLE_TYPE)) {
                    Expression left = staticCompilationTransformer.transform(leftExpression);
                    Expression right = staticCompilationTransformer.transform(rightExpression);
                    MethodCallExpression call = new MethodCallExpression(left, "compareTo", new ArgumentListExpression(right));
                    call.setImplicitThis(false);
                    call.setMethodTarget(COMPARE_TO_METHOD);
                    CompareIdentityExpression compareIdentity = new CompareIdentityExpression(left, right);
                    compareIdentity.putNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE, ClassHelper.boolean_TYPE);
                    TernaryExpression result = new TernaryExpression(// a==b
                    new BooleanExpression(compareIdentity), CONSTANT_ZERO, new TernaryExpression(// a==null
                    new BooleanExpression(new CompareToNullExpression(left, true)), CONSTANT_MINUS_ONE, new TernaryExpression(// b==null
                    new BooleanExpression(new CompareToNullExpression(right, true)), CONSTANT_ONE, call)));
                    compareIdentity.putNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE, ClassHelper.int_TYPE);
                    result.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
                    TernaryExpression expr = (TernaryExpression) result.getFalseExpression();
                    expr.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
                    expr.getFalseExpression().putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
                    return result;
                }
            }
        }
        boolean isAssignment = StaticTypeCheckingSupport.isAssignment(operationType);
        MethodCallExpression call;
        MethodNode node = (MethodNode) list[0];
        String name = (String) list[1];
        Expression left = staticCompilationTransformer.transform(leftExpression);
        Expression right = staticCompilationTransformer.transform(rightExpression);
        BinaryExpression optimized = tryOptimizeCharComparison(left, right, bin);
        if (optimized != null) {
            optimized.removeNodeMetaData(BINARY_EXP_TARGET);
            return transformBinaryExpression(optimized);
        }
        call = new MethodCallExpression(left, name, new ArgumentListExpression(right));
        call.setImplicitThis(false);
        call.setMethodTarget(node);
        MethodNode adapter = StaticCompilationTransformer.BYTECODE_BINARY_ADAPTERS.get(operationType);
        if (adapter != null) {
            ClassExpression sba = new ClassExpression(StaticCompilationTransformer.BYTECODE_ADAPTER_CLASS);
            // replace with compareEquals
            call = new MethodCallExpression(sba, "compareEquals", new ArgumentListExpression(left, right));
            call.setMethodTarget(adapter);
            call.setImplicitThis(false);
        }
        if (!isAssignment)
            return call;
        // the method represents the operation type only, and we must add an assignment
        return new BinaryExpression(left, Token.newSymbol("=", operation.getStartLine(), operation.getStartColumn()), call);
    }
    if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof TupleExpression && rightExpression instanceof ListExpression) {
        // multiple assignment
        ListOfExpressionsExpression cle = new ListOfExpressionsExpression();
        boolean isDeclaration = bin instanceof DeclarationExpression;
        List<Expression> leftExpressions = ((TupleExpression) leftExpression).getExpressions();
        List<Expression> rightExpressions = ((ListExpression) rightExpression).getExpressions();
        Iterator<Expression> leftIt = leftExpressions.iterator();
        Iterator<Expression> rightIt = rightExpressions.iterator();
        if (isDeclaration) {
            while (leftIt.hasNext()) {
                Expression left = leftIt.next();
                if (rightIt.hasNext()) {
                    Expression right = rightIt.next();
                    BinaryExpression bexp = new DeclarationExpression(left, bin.getOperation(), right);
                    bexp.setSourcePosition(right);
                    cle.addExpression(bexp);
                }
            }
        } else {
            // (next, result) = [ result, next+result ]
            // -->
            // def tmp1 = result
            // def tmp2 = next+result
            // next = tmp1
            // result = tmp2
            int size = rightExpressions.size();
            List<Expression> tmpAssignments = new ArrayList<Expression>(size);
            List<Expression> finalAssignments = new ArrayList<Expression>(size);
            for (int i = 0; i < Math.min(size, leftExpressions.size()); i++) {
                Expression left = leftIt.next();
                Expression right = rightIt.next();
                VariableExpression tmpVar = new VariableExpression("$tmpVar$" + tmpVarCounter++);
                BinaryExpression bexp = new DeclarationExpression(tmpVar, bin.getOperation(), right);
                bexp.setSourcePosition(right);
                tmpAssignments.add(bexp);
                bexp = new BinaryExpression(left, bin.getOperation(), new VariableExpression(tmpVar));
                bexp.setSourcePosition(left);
                finalAssignments.add(bexp);
            }
            for (Expression tmpAssignment : tmpAssignments) {
                cle.addExpression(tmpAssignment);
            }
            for (Expression finalAssignment : finalAssignments) {
                cle.addExpression(finalAssignment);
            }
        }
        return staticCompilationTransformer.transform(cle);
    }
    return staticCompilationTransformer.superTransform(bin);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayList(java.util.ArrayList) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Token(org.codehaus.groovy.syntax.Token) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ListOfExpressionsExpression(org.codehaus.groovy.transform.sc.ListOfExpressionsExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListOfExpressionsExpression(org.codehaus.groovy.transform.sc.ListOfExpressionsExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) StaticTypesTypeChooser(org.codehaus.groovy.classgen.asm.sc.StaticTypesTypeChooser)

Example 45 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy-core by groovy.

the class TraitComposer method createSuperFallback.

private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) {
    ArgumentListExpression args = new ArgumentListExpression();
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE));
    MethodCallExpression superCall = new MethodCallExpression(new VariableExpression("super"), forwarderMethod.getName(), args);
    superCall.setImplicitThis(false);
    CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this"));
    MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS);
    getProxy.setImplicitThis(true);
    StaticMethodCallExpression proxyCall = new StaticMethodCallExpression(ClassHelper.make(InvokerHelper.class), "invokeMethod", new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())));
    IfStatement stmt = new IfStatement(new BooleanExpression(instanceOfExpr), new ExpressionStatement(new CastExpression(returnType, proxyCall)), new ExpressionStatement(superCall));
    return stmt;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)48 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)24 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)24 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)23 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)23 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)18 Expression (org.codehaus.groovy.ast.expr.Expression)18 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)16 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)16 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)15 Statement (org.codehaus.groovy.ast.stmt.Statement)14 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)13 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)12 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)12 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)12 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)12 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)12 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)12