Compare commits
2 Commits
be60261795
...
139325e78f
Author | SHA1 | Date | |
---|---|---|---|
|
139325e78f | ||
|
b18b0a38cf |
@ -10,7 +10,7 @@ public record Rec(Object a, Object b) {}
|
|||||||
public class SwitchInfered {
|
public class SwitchInfered {
|
||||||
public main(o) {
|
public main(o) {
|
||||||
return switch (o) {
|
return switch (o) {
|
||||||
case Rec(a, b) -> a+b;
|
case Rec(a, b) -> a;
|
||||||
case Integer i -> i+1;
|
case Integer i -> i+1;
|
||||||
default -> 0;
|
default -> 0;
|
||||||
};
|
};
|
||||||
|
15
resources/bytecode/javFiles/recordList.jav
Normal file
15
resources/bytecode/javFiles/recordList.jav
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
sealed interface List<A> permits Cons, Nil {}
|
||||||
|
|
||||||
|
record Cons(A a, List<A> l) implements List<A> {}
|
||||||
|
|
||||||
|
record Nil()implements List<A> {
|
||||||
|
|
||||||
|
void m(x) {
|
||||||
|
switch(x) {
|
||||||
|
case Cons(a, Cons(b, List<A> l)) -> System.out.println(a);
|
||||||
|
case Cons(a, Nil()) -> System.out.println(a);
|
||||||
|
case Nil() -> System.out.println("Nil");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
//PL 2018-12-19: Merge chekcen
|
//PL 2018-12-19: Merge chekcen
|
||||||
package de.dhbwstuttgart.typeinference.typeAlgo;
|
package de.dhbwstuttgart.typeinference.typeAlgo;
|
||||||
|
|
||||||
|
import java.lang.Record;
|
||||||
import java.sql.Ref;
|
import java.sql.Ref;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||||
import de.dhbwstuttgart.parser.NullToken;
|
import de.dhbwstuttgart.parser.NullToken;
|
||||||
@ -48,7 +50,9 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
|
|
||||||
private SourceLoc loc(Token token) {
|
private SourceLoc loc(Token token) {
|
||||||
return new SourceLoc(info.getCurrentClass().getFileName(), token.getLine());
|
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.
|
* 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.
|
||||||
@ -628,14 +632,14 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
public static RefTypeOrTPHOrWildcardOrGeneric getReceiverType(ClassOrInterface receiver, GenericsResolver resolver) {
|
public static RefTypeOrTPHOrWildcardOrGeneric getReceiverType(ClassOrInterface receiver, GenericsResolver resolver) {
|
||||||
if (receiver == null) return null;
|
if (receiver == null) return null;
|
||||||
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
||||||
for(GenericTypeVar gtv : receiver.getGenerics()){
|
for (GenericTypeVar gtv : receiver.getGenerics()) {
|
||||||
//Die Generics werden alle zu TPHs umgewandelt.
|
//Die Generics werden alle zu TPHs umgewandelt.
|
||||||
params.add(resolver.resolve(new GenericRefType(gtv.getName(), new NullToken())));
|
params.add(resolver.resolve(new GenericRefType(gtv.getName(), new NullToken())));
|
||||||
}
|
}
|
||||||
RefTypeOrTPHOrWildcardOrGeneric receiverType;
|
RefTypeOrTPHOrWildcardOrGeneric receiverType;
|
||||||
if(receiver instanceof FunNClass){
|
if (receiver instanceof FunNClass) {
|
||||||
receiverType = new RefType(new JavaClassName(receiver.getClassName().toString()+"$$"), params, new NullToken()); // new FunN(params);
|
receiverType = new RefType(new JavaClassName(receiver.getClassName().toString() + "$$"), params, new NullToken()); // new FunN(params);
|
||||||
}else{
|
} else {
|
||||||
receiverType = new RefType(receiver.getClassName(), params, new NullToken());
|
receiverType = new RefType(receiver.getClassName(), params, new NullToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -834,7 +838,15 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
if (caseExpressionType == null) {
|
if (caseExpressionType == null) {
|
||||||
for (var child : switchStmt.getBlocks()) {
|
for (var child : switchStmt.getBlocks()) {
|
||||||
for (var label : child.getLabels()) {
|
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())));
|
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -844,6 +856,33 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (var child : switchStmt.getBlocks()) {
|
for (var child : switchStmt.getBlocks()) {
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
child.accept(this);
|
||||||
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset())));
|
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset())));
|
||||||
}
|
}
|
||||||
@ -860,7 +899,7 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(SwitchLabel switchLabel) {
|
public void visit(SwitchLabel switchLabel) {
|
||||||
// TODO Auto-generated method stub
|
System.out.println("I MOCH WAS");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -690,7 +690,24 @@ public class TestComplete {
|
|||||||
var r1 = ctor.newInstance(1, 1);
|
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")
|
@Ignore("Not implemented")
|
||||||
|
Loading…
Reference in New Issue
Block a user