Split up equality
This commit is contained in:
parent
3de89a5cfa
commit
11eb03cdf0
@ -36,7 +36,7 @@ public class ASTToTargetAST {
|
||||
return all.stream().map(GenericsResult::new).toList();
|
||||
}
|
||||
|
||||
record Generics(Set<ResultPair<?, ?>> javaGenerics, Set<TargetGeneric> txGenerics) {}
|
||||
record Generics(Set<ResultPair<?, ?>> javaGenerics, Set<ResultPair<?, ?>> txGenerics) {}
|
||||
|
||||
class Sigma {
|
||||
Map<Method, Generics> computedGenericsOfMethods = new HashMap<>();
|
||||
@ -46,6 +46,7 @@ public class ASTToTargetAST {
|
||||
Set<PairTPHsmallerTPH> simplifiedConstraints = new HashSet<>();
|
||||
Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> concreteTypes = new HashMap<>();
|
||||
Map<TypePlaceholder, TypePlaceholder> equality = new HashMap<>();
|
||||
Map<TypePlaceholder, TypePlaceholder> txEquality = new HashMap<>();
|
||||
|
||||
Sigma(ResultSet constraints) {
|
||||
ASTToTargetAST.this.sigma = this;
|
||||
@ -55,6 +56,7 @@ public class ASTToTargetAST {
|
||||
simplifiedConstraints.add(p);
|
||||
} else if (constraint instanceof PairTPHEqualTPH p) {
|
||||
equality.put(p.getLeft(), p.getRight());
|
||||
txEquality.put(p.getLeft(), p.getRight());
|
||||
} else if (constraint instanceof PairTPHequalRefTypeOrWildcardType p) {
|
||||
concreteTypes.put(this.equality.getOrDefault(p.left, p.left), p.right);
|
||||
}
|
||||
@ -63,22 +65,22 @@ public class ASTToTargetAST {
|
||||
System.out.println("Simplified constraints: " + simplifiedConstraints);
|
||||
}
|
||||
|
||||
Set<TypePlaceholder> findTypeVariables(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||
Set<TypePlaceholder> findTypeVariables(RefTypeOrTPHOrWildcardOrGeneric type, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
var result = new HashSet<TypePlaceholder>();
|
||||
if (type instanceof TypePlaceholder tph) {
|
||||
tph = equality.getOrDefault(tph, tph);
|
||||
if (concreteTypes.containsKey(tph)) {
|
||||
result.addAll(findTypeVariables(concreteTypes.get(tph)));
|
||||
result.addAll(findTypeVariables(concreteTypes.get(tph), equality));
|
||||
return result;
|
||||
}
|
||||
result.add(tph);
|
||||
} else if (type instanceof RefType refType) {
|
||||
for (var t : refType.getParaList())
|
||||
result.addAll(findTypeVariables(t));
|
||||
result.addAll(findTypeVariables(t, equality));
|
||||
} else if (type instanceof ExtendsWildcardType wildcardType) {
|
||||
result.addAll(findTypeVariables(wildcardType.getInnerType()));
|
||||
result.addAll(findTypeVariables(wildcardType.getInnerType(), equality));
|
||||
} else if (type instanceof SuperWildcardType wildcardType) {
|
||||
result.addAll(findTypeVariables(wildcardType.getInnerType()));
|
||||
result.addAll(findTypeVariables(wildcardType.getInnerType(), equality));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -135,144 +137,115 @@ public class ASTToTargetAST {
|
||||
return all;
|
||||
}
|
||||
|
||||
// Family of generated Generics
|
||||
Generics generics(ClassOrInterface owner, Method method) {
|
||||
if (computedGenericsOfMethods.containsKey(method))
|
||||
return computedGenericsOfMethods.get(method);
|
||||
|
||||
Set<ResultPair<?, ?>> txResult = new HashSet<>();
|
||||
Set<ResultPair<?, ?>> javaResult = new HashSet<>();
|
||||
Set<TargetGeneric> generatedTxGenerics = new HashSet<>();
|
||||
var generics = new Generics(javaResult, generatedTxGenerics);
|
||||
computedGenericsOfMethods.put(method, generics);
|
||||
|
||||
var genericsOfClass = generics(owner).javaGenerics();
|
||||
var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints);
|
||||
|
||||
HashSet<TypePlaceholder> typeVariables = new HashSet<>();
|
||||
HashSet<TypePlaceholder> typeVariablesOfClass = new HashSet<>();
|
||||
|
||||
for (var pair : genericsOfClass) {
|
||||
typeVariablesOfClass.add((TypePlaceholder) pair.getLeft());
|
||||
}
|
||||
|
||||
typeVariables.addAll(findTypeVariables(method.getReturnType()));
|
||||
for (var arg : method.getParameterList().getFormalparalist()) {
|
||||
typeVariables.addAll(findTypeVariables(arg.getType()));
|
||||
}
|
||||
|
||||
method.block.accept(new TracingStatementVisitor() {
|
||||
@Override
|
||||
public void visit(LocalVarDecl localVarDecl) {
|
||||
typeVariables.addAll(findTypeVariables(localVarDecl.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
super.visit(methodCall);
|
||||
typeVariables.addAll(findTypeVariables(methodCall.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Assign assign) {}
|
||||
});
|
||||
|
||||
|
||||
private void methodFindConstraints(
|
||||
ClassOrInterface owner, Method method,
|
||||
Set<PairTPHsmallerTPH> simplifiedConstraints,
|
||||
HashSet<TypePlaceholder> typeVariables,
|
||||
HashSet<TypePlaceholder> typeVariablesOfClass,
|
||||
Set<ResultPair<?, ?>> result,
|
||||
Map<TypePlaceholder, TypePlaceholder> equality
|
||||
) {
|
||||
// Type variables with bounds that are also type variables of the method
|
||||
for (var typeVariable : new HashSet<>(typeVariables)) {
|
||||
if (typeVariablesOfClass.contains(typeVariable)) continue;
|
||||
for (var pair : simplifiedConstraints) {
|
||||
if (pair.left.equals(typeVariable) && typeVariables.contains(pair.right)) {
|
||||
addToPairs(txResult, new PairTPHsmallerTPH(pair.left, equality.getOrDefault(pair.right, pair.right)));
|
||||
addToPairs(result, new PairTPHsmallerTPH(pair.left, equality.getOrDefault(pair.right, pair.right)));
|
||||
typeVariables.add(pair.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
method.block.accept(new TracingStatementVisitor() {
|
||||
|
||||
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
//Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg
|
||||
//betrachtet werden muessen
|
||||
//Definition 7.2 (Family of generated generics). T1 <. R1 <.^∗ R2 <. T2
|
||||
Set<RefTypeOrTPHOrWildcardOrGeneric> T1s =
|
||||
methodCall.getArgumentList()
|
||||
.getArguments()
|
||||
.stream()
|
||||
.map(TypableStatement::getType)
|
||||
.collect(Collectors.toCollection(HashSet::new))
|
||||
.stream().filter(x -> x instanceof TypePlaceholder)
|
||||
.map(tph -> equality.getOrDefault(tph, (TypePlaceholder) tph))
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
RefTypeOrTPHOrWildcardOrGeneric T2 = superType;
|
||||
|
||||
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
//Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg
|
||||
//betrachtet werden muessen
|
||||
//Definition 7.2 (Family of generated generics). T1 <. R1 <.^∗ R2 <. T2
|
||||
Set<RefTypeOrTPHOrWildcardOrGeneric> T1s =
|
||||
methodCall.getArgumentList()
|
||||
.getArguments()
|
||||
.stream()
|
||||
.map(TypableStatement::getType)
|
||||
.collect(Collectors.toCollection(HashSet::new))
|
||||
.stream().filter(x -> x instanceof TypePlaceholder)
|
||||
.map(tph -> equality.getOrDefault(tph, (TypePlaceholder) tph))
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
RefTypeOrTPHOrWildcardOrGeneric T2 = superType;
|
||||
if (T2 instanceof TypePlaceholder tph) T2 = equality.getOrDefault(tph, tph);
|
||||
|
||||
System.out.println("T1s: " + T1s + "\nT2: " + T2);
|
||||
//Ende
|
||||
|
||||
superType = methodCall.receiverType;
|
||||
methodCall.receiver.accept(this);
|
||||
for(int i = 0;i<methodCall.arglist.getArguments().size();i++){
|
||||
superType = methodCall.signature.get(i);
|
||||
methodCall.arglist.getArguments().get(i).accept(this);
|
||||
System.out.println("T1s: " + T1s + "\nT2: " + T2);
|
||||
//Ende
|
||||
|
||||
superType = methodCall.receiverType;
|
||||
methodCall.receiver.accept(this);
|
||||
for(int i = 0;i<methodCall.arglist.getArguments().size();i++){
|
||||
superType = methodCall.signature.get(i);
|
||||
methodCall.arglist.getArguments().get(i).accept(this);
|
||||
}
|
||||
|
||||
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver) {
|
||||
if (expressionReceiver.expr instanceof This) {
|
||||
var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList());
|
||||
if (optMethod.isEmpty()) return;
|
||||
var method = optMethod.get();
|
||||
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver) {
|
||||
if (expressionReceiver.expr instanceof This) {
|
||||
var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList());
|
||||
if (optMethod.isEmpty()) return;
|
||||
var method = optMethod.get();
|
||||
|
||||
var generics = generics(owner, method).javaGenerics();
|
||||
var generics = generics(owner, method).javaGenerics();
|
||||
|
||||
// transitive and
|
||||
var all = transitiveClosure(generics);
|
||||
// reflexive
|
||||
var toAdd = new HashSet<ResultPair<?, ?>>();
|
||||
for (var generic : all) {
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) generic.getLeft(), (TypePlaceholder) generic.getLeft()));
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
// transitive and
|
||||
var all = transitiveClosure(generics);
|
||||
// reflexive
|
||||
var toAdd = new HashSet<ResultPair<?, ?>>();
|
||||
for (var generic : all) {
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) generic.getLeft(), (TypePlaceholder) generic.getLeft()));
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
|
||||
HashSet<PairTPHsmallerTPH> newPairs = new HashSet<>();
|
||||
HashSet<PairTPHsmallerTPH> newPairs = new HashSet<>();
|
||||
|
||||
// Loop from hell
|
||||
outer:
|
||||
for (var tph : typeVariables) {
|
||||
if (typeVariablesOfClass.contains(tph)) continue;
|
||||
for (var generic : all) {
|
||||
if (!(generic.getRight() instanceof TypePlaceholder type))
|
||||
continue;
|
||||
System.out.println("XXXX");
|
||||
System.out.println(typeVariables);
|
||||
System.out.println(typeVariablesOfClass);
|
||||
System.out.println(simplifiedConstraints);
|
||||
System.out.println(result);
|
||||
System.out.println("XXXX");
|
||||
// Loop from hell
|
||||
outer:
|
||||
for (var tph : typeVariables) {
|
||||
if (typeVariablesOfClass.contains(tph)) continue;
|
||||
for (var generic : all) {
|
||||
if (!(generic.getRight() instanceof TypePlaceholder type))
|
||||
continue;
|
||||
|
||||
for (var pair : simplifiedConstraints) {
|
||||
if (!(pair.left.equals(tph) && pair.right.equals(generic.getLeft())))
|
||||
continue;
|
||||
for (var pair : simplifiedConstraints) {
|
||||
if (!(pair.left.equals(tph) && pair.right.equals(generic.getLeft())))
|
||||
continue;
|
||||
|
||||
for (var tph2 : typeVariables) {
|
||||
for (var pair2 : simplifiedConstraints) {
|
||||
if (!(pair2.right.equals(tph2) && pair2.left.equals(type)))
|
||||
continue;
|
||||
if (tph.equals(tph2)) continue;
|
||||
if (!T1s.contains(tph) || !tph2.equals(T2)) continue;
|
||||
for (var tph2 : typeVariables) {
|
||||
for (var pair2 : simplifiedConstraints) {
|
||||
if (!(pair2.right.equals(tph2) && pair2.left.equals(type)))
|
||||
continue;
|
||||
if (tph.equals(tph2)) continue;
|
||||
if (!T1s.contains(tph) || !tph2.equals(T2)) continue;
|
||||
|
||||
var newPair = new PairTPHsmallerTPH(tph, tph2);
|
||||
newPairs.add(newPair);
|
||||
var newPair = new PairTPHsmallerTPH(tph, tph2);
|
||||
newPairs.add(newPair);
|
||||
|
||||
if (!containsRelation(txResult, newPair))
|
||||
addToPairs(txResult, newPair);
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
simplifiedConstraints.addAll(newPairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!containsRelation(result, newPair))
|
||||
addToPairs(result, newPair);
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
simplifiedConstraints.addAll(newPairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@ -475,18 +448,19 @@ public class ASTToTargetAST {
|
||||
}
|
||||
|
||||
if (minimalPair != null)
|
||||
addToPairs(txResult, minimalPair);
|
||||
addToPairs(result, minimalPair);
|
||||
}
|
||||
|
||||
|
||||
// All unbounded type variables (bounds not in method)
|
||||
outer:
|
||||
for (var typeVariable : typeVariables) {
|
||||
if (typeVariablesOfClass.contains(typeVariable)) continue;
|
||||
for (var pair : txResult) {
|
||||
for (var pair : result) {
|
||||
if (pair.getLeft().equals(typeVariable))
|
||||
continue outer;
|
||||
}
|
||||
addToPairs(txResult, new PairTPHequalRefTypeOrWildcardType(typeVariable, OBJECT));
|
||||
addToPairs(result, new PairTPHequalRefTypeOrWildcardType(typeVariable, OBJECT));
|
||||
}
|
||||
|
||||
// All unbounded bounds
|
||||
@ -497,63 +471,112 @@ public class ASTToTargetAST {
|
||||
continue outer;
|
||||
}
|
||||
if (!typeVariablesOfClass.contains(pair.right) && typeVariables.contains(pair.right)) {
|
||||
addToPairs(txResult, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT));
|
||||
addToPairs(result, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
javaResult.addAll(txResult); // Same result until here
|
||||
private void methodFindTypeVariables(
|
||||
Method method,
|
||||
Set<ResultPair<?, ?>> genericsOfClass,
|
||||
Set<TypePlaceholder> typeVariablesOfClass,
|
||||
Set<TypePlaceholder> typeVariables,
|
||||
Map<TypePlaceholder, TypePlaceholder> equality
|
||||
) {
|
||||
|
||||
for (var pair : genericsOfClass) {
|
||||
typeVariablesOfClass.add((TypePlaceholder) pair.getLeft());
|
||||
}
|
||||
|
||||
typeVariables.addAll(findTypeVariables(method.getReturnType(), equality));
|
||||
for (var arg : method.getParameterList().getFormalparalist()) {
|
||||
typeVariables.addAll(findTypeVariables(arg.getType(), equality));
|
||||
}
|
||||
|
||||
method.block.accept(new TracingStatementVisitor() {
|
||||
@Override
|
||||
public void visit(LocalVarDecl localVarDecl) {
|
||||
typeVariables.addAll(findTypeVariables(localVarDecl.getType(), equality));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
super.visit(methodCall);
|
||||
typeVariables.addAll(findTypeVariables(methodCall.getType(), equality));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Assign assign) {}
|
||||
});
|
||||
}
|
||||
|
||||
// Family of generated Generics
|
||||
Generics generics(ClassOrInterface owner, Method method) {
|
||||
if (computedGenericsOfMethods.containsKey(method))
|
||||
return computedGenericsOfMethods.get(method);
|
||||
|
||||
Set<ResultPair<?, ?>> txResult = new HashSet<>();
|
||||
Set<ResultPair<?, ?>> javaResult = new HashSet<>();
|
||||
var generics = new Generics(javaResult, txResult);
|
||||
computedGenericsOfMethods.put(method, generics);
|
||||
|
||||
var genericsOfClass = generics(owner).javaGenerics();
|
||||
var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints);
|
||||
|
||||
HashSet<TypePlaceholder> txTypeVariables = new HashSet<>();
|
||||
HashSet<TypePlaceholder> javaTypeVariables = new HashSet<>();
|
||||
HashSet<TypePlaceholder> txTypeVariablesOfClass = new HashSet<>();
|
||||
HashSet<TypePlaceholder> javaTypeVariablesOfClass = new HashSet<>();
|
||||
|
||||
methodFindTypeVariables(method, genericsOfClass, javaTypeVariablesOfClass, javaTypeVariables, equality);
|
||||
methodFindTypeVariables(method, genericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality);
|
||||
|
||||
methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, javaTypeVariablesOfClass, javaResult, equality);
|
||||
methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, txTypeVariablesOfClass, txResult, txEquality);
|
||||
|
||||
var eq = new HashMap<>(equality);
|
||||
{ // Java Generics
|
||||
eliminateCycles(javaResult);
|
||||
eliminateInfima(javaResult);
|
||||
eliminateCycles(javaResult, equality);
|
||||
eliminateInfima(javaResult, equality);
|
||||
|
||||
var usedTphs = new HashSet<TypePlaceholder>();
|
||||
// For eliminating inner type variables we need to figure out which ones are actually used
|
||||
// TODO Maybe don't do this twice?
|
||||
for (var param : method.getParameterList().getFormalparalist()) {
|
||||
usedTphs.addAll(findTypeVariables(param.getType()));
|
||||
usedTphs.addAll(findTypeVariables(param.getType(), equality));
|
||||
}
|
||||
usedTphs.addAll(findTypeVariables(method.getReturnType()));
|
||||
usedTphs.addAll(findTypeVariables(method.getReturnType(), equality));
|
||||
var referenced = new HashSet<>(usedTphs);
|
||||
referenced.addAll(typeVariablesOfClass);
|
||||
referenced.addAll(javaTypeVariablesOfClass);
|
||||
|
||||
eliminateInnerTypeVariables(referenced, javaResult);
|
||||
equalizeTypeVariables(javaResult);
|
||||
equalizeTypeVariables(javaResult, equality);
|
||||
usedTPHsOfMethods.put(method, usedTphs);
|
||||
}
|
||||
|
||||
var storedEq = equality; //TODO Hack: pass equality as parameter instead
|
||||
equality = eq;
|
||||
{ // JavaTX Generics
|
||||
eliminateInfima(txResult);
|
||||
eliminateInfima(txResult, txEquality);
|
||||
|
||||
var referenced = new HashSet<TypePlaceholder>();
|
||||
for (var param : method.getParameterList().getFormalparalist()) {
|
||||
referenced.addAll(findTypeVariables(param.getType()));
|
||||
referenced.addAll(findTypeVariables(param.getType(), txEquality));
|
||||
}
|
||||
referenced.addAll(findTypeVariables(method.getReturnType()));
|
||||
referenced.addAll(typeVariablesOfClass);
|
||||
referenced.addAll(findTypeVariables(method.getReturnType(), txEquality));
|
||||
referenced.addAll(txTypeVariablesOfClass);
|
||||
|
||||
eliminateInnerTypeVariables(referenced, txResult);
|
||||
|
||||
// Generate generics with current equality
|
||||
generatedTxGenerics.addAll(convert(txResult));
|
||||
}
|
||||
equality = storedEq;
|
||||
|
||||
System.out.println(method.name + ": " + txResult + " & " + javaResult);
|
||||
|
||||
return generics;
|
||||
}
|
||||
|
||||
void findAllBounds(RefTypeOrTPHOrWildcardOrGeneric type, Set<ResultPair<?, ?>> generics) {
|
||||
void findAllBounds(RefTypeOrTPHOrWildcardOrGeneric type, Set<ResultPair<?, ?>> generics, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
if (type instanceof TypePlaceholder tph) {
|
||||
tph = equality.getOrDefault(tph, tph);
|
||||
|
||||
var concreteType = concreteTypes.get(tph);
|
||||
if (concreteType != null) {
|
||||
findAllBounds(concreteType, generics);
|
||||
findAllBounds(concreteType, generics, equality);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -565,7 +588,7 @@ public class ASTToTargetAST {
|
||||
var pair = new PairTPHsmallerTPH(tph, right);
|
||||
if (!generics.contains(pair)) {
|
||||
generics.add(pair);
|
||||
findAllBounds(right, generics);
|
||||
findAllBounds(right, generics, equality);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
@ -573,7 +596,7 @@ public class ASTToTargetAST {
|
||||
if (!found)
|
||||
generics.add(new PairTPHequalRefTypeOrWildcardType(tph, OBJECT));
|
||||
} else if (type instanceof RefType refType) {
|
||||
refType.getParaList().forEach(t -> findAllBounds(t, generics));
|
||||
refType.getParaList().forEach(t -> findAllBounds(t, generics, equality));
|
||||
}
|
||||
}
|
||||
|
||||
@ -583,38 +606,30 @@ public class ASTToTargetAST {
|
||||
|
||||
Set<ResultPair<?, ?>> txResult = new HashSet<>();
|
||||
Set<ResultPair<?, ?>> javaResult = new HashSet<>();
|
||||
Set<TargetGeneric> generatedTxGenerics = new HashSet<>();
|
||||
var generics = new Generics(javaResult, generatedTxGenerics);
|
||||
|
||||
for (var field : classOrInterface.getFieldDecl()) {
|
||||
findAllBounds(field.getType(), txResult);
|
||||
}
|
||||
var generics = new Generics(javaResult, txResult);
|
||||
computedGenericsOfClasses.put(classOrInterface, generics);
|
||||
|
||||
javaResult.addAll(txResult);
|
||||
for (var field : classOrInterface.getFieldDecl()) {
|
||||
findAllBounds(field.getType(), javaResult, equality);
|
||||
findAllBounds(field.getType(), txResult, txEquality);
|
||||
}
|
||||
|
||||
var eq = new HashMap<>(equality);
|
||||
{ // Java Generics
|
||||
eliminateCycles(javaResult);
|
||||
eliminateInfima(javaResult);
|
||||
eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult);
|
||||
equalizeTypeVariables(javaResult);
|
||||
eliminateCycles(javaResult, equality);
|
||||
eliminateInfima(javaResult, equality);
|
||||
eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality);
|
||||
equalizeTypeVariables(javaResult, equality);
|
||||
}
|
||||
var storedEq = equality; //TODO Hack: pass equality as parameter instead
|
||||
equality = eq;
|
||||
{
|
||||
eliminateInfima(txResult);
|
||||
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult);
|
||||
{ // TX Generics
|
||||
eliminateInfima(txResult, txEquality);
|
||||
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality);
|
||||
}
|
||||
equality = storedEq;
|
||||
|
||||
generatedTxGenerics.addAll(convert(txResult));
|
||||
|
||||
System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult);
|
||||
return generics;
|
||||
}
|
||||
|
||||
void equalizeTypeVariables(Set<ResultPair<?, ?>> input) {
|
||||
void equalizeTypeVariables(Set<ResultPair<?, ?>> input, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
for (var pair : new HashSet<>(input)) {
|
||||
if (pair instanceof PairTPHsmallerTPH ptph) {
|
||||
if (ptph.left.getVariance() == 1 && ptph.right.getVariance() == -1) {
|
||||
@ -631,24 +646,24 @@ public class ASTToTargetAST {
|
||||
}
|
||||
}
|
||||
|
||||
void findTphs(RefTypeOrTPHOrWildcardOrGeneric type, Set<TypePlaceholder> tphs) {
|
||||
void findTphs(RefTypeOrTPHOrWildcardOrGeneric type, Set<TypePlaceholder> tphs, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
if (type instanceof RefType refType) {
|
||||
refType.getParaList().forEach(t -> findTphs(t, tphs));
|
||||
refType.getParaList().forEach(t -> findTphs(t, tphs, equality));
|
||||
} else if (type instanceof TypePlaceholder tph) {
|
||||
tph = equality.getOrDefault(tph, tph);
|
||||
var concreteType = concreteTypes.get(tph);
|
||||
if (concreteType != null) {
|
||||
findTphs(concreteType, tphs);
|
||||
findTphs(concreteType, tphs, equality);
|
||||
return;
|
||||
}
|
||||
tphs.add(tph);
|
||||
}
|
||||
}
|
||||
|
||||
void eliminateInnerTypeVariablesOfClass(ClassOrInterface classOrInterface, Set<ResultPair<?, ?>> input) {
|
||||
void eliminateInnerTypeVariablesOfClass(ClassOrInterface classOrInterface, Set<ResultPair<?, ?>> input, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
Set<TypePlaceholder> referenced = new HashSet<>();
|
||||
for (var field : classOrInterface.getFieldDecl()) {
|
||||
findTphs(field.getType(), referenced);
|
||||
findTphs(field.getType(), referenced, equality);
|
||||
}
|
||||
for (var method : classOrInterface.getMethods()) {
|
||||
generics(classOrInterface, method);
|
||||
@ -700,7 +715,7 @@ public class ASTToTargetAST {
|
||||
input.addAll(output);
|
||||
}
|
||||
|
||||
void eliminateCycles(Set<ResultPair<?, ?>> input) {
|
||||
void eliminateCycles(Set<ResultPair<?, ?>> input, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
var cycles = findCycles(input);
|
||||
for (var cycle : cycles) {
|
||||
var newTph = TypePlaceholder.fresh(new NullToken());
|
||||
@ -716,7 +731,7 @@ public class ASTToTargetAST {
|
||||
}
|
||||
}
|
||||
|
||||
void eliminateInfima(Set<ResultPair<?, ?>> input) {
|
||||
void eliminateInfima(Set<ResultPair<?, ?>> input, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
var foundInfima = false;
|
||||
do {
|
||||
foundInfima = false;
|
||||
@ -758,19 +773,19 @@ public class ASTToTargetAST {
|
||||
} while (foundInfima);
|
||||
}
|
||||
|
||||
RefTypeOrTPHOrWildcardOrGeneric getType(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||
RefTypeOrTPHOrWildcardOrGeneric getType(RefTypeOrTPHOrWildcardOrGeneric type, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
if (type instanceof TypePlaceholder tph) {
|
||||
if (equality.containsKey(tph)) {
|
||||
return getType(equality.get(tph));
|
||||
return getType(equality.get(tph), equality);
|
||||
}
|
||||
return concreteTypes.getOrDefault(tph, tph);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
TargetType getTargetType(TypePlaceholder tph) {
|
||||
TargetType getTargetType(TypePlaceholder tph, Map<TypePlaceholder, TypePlaceholder> equality) {
|
||||
if (equality.containsKey(tph)) {
|
||||
return getTargetType(equality.get(tph));
|
||||
return getTargetType(equality.get(tph), equality);
|
||||
}
|
||||
var type = concreteTypes.get(tph);
|
||||
if (type == null) return new TargetGenericType(tph.getName());
|
||||
@ -944,7 +959,7 @@ public class ASTToTargetAST {
|
||||
// Generate generics only if there are no user defined ones
|
||||
var generics = sigma.generics(input);
|
||||
javaGenerics = convert(generics.javaGenerics());
|
||||
txGenerics = generics.txGenerics();
|
||||
txGenerics = convert(generics.txGenerics());
|
||||
}
|
||||
|
||||
TargetBlock fieldInitializer = null;
|
||||
@ -970,8 +985,8 @@ public class ASTToTargetAST {
|
||||
return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName()));
|
||||
}
|
||||
|
||||
private Set<TargetGeneric> collectMethodGenerics(Set<TargetGeneric> generics, Method input) {
|
||||
var convertedGenerics = new HashSet<>(generics);
|
||||
private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Map<TypePlaceholder, TypePlaceholder> equality, Method input) {
|
||||
var convertedGenerics = new HashSet<>(convert(generics));
|
||||
outer:
|
||||
for (GenericTypeVar typeVar : input.getGenerics()) {
|
||||
for (var classGeneric : currentClass.getGenerics()) {
|
||||
@ -981,12 +996,12 @@ public class ASTToTargetAST {
|
||||
}
|
||||
convertedGenerics.addAll(convert(typeVar));
|
||||
}
|
||||
var returnType = sigma.getType(input.getReturnType());
|
||||
var returnType = sigma.getType(input.getReturnType(), equality);
|
||||
if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) {
|
||||
convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT)));
|
||||
}
|
||||
for (var param : input.getParameterList()) {
|
||||
var type = sigma.getType(param.getType());
|
||||
var type = sigma.getType(param.getType(), equality);
|
||||
if (type instanceof GenericRefType refType && !hasGeneric(convertedGenerics, refType)) {
|
||||
convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT)));
|
||||
}
|
||||
@ -1005,8 +1020,8 @@ 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(convert(generics.javaGenerics()), input);
|
||||
var txGenerics = collectMethodGenerics(generics.txGenerics(), input);
|
||||
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input);
|
||||
var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input);
|
||||
|
||||
result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer));
|
||||
parameterSet.add(params);
|
||||
@ -1026,8 +1041,8 @@ 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(convert(generics.javaGenerics()), input);
|
||||
var txGenerics = collectMethodGenerics(generics.txGenerics(), input);
|
||||
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input);
|
||||
var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input);
|
||||
|
||||
result.add(new TargetMethod(
|
||||
input.modifier,
|
||||
@ -1099,7 +1114,7 @@ public class ASTToTargetAST {
|
||||
|
||||
@Override
|
||||
public TargetType visit(TypePlaceholder typePlaceholder) {
|
||||
return sigma.getTargetType(typePlaceholder);
|
||||
return sigma.getTargetType(typePlaceholder, sigma.equality);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,15 +18,14 @@ public class GenericsResult {
|
||||
public Set<ResultPair<?, ?>> get(ClassOrInterface clazz) {
|
||||
var generics = this.sigma.computedGenericsOfClasses.get(clazz);
|
||||
if (generics == null) return Set.of();
|
||||
return generics.javaGenerics();
|
||||
return generics.txGenerics();
|
||||
}
|
||||
|
||||
// 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.javaGenerics();
|
||||
return generics.txGenerics();
|
||||
}
|
||||
|
||||
public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) {
|
||||
@ -68,7 +67,13 @@ public class GenericsResult {
|
||||
|
||||
public RefTypeOrTPHOrWildcardOrGeneric resolve(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||
if (type instanceof TypePlaceholder tph)
|
||||
return this.sigma.getType(tph);
|
||||
return this.sigma.getType(tph, sigma.equality);
|
||||
return type;
|
||||
}
|
||||
|
||||
public RefTypeOrTPHOrWildcardOrGeneric resolveTx(RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||
if (type instanceof TypePlaceholder tph)
|
||||
return this.sigma.getType(tph, sigma.txEquality);
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user