Add new type to record patterns
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 5m34s

This commit is contained in:
Daniel Holle 2024-12-11 16:08:59 +01:00
parent b76e1e46f0
commit 542389d35b
4 changed files with 43 additions and 28 deletions

View File

@ -483,9 +483,9 @@ public class StatementGenerator {
IdentifierContext identifierCtx = recordPatternCtx.identifier(); IdentifierContext identifierCtx = recordPatternCtx.identifier();
var text = (identifierCtx != null) ? identifierCtx.getText() : null; var text = (identifierCtx != null) ? identifierCtx.getText() : null;
//Hier evtl. Typ anpassen -> wenn kein Typ bekannt ist push neuen Typ auf Hashtable //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); var recordType = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics);
if (text != null) localVars.put(text, type); if (text != null) localVars.put(text, recordType);
var ret = new RecordPattern(subPattern, text, type, recordPatternCtx.getStart()); var ret = new RecordPattern(subPattern, text, recordType, TypePlaceholder.fresh(recordPatternCtx.getStart()), recordPatternCtx.getStart());
return ret; return ret;
} }

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -10,15 +11,12 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class RecordPattern extends FormalParameter { public class RecordPattern extends FormalParameter {
private final List<Pattern> subPattern; private final List<Pattern> subPattern;
public final RefTypeOrTPHOrWildcardOrGeneric recordType;
public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { public RecordPattern(List<Pattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric recordType, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset);
subPattern = new ArrayList<>();
}
public RecordPattern(List<Pattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset); super(name, type, offset);
this.subPattern = subPattern; this.subPattern = subPattern;
this.recordType = recordType;
} }
public List<Pattern> getSubPattern() { public List<Pattern> getSubPattern() {

View File

@ -179,12 +179,17 @@ public class ASTToTargetAST {
// This finds a common sealed interface type to group together methods that use different records // This finds a common sealed interface type to group together methods that use different records
private List<ClassOrInterface> commonSuperInterfaceTypes(TargetType a, TargetType b) { private List<ClassOrInterface> commonSuperInterfaceTypes(TargetType a, TargetType b) {
if (a instanceof TargetGenericType && b instanceof TargetGenericType) return List.of(ASTFactory.createClass(Object.class)); if (a instanceof TargetGenericType && b instanceof TargetGenericType) return List.of(ASTFactory.createClass(Object.class));
if (a instanceof TargetRefType ta && b instanceof TargetRefType tb) { if (a instanceof TargetRefType ta && b instanceof TargetRefType tb) {
var res = new HashSet<ClassOrInterface>(); var res = new HashSet<ClassOrInterface>();
var cla = compiler.getClass(new JavaClassName(ta.name())); var cla = compiler.getClass(new JavaClassName(ta.name()));
var clb = compiler.getClass(new JavaClassName(tb.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))) { while (!cla.equals(ASTFactory.createClass(Object.class))) {
var clb2 = clb; var clb2 = clb;
while (!clb2.equals(ASTFactory.createClass(Object.class))) { 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(); return mapOfSignatures.values().stream().toList();
} }
@ -438,6 +450,7 @@ public class ASTToTargetAST {
} }
private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, List<TargetMethod> overloadedMethods) { private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, List<TargetMethod> overloadedMethods) {
if (true) return overloadedMethods;
if (overloadedMethods.size() <= 1) return overloadedMethods; if (overloadedMethods.size() <= 1) return overloadedMethods;
// Check if we have a pattern as a parameter // Check if we have a pattern as a parameter
var firstMethod = overloadedMethods.getFirst(); var firstMethod = overloadedMethods.getFirst();

View File

@ -104,17 +104,23 @@ public class TYPE {
var subPatternList = recordPattern.getSubPattern(); var subPatternList = recordPattern.getSubPattern();
var resolver = new GenericsResolverSameName(); 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; int counter = 0;
for (Pattern el : subPatternList){ for (Pattern el : subPatternList){
if (el instanceof RecordPattern){ if (el instanceof RecordPattern){
constraintSet.addAll(addRecursiveParameterConstraints((RecordPattern) el, blockInformation)); constraintSet.addAll(addRecursiveParameterConstraints((RecordPattern) el, blockInformation));
} else if (recordPattern.getType() instanceof RefType refType){ } else {
var allClasses = blockInformation.getAvailableClasses();
var typename = refType.getName().getClassName();
for (ClassOrInterface allClass : allClasses) {
var className = allClass.getClassName().getClassName();
if(className.equals(typename)){
FormalParameter param = (FormalParameter) allClass.getConstructors().getFirst().getParameterList().getParameterAt(counter); FormalParameter param = (FormalParameter) allClass.getConstructors().getFirst().getParameterList().getParameterAt(counter);
FieldAssumption assumption = new FieldAssumption(param.getName(), allClass, param.getType(), blockInformation.getCurrentTypeScope()); FieldAssumption assumption = new FieldAssumption(param.getName(), allClass, param.getType(), blockInformation.getCurrentTypeScope());
@ -128,8 +134,6 @@ public class TYPE {
((RefType)assumption.getReceiverType(resolver)).getParaList().get(i), ((RefType)assumption.getReceiverType(resolver)).getParaList().get(i),
PairOperator.EQUALSDOT)); PairOperator.EQUALSDOT));
} }
}
}
} }
counter++; counter++;