Search in sources :

Example 41 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.

the class InvocationWriter method makeUncachedCall.

protected void makeUncachedCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis, boolean containsSpreadExpression) {
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    AsmClassGenerator acg = controller.getAcg();
    // ensure VariableArguments are read, not stored
    compileStack.pushLHS(false);
    // sender only for call sites
    if (adapter == AsmClassGenerator.setProperty) {
        ConstantExpression.NULL.visit(acg);
    } else {
        sender.visit(acg);
    }
    String methodName = getMethodName(message);
    if (adapter == invokeMethodOnSuper && methodName != null) {
        // for MOP method
        controller.getSuperMethodNames().add(methodName);
    }
    // receiver
    compileStack.pushImplicitThis(implicitThis);
    receiver.visit(acg);
    operandStack.box();
    compileStack.popImplicitThis();
    int operandsToRemove = 2;
    // message
    if (message != null) {
        message.visit(acg);
        operandStack.box();
        operandsToRemove += 1;
    }
    // arguments
    int numberOfArguments = containsSpreadExpression ? -1 : AsmClassGenerator.argumentSize(arguments);
    if (numberOfArguments > MethodCallerMultiAdapter.MAX_ARGS || containsSpreadExpression) {
        ArgumentListExpression ae = makeArgumentList(arguments);
        if (containsSpreadExpression) {
            acg.despreadList(ae.getExpressions(), true);
        } else {
            ae.visit(acg);
        }
    } else if (numberOfArguments > 0) {
        operandsToRemove += numberOfArguments;
        TupleExpression te = (TupleExpression) arguments;
        for (int i = 0; i < numberOfArguments; i += 1) {
            Expression argument = te.getExpression(i);
            argument.visit(acg);
            operandStack.box();
            if (argument instanceof CastExpression)
                acg.loadWrapper(argument);
        }
    }
    if (adapter == null)
        adapter = invokeMethod;
    adapter.call(controller.getMethodVisitor(), numberOfArguments, safe, spreadSafe);
    compileStack.popLHS();
    operandStack.replace(ClassHelper.OBJECT_TYPE, operandsToRemove);
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ExpressionUtils.isThisExpression(org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 42 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.

the class InvocationWriter method writeDirectConstructorCall.

private boolean writeDirectConstructorCall(final ConstructorCallExpression call) {
    if (!controller.isFastPath())
        return false;
    OptimizingStatementWriter.StatementMeta meta = call.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
    ConstructorNode cn = null;
    if (meta != null)
        cn = (ConstructorNode) meta.target;
    if (cn == null)
        return false;
    String ownerDescriptor = prepareConstructorCall(cn);
    TupleExpression args = makeArgumentList(call.getArguments());
    loadArguments(args.getExpressions(), cn.getParameters());
    finnishConstructorCall(cn, ownerDescriptor, args.getExpressions().size());
    return true;
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 43 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.

the class InvocationWriter method makeDirectConstructorCall.

private boolean makeDirectConstructorCall(final List<ConstructorNode> constructors, final ConstructorCallExpression call, final ClassNode callNode) {
    if (!controller.isConstructor())
        return false;
    Expression arguments = call.getArguments();
    List<Expression> argumentList;
    if (arguments instanceof TupleExpression) {
        argumentList = ((TupleExpression) arguments).getExpressions();
    } else {
        argumentList = new ArrayList<>();
        argumentList.add(arguments);
    }
    for (Expression expression : argumentList) {
        if (expression instanceof SpreadExpression)
            return false;
    }
    ConstructorNode cn = getMatchingConstructor(constructors, argumentList);
    if (cn == null)
        return false;
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    Parameter[] params = cn.getParameters();
    mv.visitVarInsn(ALOAD, 0);
    for (int i = 0, n = params.length; i < n; i += 1) {
        Expression expression = argumentList.get(i);
        expression.visit(controller.getAcg());
        if (!isNullConstant(expression)) {
            operandStack.doGroovyCast(params[i].getType());
        }
        operandStack.remove(1);
    }
    String descriptor = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, params);
    mv.visitMethodInsn(INVOKESPECIAL, BytecodeHelper.getClassInternalName(callNode), "<init>", descriptor, false);
    return true;
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ExpressionUtils.isThisExpression(org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Parameter(org.codehaus.groovy.ast.Parameter) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 44 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.

the class OptimizingStatementWriter method writeDeclarationExtraction.

private boolean writeDeclarationExtraction(final Statement statement) {
    Expression ex = null;
    if (statement instanceof ReturnStatement) {
        ReturnStatement rs = (ReturnStatement) statement;
        ex = rs.getExpression();
    } else if (statement instanceof ExpressionStatement) {
        ExpressionStatement es = (ExpressionStatement) statement;
        ex = es.getExpression();
    } else {
        throw new GroovyBugError("unknown statement type :" + statement.getClass());
    }
    if (!(ex instanceof DeclarationExpression))
        return true;
    DeclarationExpression declaration = (DeclarationExpression) ex;
    ex = declaration.getLeftExpression();
    if (ex instanceof TupleExpression)
        return false;
    // stash declared variable in case we do subsequent visits after we
    // change to assignment only
    StatementMeta meta = statement.getNodeMetaData(StatementMeta.class);
    if (meta != null) {
        meta.declaredVariableExpression = declaration.getVariableExpression();
    }
    // change statement to do assignment only
    BinaryExpression assignment = new BinaryExpression(declaration.getLeftExpression(), declaration.getOperation(), declaration.getRightExpression());
    assignment.setSourcePosition(declaration);
    assignment.copyNodeMetaData(declaration);
    // replace statement code
    if (statement instanceof ReturnStatement) {
        ReturnStatement rs = (ReturnStatement) statement;
        rs.setExpression(assignment);
    } else if (statement instanceof ExpressionStatement) {
        ExpressionStatement es = (ExpressionStatement) statement;
        es.setExpression(assignment);
    } else {
        throw new GroovyBugError("unknown statement type :" + statement.getClass());
    }
    return true;
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ExpressionUtils.isThisExpression(org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) GroovyBugError(org.codehaus.groovy.GroovyBugError) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 45 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.

the class StaticTypeCheckingVisitor method typeCheckAssignment.

protected void typeCheckAssignment(final BinaryExpression assignmentExpression, final Expression leftExpression, final ClassNode leftExpressionType, final Expression rightExpression, final ClassNode rightExpressionType) {
    if (leftExpression instanceof TupleExpression) {
        if (!typeCheckMultipleAssignmentAndContinue(leftExpression, rightExpression))
            return;
    }
    // TODO: need errors for write-only too!
    if (addedReadOnlyPropertyError(leftExpression))
        return;
    ClassNode rTypeWrapped = adjustTypeForSpreading(rightExpressionType, leftExpression);
    if (!checkCompatibleAssignmentTypes(leftExpressionType, rTypeWrapped, rightExpression)) {
        if (!extension.handleIncompatibleAssignment(leftExpressionType, rightExpressionType, assignmentExpression)) {
            addAssignmentError(leftExpressionType, rightExpressionType, rightExpression);
        }
    } else {
        ClassNode lTypeRedirect = leftExpressionType.redirect();
        addPrecisionErrors(lTypeRedirect, leftExpressionType, rightExpressionType, rightExpression);
        if (rightExpression instanceof ListExpression) {
            addListAssignmentConstructorErrors(lTypeRedirect, leftExpressionType, rightExpressionType, rightExpression, assignmentExpression);
        } else if (rightExpression instanceof MapExpression) {
            addMapAssignmentConstructorErrors(lTypeRedirect, leftExpression, rightExpression);
        }
        if (!hasGStringStringError(leftExpressionType, rTypeWrapped, rightExpression) && !isConstructorAbbreviation(leftExpressionType, rightExpression)) {
            checkTypeGenerics(leftExpressionType, rTypeWrapped, rightExpression);
        }
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) StaticTypeCheckingSupport.findDGMMethodsForClassNode(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsForClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Aggregations

TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)78 Expression (org.codehaus.groovy.ast.expr.Expression)65 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)56 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)44 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)42 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)41 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)40 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)35 ClassNode (org.codehaus.groovy.ast.ClassNode)34 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)34 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)31 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)23 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)23 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)22 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)20 Parameter (org.codehaus.groovy.ast.Parameter)20 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)18 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)18 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)17