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

This commit is contained in:
pl@gohorb.ba-horb.de 2023-07-18 11:44:43 +02:00
commit b65df7c390
4 changed files with 40 additions and 25 deletions

View File

@ -548,16 +548,13 @@ public class JavaTXCompiler {
SourceFile sf = sourceFiles.get(f); SourceFile sf = sourceFiles.get(f);
allClasses.addAll(getAvailableClasses(sf)); allClasses.addAll(getAvailableClasses(sf));
allClasses.addAll(sf.getClasses()); allClasses.addAll(sf.getClasses());
var newClasses = CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).collect(Collectors.toList()); var newClasses = CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).toList();
for (var clazz : newClasses) { for (var clazz : newClasses) {
var found = false; // Don't load classes that get recompiled
for (var old : allClasses) { if (sf.getClasses().stream().anyMatch(nf -> nf.getClassName().equals(clazz.getClassName())))
if (clazz.getClassName().equals(old.getClassName())) { continue;
found = true; if (allClasses.stream().noneMatch(old -> old.getClassName().equals(clazz.getClassName())))
break; allClasses.add(clazz);
}
}
if (!found) allClasses.add(clazz);
} }
} }

View File

@ -295,7 +295,9 @@ public class ASTToTargetAST {
try { try {
classLoader.findClass(superClassName); classLoader.findClass(superClassName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
try {
classLoader.loadClass(code); classLoader.loadClass(code);
} catch (LinkageError ignored) {}
} }
auxiliaries.put(superClassName, code); auxiliaries.put(superClassName, code);
} }
@ -306,7 +308,9 @@ public class ASTToTargetAST {
try { try {
classLoader.findClass(className); classLoader.findClass(className);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
try {
classLoader.loadClass(code); classLoader.loadClass(code);
} catch (LinkageError ignored) {}
} }
usedFunN.put(className, gep); usedFunN.put(className, gep);
auxiliaries.put(className, code); auxiliaries.put(className, code);

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.target.generate; package de.dhbwstuttgart.target.generate;
import com.google.common.collect.Streams;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Constructor;
@ -18,6 +19,8 @@ import de.dhbwstuttgart.util.Pair;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public abstract class GenerateGenerics { public abstract class GenerateGenerics {
@ -140,10 +143,12 @@ public abstract class GenerateGenerics {
this.astToTargetAST = astToTargetAST; this.astToTargetAST = astToTargetAST;
for (var constraint : constraints.results) { for (var constraint : constraints.results) {
if (constraint instanceof PairTPHsmallerTPH p) { if (constraint instanceof PairTPHsmallerTPH p) {
System.out.println(p.left + " " + p.left.getVariance());
simplifiedConstraints.add(new PairLT(new TPH(p.left), new TPH(p.right))); simplifiedConstraints.add(new PairLT(new TPH(p.left), new TPH(p.right)));
} else if (constraint instanceof PairTPHEqualTPH p) { } else if (constraint instanceof PairTPHEqualTPH p) {
equality.put(p.getLeft(), p.getRight()); equality.put(p.getLeft(), p.getRight());
} else if (constraint instanceof PairTPHequalRefTypeOrWildcardType p) { } else if (constraint instanceof PairTPHequalRefTypeOrWildcardType p) {
System.out.println(p.left + " = " + p.right);
concreteTypes.put(new TPH(p.left), p.right); concreteTypes.put(new TPH(p.left), p.right);
} }
} }
@ -213,7 +218,8 @@ public abstract class GenerateGenerics {
equality.put(entry.getKey(), to); equality.put(entry.getKey(), to);
} }
} }
to.setVariance(from.getVariance()); System.out.println(from + " -> " + to + " " + from.getVariance());
//from.setVariance(to.getVariance());
equality.put(from, to); equality.put(from, to);
referenced.remove(new TPH(from)); referenced.remove(new TPH(from));
referenced.add(new TPH(to)); referenced.add(new TPH(to));
@ -759,14 +765,14 @@ public abstract class GenerateGenerics {
oldFamily.clear(); oldFamily.clear();
oldFamily.putAll(familyOfMethods); oldFamily.putAll(familyOfMethods);
familyOfMethods.clear(); familyOfMethods.clear();
for (var method : classOrInterface.getMethods()) { Stream.concat(classOrInterface.getMethods().stream(), classOrInterface.getConstructors().stream()).forEach(method -> {
family(classOrInterface, method); family(classOrInterface, method);
} });
} while(!oldFamily.equals(familyOfMethods)); } while(!oldFamily.equals(familyOfMethods));
for (var method : classOrInterface.getMethods()) { Stream.concat(classOrInterface.getMethods().stream(), classOrInterface.getConstructors().stream()).forEach(method -> {
generics(classOrInterface, method); generics(classOrInterface, method);
} });
} }
private void findChain(Set<TPH> referenced, Set<Pair> input, Set<Pair> output, TPH start, TPH end, Set<TPH> chain) { private void findChain(Set<TPH> referenced, Set<Pair> input, Set<Pair> output, TPH start, TPH end, Set<TPH> chain) {
@ -838,19 +844,24 @@ public abstract class GenerateGenerics {
} }
} }
TPH findConnectionToReturnType(Set<TPH> returnTypes, Set<Pair> input, Set<TPH> visited, TPH tph) { Set<TPH> findConnectionToReturnType(Set<TPH> returnTypes, Set<Pair> input, Set<TPH> visited, TPH tph) {
if (returnTypes.contains(tph)) { if (returnTypes.contains(tph)) {
return tph; var res = new HashSet<TPH>();
res.add(tph);
return res;
} else { } else {
for (var pair : input) if (pair instanceof PairLT ptph) { for (var pair : input) if (pair instanceof PairLT ptph) {
if (ptph.left.equals(tph) && !visited.contains(ptph.right)) { if (ptph.left.equals(tph) && !visited.contains(ptph.right)) {
visited.add(ptph.right); visited.add(ptph.right);
var result = findConnectionToReturnType(returnTypes, input, visited, ptph.right); var result = findConnectionToReturnType(returnTypes, input, visited, ptph.right);
if (result != null) return result; if (result.size() > 0) {
result.add(ptph.right);
return result;
};
} }
} }
} }
return null; return new HashSet<>();
} }
void eliminateInfimaConnectedToReturnType(Method method, Set<Pair> input, Set<TPH> referenced) { void eliminateInfimaConnectedToReturnType(Method method, Set<Pair> input, Set<TPH> referenced) {
@ -867,19 +878,22 @@ public abstract class GenerateGenerics {
} }
} }
if (infima.size() > 1) { if (infima.size() > 1) {
System.out.println(infima);
for (var pair : infima) { for (var pair : infima) {
var returnTypes = findTypeVariables(method.getReturnType()); var returnTypes = findTypeVariables(method.getReturnType());
var returnType = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left); var chain = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left);
if (returnType != null && !returnType.equals(pair.left)) { System.out.println("Find: " + pair.left + " " + chain);
System.out.println("Equals now: " + pair.left.resolve() + " " + returnType.resolve()); chain.remove(pair.left);
addToEquality(pair.left.resolve(), returnType.resolve(), referenced); if (chain.size() > 0) {
for (var tph : chain)
addToEquality(pair.left.resolve(), tph.resolve(), referenced);
foundInfima = true; foundInfima = true;
} }
} }
} }
} }
} while (foundInfima); } while (foundInfima);
input.removeIf((i) -> !(i instanceof PairLT lt) || lt.left.equals(lt.right)); input.removeIf((i) -> (i instanceof PairLT lt) && lt.left.equals(lt.right));
} }
void eliminateInfima(Set<Pair> input, Set<TPH> referenced) { void eliminateInfima(Set<Pair> input, Set<TPH> referenced) {