diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 1a9589d86..6a8e2612e 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -13,6 +13,7 @@ import de.dhbwstuttgart.typeinference.result.*; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; public class ASTToTargetAST { @@ -268,10 +269,45 @@ public class ASTToTargetAST { } computedGenericsOfClasses.put(classOrInterface, result); eliminateCyclesAndInfima(result); + eliminateInnerTypeVariables(classOrInterface, result); System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + result); return result; } + void findTphs(RefTypeOrTPHOrWildcardOrGeneric type, Set tphs) { + if (type instanceof RefType refType) { + refType.getParaList().forEach(t -> findTphs(t, tphs)); + } else if (type instanceof TypePlaceholder tph) { + tph = equality.getOrDefault(tph, tph); + var concreteType = concreteTypes.get(tph); + if (concreteType != null) { + findTphs(concreteType, tphs); + return; + } + tphs.add(tph); + } + } + + void eliminateInnerTypeVariables(ClassOrInterface classOrInterface, Set> input) { + Set referenced = new HashSet<>(); + for (var field : classOrInterface.getFieldDecl()) { + findTphs(field.getType(), referenced); + } + + var oldInput = new HashSet<>(input); + for (var pair : oldInput) { + if (!referenced.contains(pair.getLeft())) { + input.remove(pair); + for (var pair2 : oldInput) { + if (pair2.getRight().equals(pair.getLeft())) { + input.remove(pair2); + input.add(new PairTPHsmallerTPH((TypePlaceholder) pair2.getLeft(), (TypePlaceholder) pair.getRight())); + } + } + } + } + } + void eliminateCyclesAndInfima(Set> input) { // Eliminate cycles var cycles = findCycles(input); @@ -346,7 +382,9 @@ public class ASTToTargetAST { } static Set allNodes(Set> input) { - return input.stream().map(pair -> (TypePlaceholder) pair.getLeft()).collect(Collectors.toSet()); + return input.stream() + .filter(pair -> pair instanceof PairTPHsmallerTPH) + .flatMap(pair -> Stream.of((TypePlaceholder) pair.getLeft(), (TypePlaceholder) pair.getRight())).collect(Collectors.toSet()); } static Set outgoingEdgesOf(TypePlaceholder tph, Set> input) {