forked from JavaTX/JavaCompilerCore
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:
parent
e8eaa3ac6e
commit
0944cf59fb
@ -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<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 {
|
||||
|
@ -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<SourceFile> sfs;
|
||||
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){
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new HashMap<>();
|
||||
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
|
||||
private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
|
||||
|
||||
public TYPEStmt(TypeInferenceBlockInformation info){
|
||||
this.info = info;
|
||||
@ -42,7 +43,7 @@ public class TYPEStmt implements StatementVisitor{
|
||||
return constraintsSet;
|
||||
}
|
||||
|
||||
public Map<String, Map<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() {
|
||||
public BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> getMethCallargTypesRetType() {
|
||||
return methCallargTypesRetType;
|
||||
}
|
||||
|
||||
@ -64,7 +65,8 @@ public class TYPEStmt implements StatementVisitor{
|
||||
@Override
|
||||
public void visit(ArgumentList arglist) {
|
||||
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:
|
||||
TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression));
|
||||
superType = new Void(new NullToken());
|
||||
lambdaExpression.methodBody.accept(lambdaScope);
|
||||
constraintsSet.addAll(lambdaScope.getConstraints());
|
||||
}
|
||||
@ -92,6 +95,7 @@ public class TYPEStmt implements StatementVisitor{
|
||||
@Override
|
||||
public void visit(Assign assign) {
|
||||
assign.lefSide.accept(this);
|
||||
superType = assign.lefSide.getType();
|
||||
assign.rightSide.accept(this);
|
||||
constraintsSet.addUndConstraint(new Pair(
|
||||
assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT));
|
||||
@ -100,6 +104,7 @@ public class TYPEStmt implements StatementVisitor{
|
||||
@Override
|
||||
public void visit(Block block) {
|
||||
for(Statement stmt : block.getStatements()){
|
||||
superType = new Void(new NullToken());
|
||||
stmt.accept(this);
|
||||
}
|
||||
}
|
||||
@ -116,6 +121,7 @@ public class TYPEStmt implements StatementVisitor{
|
||||
|
||||
@Override
|
||||
public void visit(FieldVar fieldVar) {
|
||||
superType = new Void(new NullToken());
|
||||
fieldVar.receiver.accept(this);
|
||||
Set<Constraint> 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<Set<RefTypeOrTPHOrWildcardOrGeneric>, 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<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric> argTypesRetType = new BiRelation<>();
|
||||
Set<RefTypeOrTPHOrWildcardOrGeneric> 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<Constraint<Pair>> 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<Pair> ret = new HashSet<>();
|
||||
for(int i = 0;i<foMethod.arglist.getArguments().size();i++){
|
||||
superType = assumption.getArgTypes(resolver).get(i);
|
||||
foMethod.arglist.getArguments().get(i).accept(this);
|
||||
RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType();
|
||||
RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i);
|
||||
|
@ -44,8 +44,8 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||
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.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
@ -26,6 +26,7 @@ import de.dhbwstuttgart.typeinference.unify.Match;
|
||||
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
||||
import de.dhbwstuttgart.util.Pair;
|
||||
|
||||
/**
|
||||
* The finite closure for the type unification
|
||||
|
@ -19,6 +19,7 @@ import com.google.common.collect.Ordering;
|
||||
import de.dhbwstuttgart.typeinference.unify.Match;
|
||||
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||
import de.dhbwstuttgart.util.Pair;
|
||||
|
||||
|
||||
|
||||
|
19
src/main/java/de/dhbwstuttgart/util/BiRelation.java
Normal file
19
src/main/java/de/dhbwstuttgart/util/BiRelation.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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<T, T1> {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "(" + key.toString() + "," + "," + value.toString() + ")\n";
|
||||
return "(" + key.toString() + "," + value.toString() + ")\n";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user