Search in sources :

Example 1 with StaticCompilationVisitor

use of org.codehaus.groovy.transform.sc.StaticCompilationVisitor in project groovy by apache.

the class StaticTypesBinaryExpressionMultiTypeDispatcher method assignToArray.

protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader) {
    ClassNode current = getController().getClassNode();
    ClassNode arrayType = getController().getTypeChooser().resolveType(receiver, current);
    ClassNode arrayComponentType = arrayType.getComponentType();
    int operationType = getOperandType(arrayComponentType);
    BinaryExpressionWriter bew = binExpWriter[operationType];
    if (bew.arraySet(true) && arrayType.isArray()) {
        super.assignToArray(parent, receiver, index, rhsValueLoader);
    } else {
        /******
            / This code path is needed because ACG creates array access expressions
            *******/
        WriterController controller = getController();
        StaticTypeCheckingVisitor visitor = new StaticCompilationVisitor(controller.getSourceUnit(), controller.getClassNode());
        // let's replace this assignment to a subscript operator with a
        // method call
        // e.g. x[5] = 10
        // -> (x, [], 5), =, 10
        // -> methodCall(x, "putAt", [5, 10])
        ArgumentListExpression ae = new ArgumentListExpression(index, rhsValueLoader);
        if (rhsValueLoader instanceof VariableSlotLoader && parent instanceof BinaryExpression) {
            // GROOVY-6061
            rhsValueLoader.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getTypeChooser().resolveType(parent, controller.getClassNode()));
        }
        MethodCallExpression mce = new MethodCallExpression(receiver, "putAt", ae);
        mce.setSourcePosition(parent);
        visitor.visitMethodCallExpression(mce);
        OperandStack operandStack = controller.getOperandStack();
        int height = operandStack.getStackLength();
        mce.visit(controller.getAcg());
        operandStack.pop();
        operandStack.remove(operandStack.getStackLength() - height);
        // return value of assignment
        rhsValueLoader.visit(controller.getAcg());
    }
}
Also used : StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) StaticCompilationVisitor(org.codehaus.groovy.transform.sc.StaticCompilationVisitor)

Example 2 with StaticCompilationVisitor

use of org.codehaus.groovy.transform.sc.StaticCompilationVisitor in project groovy-core by groovy.

the class StaticTypesBinaryExpressionMultiTypeDispatcher method assignToArray.

protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader) {
    ClassNode current = getController().getClassNode();
    ClassNode arrayType = getController().getTypeChooser().resolveType(receiver, current);
    ClassNode arrayComponentType = arrayType.getComponentType();
    int operationType = getOperandType(arrayComponentType);
    BinaryExpressionWriter bew = binExpWriter[operationType];
    AsmClassGenerator acg = getController().getAcg();
    if (bew.arraySet(true) && arrayType.isArray()) {
        super.assignToArray(parent, receiver, index, rhsValueLoader);
    } else {
        /**
         ****
         *            / This code path is needed because ACG creates array access expressions
         ******
         */
        WriterController controller = getController();
        StaticTypeCheckingVisitor visitor = new StaticCompilationVisitor(controller.getSourceUnit(), controller.getClassNode());
        // let's replace this assignment to a subscript operator with a
        // method call
        // e.g. x[5] = 10
        // -> (x, [], 5), =, 10
        // -> methodCall(x, "putAt", [5, 10])
        ArgumentListExpression ae = new ArgumentListExpression(index, rhsValueLoader);
        if (rhsValueLoader instanceof VariableSlotLoader && parent instanceof BinaryExpression) {
            // GROOVY-6061
            Expression right = ((BinaryExpression) parent).getRightExpression();
            rhsValueLoader.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getTypeChooser().resolveType(parent, controller.getClassNode()));
        }
        MethodCallExpression mce = new MethodCallExpression(receiver, "putAt", ae);
        mce.setSourcePosition(parent);
        visitor.visitMethodCallExpression(mce);
        OperandStack operandStack = controller.getOperandStack();
        int height = operandStack.getStackLength();
        mce.visit(controller.getAcg());
        operandStack.pop();
        operandStack.remove(operandStack.getStackLength() - height);
        // return value of assignment
        rhsValueLoader.visit(controller.getAcg());
    }
}
Also used : StaticCompilationVisitor(org.codehaus.groovy.transform.sc.StaticCompilationVisitor) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor)

Example 3 with StaticCompilationVisitor

use of org.codehaus.groovy.transform.sc.StaticCompilationVisitor in project groovy by apache.

the class StaticTypesBinaryExpressionMultiTypeDispatcher method assignToArray.

@Override
protected void assignToArray(final Expression enclosing, final Expression receiver, final Expression subscript, final Expression rhsValueLoader, final boolean safe) {
    ClassNode receiverType = controller.getTypeChooser().resolveType(receiver, controller.getClassNode());
    if (receiverType.isArray() && !safe && binExpWriter[getOperandType(receiverType.getComponentType())].arraySet(true)) {
        super.assignToArray(enclosing, receiver, subscript, rhsValueLoader, safe);
    } else {
        // GROOVY-6061
        if (rhsValueLoader instanceof VariableSlotLoader && enclosing instanceof BinaryExpression) {
            rhsValueLoader.putNodeMetaData(INFERRED_TYPE, controller.getTypeChooser().resolveType(enclosing, controller.getClassNode()));
        }
        // GROOVY-9771
        receiver.visit(new StaticCompilationVisitor(controller.getSourceUnit(), controller.getClassNode()));
        // replace assignment to a subscript operator with a method call
        // e.g. x[5] = 10 -> methodCall(x, "putAt", [5, 10])
        MethodCallExpression call = callX(receiver, "putAt", args(subscript, rhsValueLoader));
        call.setSafe(safe);
        call.setSourcePosition(enclosing);
        OperandStack operandStack = controller.getOperandStack();
        int height = operandStack.getStackLength();
        call.visit(controller.getAcg());
        operandStack.pop();
        operandStack.remove(operandStack.getStackLength() - height);
        // return value of assignment
        rhsValueLoader.visit(controller.getAcg());
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) ClassNode(org.codehaus.groovy.ast.ClassNode) VariableSlotLoader(org.codehaus.groovy.classgen.asm.VariableSlotLoader) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) StaticCompilationVisitor(org.codehaus.groovy.transform.sc.StaticCompilationVisitor)

Aggregations

StaticCompilationVisitor (org.codehaus.groovy.transform.sc.StaticCompilationVisitor)3 StaticTypeCheckingVisitor (org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor)2 ClassNode (org.codehaus.groovy.ast.ClassNode)1 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)1 OperandStack (org.codehaus.groovy.classgen.asm.OperandStack)1 VariableSlotLoader (org.codehaus.groovy.classgen.asm.VariableSlotLoader)1