use of com.google.devtools.j2objc.ast.InfixExpression 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.InfixExpression 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.InfixExpression in project j2objc by google.
the class OperatorRewriter method rewriteStringConcatenation.
private void rewriteStringConcatenation(InfixExpression node) {
List<Expression> childOperands = node.getOperands();
List<Expression> operands = Lists.newArrayListWithCapacity(childOperands.size());
TreeUtil.moveList(childOperands, operands);
operands = coalesceStringLiterals(operands);
if (operands.size() == 1 && typeUtil.isString(operands.get(0).getTypeMirror())) {
node.replaceWith(operands.get(0));
return;
}
TypeMirror stringType = typeUtil.getJavaString().asType();
FunctionElement element = new FunctionElement("JreStrcat", stringType, null).addParameters(TypeUtil.NATIVE_CHAR_PTR).setIsVarargs(true);
FunctionInvocation invocation = new FunctionInvocation(element, stringType);
List<Expression> args = invocation.getArguments();
args.add(getStrcatTypesCString(operands));
args.addAll(operands);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.InfixExpression in project j2objc by google.
the class OperatorRewriter method endVisit.
@Override
public void endVisit(InfixExpression node) {
InfixExpression.Operator op = node.getOperator();
TypeMirror nodeType = node.getTypeMirror();
String funcName = getInfixFunction(op, nodeType);
if (funcName != null) {
Iterator<Expression> operandIter = node.getOperands().iterator();
Expression leftOperand = operandIter.next();
operandIter.remove();
// translated here are all left-associative.
while (operandIter.hasNext()) {
Expression rightOperand = operandIter.next();
operandIter.remove();
FunctionElement element = new FunctionElement(funcName, nodeType, null).addParameters(leftOperand.getTypeMirror(), rightOperand.getTypeMirror());
FunctionInvocation invocation = new FunctionInvocation(element, nodeType);
List<Expression> args = invocation.getArguments();
args.add(leftOperand);
args.add(rightOperand);
leftOperand = invocation;
}
node.replaceWith(leftOperand);
} else if (op == InfixExpression.Operator.PLUS && typeUtil.isString(nodeType) && !isStringAppend(node.getParent())) {
rewriteStringConcatenation(node);
}
}
use of com.google.devtools.j2objc.ast.InfixExpression in project j2objc by google.
the class Rewriter method rewriteStringConcat.
private void rewriteStringConcat(InfixExpression node) {
// Collect all non-string operands that precede the first string operand.
// If there are multiple such operands, move them into a sub-expression.
List<Expression> nonStringOperands = new ArrayList<>();
TypeMirror nonStringExprType = null;
for (Expression operand : node.getOperands()) {
TypeMirror operandType = operand.getTypeMirror();
if (typeUtil.isString(operandType)) {
break;
}
nonStringOperands.add(operand);
nonStringExprType = getAdditionType(nonStringExprType, operandType);
}
if (nonStringOperands.size() < 2) {
return;
}
InfixExpression nonStringExpr = new InfixExpression(nonStringExprType, InfixExpression.Operator.PLUS);
for (Expression operand : nonStringOperands) {
nonStringExpr.addOperand(TreeUtil.remove(operand));
}
node.addOperand(0, nonStringExpr);
}
Aggregations