Search in sources :

Example 46 with TupleExpression

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

the class InnerClassVisitor method insertThis0ToSuperCall.

private void insertThis0ToSuperCall(final ConstructorCallExpression call, final ClassNode cn) {
    // calculate outer class which we need for this$0
    ClassNode parent = classNode;
    int level = 0;
    for (; parent != null && parent != cn.getOuterClass(); parent = parent.getOuterClass()) {
        level++;
    }
    // if constructor call is not in outer class, don't pass 'this' implicitly. Return.
    if (parent == null)
        return;
    // add this parameter to node
    Expression argsExp = call.getArguments();
    if (argsExp instanceof TupleExpression) {
        TupleExpression argsListExp = (TupleExpression) argsExp;
        Expression this0 = VariableExpression.THIS_EXPRESSION;
        for (int i = 0; i != level; ++i) this0 = new PropertyExpression(this0, "this$0");
        argsListExp.getExpressions().add(0, this0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 47 with TupleExpression

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

the class ClosureWriter method addGeneratedClosureConstructorCall.

public boolean addGeneratedClosureConstructorCall(ConstructorCallExpression call) {
    ClassNode classNode = controller.getClassNode();
    if (!classNode.declaresInterface(ClassHelper.GENERATED_CLOSURE_Type))
        return false;
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitVarInsn(ALOAD, 0);
    ClassNode callNode = classNode.getSuperClass();
    TupleExpression arguments = (TupleExpression) call.getArguments();
    if (arguments.getExpressions().size() != 2)
        throw new GroovyBugError("expected 2 arguments for closure constructor super call, but got" + arguments.getExpressions().size());
    arguments.getExpression(0).visit(acg);
    operandStack.box();
    arguments.getExpression(1).visit(acg);
    operandStack.box();
    // TODO: replace with normal String, p not needed
    Parameter p = new Parameter(ClassHelper.OBJECT_TYPE, "_p");
    String descriptor = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, new Parameter[] { p, p });
    mv.visitMethodInsn(INVOKESPECIAL, BytecodeHelper.getClassInternalName(callNode), "<init>", descriptor, false);
    operandStack.remove(2);
    return true;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) Parameter(org.codehaus.groovy.ast.Parameter) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 48 with TupleExpression

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

the class InnerClassCompletionVisitor method addThisReference.

private void addThisReference(ConstructorNode node) {
    if (!shouldHandleImplicitThisForInnerClass(classNode))
        return;
    Statement code = node.getCode();
    // add "this$0" field init
    // add this parameter to node
    Parameter[] params = node.getParameters();
    Parameter[] newParams = new Parameter[params.length + 1];
    System.arraycopy(params, 0, newParams, 1, params.length);
    String name = getUniqueName(params, node);
    Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
    newParams[0] = thisPara;
    node.setParameters(newParams);
    BlockStatement block = null;
    if (code == null) {
        block = new BlockStatement();
    } else if (!(code instanceof BlockStatement)) {
        block = new BlockStatement();
        block.addStatement(code);
    } else {
        block = (BlockStatement) code;
    }
    BlockStatement newCode = new BlockStatement();
    addFieldInit(thisPara, thisField, newCode);
    ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
    if (cce == null) {
        cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression());
        block.getStatements().add(0, new ExpressionStatement(cce));
    }
    if (shouldImplicitlyPassThisPara(cce)) {
        // add thisPara to this(...)
        TupleExpression args = (TupleExpression) cce.getArguments();
        List<Expression> expressions = args.getExpressions();
        VariableExpression ve = new VariableExpression(thisPara.getName());
        ve.setAccessedVariable(thisPara);
        expressions.add(0, ve);
    }
    if (cce.isSuperCall()) {
        // we have a call to super here, so we need to add
        // our code after that
        block.getStatements().add(1, newCode);
    }
    node.setCode(block);
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 49 with TupleExpression

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

the class ClassCompletionVerifier method visitMethodCallExpression.

public void visitMethodCallExpression(MethodCallExpression mce) {
    super.visitMethodCallExpression(mce);
    Expression aexp = mce.getArguments();
    if (aexp instanceof TupleExpression) {
        TupleExpression arguments = (TupleExpression) aexp;
        for (Expression e : arguments.getExpressions()) {
            checkForInvalidDeclaration(e);
        }
    } else {
        checkForInvalidDeclaration(aexp);
    }
}
Also used : PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 50 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