From 170955b333b761c3cd33a7d90b6787bc3b101a95 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Tue, 19 Nov 2024 15:19:40 +0100 Subject: [PATCH] More complex overloading for switch --- .../bytecode/javFiles/OverloadPattern.jav | 16 ++++++++--- .../de/dhbwstuttgart/bytecode/Codegen.java | 6 ++-- .../target/generate/ASTToTargetAST.java | 28 +++++++++++++------ .../target/tree/MethodParameter.java | 4 +++ .../tree/expression/TargetComplexPattern.java | 5 ++++ .../target/tree/expression/TargetGuard.java | 5 ++++ .../target/tree/expression/TargetPattern.java | 2 ++ .../tree/expression/TargetTypePattern.java | 5 ++++ src/test/java/TestComplete.java | 20 +++++++++---- 9 files changed, 71 insertions(+), 20 deletions(-) diff --git a/resources/bytecode/javFiles/OverloadPattern.jav b/resources/bytecode/javFiles/OverloadPattern.jav index 515d5f85..2bf0d351 100644 --- a/resources/bytecode/javFiles/OverloadPattern.jav +++ b/resources/bytecode/javFiles/OverloadPattern.jav @@ -6,19 +6,27 @@ public record Point(Number x, Number y) {} public class OverloadPattern { public Number m(Point(Integer x, Integer y), Point(Float a, Float b)) { - return x + y; + return 1; } public Number m(Point(Integer x, Integer y), Point(Integer a, Integer b)) { - return x + y; + return 2; } public Number m(Point(Float x, Float y), Point(Integer a, Integer b)) { - return x * y; + return 3; } public Number m(Point(Float x, Float y), Point(Float a, Float b)) { - return x * y; + return 4; + } + + public Number m(Point(Integer x, Integer y)) { + return 5; + } + + public Number m(Point(Float x, Float y)) { + return 6; } public m(Integer x) { diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 2e884fcd..43d47723 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1393,8 +1393,10 @@ public class Codegen { state.breakStack.pop(); if (aSwitch.isExpression()) { - mv.visitVarInsn(ALOAD, state.switchResultValue.peek()); - unboxPrimitive(state, aSwitch.type()); + if (aSwitch.type() != null) { + mv.visitVarInsn(ALOAD, state.switchResultValue.peek()); + unboxPrimitive(state, aSwitch.type()); + } state.popSwitch(); } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index bcd54df1..fe701aad 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -247,9 +247,14 @@ public class ASTToTargetAST { } public List convert(ParameterList input, GenerateGenerics generics) { - return input.getFormalparalist().stream().map(param -> - new MethodParameter((TargetPattern) convert(param)) - ).toList(); + var res = new ArrayList(); + for (var i = 0; i < input.getFormalparalist().size(); i++) { + var param = input.getFormalparalist().get(i); + var pattern = (TargetPattern) convert(param); + if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i); + res.add(new MethodParameter(pattern)); + } + return res; } private boolean hasGeneric(Set generics, GenericRefType type) { @@ -341,11 +346,17 @@ public class ASTToTargetAST { for (var method : methods) { var patternsRec = new ArrayList<>(patterns); + TargetExpression expr = null; var i = 0; for (var param : method.signature().parameters()) { if (param.pattern() instanceof TargetComplexPattern pat) { if (i == offset) { patternsRec.add(pat); + } + if (i > offset) { + // Find next pattern + var pattern = param.pattern(); + expr = new TargetLocalVar(pattern.type(), pattern.name()); break; } i++; @@ -364,16 +375,14 @@ public class ASTToTargetAST { return true; }).toList(); - var last = patternsRec.getLast(); - var expr = new TargetLocalVar(last.type(), last.name()); var caseBody = generatePatternOverloadsRec(offset + 1, expr, params, patternsRec, candidates, classType); var body = new TargetBlock(List.of(caseBody)); - var case_ = new TargetSwitch.Case(List.of(method.signature().parameters().getFirst().pattern()), body); + var case_ = new TargetSwitch.Case(List.of(patternsRec.getLast()), body); cases.add(case_); } - return new TargetSwitch(switchExpr, cases, null); + return new TargetSwitch(switchExpr, cases, null, true); } private List generatePatternOverloads(ClassOrInterface clazz, List overloadedMethods) { @@ -490,7 +499,6 @@ public class ASTToTargetAST { var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics)); signatures.add(new Signature(javaSignature, txSignature, generics)); - System.out.println(javaSignature); var listOfGenerics = collectedGenerics.getOrDefault(javaSignature, new ArrayList<>()); listOfGenerics.add(generics); collectedGenerics.put(javaSignature, listOfGenerics); @@ -513,7 +521,9 @@ public class ASTToTargetAST { var list = new ArrayList(); for (var i = 0; i < paraList.getFormalparalist().size(); i++) { var param = paraList.getParameterAt(i); - list.add(new MethodParameter((TargetPattern) convert(param)).withType(convert(superList.getParameterAt(i).getType(), generics))); + var pattern = (TargetPattern) convert(param); + if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i); + list.add(new MethodParameter(pattern).withType(convert(superList.getParameterAt(i).getType(), generics))); } return list; } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java b/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java index 48ae0916..a4b7b1b9 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/MethodParameter.java @@ -11,4 +11,8 @@ public record MethodParameter(TargetPattern pattern) { public MethodParameter withType(TargetType type) { return new MethodParameter(pattern.withType(type)); } + + public MethodParameter withName(String name) { + return new MethodParameter(pattern.withName(name)); + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java index 6ce331f6..98d65541 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetComplexPattern.java @@ -9,4 +9,9 @@ public record TargetComplexPattern(TargetType type, String name, List