use of com.google.devtools.j2objc.ast.Assignment 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 CastExpression(voidType, new ParenthesizedExpression(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.Assignment in project j2objc by google.
the class TreeConverter method convertAssignment.
private static TreeNode convertAssignment(org.eclipse.jdt.core.dom.Assignment node) {
Assignment newNode = new Assignment();
convertExpression(node, newNode);
return newNode.setOperator(Assignment.Operator.fromJdtOperatorName(node.getOperator().toString())).setLeftHandSide((Expression) convert(node.getLeftHandSide())).setRightHandSide((Expression) convert(node.getRightHandSide()));
}
use of com.google.devtools.j2objc.ast.Assignment in project j2objc by google.
the class ArrayRewriter method newArrayAssignment.
private FunctionInvocation newArrayAssignment(Assignment assignmentNode, ArrayAccess arrayAccessNode, TypeMirror componentType) {
Assignment.Operator op = assignmentNode.getOperator();
assert !componentType.getKind().isPrimitive();
assert op == Assignment.Operator.ASSIGN;
Expression value = TreeUtil.remove(assignmentNode.getRightHandSide());
Expression retainedValue = TranslationUtil.retainResult(value);
String funcName = "IOSObjectArray_Set";
if (retainedValue != null) {
funcName = "IOSObjectArray_SetAndConsume";
value = retainedValue;
}
TypeElement objArrayType = TypeUtil.IOS_OBJECT_ARRAY;
TypeMirror idType = TypeUtil.ID_TYPE;
FunctionElement element = new FunctionElement(funcName, idType, objArrayType).addParameters(objArrayType.asType(), typeUtil.getInt(), idType);
FunctionInvocation invocation = new FunctionInvocation(element, componentType);
List<Expression> args = invocation.getArguments();
args.add(TreeUtil.remove(arrayAccessNode.getArray()));
args.add(TreeUtil.remove(arrayAccessNode.getIndex()));
args.add(value);
return invocation;
}
use of com.google.devtools.j2objc.ast.Assignment in project j2objc by google.
the class TreeConverter method convertAssignOp.
private TreeNode convertAssignOp(CompoundAssignmentTree node, TreePath parent) {
TreePath path = getTreePath(parent, node);
Assignment newNode = new Assignment();
return newNode.setOperator(Assignment.Operator.from(node.getKind())).setLeftHandSide((Expression) convert(node.getVariable(), path)).setRightHandSide((Expression) convert(node.getExpression(), path));
}
use of com.google.devtools.j2objc.ast.Assignment in project j2objc by google.
the class MethodTranslator method visitAssignmentExpression.
@Override
public TreeNode visitAssignmentExpression(AssignmentExpression node, Void data) {
Expression leftExpr = (Expression) node.getLeft().acceptVisitor(this, null);
Expression rightExpr = (Expression) node.getRight().acceptVisitor(this, null);
Assignment assignment = new Assignment(leftExpr, rightExpr);
switch(node.getOperator()) {
case ASSIGN:
assignment.setOperator(Assignment.Operator.ASSIGN);
break;
case ADD:
assignment.setOperator(Assignment.Operator.PLUS_ASSIGN);
break;
case SUBTRACT:
assignment.setOperator(Assignment.Operator.MINUS_ASSIGN);
break;
case MULTIPLY:
assignment.setOperator(Assignment.Operator.TIMES_ASSIGN);
break;
case DIVIDE:
assignment.setOperator(Assignment.Operator.DIVIDE_ASSIGN);
break;
case MODULUS:
assignment.setOperator(Assignment.Operator.REMAINDER_ASSIGN);
break;
case SHIFT_LEFT:
assignment.setOperator(Assignment.Operator.LEFT_SHIFT_ASSIGN);
break;
case SHIFT_RIGHT:
assignment.setOperator(Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN);
break;
case UNSIGNED_SHIFT_RIGHT:
assignment.setOperator(Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN);
break;
case BITWISE_AND:
assignment.setOperator(Assignment.Operator.BIT_AND_ASSIGN);
break;
case BITWISE_OR:
assignment.setOperator(Assignment.Operator.BIT_OR_ASSIGN);
break;
case EXCLUSIVE_OR:
assignment.setOperator(Assignment.Operator.BIT_XOR_ASSIGN);
break;
default:
throw new AssertionError("Unsupported assignment operator: " + node.getOperator());
}
return assignment;
}
Aggregations