use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(ExpressionStatement node) {
Expression expression = node.getExpression();
TypeMirror type = expression.getTypeMirror();
if (!type.getKind().isPrimitive() && !type.getKind().equals(TypeKind.VOID) && options.useARC() && (expression instanceof MethodInvocation || expression instanceof SuperMethodInvocation || expression instanceof FunctionInvocation)) {
// Avoid clang warning that the return value is unused.
buffer.append("(void) ");
}
expression.accept(this);
buffer.append(";\n");
return false;
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(FunctionInvocation node) {
// If the function is actually a macro, then it's arguments may need to be wrapped in
// parentheses.
boolean isMacro = node.getFunctionElement().isMacro();
buffer.append(node.getName());
buffer.append('(');
for (Iterator<Expression> iter = node.getArguments().iterator(); iter.hasNext(); ) {
Expression arg = iter.next();
if (isMacro) {
acceptMacroArgument(arg);
} else {
arg.accept(this);
}
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(')');
return false;
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class SuperMethodInvocationRewriter method endVisit.
@Override
public void endVisit(SuperMethodInvocation node) {
Expression receiver = node.getReceiver();
ExecutableElement method = node.getExecutableElement();
TypeMirror exprType = node.getTypeMirror();
assert !ElementUtil.isDefault(method) : "Default methods are handled in Functionizer.java";
if (receiver == null) {
return;
}
VariableElement var = TreeUtil.getVariableElement(receiver);
assert var != null : "Expected receiver to be a variable";
TypeMirror receiverType = var.asType();
TypeElement receiverElem = TypeUtil.asTypeElement(receiverType);
SuperMethodElementPair superMethod = new SuperMethodElementPair(receiverElem, method);
superMethods.add(superMethod);
FunctionElement element = new FunctionElement(getSuperFunctionName(superMethod), exprType, receiverElem).addParameters(receiverType, TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
FunctionInvocation invocation = new FunctionInvocation(element, exprType);
List<Expression> args = invocation.getArguments();
args.add(TreeUtil.remove(receiver));
String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
args.add(new NativeExpression(selectorExpr, TypeUtil.ID_TYPE));
TreeUtil.copyList(node.getArguments(), args);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class UnsequencedExpressionRewriter method extractConditionalExpression.
private void extractConditionalExpression(List<Statement> stmtList, ConditionalExpression conditional, List<VariableAccess> toExtract) {
Expression condition = conditional.getExpression();
Expression thenExpr = conditional.getThenExpression();
Expression elseExpr = conditional.getElseExpression();
List<VariableAccess> conditionAccesses = Lists.newArrayList();
List<VariableAccess> thenAccesses = Lists.newArrayList();
List<VariableAccess> elseAccesses = Lists.newArrayList();
boolean needsExtraction = false;
for (VariableAccess access : toExtract) {
TreeNode node = access.expression;
while (node.getParent() != conditional) {
node = node.getParent();
}
if (node == condition) {
conditionAccesses.add(access);
} else if (node == thenExpr) {
thenAccesses.add(access);
} else if (node == elseExpr) {
elseAccesses.add(access);
} else {
throw new AssertionError();
}
if (node != condition) {
// We need to extract an if-statement if there is an access that
// executes conditionally.
needsExtraction = true;
}
}
extractOrderedAccesses(stmtList, condition, conditionAccesses);
// The recursive call might replace the condition child.
condition = conditional.getExpression();
if (needsExtraction) {
VariableElement resultVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, conditional.getTypeMirror(), currentMethod);
conditional.replaceWith(new SimpleName(resultVar));
stmtList.add(new VariableDeclarationStatement(resultVar, null));
IfStatement newIf = new IfStatement();
newIf.setExpression(condition.copy());
stmtList.add(newIf);
Block thenBlock = new Block();
newIf.setThenStatement(thenBlock);
List<Statement> thenStmts = thenBlock.getStatements();
extractOrderedAccesses(thenStmts, thenExpr, thenAccesses);
// The recursive call might replace the then expression child.
thenExpr = conditional.getThenExpression();
thenStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), thenExpr.copy())));
Block elseBlock = new Block();
newIf.setElseStatement(elseBlock);
List<Statement> elseStmts = elseBlock.getStatements();
extractOrderedAccesses(elseStmts, elseExpr, elseAccesses);
// The recursive call might replace the else expression child.
elseExpr = conditional.getElseExpression();
elseStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), elseExpr.copy())));
} else {
extractOrderedAccesses(stmtList, thenExpr, thenAccesses);
extractOrderedAccesses(stmtList, elseExpr, elseAccesses);
}
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class UnsequencedExpressionRewriter method extractVariableDeclarationFragments.
private void extractVariableDeclarationFragments(List<VariableDeclarationFragment> fragments, List<Statement> stmtList) {
for (int i = 0; i < fragments.size(); i++) {
VariableDeclarationFragment frag = fragments.get(i);
Expression init = frag.getInitializer();
if (init == null) {
continue;
}
newExpression(init);
init.accept(this);
List<VariableAccess> toExtract = getUnsequencedAccesses();
if (!toExtract.isEmpty()) {
if (i > 0) {
// Extract all fragments before the current one to preserve ordering.
VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0).copy());
for (int j = 1; j < i; j++) {
newDecl.addFragment(fragments.get(j).copy());
}
stmtList.add(newDecl);
fragments.subList(0, i).clear();
}
extractOrderedAccesses(stmtList, currentTopNode, toExtract);
i = 0;
}
}
}
Aggregations