use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OperatorRewriter method rewriteRegularAssignment.
private void rewriteRegularAssignment(Assignment node) {
VariableElement var = TreeUtil.getVariableElement(node.getLeftHandSide());
if (var == null) {
return;
}
handleRetainedLocal(var, node.getRightHandSide());
boolean isRetainedWith = ElementUtil.isRetainedWithField(var);
String funcName = getAssignmentFunctionName(node, var, isRetainedWith);
if (funcName == null) {
return;
}
TypeMirror type = node.getTypeMirror();
TypeMirror idType = TypeUtil.ID_TYPE;
TypeMirror declaredType = type.getKind().isPrimitive() ? type : idType;
Expression lhs = node.getLeftHandSide();
FunctionElement element = new FunctionElement(funcName, declaredType, null);
FunctionInvocation invocation = new FunctionInvocation(element, type);
List<Expression> args = invocation.getArguments();
if (isRetainedWith) {
element.addParameters(idType);
args.add(getRetainedWithTarget(node, var));
}
element.addParameters(TypeUtil.ID_PTR_TYPE, idType);
args.add(new PrefixExpression(new PointerType(lhs.getTypeMirror()), PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
args.add(TreeUtil.remove(node.getRightHandSide()));
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.Expression 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.Expression in project j2objc by google.
the class OperatorRewriter method rewriteStringAppend.
private void rewriteStringAppend(Assignment node) {
List<Expression> operands = getStringAppendOperands(node);
Expression lhs = node.getLeftHandSide();
TypeMirror lhsType = lhs.getTypeMirror();
String funcName = "JreStrAppend" + translationUtil.getOperatorFunctionModifier(lhs);
FunctionElement element = new FunctionElement(funcName, TypeUtil.ID_TYPE, null).addParameters(TypeUtil.ID_PTR_TYPE, TypeUtil.NATIVE_CHAR_PTR).setIsVarargs(true);
FunctionInvocation invocation = new FunctionInvocation(element, lhsType);
List<Expression> args = invocation.getArguments();
args.add(new PrefixExpression(new PointerType(lhsType), PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
args.add(getStrcatTypesCString(operands));
args.addAll(operands);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.Expression 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.Expression in project j2objc by google.
the class JavaToIOSMethodTranslator method visit.
@Override
public boolean visit(ClassInstanceCreation node) {
// translate any embedded method invocations
if (node.getExpression() != null) {
node.getExpression().accept(this);
}
for (Expression e : node.getArguments()) {
e.accept(this);
}
if (node.getAnonymousClassDeclaration() != null) {
node.getAnonymousClassDeclaration().accept(this);
}
ExecutableElement method = node.getExecutableElement();
String key = Mappings.getMethodKey(method, typeUtil);
String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
if (selector != null) {
assert !node.hasRetainedResult();
if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
// Special case: replace new String(constant) to constant (avoid clang warning).
Expression arg = node.getArgument(0);
if (arg instanceof StringLiteral) {
node.replaceWith(arg.copy());
return false;
}
}
ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
node.replaceWith(newInvocation);
}
return true;
}
Aggregations