Search in sources :

Example 6 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy by apache.

the class AntlrParserPlugin method statement.

// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
    Statement statement = null;
    int type = node.getType();
    switch(type) {
        case SLIST:
        case LITERAL_finally:
            statement = statementList(node);
            break;
        case METHOD_CALL:
            statement = methodCall(node);
            break;
        case VARIABLE_DEF:
            statement = variableDef(node);
            break;
        case LABELED_STAT:
            return labelledStatement(node);
        case LITERAL_assert:
            statement = assertStatement(node);
            break;
        case LITERAL_break:
            statement = breakStatement(node);
            break;
        case LITERAL_continue:
            statement = continueStatement(node);
            break;
        case LITERAL_if:
            statement = ifStatement(node);
            break;
        case LITERAL_for:
            statement = forStatement(node);
            break;
        case LITERAL_return:
            statement = returnStatement(node);
            break;
        case LITERAL_synchronized:
            statement = synchronizedStatement(node);
            break;
        case LITERAL_switch:
            statement = switchStatement(node);
            break;
        case LITERAL_try:
            statement = tryStatement(node);
            break;
        case LITERAL_throw:
            statement = throwStatement(node);
            break;
        case LITERAL_while:
            statement = whileStatement(node);
            break;
        default:
            statement = new ExpressionStatement(expression(node));
    }
    if (statement != null) {
        configureAST(statement, node);
    }
    return statement;
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 7 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy-core by groovy.

the class AsmClassGenerator method visitClosureListExpression.

public void visitClosureListExpression(ClosureListExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushVariableScope(expression.getVariableScope());
    List<Expression> expressions = expression.getExpressions();
    final int size = expressions.size();
    // init declarations
    LinkedList<DeclarationExpression> declarations = new LinkedList<DeclarationExpression>();
    for (int i = 0; i < size; i++) {
        Expression expr = expressions.get(i);
        if (expr instanceof DeclarationExpression) {
            declarations.add((DeclarationExpression) expr);
            DeclarationExpression de = (DeclarationExpression) expr;
            BinaryExpression be = new BinaryExpression(de.getLeftExpression(), de.getOperation(), de.getRightExpression());
            expressions.set(i, be);
            de.setRightExpression(ConstantExpression.NULL);
            visitDeclarationExpression(de);
        }
    }
    LinkedList instructions = new LinkedList();
    BytecodeSequence seq = new BytecodeSequence(instructions);
    BlockStatement bs = new BlockStatement();
    bs.addStatement(seq);
    Parameter closureIndex = new Parameter(ClassHelper.int_TYPE, "__closureIndex");
    ClosureExpression ce = new ClosureExpression(new Parameter[] { closureIndex }, bs);
    ce.setVariableScope(expression.getVariableScope());
    // to keep stack height put a null on stack
    instructions.add(ConstantExpression.NULL);
    // init table
    final Label dflt = new Label();
    final Label tableEnd = new Label();
    final Label[] labels = new Label[size];
    instructions.add(new BytecodeInstruction() {

        public void visit(MethodVisitor mv) {
            mv.visitVarInsn(ILOAD, 1);
            mv.visitTableSwitchInsn(0, size - 1, dflt, labels);
        }
    });
    // visit cases
    for (int i = 0; i < size; i++) {
        final Label label = new Label();
        Object expr = expressions.get(i);
        final boolean isStatement = expr instanceof Statement;
        labels[i] = label;
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitLabel(label);
                // so expressions need to pop the alibi null
                if (!isStatement)
                    mv.visitInsn(POP);
            }
        });
        instructions.add(expr);
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitJumpInsn(GOTO, tableEnd);
            }
        });
    }
    // default case
    {
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitLabel(dflt);
            }
        });
        ConstantExpression text = new ConstantExpression("invalid index for closure");
        ConstructorCallExpression cce = new ConstructorCallExpression(ClassHelper.make(IllegalArgumentException.class), text);
        ThrowStatement ts = new ThrowStatement(cce);
        instructions.add(ts);
    }
    // return
    instructions.add(new BytecodeInstruction() {

        public void visit(MethodVisitor mv) {
            mv.visitLabel(tableEnd);
            mv.visitInsn(ARETURN);
        }
    });
    // load main Closure
    visitClosureExpression(ce);
    // we need later an array to store the curried
    // closures, so we create it here and ave it
    // in a temporary variable
    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    int listArrayVar = controller.getCompileStack().defineTemporaryVariable("_listOfClosures", true);
    // add curried versions
    for (int i = 0; i < size; i++) {
        // stack: closure
        // we need to create a curried closure version
        // so we store the type on stack
        mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/CurriedClosure");
        // stack: closure, type
        // for a constructor call we need the type two times
        // and the closure after them
        mv.visitInsn(DUP2);
        mv.visitInsn(SWAP);
        // stack: closure,type,type,closure
        // so we can create the curried closure
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_0);
        mv.visitLdcInsn(i);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        mv.visitInsn(AASTORE);
        mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/CurriedClosure", "<init>", "(Lgroovy/lang/Closure;[Ljava/lang/Object;)V", false);
        // stack: closure,curriedClosure
        // we need to save the result
        mv.visitVarInsn(ALOAD, listArrayVar);
        mv.visitInsn(SWAP);
        BytecodeHelper.pushConstant(mv, i);
        mv.visitInsn(SWAP);
        mv.visitInsn(AASTORE);
    // stack: closure
    }
    // we don't need the closure any longer, so remove it
    mv.visitInsn(POP);
    // we load the array and create a list from it
    mv.visitVarInsn(ALOAD, listArrayVar);
    createListMethod.call(mv);
    // remove the temporary variable to keep the
    // stack clean
    controller.getCompileStack().removeVar(listArrayVar);
    controller.getOperandStack().pop();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) Label(org.objectweb.asm.Label) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) LinkedList(java.util.LinkedList) MethodVisitor(org.objectweb.asm.MethodVisitor) Parameter(org.codehaus.groovy.ast.Parameter) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 8 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy-core by groovy.

the class ImmutableASTTransformation method checkFinalArgNotOverridden.

private Statement checkFinalArgNotOverridden(ClassNode cNode, FieldNode fNode) {
    final String name = fNode.getName();
    Expression value = findArg(name);
    return ifS(notX(equalsNullX(value)), new ThrowStatement(ctorX(READONLYEXCEPTION_TYPE, args(constX(name), constX(cNode.getName())))));
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ToStringASTTransformation.createToString(org.codehaus.groovy.transform.ToStringASTTransformation.createToString) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Aggregations

ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)8 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)5 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)5 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)4 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)4 Statement (org.codehaus.groovy.ast.stmt.Statement)4 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)4 Parameter (org.codehaus.groovy.ast.Parameter)3 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)3 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)3 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)3 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)3 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)3 LinkedList (java.util.LinkedList)2 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)2 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)2 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)2