txGenerics

This commit is contained in:
Victorious3 2023-03-01 11:23:00 +01:00
parent 1ad4cfcb21
commit c0348ed304
2 changed files with 36 additions and 19 deletions

View File

@ -36,7 +36,7 @@ public class ASTToTargetAST {
return all.stream().map(GenericsResult::new).toList(); return all.stream().map(GenericsResult::new).toList();
} }
record Generics(Set<ResultPair<?, ?>> javaGenerics, Set<ResultPair<?, ?>> txGenerics) {} record Generics(Set<ResultPair<?, ?>> javaGenerics, Set<TargetGeneric> txGenerics) {}
class Sigma { class Sigma {
Map<Method, Generics> computedGenericsOfMethods = new HashMap<>(); Map<Method, Generics> computedGenericsOfMethods = new HashMap<>();
@ -142,10 +142,11 @@ public class ASTToTargetAST {
Set<ResultPair<?, ?>> txResult = new HashSet<>(); Set<ResultPair<?, ?>> txResult = new HashSet<>();
Set<ResultPair<?, ?>> javaResult = new HashSet<>(); Set<ResultPair<?, ?>> javaResult = new HashSet<>();
var generics = new Generics(javaResult, txResult); Set<TargetGeneric> generatedTxGenerics = new HashSet<>();
var generics = new Generics(javaResult, generatedTxGenerics);
computedGenericsOfMethods.put(method, generics); computedGenericsOfMethods.put(method, generics);
var genericsOfClass = generics(owner).txGenerics(); var genericsOfClass = generics(owner).javaGenerics();
var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints); var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints);
HashSet<TypePlaceholder> typeVariables = new HashSet<>(); HashSet<TypePlaceholder> typeVariables = new HashSet<>();
@ -225,7 +226,7 @@ public class ASTToTargetAST {
if (optMethod.isEmpty()) return; if (optMethod.isEmpty()) return;
var method = optMethod.get(); var method = optMethod.get();
var generics = generics(owner, method).txGenerics(); var generics = generics(owner, method).javaGenerics();
// transitive and // transitive and
var all = transitiveClosure(generics); var all = transitiveClosure(generics);
@ -535,6 +536,9 @@ public class ASTToTargetAST {
referenced.addAll(typeVariablesOfClass); referenced.addAll(typeVariablesOfClass);
eliminateInnerTypeVariables(referenced, txResult); eliminateInnerTypeVariables(referenced, txResult);
// Generate generics with current equality
generatedTxGenerics.addAll(convert(txResult));
} }
equality = storedEq; equality = storedEq;
@ -579,7 +583,8 @@ public class ASTToTargetAST {
Set<ResultPair<?, ?>> txResult = new HashSet<>(); Set<ResultPair<?, ?>> txResult = new HashSet<>();
Set<ResultPair<?, ?>> javaResult = new HashSet<>(); Set<ResultPair<?, ?>> javaResult = new HashSet<>();
var generics = new Generics(javaResult, txResult); Set<TargetGeneric> generatedTxGenerics = new HashSet<>();
var generics = new Generics(javaResult, generatedTxGenerics);
for (var field : classOrInterface.getFieldDecl()) { for (var field : classOrInterface.getFieldDecl()) {
findAllBounds(field.getType(), txResult); findAllBounds(field.getType(), txResult);
@ -587,12 +592,23 @@ public class ASTToTargetAST {
computedGenericsOfClasses.put(classOrInterface, generics); computedGenericsOfClasses.put(classOrInterface, generics);
javaResult.addAll(txResult); javaResult.addAll(txResult);
eliminateCycles(javaResult);
eliminateInfima(txResult); var eq = new HashMap<>(equality);
eliminateInfima(javaResult); { // Java Generics
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult); eliminateCycles(javaResult);
eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult); eliminateInfima(javaResult);
equalizeTypeVariables(javaResult); eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult);
equalizeTypeVariables(javaResult);
}
var storedEq = equality; //TODO Hack: pass equality as parameter instead
equality = eq;
{
eliminateInfima(txResult);
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult);
}
equality = storedEq;
generatedTxGenerics.addAll(convert(txResult));
System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult); System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult);
return generics; return generics;
@ -928,7 +944,7 @@ public class ASTToTargetAST {
// Generate generics only if there are no user defined ones // Generate generics only if there are no user defined ones
var generics = sigma.generics(input); var generics = sigma.generics(input);
javaGenerics = convert(generics.javaGenerics()); javaGenerics = convert(generics.javaGenerics());
txGenerics = convert(generics.txGenerics()); txGenerics = generics.txGenerics();
} }
TargetBlock fieldInitializer = null; TargetBlock fieldInitializer = null;
@ -954,8 +970,8 @@ public class ASTToTargetAST {
return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName())); return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName()));
} }
private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Method input) { private Set<TargetGeneric> collectMethodGenerics(Set<TargetGeneric> generics, Method input) {
var convertedGenerics = new HashSet<>(convert(generics)); var convertedGenerics = new HashSet<>(generics);
outer: outer:
for (GenericTypeVar typeVar : input.getGenerics()) { for (GenericTypeVar typeVar : input.getGenerics()) {
for (var classGeneric : currentClass.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) {
@ -989,7 +1005,7 @@ public class ASTToTargetAST {
var generics = sigma.generics(currentClass, input); var generics = sigma.generics(currentClass, input);
List<MethodParameter> params = convert(input.getParameterList()); List<MethodParameter> params = convert(input.getParameterList());
if (parameterSet.stream().noneMatch(p -> p.equals(params))) { if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), input); var javaGenerics = collectMethodGenerics(convert(generics.javaGenerics()), input);
var txGenerics = collectMethodGenerics(generics.txGenerics(), input); var txGenerics = collectMethodGenerics(generics.txGenerics(), input);
result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer)); result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer));
@ -1010,7 +1026,7 @@ public class ASTToTargetAST {
var generics = sigma.generics(currentClass, input); var generics = sigma.generics(currentClass, input);
List<MethodParameter> params = convert(input.getParameterList()); List<MethodParameter> params = convert(input.getParameterList());
if (parameterSet.stream().noneMatch(p -> p.equals(params))) { if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), input); var javaGenerics = collectMethodGenerics(convert(generics.javaGenerics()), input);
var txGenerics = collectMethodGenerics(generics.txGenerics(), input); var txGenerics = collectMethodGenerics(generics.txGenerics(), input);
result.add(new TargetMethod( result.add(new TargetMethod(

View File

@ -16,16 +16,17 @@ public class GenericsResult {
} }
public Set<ResultPair<?, ?>> get(ClassOrInterface clazz) { public Set<ResultPair<?, ?>> get(ClassOrInterface clazz) {
var generics = this.sigma.computedGenericsOfClasses.get(clazz); var generics = this.sigma.computedGenericsOfClasses.get(clazz);
if (generics == null) return Set.of(); if (generics == null) return Set.of();
return generics.txGenerics(); return generics.javaGenerics();
} }
// TODO Compute generics if not present? // TODO Compute generics if not present?
// TODO Maybe use tx Generics here (two equalities)
public Set<ResultPair<?, ?>> get(Method method) { public Set<ResultPair<?, ?>> get(Method method) {
var generics = this.sigma.computedGenericsOfMethods.get(method); var generics = this.sigma.computedGenericsOfMethods.get(method);
if (generics == null) return Set.of(); if (generics == null) return Set.of();
return generics.txGenerics(); return generics.javaGenerics();
} }
public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) { public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) {