Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 653f704265 | |||
| b36ba5e128 | |||
| a43f49cadb | |||
| 69292c37fc | |||
| 218e704272 | |||
| e83e9a041c | |||
| 2f7e4fc1e4 | |||
| 433dcf83c2 |
@@ -0,0 +1,10 @@
|
|||||||
|
import java.lang.String;
|
||||||
|
|
||||||
|
public class Bug389 {
|
||||||
|
public swap(f) {
|
||||||
|
return x -> y -> f.apply(y).apply(x);
|
||||||
|
}
|
||||||
|
public swap(f) {
|
||||||
|
return x -> y -> z -> f.apply(z).apply(x).apply(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import Bug389;
|
||||||
|
import java.util.List;
|
||||||
|
import java.lang.String;
|
||||||
|
import java.lang.Integer;
|
||||||
|
|
||||||
|
public class Bug389Main {
|
||||||
|
public static main(args) {
|
||||||
|
var func = x -> y -> z -> x + y + z;
|
||||||
|
var swap = new Bug389();
|
||||||
|
swap.swap(func).apply(1).apply(2).apply(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
sealed interface List<T> permits Cons, Empty {}
|
import Cons;
|
||||||
|
import Empty;
|
||||||
public record Cons<T>(T a, List<T> l) implements List<T> {}
|
import List;
|
||||||
public record Empty<T>() implements List<T> {}
|
import Pair;
|
||||||
public record Tuple<T1, T2>(T1 a, T2 b) {}
|
|
||||||
|
|
||||||
public class PatternMatching {
|
public class PatternMatching {
|
||||||
public zip(Cons(x, xs), Cons(y, ys)) {
|
public zip(Cons(x, xs), Cons(y, ys)) {
|
||||||
return new Cons(new Tuple(x, y), zip(xs, ys));
|
return new Cons<>(new Pair<>(x, y), zip(xs, ys));
|
||||||
}
|
}
|
||||||
public zip(Empty x, Empty y) { return new Empty(); }
|
public zip(Empty x, Empty y) { return new Empty<>(); }
|
||||||
|
|
||||||
/*public zip(Empty x, Cons y) { return new Empty(); }
|
/*public zip(Empty x, Cons y) { return new Empty(); }
|
||||||
public zip(Cons x, Empty y) { return new Empty(); }
|
public zip(Cons x, Empty y) { return new Empty(); }
|
||||||
|
|||||||
@@ -2,18 +2,24 @@ import java.lang.Object;
|
|||||||
import List;
|
import List;
|
||||||
import Cons;
|
import Cons;
|
||||||
import Empty;
|
import Empty;
|
||||||
import Tuple;
|
import Pair;
|
||||||
|
|
||||||
public class PatternMatchingJava {
|
public class PatternMatchingJava {
|
||||||
public zip(a, b) {
|
public <A, B> Cons<Pair<A, B>> zip(Cons<A> a, Cons<B> b) {
|
||||||
switch (a) {
|
switch (a) {
|
||||||
case Cons(x, Cons xs) -> {
|
case Cons(x, Cons xs) -> {
|
||||||
switch (b) {
|
switch (b) {
|
||||||
case Cons(y, Cons ys) -> { return new Cons<>(new Tuple<>(x, y), zip(xs, ys)); }
|
case Cons(y, Cons ys) -> { return new Cons<>(new Pair<>(x, y), zip(xs, ys)); }
|
||||||
case Cons(y, Empty()) -> { return new Empty<>(); }
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
case Cons(x, Empty()) -> { return new Empty<>(); }
|
case Cons(x, Empty xs) -> {
|
||||||
|
switch (b) {
|
||||||
|
case Cons(y, Empty ys) -> { return new Cons<>(new Pair<>(x, y), zip(xs, ys)); }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public <A, B> Empty<Pair<A, B>> zip(Empty<A> a, Empty<B>) {
|
||||||
|
return new Empty<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
sealed interface List<T> permits Cons, Empty {}
|
public sealed interface List<T> permits Cons, Empty {}
|
||||||
|
|
||||||
public record Cons<T>(T a, List<T> l) implements List<T> {}
|
public record Cons<T>(T a, List<T> l) implements List<T> {}
|
||||||
public record Empty<T>() implements List<T> {}
|
public record Empty<T>() implements List<T> {}
|
||||||
public record Tuple<T1, T2>(T1 a, T2 b) {}
|
public record Pair<T1, T2>(T1 a, T2 b) {}
|
||||||
@@ -4,6 +4,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler;
|
|||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.Pattern;
|
||||||
import de.dhbwstuttgart.target.generate.ASTToTargetAST;
|
import de.dhbwstuttgart.target.generate.ASTToTargetAST;
|
||||||
import de.dhbwstuttgart.target.tree.*;
|
import de.dhbwstuttgart.target.tree.*;
|
||||||
import de.dhbwstuttgart.target.tree.expression.*;
|
import de.dhbwstuttgart.target.tree.expression.*;
|
||||||
@@ -1498,12 +1499,13 @@ public class Codegen {
|
|||||||
state.exitScope();
|
state.exitScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void extractField(State state, TargetType type, int i, ClassOrInterface clazz) {
|
private void extractField(State state, TargetType ret, TargetType type, int i, ClassOrInterface clazz) {
|
||||||
if (i >= clazz.getFieldDecl().size())
|
if (i >= clazz.getFieldDecl().size())
|
||||||
throw new CodeGenException("Couldn't find suitable field accessor for '" + type.name() + "'");
|
throw new CodeGenException("Couldn't find suitable field accessor for '" + type.name() + "'");
|
||||||
var field = clazz.getFieldDecl().get(i);
|
var field = clazz.getFieldDecl().get(i);
|
||||||
var fieldType = converter.convert(field.getType());
|
var fieldType = converter.convert(field.getType());
|
||||||
state.mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), field.getName(), "()" + fieldType.toDescriptor(), false);
|
state.mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), field.getName(), "()" + fieldType.toDescriptor(), false);
|
||||||
|
if (ret != null) convertTo(state, fieldType, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindPattern(State state, TargetType type, TargetPattern pat, Label start, int index, int depth) {
|
private void bindPattern(State state, TargetType type, TargetPattern pat, Label start, int index, int depth) {
|
||||||
@@ -1566,7 +1568,7 @@ public class Codegen {
|
|||||||
var subPattern = cp.subPatterns().get(i);
|
var subPattern = cp.subPatterns().get(i);
|
||||||
|
|
||||||
state.mv.visitInsn(DUP);
|
state.mv.visitInsn(DUP);
|
||||||
extractField(state, cp.type(), i, clazz);
|
extractField(state, null, cp.type(), i, clazz);
|
||||||
|
|
||||||
if (subPattern.type() instanceof TargetRefType || subPattern.type() instanceof TargetExtendsWildcard) {
|
if (subPattern.type() instanceof TargetRefType || subPattern.type() instanceof TargetExtendsWildcard) {
|
||||||
state.mv.visitInsn(DUP);
|
state.mv.visitInsn(DUP);
|
||||||
@@ -1668,7 +1670,7 @@ public class Codegen {
|
|||||||
if (i < cp.subPatterns().size() - 1)
|
if (i < cp.subPatterns().size() - 1)
|
||||||
state.mv.visitInsn(DUP);
|
state.mv.visitInsn(DUP);
|
||||||
|
|
||||||
extractField(state, cp.type(), i, clazz);
|
extractField(state, subPattern.type(), cp.type(), i, clazz);
|
||||||
if (subPattern.type() instanceof TargetRefType)
|
if (subPattern.type() instanceof TargetRefType)
|
||||||
state.mv.visitTypeInsn(CHECKCAST, subPattern.type().getInternalName());
|
state.mv.visitTypeInsn(CHECKCAST, subPattern.type().getInternalName());
|
||||||
offset = state.createVariable(subPattern.name(), subPattern.type()).index;
|
offset = state.createVariable(subPattern.name(), subPattern.type()).index;
|
||||||
|
|||||||
@@ -422,43 +422,6 @@ public class JavaTXCompiler {
|
|||||||
return typeInference(List.of(file));
|
return typeInference(List.of(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String constraintSetToDot(ConstraintSet<Pair> c){
|
|
||||||
|
|
||||||
Iterator<String> colors = List.of(
|
|
||||||
"#FF0000", "#FF7F00", "#FFFF00", "#7FFF00", "#00FF00", "#00FF7F", "#00FFFF", "#007FFF",
|
|
||||||
"#0000FF", "#7F00FF", "#FF00FF", "#FF007F", "#BF0000", "#BF5F00", "#BFBF00", "#5FBF00",
|
|
||||||
"#00BF00", "#00BF5F", "#00BFBF", "#005FBF", "#0000BF", "#5F00BF", "#BF00BF", "#BF005F",
|
|
||||||
"#800000", "#804000", "#808000", "#408000", "#008000", "#008040", "#008080", "#004080",
|
|
||||||
"#000080", "#400080", "#800080", "#800040", "#FF4040", "#FF8040", "#FFFF40", "#80FF40",
|
|
||||||
"#40FF40", "#40FF80", "#40FFFF", "#4080FF", "#4040FF", "#8040FF", "#FF40FF", "#FF4080",
|
|
||||||
"#BF4040", "#BF8040", "#BFBF40", "#80BF40", "#40BF40", "#40BF80", "#40BFBF", "#4080BF",
|
|
||||||
"#4040BF", "#8040BF", "#BF40BF", "#BF4080", "#FF8080", "#FFBF80", "#FFFF80", "#BFFF80",
|
|
||||||
"#80FF80", "#80FFBF", "#80FFFF", "#80BFFF", "#8080FF", "#BF80FF", "#FF80FF", "#FF80BF").iterator();
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.append("diagraph G{");
|
|
||||||
|
|
||||||
//and constraints to dot
|
|
||||||
sb.append(c.getUndConstraints().stream().map(Pair::toDot).collect(Collectors.joining("\n")));
|
|
||||||
|
|
||||||
//or constraints to dot
|
|
||||||
for (var orConstSet : c.getOderConstraints()){
|
|
||||||
for(var cons : orConstSet){
|
|
||||||
String color = colors.next();
|
|
||||||
sb.append(cons.stream().map(x -> x.toDot(color)).collect(Collectors.joining("\n")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.append("}");
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String q(String s) {
|
|
||||||
return "\"" + s.replace("\"", "\\\"") + "\"";
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ResultSet> typeInference(List<File> files) throws ClassNotFoundException, IOException {
|
public List<ResultSet> typeInference(List<File> files) throws ClassNotFoundException, IOException {
|
||||||
Set<ClassOrInterface> allClasses = new HashSet<>();
|
Set<ClassOrInterface> allClasses = new HashSet<>();
|
||||||
|
|
||||||
@@ -475,36 +438,6 @@ public class JavaTXCompiler {
|
|||||||
TYPE ty = new TYPE(definedClasses, allClasses);
|
TYPE ty = new TYPE(definedClasses, allClasses);
|
||||||
var cons = ty.getConstraints();
|
var cons = ty.getConstraints();
|
||||||
|
|
||||||
var ANDconstraints = cons.getUndConstraints();
|
|
||||||
var ORConstraints = cons.getOderConstraints();
|
|
||||||
|
|
||||||
var orIterator = ORConstraints.iterator();
|
|
||||||
|
|
||||||
while(orIterator.hasNext()){
|
|
||||||
Set<Constraint<Pair>> y = orIterator.next();
|
|
||||||
if (y.isEmpty()) orIterator.remove();
|
|
||||||
else if (y.size() == 1){
|
|
||||||
ANDconstraints.addAll(y.iterator().next()); // add the OR constraint to the AND constraint since we only have one option
|
|
||||||
orIterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String dot = constraintSetToDot(cons);
|
|
||||||
|
|
||||||
|
|
||||||
ANDconstraints.forEach(System.out::println);
|
|
||||||
|
|
||||||
for (var orc : ORConstraints){
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("------");
|
|
||||||
System.out.println();
|
|
||||||
for(var hashs : orc){
|
|
||||||
System.out.print(hashs);
|
|
||||||
System.out.print("\n | \n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Set<Set<UnifyPair>> results = new HashSet<>();
|
Set<Set<UnifyPair>> results = new HashSet<>();
|
||||||
PlaceholderRegistry placeholderRegistry = new PlaceholderRegistry();
|
PlaceholderRegistry placeholderRegistry = new PlaceholderRegistry();
|
||||||
|
|
||||||
|
|||||||
@@ -700,7 +700,7 @@ public class StatementGenerator {
|
|||||||
case ConditionalassignexpressionContext condassign:
|
case ConditionalassignexpressionContext condassign:
|
||||||
return convert(condassign);
|
return convert(condassign);
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException(expression.getClass().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ public class TypeGenerator {
|
|||||||
switch (typeContext.primitiveType().getText()) {
|
switch (typeContext.primitiveType().getText()) {
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart());
|
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart());
|
||||||
|
case "char":
|
||||||
|
return new RefType(ASTFactory.createClass(Character.class).getClassName(), typeContext.getStart());
|
||||||
case "int":
|
case "int":
|
||||||
return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart());
|
return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart());
|
||||||
case "double":
|
case "double":
|
||||||
@@ -71,7 +73,7 @@ public class TypeGenerator {
|
|||||||
case "float":
|
case "float":
|
||||||
return new RefType(ASTFactory.createClass(Float.class).getClassName(), typeContext.getStart());
|
return new RefType(ASTFactory.createClass(Float.class).getClassName(), typeContext.getStart());
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException(typeContext.primitiveType().getText());
|
||||||
}
|
}
|
||||||
} else if (!typeContext.LBRACK().isEmpty()) { // ArrayType über eckige Klammer prüfen
|
} else if (!typeContext.LBRACK().isEmpty()) { // ArrayType über eckige Klammer prüfen
|
||||||
// JavaTXParser.logger.info(unannTypeContext.getText());
|
// JavaTXParser.logger.info(unannTypeContext.getText());
|
||||||
|
|||||||
@@ -229,9 +229,6 @@ public class ASTFactory {
|
|||||||
public static Method createMethod(java.lang.reflect.Method jreMethod, String signature, java.lang.Class inClass, Boolean isInherited, Boolean isImplemented) {
|
public static Method createMethod(java.lang.reflect.Method jreMethod, String signature, java.lang.Class inClass, Boolean isInherited, Boolean isImplemented) {
|
||||||
String name = jreMethod.getName();
|
String name = jreMethod.getName();
|
||||||
RefTypeOrTPHOrWildcardOrGeneric returnType;
|
RefTypeOrTPHOrWildcardOrGeneric returnType;
|
||||||
if (inClass.getName().equals("Swap")){
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
Type jreRetType;
|
Type jreRetType;
|
||||||
if (jreMethod.getGenericReturnType() != null) {
|
if (jreMethod.getGenericReturnType() != null) {
|
||||||
jreRetType = jreMethod.getGenericReturnType();
|
jreRetType = jreMethod.getGenericReturnType();
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public class MethodCall extends Statement
|
|||||||
|
|
||||||
//sind Tphs, repraesentieren im Resultset die Signatur der aufgerufenen Methoden, letztes Element ist der Returntyp
|
//sind Tphs, repraesentieren im Resultset die Signatur der aufgerufenen Methoden, letztes Element ist der Returntyp
|
||||||
public final ArrayList<TypePlaceholder> signature;
|
public final ArrayList<TypePlaceholder> signature;
|
||||||
|
public int modifiers;
|
||||||
|
|
||||||
public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList,
|
public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList,
|
||||||
RefTypeOrTPHOrWildcardOrGeneric receiverType, ArrayList<TypePlaceholder> signature, Token offset){
|
RefTypeOrTPHOrWildcardOrGeneric receiverType, ArrayList<TypePlaceholder> signature, Token offset){
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.FunNGenerator;
|
|||||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
||||||
import de.dhbwstuttgart.exceptions.DebugException;
|
import de.dhbwstuttgart.exceptions.DebugException;
|
||||||
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.parser.NullToken;
|
import de.dhbwstuttgart.parser.NullToken;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.*;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
@@ -11,17 +12,17 @@ import de.dhbwstuttgart.syntaxtree.Record;
|
|||||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.*;
|
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator;
|
|
||||||
import de.dhbwstuttgart.target.tree.*;
|
import de.dhbwstuttgart.target.tree.*;
|
||||||
import de.dhbwstuttgart.target.tree.expression.*;
|
import de.dhbwstuttgart.target.tree.expression.*;
|
||||||
import de.dhbwstuttgart.target.tree.type.*;
|
import de.dhbwstuttgart.target.tree.type.*;
|
||||||
import de.dhbwstuttgart.typeinference.result.*;
|
import de.dhbwstuttgart.typeinference.result.*;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.*;
|
||||||
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dholle
|
* @author dholle
|
||||||
@@ -61,10 +62,14 @@ public class ASTToTargetAST {
|
|||||||
return converter.result;
|
return converter.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Generics(JavaGenerics javaGenerics, TxGenerics txGenerics) {
|
public record Generics(IGenerics javaGenerics, IGenerics txGenerics) {
|
||||||
public Generics(JavaTXCompiler compiler, ResultSet set) {
|
public Generics(JavaTXCompiler compiler, ResultSet set) {
|
||||||
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Generics nullGenerics() {
|
||||||
|
return new Generics(null, new ResultSet(Set.of()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IByteArrayClassLoader classLoader;
|
public IByteArrayClassLoader classLoader;
|
||||||
@@ -94,10 +99,15 @@ public class ASTToTargetAST {
|
|||||||
tphsInMethods.put(currentMethod, set);
|
tphsInMethods.put(currentMethod, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, JavaTXCompiler compiler) {
|
||||||
|
return findMethod(owner, name, argumentList, Generics.nullGenerics().javaGenerics(), compiler);
|
||||||
|
}
|
||||||
|
|
||||||
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, IGenerics generics, JavaTXCompiler compiler) {
|
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, IGenerics generics, JavaTXCompiler compiler) {
|
||||||
Optional<Method> method = Optional.empty();
|
Optional<Method> method = Optional.empty();
|
||||||
while (method.isEmpty()) {
|
while (method.isEmpty()) {
|
||||||
method = owner.getMethods().stream().filter(m -> m.name.equals(name) && parameterEquals(m.getParameterList(), argumentList, generics)).findFirst();
|
method = owner.getMethods().stream().filter(m -> m.name.equals(name) &&
|
||||||
|
parameterEquals(m.getParameterList().getFormalparalist().stream().map(p -> generics.getTargetType(p.getType())).toList(), argumentList)).findFirst();
|
||||||
if (owner.getClassName().toString().equals("java.lang.Object")) break;
|
if (owner.getClassName().toString().equals("java.lang.Object")) break;
|
||||||
owner = compiler.getClass(owner.getSuperClass().getName());
|
owner = compiler.getClass(owner.getSuperClass().getName());
|
||||||
}
|
}
|
||||||
@@ -105,16 +115,16 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Optional<Constructor> findConstructor(ClassOrInterface owner, List<TargetType> argumentList, IGenerics generics) {
|
Optional<Constructor> findConstructor(ClassOrInterface owner, List<TargetType> argumentList, IGenerics generics) {
|
||||||
return owner.getConstructors().stream().filter(c -> parameterEquals(c.getParameterList(), argumentList, generics)).findFirst();
|
return owner.getConstructors().stream().filter(c ->
|
||||||
|
parameterEquals(c.getParameterList().getFormalparalist().stream().map(p -> generics.getTargetType(p.getType())).toList(), argumentList)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean parameterEquals(ParameterList parameterList, List<TargetType> arguments, IGenerics generics) {
|
static boolean parameterEquals(List<TargetType> pars, List<TargetType> arguments) {
|
||||||
var pars = parameterList.getFormalparalist();
|
|
||||||
if (pars.size() != arguments.size())
|
if (pars.size() != arguments.size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (var i = 0; i < pars.size(); i++) {
|
for (var i = 0; i < pars.size(); i++) {
|
||||||
var type1 = generics.getTargetType(pars.get(i).getType());
|
var type1 = pars.get(i);
|
||||||
var type2 = arguments.get(i);
|
var type2 = arguments.get(i);
|
||||||
if (type1 instanceof TargetGenericType)
|
if (type1 instanceof TargetGenericType)
|
||||||
return true;
|
return true;
|
||||||
@@ -147,190 +157,168 @@ public class ASTToTargetAST {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This finds a common sealed interface type to group together methods that use different records
|
private static UnifyType toUnifyType(TargetType a) {
|
||||||
// This function should do matching or unification
|
return switch(a) {
|
||||||
private List<ClassOrInterface> commonSuperInterfaceTypes(TargetType a, TargetType b) {
|
case TargetExtendsWildcard targetExtendsWildcard -> new ExtendsType(toUnifyType(targetExtendsWildcard.innerType()));
|
||||||
if (a instanceof TargetGenericType && b instanceof TargetGenericType) return List.of(ASTFactory.createObjectClass());
|
case TargetSuperWildcard targetSuperWildcard -> new SuperType(toUnifyType(targetSuperWildcard.innerType()));
|
||||||
if (a instanceof TargetRefType ta && b instanceof TargetGenericType)
|
case TargetGenericType targetGenericType -> new PlaceholderType(targetGenericType.name(), JavaTXCompiler.defaultClientPlaceholderRegistry);
|
||||||
return List.of(compiler.getClass(new JavaClassName(ta.name())));
|
case TargetPrimitiveType _ -> throw new NotImplementedException();
|
||||||
if (b instanceof TargetRefType tb && a instanceof TargetGenericType)
|
case TargetFunNType targetFunNType -> FunNType.getFunNType(new TypeParams(targetFunNType.params().stream().map(ASTToTargetAST::toUnifyType).toList()));
|
||||||
return List.of(compiler.getClass(new JavaClassName(tb.name())));
|
case TargetRefType targetRefType -> new ReferenceType(targetRefType.name(), new TypeParams(targetRefType.params().stream().map(ASTToTargetAST::toUnifyType).toList()));
|
||||||
|
};
|
||||||
if (a instanceof TargetRefType ta && b instanceof TargetRefType tb) {
|
|
||||||
var res = new HashSet<ClassOrInterface>();
|
|
||||||
|
|
||||||
var cla = compiler.getClass(new JavaClassName(ta.name()));
|
|
||||||
var clb = compiler.getClass(new JavaClassName(tb.name()));
|
|
||||||
|
|
||||||
if (cla.equals(clb)) return List.of(cla);
|
|
||||||
|
|
||||||
while (!cla.equals(ASTFactory.createObjectClass())) {
|
|
||||||
var clb2 = clb;
|
|
||||||
while (!clb2.equals(ASTFactory.createObjectClass())) {
|
|
||||||
for (var intfa : cla.getSuperInterfaces()) {
|
|
||||||
for (var intfb : clb.getSuperInterfaces()) {
|
|
||||||
if (intfa.equals(intfb)) {
|
|
||||||
var clintf = compiler.getClass(intfa.getName());
|
|
||||||
if (clintf.isSealed()) {
|
|
||||||
res.add(clintf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clb2 = compiler.getClass(clb2.getSuperClass().getName());
|
|
||||||
}
|
|
||||||
cla = compiler.getClass(cla.getSuperClass().getName());
|
|
||||||
}
|
|
||||||
return res.stream().toList();
|
|
||||||
}
|
|
||||||
return List.of();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canCombine(Signature m1, Signature m2) {
|
private static TargetType toTargetType(UnifyType a) {
|
||||||
var pl1 = m1.java.parameters();
|
return switch (a) {
|
||||||
var pl2 = m2.java.parameters();
|
case ExtendsType extendType -> new TargetExtendsWildcard(toTargetType(extendType.getExtendedType()));
|
||||||
if (pl1.size() != pl2.size()) return false;
|
case SuperType superType -> new TargetSuperWildcard(toTargetType(superType.getSuperedType()));
|
||||||
if (pl1.isEmpty()) return false;
|
case PlaceholderType placeholderType -> new TargetGenericType(placeholderType.getName());
|
||||||
for (var i = 0; i < pl1.size(); i++) {
|
case FunNType funNType -> TargetFunNType.fromParams(StreamSupport.stream(funNType.getTypeParams().spliterator(), false).map(ASTToTargetAST::toTargetType).toList(), 1); // FIXME How does this work with Fun0??
|
||||||
var p1 = pl1.get(i).pattern();
|
case ReferenceType referenceType -> new TargetRefType(referenceType.getName(), StreamSupport.stream(referenceType.getTypeParams().spliterator(), false).map(ASTToTargetAST::toTargetType).toList());
|
||||||
var p2 = pl2.get(i).pattern();
|
default -> throw new NotImplementedException();
|
||||||
// TPH <> RefType sind nicht unterscheidbar
|
};
|
||||||
if (p1.type() instanceof TargetGenericType || p2.type() instanceof TargetGenericType) continue;
|
|
||||||
// Pattern(X) <> Pattern(Y) ist nicht unterscheidbar
|
|
||||||
if (p1 instanceof TargetComplexPattern pc1 && p2 instanceof TargetComplexPattern pc2 &&
|
|
||||||
pc1.type().equals(pc2.type())) continue;
|
|
||||||
if (!p1.equals(p2)) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private record Combination(MethodWithTphs a, MethodWithTphs b) {
|
private static Optional<TargetType> unify(TargetType a, TargetType b) {
|
||||||
@Override
|
if (a.equals(b)) return Optional.of(a);
|
||||||
public boolean equals(Object o) {
|
var unify = new MartelliMontanariUnify();
|
||||||
if (!(o instanceof Combination(MethodWithTphs a1, MethodWithTphs b1))) return false;
|
var ua = toUnifyType(a);
|
||||||
return this.a.equals(a1) && this.b.equals(b1) ||
|
var unifier = unify.unify(Set.of(ua, toUnifyType(b)));
|
||||||
this.a.equals(b1) && this.b.equals(a1);
|
if (unifier.isEmpty()) return Optional.empty();
|
||||||
|
return Optional.of(toTargetType(unifier.get().apply(ua)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private static Optional<List<MethodParameter>> unify(List<MethodParameter> a, List<MethodParameter> b) {
|
||||||
public int hashCode() {
|
if (a.size() != b.size()) return Optional.empty();
|
||||||
return Objects.hashCode(a) + Objects.hashCode(b);
|
var result = new ArrayList<MethodParameter>();
|
||||||
|
for (var i = 0; i < a.size(); i++) {
|
||||||
|
var u = unify(a.get(i).pattern().type(), b.get(i).pattern().type());
|
||||||
|
if (u.isEmpty()) return Optional.empty();
|
||||||
|
// Strip off patterns, we don't need them for merged methods, they do a switch case
|
||||||
|
result.add(new MethodParameter(u.get(), a.get(i).pattern().name()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.of(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<String> findUsedGenerics(TargetType type) {
|
||||||
|
if (type instanceof TargetSpecializedType tspec) {
|
||||||
|
return tspec.params().stream().map(ASTToTargetAST::findUsedGenerics).flatMap(Set::stream).collect(Collectors.toSet());
|
||||||
|
} else if (type instanceof TargetGenericType(String name)) {
|
||||||
|
var set = new HashSet<String>();
|
||||||
|
set.add(name);
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
return new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// When unifying types we need to keep the generics that are still used in the signature
|
||||||
|
// This is done in an extra step after all generics have been combined into one list
|
||||||
|
private static void correctGenerics(TargetMethod.Signature signature, List<TargetGeneric> generics) {
|
||||||
|
var outGenerics = signature.generics();
|
||||||
|
outGenerics.addAll(generics);
|
||||||
|
var foundGenerics = signature.parameters().stream().map(p -> findUsedGenerics(p.pattern().type()))
|
||||||
|
.flatMap(Set::stream)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
foundGenerics.addAll(findUsedGenerics(signature.returnType()));
|
||||||
|
|
||||||
|
for (var generic : new HashSet<>(outGenerics)) {
|
||||||
|
if (!foundGenerics.contains(generic.name())) outGenerics.remove(generic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<List<MethodWithTphs>> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
private static Optional<TargetMethod.Signature> unify(TargetMethod.Signature a, TargetMethod.Signature b) {
|
||||||
var mapOfTargetMethods = new HashMap<Generics, MethodWithTphs[]>();
|
if (a.equals(b)) return Optional.empty();
|
||||||
for (var gen : all) {
|
var res = unify(a.parameters(), b.parameters());
|
||||||
mapOfTargetMethods.put(gen, new MethodWithTphs[methods.size()]);
|
if (res.isEmpty()) return Optional.empty();
|
||||||
|
var returnType = unify(a.returnType(), b.returnType());
|
||||||
|
if (returnType.isEmpty()) return Optional.empty();
|
||||||
|
var generics = new ArrayList<>(a.generics());
|
||||||
|
generics.addAll(b.generics());
|
||||||
|
var signature = new TargetMethod.Signature(new HashSet<>(), res.get(), returnType.get());
|
||||||
|
correctGenerics(signature, generics);
|
||||||
|
return Optional.of(signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Optional<MethodGroup> unify(MethodGroup a, MethodGroup b) {
|
||||||
|
var junified = unify(a.signature.java, b.signature.java);
|
||||||
|
if (junified.isEmpty()) return Optional.empty();
|
||||||
|
var txunified = unify(a.signature.tx, b.signature.tx);
|
||||||
|
assert txunified.isPresent();
|
||||||
|
|
||||||
|
var unifiedSignature = new Signature(junified.get(), txunified.get(), a.signature().generics);
|
||||||
|
|
||||||
|
var concat = new HashSet<>(a.methods);
|
||||||
|
concat.addAll(b.methods);
|
||||||
|
return Optional.of(new MethodGroup(unifiedSignature, concat));
|
||||||
|
}
|
||||||
|
|
||||||
|
private record NameAndParameters(String name, int parameters) {}
|
||||||
|
|
||||||
|
private List<List<Method>> groupMethods(ClassOrInterface input, List<Method> methods) {
|
||||||
|
var groups = new HashMap<NameAndParameters, List<Method>>();
|
||||||
|
for (var m : methods) {
|
||||||
|
var nameAndParameters = new NameAndParameters(m.name, m.getParameterList().getFormalparalist().size());
|
||||||
|
var l = groups.getOrDefault(nameAndParameters, new ArrayList<>());
|
||||||
|
l.add(m);
|
||||||
|
groups.put(nameAndParameters, l);
|
||||||
|
}
|
||||||
|
|
||||||
|
return groups.values().stream().toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<MethodGroup> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
||||||
|
var a = new HashSet<MethodGroup>();
|
||||||
|
|
||||||
for (var i = 0; i < methods.size(); i++) {
|
for (var i = 0; i < methods.size(); i++) {
|
||||||
var method = methods.get(i);
|
var method = methods.get(i);
|
||||||
// Convert all methods
|
// Convert all methods
|
||||||
var methodsWithTphs = convert(input, method);
|
var methodsWithTphs = convert(input, method);
|
||||||
for (var m : methodsWithTphs) {
|
for (var m : methodsWithTphs) {
|
||||||
var resultMethods = mapOfTargetMethods.get(m.generics);
|
var mtph = new MethodWithTphs(m.method, m.generics, m.signature);
|
||||||
resultMethods[i] = new MethodWithTphs(m.method, m.generics, m.signature);
|
a.add(new MethodGroup(m.signature, Set.of(mtph)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("============== INPUT ==============");
|
System.out.println("============== INPUT ==============");
|
||||||
for (var m : mapOfTargetMethods.values()) {
|
for (var m : a) {
|
||||||
for (var v : m) if (v != null) System.out.println(v.signature.java.returnType() + " " + v.method.name + " " + v.signature.java().parameters());
|
System.out.println(m);
|
||||||
System.out.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var allCombinations = new HashSet<Set<Combination>>();
|
// Algorithm
|
||||||
// Combine methods based on their signature and position in the result set
|
|
||||||
for (var g1 : all) {
|
|
||||||
var resMeth1 = mapOfTargetMethods.get(g1);
|
|
||||||
for (var i = 0; i < methods.size(); i++) {
|
|
||||||
var m1 = resMeth1[i];
|
|
||||||
if (m1 == null) continue;
|
|
||||||
|
|
||||||
for (var g2 : all) {
|
var R = new HashSet<MethodGroup>();
|
||||||
if (g1 == g2) continue; // No need to combine the same method
|
var i = new HashSet<>(a);
|
||||||
var resMeth2 = mapOfTargetMethods.get(g2);
|
|
||||||
var m2 = resMeth2[i];
|
|
||||||
if (m2 == null) continue;
|
|
||||||
|
|
||||||
var combinations = new HashSet<Combination>();
|
while (!i.isEmpty()) {
|
||||||
|
var m = i.iterator().next();
|
||||||
if (canCombine(m1.signature, m2.signature)) {
|
i.remove(m);
|
||||||
//System.out.println(" Combining " + m1.signature.java.getSignature() + " and " + m2.signature.java.getSignature());
|
R.add(m);
|
||||||
combinations.add(new Combination(m1, m2));
|
var a1 = new HashSet<>(a);
|
||||||
for (var j = 0; j < methods.size(); j++) {
|
for (var m1 : a1) {
|
||||||
if (j == i) continue;
|
if (m1 != m) {
|
||||||
var m3 = resMeth2[j];
|
var u_opt = unify(m, m1);
|
||||||
if (m3 == null) continue;
|
if (u_opt.isPresent()) {
|
||||||
var m4 = resMeth1[j];
|
var u = u_opt.get();
|
||||||
if (m4 == null) continue;
|
//System.out.println("Unified " + m + " AND " + m1 + "\n\t" + u);
|
||||||
combinations.add(new Combination(m4, m3));
|
i.remove(m1);
|
||||||
//System.out.println("Also Combining " + m4.signature.java.getSignature() + " and " + m3.signature.java.getSignature());
|
R.remove(m);
|
||||||
}
|
R.remove(m1);
|
||||||
} else {
|
R.add(u);
|
||||||
//System.out.println(" Not Combining " + m1.signature.java.getSignature() + " and " + m2.signature.java.getSignature());
|
a.add(u);
|
||||||
}
|
} /*else {
|
||||||
if (!combinations.isEmpty()) allCombinations.add(combinations);
|
System.out.println("Couldn't unify " + m + " AND " + m1);
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allCombinations.isEmpty()) allCombinations.add(new HashSet<>());
|
System.out.println("============== OUTPUT ==============");
|
||||||
|
for (var mg : R) {
|
||||||
// Combine back into output format
|
System.out.println(mg.methods.size() + " " + mg);
|
||||||
var r0 = new HashSet<Set<MethodWithTphs>>();
|
|
||||||
for (var combinations : allCombinations) {
|
|
||||||
var r1 = new HashSet<Set<MethodWithTphs>>();
|
|
||||||
// This is used to weed out duplicates
|
|
||||||
var uniqued = new HashSet<MethodWithTphs>();
|
|
||||||
// We go over all methods in the result
|
|
||||||
for (var g : all) for (var i = 0; i < methods.size(); i++) {
|
|
||||||
var r2 = new HashSet<MethodWithTphs>();
|
|
||||||
var m = mapOfTargetMethods.get(g)[i];
|
|
||||||
if (m == null) continue;
|
|
||||||
if (!uniqued.contains(m)) {
|
|
||||||
// Add the method to r2
|
|
||||||
r2.add(m);
|
|
||||||
uniqued.add(m);
|
|
||||||
} else continue;
|
|
||||||
// Find all combinations that contain the method and add them to the result
|
|
||||||
// if not filtered out by uniqued
|
|
||||||
for (var c : combinations) {
|
|
||||||
if (c.a.equals(m) || c.b.equals(m)) {
|
|
||||||
if (!uniqued.contains(c.a)) {
|
|
||||||
r2.add(c.a);
|
|
||||||
uniqued.add(c.a);
|
|
||||||
}
|
|
||||||
if (!uniqued.contains(c.b)) {
|
|
||||||
r2.add(c.b);
|
|
||||||
uniqued.add(c.b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r1.add(r2);
|
|
||||||
}
|
|
||||||
outer: for (var s1 : r1) {
|
|
||||||
for (var s2 : new HashSet<>(r0)) {
|
|
||||||
if (s2.containsAll(s1)) {
|
|
||||||
continue outer;
|
|
||||||
} else if (s1.containsAll(s2)) {
|
|
||||||
r0.remove(s2);
|
|
||||||
r0.add(s1);
|
|
||||||
continue outer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r0.add(s1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = r0.stream().map(l -> l.stream().toList()).toList();
|
return R.stream().toList();
|
||||||
|
|
||||||
//System.out.println("============== OUTPUT ==============");
|
|
||||||
//for (var l : result) {
|
|
||||||
// for (var m : l) System.out.println(m.method.name + " " + m.signature.java.getSignature());
|
|
||||||
// System.out.println();
|
|
||||||
//}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TargetStructure convert(ClassOrInterface input) {
|
public TargetStructure convert(ClassOrInterface input) {
|
||||||
@@ -364,13 +352,15 @@ public class ASTToTargetAST {
|
|||||||
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics, compiler)).toList();
|
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics, compiler)).toList();
|
||||||
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer, generics)).flatMap(List::stream).toList();
|
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer, generics)).flatMap(List::stream).toList();
|
||||||
var fields = input.getFieldDecl().stream().map(f -> convert(f, generics.javaGenerics)).toList();
|
var fields = input.getFieldDecl().stream().map(f -> convert(f, generics.javaGenerics)).toList();
|
||||||
var methods = groupOverloads(input, input.getMethods()).stream().map(m -> generatePatternOverloads(input, m)).flatMap(List::stream)
|
var m0 = groupMethods(input, input.getMethods());
|
||||||
.collect(Collectors.toSet()).stream().toList(); // Unique generated methods
|
var m1 = m0.stream().map(ms ->
|
||||||
|
groupOverloads(input, ms).stream().map(g -> generatePatternOverloads(input, g)).flatMap(List::stream).toList()).toList();
|
||||||
|
var methods = m1.stream().flatMap(List::stream).collect(Collectors.toSet()).stream().toList();
|
||||||
|
|
||||||
TargetMethod staticConstructor = null;
|
TargetMethod staticConstructor = null;
|
||||||
if (input.getStaticInitializer().isPresent()) {
|
if (input.getStaticInitializer().isPresent()) {
|
||||||
var init = this.convert(input, input.getStaticInitializer().get()).stream().findFirst().orElseThrow();
|
var init = this.convert(input, input.getStaticInitializer().get()).stream().findFirst().orElseThrow();
|
||||||
staticConstructor = this.convert(init, init.generics.javaGenerics);
|
staticConstructor = this.convert(init);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input instanceof Record)
|
if (input instanceof Record)
|
||||||
@@ -380,9 +370,15 @@ public class ASTToTargetAST {
|
|||||||
else return new TargetClass(input.getModifiers(), input.getClassName(), convert(input.getSuperClass(), generics.javaGenerics, compiler), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
|
else return new TargetClass(input.getModifiers(), input.getClassName(), convert(input.getSuperClass(), generics.javaGenerics, compiler), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public List<MethodParameter> convert(ParameterList input) {
|
public List<MethodParameter> convert(ParameterList input) {
|
||||||
return convert(input, all.getFirst().javaGenerics);
|
var res = new ArrayList<MethodParameter>();
|
||||||
|
for (var i = 0; i < input.getFormalparalist().size(); i++) {
|
||||||
|
var param = input.getFormalparalist().get(i);
|
||||||
|
var pattern = (TargetPattern) convert(param, Generics.nullGenerics().javaGenerics);
|
||||||
|
if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i);
|
||||||
|
res.add(new MethodParameter(pattern));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<MethodParameter> convert(ParameterList input, IGenerics generics) {
|
public List<MethodParameter> convert(ParameterList input, IGenerics generics) {
|
||||||
@@ -542,38 +538,13 @@ public class ASTToTargetAST {
|
|||||||
return new TargetSwitch(switchExpr, cases, null, true);
|
return new TargetSwitch(switchExpr, cases, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, List<MethodWithTphs> overloadedMethods) {
|
private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, MethodGroup group) {
|
||||||
|
var overloadedMethods = group.methods.stream().toList();
|
||||||
if (overloadedMethods.isEmpty()) return List.of();
|
if (overloadedMethods.isEmpty()) return List.of();
|
||||||
// Check if we have a pattern as a parameter
|
// Check if we have a pattern as a parameter
|
||||||
var firstMethod = convert(overloadedMethods.getFirst(), overloadedMethods.getFirst().generics.javaGenerics);
|
var firstMethod = convert(overloadedMethods.getFirst());
|
||||||
if (overloadedMethods.size() == 1) return List.of(firstMethod);
|
if (overloadedMethods.size() == 1) return List.of(firstMethod);
|
||||||
var secondMethod = convert(overloadedMethods.get(1), overloadedMethods.get(1).generics.javaGenerics);
|
var signatureParams = group.signature.java.parameters();
|
||||||
if (firstMethod.signature().parameters().stream().noneMatch(mp -> mp.pattern() instanceof TargetComplexPattern))
|
|
||||||
return overloadedMethods.stream().map(m -> convert(m, m.generics.javaGenerics)).toList();
|
|
||||||
|
|
||||||
var signatureParams = new ArrayList<MethodParameter>();
|
|
||||||
for (var i = 0; i < firstMethod.signature().parameters().size(); i++) {
|
|
||||||
var p1 = firstMethod.signature().parameters().get(i).pattern();
|
|
||||||
var t1 = p1.type();
|
|
||||||
var t2 = secondMethod.signature().parameters().get(i).pattern().type();
|
|
||||||
var commonSubTypes = new HashSet<>(commonSuperInterfaceTypes(t1, t2));
|
|
||||||
for (var m : overloadedMethods.subList(2, overloadedMethods.size())) {
|
|
||||||
var t3 = m.signature().java.parameters().get(i).pattern().type();
|
|
||||||
commonSubTypes.retainAll(commonSuperInterfaceTypes(t1, t3));
|
|
||||||
}
|
|
||||||
if (commonSubTypes.size() > 1) throw new DebugException("Invalid overload");
|
|
||||||
// TODO accept multiple types
|
|
||||||
var superType = ASTFactory.createObjectClass();
|
|
||||||
if (!commonSubTypes.isEmpty())
|
|
||||||
superType = commonSubTypes.iterator().next();
|
|
||||||
|
|
||||||
String name;
|
|
||||||
if (p1 instanceof TargetComplexPattern) name = "__var" + i;
|
|
||||||
else name = p1.name();
|
|
||||||
signatureParams.add(new MethodParameter(new TargetRefType(superType.getClassName().toString()), name));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rename existing methods
|
|
||||||
|
|
||||||
var res = new ArrayList<TargetMethod>();
|
var res = new ArrayList<TargetMethod>();
|
||||||
for (var method : overloadedMethods) {
|
for (var method : overloadedMethods) {
|
||||||
@@ -592,12 +563,6 @@ public class ASTToTargetAST {
|
|||||||
res.add(new TargetMethod(tMethod.access(), name, tMethod.block(), tMethod.signature(), tMethod.txSignature()));
|
res.add(new TargetMethod(tMethod.access(), name, tMethod.block(), tMethod.signature(), tMethod.txSignature()));
|
||||||
}
|
}
|
||||||
|
|
||||||
var commonSubTypes = new HashSet<>(commonSuperInterfaceTypes(firstMethod.signature().returnType(), secondMethod.signature().returnType()));
|
|
||||||
for (var m : overloadedMethods.subList(2, overloadedMethods.size())) {
|
|
||||||
commonSubTypes.retainAll(commonSuperInterfaceTypes(firstMethod.signature().returnType(), m.signature().java.returnType()));
|
|
||||||
}
|
|
||||||
var returnType = commonSubTypes.isEmpty() ? TargetType.Object : new TargetRefType(commonSubTypes.iterator().next().getClassName().toString());
|
|
||||||
|
|
||||||
var parameters = signatureParams.stream().map( p -> new TargetLocalVar(p.pattern().type(), p.pattern().name())).toList();
|
var parameters = signatureParams.stream().map( p -> new TargetLocalVar(p.pattern().type(), p.pattern().name())).toList();
|
||||||
//var patterns = List.of((TargetComplexPattern) firstMethod.signature().parameters().stream()
|
//var patterns = List.of((TargetComplexPattern) firstMethod.signature().parameters().stream()
|
||||||
// .filter(p -> p.pattern() instanceof TargetComplexPattern).findFirst().orElseThrow().pattern());
|
// .filter(p -> p.pattern() instanceof TargetComplexPattern).findFirst().orElseThrow().pattern());
|
||||||
@@ -607,9 +572,7 @@ public class ASTToTargetAST {
|
|||||||
var stmt = generatePatternOverloadsRec(0, new TargetLocalVar(signatureParams.getFirst().pattern().type(), signatureParams.getFirst().pattern().name()), parameters, List.of(), res, classType);
|
var stmt = generatePatternOverloadsRec(0, new TargetLocalVar(signatureParams.getFirst().pattern().type(), signatureParams.getFirst().pattern().name()), parameters, List.of(), res, classType);
|
||||||
var block = new TargetBlock(List.of(stmt));
|
var block = new TargetBlock(List.of(stmt));
|
||||||
|
|
||||||
var signature = new TargetMethod.Signature(Set.of(), signatureParams, returnType);
|
var bridgeMethod = new TargetMethod(firstMethod.access(), firstMethod.name(), block, group.signature.java, group.signature.tx);
|
||||||
var bridgeMethod = new TargetMethod(firstMethod.access(), firstMethod.name(), block, signature, firstMethod.txSignature());
|
|
||||||
|
|
||||||
res.add(bridgeMethod);
|
res.add(bridgeMethod);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -655,6 +618,15 @@ public class ASTToTargetAST {
|
|||||||
}).findFirst();
|
}).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record MethodGroup(Signature signature, Set<MethodWithTphs> methods) {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
assert !methods.isEmpty();
|
||||||
|
var first = methods.iterator().next();
|
||||||
|
return signature.java.returnType() + " " + first.method.name + " " + signature.java.generics() + " " + signature.java.parameters();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
record MethodWithTphs(Method method, Generics generics, Signature signature) {
|
record MethodWithTphs(Method method, Generics generics, Signature signature) {
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
@@ -669,14 +641,30 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TargetMethod convert(MethodWithTphs mtph) {
|
||||||
|
return convert(mtph, mtph.generics.javaGenerics);
|
||||||
|
}
|
||||||
|
|
||||||
private TargetMethod convert(MethodWithTphs mtph, IGenerics generics) {
|
private TargetMethod convert(MethodWithTphs mtph, IGenerics generics) {
|
||||||
return new TargetMethod(mtph.method.modifier, mtph.method.name, convert(mtph.method.block, generics), mtph.signature.java(), mtph.signature.tx());
|
return new TargetMethod(mtph.method.modifier, mtph.method.name, convert(mtph.method.block, generics), mtph.signature.java(), mtph.signature.tx());
|
||||||
}
|
}
|
||||||
|
|
||||||
record Signature(TargetMethod.Signature java, TargetMethod.Signature tx, Generics generics) {}
|
record Signature(TargetMethod.Signature java, TargetMethod.Signature tx, Generics generics) {
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (this == other) return true;
|
||||||
|
if (!(other instanceof Signature that)) return false;
|
||||||
|
return Objects.equals(this.java, that.java) && Objects.equals(this.tx, that.tx);
|
||||||
|
}
|
||||||
|
|
||||||
private List<MethodWithTphs> convert(ClassOrInterface currentClass, Method method) {
|
@Override
|
||||||
List<MethodWithTphs> result = new ArrayList<>();
|
public int hashCode() {
|
||||||
|
return Objects.hash(java, tx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<MethodWithTphs> convert(ClassOrInterface currentClass, Method method) {
|
||||||
|
Set<MethodWithTphs> result = new HashSet<>();
|
||||||
this.currentMethod = method;
|
this.currentMethod = method;
|
||||||
|
|
||||||
List<Signature> signatures = new ArrayList<>();
|
List<Signature> signatures = new ArrayList<>();
|
||||||
@@ -701,6 +689,7 @@ public class ASTToTargetAST {
|
|||||||
var txMethodGenerics = collectMethodGenerics(currentClass, generics.txGenerics(), txGenerics, method);
|
var txMethodGenerics = collectMethodGenerics(currentClass, generics.txGenerics(), txGenerics, method);
|
||||||
|
|
||||||
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
|
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
|
||||||
|
System.out.println(javaSignature.getDescriptor());
|
||||||
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), generics.txGenerics, compiler));
|
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), generics.txGenerics, compiler));
|
||||||
|
|
||||||
signatures.add(new Signature(javaSignature, txSignature, generics));
|
signatures.add(new Signature(javaSignature, txSignature, generics));
|
||||||
@@ -754,7 +743,7 @@ public class ASTToTargetAST {
|
|||||||
for (var i = 0; i < tspec.params().size(); i++) {
|
for (var i = 0; i < tspec.params().size(); i++) {
|
||||||
var param = tspec.params().get(i);
|
var param = tspec.params().get(i);
|
||||||
if (param instanceof TargetSpecializedType fn) {
|
if (param instanceof TargetSpecializedType fn) {
|
||||||
collectArguments(tspec, newParams);
|
collectArguments(fn, newParams);
|
||||||
} else {
|
} else {
|
||||||
newParams.add(param);
|
newParams.add(param);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -216,6 +216,10 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
return ASTToTargetAST.findMethod(converter.compiler.getClass(className), name, args, generics, compiler);
|
return ASTToTargetAST.findMethod(converter.compiler.getClass(className), name, args, generics, compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<Method> findMethod(JavaClassName className, String name, List<TargetType> args, JavaTXCompiler compiler) {
|
||||||
|
return ASTToTargetAST.findMethod(converter.compiler.getClass(className), name, args, compiler);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MethodCall methodCall) {
|
public void visit(MethodCall methodCall) {
|
||||||
var receiverType = converter.convert(methodCall.receiver.getType(), generics);
|
var receiverType = converter.convert(methodCall.receiver.getType(), generics);
|
||||||
@@ -246,7 +250,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
} else if (!isFunNType) {
|
} else if (!isFunNType) {
|
||||||
receiverClass = converter.compiler.getClass(receiverName);
|
receiverClass = converter.compiler.getClass(receiverName);
|
||||||
if (receiverClass == null) throw new DebugException("Class " + receiverName + " does not exist!");
|
if (receiverClass == null) throw new DebugException("Class " + receiverName + " does not exist!");
|
||||||
foundMethod = findMethod(receiverName, methodCall.name, signature, generics, converter.compiler).orElseThrow();
|
foundMethod = findMethod(receiverName, methodCall.name, signature, converter.compiler).orElseThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isFunNType) {
|
if (!isFunNType) {
|
||||||
|
|||||||
@@ -3,4 +3,9 @@ package de.dhbwstuttgart.target.tree;
|
|||||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
public record TargetGeneric(String name, TargetType bound) {
|
public record TargetGeneric(String name, TargetType bound) {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if (bound.equals(TargetType.Object)) return "'" + name;
|
||||||
|
else return "'" + name + " < " + bound;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,12 +85,11 @@ public class Constraint<A extends IConstraintElement> extends HashSet<A> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString();
|
return super.toString() + "\nisInherited = " + isInherited
|
||||||
// + "\nisInherited = " + isInherited
|
+ " isOveridden = " + isImplemented
|
||||||
// + " isOveridden = " + isImplemented
|
+ " msc[" + methodSignatureConstraint.size() + "] = " + methodSignatureConstraint
|
||||||
// + " msc[" + methodSignatureConstraint.size() + "] = " + methodSignatureConstraint
|
//" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" )
|
||||||
// //" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" )
|
+ "\n";
|
||||||
// + "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toStringBase() {
|
public String toStringBase() {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import de.dhbwstuttgart.server.packet.dataContainers.ISerializableData;
|
|||||||
import de.dhbwstuttgart.server.packet.dataContainers.KeyStorage;
|
import de.dhbwstuttgart.server.packet.dataContainers.KeyStorage;
|
||||||
import de.dhbwstuttgart.server.packet.dataContainers.serialized.SerialMap;
|
import de.dhbwstuttgart.server.packet.dataContainers.serialized.SerialMap;
|
||||||
import de.dhbwstuttgart.typeinference.unify.UnifyContext;
|
import de.dhbwstuttgart.typeinference.unify.UnifyContext;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -14,8 +13,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||||
|
|
||||||
import static de.dhbwstuttgart.core.JavaTXCompiler.q;
|
|
||||||
|
|
||||||
|
|
||||||
public class Pair implements Serializable, IConstraintElement, ISerializableData {
|
public class Pair implements Serializable, IConstraintElement, ISerializableData {
|
||||||
public final RefTypeOrTPHOrWildcardOrGeneric TA1;
|
public final RefTypeOrTPHOrWildcardOrGeneric TA1;
|
||||||
@@ -176,27 +173,5 @@ public class Pair implements Serializable, IConstraintElement, ISerializableData
|
|||||||
if (location != null) pair.location = SourceLoc.fromSerial(location);
|
if (location != null) pair.location = SourceLoc.fromSerial(location);
|
||||||
return pair;
|
return pair;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toDot(String color) {
|
|
||||||
|
|
||||||
return q(this.TA1.toString()) +
|
|
||||||
" -> " +
|
|
||||||
q(this.TA2.toString()) +
|
|
||||||
" [label=" +
|
|
||||||
q(this.GetOperator().toString()) +
|
|
||||||
",color=" + q(color) + "]" +
|
|
||||||
";\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toDot() {
|
|
||||||
|
|
||||||
return q(this.TA1.toString()) +
|
|
||||||
" -> " +
|
|
||||||
q(this.TA2.toString()) +
|
|
||||||
" [label=" +
|
|
||||||
q(this.GetOperator().toString()) +
|
|
||||||
"]" +
|
|
||||||
";\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// ino.end
|
// ino.end
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ public class TYPE {
|
|||||||
this.definedClasses = definedClasses;
|
this.definedClasses = definedClasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConstraintSet<Pair> getConstraints() {
|
public ConstraintSet getConstraints() {
|
||||||
ConstraintSet<Pair> ret = new ConstraintSet<>();
|
ConstraintSet ret = new ConstraintSet();
|
||||||
for (ClassOrInterface cl : definedClasses) {
|
for (ClassOrInterface cl : definedClasses) {
|
||||||
Set<ClassOrInterface> allClasses = TypeUnifyTaskHelper.getPresizedHashSet(allAvailableClasses.size());
|
Set<ClassOrInterface> allClasses = TypeUnifyTaskHelper.getPresizedHashSet(allAvailableClasses.size());
|
||||||
allClasses.addAll(allAvailableClasses);
|
allClasses.addAll(allAvailableClasses);
|
||||||
@@ -45,9 +45,9 @@ public class TYPE {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConstraintSet<Pair> getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) {
|
private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) {
|
||||||
ConstraintSet<Pair> ret = new ConstraintSet<>();
|
ConstraintSet ret = new ConstraintSet();
|
||||||
ConstraintSet<Pair> methConstrains;
|
ConstraintSet methConstrains;
|
||||||
for(Method m : cl.getMethods()){
|
for(Method m : cl.getMethods()){
|
||||||
ret.addAll(methConstrains = getConstraintsMethod(m,info, cl));
|
ret.addAll(methConstrains = getConstraintsMethod(m,info, cl));
|
||||||
m.constraints.addAll(methConstrains);
|
m.constraints.addAll(methConstrains);
|
||||||
@@ -86,11 +86,11 @@ public class TYPE {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private ConstraintSet<Pair> getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) {
|
private ConstraintSet getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) {
|
||||||
if(m.block == null)return new ConstraintSet<Pair>(); //Abstrakte Methoden generieren keine Constraints
|
if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints
|
||||||
TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m);
|
TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m);
|
||||||
TYPEStmt methodScope = new TYPEStmt(blockInfo);
|
TYPEStmt methodScope = new TYPEStmt(blockInfo);
|
||||||
ConstraintSet<Pair> constraintSet = new ConstraintSet<>();
|
ConstraintSet constraintSet = new ConstraintSet();
|
||||||
|
|
||||||
if (m.name.equals("main") && Modifier.isStatic(m.modifier) && m.getParameterList().getFormalparalist().size() == 1) {
|
if (m.name.equals("main") && Modifier.isStatic(m.modifier) && m.getParameterList().getFormalparalist().size() == 1) {
|
||||||
// Add constraint for main method
|
// Add constraint for main method
|
||||||
|
|||||||
@@ -959,6 +959,7 @@ public class TestComplete {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPatternMatchingZip() throws Exception {
|
public void testPatternMatchingZip() throws Exception {
|
||||||
|
//ConsoleInterface.logLevel = Logger.LogLevel.DEBUG;
|
||||||
var classFiles = generateClassFiles(createClassLoader(), "PatternMatching.jav");
|
var classFiles = generateClassFiles(createClassLoader(), "PatternMatching.jav");
|
||||||
var clazz = classFiles.get("PatternMatching");
|
var clazz = classFiles.get("PatternMatching");
|
||||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||||
@@ -966,7 +967,6 @@ public class TestComplete {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPatternMatchingZipJava() throws Exception {
|
public void testPatternMatchingZipJava() throws Exception {
|
||||||
ConsoleInterface.logLevel = Logger.LogLevel.DEBUG;
|
|
||||||
var classFiles = generateClassFiles(createClassLoader(), false, "PatternMatchingJava.jav", "PatternMatchingJava2.jav");
|
var classFiles = generateClassFiles(createClassLoader(), false, "PatternMatchingJava.jav", "PatternMatchingJava2.jav");
|
||||||
var clazz = classFiles.get("PatternMatchingJava");
|
var clazz = classFiles.get("PatternMatchingJava");
|
||||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||||
@@ -1536,10 +1536,24 @@ public class TestComplete {
|
|||||||
// TODO This logs output that we should validate
|
// TODO This logs output that we should validate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBug389() throws Exception {
|
||||||
|
var classFiles = generateClassFiles(createClassLoader(), false, "Bug389.jav", "Bug389Main.jav");
|
||||||
|
var clazz = classFiles.get("Bug389Main");
|
||||||
|
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBug390() throws Exception {
|
public void testBug390() throws Exception {
|
||||||
var classFiles = generateClassFiles(createClassLoader(), "Bug390.jav");
|
var classFiles = generateClassFiles(createClassLoader(), "Bug390.jav");
|
||||||
var clazz = classFiles.get("Bug390");
|
var clazz = classFiles.get("Bug390");
|
||||||
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBug392() throws Exception {
|
||||||
|
var classFiles = generateClassFiles(createClassLoader(), "Bug392.jav");
|
||||||
|
var clazz = classFiles.get("Bug392");
|
||||||
|
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user