Rename test file and some work on the overloading behavior
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 50s

This commit is contained in:
Daniel Holle 2024-11-07 17:04:43 +01:00
parent c33e372446
commit e2bf09548f
4 changed files with 43 additions and 21 deletions

View File

@ -5,13 +5,13 @@ import java.lang.Float;
public record Point(Number x, Number y) {}
public class OverloadPattern {
public m(Point(x, y), Point(z, a)) {
return x + y + z + a;
public Number m(Point(Integer x, Integer y)) {
return x + y;
}
/*public m(Point(Float x, Float y)) {
public Number m(Point(Float x, Float y)) {
return x * y;
}*/
}
public m(Integer x) {
return x;

View File

@ -5,7 +5,7 @@ sealed interface List<T> permits LinkedElem, Elem {}
public record LinkedElem<T>(T a, List<T> l) implements List<T> {}
public record Elem<T>(T a) implements List<T> {}
public class PaternMatchingHaskellStyle {
public class PatternMatchingHaskellStyle {
public append(LinkedElem(a, b), list2) {
return handleAppend(a, b, list2);

View File

@ -145,37 +145,60 @@ public class ASTToTargetAST {
return ret;
}
// This is used to serve as a custom equality to signature that performs a weak check without going into record patterns.
// The two signatures are considered equal if all the argument types match.
record WeakSignature(TargetMethod.Signature signature) {
@Override
public boolean equals(Object o) {
if (!(o instanceof WeakSignature other)) return false;
if (other.signature.parameters().size() != signature.parameters().size()) return false;
for (var i = 0; i < signature.parameters().size(); i++) {
var p1 = signature.parameters().get(i).pattern().type();
var p2 = other.signature.parameters().get(i).pattern().type();
if (!p1.equals(p2)) return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(signature.parameters().stream().map(p -> p.pattern().type()).toArray());
}
}
public List<List<TargetMethod>> groupOverloads(ClassOrInterface input, List<Method> methods) {
var res = new ArrayList<List<TargetMethod>>();
var mapOfSignatures = new HashMap<WeakSignature, List<MethodWithTphs>>();
for (var method : methods) {
// Convert all methods
var methodsWithTphs = convert(input, method);
// Then check for methods with the same signature
var mapOfSignatures = new HashMap<TargetMethod.Signature, List<MethodWithTphs>>();
for (var m : methodsWithTphs) {
var methodsWithSameSignature = mapOfSignatures.getOrDefault(m.method.signature(), new ArrayList<>());
var signature = new WeakSignature(m.method.signature());
var methodsWithSameSignature = mapOfSignatures.getOrDefault(signature, new ArrayList<>());
methodsWithSameSignature.add(m);
mapOfSignatures.put(m.method.signature(), methodsWithSameSignature);
mapOfSignatures.put(signature, methodsWithSameSignature);
}
}
for (var methodsWithSignature : mapOfSignatures.values()) {
var resMethods = new HashSet<TargetMethod>();
for (var methodsWithSignature : mapOfSignatures.values()) {
outer: for (var m1 : methodsWithSignature) {
for (var m2 : methodsWithSignature) {
for (var i = 0; i < m1.args.size(); i++) {
var arg1 = m1.args.get(i);
var arg2 = m2.args.get(i);
if (arg1.parameter.equals(arg2.parameter)) {
if (isSupertype(arg1.signature, arg2.signature) &&
outer: for (var m1 : methodsWithSignature) {
for (var m2 : methodsWithSignature) {
for (var i = 0; i < m1.args.size(); i++) {
var arg1 = m1.args.get(i);
var arg2 = m2.args.get(i);
if (arg1.parameter.equals(arg2.parameter)) {
if (isSupertype(arg1.signature, arg2.signature) &&
!arg1.signature.equals(arg2.signature)) continue outer;
}
}
}
resMethods.add(m1.method);
}
resMethods.add(m1.method);
}
res.add(resMethods.stream().toList());
}
return res;
}
@ -309,7 +332,6 @@ public class ASTToTargetAST {
}
// Generate dispatch method
return res;
}

View File

@ -857,8 +857,8 @@ public class TestComplete {
//@Ignore("Not implemented")
@Test
public void testOverloadPatternMatching() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PaternMatchingHaskellStyle.jav");
var clazz = classFiles.get("PaternMatchingHaskellStyle");
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav");
var clazz = classFiles.get("PatternMatchingHaskellStyle");
var R2 = classFiles.get("Elem");
var R = classFiles.get("LinkedElem");
var I = classFiles.get("List");