use of com.google.devtools.j2objc.ast.PostfixExpression in project j2objc by google.
the class TreeConverter method convertPostfixExpression.
private static TreeNode convertPostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression node) {
PostfixExpression newNode = new PostfixExpression();
convertExpression(node, newNode);
return newNode.setOperator(PostfixExpression.Operator.parse(node.getOperator().toString())).setOperand((Expression) TreeConverter.convert(node.getOperand()));
}
use of com.google.devtools.j2objc.ast.PostfixExpression in project j2objc by google.
the class EnhancedForRewriter method handleArrayIteration.
private void handleArrayIteration(EnhancedForStatement node) {
Expression expression = node.getExpression();
ArrayType expressionType = (ArrayType) expression.getTypeMirror();
VariableElement loopVariable = node.getParameter().getVariableElement();
TypeMirror componentType = expressionType.getComponentType();
TypeElement iosArrayType = typeUtil.getIosArray(componentType);
TypeMirror bufferType = new PointerType(componentType);
VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
WhileStatement loop = new WhileStatement();
loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
loop.setBody(newLoopBody);
newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
Block block = new Block();
List<Statement> stmts = block.getStatements();
stmts.add(arrayDecl);
stmts.add(bufferDecl);
stmts.add(endDecl);
stmts.add(loop);
replaceLoop(node, block, loop);
}
use of com.google.devtools.j2objc.ast.PostfixExpression in project j2objc by google.
the class EnumRewriter method addSimpleNonArcInitialization.
private void addSimpleNonArcInitialization(EnumDeclaration node) {
List<EnumConstantDeclaration> constants = node.getEnumConstants();
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
stmts.add(new VariableDeclarationStatement(localEnum, null));
// Create a local array of enum names only if reflection is stripped but
// enum constants are not. For non-stripped classes, enum names are now
// retrieved from metadata, to avoid duplicates.
boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
if (useNamesArray) {
StringBuilder sb = new StringBuilder("id names[] = {\n ");
for (EnumConstantDeclaration constant : constants) {
sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
}
sb.append("\n};");
stmts.add(new NativeStatement(sb.toString()));
}
TypeMirror intType = typeUtil.getInt();
GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
Block loopBody = new Block();
stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
String enumClassName = nameTable.getFullName(node.getTypeElement());
loopBody.addStatement(new NativeStatement("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
if (useNamesArray) {
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
} else if (options.stripEnumConstants()) {
String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
loopBody.addStatement(new NativeStatement(statementText));
} else {
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), i), i);"));
}
}
use of com.google.devtools.j2objc.ast.PostfixExpression in project j2objc by google.
the class UnsequencedExpressionRewriter method isUnsequenced.
private boolean isUnsequenced(VariableAccess modification, Set<TreeNode> modificationAncestors, VariableAccess access) {
TreeNode commonAncestor = currentTopNode;
TreeNode node = access.expression;
while (node != currentTopNode) {
if (modificationAncestors.contains(node)) {
commonAncestor = node;
break;
}
node = node.getParent();
}
// contain the other access, then they are not unsequenced.
if (isConditional(commonAncestor) && (isWithinConditionalBranch(modification.expression, commonAncestor) || isWithinConditionalBranch(access.expression, commonAncestor))) {
return false;
} else if (commonAncestor instanceof CommaExpression) {
return false;
} else if (commonAncestor instanceof Assignment && modification.expression == commonAncestor) {
// "i = 1 + i++;" is unsequenced (according to clang).
return access.expression instanceof PrefixExpression || access.expression instanceof PostfixExpression;
}
return true;
}
use of com.google.devtools.j2objc.ast.PostfixExpression in project j2objc by google.
the class TranslationUtil method isAssigned.
public static boolean isAssigned(Expression node) {
TreeNode parent = node.getParent();
while (parent instanceof ParenthesizedExpression) {
node = (Expression) parent;
parent = node.getParent();
}
if (parent instanceof PostfixExpression) {
PostfixExpression.Operator op = ((PostfixExpression) parent).getOperator();
if (op == PostfixExpression.Operator.INCREMENT || op == PostfixExpression.Operator.DECREMENT) {
return true;
}
} else if (parent instanceof PrefixExpression) {
PrefixExpression.Operator op = ((PrefixExpression) parent).getOperator();
if (op == PrefixExpression.Operator.INCREMENT || op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.ADDRESS_OF) {
return true;
}
} else if (parent instanceof Assignment) {
return node == ((Assignment) parent).getLeftHandSide();
}
return false;
}
Aggregations