diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 428c08e2..6bbd023f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -1,15 +1,13 @@ 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.ObjectType; import de.dhbwstuttgart.syntaxtree.type.RefType; 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.Pair; -import de.dhbwstuttgart.typeinference.Pair.PairOperator; public class UnifyTypeFactory { diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index fca0e120..cc4c7dbf 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -84,8 +84,34 @@ public class RuleSet implements IRuleSet{ else if(lhsType instanceof ExtendsType) lhsSType = (SimpleType) ((ExtendsType) lhsType).GetExtendedType(); 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 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 @@ -116,7 +142,7 @@ public class RuleSet implements IRuleSet{ SimpleType lhsSType = (SimpleType) lhsType; 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(); int[] pi = pi(lhsSType, rhsSType); @@ -129,7 +155,7 @@ public class RuleSet implements IRuleSet{ Set result = new HashSet<>(); 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); } @@ -165,12 +191,13 @@ public class RuleSet implements IRuleSet{ rhsSType = (SimpleType) ((SuperType) rhsType).GetSuperedType(); else return Optional.empty(); - - if(rhsSType.getTypeParams().empty()) + + if(!rhsSType.getName().equals(lhsSType.getName())) return Optional.empty(); - if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) - return Optional.empty(); + Assert.assertEquals(lhsSType.getTypeParams().size(), rhsSType.getTypeParams().size()); + //if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) + // return Optional.empty(); Set 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. */ private int[] pi(Type C, Type D) { - Type dFromFc = finiteClosure.getType(D.getName()); - if(dFromFc == null) + Type cFromFc = finiteClosure.getType(C.getName()); + if(cFromFc == null) return new int[0]; - Set smallerRhs = finiteClosure.smaller(dFromFc); - Optional opt = smallerRhs.stream().filter(x -> x.getName().equals(C.getName())).findAny(); + Optional opt = Optional.empty(); + 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()) return new int[0]; - Type cFromFc = opt.get(); + Type dFromFc = opt.get(); Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size()); Assert.assertTrue(dFromFc.getTypeParams().size() > 0); diff --git a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java index ca7f5c32..7b26ca17 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java @@ -1,9 +1,18 @@ package de.dhbwstuttgart.typinference.unify.model; public class ExtendsType extends Type { + private Type extendedType; + + public ExtendsType(Type extendedType) { + this.extendedType = extendedType; + } public Type GetExtendedType() { - return null; - // TODO + return extendedType; + } + + @Override + public TypeParams getTypeParams() { + return extendedType.getTypeParams(); } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java index a5377bb8..a62fd9f3 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java @@ -16,4 +16,9 @@ public class SuperType extends Type { public String toString() { return "? super " + superedType; } + + @Override + public TypeParams getTypeParams() { + return superedType.getTypeParams(); + } }