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);
}
}
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();
}
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);
}
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);
}
Aggregations