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

View File

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