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
This commit is contained in:
pl@gohorb.ba-horb.de 2023-01-17 17:22:22 +01:00
parent e8eaa3ac6e
commit 0944cf59fb
8 changed files with 69 additions and 18 deletions

View File

@ -45,6 +45,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.util.BiRelation;
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
import de.dhbwstuttgart.typeinference.unify.UnifyResultListener; import de.dhbwstuttgart.typeinference.unify.UnifyResultListener;
import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl; import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl;
@ -71,7 +72,7 @@ public class JavaTXCompiler {
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
private final DirectoryClassLoader classLoader; private final DirectoryClassLoader classLoader;
private Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {

View File

@ -9,6 +9,8 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.util.BiRelation;
import java.util.*; import java.util.*;
public class TYPE { public class TYPE {
@ -16,7 +18,7 @@ public class TYPE {
private final Collection<SourceFile> sfs; private final Collection<SourceFile> sfs;
private final TypeInferenceInformation typeInferenceInformation; private final TypeInferenceInformation typeInferenceInformation;
private Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
public TYPE(Collection<SourceFile> sourceFiles, Collection<ClassOrInterface> allAvailableClasses){ public TYPE(Collection<SourceFile> sourceFiles, Collection<ClassOrInterface> allAvailableClasses){
sfs = sourceFiles; sfs = sourceFiles;
@ -24,7 +26,7 @@ public class TYPE {
} }
public Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { public BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() {
return methCallargTypesRetType; return methCallargTypesRetType;
} }

View File

@ -21,6 +21,7 @@ import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.util.BiRelation;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -31,8 +32,8 @@ public class TYPEStmt implements StatementVisitor{
private final TypeInferenceBlockInformation info; private final TypeInferenceBlockInformation info;
private final ConstraintSet constraintsSet = new ConstraintSet(); private final ConstraintSet constraintsSet = new ConstraintSet();
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>(); private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
public TYPEStmt(TypeInferenceBlockInformation info){ public TYPEStmt(TypeInferenceBlockInformation info){
this.info = info; this.info = info;
@ -42,7 +43,7 @@ public class TYPEStmt implements StatementVisitor{
return constraintsSet; return constraintsSet;
} }
public Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() { public BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() {
return methCallargTypesRetType; return methCallargTypesRetType;
} }
@ -64,7 +65,8 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(ArgumentList arglist) { public void visit(ArgumentList arglist) {
for(int i = 0;i<arglist.getArguments().size();i++){ for(int i = 0;i<arglist.getArguments().size();i++){
arglist.getArguments().get(i).accept(this); superType = arglist.getArguments().get(i).getType();
arglist.getArguments().get(i).accept(this);
} }
} }
@ -85,6 +87,7 @@ public class TYPEStmt implements StatementVisitor{
//Constraints des Bodys generieren: //Constraints des Bodys generieren:
TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression)); TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression));
superType = new Void(new NullToken());
lambdaExpression.methodBody.accept(lambdaScope); lambdaExpression.methodBody.accept(lambdaScope);
constraintsSet.addAll(lambdaScope.getConstraints()); constraintsSet.addAll(lambdaScope.getConstraints());
} }
@ -92,6 +95,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(Assign assign) { public void visit(Assign assign) {
assign.lefSide.accept(this); assign.lefSide.accept(this);
superType = assign.lefSide.getType();
assign.rightSide.accept(this); assign.rightSide.accept(this);
constraintsSet.addUndConstraint(new Pair( constraintsSet.addUndConstraint(new Pair(
assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT)); assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT));
@ -100,6 +104,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(Block block) { public void visit(Block block) {
for(Statement stmt : block.getStatements()){ for(Statement stmt : block.getStatements()){
superType = new Void(new NullToken());
stmt.accept(this); stmt.accept(this);
} }
} }
@ -116,6 +121,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(FieldVar fieldVar) { public void visit(FieldVar fieldVar) {
superType = new Void(new NullToken());
fieldVar.receiver.accept(this); fieldVar.receiver.accept(this);
Set<Constraint> oderConstraints = new HashSet<>(); Set<Constraint> oderConstraints = new HashSet<>();
for(FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)){ for(FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)){
@ -140,14 +146,17 @@ public class TYPEStmt implements StatementVisitor{
public void visit(IfStmt ifStmt) { public void visit(IfStmt ifStmt) {
RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken());
//Expression inferieren: //Expression inferieren:
superType = new Void(new NullToken());
ifStmt.expr.accept(this); ifStmt.expr.accept(this);
//Expression muss boolean sein: //Expression muss boolean sein:
constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT));
//Blöcke inferieren: //Blöcke inferieren:
superType = new Void(new NullToken());
ifStmt.then_block.accept(this); ifStmt.then_block.accept(this);
//Beide Blöcke müssen den gleichen Supertyp haben, welcher den Rückgabetyp des If-Stmts darstellt //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)); constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT));
if(ifStmt.else_block != null){ if(ifStmt.else_block != null){
superType = new Void(new NullToken());
ifStmt.else_block.accept(this); ifStmt.else_block.accept(this);
constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT)); 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 //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) //und die Typen der Rückgabewerte immer rechts stehen (vgl. JavaTXCompiler)
public void visit(MethodCall methodCall) { public void visit(MethodCall methodCall) {
Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric> argTypesRetType = new HashMap<>();
argTypesRetType.put(methodCall.getArgumentList() //Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg
.getArguments() //betrachtet werden muessen
.stream() BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric> argTypesRetType = new BiRelation<>();
.map(x -> x.getType()) Set<RefTypeOrTPHOrWildcardOrGeneric> argTphSet = methodCall.getArgumentList()
.collect(Collectors.toCollection(HashSet::new)), .getArguments()
methodCall.getType()); .stream()
methCallargTypesRetType.put(methodCall.name, argTypesRetType); .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); methodCall.receiver.accept(this);
//Overloading: //Overloading:
Set<Constraint<Pair>> methodConstraints = new HashSet<>(); Set<Constraint<Pair>> methodConstraints = new HashSet<>();
@ -226,6 +245,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(ExpressionReceiver receiver) { public void visit(ExpressionReceiver receiver) {
superType = new Void(new NullToken());
receiver.expr.accept(this); 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 //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) //und die Typen der Rückgabewerte immer rechts stehen (vgl. JavaTXCompiler)
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
superType = new Void(new NullToken());
binary.lexpr.accept(this); binary.lexpr.accept(this);
superType = new Void(new NullToken());
binary.rexpr.accept(this); binary.rexpr.accept(this);
if(binary.operation.equals(BinaryExpr.Operator.DIV) || if(binary.operation.equals(BinaryExpr.Operator.DIV) ||
binary.operation.equals(BinaryExpr.Operator.MUL)|| binary.operation.equals(BinaryExpr.Operator.MUL)||
@ -505,6 +527,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(Return returnExpr) { public void visit(Return returnExpr) {
superType = returnExpr.getType();
returnExpr.retexpr.accept(this); returnExpr.retexpr.accept(this);
constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT)); 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) { public void visit(WhileStmt whileStmt) {
RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken());
//Expression inferieren: //Expression inferieren:
superType = new Void(new NullToken());
whileStmt.expr.accept(this); whileStmt.expr.accept(this);
//Expression muss boolean sein: //Expression muss boolean sein:
constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT));
//LoopBlock inferieren: //LoopBlock inferieren:
superType = new Void(new NullToken());
whileStmt.loopBlock.accept(this); whileStmt.loopBlock.accept(this);
} }
@ -571,6 +596,7 @@ public class TYPEStmt implements StatementVisitor{
public void visit(AssignToField assignLeftSide) { public void visit(AssignToField assignLeftSide) {
//Hier ist kein Code nötig. Es werden keine extra Constraints generiert //Hier ist kein Code nötig. Es werden keine extra Constraints generiert
//HIER muss Code rein PL 2018-10-24 //HIER muss Code rein PL 2018-10-24
superType = new Void(new NullToken());
assignLeftSide.field.accept(this); assignLeftSide.field.accept(this);
} }
@ -622,6 +648,7 @@ public class TYPEStmt implements StatementVisitor{
TypeInferenceBlockInformation info, GenericsResolver resolver) { TypeInferenceBlockInformation info, GenericsResolver resolver) {
Set<Pair> ret = new HashSet<>(); Set<Pair> ret = new HashSet<>();
for(int i = 0;i<foMethod.arglist.getArguments().size();i++){ for(int i = 0;i<foMethod.arglist.getArguments().size();i++){
superType = assumption.getArgTypes(resolver).get(i);
foMethod.arglist.getArguments().get(i).accept(this); foMethod.arglist.getArguments().get(i).accept(this);
RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType(); RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType();
RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i); RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i);

View File

@ -44,8 +44,8 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.typeinference.unify.model.WildcardType; import de.dhbwstuttgart.typeinference.unify.model.WildcardType;
import de.dhbwstuttgart.util.Pair;
import de.dhbwstuttgart.typeinference.unify.model.OrderingUnifyPair; import de.dhbwstuttgart.typeinference.unify.model.OrderingUnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.Pair;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;

View File

@ -26,6 +26,7 @@ import de.dhbwstuttgart.typeinference.unify.Match;
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
import de.dhbwstuttgart.util.Pair;
/** /**
* The finite closure for the type unification * The finite closure for the type unification

View File

@ -19,6 +19,7 @@ import com.google.common.collect.Ordering;
import de.dhbwstuttgart.typeinference.unify.Match; import de.dhbwstuttgart.typeinference.unify.Match;
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.util.Pair;

View File

@ -0,0 +1,19 @@
package de.dhbwstuttgart.util;
import java.util.ArrayList;
public class BiRelation<X,Y> extends ArrayList<Pair<X,Y>>{
public void put(X x, Y y) {
this.add(new Pair<>(x,y));
}
public void put(Pair<X,Y> p) {
this.add(p);
}
public void putAll(BiRelation<X,Y> br) {
this.addAll(br);
}
}

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typeinference.unify.model; package de.dhbwstuttgart.util;
import java.util.Optional; import java.util.Optional;
@ -20,6 +20,6 @@ public class Pair<T, T1> {
} }
public String toString() { public String toString() {
return "(" + key.toString() + "," + "," + value.toString() + ")\n"; return "(" + key.toString() + "," + value.toString() + ")\n";
} }
} }