use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class NilCheckResolver method visit.
@Override
public boolean visit(InfixExpression node) {
InfixExpression.Operator op = node.getOperator();
boolean logicalAnd = op == InfixExpression.Operator.CONDITIONAL_AND;
boolean logicalOr = op == InfixExpression.Operator.CONDITIONAL_OR;
if (logicalAnd || logicalOr) {
return handleConditionalOperator(node, logicalAnd);
}
boolean equals = op == InfixExpression.Operator.EQUALS;
boolean notEquals = op == InfixExpression.Operator.NOT_EQUALS;
if (equals || notEquals) {
Expression lhs = node.getOperand(0);
Expression rhs = node.getOperand(1);
VariableElement maybeNullVar = null;
if (lhs instanceof NullLiteral) {
maybeNullVar = TreeUtil.getVariableElement(rhs);
} else if (rhs instanceof NullLiteral) {
maybeNullVar = TreeUtil.getVariableElement(lhs);
}
if (maybeNullVar != null) {
if (equals) {
setConditionalSafeVars(node, EMPTY_VARS, Collections.singleton(maybeNullVar));
} else {
setConditionalSafeVars(node, Collections.singleton(maybeNullVar), EMPTY_VARS);
}
}
}
return true;
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OperatorRewriter method getStringAppendOperands.
private List<Expression> getStringAppendOperands(Assignment node) {
Expression rhs = node.getRightHandSide();
if (rhs instanceof InfixExpression && typeUtil.isString(rhs.getTypeMirror())) {
InfixExpression infixExpr = (InfixExpression) rhs;
if (infixExpr.getOperator() == InfixExpression.Operator.PLUS) {
List<Expression> operands = infixExpr.getOperands();
List<Expression> result = Lists.newArrayListWithCapacity(operands.size());
TreeUtil.moveList(operands, result);
return coalesceStringLiterals(result);
}
}
return Collections.singletonList(TreeUtil.remove(rhs));
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OperatorRewriter method getAssignmentFunctionName.
private String getAssignmentFunctionName(Assignment node, VariableElement var, boolean isRetainedWith) {
if (!ElementUtil.isField(var)) {
return null;
}
TypeMirror type = var.asType();
boolean isPrimitive = type.getKind().isPrimitive();
boolean isStrong = !isPrimitive && !ElementUtil.isWeakReference(var);
boolean isVolatile = ElementUtil.isVolatile(var);
if (isRetainedWith) {
return isVolatile ? "JreVolatileRetainedWithAssign" : "JreRetainedWithAssign";
}
if (isVolatile) {
// same field.
return isStrong ? "JreVolatileStrongAssign" : "JreAssignVolatile" + (isPrimitive ? NameTable.capitalize(TypeUtil.getName(type)) : "Id");
}
if (isStrong && options.useReferenceCounting()) {
String funcName = "JreStrongAssign";
Expression retainedRhs = TranslationUtil.retainResult(node.getRightHandSide());
if (retainedRhs != null) {
funcName += "AndConsume";
node.setRightHandSide(retainedRhs);
}
return funcName;
}
return null;
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OperatorRewriter method getRetainedWithTarget.
// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
Expression lhs = node.getLeftHandSide();
if (!(lhs instanceof FieldAccess)) {
return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
}
// To avoid duplicating the target expression we must save the result to a local variable.
FieldAccess fieldAccess = (FieldAccess) lhs;
Expression target = fieldAccess.getExpression();
VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
fieldAccess.setExpression(new SimpleName(targetVar));
CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
node.replaceWith(commaExpr);
commaExpr.addExpression(node);
return new SimpleName(targetVar);
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OperatorRewriter method shouldRewriteCompoundAssign.
private static boolean shouldRewriteCompoundAssign(Assignment node) {
Expression lhs = node.getLeftHandSide();
TypeMirror lhsType = lhs.getTypeMirror();
TypeMirror rhsType = node.getRightHandSide().getTypeMirror();
switch(node.getOperator()) {
case LEFT_SHIFT_ASSIGN:
case RIGHT_SHIFT_SIGNED_ASSIGN:
case RIGHT_SHIFT_UNSIGNED_ASSIGN:
return true;
case PLUS_ASSIGN:
case MINUS_ASSIGN:
case TIMES_ASSIGN:
case DIVIDE_ASSIGN:
case REMAINDER_ASSIGN:
return isVolatile(lhs) || TypeUtil.isFloatingPoint(lhsType) || TypeUtil.isFloatingPoint(rhsType);
default:
return isVolatile(lhs);
}
}
Aggregations