use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class UnsequencedExpressionRewriter method extractOrderedAccesses.
private void extractOrderedAccesses(List<Statement> stmtList, TreeNode subExpr, List<VariableAccess> toExtract) {
for (int i = 0; i < toExtract.size(); i++) {
VariableAccess access = toExtract.get(i);
TreeNode topConditional = getTopConditional(access.expression, subExpr);
if (topConditional != null) {
// Conditional expressions require special handling when extracting the
// access because execution of the access may not be guaranteed.
// Here we collect all accesses that are decendant of the conditional
// expression and pass them to an appropriate extraction method.
int j = i + 1;
for (; j < toExtract.size(); j++) {
if (getTopConditional(toExtract.get(j).expression, subExpr) != topConditional) {
break;
}
}
if (topConditional instanceof InfixExpression) {
extractInfixConditional(stmtList, (InfixExpression) topConditional, toExtract.subList(i, j));
} else if (topConditional instanceof ConditionalExpression) {
extractConditionalExpression(stmtList, (ConditionalExpression) topConditional, toExtract.subList(i, j));
} else {
throw new AssertionError("Unexpected conditional node type: " + topConditional.getClass().toString());
}
i = j - 1;
} else {
VariableElement newVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, access.expression.getTypeMirror(), currentMethod);
stmtList.add(new VariableDeclarationStatement(newVar, access.expression.copy()));
access.expression.replaceWith(new SimpleName(newVar));
}
}
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class UnsequencedExpressionRewriter method isWithinConditionalBranch.
private boolean isWithinConditionalBranch(TreeNode node, TreeNode limit) {
while (node != limit) {
TreeNode parent = node.getParent();
if (isConditional(parent) && getConditionChild(parent) != node) {
return true;
}
node = parent;
}
return false;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class ComplexExpressionExtractor method endVisit.
@Override
public void endVisit(PrefixExpression node) {
TreeNode parent = node.getParent();
if (parent == null) {
return;
}
// Check for balancing dereference and address-of operators.
if (parent instanceof PrefixExpression) {
PrefixExpression.Operator thisOp = node.getOperator();
PrefixExpression.Operator parentOp = ((PrefixExpression) parent).getOperator();
if ((thisOp == PrefixExpression.Operator.DEREFERENCE && parentOp == PrefixExpression.Operator.ADDRESS_OF) || (thisOp == PrefixExpression.Operator.ADDRESS_OF && parentOp == PrefixExpression.Operator.DEREFERENCE)) {
parent.replaceWith(TreeUtil.remove(node.getOperand()));
return;
}
}
// without checking if parentheses were necessary.
switch(parent.getKind()) {
case POSTFIX_EXPRESSION:
case // Parentheses not needed, but better for readability.
PREFIX_EXPRESSION:
ParenthesizedExpression.parenthesizeAndReplace(node);
break;
default:
}
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(EmptyStatement node) {
// Preserve line number difference with owner, to allow suppression of
// clang empty-statement warnings in Java source.
TreeNode parent = node.getParent();
if (parent.getKind() != Kind.SWITCH_STATEMENT && node.getLineNumber() != parent.getLineNumber()) {
buffer.newline();
buffer.printIndent();
}
buffer.append(";\n");
return false;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class TreeConverter method convertCompilationUnit.
public static CompilationUnit convertCompilationUnit(Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
String sourceFilePath = getPath(javacUnit.getSourceFile());
try {
TreeConverter converter = new TreeConverter(javacUnit, env);
JavaFileObject sourceFile = javacUnit.getSourceFile();
String source = sourceFile.getCharContent(false).toString();
String mainTypeName = FileUtil.getMainTypeName(sourceFile);
converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), sourceFilePath, mainTypeName, source);
PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
converter.newUnit.setPackage(converter.convertPackage(pkg, Trees.instance(env.task())));
for (JCTree type : javacUnit.getTypeDecls()) {
TreeNode newNode = converter.convert(type);
if (newNode.getKind() != TreeNode.Kind.EMPTY_STATEMENT) {
converter.newUnit.addType((AbstractTypeDeclaration) newNode);
}
}
addOcniComments(converter.newUnit, options.jsniWarnings());
return converter.newUnit;
} catch (Throwable e) {
ErrorUtil.fatalError(e, sourceFilePath);
return null;
}
}
Aggregations