Skip to content

Commit

Permalink
WIP: deconstructor translation
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Jun 5, 2023
1 parent 2c8d86f commit ae45693
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
import static com.sun.tools.javac.code.Flags.STATIC;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Kinds.Kind;

import static com.sun.tools.javac.code.Kinds.Kind.MTH;
import static com.sun.tools.javac.code.Kinds.Kind.VAR;
import static com.sun.tools.javac.code.TypeTag.*;

import com.sun.tools.javac.code.Preview;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.BindingSymbol;
Expand Down Expand Up @@ -68,29 +72,12 @@
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;

import java.util.Collections;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Set;

import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.RecordComponent;
import com.sun.tools.javac.code.Type;
import static com.sun.tools.javac.code.TypeTag.ARRAY;
import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
import static com.sun.tools.javac.code.TypeTag.BOT;
import static com.sun.tools.javac.code.TypeTag.BYTE;
import static com.sun.tools.javac.code.TypeTag.CHAR;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.DOUBLE;
import static com.sun.tools.javac.code.TypeTag.FLOAT;
import static com.sun.tools.javac.code.TypeTag.INT;
import static com.sun.tools.javac.code.TypeTag.LONG;
import static com.sun.tools.javac.code.TypeTag.SHORT;
import static com.sun.tools.javac.code.TypeTag.VOID;
import com.sun.tools.javac.jvm.PoolConstant.LoadableConstant;
import com.sun.tools.javac.jvm.Target;
import com.sun.tools.javac.tree.JCTree;
Expand Down Expand Up @@ -1155,9 +1142,42 @@ public void visitMethodDef(JCMethodDecl tree) {
currentMethodSym = tree.sym;
variableIndex = 0;
deconstructorCalls = null;
// if (tree.sym.isDeconstructor()) {
// tree.params = List.nil();
// }
if (tree.sym.isDeconstructor()) {
List<JCStatement> stats = tree.body.stats;
boolean genCarrier = false;
for (int i = 0; i < stats.size(); i++) {
var statement = stats.get(i);
if (statement instanceof JCExpressionStatement eStat &&
eStat.expr instanceof JCAssign assignment &&
assignment.lhs.hasTag(Tag.IDENT) &&
((JCIdent) assignment.lhs).sym.owner.isDeconstructor()) {
genCarrier = true;
}
}

if (genCarrier) {
// MethodType returnType = MethodType.methodType(Object.class, Collection.class);
// Object carrier = Carriers.factory(returnType).invoke(is);
// return carrier;
Type lhsType = tree.params.get(0).type;

MethodSymbol methodTypeCallSym = rs.resolveInternalMethod(tree.pos(), env, syms.methodTypeType, names.fromString("methodType"), List.of(syms.classType, types.makeArrayType(types.erasure(syms.classType))), List.nil());
List<JCExpression> params = List.of(classOfType(syms.objectType, tree.pos()), classOfType(types.erasure(lhsType), tree.pos()));
JCMethodInvocation methodTypeCall = make.App(make.QualIdent(methodTypeCallSym), params).setType(syms.methodTypeType);
methodTypeCall.varargsElement = types.erasure(types.elemtype(methodTypeCallSym.type.getParameterTypes().last()));

MethodSymbol factoryMethodSym = rs.resolveInternalMethod(tree.pos(), env, syms.carriersType, names.fromString("factory"), List.of(syms.methodTypeType), List.nil());
List<JCExpression> factoryMethodParams = List.of(methodTypeCall);
JCMethodInvocation factoryMethodCall = make.App(make.QualIdent(factoryMethodSym), factoryMethodParams).setType(syms.methodHandleType);

MethodSymbol invokeMethodSym = rs.resolveInternalMethod(tree.pos(), env, syms.methodHandleType, names.fromString("invoke"), List.of(types.makeArrayType(types.erasure(syms.objectsType))), List.nil());
List<JCExpression> invokeMethodParam = List.of(make.Ident(tree.params.get(0)));
JCMethodInvocation invokeMethodCall = make.App(make.Select(factoryMethodCall, invokeMethodSym), invokeMethodParam).setType(syms.objectType);
invokeMethodCall.varargsElement = types.erasure(types.elemtype(invokeMethodSym.type.getParameterTypes().last()));

tree.body.stats = tree.body.stats.append(make.Return(invokeMethodCall));
}
}
super.visitMethodDef(tree);
preparePatternMatchingCatchIfNeeded(tree.body);
} finally {
Expand Down
7 changes: 2 additions & 5 deletions test/langtools/tools/javac/patterns/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ public static List<Integer> test(Object o) {
return List.of(-1);
}

public __matcher Points(@Foo Collection<Integer> is) throws Throwable {
// is = this.is;

MethodType returnType = MethodType.methodType(Object.class, Collection.class);
return Carriers.factory(returnType).invoke(this.is);
public __matcher Points(@Foo Collection<Integer> is) {
is = this.is;
}

@Target(ElementType.PARAMETER)
Expand Down

0 comments on commit ae45693

Please sign in to comment.