Search in sources :

Example 16 with BytecodeExpression

use of org.codehaus.groovy.classgen.BytecodeExpression in project groovy by apache.

the class BinaryExpressionHelper method writePostOrPrefixMethod.

protected void writePostOrPrefixMethod(int op, String method, Expression expression, Expression orig) {
    final OperandStack operandStack = controller.getOperandStack();
    // at this point the receiver will be already on the stack.
    // in a[1]++ the method will be "++" aka "next" and the receiver a[1]
    ClassNode BEType = controller.getTypeChooser().resolveType(expression, controller.getClassNode());
    Expression callSiteReceiverSwap = new BytecodeExpression(BEType) {

        @Override
        public void visit(MethodVisitor mv) {
            // CallSite is normally not showing up on the 
            // operandStack, so we place a dummy here with same
            // slot length.
            operandStack.push(ClassHelper.OBJECT_TYPE);
            // change (receiver,callsite) to (callsite,receiver)
            operandStack.swap();
            setType(operandStack.getTopOperand());
            // no need to keep any of those on the operand stack
            // after this expression is processed, the operand stack
            // will contain callSiteReceiverSwap.getType()
            operandStack.remove(2);
        }
    };
    // execute method
    // this will load the callsite and the receiver normally in the wrong
    // order since the receiver is already present, but before the callsite
    // Therefore we use callSiteReceiverSwap to correct the order. 
    // After this call the JVM operand stack will contain the the result of
    // the method call... usually simply Object in operandStack
    controller.getCallSiteWriter().makeCallSite(callSiteReceiverSwap, method, MethodCallExpression.NO_ARGUMENTS, false, false, false, false);
// now rhs is completely done and we need only to store. In a[1]++ this 
// would be a.getAt(1).next() for the rhs, "lhs" code is a.putAt(1, rhs)
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

BytecodeExpression (org.codehaus.groovy.classgen.BytecodeExpression)16 ClassNode (org.codehaus.groovy.ast.ClassNode)12 MethodVisitor (org.objectweb.asm.MethodVisitor)12 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)6 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)6 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)6 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)4 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)4 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)4 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 ElvisOperatorExpression (org.codehaus.groovy.ast.expr.ElvisOperatorExpression)4 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)4 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)4 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)4 PostfixExpression (org.codehaus.groovy.ast.expr.PostfixExpression)4 PrefixExpression (org.codehaus.groovy.ast.expr.PrefixExpression)4 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)4 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)4