use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class MethodTranslator method visitVariableDeclaration.
@Override
public TreeNode visitVariableDeclaration(com.strobel.decompiler.languages.java.ast.VariableDeclarationStatement node, Void data) {
VariableDeclarationStatement varDecl = new VariableDeclarationStatement().setModifiers(ElementUtil.fromModifierSet(new HashSet<>(node.getModifiers())));
AstType astType = node.getType();
Type type = (Type) astType.acceptVisitor(this, null);
for (VariableInitializer init : node.getVariables()) {
Expression expr = (Expression) init.acceptVisitor(this, null);
String varName = init.getName();
GeneratedVariableElement elem = GeneratedVariableElement.newLocalVar(varName, type.getTypeMirror(), executableElement);
elem.addModifiers(node.getModifiers());
localVariableTable.put(varName, elem);
varDecl.addFragment(new VariableDeclarationFragment(elem, expr));
}
return varDecl;
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement 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.VariableDeclarationStatement in project j2objc by google.
the class EnhancedForRewriter method boxLoopVariable.
private void boxLoopVariable(EnhancedForStatement node, TypeMirror expressionType, VariableElement loopVariable) {
DeclaredType iterableType = typeUtil.findSupertype(expressionType, "java.lang.Iterable");
List<? extends TypeMirror> typeArgs = iterableType.getTypeArguments();
assert typeArgs.size() == 1 && typeUtil.isBoxedType(typeArgs.get(0));
VariableElement boxVariable = GeneratedVariableElement.newLocalVar("boxed__", typeArgs.get(0), null);
node.setParameter(new SingleVariableDeclaration(boxVariable));
makeBlock(node.getBody()).addStatement(0, new VariableDeclarationStatement(loopVariable, new SimpleName(boxVariable)));
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement 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.VariableDeclarationStatement in project j2objc by google.
the class EnhancedForRewriter method convertToJavaIteratorLoop.
private void convertToJavaIteratorLoop(EnhancedForStatement node) {
Expression expression = node.getExpression();
TypeMirror expressionType = expression.getTypeMirror();
VariableElement loopVariable = node.getParameter().getVariableElement();
DeclaredType iterableType = typeUtil.findSupertype(expressionType, "java.lang.Iterable");
ExecutablePair iteratorMethod = typeUtil.findMethod(iterableType, "iterator");
DeclaredType iteratorType = (DeclaredType) iteratorMethod.type().getReturnType();
ExecutablePair hasNextMethod = typeUtil.findMethod(iteratorType, "hasNext");
ExecutablePair nextMethod = typeUtil.findMethod(iteratorType, "next");
assert hasNextMethod != null && nextMethod != null;
VariableElement iteratorVariable = GeneratedVariableElement.newLocalVar("iter__", iteratorType, null);
MethodInvocation iteratorInvocation = new MethodInvocation(iteratorMethod, TreeUtil.remove(expression));
VariableDeclarationStatement iteratorDecl = new VariableDeclarationStatement(iteratorVariable, iteratorInvocation);
MethodInvocation hasNextInvocation = new MethodInvocation(hasNextMethod, new SimpleName(iteratorVariable));
MethodInvocation nextInvocation = new MethodInvocation(nextMethod, new SimpleName(iteratorVariable));
Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, nextInvocation));
WhileStatement whileLoop = new WhileStatement();
whileLoop.setExpression(hasNextInvocation);
whileLoop.setBody(newLoopBody);
Block block = new Block();
List<Statement> stmts = block.getStatements();
stmts.add(iteratorDecl);
stmts.add(whileLoop);
replaceLoop(node, block, whileLoop);
}
Aggregations