Search in sources :

Example 16 with TupleExpression

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

the class EnumCompletionVisitor method transformConstructor.

/**
 * If constructor does not define a call to super, then transform constructor
 * to get String,int parameters at beginning and add call super(String,int).
 */
private void transformConstructor(final ConstructorNode ctor) {
    boolean chainedThisConstructorCall = false;
    ConstructorCallExpression cce = null;
    if (ctor.firstStatementIsSpecialConstructorCall()) {
        Statement code = ctor.getFirstStatement();
        cce = (ConstructorCallExpression) ((ExpressionStatement) code).getExpression();
        if (cce.isSuperCall())
            return;
        // must be call to this(...)
        chainedThisConstructorCall = true;
    }
    // we need to add parameters
    Parameter[] oldP = ctor.getParameters();
    Parameter[] newP = new Parameter[oldP.length + 2];
    String stringParameterName = getUniqueVariableName("__str", ctor.getCode());
    newP[0] = new Parameter(ClassHelper.STRING_TYPE, stringParameterName);
    String intParameterName = getUniqueVariableName("__int", ctor.getCode());
    newP[1] = new Parameter(ClassHelper.int_TYPE, intParameterName);
    System.arraycopy(oldP, 0, newP, 2, oldP.length);
    ctor.setParameters(newP);
    VariableExpression stringVariable = new VariableExpression(newP[0]);
    VariableExpression intVariable = new VariableExpression(newP[1]);
    if (chainedThisConstructorCall) {
        TupleExpression args = (TupleExpression) cce.getArguments();
        List<Expression> argsExprs = args.getExpressions();
        argsExprs.add(0, stringVariable);
        argsExprs.add(1, intVariable);
    } else {
        // add a super call
        List<Expression> args = new ArrayList<>();
        args.add(stringVariable);
        args.add(intVariable);
        if (EnumVisitor.isAnonymousInnerClass(ctor.getDeclaringClass())) {
            for (Parameter parameter : oldP) {
                args.add(new VariableExpression(parameter));
            }
            ClassNode enumClass = ctor.getDeclaringClass().getSuperClass();
            // GROOVY-6747: bridge enum's private constructor
            makeBridgeConstructor(enumClass, newP);
            args.add(new CastExpression(enumClass.getPlainNodeReference(), ConstantExpression.NULL));
        }
        cce = new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(args));
        BlockStatement code = new BlockStatement();
        code.addStatement(new ExpressionStatement(cce));
        Statement oldCode = ctor.getCode();
        if (oldCode != null)
            code.addStatement(oldCode);
        ctor.setCode(code);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayList(java.util.ArrayList) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 17 with TupleExpression

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

the class InnerClassVisitor method visitConstructorCallExpression.

@Override
public void visitConstructorCallExpression(ConstructorCallExpression call) {
    super.visitConstructorCallExpression(call);
    if (!call.isUsingAnonymousInnerClass()) {
        passThisReference(call);
        return;
    }
    InnerClassNode innerClass = (InnerClassNode) call.getType();
    ClassNode outerClass = innerClass.getOuterClass();
    ClassNode superClass = innerClass.getSuperClass();
    if (!superClass.isInterface() && superClass.getOuterClass() != null && !(superClass.isStaticClass() || (superClass.getModifiers() & ACC_STATIC) != 0)) {
        insertThis0ToSuperCall(call, innerClass);
    }
    if (!innerClass.getDeclaredConstructors().isEmpty())
        return;
    if ((innerClass.getModifiers() & ACC_STATIC) != 0)
        return;
    VariableScope scope = innerClass.getVariableScope();
    if (scope == null)
        return;
    boolean isStatic = !inClosure && isStatic(innerClass, scope, call);
    // expressions = constructor call arguments
    List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
    // block = init code for the constructor we produce
    BlockStatement block = new BlockStatement();
    // parameters = parameters of the constructor
    int additionalParamCount = (isStatic ? 0 : 1) + scope.getReferencedLocalVariablesCount();
    List<Parameter> parameters = new ArrayList<>(expressions.size() + additionalParamCount);
    // superCallArguments = arguments for the super call == the constructor call arguments
    List<Expression> superCallArguments = new ArrayList<>(expressions.size());
    // first we add a super() call for all expressions given in the constructor call expression
    for (int i = 0, n = expressions.size(); i < n; i += 1) {
        // add one parameter for each expression in the constructor call
        Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + additionalParamCount + i);
        parameters.add(param);
        // add the corresponding argument to the super constructor call
        superCallArguments.add(new VariableExpression(param));
    }
    // add the super call
    ConstructorCallExpression cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression(superCallArguments));
    block.addStatement(new ExpressionStatement(cce));
    int pCount = 0;
    if (!isStatic) {
        // need to pass "this" to access unknown methods/properties
        ClassNode enclosingType = (inClosure ? ClassHelper.CLOSURE_TYPE : outerClass).getPlainNodeReference();
        expressions.add(pCount, new VariableExpression("this", enclosingType));
        Parameter thisParameter = new Parameter(enclosingType, "p" + pCount);
        parameters.add(pCount++, thisParameter);
        // "this" reference is saved in a field named "this$0"
        FieldNode thisField = innerClass.addField("this$0", ACC_FINAL | ACC_SYNTHETIC, enclosingType, null);
        addFieldInit(thisParameter, thisField, block);
    }
    // for each shared variable, add a Reference field
    for (Iterator<Variable> it = scope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
        Variable var = it.next();
        VariableExpression ve = new VariableExpression(var);
        ve.setClosureSharedVariable(true);
        ve.setUseReferenceDirectly(true);
        expressions.add(pCount, ve);
        ClassNode referenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference();
        Parameter p = new Parameter(referenceType, "p" + pCount);
        p.setOriginType(var.getOriginType());
        parameters.add(pCount++, p);
        VariableExpression initial = new VariableExpression(p);
        initial.setSynthetic(true);
        initial.setUseReferenceDirectly(true);
        FieldNode pField = innerClass.addFieldFirst(ve.getName(), ACC_PUBLIC | ACC_SYNTHETIC, referenceType, initial);
        pField.setHolder(true);
        pField.setOriginType(ClassHelper.getWrapper(var.getOriginType()));
    }
    innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, block);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Variable(org.codehaus.groovy.ast.Variable) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 18 with TupleExpression

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

the class InnerClassVisitor method passThisReference.

// this is the counterpart of addThisReference(). To non-static inner classes, outer this should be
// passed as the first argument implicitly.
private void passThisReference(final ConstructorCallExpression call) {
    ClassNode cn = call.getType().redirect();
    if (!shouldHandleImplicitThisForInnerClass(cn))
        return;
    boolean isInStaticContext;
    if (currentMethod != null)
        isInStaticContext = currentMethod.isStatic();
    else if (currentField != null)
        isInStaticContext = currentField.isStatic();
    else
        isInStaticContext = !processingObjInitStatements;
    // GROOVY-10289
    ClassNode enclosing = classNode;
    while (!isInStaticContext && !enclosing.equals(cn.getOuterClass())) {
        isInStaticContext = (enclosing.getModifiers() & ACC_STATIC) != 0;
        // TODO: if enclosing is a local type, also test field or method
        enclosing = enclosing.getOuterClass();
        if (enclosing == null)
            break;
    }
    if (isInStaticContext) {
        // constructor call is in static context and the inner class is non-static - 1st arg is supposed to be
        // passed as enclosing "this" instance
        Expression args = call.getArguments();
        if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty()) {
            addError("No enclosing instance passed in constructor call of a non-static inner class", call);
        }
    } else {
        insertThis0ToSuperCall(call, cn);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 19 with TupleExpression

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

the class StaticImportVisitor method transformMethodCallExpression.

protected Expression transformMethodCallExpression(MethodCallExpression mce) {
    Expression object = transform(mce.getObjectExpression());
    Expression method = transform(mce.getMethod());
    Expression args = transform(mce.getArguments());
    // GROOVY-10396: skip the instance method checks when the context is static with-respect-to current class
    boolean staticWrtCurrent = inSpecialConstructorCall || currentMethod != null && currentMethod.isStatic();
    if (mce.isImplicitThis()) {
        String name = mce.getMethodAsString();
        boolean thisOrSuperMethod = staticWrtCurrent ? hasPossibleStaticMethod(currentClass, name, args, true) : currentClass.tryFindPossibleMethod(name, args) != null;
        if (!thisOrSuperMethod && currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
            Expression result = findStaticMethodImportFromModule(method, args);
            if (result != null) {
                setSourcePosition(result, mce);
                return result;
            }
        }
    } else if (staticWrtCurrent && isSuperExpression(object)) {
        Expression result = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
        result.setSourcePosition(mce);
        return result;
    }
    if (method instanceof ConstantExpression && ((ConstantExpression) method).getValue() instanceof String && (mce.isImplicitThis() || isThisOrSuper(object))) {
        String methodName = (String) ((ConstantExpression) method).getValue();
        boolean foundInstanceMethod = !staticWrtCurrent && currentClass.hasPossibleMethod(methodName, args);
        Predicate<ClassNode> hasPossibleStaticMember = cn -> {
            if (hasPossibleStaticMethod(cn, methodName, args, true)) {
                return true;
            }
            // GROOVY-9587: don't check for property for non-empty call args
            if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty() && hasPossibleStaticProperty(cn, methodName)) {
                return true;
            }
            return false;
        };
        if (mce.isImplicitThis()) {
            if (isInnerClass(currentClass)) {
                if (inSpecialConstructorCall && !foundInstanceMethod) {
                    // check for reference to outer class method in this(...) or super(...)
                    if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
                        object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
                    } else if (hasPossibleStaticMember.test(currentClass.getOuterClass())) {
                        Expression result = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
                        result.setSourcePosition(mce);
                        return result;
                    }
                }
            } else if (inSpecialConstructorCall || (!inClosure && !foundInstanceMethod && !methodName.equals("call"))) {
                // check for reference to static method in this(...) or super(...) or when call not resolved
                if (hasPossibleStaticMember.test(currentClass)) {
                    Expression result = new StaticMethodCallExpression(currentClass, methodName, args);
                    result.setSourcePosition(mce);
                    return result;
                }
            }
        }
    }
    MethodCallExpression result = new MethodCallExpression(object, method, args);
    result.setGenericsTypes(mce.getGenericsTypes());
    result.setMethodTarget(mce.getMethodTarget());
    result.setImplicitThis(mce.isImplicitThis());
    result.setSpreadSafe(mce.isSpreadSafe());
    result.setSafe(mce.isSafe());
    result.setSourcePosition(mce);
    return result;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ClassNodeUtils.hasPossibleStaticMethod(org.apache.groovy.ast.tools.ClassNodeUtils.hasPossibleStaticMethod) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) GeneralUtils.getSetterName(org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassNodeUtils.getPropNameForAccessor(org.apache.groovy.ast.tools.ClassNodeUtils.getPropNameForAccessor) ClosureUtils.getParametersSafe(org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe) GeneralUtils.getGetterName(org.codehaus.groovy.ast.tools.GeneralUtils.getGetterName) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer) ModuleNode(org.codehaus.groovy.ast.ModuleNode) Variable(org.codehaus.groovy.ast.Variable) MethodNode(org.codehaus.groovy.ast.MethodNode) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) Map(java.util.Map) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) ClassNodeUtils.hasPossibleStaticProperty(org.apache.groovy.ast.tools.ClassNodeUtils.hasPossibleStaticProperty) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) Predicate(java.util.function.Predicate) ClassHelper(org.codehaus.groovy.ast.ClassHelper) Types(org.codehaus.groovy.syntax.Types) ClassNodeUtils.isValidAccessorName(org.apache.groovy.ast.tools.ClassNodeUtils.isValidAccessorName) BeanUtils.capitalize(org.apache.groovy.util.BeanUtils.capitalize) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassNodeUtils.hasStaticProperty(org.apache.groovy.ast.tools.ClassNodeUtils.hasStaticProperty) ClassNodeUtils.getField(org.apache.groovy.ast.tools.ClassNodeUtils.getField) ExpressionUtils.isThisOrSuper(org.apache.groovy.ast.tools.ExpressionUtils.isThisOrSuper) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClassNodeUtils.isInnerClass(org.apache.groovy.ast.tools.ClassNodeUtils.isInnerClass) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) List(java.util.List) ImportNode(org.codehaus.groovy.ast.ImportNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) ExpressionUtils.transformInlineConstants(org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ClassNode(org.codehaus.groovy.ast.ClassNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 20 with TupleExpression

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

the class BinaryExpressionHelper method evaluateEqual.

public void evaluateEqual(final BinaryExpression expression, final boolean defineVariable) {
    AsmClassGenerator acg = controller.getAcg();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    Expression leftExpression = expression.getLeftExpression();
    Expression rightExpression = expression.getRightExpression();
    boolean directAssignment = defineVariable && !(leftExpression instanceof TupleExpression);
    if (directAssignment && rightExpression instanceof EmptyExpression) {
        VariableExpression ve = (VariableExpression) leftExpression;
        BytecodeVariable var = compileStack.defineVariable(ve, controller.getTypeChooser().resolveType(ve, controller.getClassNode()), false);
        operandStack.loadOrStoreVariable(var, false);
        return;
    }
    // evaluate the RHS and store the result
    // TODO: LHS has not been visited, it could be a variable in a closure and type chooser is not aware.
    ClassNode lhsType = controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
    if (rightExpression instanceof ListExpression && lhsType.isArray()) {
        ListExpression list = (ListExpression) rightExpression;
        ArrayExpression array = new ArrayExpression(lhsType.getComponentType(), list.getExpressions());
        array.setSourcePosition(list);
        array.visit(acg);
    } else if (rightExpression instanceof EmptyExpression) {
        // TODO: lhsType?
        loadInitValue(leftExpression.getType());
    } else {
        rightExpression.visit(acg);
    }
    ClassNode rhsType = operandStack.getTopOperand();
    int rhsValueId;
    if (directAssignment) {
        VariableExpression var = (VariableExpression) leftExpression;
        if (var.isClosureSharedVariable() && ClassHelper.isPrimitiveType(rhsType)) {
            // GROOVY-5570: if a closure shared variable is a primitive type, it must be boxed
            rhsType = ClassHelper.getWrapper(rhsType);
            operandStack.box();
        }
        // form as it is closure shared
        if (var.isClosureSharedVariable() && ClassHelper.isPrimitiveType(var.getOriginType()) && isNullConstant(rightExpression)) {
            operandStack.doGroovyCast(var.getOriginType());
            // these two are never reached in bytecode and only there
            // to avoid verify errors and compiler infrastructure hazzle
            operandStack.box();
            operandStack.doGroovyCast(lhsType);
        }
        // normal type transformation
        if (!ClassHelper.isPrimitiveType(lhsType) && isNullConstant(rightExpression)) {
            operandStack.replace(lhsType);
        } else {
            operandStack.doGroovyCast(lhsType);
        }
        rhsType = lhsType;
        rhsValueId = compileStack.defineVariable(var, lhsType, true).getIndex();
    } else {
        rhsValueId = compileStack.defineTemporaryVariable("$rhs", rhsType, true);
    }
    // TODO: if RHS is VariableSlotLoader already, then skip creating a new one
    BytecodeExpression rhsValueLoader = new VariableSlotLoader(rhsType, rhsValueId, operandStack);
    // assignment for subscript
    if (leftExpression instanceof BinaryExpression) {
        BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
        if (leftBinExpr.getOperation().getType() == LEFT_SQUARE_BRACKET) {
            assignToArray(expression, leftBinExpr.getLeftExpression(), leftBinExpr.getRightExpression(), rhsValueLoader, leftBinExpr.isSafe());
        }
        compileStack.removeVar(rhsValueId);
        return;
    }
    compileStack.pushLHS(true);
    if (leftExpression instanceof TupleExpression) {
        // multiple declaration
        TupleExpression tuple = (TupleExpression) leftExpression;
        int i = 0;
        for (Expression e : tuple.getExpressions()) {
            callX(rhsValueLoader, "getAt", args(constX(i++))).visit(acg);
            if (defineVariable) {
                Variable v = (Variable) e;
                operandStack.doGroovyCast(v);
                compileStack.defineVariable(v, true);
                operandStack.remove(1);
            } else {
                e.visit(acg);
            }
        }
    } else if (defineVariable) {
        // single declaration
        rhsValueLoader.visit(acg);
        operandStack.remove(1);
        compileStack.popLHS();
        return;
    } else {
        // normal assignment
        int mark = operandStack.getStackLength();
        rhsValueLoader.visit(acg);
        leftExpression.visit(acg);
        operandStack.remove(operandStack.getStackLength() - mark);
    }
    compileStack.popLHS();
    // return value of assignment
    rhsValueLoader.visit(acg);
    compileStack.removeVar(rhsValueId);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression)

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