diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 8c43c826..f31255e0 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -3,16 +3,19 @@ package de.dhbwstuttgart.core; import de.dhbwstuttgart.environment.CompilationEnvironment; import de.dhbwstuttgart.parser.JavaTXParser; +import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -21,6 +24,7 @@ import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; import de.dhbwstuttgart.typeinference.unify.RuleSet; import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import java.io.File; @@ -28,6 +32,7 @@ import java.io.FileWriter; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; public class JavaTXCompiler { @@ -99,14 +104,53 @@ public class JavaTXCompiler { try { FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/test/logFiles/"+"log")); logFile.write("FC:\\" + finiteClosure.toString()+"\n"); + for(SourceFile sf : this.sourceFiles.values()) { + logFile.write(ASTTypePrinter.print(sf)); + } logFile.flush(); for (List> xCons : unifyCons.cartesianProduct()) { Set xConsSet = new HashSet<>(); for (Constraint constraint : xCons) { xConsSet.addAll(constraint); } - + //.collect(Collectors.toCollection(ArrayList::new)))) System.out.println(xConsSet); + Set paraTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().map(y -> y.getParameterList().getFormalparalist() + .stream().filter(z -> z.getType() instanceof TypePlaceholder) + .map(z -> ((TypePlaceholder)z.getType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;} ).get()) + .reduce((a,b) -> { a.addAll(b); return a;} ).get(); + + Set returnTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder) + .map(z -> ((TypePlaceholder)z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;} ).get(); + + + xConsSet = xConsSet.stream().map(x -> { + if ((x.getLhsType() instanceof PlaceholderType)) { + if (paraTypeVarNames.contains(x.getLhsType().getName()) + && (!(x.getRhsType() instanceof PlaceholderType) + || ((x.getRhsType() instanceof PlaceholderType) && !paraTypeVarNames.contains(x.getRhsType().getName()) && !returnTypeVarNames.contains(x.getRhsType().getName())))) { + x.setVariance((byte)1); + } + if (returnTypeVarNames.contains(x.getLhsType().getName()) + && (!(x.getRhsType() instanceof PlaceholderType) + || ((x.getRhsType() instanceof PlaceholderType) && !paraTypeVarNames.contains(x.getRhsType().getName()) && !returnTypeVarNames.contains(x.getRhsType().getName())))) { + x.setVariance((byte)-1); + } + } + if ((x.getRhsType() instanceof PlaceholderType)) { + if (paraTypeVarNames.contains(x.getRhsType().getName()) + && (!(x.getLhsType() instanceof PlaceholderType) + || ((x.getLhsType() instanceof PlaceholderType) && !paraTypeVarNames.contains(x.getLhsType().getName()) && !returnTypeVarNames.contains(x.getLhsType().getName())))) { + x.setVariance((byte)1); + } + if (returnTypeVarNames.contains(x.getRhsType().getName()) + && (!(x.getLhsType() instanceof PlaceholderType) + || ((x.getLhsType() instanceof PlaceholderType) && !paraTypeVarNames.contains(x.getLhsType().getName()) && !returnTypeVarNames.contains(x.getLhsType().getName())))) { + x.setVariance((byte)-1); + } + } + return x; + }).collect(Collectors.toCollection(HashSet::new)); Set> result = unify.unifySequential(xConsSet, finiteClosure, logFile); //Set> result = unify.unify(xConsSet, finiteClosure); System.out.println("RESULT: " + result); diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 73f6eff9..ec383afe 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -103,7 +103,7 @@ public class TypeUnifyTask extends RecursiveTask>> { * Step 1: Repeated application of reduce, adapt, erase, swap */ writeLog("Unifikation: " + eq.toString()); - eq = eq.stream().map(x -> {x.setVariance((byte)-1); return x;}).collect(Collectors.toCollection(HashSet::new)); + //eq = eq.stream().map(x -> {x.setVariance((byte)-1); return x;}).collect(Collectors.toCollection(HashSet::new)); Set eq0 = applyTypeUnificationRules(eq, fc); /* @@ -311,6 +311,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set> computeCartesianRecursive(Set> fstElems, ArrayList>> topLevelSets, Set eq, IFiniteClosure fc, boolean parallel) { ArrayList>> remainingSets = new ArrayList<>(topLevelSets); Set> nextSet = remainingSets.remove(0); + writeLog("nextSet: " + nextSet.toString()); List> nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet); Set> result = new HashSet<>(); byte variance = nextSetasList.iterator().next().iterator().next().getVariance(); @@ -610,7 +611,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { //System.out.println(pair); if (first) { //writeLog(pair.toString()+"\n"); - Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), (byte)1, fc); + Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), pair.getVariance(), fc); //System.out.println(x1); result.get(0).add(x1); } @@ -626,7 +627,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType) if (first) { //writeLog(pair.toString()+"\n"); - result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), (byte)0, fc)); + result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -639,7 +640,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 3: (a <.? ? sup Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) if (first) { //writeLog(pair.toString()+"\n"); - result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, (byte)0, fc)); + result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -659,7 +660,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if (first) { //writeLog(pair.toString()+"\n"); if (rhsType.getName().equals("A")) System.out.println(); - result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, (byte)-1, fc)); + result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -683,7 +684,7 @@ public class TypeUnifyTask extends RecursiveTask>> { else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) if (first) { //writeLog(pair.toString()+"\n"); result.get(7).add( - unifyCase8(lhsType, (PlaceholderType) rhsType, (byte)0, fc)); + unifyCase8(lhsType, (PlaceholderType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 55576bf0..12943305 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -67,6 +67,11 @@ public class OrderingUnifyPair extends Ordering> { } } + /* TODO muss noch verifiziert werden PL 2018-03-21 + * (non-Javadoc) + * fuehrt zu Fehlern bei Arrays.sort (contract nicht erfuellt) + * @see com.google.common.collect.Ordering#compare(java.lang.Object, java.lang.Object) + */ public int compare (Set left, Set right) { Set lefteq = left.stream() .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index b5bf13fa..780d05a5 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -26,6 +26,13 @@ public class UnifyPair { */ private PairOperator pairOp; + /** + * variance shows the variance of the pair + * -1: contravariant + * 1 covariant + * 0 invariant + * PL 2018-03-21 + */ private byte variance = 0; private boolean undefinedPair = false; @@ -129,7 +136,7 @@ public class UnifyPair { @Override public String toString() { - return "(" + lhs + " " + pairOp + " " + rhs + ")"; + return "(" + lhs + " " + pairOp + " " + rhs + ", " + variance + ")"; } /*