Search in sources :

Example 1 with RangeExpression

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

the class AstBuilder method visitShiftExprAlt.

@Override
public Expression visitShiftExprAlt(final ShiftExprAltContext ctx) {
    Expression left = (Expression) this.visit(ctx.left);
    Expression right = (Expression) this.visit(ctx.right);
    if (asBoolean(ctx.rangeOp)) {
        return configureAST(new RangeExpression(left, right, ctx.rangeOp.getText().startsWith("<"), ctx.rangeOp.getText().endsWith("<")), ctx);
    }
    org.codehaus.groovy.syntax.Token op;
    Token antlrToken;
    if (asBoolean(ctx.dlOp)) {
        op = this.createGroovyToken(ctx.dlOp, 2);
        antlrToken = ctx.dlOp;
    } else if (asBoolean(ctx.dgOp)) {
        op = this.createGroovyToken(ctx.dgOp, 2);
        antlrToken = ctx.dgOp;
    } else if (asBoolean(ctx.tgOp)) {
        op = this.createGroovyToken(ctx.tgOp, 3);
        antlrToken = ctx.tgOp;
    } else {
        throw createParsingFailedException("Unsupported shift expression: " + ctx.getText(), ctx);
    }
    BinaryExpression binaryExpression = new BinaryExpression(left, op, right);
    if (isTrue(ctx, IS_INSIDE_CONDITIONAL_EXPRESSION)) {
        return configureAST(binaryExpression, antlrToken);
    }
    return configureAST(binaryExpression, ctx);
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) Token(org.antlr.v4.runtime.Token)

Example 2 with RangeExpression

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

the class BinaryExpressionTransformer method transformInOperation.

private Expression transformInOperation(final BinaryExpression bin, final boolean in) {
    Expression leftExpression = bin.getLeftExpression(), rightExpression = bin.getRightExpression();
    // transform "left [!]in right" into "right.is[Not]Case(left)"
    MethodCallExpression call = callX(rightExpression, in ? "isCase" : "isNotCase", leftExpression);
    call.setImplicitThis(false);
    call.setSourcePosition(bin);
    call.copyNodeMetaData(bin);
    call.setMethodTarget(bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
    // GROOVY-7473: no null test for simple cases
    if (rightExpression instanceof ListExpression || rightExpression instanceof MapExpression || rightExpression instanceof RangeExpression || rightExpression instanceof ClassExpression || rightExpression instanceof ConstantExpression && !isNullConstant(rightExpression))
        return staticCompilationTransformer.transform(call);
    // GROOVY-6137, GROOVY-7473: null safety and one-time evaluation
    call.setObjectExpression(rightExpression = transformRepeatedReference(rightExpression));
    Expression safe = ternaryX(new CompareToNullExpression(rightExpression, true), new CompareToNullExpression(leftExpression, in), call);
    safe.putNodeMetaData("classgen.callback", classgenCallback(call.getObjectExpression()));
    return staticCompilationTransformer.transform(safe);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListOfExpressionsExpression(org.codehaus.groovy.transform.sc.ListOfExpressionsExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) TemporaryVariableExpression(org.codehaus.groovy.transform.sc.TemporaryVariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 3 with RangeExpression

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

the class StaticTypeCheckingVisitor method getType.

protected ClassNode getType(final ASTNode exp) {
    ClassNode cn = exp.getNodeMetaData(INFERRED_TYPE);
    if (cn != null) {
        return cn;
    }
    if (exp instanceof ClassExpression) {
        ClassNode node = CLASS_Type.getPlainNodeReference();
        node.setGenericsTypes(new GenericsType[] { new GenericsType(((ClassExpression) exp).getType()) });
        return node;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression vexp = (VariableExpression) exp;
        ClassNode selfTrait = isTraitSelf(vexp);
        if (selfTrait != null)
            return makeSelf(selfTrait);
        if (vexp.isThisExpression())
            return makeThis();
        if (vexp.isSuperExpression())
            return makeSuper();
        Variable variable = vexp.getAccessedVariable();
        if (variable instanceof FieldNode) {
            FieldNode fieldNode = (FieldNode) variable;
            ClassNode fieldType = fieldNode.getOriginType();
            if (!fieldNode.isStatic() && GenericsUtils.hasUnresolvedGenerics(fieldType)) {
                ClassNode declType = fieldNode.getDeclaringClass(), thisType = typeCheckingContext.getEnclosingClassNode();
                fieldType = resolveGenericsWithContext(extractPlaceHolders(thisType, declType), fieldType);
            }
            return fieldType;
        }
        if (variable != vexp && variable instanceof VariableExpression) {
            return getType((VariableExpression) variable);
        }
        if (variable instanceof Parameter) {
            Parameter parameter = (Parameter) variable;
            ClassNode type = null;
            // check if param part of control structure - but not if inside instanceof
            List<ClassNode> temporaryTypesForExpression = getTemporaryTypesForExpression(vexp);
            if (temporaryTypesForExpression == null || temporaryTypesForExpression.isEmpty()) {
                type = typeCheckingContext.controlStructureVariables.get(parameter);
            }
            // now check for closure override
            TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
            if (type == null && enclosingClosure != null && temporaryTypesForExpression == null) {
                type = getTypeFromClosureArguments(parameter, enclosingClosure);
            }
            if (type != null) {
                storeType(vexp, type);
                return type;
            }
            return getType((Parameter) variable);
        }
        return vexp.getOriginType();
    }
    if (exp instanceof ListExpression) {
        return inferListExpressionType((ListExpression) exp);
    }
    if (exp instanceof MapExpression) {
        return inferMapExpressionType((MapExpression) exp);
    }
    if (exp instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) exp).getType();
    }
    if (exp instanceof MethodNode) {
        if ((exp == GET_DELEGATE || exp == GET_OWNER || exp == GET_THISOBJECT) && typeCheckingContext.getEnclosingClosure() != null) {
            return typeCheckingContext.getEnclosingClassNode();
        }
        ClassNode ret = getInferredReturnType(exp);
        return ret != null ? ret : ((MethodNode) exp).getReturnType();
    }
    if (exp instanceof FieldNode || exp instanceof PropertyNode) {
        return ((Variable) exp).getOriginType();
    }
    if (exp instanceof RangeExpression) {
        ClassNode plain = RANGE_TYPE.getPlainNodeReference();
        RangeExpression re = (RangeExpression) exp;
        ClassNode fromType = getType(re.getFrom());
        ClassNode toType = getType(re.getTo());
        if (fromType.equals(toType)) {
            plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(fromType)) });
        } else {
            plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(lowestUpperBound(fromType, toType))) });
        }
        return plain;
    }
    if (exp instanceof UnaryPlusExpression) {
        return getType(((UnaryPlusExpression) exp).getExpression());
    }
    if (exp instanceof UnaryMinusExpression) {
        return getType(((UnaryMinusExpression) exp).getExpression());
    }
    if (exp instanceof BitwiseNegationExpression) {
        return getType(((BitwiseNegationExpression) exp).getExpression());
    }
    if (exp instanceof Parameter) {
        return ((Parameter) exp).getOriginType();
    }
    if (exp instanceof ClosureExpression) {
        ClassNode type = CLOSURE_TYPE.getPlainNodeReference();
        ClassNode returnType = getInferredReturnType(exp);
        if (returnType != null) {
            type.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(returnType)) });
        }
        Parameter[] parameters = ((ClosureExpression) exp).getParameters();
        int nParameters = parameters == null ? 0 : parameters.length == 0 ? -1 : parameters.length;
        type.putNodeMetaData(CLOSURE_ARGUMENTS, nParameters);
        return type;
    } else if (exp instanceof MethodCall) {
        MethodNode target = exp.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
        if (target != null) {
            return getType(target);
        }
    }
    return ((Expression) exp).getType();
}
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) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) Variable(org.codehaus.groovy.ast.Variable) StaticTypeCheckingSupport.findTargetVariable(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findTargetVariable) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MethodCall(org.codehaus.groovy.ast.expr.MethodCall) ClosureSignatureHint(groovy.transform.stc.ClosureSignatureHint) MethodNode(org.codehaus.groovy.ast.MethodNode) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticTypeCheckingSupport.evaluateExpression(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.evaluateExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) GenericsType(org.codehaus.groovy.ast.GenericsType) StaticTypeCheckingSupport.getCombinedGenericsType(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCombinedGenericsType) ClosureUtils.hasImplicitParameter(org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter) Parameter(org.codehaus.groovy.ast.Parameter) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 4 with RangeExpression

use of org.codehaus.groovy.ast.expr.RangeExpression in project gradle by gradle.

the class ExpressionReplacingVisitorSupport method visitRangeExpression.

@Override
public void visitRangeExpression(RangeExpression expr) {
    RangeExpression result = new RangeExpression(replaceExpr(expr.getFrom()), replaceExpr(expr.getTo()), expr.isInclusive());
    result.setType(expr.getType());
    result.setSourcePosition(expr);
    replaceVisitedExpressionWith(result);
}
Also used : RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression)

Example 5 with RangeExpression

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

the class RangeExpressionTransformer method transformRangeExpression.

public Expression transformRangeExpression(final RangeExpression range) {
    if (INTRANGE_TYPE.equals(range.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE))) {
        Expression inclLeft = constX(!range.isExclusiveLeft(), true), inclRight = constX(!range.isExclusiveRight(), true);
        Expression cce = ctorX(INTRANGE_TYPE, args(inclLeft, inclRight, range.getFrom(), range.getTo()));
        cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, INTRANGE_CTOR);
        cce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, INTRANGE_TYPE);
        cce.setSourcePosition(range);
        return transformer.transform(cce);
    }
    return transformer.superTransform(range);
}
Also used : RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) Expression(org.codehaus.groovy.ast.expr.Expression)

Aggregations

RangeExpression (org.codehaus.groovy.ast.expr.RangeExpression)5 Expression (org.codehaus.groovy.ast.expr.Expression)4 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)3 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)3 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)3 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)3 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)3 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)3 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)3 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)3 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)3 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)3 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)3 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)2 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)2 AttributeExpression (org.codehaus.groovy.ast.expr.AttributeExpression)2 BitwiseNegationExpression (org.codehaus.groovy.ast.expr.BitwiseNegationExpression)2 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)2 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)2 ClosureListExpression (org.codehaus.groovy.ast.expr.ClosureListExpression)2