forked from JavaTX/JavaCompilerCore
reduceExt
This commit is contained in:
parent
2483044e0c
commit
c08a8fd347
src/de/dhbwstuttgart
syntaxtree/factory
typeinference/unifynew
typinference/unify/model
@ -1,15 +1,13 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.factory;
|
package de.dhbwstuttgart.syntaxtree.factory;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.Type;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
|
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
||||||
import de.dhbwstuttgart.typeinference.Menge;
|
import de.dhbwstuttgart.typeinference.Menge;
|
||||||
import de.dhbwstuttgart.typeinference.Pair;
|
|
||||||
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
|
|
||||||
|
|
||||||
public class UnifyTypeFactory {
|
public class UnifyTypeFactory {
|
||||||
|
|
||||||
|
@ -84,8 +84,34 @@ public class RuleSet implements IRuleSet{
|
|||||||
else if(lhsType instanceof ExtendsType)
|
else if(lhsType instanceof ExtendsType)
|
||||||
lhsSType = (SimpleType) ((ExtendsType) lhsType).GetExtendedType();
|
lhsSType = (SimpleType) ((ExtendsType) lhsType).GetExtendedType();
|
||||||
else
|
else
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
return null;
|
if(lhsSType.getTypeParams().empty())
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
Type rhsType = pair.getRhsType();
|
||||||
|
|
||||||
|
if(!(rhsType instanceof ExtendsType))
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
SimpleType rhsSType = (SimpleType) ((ExtendsType) rhsType).GetExtendedType();
|
||||||
|
|
||||||
|
if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size())
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
int[] pi = pi(lhsSType, rhsType);
|
||||||
|
|
||||||
|
if(pi.length == 0)
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
Type[] rhsTypeParams = rhsSType.getTypeParams().asArray();
|
||||||
|
Type[] lhsTypeParams = lhsSType.getTypeParams().asArray();
|
||||||
|
Set<MPair> result = new HashSet<>();
|
||||||
|
|
||||||
|
for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++)
|
||||||
|
result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOTWC));
|
||||||
|
|
||||||
|
return Optional.of(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -116,7 +142,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
SimpleType lhsSType = (SimpleType) lhsType;
|
SimpleType lhsSType = (SimpleType) lhsType;
|
||||||
SimpleType rhsSType = (SimpleType) rhsType;
|
SimpleType rhsSType = (SimpleType) rhsType;
|
||||||
|
|
||||||
if(lhsSType.getTypeParams().empty()|| rhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size())
|
if(lhsSType.getTypeParams().empty() || rhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
int[] pi = pi(lhsSType, rhsSType);
|
int[] pi = pi(lhsSType, rhsSType);
|
||||||
@ -129,7 +155,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
Set<MPair> result = new HashSet<>();
|
Set<MPair> result = new HashSet<>();
|
||||||
|
|
||||||
for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++)
|
for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++)
|
||||||
result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOT));
|
result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOTWC));
|
||||||
|
|
||||||
return Optional.of(result);
|
return Optional.of(result);
|
||||||
}
|
}
|
||||||
@ -166,11 +192,12 @@ public class RuleSet implements IRuleSet{
|
|||||||
else
|
else
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
if(rhsSType.getTypeParams().empty())
|
if(!rhsSType.getName().equals(lhsSType.getName()))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size())
|
Assert.assertEquals(lhsSType.getTypeParams().size(), rhsSType.getTypeParams().size());
|
||||||
return Optional.empty();
|
//if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size())
|
||||||
|
// return Optional.empty();
|
||||||
|
|
||||||
Set<MPair> result = new HashSet<>();
|
Set<MPair> result = new HashSet<>();
|
||||||
|
|
||||||
@ -261,17 +288,31 @@ public class RuleSet implements IRuleSet{
|
|||||||
* @return An array containing the values of pi for every type argument of C or an empty array if the search failed.
|
* @return An array containing the values of pi for every type argument of C or an empty array if the search failed.
|
||||||
*/
|
*/
|
||||||
private int[] pi(Type C, Type D) {
|
private int[] pi(Type C, Type D) {
|
||||||
Type dFromFc = finiteClosure.getType(D.getName());
|
Type cFromFc = finiteClosure.getType(C.getName());
|
||||||
if(dFromFc == null)
|
if(cFromFc == null)
|
||||||
return new int[0];
|
return new int[0];
|
||||||
|
|
||||||
Set<Type> smallerRhs = finiteClosure.smaller(dFromFc);
|
Optional<Type> opt = Optional.empty();
|
||||||
Optional<Type> opt = smallerRhs.stream().filter(x -> x.getName().equals(C.getName())).findAny();
|
if(D instanceof ExtendsType) {
|
||||||
|
SimpleType dSType = (SimpleType) ((ExtendsType) D).GetExtendedType();
|
||||||
|
opt = finiteClosure.grArg(cFromFc).stream()
|
||||||
|
.filter(x -> x instanceof ExtendsType)
|
||||||
|
.filter(x -> ((ExtendsType) x).GetExtendedType().equals(dSType.getName())).findAny();
|
||||||
|
}
|
||||||
|
else if(D instanceof SuperType) {
|
||||||
|
SimpleType dSType = (SimpleType) ((SuperType) D).GetSuperedType();
|
||||||
|
opt = finiteClosure.grArg(cFromFc).stream()
|
||||||
|
.filter(x -> x instanceof SuperType)
|
||||||
|
.filter(x -> ((SuperType) x).GetSuperedType().equals(dSType.getName())).findAny();
|
||||||
|
}
|
||||||
|
else if (D instanceof SimpleType)
|
||||||
|
opt = finiteClosure.greater(cFromFc).stream()
|
||||||
|
.filter(x -> x.getName().equals(D.getName())).findAny();
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return new int[0];
|
return new int[0];
|
||||||
|
|
||||||
Type cFromFc = opt.get();
|
Type dFromFc = opt.get();
|
||||||
|
|
||||||
Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size());
|
Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size());
|
||||||
Assert.assertTrue(dFromFc.getTypeParams().size() > 0);
|
Assert.assertTrue(dFromFc.getTypeParams().size() > 0);
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typinference.unify.model;
|
||||||
|
|
||||||
public class ExtendsType extends Type {
|
public class ExtendsType extends Type {
|
||||||
|
private Type extendedType;
|
||||||
|
|
||||||
|
public ExtendsType(Type extendedType) {
|
||||||
|
this.extendedType = extendedType;
|
||||||
|
}
|
||||||
|
|
||||||
public Type GetExtendedType() {
|
public Type GetExtendedType() {
|
||||||
return null;
|
return extendedType;
|
||||||
// TODO
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeParams getTypeParams() {
|
||||||
|
return extendedType.getTypeParams();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,4 +16,9 @@ public class SuperType extends Type {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "? super " + superedType;
|
return "? super " + superedType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeParams getTypeParams() {
|
||||||
|
return superedType.getTypeParams();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user