From 0944cf59fbdc67842f35be4a8ca7056d37a3859b Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 17 Jan 2023 17:22:22 +0100 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java new file: src/main/java/de/dhbwstuttgart/util/BiRelation.java renamed: src/main/java/de/dhbwstuttgart/typeinference/unify/model/Pair.java -> src/main/java/de/dhbwstuttgart/util/Pair.java --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 3 +- .../typeinference/typeAlgo/TYPE.java | 6 ++- .../typeinference/typeAlgo/TYPEStmt.java | 51 ++++++++++++++----- .../typeinference/unify/TypeUnifyTask.java | 2 +- .../unify/model/FiniteClosure.java | 1 + .../unify/model/OrderingUnifyPair.java | 1 + .../de/dhbwstuttgart/util/BiRelation.java | 19 +++++++ .../unify/model => util}/Pair.java | 4 +- 8 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/util/BiRelation.java rename src/main/java/de/dhbwstuttgart/{typeinference/unify/model => util}/Pair.java (74%) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index eeff2a45..629524a3 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 05b0050a..1d5fde1f 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 eb6f5b75..447ec953 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 de05c97d..3046afda 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"; } }