Skip to content

Commit

Permalink
Finished null literals, array creation, and array indexing.
Browse files Browse the repository at this point in the history
	Change on 2017/08/09 by manvithn <manvithn@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=164794972
  • Loading branch information
manvithn authored and tomball committed Aug 16, 2017
1 parent 51084b6 commit 6d3a3c2
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 39 deletions.
Expand Up @@ -14,19 +14,25 @@

package com.google.devtools.j2objc.javac;

import com.google.devtools.j2objc.ast.ArrayAccess;
import com.google.devtools.j2objc.ast.ArrayCreation;
import com.google.devtools.j2objc.ast.ArrayInitializer;
import com.google.devtools.j2objc.ast.ArrayType;
import com.google.devtools.j2objc.ast.Assignment;
import com.google.devtools.j2objc.ast.Block;
import com.google.devtools.j2objc.ast.ConditionalExpression;
import com.google.devtools.j2objc.ast.Expression;
import com.google.devtools.j2objc.ast.ExpressionStatement;
import com.google.devtools.j2objc.ast.IfStatement;
import com.google.devtools.j2objc.ast.InfixExpression;
import com.google.devtools.j2objc.ast.NullLiteral;
import com.google.devtools.j2objc.ast.ParenthesizedExpression;
import com.google.devtools.j2objc.ast.ReturnStatement;
import com.google.devtools.j2objc.ast.SimpleName;
import com.google.devtools.j2objc.ast.SourcePosition;
import com.google.devtools.j2objc.ast.Statement;
import com.google.devtools.j2objc.ast.SuperConstructorInvocation;
import com.google.devtools.j2objc.ast.ThisExpression;
import com.google.devtools.j2objc.ast.TreeNode;
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.Type;
Expand All @@ -39,12 +45,15 @@
import com.google.devtools.j2objc.util.TranslationUtil;
import com.strobel.assembler.metadata.TypeReference;
import com.strobel.decompiler.languages.java.ast.AstNode;
import com.strobel.decompiler.languages.java.ast.AstNodeCollection;
import com.strobel.decompiler.languages.java.ast.AstType;
import com.strobel.decompiler.languages.java.ast.IAstVisitor;
import com.strobel.decompiler.patterns.Pattern;
import com.sun.tools.javac.code.Symbol;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -171,19 +180,18 @@ public TreeNode visitIdentifier(
@Override
public TreeNode visitNullReferenceExpression(
com.strobel.decompiler.languages.java.ast.NullReferenceExpression node, Void data) {
throw new AssertionError("Method not yet implemented");
return new NullLiteral(translationEnv.typeUtil().getNull());
}

@Override
public TreeNode visitThisReferenceExpression(
com.strobel.decompiler.languages.java.ast.ThisReferenceExpression node, Void data) {
throw new AssertionError("Method not yet implemented");
return new ThisExpression().setTypeMirror(typeDecl.getTypeElement().asType());
}

@Override
public TreeNode visitSuperReferenceExpression(
com.strobel.decompiler.languages.java.ast.SuperReferenceExpression node, Void data) {
//TODO(user): arguments, super.someMethod, etc
TypeMirror objType = translationEnv.typeUtil().getJavaObject().asType();
TypeMirror nodeType = typeDecl.getTypeElement().asType();
assert !parserEnv.typeUtilities().isSameType(objType, nodeType);
Expand Down Expand Up @@ -274,7 +282,8 @@ public TreeNode visitLabeledStatement(
@Override
public TreeNode visitReturnStatement(
com.strobel.decompiler.languages.java.ast.ReturnStatement node, Void data) {
return new ReturnStatement((Expression) node.getExpression().acceptVisitor(this, null));
return new ReturnStatement()
.setExpression((Expression) node.getExpression().acceptVisitor(this, null));
}

@Override
Expand Down Expand Up @@ -322,15 +331,16 @@ public TreeNode visitNewLine(
@Override
public TreeNode visitVariableDeclaration(
com.strobel.decompiler.languages.java.ast.VariableDeclarationStatement node, Void data) {
//TODO(user): modifiers/multiple declaration
//TODO(user): multiple declaration array weirdness: (float arr1, float[] arr2)[][]
AstType astType = node.getType();
com.strobel.decompiler.languages.java.ast.VariableInitializer init =
(com.strobel.decompiler.languages.java.ast.VariableInitializer) astType.getNextSibling();
Type type = (Type) astType.acceptVisitor(this, null);
Expression expr = (Expression) init.acceptVisitor(this, null);
String varName = init.getName();
VariableElement elem = GeneratedVariableElement
.newLocalVar(varName, type.getTypeMirror(), executableElement);
GeneratedVariableElement elem =
GeneratedVariableElement.newLocalVar(varName, type.getTypeMirror(), executableElement);
elem.addModifiers(node.getModifiers());
localVariableTable.put(varName, elem);
return new VariableDeclarationStatement(elem, expr);
}
Expand Down Expand Up @@ -421,7 +431,7 @@ public TreeNode visitArraySpecifier(
@Override
public TreeNode visitComposedType(
com.strobel.decompiler.languages.java.ast.ComposedType node, Void data) {
throw new AssertionError("Method not yet implemented");
return Type.newType(resolve(node));
}

@Override
Expand Down Expand Up @@ -533,7 +543,9 @@ public TreeNode visitInstanceOfExpression(
@Override
public TreeNode visitIndexerExpression(
com.strobel.decompiler.languages.java.ast.IndexerExpression node, Void data) {
throw new AssertionError("Method not yet implemented");
return new ArrayAccess()
.setArray((Expression) node.getTarget().acceptVisitor(this, null))
.setIndex((Expression) node.getArgument().acceptVisitor(this, null));
}

@Override
Expand Down Expand Up @@ -566,7 +578,10 @@ public TreeNode visitConditionalExpression(
@Override
public TreeNode visitArrayInitializerExpression(
com.strobel.decompiler.languages.java.ast.ArrayInitializerExpression node, Void data) {
throw new AssertionError("Method not yet implemented");
List<Expression> expressions = node.getElements().stream()
.map(e -> (Expression) e.acceptVisitor(this, null))
.collect(Collectors.toList());
return new ArrayInitializer().setExpressions(expressions);
}

@Override
Expand All @@ -578,7 +593,26 @@ public TreeNode visitObjectCreationExpression(
@Override
public TreeNode visitArrayCreationExpression(
com.strobel.decompiler.languages.java.ast.ArrayCreationExpression node, Void data) {
throw new AssertionError("Method not yet implemented");
Type baseType = (Type) node.getType().acceptVisitor(this, null);
AstNodeCollection<com.strobel.decompiler.languages.java.ast.Expression> dimexprs
= node.getDimensions();
com.strobel.decompiler.languages.java.ast.ArrayInitializerExpression init
= node.getInitializer();
ArrayType arrayType = new ArrayType(translationEnv.typeUtil().getArrayType(
baseType.getTypeMirror(), dimexprs.size() + node.getAdditionalArraySpecifiers().size()));
if (init.isNull()) {
List<Expression> dimensions = dimexprs.stream()
.map(e -> (Expression) e.acceptVisitor(this, null))
.collect(Collectors.toList());
return new ArrayCreation()
.setType(arrayType)
.setDimensions(dimensions);
} else {
ArrayInitializer arrayInit = (ArrayInitializer) init.acceptVisitor(this, null);
return new ArrayCreation()
.setType(arrayType)
.setInitializer(arrayInit.setTypeMirror(arrayType.getTypeMirror()));
}
}

@Override
Expand Down
Expand Up @@ -302,6 +302,17 @@ public ArrayType getArrayType(TypeMirror componentType) {
return javacTypes.getArrayType(componentType);
}

public ArrayType getArrayType(TypeMirror componentType, int dims) {
if (dims < 1) {
throw new IllegalArgumentException("dims must be greater than or equal to 1");
}
if (dims == 1) {
return getArrayType(componentType);
} else {
return getArrayType(getArrayType(componentType), dims - 1);
}
}

boolean isGeneratedType(TypeMirror type) {
return type instanceof AbstractTypeMirror;
}
Expand Down
Expand Up @@ -784,8 +784,8 @@ public void testMultiVariableDeclAssign() throws IOException {
"package foo.bar;",
"class Test {",
" int run(int i, int j, int k) {",
" int a = 1, b = 2, c = 3;",
" int x = a + i;",
" final int a = i + 1, b = j + 2, c = k + 3;",
" final int x = a + i;",
" int y = b + j;",
" int z = c + k;",
" z += (y += x);",
Expand All @@ -802,26 +802,23 @@ public void testBinaryOperator() throws IOException {
"package foo.bar;",
"class Binary {",
" boolean coverage(int a, int b) {",
" int c = (a & b) | (a ^ b);",
" int d = a + b - c * a / b % c << a >> b >>> c;",
" boolean e = (a < b && b <= c) || (c > b && b >= a);",
" final int c = (a & b) | (a ^ b);",
" final int d = a + b - c * a / b % c << a >> b >>> c;",
" final boolean e = (a < b && b <= c) || (c > b && b >= a);",
" return a == b || (c != d && e);",
" }",
" int parenthesis(int a, int b, int c, int d, int e, int f, int g) {",
" return a * (b + (c << (d & (e | (f ^ g)))));",
" }",
" double promotion(byte a, short b, int c, long d, float e, double f) {",
" long g = a * b / c * d;",
" long h = d * (c / (b * a));",
" float i = g * e;",
" double j = h / f;",
" float k = e * g;",
" double l = f / h;",
" double m = i + j;",
" double n = l + k;",
// " String o = \"Hello\" + m;",
// " String p = n + \"World\";",
// " return o + p;",
" final long g = a * b / c * d;",
" final long h = d * (c / (b * a));",
" final float i = g * e;",
" final double j = h / f;",
" final float k = e * g;",
" final double l = f / h;",
" final double m = i + j;",
" final double n = l + k;",
" return m + n;",
" }",
"}"
Expand All @@ -838,37 +835,156 @@ public void testTernaryOperator() throws IOException {
" return c ? a : b;",
" }",
" int promotion1(byte a, short b, boolean c) {",
" short x = c ? a : b;",
" short y = c ? b : a;",
" final short x = c ? a : b;",
" final short y = c ? b : a;",
" return x + y;",
" }",
" int promotion2(short a, int b, boolean c) {",
" int x = c ? a : b;",
" int y = c ? b : a;",
" final int x = c ? a : b;",
" final int y = c ? b : a;",
" return x + y;",
" }",
" long promotion3(int a, long b, boolean c) {",
" long x = c ? a : b;",
" long y = c ? b : a;",
" final long x = c ? a : b;",
" final long y = c ? b : a;",
" return x + y;",
" }",
" double promotion4(float a, double b, boolean c) {",
" double x = c ? a : b;",
" double y = c ? b : a;",
" final double x = c ? a : b;",
" final double y = c ? b : a;",
" return x + y;",
" }",
" float promotion5(long a, float b, boolean c) {",
" float x = c ? a : b;",
" float y = c ? b : a;",
" final float x = c ? a : b;",
" final float y = c ? b : a;",
" return x + y;",
" }",
" double promotion6(long a, double b, boolean c) {",
" double x = c ? a : b;",
" double y = c ? b : a;",
" final double x = c ? a : b;",
" final double y = c ? b : a;",
" return x + y;",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

public void testNull() throws IOException {
String type = "foo.bar.Test";
String source = String.join("\n",
"package foo.bar;",
"class Test {",
" String returnNull() {",
" return null;",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

public void testBasicArrays() throws IOException {
String type = "foo.bar.Test";
String source = String.join("\n",
"package foo.bar;",
"class Test {",
" void createArrayInt(int[] arr, int size) {",
" final int[] arrnew = new int[size];",
" arrnew[0] = arr[0];",
" }",
" void createArrayString(String[] arr, int size) {",
" final String[] arrnew = new String[size];",
" arrnew[0] = arr[0];",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

public void testMultidimensionalArrays() throws IOException {
String type = "foo.bar.Test";
String source = String.join("\n",
"package foo.bar;",
"class Test {",
" void createArray1(int[][][] arr, int size) {",
" final int[][][] arrnew = new int[size][size + 1][size + 2];",
" arrnew[0] = arr[0];",
" arrnew[size - 1] = arr[size - 1];",
" arrnew[size - 1][size - 1][size - 1] = arr[size - 1][size - 1][size - 1];",
" }",
" void createArray2(int[][][][] arr, int size) {",
" final int[][][][] arrnew = new int[size][size][][];",
" arrnew[0] = new int[size][][];",
" arrnew[size - 1] = arr[size - 1];",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

public void testArrayInitializers() throws IOException {
String type = "foo.bar.Test";
String source = String.join("\n",
"package foo.bar;",
"class Test {",
" int[] createArrayInit1(int[] arr) {",
" final int[] arrnew = {1, 2, 3, 4, 5};",
" arr = new int[] {6, 7, 8, 9, 10};",
" return arrnew;",
" }",
" int[][][] createArrayInit2(int[][][] arr) {",
" final int[][][] arrnew = {{{1, 2}, {3}}, {{4, 5}}};",
" arr = new int[][][] {{{6, 7}, {8}}, {{9, 10}}};",
" return arrnew;",
" }",
" int[][][] createArrayInit3(int[][][] arr) {",
" final int[][][] arrnew = {{new int[2], null}, new int[1][]};",
" arr = new int[][][] {{new int[2], null}, new int[1][]};",
" return arrnew;",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

public void testArraySubtyping() throws IOException {
String type = "foo.bar.Test";
String source = String.join("\n",
"package foo.bar;",
"class Test {",
" Object[] createArray(int size) {",
" final Object[] arrnew = new String[size];",
" return arrnew;",
" }",
"}"
);
assertEqualASTSrcClassfile(type, source);
}

// public void testThis() throws IOException {
// String type = "foo.bar.Point";
// String source = String.join("\n",
// "package foo.bar;",
// "class Point {",
// " int x;",
// " int y;",
// " Point(int x, int y) {",
// " this.x = x;",
// " this.y = y;",
// " }",
// " Point(int z) {",
// " this(z, z);",
// " }",
// " int getX() {",
// " return this.x;",
// " }",
// " int getY() {",
// " return this.y;",
// " }",
// " Point identity() {",
// " return this;",
// " }",
// "}"
// );
// assertEqualASTSrcClassfile(type, source);
// }
}

0 comments on commit 6d3a3c2

Please sign in to comment.