Fix generics

This commit is contained in:
Daniel Holle 2024-12-04 13:57:55 +01:00
parent d0d9c46a67
commit 77411973be
2 changed files with 39 additions and 11 deletions
src/main/java/de/dhbwstuttgart
target/generate
typeinference/typeAlgo

@ -1,10 +1,7 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
@ -170,6 +167,25 @@ public abstract class GenerateGenerics {
else if (to instanceof RefType t) concreteTypes.put(new TPH(from), t);
}*/
Set<TPH> findTypeVariables(ParameterList params) {
var res = new HashSet<TPH>();
for (var param : params.getFormalparalist()) {
res.addAll(findTypeVariables(param));
}
return res;
}
Set<TPH> findTypeVariables(Pattern pattern) {
var res = new HashSet<TPH>();
if (pattern instanceof RecordPattern rp) {
for (var subPattern : rp.getSubPattern()) {
res.addAll(findTypeVariables(subPattern));
}
}
res.addAll(findTypeVariables(pattern.getType()));
return res;
}
Set<TPH> findTypeVariables(RefTypeOrTPHOrWildcardOrGeneric type) {
var result = new HashSet<TPH>();
if (type instanceof TypePlaceholder tph) {
@ -506,9 +522,7 @@ public abstract class GenerateGenerics {
if (!(method instanceof Constructor))
typeVariables.addAll(findTypeVariables(method.getReturnType()));
for (var arg : method.getParameterList().getFormalparalist()) {
typeVariables.addAll(findTypeVariables(arg.getType()));
}
typeVariables.addAll(findTypeVariables(method.getParameterList()));
if (method.block != null)
method.block.accept(new TracingStatementVisitor() {
@ -572,9 +586,7 @@ public abstract class GenerateGenerics {
var usedTphs = new HashSet<TPH>();
// For eliminating inner type variables we need to figure out which ones are actually used
for (var param : method.getParameterList().getFormalparalist()) {
usedTphs.addAll(findTypeVariables(param.getType()));
}
usedTphs.addAll(findTypeVariables(method.getParameterList()));
usedTphs.addAll(findTypeVariables(method.getReturnType()));
referenced.addAll(usedTphs);
referenced.addAll(typeVariablesOfClass);
@ -672,11 +684,21 @@ public abstract class GenerateGenerics {
if (p1 instanceof PairLT ptph && ptph.left.resolve().equals(ptph.right.resolve()))
result.remove(p1); // TODO This is a bit strange
}
for (var tph : usedTphs) {
if (classGenerics == null || classGenerics.stream().noneMatch((pair) -> pair.left.equals(tph)))
addToPairs(result, new PairEQ(tph, ASTToTargetAST.OBJECT));
}
var all = new HashSet<>(result);
for (var p : all) {
if (p instanceof PairEQ peq && peq.right.equals(ASTToTargetAST.OBJECT)) {
for (var p2 : all) {
if (p2 instanceof PairLT && p2.left.equals(p.left)) {
result.remove(p);
break;
}
}
}
}
}
private record ToAdd(TypePlaceholder left, TypePlaceholder right) {}

@ -122,6 +122,12 @@ public class TYPE {
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));
}
}
}