diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 688ea471..8da9f3a0 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -483,9 +483,9 @@ public class StatementGenerator { IdentifierContext identifierCtx = recordPatternCtx.identifier(); var text = (identifierCtx != null) ? identifierCtx.getText() : null; //Hier evtl. Typ anpassen -> wenn kein Typ bekannt ist push neuen Typ auf Hashtable - var type = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics); - if (text != null) localVars.put(text, type); - var ret = new RecordPattern(subPattern, text, type, recordPatternCtx.getStart()); + var recordType = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics); + if (text != null) localVars.put(text, recordType); + var ret = new RecordPattern(subPattern, text, recordType, TypePlaceholder.fresh(recordPatternCtx.getStart()), recordPatternCtx.getStart()); return ret; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java index 506a6dd6..34041ce6 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree; import java.util.ArrayList; import java.util.List; +import de.dhbwstuttgart.syntaxtree.type.RefType; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -10,15 +11,12 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class RecordPattern extends FormalParameter { private final List subPattern; + public final RefTypeOrTPHOrWildcardOrGeneric recordType; - public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { - super(name, type, offset); - subPattern = new ArrayList<>(); - } - - public RecordPattern(List subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + public RecordPattern(List subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric recordType, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(name, type, offset); this.subPattern = subPattern; + this.recordType = recordType; } public List getSubPattern() { diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 217ab74b..8a073b63 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -179,12 +179,17 @@ public class ASTToTargetAST { // This finds a common sealed interface type to group together methods that use different records private List commonSuperInterfaceTypes(TargetType a, TargetType b) { if (a instanceof TargetGenericType && b instanceof TargetGenericType) return List.of(ASTFactory.createClass(Object.class)); + if (a instanceof TargetRefType ta && b instanceof TargetRefType tb) { var res = new HashSet(); var cla = compiler.getClass(new JavaClassName(ta.name())); var clb = compiler.getClass(new JavaClassName(tb.name())); + if (cla.equals(clb) && cla.isInterface() && cla.isSealed()) { + res.add(cla); + } + while (!cla.equals(ASTFactory.createClass(Object.class))) { var clb2 = clb; while (!clb2.equals(ASTFactory.createClass(Object.class))) { @@ -238,6 +243,13 @@ public class ASTToTargetAST { } } + mapOfSignatures.values().forEach(e -> { + e.forEach(e2 -> { + System.out.println(e2.name() + " -> " + e2.signature().parameters().stream().map(m -> m.pattern().type()).toList()); + }); + System.out.println(); + }); + return mapOfSignatures.values().stream().toList(); } @@ -438,6 +450,7 @@ public class ASTToTargetAST { } private List generatePatternOverloads(ClassOrInterface clazz, List overloadedMethods) { + if (true) return overloadedMethods; if (overloadedMethods.size() <= 1) return overloadedMethods; // Check if we have a pattern as a parameter var firstMethod = overloadedMethods.getFirst(); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 6b591324..ce017b6c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -104,31 +104,35 @@ public class TYPE { var subPatternList = recordPattern.getSubPattern(); var resolver = new GenericsResolverSameName(); + var refType = (RefType) resolver.visit((RefType) recordPattern.recordType); + + var allClasses = blockInformation.getAvailableClasses(); + var typename = refType.getName().getClassName(); + + ClassOrInterface allClass = allClasses.stream().filter(c -> c.getClassName().getClassName().equals(typename)).findFirst().orElseThrow(); + // Constraints on the type of the pattern itself + constraintSet.addUndConstraint(new Pair(refType, recordPattern.getType(), PairOperator.SMALLERDOT)); + for (var superInterface : allClass.getSuperInterfaces()) { + constraintSet.addUndConstraint(new Pair(recordPattern.getType(), resolver.visit(superInterface), PairOperator.SMALLERDOT)); + } + int counter = 0; for (Pattern el : subPatternList){ if (el instanceof RecordPattern){ constraintSet.addAll(addRecursiveParameterConstraints((RecordPattern) el, blockInformation)); - } else if (recordPattern.getType() instanceof RefType refType){ - var allClasses = blockInformation.getAvailableClasses(); - var typename = refType.getName().getClassName(); + } else { + FormalParameter param = (FormalParameter) allClass.getConstructors().getFirst().getParameterList().getParameterAt(counter); + FieldAssumption assumption = new FieldAssumption(param.getName(), allClass, param.getType(), blockInformation.getCurrentTypeScope()); - for (ClassOrInterface allClass : allClasses) { - var className = allClass.getClassName().getClassName(); - if(className.equals(typename)){ - FormalParameter param = (FormalParameter) allClass.getConstructors().getFirst().getParameterList().getParameterAt(counter); - FieldAssumption assumption = new FieldAssumption(param.getName(), allClass, param.getType(), blockInformation.getCurrentTypeScope()); + var fieldCons = new Pair(el.getType(), assumption.getType(resolver), PairOperator.EQUALSDOT); + var recvCons = new Pair(refType, assumption.getReceiverType(resolver), PairOperator.EQUALSDOT); + constraintSet.addUndConstraint(fieldCons); + constraintSet.addUndConstraint(recvCons); - var fieldCons = new Pair(el.getType(), assumption.getType(resolver), PairOperator.EQUALSDOT); - var recvCons = new Pair(refType, assumption.getReceiverType(resolver), PairOperator.EQUALSDOT); - constraintSet.addUndConstraint(fieldCons); - constraintSet.addUndConstraint(recvCons); - - for (var i = 0; i < refType.getParaList().size(); i++) { - constraintSet.addUndConstraint(new Pair(refType.getParaList().get(i), - ((RefType)assumption.getReceiverType(resolver)).getParaList().get(i), - PairOperator.EQUALSDOT)); - } - } + for (var i = 0; i < refType.getParaList().size(); i++) { + constraintSet.addUndConstraint(new Pair(refType.getParaList().get(i), + ((RefType)assumption.getReceiverType(resolver)).getParaList().get(i), + PairOperator.EQUALSDOT)); } }