Search in sources :

Example 21 with Expression

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

the class StatementWriter method writeForLoopWithClosureList.

protected void writeForLoopWithClosureList(ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    ClosureListExpression clExpr = (ClosureListExpression) loop.getCollectionExpression();
    controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());
    List expressions = clExpr.getExpressions();
    int size = expressions.size();
    // middle element is condition, lower half is init, higher half is increment
    int condIndex = (size - 1) / 2;
    // visit init
    for (int i = 0; i < condIndex; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();
    Label cond = new Label();
    mv.visitLabel(cond);
    // visit condition leave boolean on stack
    {
        Expression condExpr = (Expression) expressions.get(condIndex);
        int mark = controller.getOperandStack().getStackLength();
        condExpr.visit(controller.getAcg());
        controller.getOperandStack().castToBool(mark, true);
    }
    // jump if we don't want to continue
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    controller.getOperandStack().jump(IFEQ, breakLabel);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    // visit increment
    mv.visitLabel(continueLabel);
    for (int i = condIndex + 1; i < size; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    // jump to test the condition again
    mv.visitJumpInsn(GOTO, cond);
    // loop end
    mv.visitLabel(breakLabel);
    controller.getCompileStack().pop();
    controller.getCompileStack().pop();
}
Also used : ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Label(org.objectweb.asm.Label) List(java.util.List) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 22 with Expression

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

the class StatementWriter method writeReturn.

public void writeReturn(ReturnStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitReturnStatement");
    writeStatementLabel(statement);
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    ClassNode returnType = controller.getReturnType();
    if (returnType == ClassHelper.VOID_TYPE) {
        if (!(statement.isReturningNullOrVoid())) {
            //TODO: move to Verifier
            controller.getAcg().throwException("Cannot use return statement with an expression on a method that returns void");
        }
        controller.getCompileStack().applyBlockRecorder();
        mv.visitInsn(RETURN);
        return;
    }
    Expression expression = statement.getExpression();
    expression.visit(controller.getAcg());
    operandStack.doGroovyCast(returnType);
    if (controller.getCompileStack().hasBlockRecorder()) {
        ClassNode type = operandStack.getTopOperand();
        int returnValueIdx = controller.getCompileStack().defineTemporaryVariable("returnValue", returnType, true);
        controller.getCompileStack().applyBlockRecorder();
        operandStack.load(type, returnValueIdx);
    }
    BytecodeHelper.doReturn(mv, returnType);
    operandStack.remove(1);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 23 with Expression

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

the class StatementWriter method writeWhileLoop.

public void writeWhileLoop(WhileStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitWhileLoop");
    writeStatementLabel(loop);
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(loop.getStatementLabels());
    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();
    mv.visitLabel(continueLabel);
    Expression bool = loop.getBooleanExpression();
    boolean boolHandled = false;
    if (bool instanceof ConstantExpression) {
        ConstantExpression constant = (ConstantExpression) bool;
        if (constant.getValue() == Boolean.TRUE) {
            boolHandled = true;
        // do nothing
        } else if (constant.getValue() == Boolean.FALSE) {
            boolHandled = true;
            mv.visitJumpInsn(GOTO, breakLabel);
        }
    }
    if (!boolHandled) {
        bool.visit(controller.getAcg());
        controller.getOperandStack().jump(IFEQ, breakLabel);
    }
    loop.getLoopBlock().visit(controller.getAcg());
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
    controller.getCompileStack().pop();
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 24 with Expression

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

the class StaticTypesStatementWriter method writeForInLoop.

@Override
protected void writeForInLoop(final ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    // Identify type of collection
    TypeChooser typeChooser = controller.getTypeChooser();
    Expression collectionExpression = loop.getCollectionExpression();
    ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
    Parameter loopVariable = loop.getVariable();
    int size = operandStack.getStackLength();
    if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
        writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
        writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else {
        writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    }
    operandStack.popDownTo(size);
    compileStack.pop();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 25 with Expression

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

the class AnnotationConstantsVisitor method transformConstantExpression.

private Expression transformConstantExpression(Expression val, ClassNode returnType) {
    ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
    if (val instanceof ConstantExpression) {
        Expression result = revertType(val, returnWrapperType);
        if (result != null) {
            return result;
        }
        return val;
    }
    if (val instanceof CastExpression) {
        CastExpression castExp = (CastExpression) val;
        Expression castee = castExp.getExpression();
        if (castee instanceof ConstantExpression) {
            if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
                return castee;
            }
            Expression result = revertType(castee, returnWrapperType);
            if (result != null) {
                return result;
            }
            return castee;
        }
    }
    return val;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

Expression (org.codehaus.groovy.ast.expr.Expression)369 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)257 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)244 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)187 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)187 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)152 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)151 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)149 ClassNode (org.codehaus.groovy.ast.ClassNode)146 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)134 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)130 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)113 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)103 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)95 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)84 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)78 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)63 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)63 Statement (org.codehaus.groovy.ast.stmt.Statement)63 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)61