diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 3e477142..e4b89c66 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -327,74 +327,6 @@ public class JavaTXCompiler { logFile.write(ASTTypePrinter.print(f)); } logFile.flush(); - - Set methodParaTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().map(y -> y.getParameterList().getFormalparalist().stream().filter(z -> z.getType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }, (a, b) -> { - a.addAll(b); - return a; - })).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }); - - Set constructorParaTypeVarNames = allClasses.stream().map(x -> x.getConstructors().stream().map(y -> y.getParameterList().getFormalparalist().stream().filter(z -> z.getType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }, (a, b) -> { - a.addAll(b); - return a; - })).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }); - - Set paraTypeVarNames = methodParaTypeVarNames; - paraTypeVarNames.addAll(constructorParaTypeVarNames); - - Set returnTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a, b) -> { - a.addAll(b); - return a; - }).get(); - - Set fieldTypeVarNames = allClasses.stream().map(x -> x.getFieldDecl().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a, b) -> { - a.addAll(b); - return a; - }).get(); - - returnTypeVarNames.addAll(fieldTypeVarNames); - - unifyCons = unifyCons.map(x -> { - // Hier muss ueberlegt werden, ob - // 1. alle Argument- und Retuntyp-Variablen in allen UnifyPairs - // mit disableWildcardtable() werden. - // 2. alle Typvariablen mit Argument- oder Retuntyp-Variablen - // in Beziehung auch auf disableWildcardtable() gesetzt werden muessen - // PL 2018-04-23 - if ((x.getLhsType() instanceof PlaceholderType)) { - if (paraTypeVarNames.contains(x.getLhsType().getName())) { - ((PlaceholderType) x.getLhsType()).setVariance((byte) 1); - ((PlaceholderType) x.getLhsType()).disableWildcardtable(); - } - if (returnTypeVarNames.contains(x.getLhsType().getName())) { - ((PlaceholderType) x.getLhsType()).setVariance((byte) -1); - ((PlaceholderType) x.getLhsType()).disableWildcardtable(); - } - } - if ((x.getRhsType() instanceof PlaceholderType)) { - if (paraTypeVarNames.contains(x.getRhsType().getName())) { - ((PlaceholderType) x.getRhsType()).setVariance((byte) 1); - ((PlaceholderType) x.getRhsType()).disableWildcardtable(); - } - if (returnTypeVarNames.contains(x.getRhsType().getName())) { - ((PlaceholderType) x.getRhsType()).setVariance((byte) -1); - ((PlaceholderType) x.getRhsType()).disableWildcardtable(); - } - } - return x;// HIER DIE JEWEILS RECHT BZW. LINKE SEITE AUF GLEICHE VARIANZ SETZEN WIE DIE - // JEWEILS ANDERE SEITE - }); Set varianceTPHold; Set varianceTPH = new HashSet<>(); varianceTPH = varianceInheritanceConstraintSet(unifyCons); @@ -461,85 +393,6 @@ public class JavaTXCompiler { logFile.write(ASTTypePrinter.print(sf)); System.out.println(ASTTypePrinter.print(sf)); logFile.flush(); - - Set methodParaTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().map(y -> y.getParameterList().getFormalparalist().stream().filter(z -> z.getType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }, (a, b) -> { - a.addAll(b); - return a; - })).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }); - - Set constructorParaTypeVarNames = allClasses.stream().map(x -> x.getConstructors().stream().map(y -> y.getParameterList().getFormalparalist().stream().filter(z -> z.getType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }, (a, b) -> { - a.addAll(b); - return a; - })).reduce(new HashSet(), (a, b) -> { - a.addAll(b); - return a; - }); - - Set paraTypeVarNames = methodParaTypeVarNames; - paraTypeVarNames.addAll(constructorParaTypeVarNames); - - Set returnTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a, b) -> { - a.addAll(b); - return a; - }).get(); - - Set fieldTypeVarNames = allClasses.stream().map(x -> x.getFieldDecl().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder).map(z -> ((TypePlaceholder) z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a, b) -> { - a.addAll(b); - return a; - }).get(); - - returnTypeVarNames.addAll(fieldTypeVarNames); - - unifyCons = unifyCons.map(x -> { - // Hier muss ueberlegt werden, ob - // 1. alle Argument- und Retuntyp-Variablen in allen UnifyPairs - // mit disableWildcardtable() werden. - // 2. alle Typvariablen mit Argument- oder Retuntyp-Variablen - // in Beziehung auch auf disableWildcardtable() gesetzt werden muessen - // PL 2018-04-23 - if ((x.getLhsType() instanceof PlaceholderType)) { - if (paraTypeVarNames.contains(x.getLhsType().getName())) { - ((PlaceholderType) x.getLhsType()).setVariance((byte) 1); - ((PlaceholderType) x.getLhsType()).disableWildcardtable(); - } - if (returnTypeVarNames.contains(x.getLhsType().getName())) { - ((PlaceholderType) x.getLhsType()).setVariance((byte) -1); - ((PlaceholderType) x.getLhsType()).disableWildcardtable(); - } - } - if ((x.getRhsType() instanceof PlaceholderType)) { - if (paraTypeVarNames.contains(x.getRhsType().getName())) { - ((PlaceholderType) x.getRhsType()).setVariance((byte) 1); - ((PlaceholderType) x.getRhsType()).disableWildcardtable(); - } - if (returnTypeVarNames.contains(x.getRhsType().getName())) { - ((PlaceholderType) x.getRhsType()).setVariance((byte) -1); - ((PlaceholderType) x.getRhsType()).disableWildcardtable(); - } - } - return x;// HIER DIE JEWEILS RECHT BZW. LINKE SEITE AUF GLEICHE VARIANZ SETZEN WIE DIE - // JEWEILS ANDERE SEITE - }); - - // PL 2020-02-05 alle Oder-Constraints Receiver und Parameter werden auf - // variance 1 gesetzt - // 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); } else if ((x.getRhsType() instanceof PlaceholderType) && x.getPairOp().compareTo(PairOperator.EQUALSDOT) == 0) { ((PlaceholderType) x.getRhsType()).setVariance((byte)-1); } }))); - */ - System.out.println("Unify nach Oder-Constraints-Anpassung:" + unifyCons.toString()); Set varianceTPHold; Set varianceTPH = new HashSet<>(); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 0b5a322e..ce637f08 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -147,7 +147,7 @@ public class StatementGenerator { if (fp.typeType() != null) { type = TypeGenerator.convert(fp.typeType(), reg, generics); } else { - type = TypePlaceholder.fresh(fp.getStart()); + type = TypePlaceholder.fresh(fp.getStart(), 1, false); } ret.add(new FormalParameter(paramName, type, fp.getStart())); localVars.put(paramName, type); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index fab148e0..475adc9e 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -532,7 +532,7 @@ public class SyntaxTreeGenerator { RefTypeOrTPHOrWildcardOrGeneric retType; if (Objects.isNull(header.refType())) { - retType = TypePlaceholder.fresh(header.getStart()); + retType = TypePlaceholder.fresh(header.getStart(), -1, false); } else { if (header.refType() instanceof RefType2Context reftype) { retType = TypeGenerator.convert(reftype.typeType(), reg, generics); @@ -594,7 +594,7 @@ public class SyntaxTreeGenerator { } else { // PL 2019-12-06: variableDeclaratorList() eingefuegt, um als Token nicht die // Modifier zu bekommen - fieldType = TypePlaceholder.fresh(fieldDeclContext.variableDeclarators().getStart()); + fieldType = TypePlaceholder.fresh(fieldDeclContext.variableDeclarators().getStart(), -1, false); } for (Java17Parser.VariableDeclaratorContext varDecl : fieldDeclContext.variableDeclarators().variableDeclarator()) { String fieldName = varDecl.variableDeclaratorId().getText(); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index e73e9ba7..8955773f 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -129,9 +129,10 @@ public class UnifyTypeFactory { if (tph.getName().equals("AFR")) { System.out.println("XXX"+innerType); } - PlaceholderType ntph = new PlaceholderType(tph.getName()); + PlaceholderType ntph = new PlaceholderType(tph.getName(), tph.getVariance()); ntph.setVariance(tph.getVariance()); ntph.setOrCons(tph.getOrCons()); + ntph.setWildcardtable(tph.getWildcardtable()); 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 8452b2c8..88d90272 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -21,10 +21,18 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric private final String name; /** - * wird im Generate Generics Teil nach der Rueckumwandlung nach dem Unify genutzt - */ + * variance shows the variance of the pair + * 1: contravariant + * -1 covariant + * 0 invariant + */ private int variance = 0; + /** + * isWildcardable gibt an, ob ein Wildcardtyp dem PlaceholderType zugeordnet werden darf + */ + private boolean wildcardable = true; + /* * Fuer Oder-Constraints: * orCons = 1: Receiver @@ -39,10 +47,12 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric * Factory-Methode fresh() erzeugt. *
Author: J�rg B�uerle */ - private TypePlaceholder(String name, Token offset) + private TypePlaceholder(String name, Token offset, int variance, boolean wildcardable) { super(offset); this.name = name; + this.variance = variance; + this.wildcardable = wildcardable; } @@ -53,11 +63,15 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric * @return */ public static TypePlaceholder fresh(Token position){ - return new TypePlaceholder(NameGenerator.makeNewName(), position); + return new TypePlaceholder(NameGenerator.makeNewName(), position, 0, true); + } + + public static TypePlaceholder fresh(Token position, int variance, boolean wildcardable){ + return new TypePlaceholder(NameGenerator.makeNewName(), position, variance, wildcardable); } public static RefTypeOrTPHOrWildcardOrGeneric of(String name) { - return new TypePlaceholder(name, new NullToken()); + return new TypePlaceholder(name, new NullToken(),0, true); } @@ -120,4 +134,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric public byte getOrCons() { return orCons; } + + + public Boolean getWildcardtable() { + return wildcardable; + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 361cf4a0..c8bc2963 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -822,9 +822,9 @@ public class TYPEStmt implements StatementVisitor { public void visit(Switch switchStmt) { switchStack.push(switchStmt); - //if(switchStmt.getSwitch().getType() instanceof TypePlaceholder){ - // ((TypePlaceholder) switchStmt.getSwitch().getType()).setVariance(1); - //} + if(switchStmt.getSwitch().getType() instanceof TypePlaceholder){ + ((TypePlaceholder) switchStmt.getSwitch().getType()).setVariance(-1); + } RefTypeOrTPHOrWildcardOrGeneric caseExpressionType = null; for (var child : switchStmt.getBlocks()) { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 07d85727..f275c6aa 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -53,8 +53,8 @@ public final class PlaceholderType extends UnifyType{ /** * variance shows the variance of the pair - * -1: contravariant - * 1 covariant + * 1: contravariant + * -1 covariant * 0 invariant * PL 2018-03-21 */ @@ -77,6 +77,14 @@ public final class PlaceholderType extends UnifyType{ IsGenerated = false; // This type is user generated } + + public PlaceholderType(String name, int variance) { + super(name, new TypeParams()); + EXISTING_PLACEHOLDERS.add(name); // Add to list of existing placeholder names + IsGenerated = false; // This type is user generated + this.variance = variance; + } + /** * Creates a new placeholdertype * @param isGenerated true if this placeholder is auto-generated, false if it is user-generated. diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index c54899c0..576b5fbb 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -721,7 +721,7 @@ public class TestComplete { var list2 = listCtor.newInstance("D", listCtor.newInstance("E", elemCtor.newInstance("F"))); var instance = clazz.getDeclaredConstructor().newInstance(); - var appendMethod = clazz.getDeclaredMethod("append", Object.class, listClass); + var appendMethod = clazz.getDeclaredMethod("append", linkedElemClass, listClass); var res = appendMethod.invoke(instance, list2, list1); var expected = listCtor.newInstance("D", listCtor.newInstance("E", listCtor.newInstance("F", listCtor.newInstance("A", listCtor.newInstance("B", elemCtor.newInstance("C"))))));