Search in sources :

Example 1 with CastExpression

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

the class CastExpressionOptimizer method transformCastExpression.

public Expression transformCastExpression(final CastExpression cast) {
    if (cast.isCoerce()) {
        Expression expression = cast.getExpression();
        ClassNode exprInferredType = transformer.getTypeChooser().resolveType(expression, transformer.getClassNode());
        ClassNode castType = cast.getType();
        if (castType.isArray() && expression instanceof ListExpression) {
            ArrayExpression arrayExpression = new ArrayExpression(castType.getComponentType(), ((ListExpression) expression).getExpressions());
            arrayExpression.setSourcePosition(cast);
            return transformer.transform(arrayExpression);
        }
        if (isOptimizable(exprInferredType, castType)) {
            // coerce is not needed
            CastExpression trn = new CastExpression(castType, transformer.transform(expression));
            trn.setSourcePosition(cast);
            trn.copyNodeMetaData(cast);
            return trn;
        }
    } else if (ClassHelper.char_TYPE.equals(cast.getType())) {
        Expression expression = cast.getExpression();
        if (expression instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) expression;
            if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
                String val = (String) ce.getValue();
                if (val != null && val.length() == 1) {
                    ConstantExpression result = new ConstantExpression(val.charAt(0), true);
                    result.setSourcePosition(cast);
                    return result;
                }
            }
        }
    }
    return transformer.superTransform(cast);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 2 with CastExpression

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

the class AnnotationConstantsVisitor method transformConstantExpression.

private static Expression transformConstantExpression(Expression val, ClassNode returnType) {
    ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
    if (val instanceof ConstantExpression) {
        Expression result = revertType(val, returnWrapperType);
        if (result != null) {
            return result;
        }
        return val;
    }
    if (val instanceof CastExpression) {
        CastExpression castExp = (CastExpression) val;
        Expression castee = castExp.getExpression();
        if (castee instanceof ConstantExpression) {
            if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
                return castee;
            }
            Expression result = revertType(castee, returnWrapperType);
            if (result != null) {
                return result;
            }
            return castee;
        }
    }
    return val;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 3 with CastExpression

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

the class OperandStack method pushDynamicName.

public void pushDynamicName(Expression name) {
    if (name instanceof ConstantExpression) {
        ConstantExpression ce = (ConstantExpression) name;
        Object value = ce.getValue();
        if (value instanceof String) {
            pushConstant(ce);
            return;
        }
    }
    new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg());
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 4 with CastExpression

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

the class InvokeDynamicWriter method makeIndyCall.

private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
    OperandStack operandStack = controller.getOperandStack();
    String sig = prepareIndyCall(receiver, implicitThis);
    // load arguments
    int numberOfArguments = 1;
    ArgumentListExpression ae = makeArgumentList(arguments);
    boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
    if (containsSpreadExpression) {
        controller.getAcg().despreadList(ae.getExpressions(), true);
        sig += getTypeDescription(Object[].class);
    } else {
        for (Expression arg : ae.getExpressions()) {
            arg.visit(controller.getAcg());
            if (arg instanceof CastExpression) {
                operandStack.box();
                controller.getAcg().loadWrapper(arg);
                sig += getTypeDescription(Wrapper.class);
            } else {
                sig += getTypeDescription(operandStack.getTopOperand());
            }
            numberOfArguments++;
        }
    }
    sig += ")Ljava/lang/Object;";
    String callSiteName = METHOD.getCallSiteName();
    if (adapter == null)
        callSiteName = INIT.getCallSiteName();
    int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
    finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) Wrapper(org.codehaus.groovy.runtime.wrappers.Wrapper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 5 with CastExpression

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

the class Verifier method addDefaultParameters.

protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
    Parameter[] parameters = method.getParameters();
    int counter = 0;
    List paramValues = new ArrayList();
    int size = parameters.length;
    for (int i = size - 1; i >= 0; i--) {
        Parameter parameter = parameters[i];
        if (parameter != null && parameter.hasInitialExpression()) {
            paramValues.add(i);
            paramValues.add(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
            counter++;
        }
    }
    for (int j = 1; j <= counter; j++) {
        Parameter[] newParams = new Parameter[parameters.length - j];
        ArgumentListExpression arguments = new ArgumentListExpression();
        int index = 0;
        int k = 1;
        for (Parameter parameter : parameters) {
            if (parameter == null) {
                throw new GroovyBugError("Parameter should not be null for method " + methodNode.getName());
            } else {
                if (k > counter - j && parameter.hasInitialExpression()) {
                    arguments.addExpression(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
                    k++;
                } else if (parameter.hasInitialExpression()) {
                    newParams[index++] = parameter;
                    arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
                    k++;
                } else {
                    newParams[index++] = parameter;
                    arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
                }
            }
        }
        action.call(arguments, newParams, method);
    }
    for (Parameter parameter : parameters) {
        // remove default expression and store it as node metadata
        parameter.putNodeMetaData(Verifier.INITIAL_EXPRESSION, parameter.getInitialExpression());
        parameter.setInitialExpression(null);
    }
}
Also used : ArrayList(java.util.ArrayList) GroovyBugError(org.codehaus.groovy.GroovyBugError) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) List(java.util.List) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

CastExpression (org.codehaus.groovy.ast.expr.CastExpression)23 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)17 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 Expression (org.codehaus.groovy.ast.expr.Expression)15 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)13 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)11 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)9 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)9 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)9 ArrayList (java.util.ArrayList)7 ClassNode (org.codehaus.groovy.ast.ClassNode)7 MethodNode (org.codehaus.groovy.ast.MethodNode)7 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)7 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)6 List (java.util.List)5 Parameter (org.codehaus.groovy.ast.Parameter)5 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)5 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)5 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)5 LinkedList (java.util.LinkedList)4