use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class Verifier method addFieldInitialization.
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
Expression expression = fieldNode.getInitialExpression();
if (expression != null) {
final FieldExpression fe = new FieldExpression(fieldNode);
if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
fe.setUseReferenceDirectly(true);
}
ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
if (fieldNode.isStatic()) {
// GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
// initialized first so that code dependent on it does not see their values as empty
Expression initialValueExpression = fieldNode.getInitialValueExpression();
if (initialValueExpression instanceof ConstantExpression) {
ConstantExpression cexp = (ConstantExpression) initialValueExpression;
cexp = transformToPrimitiveConstantIfPossible(cexp);
if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
// GROOVY-5150: primitive type constants will be initialized directly
return;
}
staticList.add(0, statement);
} else {
staticList.add(statement);
}
// to avoid double initialization in case of several constructors
fieldNode.setInitialValueExpression(null);
/*
* If it is a statement for an explicitly declared static field inside an enum, store its
* reference. For enums, they need to be handled differently as such init statements should
* come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
*/
if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
initStmtsAfterEnumValuesInit.add(statement);
}
} else {
list.add(statement);
}
}
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method evaluateBinaryExpressionWithAssignment.
protected void evaluateBinaryExpressionWithAssignment(String method, BinaryExpression expression) {
Expression leftExpression = expression.getLeftExpression();
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
if (leftExpression instanceof BinaryExpression) {
BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
if (leftBinExpr.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
evaluateArrayAssignmentWithOperator(method, expression, leftBinExpr);
return;
}
}
evaluateBinaryExpression(method, expression);
// br to leave a copy of rvalue on the stack. see also isPopRequired()
operandStack.dup();
controller.getCompileStack().pushLHS(true);
leftExpression.visit(acg);
controller.getCompileStack().popLHS();
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method evaluateBinaryExpression.
protected void evaluateBinaryExpression(String message, BinaryExpression binExp) {
CompileStack compileStack = controller.getCompileStack();
Expression receiver = binExp.getLeftExpression();
Expression arguments = binExp.getRightExpression();
// ensure VariableArguments are read, not stored
compileStack.pushLHS(false);
controller.getInvocationWriter().makeSingleArgumentCall(receiver, message, arguments);
compileStack.popLHS();
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method evaluateCompareTo.
private void evaluateCompareTo(BinaryExpression expression) {
Expression leftExpression = expression.getLeftExpression();
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
leftExpression.visit(acg);
operandStack.box();
// if the right hand side is a boolean expression, we need to autobox
Expression rightExpression = expression.getRightExpression();
rightExpression.visit(acg);
operandStack.box();
compareToMethod.call(controller.getMethodVisitor());
operandStack.replace(ClassHelper.Integer_TYPE, 2);
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method execMethodAndStoreForSubscriptOperator.
private void execMethodAndStoreForSubscriptOperator(int op, String method, Expression expression, VariableSlotLoader usesSubscript, Expression orig) {
final OperandStack operandStack = controller.getOperandStack();
writePostOrPrefixMethod(op, method, expression, orig);
// we need special code for arrays to store the result (like for a[1]++)
if (usesSubscript != null) {
CompileStack compileStack = controller.getCompileStack();
BinaryExpression be = (BinaryExpression) expression;
ClassNode methodResultType = operandStack.getTopOperand();
final int resultIdx = compileStack.defineTemporaryVariable("postfix_" + method, methodResultType, true);
BytecodeExpression methodResultLoader = new VariableSlotLoader(methodResultType, resultIdx, operandStack);
// execute the assignment, this will leave the right side
// (here the method call result) on the stack
assignToArray(be, be.getLeftExpression(), usesSubscript, methodResultLoader);
compileStack.removeVar(resultIdx);
} else // here we handle a.b++ and a++
if (expression instanceof VariableExpression || expression instanceof FieldExpression || expression instanceof PropertyExpression) {
operandStack.dup();
controller.getCompileStack().pushLHS(true);
expression.visit(controller.getAcg());
controller.getCompileStack().popLHS();
}
// other cases don't need storing, so nothing to be done for them
}
Aggregations