Search in sources :

Example 1 with BitwiseNegationExpression

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

the class StaticTypesUnaryExpressionHelper method writeBitwiseNegate.

@Override
public void writeBitwiseNegate(final BitwiseNegationExpression expression) {
    expression.getExpression().visit(controller.getAcg());
    ClassNode top = controller.getOperandStack().getTopOperand();
    if (isPrimitiveInt(top) || isPrimitiveLong(top) || isPrimitiveShort(top) || isPrimitiveByte(top) || isPrimitiveChar(top)) {
        bytecodeX(mv -> {
            if (isPrimitiveLong(top)) {
                mv.visitLdcInsn(-1L);
                mv.visitInsn(LXOR);
            } else {
                mv.visitInsn(ICONST_M1);
                mv.visitInsn(IXOR);
                if (isPrimitiveByte(top)) {
                    mv.visitInsn(I2B);
                } else if (isPrimitiveChar(top)) {
                    mv.visitInsn(I2C);
                } else if (isPrimitiveShort(top)) {
                    mv.visitInsn(I2S);
                }
            }
        }).visit(controller.getAcg());
        controller.getOperandStack().remove(1);
    } else {
        super.writeBitwiseNegate(EMPTY_BITWISE_NEGATE);
    }
}
Also used : ClassHelper.isPrimitiveDouble(org.codehaus.groovy.ast.ClassHelper.isPrimitiveDouble) WriterController(org.codehaus.groovy.classgen.asm.WriterController) Label(org.objectweb.asm.Label) I2B(org.objectweb.asm.Opcodes.I2B) IFNE(org.objectweb.asm.Opcodes.IFNE) I2C(org.objectweb.asm.Opcodes.I2C) IXOR(org.objectweb.asm.Opcodes.IXOR) ClassHelper.isPrimitiveShort(org.codehaus.groovy.ast.ClassHelper.isPrimitiveShort) ClassHelper.boolean_TYPE(org.codehaus.groovy.ast.ClassHelper.boolean_TYPE) ClassHelper.isPrimitiveFloat(org.codehaus.groovy.ast.ClassHelper.isPrimitiveFloat) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) INEG(org.objectweb.asm.Opcodes.INEG) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassHelper.isPrimitiveInt(org.codehaus.groovy.ast.ClassHelper.isPrimitiveInt) ClassHelper.isPrimitiveLong(org.codehaus.groovy.ast.ClassHelper.isPrimitiveLong) ClassHelper.isPrimitiveBoolean(org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean) ICONST_M1(org.objectweb.asm.Opcodes.ICONST_M1) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) TypeChooser(org.codehaus.groovy.classgen.asm.TypeChooser) UnaryExpressionHelper(org.codehaus.groovy.classgen.asm.UnaryExpressionHelper) FNEG(org.objectweb.asm.Opcodes.FNEG) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) GeneralUtils.bytecodeX(org.codehaus.groovy.ast.tools.GeneralUtils.bytecodeX) LXOR(org.objectweb.asm.Opcodes.LXOR) ClassHelper.isPrimitiveChar(org.codehaus.groovy.ast.ClassHelper.isPrimitiveChar) ClassHelper.isPrimitiveByte(org.codehaus.groovy.ast.ClassHelper.isPrimitiveByte) LNEG(org.objectweb.asm.Opcodes.LNEG) GOTO(org.objectweb.asm.Opcodes.GOTO) I2S(org.objectweb.asm.Opcodes.I2S) DNEG(org.objectweb.asm.Opcodes.DNEG) ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 2 with BitwiseNegationExpression

use of org.codehaus.groovy.ast.expr.BitwiseNegationExpression 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 3 with BitwiseNegationExpression

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

the class ExpressionReplacingVisitorSupport method visitBitwiseNegationExpression.

@Override
public void visitBitwiseNegationExpression(BitwiseNegationExpression expr) {
    BitwiseNegationExpression result = new BitwiseNegationExpression(replaceExpr(expr.getExpression()));
    result.setType(expr.getType());
    result.setSourcePosition(expr);
    replaceVisitedExpressionWith(result);
}
Also used : BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression)

Example 4 with BitwiseNegationExpression

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

the class UnaryExpressionHelper method writeBitwiseNegate.

public void writeBitwiseNegate(BitwiseNegationExpression expression) {
    Expression subExpression = expression.getExpression();
    subExpression.visit(controller.getAcg());
    controller.getOperandStack().box();
    bitwiseNegate.call(controller.getMethodVisitor());
    controller.getOperandStack().replace(ClassHelper.OBJECT_TYPE);
    controller.getAssertionWriter().record(expression);
}
Also used : UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) Expression(org.codehaus.groovy.ast.expr.Expression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression)

Aggregations

BitwiseNegationExpression (org.codehaus.groovy.ast.expr.BitwiseNegationExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)3 NotExpression (org.codehaus.groovy.ast.expr.NotExpression)3 UnaryMinusExpression (org.codehaus.groovy.ast.expr.UnaryMinusExpression)3 UnaryPlusExpression (org.codehaus.groovy.ast.expr.UnaryPlusExpression)3 ClassNode (org.codehaus.groovy.ast.ClassNode)2 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)2 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)1 ClassHelper.boolean_TYPE (org.codehaus.groovy.ast.ClassHelper.boolean_TYPE)1 ClassHelper.isPrimitiveBoolean (org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean)1 ClassHelper.isPrimitiveByte (org.codehaus.groovy.ast.ClassHelper.isPrimitiveByte)1 ClassHelper.isPrimitiveChar (org.codehaus.groovy.ast.ClassHelper.isPrimitiveChar)1 ClassHelper.isPrimitiveDouble (org.codehaus.groovy.ast.ClassHelper.isPrimitiveDouble)1 ClassHelper.isPrimitiveFloat (org.codehaus.groovy.ast.ClassHelper.isPrimitiveFloat)1 ClassHelper.isPrimitiveInt (org.codehaus.groovy.ast.ClassHelper.isPrimitiveInt)1 ClassHelper.isPrimitiveLong (org.codehaus.groovy.ast.ClassHelper.isPrimitiveLong)1 ClassHelper.isPrimitiveShort (org.codehaus.groovy.ast.ClassHelper.isPrimitiveShort)1 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)1 FieldNode (org.codehaus.groovy.ast.FieldNode)1 GenericsType (org.codehaus.groovy.ast.GenericsType)1