use of org.codehaus.groovy.ast.expr.EmptyExpression in project groovy by apache.
the class LazyASTTransformation method getInitExpr.
private static Expression getInitExpr(ErrorCollecting xform, FieldNode fieldNode) {
Expression initExpr = fieldNode.getInitialValueExpression();
fieldNode.setInitialValueExpression(null);
if (initExpr == null || initExpr instanceof EmptyExpression) {
if (fieldNode.getType().isAbstract()) {
xform.addError("You cannot lazily initialize '" + fieldNode.getName() + "' from the abstract class '" + fieldNode.getType().getName() + "'", fieldNode);
}
initExpr = ctorX(fieldNode.getType());
}
return initExpr;
}
use of org.codehaus.groovy.ast.expr.EmptyExpression in project groovy by apache.
the class MarkupBuilderCodeTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression bin) {
Expression left = bin.getLeftExpression();
Expression right = bin.getRightExpression();
boolean assignment = bin.getOperation().getType() == Types.ASSIGN;
if (assignment && left instanceof VariableExpression) {
VariableExpression var = (VariableExpression) left;
if (var.getAccessedVariable() instanceof DynamicVariable) {
String varName = var.getName();
if (!"modelTypes".equals(varName)) {
MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
callGetModel.setImplicitThis(true);
callGetModel.setSourcePosition(left);
MethodCallExpression mce = new MethodCallExpression(callGetModel, "put", new ArgumentListExpression(new ConstantExpression(varName), right));
mce.setSourcePosition(left);
mce.setImplicitThis(false);
return transform(mce);
}
}
}
if (assignment && left instanceof VariableExpression && right instanceof ClosureExpression) {
VariableExpression var = (VariableExpression) left;
if ("modelTypes".equals(var.getName())) {
// template declaring its expected types from model directly
// modelTypes = {
// List<String> items
// ...
// }
Map<String, ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression) right);
Expression result = new EmptyExpression();
result.setSourcePosition(bin);
classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
return result;
}
}
return super.transform(bin);
}
use of org.codehaus.groovy.ast.expr.EmptyExpression in project groovy by apache.
the class BinaryExpressionHelper method evaluateEqual.
public void evaluateEqual(BinaryExpression expression, boolean defineVariable) {
AsmClassGenerator acg = controller.getAcg();
CompileStack compileStack = controller.getCompileStack();
OperandStack operandStack = controller.getOperandStack();
Expression rightExpression = expression.getRightExpression();
Expression leftExpression = expression.getLeftExpression();
ClassNode lhsType = controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
if (defineVariable && rightExpression instanceof EmptyExpression && !(leftExpression instanceof TupleExpression)) {
VariableExpression ve = (VariableExpression) leftExpression;
BytecodeVariable var = compileStack.defineVariable(ve, controller.getTypeChooser().resolveType(ve, controller.getClassNode()), false);
operandStack.loadOrStoreVariable(var, false);
return;
}
// let's evaluate the RHS and store the result
ClassNode rhsType;
if (rightExpression instanceof ListExpression && lhsType.isArray()) {
ListExpression list = (ListExpression) rightExpression;
ArrayExpression array = new ArrayExpression(lhsType.getComponentType(), list.getExpressions());
array.setSourcePosition(list);
array.visit(acg);
} else if (rightExpression instanceof EmptyExpression) {
rhsType = leftExpression.getType();
loadInitValue(rhsType);
} else {
rightExpression.visit(acg);
}
rhsType = operandStack.getTopOperand();
boolean directAssignment = defineVariable && !(leftExpression instanceof TupleExpression);
int rhsValueId;
if (directAssignment) {
VariableExpression var = (VariableExpression) leftExpression;
if (var.isClosureSharedVariable() && ClassHelper.isPrimitiveType(rhsType)) {
// GROOVY-5570: if a closure shared variable is a primitive type, it must be boxed
rhsType = ClassHelper.getWrapper(rhsType);
operandStack.box();
}
// form as it is closure shared
if (var.isClosureSharedVariable() && ClassHelper.isPrimitiveType(var.getOriginType()) && isNull(rightExpression)) {
operandStack.doGroovyCast(var.getOriginType());
// these two are never reached in bytecode and only there
// to avoid verifyerrors and compiler infrastructure hazzle
operandStack.box();
operandStack.doGroovyCast(lhsType);
}
// normal type transformation
if (!ClassHelper.isPrimitiveType(lhsType) && isNull(rightExpression)) {
operandStack.replace(lhsType);
} else {
operandStack.doGroovyCast(lhsType);
}
rhsType = lhsType;
rhsValueId = compileStack.defineVariable(var, lhsType, true).getIndex();
} else {
rhsValueId = compileStack.defineTemporaryVariable("$rhs", rhsType, true);
}
//TODO: if rhs is VariableSlotLoader already, then skip crating a new one
BytecodeExpression rhsValueLoader = new VariableSlotLoader(rhsType, rhsValueId, operandStack);
// assignment for subscript
if (leftExpression instanceof BinaryExpression) {
BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
if (leftBinExpr.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
assignToArray(expression, leftBinExpr.getLeftExpression(), leftBinExpr.getRightExpression(), rhsValueLoader);
}
compileStack.removeVar(rhsValueId);
return;
}
compileStack.pushLHS(true);
// multiple declaration
if (leftExpression instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) leftExpression;
int i = 0;
for (Expression e : tuple.getExpressions()) {
VariableExpression var = (VariableExpression) e;
MethodCallExpression call = new MethodCallExpression(rhsValueLoader, "getAt", new ArgumentListExpression(new ConstantExpression(i)));
call.visit(acg);
i++;
if (defineVariable) {
operandStack.doGroovyCast(var);
compileStack.defineVariable(var, true);
operandStack.remove(1);
} else {
acg.visitVariableExpression(var);
}
}
} else // single declaration
if (defineVariable) {
rhsValueLoader.visit(acg);
operandStack.remove(1);
compileStack.popLHS();
return;
} else // normal assignment
{
int mark = operandStack.getStackLength();
// to leave a copy of the rightExpression value on the stack after the assignment.
rhsValueLoader.visit(acg);
TypeChooser typeChooser = controller.getTypeChooser();
ClassNode targetType = typeChooser.resolveType(leftExpression, controller.getClassNode());
operandStack.doGroovyCast(targetType);
leftExpression.visit(acg);
operandStack.remove(operandStack.getStackLength() - mark);
}
compileStack.popLHS();
// return value of assignment
rhsValueLoader.visit(acg);
compileStack.removeVar(rhsValueId);
}
use of org.codehaus.groovy.ast.expr.EmptyExpression in project groovy by apache.
the class StaticImportVisitor method findStaticPropertyAccessorByFullName.
private Expression findStaticPropertyAccessorByFullName(ClassNode staticImportType, String accessorMethodName) {
// anything will do as we only check size == 1
ArgumentListExpression dummyArgs = new ArgumentListExpression();
dummyArgs.addExpression(new EmptyExpression());
return findStaticMethod(staticImportType, accessorMethodName, (inLeftExpression ? dummyArgs : ArgumentListExpression.EMPTY_ARGUMENTS));
}
use of org.codehaus.groovy.ast.expr.EmptyExpression in project groovy by apache.
the class BaseScriptASTTransformation method changeBaseScriptTypeFromDeclaration.
private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
if (de.isMultipleAssignmentDeclaration()) {
addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
return;
}
if (!(de.getRightExpression() instanceof EmptyExpression)) {
addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
return;
}
Expression value = node.getMember("value");
if (value != null) {
addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
return;
}
ClassNode cNode = de.getDeclaringClass();
ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
de.setRightExpression(new VariableExpression("this"));
changeBaseScriptType(de, cNode, baseScriptType);
}
Aggregations