From a5662cdd9a07fd022f64c0d74f3b09f5f40c1cb8 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Thu, 6 Feb 2020 11:39:27 +0100 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java Nachtraegliches Einfuegen von Variance in OderConstraints auskommentiert. modified: src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java Variance im konvertieren eingefuegt modified: src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java Variace eingefuegt modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java In OderConstraints Variance bei der Erstellung eingefuegt --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 2 ++ .../syntaxtree/factory/UnifyTypeFactory.java | 1 + .../syntaxtree/type/TypePlaceholder.java | 11 ++++++++++- .../typeinference/typeAlgo/TYPEStmt.java | 19 +++++++++++++++---- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index aca8b17f..989c100d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -603,6 +603,7 @@ public class JavaTXCompiler { //Es wird davon ausgegangen, dass in OderConstraints in Bedingungen für Parameter die Typen der Argumente links stehen //und die Typen der Rückgabewerte immer rechts stehen + /* unifyCons.getOderConstraints().forEach(z -> z.forEach(y -> y.forEach(x -> { if ((x.getLhsType() instanceof PlaceholderType) && x.getPairOp().compareTo(PairOperator.SMALLERDOT) == 0) { ((PlaceholderType) x.getLhsType()).setVariance((byte)1); @@ -611,6 +612,7 @@ public class JavaTXCompiler { ((PlaceholderType) x.getRhsType()).setVariance((byte)-1); } }))); + */ System.out.println("Unify nach Oder-Constraints-Anpassung:" + unifyCons.toString()); Set varianceTPHold; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 33d137eb..84d0f1e5 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -122,6 +122,7 @@ public class UnifyTypeFactory { System.out.println("XXX"+innerType); } PlaceholderType ntph = new PlaceholderType(tph.getName()); + ntph.setVariance(tph.getVariance()); int in = PLACEHOLDERS.indexOf(ntph); if (in == -1) { PLACEHOLDERS.add(ntph); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 6f0fa3ff..533ea9a0 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -18,7 +18,8 @@ import org.antlr.v4.runtime.Token; public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric { private final String name; - + + int variance = 0; /** @@ -69,6 +70,14 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric return name; } + public void setVariance(int variance) { + this.variance= variance; + } + + public int getVariance() { + return this.variance; + } + @Override public void accept(ASTVisitor visitor) { visitor.visit(this); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 2639c6a4..aca5912e 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -17,6 +17,7 @@ import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.*; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import java.util.*; import java.util.stream.Collectors; @@ -164,7 +165,7 @@ public class TYPEStmt implements StatementVisitor{ public void visit(MethodCall methodCall) { methodCall.receiver.accept(this); //Overloading: - Set methodConstraints = new HashSet<>(); + Set> methodConstraints = new HashSet<>(); for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){ GenericsResolver resolver = getResolverInstance(); methodConstraints.add(generateConstraint(methodCall, m, info, resolver)); @@ -559,7 +560,7 @@ public class TYPEStmt implements StatementVisitor{ protected Constraint generateConstraint(MethodCall forMethod, MethodAssumption assumption, TypeInferenceBlockInformation info, GenericsResolver resolver){ - Constraint methodConstraint = new Constraint(); + Constraint methodConstraint = new Constraint<>(); ClassOrInterface receiverCl = assumption.getReceiver(); /* List params = new ArrayList<>(); @@ -572,10 +573,17 @@ public class TYPEStmt implements StatementVisitor{ */ RefTypeOrTPHOrWildcardOrGeneric retType = assumption.getReceiverType(resolver); - methodConstraint.add(new Pair(forMethod.receiver.getType(), retType, + RefTypeOrTPHOrWildcardOrGeneric zwSp; + methodConstraint.add(new Pair(zwSp = forMethod.receiver.getType(), retType, PairOperator.SMALLERDOT)); - methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), + if (zwSp instanceof TypePlaceholder) { + ((TypePlaceholder) zwSp).setVariance(1); + } + methodConstraint.add(new Pair(assumption.getReturnType(resolver), zwSp = forMethod.getType(), PairOperator.EQUALSDOT)); + if (zwSp instanceof TypePlaceholder) { + ((TypePlaceholder) zwSp).setVariance(-1); + } methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info, resolver)); return methodConstraint; } @@ -588,6 +596,9 @@ public class TYPEStmt implements StatementVisitor{ RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType(); RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i); ret.add(new Pair(argType, assType, PairOperator.SMALLERDOT)); + if (argType instanceof TypePlaceholder) { + ((TypePlaceholder) argType).setVariance(1); + } } return ret; }