forked from JavaTX/JavaCompilerCore
adapt revisit
This commit is contained in:
parent
be6a719433
commit
3f4f2cd27b
@ -1,5 +1,6 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
||||||
@ -48,5 +49,5 @@ public interface IFiniteClosure {
|
|||||||
public Set<Type> grArg(PlaceholderType type);
|
public Set<Type> grArg(PlaceholderType type);
|
||||||
public Set<Type> smArg(PlaceholderType type);
|
public Set<Type> smArg(PlaceholderType type);
|
||||||
|
|
||||||
public Type getType(Type type);
|
public Optional<Type> getGenericType(String typeName);
|
||||||
}
|
}
|
||||||
|
@ -308,42 +308,45 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
Type typeD = pair.getLhsType();
|
||||||
if(!(lhsType instanceof SimpleType))
|
if(!(typeD instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
Type typeDs = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SimpleType))
|
if(!(typeDs instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0)
|
if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
if(lhsType.getName().equals(rhsType.getName()))
|
if(typeD.getName().equals(typeDs.getName()))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsFromFc = finiteClosure.getType(lhsType);
|
Optional<Type> opt = finiteClosure.getGenericType(typeD.getName());
|
||||||
|
|
||||||
if(lhsFromFc == null)
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Set<Type> greater = finiteClosure.greater(lhsFromFc);
|
// The generic Version of Type D (D<a1, a2, a3, ... >)
|
||||||
|
Type typeDgen = opt.get();
|
||||||
Optional<Type> opt = greater.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny();
|
|
||||||
|
// Actually greater+ because the types are ensured to have different names
|
||||||
|
Set<Type> greater = finiteClosure.greater(typeDgen);
|
||||||
|
opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type newLhs = opt.get();
|
Type newLhs = opt.get();
|
||||||
|
|
||||||
TypeParams lhsTypeParams = lhsType.getTypeParams();
|
TypeParams typeDParams = typeD.getTypeParams();
|
||||||
TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams();
|
TypeParams typeDgenParams = typeDgen.getTypeParams();
|
||||||
|
|
||||||
Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0));
|
Unifier unif = new Unifier(typeDgenParams.get(0), typeDParams.get(0));
|
||||||
for(int i = 1; i < lhsTypeParams.size(); i++)
|
for(int i = 1; i < typeDParams.size(); i++)
|
||||||
unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i)));
|
unif.andThen(new Unifier(typeDgenParams.get(i), typeDParams.get(i)));
|
||||||
|
|
||||||
return Optional.of(new MPair(newLhs.apply(unif), rhsType, PairOperator.SMALLERDOT));
|
return Optional.of(new MPair(newLhs.apply(unif), typeDs, PairOperator.SMALLERDOT));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -451,11 +454,15 @@ public class RuleSet implements IRuleSet{
|
|||||||
private int[] pi(Type C, Type D) {
|
private int[] pi(Type C, Type D) {
|
||||||
Type cFromFc = null;
|
Type cFromFc = null;
|
||||||
if(C instanceof SimpleType)
|
if(C instanceof SimpleType)
|
||||||
cFromFc = finiteClosure.getType(C);
|
cFromFc = finiteClosure.getGenericType(C.getName()).orElse(null);
|
||||||
else if(C instanceof ExtendsType)
|
else if(C instanceof ExtendsType) {
|
||||||
cFromFc = new ExtendsType(finiteClosure.getType(((ExtendsType) C).getExtendedType()));
|
Optional<Type> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
|
||||||
else if(C instanceof SuperType)
|
if(opt.isPresent()) cFromFc = new ExtendsType(opt.get());
|
||||||
cFromFc = new SuperType(finiteClosure.getType(((SuperType) C).getSuperedType()));
|
}
|
||||||
|
else if(C instanceof SuperType) {
|
||||||
|
Optional<Type> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
|
||||||
|
if(opt.isPresent()) cFromFc = new SuperType(opt.get());
|
||||||
|
}
|
||||||
|
|
||||||
if(cFromFc == null)
|
if(cFromFc == null)
|
||||||
return new int[0];
|
return new int[0];
|
||||||
|
@ -2,6 +2,7 @@ package de.dhbwstuttgart.typinference.unify.model;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
@ -64,7 +65,6 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all types of the finite closure that are supertypes of the argument.
|
* Returns all types of the finite closure that are supertypes of the argument.
|
||||||
* @return The set of supertypes of the argument.
|
* @return The set of supertypes of the argument.
|
||||||
@ -79,7 +79,6 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(Type type) {
|
public Set<Type> grArg(Type type) {
|
||||||
return type.grArg(this);
|
return type.grArg(this);
|
||||||
@ -192,18 +191,18 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type getType(Type type) {
|
public Optional<Type> getGenericType(String typeName) {
|
||||||
if(!strInheritanceGraph.containsKey(type.getName()))
|
if(!strInheritanceGraph.containsKey(typeName))
|
||||||
return null;
|
return Optional.empty();
|
||||||
|
|
||||||
HashSet<Node<Type>> candidates = strInheritanceGraph.get(type.getName());
|
HashSet<Node<Type>> candidates = strInheritanceGraph.get(typeName);
|
||||||
|
|
||||||
for(Node<Type> node : candidates) {
|
for(Node<Type> node : candidates) {
|
||||||
Type candidate = node.getContent();
|
Type candidate = node.getContent();
|
||||||
if(candidate.getTypeParams().arePlaceholders())
|
if(candidate.getTypeParams().arePlaceholders())
|
||||||
return candidate;
|
return Optional.of(candidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user