use of javax.lang.model.element.VariableElement in project j2objc by google.
the class DeadCodeEliminator method removeDeadFields.
/**
* Deletes non-constant dead fields from a type's body declarations list.
*/
private void removeDeadFields(String clazz, List<BodyDeclaration> declarations) {
Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
while (declarationsIter.hasNext()) {
BodyDeclaration declaration = declarationsIter.next();
if (declaration instanceof FieldDeclaration) {
FieldDeclaration field = (FieldDeclaration) declaration;
Iterator<VariableDeclarationFragment> fragmentsIter = field.getFragments().iterator();
while (fragmentsIter.hasNext()) {
VariableDeclarationFragment fragment = fragmentsIter.next();
// Don't delete any constants because we can't detect their use.
VariableElement var = fragment.getVariableElement();
if (var.getConstantValue() == null && deadCodeMap.containsField(clazz, ElementUtil.getName(var))) {
fragmentsIter.remove();
}
}
if (field.getFragments().isEmpty()) {
declarationsIter.remove();
}
}
}
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class UnsequencedExpressionRewriter method extractInfixConditional.
private void extractInfixConditional(List<Statement> stmtList, InfixExpression conditional, List<VariableAccess> toExtract) {
InfixExpression.Operator op = conditional.getOperator();
List<Expression> branches = conditional.getOperands();
int lastIfExtractIdx = 0;
VariableElement conditionalVar = null;
int lastExtracted = 0;
Expression lastBranch = null;
for (int i = 0; i < toExtract.size(); i++) {
VariableAccess access = toExtract.get(i);
TreeNode node = access.expression;
while (node.getParent() != conditional) {
node = node.getParent();
}
assert node instanceof Expression;
Expression branch = (Expression) node;
// Extract all accesses from the previous branch.
if (lastBranch != null && branch != lastBranch) {
extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, i));
lastExtracted = i;
}
lastBranch = branch;
// If there's a new access in a new branch, then we extract an if-statement.
if (branch != branches.get(lastIfExtractIdx)) {
TypeMirror boolType = typeUtil.getBoolean();
if (conditionalVar == null) {
conditionalVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, boolType, currentMethod);
conditional.replaceWith(new SimpleName(conditionalVar));
stmtList.add(new VariableDeclarationStatement(conditionalVar, null));
}
List<Expression> subBranches = branches.subList(lastIfExtractIdx, branches.indexOf(branch));
IfStatement newIf = new IfStatement();
Expression ifExpr = new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(subBranches, op));
if (op == InfixExpression.Operator.CONDITIONAL_OR) {
ifExpr = new PrefixExpression(boolType, PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(ifExpr));
}
newIf.setExpression(ifExpr);
stmtList.add(newIf);
Block thenBlock = new Block();
stmtList = thenBlock.getStatements();
newIf.setThenStatement(thenBlock);
lastIfExtractIdx = branches.indexOf(branch);
}
}
extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, toExtract.size()));
if (conditionalVar != null) {
List<Expression> remainingBranches = Lists.newArrayList();
remainingBranches.add(new SimpleName(conditionalVar));
remainingBranches.addAll(branches.subList(lastIfExtractIdx, branches.size()));
stmtList.add(new ExpressionStatement(new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(remainingBranches, op))));
}
}
use of javax.lang.model.element.VariableElement 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 javax.lang.model.element.VariableElement in project j2objc by google.
the class NilCheckResolver method addNilCheck.
private void addNilCheck(Expression node) {
if (!needsNilCheck(node)) {
return;
}
VariableElement var = TreeUtil.getVariableElement(node);
if (var != null) {
addSafeVar(var);
}
TypeMirror idType = TypeUtil.ID_TYPE;
FunctionElement element = new FunctionElement("nil_chk", idType, null).addParameters(idType);
FunctionInvocation nilChkInvocation = new FunctionInvocation(element, node.getTypeMirror());
node.replaceWith(nilChkInvocation);
nilChkInvocation.addArgument(node);
}
use of javax.lang.model.element.VariableElement 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;
}
Aggregations