use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class StatementWriter method writeForLoopWithClosureList.
protected void writeForLoopWithClosureList(ForStatement loop) {
controller.getAcg().onLineNumber(loop, "visitForLoop");
writeStatementLabel(loop);
MethodVisitor mv = controller.getMethodVisitor();
controller.getCompileStack().pushLoop(loop.getVariableScope(), loop.getStatementLabels());
ClosureListExpression clExpr = (ClosureListExpression) loop.getCollectionExpression();
controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());
List expressions = clExpr.getExpressions();
int size = expressions.size();
// middle element is condition, lower half is init, higher half is increment
int condIndex = (size - 1) / 2;
// visit init
for (int i = 0; i < condIndex; i++) {
visitExpressionOrStatement(expressions.get(i));
}
Label continueLabel = controller.getCompileStack().getContinueLabel();
Label breakLabel = controller.getCompileStack().getBreakLabel();
Label cond = new Label();
mv.visitLabel(cond);
// visit condition leave boolean on stack
{
Expression condExpr = (Expression) expressions.get(condIndex);
int mark = controller.getOperandStack().getStackLength();
condExpr.visit(controller.getAcg());
controller.getOperandStack().castToBool(mark, true);
}
// jump if we don't want to continue
// note: ifeq tests for ==0, a boolean is 0 if it is false
controller.getOperandStack().jump(IFEQ, breakLabel);
// Generate the loop body
loop.getLoopBlock().visit(controller.getAcg());
// visit increment
mv.visitLabel(continueLabel);
for (int i = condIndex + 1; i < size; i++) {
visitExpressionOrStatement(expressions.get(i));
}
// jump to test the condition again
mv.visitJumpInsn(GOTO, cond);
// loop end
mv.visitLabel(breakLabel);
controller.getCompileStack().pop();
controller.getCompileStack().pop();
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class StatementWriter method writeReturn.
public void writeReturn(ReturnStatement statement) {
controller.getAcg().onLineNumber(statement, "visitReturnStatement");
writeStatementLabel(statement);
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
ClassNode returnType = controller.getReturnType();
if (returnType == ClassHelper.VOID_TYPE) {
if (!(statement.isReturningNullOrVoid())) {
//TODO: move to Verifier
controller.getAcg().throwException("Cannot use return statement with an expression on a method that returns void");
}
controller.getCompileStack().applyBlockRecorder();
mv.visitInsn(RETURN);
return;
}
Expression expression = statement.getExpression();
expression.visit(controller.getAcg());
operandStack.doGroovyCast(returnType);
if (controller.getCompileStack().hasBlockRecorder()) {
ClassNode type = operandStack.getTopOperand();
int returnValueIdx = controller.getCompileStack().defineTemporaryVariable("returnValue", returnType, true);
controller.getCompileStack().applyBlockRecorder();
operandStack.load(type, returnValueIdx);
}
BytecodeHelper.doReturn(mv, returnType);
operandStack.remove(1);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class StatementWriter method writeWhileLoop.
public void writeWhileLoop(WhileStatement loop) {
controller.getAcg().onLineNumber(loop, "visitWhileLoop");
writeStatementLabel(loop);
MethodVisitor mv = controller.getMethodVisitor();
controller.getCompileStack().pushLoop(loop.getStatementLabels());
Label continueLabel = controller.getCompileStack().getContinueLabel();
Label breakLabel = controller.getCompileStack().getBreakLabel();
mv.visitLabel(continueLabel);
Expression bool = loop.getBooleanExpression();
boolean boolHandled = false;
if (bool instanceof ConstantExpression) {
ConstantExpression constant = (ConstantExpression) bool;
if (constant.getValue() == Boolean.TRUE) {
boolHandled = true;
// do nothing
} else if (constant.getValue() == Boolean.FALSE) {
boolHandled = true;
mv.visitJumpInsn(GOTO, breakLabel);
}
}
if (!boolHandled) {
bool.visit(controller.getAcg());
controller.getOperandStack().jump(IFEQ, breakLabel);
}
loop.getLoopBlock().visit(controller.getAcg());
mv.visitJumpInsn(GOTO, continueLabel);
mv.visitLabel(breakLabel);
controller.getCompileStack().pop();
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class StaticTypesStatementWriter method writeForInLoop.
@Override
protected void writeForInLoop(final ForStatement loop) {
controller.getAcg().onLineNumber(loop, "visitForLoop");
writeStatementLabel(loop);
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
// Identify type of collection
TypeChooser typeChooser = controller.getTypeChooser();
Expression collectionExpression = loop.getCollectionExpression();
ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
Parameter loopVariable = loop.getVariable();
int size = operandStack.getStackLength();
if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
} else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
} else {
writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
}
operandStack.popDownTo(size);
compileStack.pop();
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class AnnotationConstantsVisitor method transformConstantExpression.
private 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;
}
Aggregations