forked from JavaTX/JavaCompilerCore
modified: src/de/dhbwstuttgart/core/JavaTXCompiler.java
Variancebestimmung fuer Argument- und Returntypen der Methoden eingefuegt.
This commit is contained in:
parent
46255efb70
commit
f107c8d1f7
@ -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<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()) {
|
||||
Set<UnifyPair> xConsSet = new HashSet<>();
|
||||
for (Constraint<UnifyPair> constraint : xCons) {
|
||||
xConsSet.addAll(constraint);
|
||||
}
|
||||
|
||||
//.collect(Collectors.toCollection(ArrayList::new))))
|
||||
System.out.println(xConsSet);
|
||||
Set<String> 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<String> 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<Set<UnifyPair>> result = unify.unifySequential(xConsSet, finiteClosure, logFile);
|
||||
//Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
||||
System.out.println("RESULT: " + result);
|
||||
|
@ -103,7 +103,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
* 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<UnifyPair> eq0 = applyTypeUnificationRules(eq, fc);
|
||||
|
||||
/*
|
||||
@ -311,6 +311,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
|
||||
ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
|
||||
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
|
||||
writeLog("nextSet: " + nextSet.toString());
|
||||
List<Set<UnifyPair>> nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet);
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
byte variance = nextSetasList.iterator().next().iterator().next().getVariance();
|
||||
@ -610,7 +611,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) {
|
||||
//System.out.println(pair);
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
Set<Set<UnifyPair>> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), (byte)1, fc);
|
||||
Set<Set<UnifyPair>> 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<Set<Set<UnifyPair>>> {
|
||||
// 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<UnifyPair> s1 = new HashSet<>();
|
||||
@ -639,7 +640,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
// 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<UnifyPair> s1 = new HashSet<>();
|
||||
@ -659,7 +660,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
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<UnifyPair> s1 = new HashSet<>();
|
||||
@ -683,7 +684,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
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<UnifyPair> s1 = new HashSet<>();
|
||||
|
@ -67,6 +67,11 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
|
||||
}
|
||||
}
|
||||
|
||||
/* 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<UnifyPair> left, Set<UnifyPair> right) {
|
||||
Set<UnifyPair> lefteq = left.stream()
|
||||
.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT))
|
||||
|
@ -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 + ")";
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user