use of com.google.devtools.j2objc.ast.CommaExpression in project j2objc by google.
the class EnumRewriter method addNonArcInitialization.
private void addNonArcInitialization(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
int baseTypeCount = 0;
List<Statement> sizeStatements = new ArrayList<>();
List<Statement> initStatements = new ArrayList<>();
TypeMirror voidType = typeUtil.getVoid();
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
String varName = ElementUtil.getName(varElement);
ExecutableElement methodElement = constant.getExecutableElement();
TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
boolean isAnonymous = valueType != type;
String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
if (isAnonymous) {
sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
} else {
baseTypeCount++;
}
initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
String initName = nameTable.getFullFunctionName(methodElement);
FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
initFunc.addArgument(new SimpleName(localEnum));
TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
initFunc.addArgument(new StringLiteral(varName, typeUtil));
initFunc.addArgument(new NumberLiteral(i++, typeUtil));
initStatements.add(new ExpressionStatement(initFunc));
}
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
if (baseTypeCount == 0) {
stmts.add(new NativeStatement("size_t allocSize = 0;"));
} else {
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
}
stmts.addAll(sizeStatements);
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
stmts.add(new VariableDeclarationStatement(localEnum, null));
stmts.addAll(initStatements);
}
use of com.google.devtools.j2objc.ast.CommaExpression in project j2objc by google.
the class OperatorRewriter method getRetainedWithTarget.
// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
Expression lhs = node.getLeftHandSide();
if (!(lhs instanceof FieldAccess)) {
return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
}
// To avoid duplicating the target expression we must save the result to a local variable.
FieldAccess fieldAccess = (FieldAccess) lhs;
Expression target = fieldAccess.getExpression();
VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
fieldAccess.setExpression(new SimpleName(targetVar));
CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
node.replaceWith(commaExpr);
commaExpr.addExpression(node);
return new SimpleName(targetVar);
}
use of com.google.devtools.j2objc.ast.CommaExpression in project j2objc by google.
the class StaticVarRewriter method visit.
@Override
public boolean visit(FieldAccess node) {
VariableElement var = node.getVariableElement();
if (ElementUtil.isInstanceVar(var)) {
node.getExpression().accept(this);
return false;
}
Expression expr = TreeUtil.remove(node.getExpression());
Expression varNode = TreeUtil.remove(node.getName());
if (!TranslationUtil.hasSideEffect(expr)) {
node.replaceWith(varNode);
varNode.accept(this);
return false;
}
CommaExpression commaExpr = new CommaExpression(expr);
if (TranslationUtil.isAssigned(node)) {
commaExpr.addExpression(new PrefixExpression(new PointerType(var.asType()), PrefixExpression.Operator.ADDRESS_OF, varNode));
node.replaceWith(new PrefixExpression(var.asType(), PrefixExpression.Operator.DEREFERENCE, commaExpr));
} else {
commaExpr.addExpression(varNode);
node.replaceWith(commaExpr);
}
commaExpr.accept(this);
return false;
}
use of com.google.devtools.j2objc.ast.CommaExpression in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(CommaExpression node) {
buffer.append('(');
for (Iterator<Expression> it = node.getExpressions().iterator(); it.hasNext(); ) {
Expression e = it.next();
e.accept(this);
if (it.hasNext()) {
buffer.append(", ");
}
}
buffer.append(')');
return false;
}
use of com.google.devtools.j2objc.ast.CommaExpression 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 (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;
}
Aggregations