diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index eeff2a45b..629524a3a 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -45,6 +45,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import de.dhbwstuttgart.util.BiRelation; import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; import de.dhbwstuttgart.typeinference.unify.UnifyResultListener; import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl; @@ -71,7 +72,7 @@ public class JavaTXCompiler { public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); private final DirectoryClassLoader classLoader; - private Map, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); + private BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 05b0050a5..1d5fde1f7 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -9,6 +9,8 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.util.BiRelation; + import java.util.*; public class TYPE { @@ -16,7 +18,7 @@ public class TYPE { private final Collection sfs; private final TypeInferenceInformation typeInferenceInformation; - private Map, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); + private BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); public TYPE(Collection sourceFiles, Collection allAvailableClasses){ sfs = sourceFiles; @@ -24,7 +26,7 @@ public class TYPE { } - public Map, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { + public BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { return methCallargTypesRetType; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index eb6f5b755..447ec9532 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -21,6 +21,7 @@ import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; +import de.dhbwstuttgart.util.BiRelation; import java.util.*; import java.util.stream.Collectors; @@ -31,8 +32,8 @@ public class TYPEStmt implements StatementVisitor{ private final TypeInferenceBlockInformation info; private final ConstraintSet constraintsSet = new ConstraintSet(); - - Map, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); + private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken()); + private BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); public TYPEStmt(TypeInferenceBlockInformation info){ this.info = info; @@ -42,7 +43,7 @@ public class TYPEStmt implements StatementVisitor{ return constraintsSet; } - public Map, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { + public BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { return methCallargTypesRetType; } @@ -64,7 +65,8 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(ArgumentList arglist) { for(int i = 0;i oderConstraints = new HashSet<>(); for(FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)){ @@ -140,14 +146,17 @@ public class TYPEStmt implements StatementVisitor{ public void visit(IfStmt ifStmt) { RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); //Expression inferieren: + superType = new Void(new NullToken()); ifStmt.expr.accept(this); //Expression muss boolean sein: constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); //Blöcke inferieren: + superType = new Void(new NullToken()); ifStmt.then_block.accept(this); //Beide Blöcke müssen den gleichen Supertyp haben, welcher den Rückgabetyp des If-Stmts darstellt constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT)); if(ifStmt.else_block != null){ + superType = new Void(new NullToken()); ifStmt.else_block.accept(this); constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT)); } @@ -173,14 +182,24 @@ public class TYPEStmt implements StatementVisitor{ //Es wird in OderConstraints davon ausgegangen dass die Bedingungen für die Typen der Argumente links stehen //und die Typen der Rückgabewerte immer rechts stehen (vgl. JavaTXCompiler) public void visit(MethodCall methodCall) { - Map, RefTypeOrTPHOrWildcardOrGeneric> argTypesRetType = new HashMap<>(); - argTypesRetType.put(methodCall.getArgumentList() - .getArguments() - .stream() - .map(x -> x.getType()) - .collect(Collectors.toCollection(HashSet::new)), - methodCall.getType()); - methCallargTypesRetType.put(methodCall.name, argTypesRetType); + + //Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg + //betrachtet werden muessen + BiRelation, RefTypeOrTPHOrWildcardOrGeneric> argTypesRetType = new BiRelation<>(); + Set argTphSet = methodCall.getArgumentList() + .getArguments() + .stream() + .map(x -> x.getType()) + .collect(Collectors.toCollection(HashSet::new)) + .stream().filter(x -> x instanceof TypePlaceholder) + .collect(Collectors.toCollection(HashSet::new)); + if (superType instanceof TypePlaceholder) { + argTypesRetType.put(argTphSet, superType); + methCallargTypesRetType.put(methodCall.name, argTypesRetType); + } + //Ende + + superType = methodCall.receiver.getType(); methodCall.receiver.accept(this); //Overloading: Set> methodConstraints = new HashSet<>(); @@ -226,6 +245,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(ExpressionReceiver receiver) { + superType = new Void(new NullToken()); receiver.expr.accept(this); } @@ -258,7 +278,9 @@ public class TYPEStmt implements StatementVisitor{ //Es wird in OderConstraints davon ausgegangen dass die Bedingungen für die Typen der Argumente links stehen //und die Typen der Rückgabewerte immer rechts stehen (vgl. JavaTXCompiler) public void visit(BinaryExpr binary) { + superType = new Void(new NullToken()); binary.lexpr.accept(this); + superType = new Void(new NullToken()); binary.rexpr.accept(this); if(binary.operation.equals(BinaryExpr.Operator.DIV) || binary.operation.equals(BinaryExpr.Operator.MUL)|| @@ -505,6 +527,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(Return returnExpr) { + superType = returnExpr.getType(); returnExpr.retexpr.accept(this); constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT)); } @@ -555,10 +578,12 @@ public class TYPEStmt implements StatementVisitor{ public void visit(WhileStmt whileStmt) { RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); //Expression inferieren: + superType = new Void(new NullToken()); whileStmt.expr.accept(this); //Expression muss boolean sein: constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); //LoopBlock inferieren: + superType = new Void(new NullToken()); whileStmt.loopBlock.accept(this); } @@ -571,6 +596,7 @@ public class TYPEStmt implements StatementVisitor{ public void visit(AssignToField assignLeftSide) { //Hier ist kein Code nötig. Es werden keine extra Constraints generiert //HIER muss Code rein PL 2018-10-24 + superType = new Void(new NullToken()); assignLeftSide.field.accept(this); } @@ -622,6 +648,7 @@ public class TYPEStmt implements StatementVisitor{ TypeInferenceBlockInformation info, GenericsResolver resolver) { Set ret = new HashSet<>(); for(int i = 0;i extends ArrayList>{ + + public void put(X x, Y y) { + this.add(new Pair<>(x,y)); + } + + public void put(Pair p) { + this.add(p); + } + + + public void putAll(BiRelation br) { + this.addAll(br); + } +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Pair.java b/src/main/java/de/dhbwstuttgart/util/Pair.java similarity index 74% rename from src/main/java/de/dhbwstuttgart/typeinference/unify/model/Pair.java rename to src/main/java/de/dhbwstuttgart/util/Pair.java index de05c97df..3046afdab 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Pair.java +++ b/src/main/java/de/dhbwstuttgart/util/Pair.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typeinference.unify.model; +package de.dhbwstuttgart.util; import java.util.Optional; @@ -20,6 +20,6 @@ public class Pair { } public String toString() { - return "(" + key.toString() + "," + "," + value.toString() + ")\n"; + return "(" + key.toString() + "," + value.toString() + ")\n"; } }