Merge branch 'targetBytecode' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into targetBytecode

This commit is contained in:
pl@gohorb.ba-horb.de 2023-02-08 16:53:30 +01:00
commit 4074273ecb
2 changed files with 42 additions and 33 deletions

View File

@ -218,7 +218,7 @@ public class ASTToTargetAST {
for (var g2 : all) { for (var g2 : all) {
if (g1 instanceof PairTPHsmallerTPH pair) { if (g1 instanceof PairTPHsmallerTPH pair) {
if (g2.getLeft().equals(pair.getLeft()) && generics.stream().anyMatch(generic -> generic.getLeft().equals(pair.getRight()))) if (g2.getLeft().equals(pair.getLeft()) && generics.stream().anyMatch(generic -> generic.getLeft().equals(pair.getRight())))
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) g1.getLeft(), (TypePlaceholder) g2.getRight())); toAdd.add(new PairTPHsmallerTPH(pair.left, pair.right));
} }
} }
} }
@ -460,17 +460,22 @@ public class ASTToTargetAST {
} }
} }
eliminateCyclesAndInfima(result); eliminateCyclesAndInfima(result);
usedTPHsOfMethods.put(method, typeVariables);
// For eliminating inner type variables we need to figure out which ones are actually used // For eliminating inner type variables we need to figure out which ones are actually used
// TODO Maybe don't do this twice? // TODO Maybe don't do this twice?
var typeVariablesOfSignature = new HashSet<TypePlaceholder>(); var referenced = new HashSet<TypePlaceholder>();
for (var param : method.getParameterList().getFormalparalist()) { for (var param : method.getParameterList().getFormalparalist()) {
typeVariablesOfSignature.addAll(findTypeVariables(param.getType())); referenced.addAll(findTypeVariables(param.getType()));
}
referenced.addAll(findTypeVariables(method.getReturnType()));
for (var g : genericsOfClass) {
if (g.getLeft() instanceof TypePlaceholder left)
referenced.add(left);
} }
typeVariablesOfSignature.addAll(findTypeVariables(method.getReturnType()));
eliminateInnerTypeVariables(typeVariablesOfSignature, result); eliminateInnerTypeVariables(referenced, result);
usedTPHsOfMethods.put(method, referenced);
System.out.println(method.name + ": " + result); System.out.println(method.name + ": " + result);
return result; return result;
@ -567,37 +572,42 @@ public class ASTToTargetAST {
eliminateInnerTypeVariables(referenced, input); eliminateInnerTypeVariables(referenced, input);
} }
void eliminateInnerTypeVariables(Set<TypePlaceholder> referenced, Set<ResultPair<?, ?>> input) { private void findChain(Set<TypePlaceholder> referenced, Set<ResultPair<?, ?>> input, Set<ResultPair<?, ?>> output, TypePlaceholder start, TypePlaceholder end, Set<TypePlaceholder> chain) {
// Remove type variables that are part of a relation A < B < C where B is not in referenced if (referenced.contains(end)) {
// Add new pair A < C var pair = new PairTPHsmallerTPH(start, end);
var oldInput = new HashSet<>(input); if (input.contains(pair)) output.add(pair);
for (var pair : oldInput) {
if (pair instanceof PairTPHsmallerTPH ptph && !referenced.contains(ptph.left)) {
input.remove(pair);
equality.put(ptph.left, ptph.right);
for (var pair2 : oldInput) {
if (pair2.getRight().equals(pair.getLeft())) {
input.remove(pair2);
addToPairs(input, new PairTPHsmallerTPH((TypePlaceholder) pair2.getLeft(), (TypePlaceholder) pair.getRight()));
} }
for (var pair : input) {
if (pair instanceof PairTPHsmallerTPH ptph && ptph.left.equals(end)) {
if (chain.contains(ptph.right)) return;
chain = new HashSet<>(chain);
chain.add(ptph.right);
findChain(referenced, input, output, start, ptph.right, chain);
} }
} }
} }
// Remove lone type variables, A = RefType where A is in no relation X < A or A < X and A is not in referenced void eliminateInnerTypeVariables(Set<TypePlaceholder> referenced, Set<ResultPair<?, ?>> input) {
oldInput = new HashSet<>(input); var output = new HashSet<ResultPair<?, ?>>();
outer: for (var tph : referenced) {
for (var pair : oldInput) { for (var pair : input) {
if (pair instanceof PairTPHequalRefTypeOrWildcardType ptph) { if (pair instanceof PairTPHsmallerTPH pthp && pthp.left.equals(tph)) {
if (!referenced.contains(ptph.left)) { var chain = new HashSet<TypePlaceholder>();
for (var pair2 : oldInput) { chain.add(tph);
if (pair != pair2 && (pair2.getLeft().equals(ptph.left) || pair2.getRight().equals(ptph.left))) chain.add(pthp.right);
continue outer; findChain(referenced, input, output, tph, pthp.right, chain);
}
input.remove(pair);
} }
} }
} }
for (var pair : input) {
if (pair instanceof PairTPHequalRefTypeOrWildcardType rtph) {
if (referenced.contains(rtph.left))
output.add(rtph);
}
}
input.clear();
input.addAll(output);
} }
void eliminateCycles(Set<ResultPair<?, ?>> input) { void eliminateCycles(Set<ResultPair<?, ?>> input) {

View File

@ -14,7 +14,6 @@ import static org.junit.Assert.*;
import static targetast.TestCodegen.generateClassFiles; import static targetast.TestCodegen.generateClassFiles;
public class TestComplete { public class TestComplete {
@Test @Test
public void applyLambdaTest() throws Exception { public void applyLambdaTest() throws Exception {
var classFiles = generateClassFiles("applyLambda.jav", new ByteArrayClassLoader()); var classFiles = generateClassFiles("applyLambda.jav", new ByteArrayClassLoader());