forked from JavaTX/JavaCompilerCore
modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java
Loeschungen angezeigt modified: src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java Ansatz fuer Veraenderung des Algorithmus bei Generated Generics eingefuegt. modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java Loeschungen angezeigt modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java Loeschungen angezeigt modified: src/test/java/targetast/TestComplete.java tphTest eingfuegt
This commit is contained in:
parent
752faaca86
commit
d1fd65fad7
@ -72,8 +72,9 @@ public class JavaTXCompiler {
|
||||
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
|
||||
private final DirectoryClassLoader classLoader;
|
||||
|
||||
//zum Loeschen Anfang obsolet PL 2023-01-18
|
||||
private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
|
||||
|
||||
//zum Loeschen Ende obsolet PL 2023-01-18
|
||||
|
||||
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
|
||||
this(Arrays.asList(sourceFile), null);
|
||||
|
@ -4,15 +4,18 @@ import de.dhbwstuttgart.bytecode.FunNGenerator;
|
||||
import de.dhbwstuttgart.environment.ByteArrayClassLoader;
|
||||
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||||
import de.dhbwstuttgart.syntaxtree.*;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
||||
import de.dhbwstuttgart.target.tree.*;
|
||||
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
|
||||
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
|
||||
import de.dhbwstuttgart.target.tree.type.*;
|
||||
import de.dhbwstuttgart.typeinference.result.*;
|
||||
import de.dhbwstuttgart.util.BiRelation;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@ -163,77 +166,262 @@ public class ASTToTargetAST {
|
||||
|
||||
var visitedMethods = new HashSet<Method>();
|
||||
method.block.accept(new TracingStatementVisitor() {
|
||||
|
||||
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
//Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg
|
||||
//betrachtet werden muessen
|
||||
//Definition 7.2 (Family of generated generics). T1 <. R1 <.^∗ R2 <. T2
|
||||
Set<RefTypeOrTPHOrWildcardOrGeneric> T1s =
|
||||
methodCall.getArgumentList()
|
||||
.getArguments()
|
||||
.stream()
|
||||
.map(x -> x.getType())
|
||||
.collect(Collectors.toCollection(HashSet::new))
|
||||
.stream().filter(x -> x instanceof TypePlaceholder)
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
RefTypeOrTPHOrWildcardOrGeneric T2 = superType;
|
||||
System.out.println("T1s: " + T1s + "\nT2: " + T2);
|
||||
//Ende
|
||||
|
||||
superType = methodCall.receiverType;
|
||||
methodCall.receiver.accept(this);
|
||||
for(int i = 0;i<methodCall.arglist.getArguments().size();i++){
|
||||
superType = methodCall.argTypes.get(i);
|
||||
methodCall.arglist.getArguments().get(i).accept(this);
|
||||
}
|
||||
|
||||
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver) {
|
||||
if (expressionReceiver.expr instanceof This) {
|
||||
var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList());
|
||||
if (optMethod.isEmpty()) return;
|
||||
var method = optMethod.get();
|
||||
if (visitedMethods.contains(method)) return;
|
||||
visitedMethods.add(method);
|
||||
|
||||
var generics = generics(owner, method);
|
||||
Set<ResultPair<?, ?>> all = new HashSet<>(generics);
|
||||
|
||||
// Reflexive and Transitive closure
|
||||
HashSet<ResultPair<?, ?>> toAdd = new HashSet<>();
|
||||
int sizeBefore;
|
||||
do {
|
||||
sizeBefore = all.size();
|
||||
toAdd.clear();
|
||||
for (var g1 : all) {
|
||||
for (var g2 : all) {
|
||||
if (g1 instanceof PairTPHsmallerTPH pair) {
|
||||
if (g2.getLeft().equals(pair.getLeft()) && generics.stream().anyMatch(generic -> generic.getLeft().equals(pair.getRight())))
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) g1.getLeft(), (TypePlaceholder) g2.getRight()));
|
||||
}
|
||||
}
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
} while (sizeBefore < all.size());
|
||||
for (var generic : all) {
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) generic.getLeft(), (TypePlaceholder) generic.getLeft()));
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
HashSet<PairTPHsmallerTPH> newPairs = new HashSet<>();
|
||||
|
||||
System.out.println("Result: " + result);
|
||||
|
||||
// Loop from hell
|
||||
outer:
|
||||
for (var tph : typeVariables) {
|
||||
for (var generic : all) {
|
||||
if (!(generic.getRight() instanceof TypePlaceholder type))
|
||||
continue;
|
||||
|
||||
for (var pair : simplifiedConstraints) {
|
||||
if (!(pair.left.equals(tph) && pair.right.equals(generic.getLeft())))
|
||||
continue;
|
||||
|
||||
for (var tph2 : typeVariables) {
|
||||
for (var pair2 : simplifiedConstraints) {
|
||||
if (!(pair2.right.equals(tph2) && pair2.left.equals(type)))
|
||||
continue;
|
||||
if (tph.equals(tph2)) continue;
|
||||
|
||||
var newPair = new PairTPHsmallerTPH(tph, tph2);
|
||||
newPairs.add(newPair);
|
||||
|
||||
if (!containsRelation(result, newPair))
|
||||
addToPairs(result, newPair);
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
simplifiedConstraints.addAll(newPairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
super.visit(methodCall);
|
||||
public void visit(LambdaExpression lambdaExpression) {
|
||||
superType = new Void(new NullToken());
|
||||
lambdaExpression.methodBody.accept(this);
|
||||
}
|
||||
|
||||
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver) {
|
||||
if (expressionReceiver.expr instanceof This) {
|
||||
var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList());
|
||||
if (optMethod.isEmpty()) return;
|
||||
var method = optMethod.get();
|
||||
if (visitedMethods.contains(method)) return;
|
||||
visitedMethods.add(method);
|
||||
@Override
|
||||
public void visit(Assign assign) {
|
||||
superType = assign.lefSide.getType();
|
||||
assign.rightSide.accept(this);
|
||||
}
|
||||
|
||||
var generics = generics(owner, method);
|
||||
Set<ResultPair<?, ?>> all = new HashSet<>(generics);
|
||||
@Override
|
||||
public void visit(BinaryExpr binary) {
|
||||
superType = new Void(new NullToken());
|
||||
binary.lexpr.accept(this);
|
||||
superType = new Void(new NullToken());
|
||||
binary.rexpr.accept(this);
|
||||
}
|
||||
|
||||
// Reflexive and Transitive closure
|
||||
HashSet<ResultPair<?, ?>> toAdd = new HashSet<>();
|
||||
int sizeBefore;
|
||||
do {
|
||||
sizeBefore = all.size();
|
||||
toAdd.clear();
|
||||
for (var g1 : all) {
|
||||
for (var g2 : all) {
|
||||
if (g1 instanceof PairTPHsmallerTPH pair) {
|
||||
if (g2.getLeft().equals(pair.getLeft()) && generics.stream().anyMatch(generic -> generic.getLeft().equals(pair.getRight())))
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) g1.getLeft(), (TypePlaceholder) g2.getRight()));
|
||||
}
|
||||
}
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
} while (sizeBefore < all.size());
|
||||
for (var generic : all) {
|
||||
toAdd.add(new PairTPHsmallerTPH((TypePlaceholder) generic.getLeft(), (TypePlaceholder) generic.getLeft()));
|
||||
}
|
||||
all.addAll(toAdd);
|
||||
HashSet<PairTPHsmallerTPH> newPairs = new HashSet<>();
|
||||
|
||||
System.out.println("Result: " + result);
|
||||
|
||||
// Loop from hell
|
||||
outer:
|
||||
for (var tph : typeVariables) {
|
||||
for (var generic : all) {
|
||||
if (!(generic.getRight() instanceof TypePlaceholder type))
|
||||
continue;
|
||||
|
||||
for (var pair : simplifiedConstraints) {
|
||||
if (!(pair.left.equals(tph) && pair.right.equals(generic.getLeft())))
|
||||
continue;
|
||||
|
||||
for (var tph2 : typeVariables) {
|
||||
for (var pair2 : simplifiedConstraints) {
|
||||
if (!(pair2.right.equals(tph2) && pair2.left.equals(type)))
|
||||
continue;
|
||||
if (tph.equals(tph2)) continue;
|
||||
|
||||
var newPair = new PairTPHsmallerTPH(tph, tph2);
|
||||
newPairs.add(newPair);
|
||||
|
||||
if (!containsRelation(result, newPair))
|
||||
addToPairs(result, newPair);
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
simplifiedConstraints.addAll(newPairs);
|
||||
}
|
||||
@Override
|
||||
public void visit(Block block) {
|
||||
for (var expr : block.statements) {
|
||||
superType = new Void(new NullToken());
|
||||
expr.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(CastExpr castExpr) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(EmptyStmt emptyStmt) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(FieldVar fieldVar) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ForStmt forStmt) {
|
||||
forStmt.body_Loop_block.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IfStmt ifStmt) {
|
||||
superType = new Void(new NullToken());
|
||||
ifStmt.expr.accept(this);
|
||||
superType = new Void(new NullToken());
|
||||
ifStmt.then_block.accept(this);
|
||||
superType = new Void(new NullToken());
|
||||
ifStmt.else_block.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(InstanceOf instanceOf) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LocalVar localVar) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LocalVarDecl localVarDecl) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NewClass newClass) {
|
||||
this.visit((MethodCall) newClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NewArray newArray) {
|
||||
newArray.expr.forEach(expr -> expr.accept(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Return aReturn) {
|
||||
superType = aReturn.getType();
|
||||
aReturn.retexpr.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnVoid aReturn) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(StaticClassName staticClassName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Super aSuper) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(This aThis) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(WhileStmt whileStmt) {
|
||||
superType = new Void(new NullToken());
|
||||
whileStmt.expr.accept(this);
|
||||
superType = new Void(new NullToken());
|
||||
whileStmt.loopBlock.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DoStmt whileStmt) {
|
||||
whileStmt.loopBlock.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(AssignToField assignLeftSide) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(AssignToLocal assignLeftSide) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SuperCall superCall) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ExpressionReceiver expressionReceiver) {
|
||||
expressionReceiver.expr.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(UnaryExpr unaryExpr) {
|
||||
unaryExpr.expr.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Literal literal) {
|
||||
|
||||
}
|
||||
@Override
|
||||
public void visit(ArgumentList arglist) {
|
||||
for(int i = 0;i<arglist.getArguments().size();i++){
|
||||
superType = arglist.getArguments().get(i).getType();
|
||||
arglist.getArguments().get(i).accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
// Type variables with bounds that are also type variables of fields
|
||||
|
@ -18,7 +18,9 @@ public class TYPE {
|
||||
private final Collection<SourceFile> sfs;
|
||||
private final TypeInferenceInformation typeInferenceInformation;
|
||||
|
||||
//zum Loeschen Anfang obsolet PL 2023-01-18
|
||||
private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
|
||||
//zum Loeschen Ende obsolet PL 2023-01-18
|
||||
|
||||
public TYPE(Collection<SourceFile> sourceFiles, Collection<ClassOrInterface> allAvailableClasses){
|
||||
sfs = sourceFiles;
|
||||
|
@ -32,8 +32,10 @@ public class TYPEStmt implements StatementVisitor{
|
||||
private final TypeInferenceBlockInformation info;
|
||||
private final ConstraintSet constraintsSet = new ConstraintSet();
|
||||
|
||||
//zum Loeschen Anfang obsolet PL 2023-01-18
|
||||
private RefTypeOrTPHOrWildcardOrGeneric superType = new Void(new NullToken());
|
||||
private BiRelation<String, BiRelation<Set<RefTypeOrTPHOrWildcardOrGeneric>, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>();
|
||||
//zum Loeschen Ende obsolet PL 2023-01-18
|
||||
|
||||
public TYPEStmt(TypeInferenceBlockInformation info){
|
||||
this.info = info;
|
||||
|
@ -340,6 +340,13 @@ public class TestComplete {
|
||||
var instance = classFiles.get("SubMatrix").getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tphTest() throws Exception {
|
||||
var classFiles = generateClassFiles("Tph.jav", new ByteArrayClassLoader());
|
||||
var tph = classFiles.get("Tph");
|
||||
var instance = tph.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tph2Test() throws Exception {
|
||||
var classFiles = generateClassFiles("Tph2.jav", new ByteArrayClassLoader());
|
||||
|
Loading…
Reference in New Issue
Block a user