Search in sources :

Example 1 with StaticTypeCheckingVisitor

use of org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor 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 2 with StaticTypeCheckingVisitor

use of org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor 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 3 with StaticTypeCheckingVisitor

use of org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor in project groovy by apache.

the class StaticTypesTransformation method visit.

//    @Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    Map<String, Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        visitor = newVisitor(source, methodNode.getDeclaringClass());
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 4 with StaticTypeCheckingVisitor

use of org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor in project groovy-core by groovy.

the class StaticCompileTransformation method visit.

@Override
public void visit(final ASTNode[] nodes, final SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    Map<String, Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        classNode.putNodeMetaData(WriterControllerFactory.class, factory);
        node.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        ClassNode declaringClass = methodNode.getDeclaringClass();
        visitor = newVisitor(source, declaringClass);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        methodNode.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        if (declaringClass.getNodeMetaData(WriterControllerFactory.class) == null) {
            declaringClass.putNodeMetaData(WriterControllerFactory.class, factory);
        }
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
    StaticCompilationTransformer transformer = new StaticCompilationTransformer(source, visitor);
    if (node instanceof ClassNode) {
        transformer.visitClass((ClassNode) node);
    } else if (node instanceof MethodNode) {
        transformer.visitMethod((MethodNode) node);
    }
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) WriterControllerFactory(org.codehaus.groovy.classgen.asm.WriterControllerFactory) StaticCompilationTransformer(org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer)

Example 5 with StaticTypeCheckingVisitor

use of org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor in project groovy-core by groovy.

the class StaticTypesTransformation method visit.

//    @Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    Map<String, Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        visitor = newVisitor(source, methodNode.getDeclaringClass());
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Aggregations

StaticTypeCheckingVisitor (org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor)6 Expression (org.codehaus.groovy.ast.expr.Expression)4 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)4 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)2 WriterControllerFactory (org.codehaus.groovy.classgen.asm.WriterControllerFactory)2 StaticCompilationVisitor (org.codehaus.groovy.transform.sc.StaticCompilationVisitor)2 StaticCompilationTransformer (org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer)2 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)1