diff --git a/resources/bytecode/javFiles/SwitchInfered.jav b/resources/bytecode/javFiles/SwitchInfered.jav index e0b02007..48f78446 100644 --- a/resources/bytecode/javFiles/SwitchInfered.jav +++ b/resources/bytecode/javFiles/SwitchInfered.jav @@ -10,7 +10,7 @@ public record Rec(Object a, Object b) {} public class SwitchInfered { public main(o) { return switch (o) { - case Rec(a, b) -> a+b; + case Rec(a, b) -> a; case Integer i -> i+1; default -> 0; }; diff --git a/resources/bytecode/javFiles/recordList.jav b/resources/bytecode/javFiles/recordList.jav new file mode 100644 index 00000000..d21e5eb7 --- /dev/null +++ b/resources/bytecode/javFiles/recordList.jav @@ -0,0 +1,15 @@ +sealed interface List permits Cons, Nil {} + +record Cons(A a, List l) implements List {} + +record Nil()implements List { + + void m(x) { + switch(x) { + case Cons(a, Cons(b, List l)) -> System.out.println(a); + case Cons(a, Nil()) -> System.out.println(a); + case Nil() -> System.out.println("Nil"); + } + + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index e068d9b2..8a6ae914 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -1,10 +1,12 @@ //PL 2018-12-19: Merge chekcen package de.dhbwstuttgart.typeinference.typeAlgo; +import java.lang.Record; import java.sql.Ref; import java.util.*; import java.util.stream.Collectors; +import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; @@ -48,11 +50,13 @@ public class TYPEStmt implements StatementVisitor { private SourceLoc loc(Token token) { return new SourceLoc(info.getCurrentClass().getFileName(), token.getLine()); - }; + } + + ; /** * Erstellt einen neuen GenericResolver Die Idee dieser Datenstruktur ist es, GTVs einen eindeutigen TPH zuzuweisen. Bei Methodenaufrufen oder anderen Zugriffen, bei denen alle benutzten GTVs jeweils einen einheitlichen TPH bekommen müssen kann diese Klasse eingesetzt werden. Wichtig ist, dass hierfür jeweils eine frische Instanz benutzt wird. - * + * * @return */ private static GenericsResolver getResolverInstance() { @@ -235,7 +239,7 @@ public class TYPEStmt implements StatementVisitor { private final RefType doublee = new RefType(ASTFactory.createClass(Double.class).getClassName(), new NullToken()); private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken()); private final RefType bool = new RefType(ASTFactory.createClass(Boolean.class).getClassName(), new NullToken()); - + private final RefType charr = new RefType(ASTFactory.createClass(Character.class).getClassName(), new NullToken()); @Override @@ -261,13 +265,13 @@ public class TYPEStmt implements StatementVisitor { binary.lexpr.accept(this); binary.rexpr.accept(this); if (binary.operation.equals(BinaryExpr.Operator.DIV) - || binary.operation.equals(BinaryExpr.Operator.MUL) - || binary.operation.equals(BinaryExpr.Operator.MOD) - || binary.operation.equals(BinaryExpr.Operator.ADD) - || binary.operation.equals(BinaryExpr.Operator.SUB) - || binary.operation.equals(BinaryExpr.Operator.OR) - || binary.operation.equals(BinaryExpr.Operator.AND) - || binary.operation.equals(BinaryExpr.Operator.XOR)) { + || binary.operation.equals(BinaryExpr.Operator.MUL) + || binary.operation.equals(BinaryExpr.Operator.MOD) + || binary.operation.equals(BinaryExpr.Operator.ADD) + || binary.operation.equals(BinaryExpr.Operator.SUB) + || binary.operation.equals(BinaryExpr.Operator.OR) + || binary.operation.equals(BinaryExpr.Operator.AND) + || binary.operation.equals(BinaryExpr.Operator.XOR)) { Set> numericAdditionOrStringConcatenation = new HashSet<>(); @@ -330,7 +334,7 @@ public class TYPEStmt implements StatementVisitor { } /* * PL auskommentiert Anfang 2018-07-17 /* In Java passiert bei den binären Operatoren eine sogenannte Type Promotion: https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2 Das bedeutet, dass Java die Typen je nach belieben castet, so lange sie nur von Number erben - * + * * numeric = new Constraint<>(); numeric.add(new Pair(binary.getType(), number, PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); PL auskommentiert Ende 2018-07-17 */ @@ -354,7 +358,7 @@ public class TYPEStmt implements StatementVisitor { * shortt, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), shortt, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.getType(), bool, PairOperator.SMALLERDOT)); numericRelationConcatenation.add(numeric); numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.getType(), bool, PairOperator.SMALLERDOT)); * numericRelationConcatenation.add(numeric); numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.getType(), bool, PairOperator.SMALLERDOT)); numericRelationConcatenation.add(numeric); numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.SMALLERDOT)); numeric.add(new * Pair(binary.rexpr.getType(), floatt, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.getType(), bool, PairOperator.SMALLERDOT)); numericRelationConcatenation.add(numeric); numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.getType(), bool, PairOperator.SMALLERDOT)); numericRelationConcatenation.add(numeric); - * + * * //***ACHTUNG: Moeglicherweise oder und und-Contraint falsch constraintsSet.addOderConstraint(numericRelationConcatenation); //***ACHTUNG: Moeglicherweise oder und und-Contraint falsch */ // Testeise eingefuegt PL 2018-05-24 @@ -628,14 +632,14 @@ public class TYPEStmt implements StatementVisitor { public static RefTypeOrTPHOrWildcardOrGeneric getReceiverType(ClassOrInterface receiver, GenericsResolver resolver) { if (receiver == null) return null; List params = new ArrayList<>(); - for(GenericTypeVar gtv : receiver.getGenerics()){ + for (GenericTypeVar gtv : receiver.getGenerics()) { //Die Generics werden alle zu TPHs umgewandelt. params.add(resolver.resolve(new GenericRefType(gtv.getName(), new NullToken()))); } RefTypeOrTPHOrWildcardOrGeneric receiverType; - if(receiver instanceof FunNClass){ - receiverType = new RefType(new JavaClassName(receiver.getClassName().toString()+"$$"), params, new NullToken()); // new FunN(params); - }else{ + if (receiver instanceof FunNClass) { + receiverType = new RefType(new JavaClassName(receiver.getClassName().toString() + "$$"), params, new NullToken()); // new FunN(params); + } else { receiverType = new RefType(receiver.getClassName(), params, new NullToken()); } @@ -654,7 +658,7 @@ public class TYPEStmt implements StatementVisitor { ClassOrInterface receiverCl = assumption.getReceiver(); /* * List params = new ArrayList<>(); for(GenericTypeVar gtv : receiverCl.getGenerics()){ //Die Generics werden alle zu TPHs umgewandelt. params.add(resolver.resolve(gtv.getName())); } - * + * * RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset()); */ @@ -776,7 +780,7 @@ public class TYPEStmt implements StatementVisitor { // TODO: Hier müssen die Parameter mit den TPHs in den GEnerics des Receivers verknüpft werden /* * BEispiel: auto test = new List(); test.add("hallo"); - * + * * Hier kriegt der Receiver ja den COnstraint TPH REceiver <. List Dann mus bei dem Parameter der COnstraint entstehen: TPH A <. String */ List params = new ArrayList<>(); @@ -834,7 +838,15 @@ public class TYPEStmt implements StatementVisitor { if (caseExpressionType == null) { for (var child : switchStmt.getBlocks()) { for (var label : child.getLabels()) { - if (label.getPattern() instanceof FormalParameter) { + if (label.getPattern() == null) { + System.out.println("DefaultCase"); + } + if (label.getPattern() instanceof RecordPattern) { + RecordPattern recordPattern = (RecordPattern) label.getPattern(); + System.out.println("HALFTAN DER WEI?E ICH CHECK ECHT GART NCIHTS MEHR"); + System.out.println(recordPattern.getName()); + //constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset()))); + } else if (label.getPattern() instanceof FormalParameter) { constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset()))); } } @@ -844,6 +856,35 @@ public class TYPEStmt implements StatementVisitor { } for (var child : switchStmt.getBlocks()) { + + //Hier Constraints hinzufügen das die Parameter vom Parameter des definierten Record sein müssen -> Wie komm ich an den goofy ah Record? + + + child.getLabels().forEach(el -> { + if (el.getType() instanceof RefType) { + var recType = el; + + if(el.getPattern() instanceof RecordPattern){ + var pattern = (RecordPattern) recType.getPattern(); + + var allClasses = info.getAvailableClasses(); + var interestingClasses = allClasses.stream().filter(as -> as.getClassName().equals(((RefType) el.getType()).getName())).toList(); + + var test = ""; + + for(int i = 0; i < pattern.getSubPattern().size(); i++){ + var supPattern = pattern.getSubPattern().get(i); + for(int j = 0; j < interestingClasses.size(); j++){ + constraintsSet.addUndConstraint(new Pair(supPattern.getType(), interestingClasses.get(j).getFieldDecl().get(i).getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset()))); + } + } + + + + } + + } + }); child.accept(this); constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset()))); } @@ -860,7 +901,7 @@ public class TYPEStmt implements StatementVisitor { @Override public void visit(SwitchLabel switchLabel) { - // TODO Auto-generated method stub + System.out.println("I MOCH WAS"); } @Override diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 4dca7d3e..9f5b5255 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -690,7 +690,24 @@ public class TestComplete { var r1 = ctor.newInstance(1, 1); - assertEquals(swtch.invoke(instance, r1), 2); + assertEquals(swtch.invoke(instance, r1), 1); + } + + @Ignore("Not implemented") + @Test + public void testRecordList() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "recordList.jav"); + var clazz = classFiles.get("Nil"); + + var instance = clazz.getDeclaredConstructor().newInstance(); + var swtch = clazz.getDeclaredMethod("m", Object.class); + + //var record = classFiles.get("Rec"); + //var ctor = record.getDeclaredConstructor(Object.class, Object.class); + //var r1 = ctor.newInstance(1, 1); + + + assertEquals(swtch.invoke(instance, null), 2); } @Ignore("Not implemented")