use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class BinaryExpressionTransformer method transformBinaryExpression.
Expression transformBinaryExpression(final BinaryExpression bin) {
if (bin instanceof DeclarationExpression) {
Expression optimized = transformDeclarationExpression(bin);
if (optimized != null) {
return optimized;
}
}
Object[] list = bin.getNodeMetaData(BINARY_EXP_TARGET);
Token operation = bin.getOperation();
int operationType = operation.getType();
Expression rightExpression = bin.getRightExpression();
Expression leftExpression = bin.getLeftExpression();
if (bin instanceof DeclarationExpression && leftExpression instanceof VariableExpression) {
ClassNode declarationType = ((VariableExpression) leftExpression).getOriginType();
if (rightExpression instanceof ConstantExpression) {
ClassNode unwrapper = ClassHelper.getUnwrapper(declarationType);
ClassNode wrapper = ClassHelper.getWrapper(declarationType);
if (!rightExpression.getType().equals(declarationType) && wrapper.isDerivedFrom(ClassHelper.Number_TYPE) && WideningCategories.isDoubleCategory(unwrapper)) {
ConstantExpression constant = (ConstantExpression) rightExpression;
if (constant.getValue() != null) {
return optimizeConstantInitialization(bin, operation, constant, leftExpression, declarationType);
}
}
}
}
if (operationType == Types.EQUAL && leftExpression instanceof PropertyExpression) {
MethodNode directMCT = leftExpression.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (directMCT != null) {
return transformPropertyAssignmentToSetterCall((PropertyExpression) leftExpression, rightExpression, directMCT);
}
}
if (operationType == Types.COMPARE_EQUAL || operationType == Types.COMPARE_NOT_EQUAL) {
// let's check if one of the operands is the null constant
CompareToNullExpression compareToNullExpression = null;
if (isNullConstant(leftExpression)) {
compareToNullExpression = new CompareToNullExpression(staticCompilationTransformer.transform(rightExpression), operationType == Types.COMPARE_EQUAL);
} else if (isNullConstant(rightExpression)) {
compareToNullExpression = new CompareToNullExpression(staticCompilationTransformer.transform(leftExpression), operationType == Types.COMPARE_EQUAL);
}
if (compareToNullExpression != null) {
compareToNullExpression.setSourcePosition(bin);
return compareToNullExpression;
}
} else if (operationType == Types.KEYWORD_IN) {
return convertInOperatorToTernary(bin, rightExpression, leftExpression);
}
if (list != null) {
if (operationType == Types.COMPARE_TO) {
StaticTypesTypeChooser typeChooser = staticCompilationTransformer.getTypeChooser();
ClassNode classNode = staticCompilationTransformer.getClassNode();
ClassNode leftType = typeChooser.resolveType(leftExpression, classNode);
if (leftType.implementsInterface(ClassHelper.COMPARABLE_TYPE)) {
ClassNode rightType = typeChooser.resolveType(rightExpression, classNode);
if (rightType.implementsInterface(ClassHelper.COMPARABLE_TYPE)) {
Expression left = staticCompilationTransformer.transform(leftExpression);
Expression right = staticCompilationTransformer.transform(rightExpression);
MethodCallExpression call = new MethodCallExpression(left, "compareTo", new ArgumentListExpression(right));
call.setImplicitThis(false);
call.setMethodTarget(COMPARE_TO_METHOD);
call.setSourcePosition(bin);
CompareIdentityExpression compareIdentity = new CompareIdentityExpression(left, right);
compareIdentity.putNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE, ClassHelper.boolean_TYPE);
TernaryExpression result = new TernaryExpression(// a==b
new BooleanExpression(compareIdentity), CONSTANT_ZERO, new TernaryExpression(// a==null
new BooleanExpression(new CompareToNullExpression(left, true)), CONSTANT_MINUS_ONE, new TernaryExpression(// b==null
new BooleanExpression(new CompareToNullExpression(right, true)), CONSTANT_ONE, call)));
compareIdentity.putNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE, ClassHelper.int_TYPE);
result.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
TernaryExpression expr = (TernaryExpression) result.getFalseExpression();
expr.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
expr.getFalseExpression().putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.int_TYPE);
return result;
}
}
}
boolean isAssignment = StaticTypeCheckingSupport.isAssignment(operationType);
MethodCallExpression call;
MethodNode node = (MethodNode) list[0];
String name = (String) list[1];
Expression left = staticCompilationTransformer.transform(leftExpression);
Expression right = staticCompilationTransformer.transform(rightExpression);
BinaryExpression optimized = tryOptimizeCharComparison(left, right, bin);
if (optimized != null) {
optimized.removeNodeMetaData(BINARY_EXP_TARGET);
return transformBinaryExpression(optimized);
}
call = new MethodCallExpression(left, name, new ArgumentListExpression(right));
call.setImplicitThis(false);
call.setMethodTarget(node);
MethodNode adapter = StaticCompilationTransformer.BYTECODE_BINARY_ADAPTERS.get(operationType);
if (adapter != null) {
ClassExpression sba = new ClassExpression(StaticCompilationTransformer.BYTECODE_ADAPTER_CLASS);
// replace with compareEquals
call = new MethodCallExpression(sba, "compareEquals", new ArgumentListExpression(left, right));
call.setMethodTarget(adapter);
call.setImplicitThis(false);
}
call.setSourcePosition(bin);
if (!isAssignment)
return call;
// the method represents the operation type only, and we must add an assignment
return new BinaryExpression(left, Token.newSymbol("=", operation.getStartLine(), operation.getStartColumn()), call);
}
if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof TupleExpression && rightExpression instanceof ListExpression) {
// multiple assignment
ListOfExpressionsExpression cle = new ListOfExpressionsExpression();
boolean isDeclaration = bin instanceof DeclarationExpression;
List<Expression> leftExpressions = ((TupleExpression) leftExpression).getExpressions();
List<Expression> rightExpressions = ((ListExpression) rightExpression).getExpressions();
Iterator<Expression> leftIt = leftExpressions.iterator();
Iterator<Expression> rightIt = rightExpressions.iterator();
if (isDeclaration) {
while (leftIt.hasNext()) {
Expression left = leftIt.next();
if (rightIt.hasNext()) {
Expression right = rightIt.next();
BinaryExpression bexp = new DeclarationExpression(left, bin.getOperation(), right);
bexp.setSourcePosition(right);
cle.addExpression(bexp);
}
}
} else {
// (next, result) = [ result, next+result ]
// -->
// def tmp1 = result
// def tmp2 = next+result
// next = tmp1
// result = tmp2
int size = rightExpressions.size();
List<Expression> tmpAssignments = new ArrayList<Expression>(size);
List<Expression> finalAssignments = new ArrayList<Expression>(size);
for (int i = 0; i < Math.min(size, leftExpressions.size()); i++) {
Expression left = leftIt.next();
Expression right = rightIt.next();
VariableExpression tmpVar = new VariableExpression("$tmpVar$" + tmpVarCounter++);
BinaryExpression bexp = new DeclarationExpression(tmpVar, bin.getOperation(), right);
bexp.setSourcePosition(right);
tmpAssignments.add(bexp);
bexp = new BinaryExpression(left, bin.getOperation(), new VariableExpression(tmpVar));
bexp.setSourcePosition(left);
finalAssignments.add(bexp);
}
for (Expression tmpAssignment : tmpAssignments) {
cle.addExpression(tmpAssignment);
}
for (Expression finalAssignment : finalAssignments) {
cle.addExpression(finalAssignment);
}
}
return staticCompilationTransformer.transform(cle);
}
return staticCompilationTransformer.superTransform(bin);
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticMethodCallExpressionTransformer method transformStaticMethodCallExpression.
Expression transformStaticMethodCallExpression(final StaticMethodCallExpression orig) {
MethodNode target = (MethodNode) orig.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (target != null) {
MethodCallExpression call = new MethodCallExpression(new ClassExpression(orig.getOwnerType()), orig.getMethod(), orig.getArguments());
call.setMethodTarget(target);
call.setSourcePosition(orig);
call.copyNodeMetaData(orig);
return transformer.transform(call);
}
return transformer.superTransform(orig);
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method transformBinaryExpression.
protected Expression transformBinaryExpression(BinaryExpression be) {
Expression left = transform(be.getLeftExpression());
int type = be.getOperation().getType();
if ((type == Types.ASSIGNMENT_OPERATOR || type == Types.EQUAL) && left instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) left;
String error = "you tried to assign a value to the class '" + ce.getType().getName() + "'";
if (ce.getType().isScript()) {
error += ". Do you have a script with this name?";
}
addError(error, be.getLeftExpression());
return be;
}
if (left instanceof ClassExpression && isLeftSquareBracket(type)) {
if (be.getRightExpression() instanceof ListExpression) {
ListExpression list = (ListExpression) be.getRightExpression();
if (list.getExpressions().isEmpty()) {
// we have C[] if the list is empty -> should be an array then!
final ClassExpression ce = new ClassExpression(left.getType().makeArray());
ce.setSourcePosition(be);
return ce;
} else {
// may be we have C[k1:v1, k2:v2] -> should become (C)([k1:v1, k2:v2])
boolean map = true;
for (Expression expression : list.getExpressions()) {
if (!(expression instanceof MapEntryExpression)) {
map = false;
break;
}
}
if (map) {
final MapExpression me = new MapExpression();
for (Expression expression : list.getExpressions()) {
me.addMapEntryExpression((MapEntryExpression) transform(expression));
}
me.setSourcePosition(list);
final CastExpression ce = new CastExpression(left.getType(), me);
ce.setSourcePosition(be);
return ce;
}
}
} else if (be.getRightExpression() instanceof SpreadMapExpression) {
// we have C[*:map] -> should become (C) map
SpreadMapExpression mapExpression = (SpreadMapExpression) be.getRightExpression();
Expression right = transform(mapExpression.getExpression());
Expression ce = new CastExpression(left.getType(), right);
ce.setSourcePosition(be);
return ce;
}
if (be.getRightExpression() instanceof MapEntryExpression) {
// may be we have C[k1:v1] -> should become (C)([k1:v1])
final MapExpression me = new MapExpression();
me.addMapEntryExpression((MapEntryExpression) transform(be.getRightExpression()));
me.setSourcePosition(be.getRightExpression());
final CastExpression ce = new CastExpression(left.getType(), me);
ce.setSourcePosition(be);
return ce;
}
}
Expression right = transform(be.getRightExpression());
be.setLeftExpression(left);
be.setRightExpression(right);
return be;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method transformDeclarationExpression.
protected Expression transformDeclarationExpression(DeclarationExpression de) {
visitAnnotations(de);
Expression oldLeft = de.getLeftExpression();
checkingVariableTypeInDeclaration = true;
Expression left = transform(oldLeft);
checkingVariableTypeInDeclaration = false;
if (left instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) left;
addError("you tried to assign a value to the class " + ce.getType().getName(), oldLeft);
return de;
}
Expression right = transform(de.getRightExpression());
if (right == de.getRightExpression()) {
fixDeclaringClass(de);
return de;
}
DeclarationExpression newDeclExpr = new DeclarationExpression(left, de.getOperation(), right);
newDeclExpr.setDeclaringClass(de.getDeclaringClass());
fixDeclaringClass(newDeclExpr);
newDeclExpr.setSourcePosition(de);
newDeclExpr.addAnnotations(de.getAnnotations());
return newDeclExpr;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method correctClassClassChain.
// iterate from the inner most to the outer and check for classes
// this check will ignore a .class property, for Example Integer.class will be
// a PropertyExpression with the ClassExpression of Integer as objectExpression
// and class as property
private Expression correctClassClassChain(PropertyExpression pe) {
LinkedList<Expression> stack = new LinkedList<Expression>();
ClassExpression found = null;
for (Expression it = pe; it != null; it = ((PropertyExpression) it).getObjectExpression()) {
if (it instanceof ClassExpression) {
found = (ClassExpression) it;
break;
} else if (!(it.getClass() == PropertyExpression.class)) {
return pe;
}
stack.addFirst(it);
}
if (found == null) {
return pe;
}
if (stack.isEmpty()) {
return pe;
}
Object stackElement = stack.removeFirst();
if (!(stackElement.getClass() == PropertyExpression.class)) {
return pe;
}
PropertyExpression classPropertyExpression = (PropertyExpression) stackElement;
String propertyNamePart = classPropertyExpression.getPropertyAsString();
if (propertyNamePart == null || !propertyNamePart.equals("class")) {
return pe;
}
found.setSourcePosition(classPropertyExpression);
if (stack.isEmpty()) {
return found;
}
stackElement = stack.removeFirst();
if (!(stackElement.getClass() == PropertyExpression.class)) {
return pe;
}
PropertyExpression classPropertyExpressionContainer = (PropertyExpression) stackElement;
classPropertyExpressionContainer.setObjectExpression(found);
return pe;
}
Aggregations