From d1fd65fad7374e7cf2f204f1ff287af62b21f83e Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Wed, 18 Jan 2023 18:11:45 +0100 Subject: [PATCH] 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 --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 3 +- .../target/generate/ASTToTargetAST.java | 318 ++++++++++++++---- .../typeinference/typeAlgo/TYPE.java | 2 + .../typeinference/typeAlgo/TYPEStmt.java | 2 + src/test/java/targetast/TestComplete.java | 7 + 5 files changed, 266 insertions(+), 66 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 629524a3..67e553e6 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -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, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); - + //zum Loeschen Ende obsolet PL 2023-01-18 public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { this(Arrays.asList(sourceFile), null); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 3b34c459..a6eca776 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -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.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 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> all = new HashSet<>(generics); + + // Reflexive and Transitive closure + HashSet> 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 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> 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> 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 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 sfs; private final TypeInferenceInformation typeInferenceInformation; + //zum Loeschen Anfang obsolet PL 2023-01-18 private BiRelation, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); + //zum Loeschen Ende obsolet PL 2023-01-18 public TYPE(Collection sourceFiles, Collection allAvailableClasses){ sfs = sourceFiles; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 447ec953..5191b8f7 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -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, RefTypeOrTPHOrWildcardOrGeneric>> methCallargTypesRetType = new BiRelation<>(); + //zum Loeschen Ende obsolet PL 2023-01-18 public TYPEStmt(TypeInferenceBlockInformation info){ this.info = info; diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index 53747024..a12441c2 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -339,6 +339,13 @@ public class TestComplete { var classFiles = generateClassFiles("SubMatrix.jav", new ByteArrayClassLoader()); 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 {