From 25a5e79509e7832c4e65b15dd44bba0c93e39eeb Mon Sep 17 00:00:00 2001 From: Aldaron7 Date: Wed, 30 May 2018 15:36:28 +0200 Subject: [PATCH] update solve (not fixed). --- src/de/dhbwstuttgart/strucTypes/Construct.java | 2 +- src/de/dhbwstuttgart/strucTypes/TYPEExpr.java | 5 +++++ .../strucTypes/constraint/FieldConstraint.java | 16 ++++++++++------ .../strucTypes/constraint/MethodConstraint.java | 16 ++++++++++------ .../constraint/SubTypeConstraint.java | 17 +++++++++++------ .../syntaxtree/type/GenericRefType.java | 5 +++-- test/strucType/TestPaperExample.java | 12 +++++++----- test/strucType/constructed/A.java | 2 +- test/strucType/typedtestclasses/MyInteger.java | 3 ++- 9 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/de/dhbwstuttgart/strucTypes/Construct.java b/src/de/dhbwstuttgart/strucTypes/Construct.java index bbc8651b..f4fce6b5 100644 --- a/src/de/dhbwstuttgart/strucTypes/Construct.java +++ b/src/de/dhbwstuttgart/strucTypes/Construct.java @@ -56,6 +56,7 @@ public class Construct extends DefaultASTVisitor { } public Set getSubTypeConstraints() { + this.subTypeConstraints.forEach(sc -> sc.inferTypes(this.inferredTypes)); return subTypeConstraints; } @@ -130,7 +131,6 @@ public class Construct extends DefaultASTVisitor { TypePlaceholder x = TypePlaceholder.fresh(offset); this.subTypeConstraints.add(new SubTypeConstraint(x, inh_tyterm)); this.inferredTypes.put(i, x); - this.subTypeConstraints.forEach(sc -> sc.inferTypes(this.inferredTypes)); final int modifiers = Modifier.PUBLIC; final RefType superClass = this.createSuperClass(); diff --git a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java index aacb73a2..f16f1f82 100644 --- a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java +++ b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java @@ -19,6 +19,7 @@ import de.dhbwstuttgart.syntaxtree.statement.CastExpr; import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.MethodCall; import de.dhbwstuttgart.syntaxtree.statement.NewClass; @@ -225,5 +226,9 @@ public class TYPEExpr extends DefaultASTVisitor { public void visit(GenericRefType genericRefType){ } + + @Override + public void visit(Literal literal) { + } } diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java index c9e3c17e..a816efef 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.strucTypes.constraint; import de.dhbwstuttgart.strucTypes.InferredTypes; +import de.dhbwstuttgart.strucTypes.visitor.InferTypes; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; /** @@ -35,12 +36,15 @@ public class FieldConstraint { } public void inferTypes(InferredTypes inferredTypes) { - if (inferredTypes.containsKey(classType)) { - this.classType = inferredTypes.get(classType); - } - if (inferredTypes.containsKey(fieldType)) { - this.fieldType = inferredTypes.get(fieldType); - } +// if (inferredTypes.containsKey(classType)) { +// this.classType = inferredTypes.get(classType); +// } +// if (inferredTypes.containsKey(fieldType)) { +// this.fieldType = inferredTypes.get(fieldType); +// } + InferTypes inferTypes = new InferTypes(inferredTypes); + classType = classType.accept(inferTypes); + fieldType = fieldType.accept(inferTypes); } @Override diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java index b6ed99a1..772bbe9a 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java @@ -6,6 +6,7 @@ import java.util.List; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.strucTypes.InferredTypes; +import de.dhbwstuttgart.strucTypes.visitor.InferTypes; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -63,12 +64,15 @@ public class MethodConstraint { } public void inferTypes(InferredTypes inferredTypes) { - if (inferredTypes.containsKey(classType)) { - this.classType = inferredTypes.get(classType); - } - if (inferredTypes.containsKey(returnType)) { - this.returnType = inferredTypes.get(returnType); - } +// if (inferredTypes.containsKey(classType)) { +// this.classType = inferredTypes.get(classType); +// } +// if (inferredTypes.containsKey(returnType)) { +// this.returnType = inferredTypes.get(returnType); +// } + InferTypes inferTypes = new InferTypes(inferredTypes); + classType = classType.accept(inferTypes); + returnType = returnType.accept(inferTypes); arguments.forEach(c -> c.inferTypes(inferredTypes)); } diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java index 095003ec..e14cce7c 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes.constraint; import de.dhbwstuttgart.strucTypes.InferredTypes; import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; +import de.dhbwstuttgart.strucTypes.visitor.InferTypes; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -33,13 +34,17 @@ public class SubTypeConstraint { } public void inferTypes(InferredTypes inferredTypes) { - if (inferredTypes.containsKey(subtype)) { - this.subtype = inferredTypes.get(subtype); - } - if (inferredTypes.containsKey(supertype)) { - this.supertype = inferredTypes.get(supertype); - } +// if (inferredTypes.containsKey(subtype)) { +// this.subtype = inferredTypes.get(subtype); +// } +// if (inferredTypes.containsKey(supertype)) { +// this.supertype = inferredTypes.get(supertype); +// } + InferTypes inferTypes = new InferTypes(inferredTypes); + subtype = subtype.accept(inferTypes); + supertype = supertype.accept(inferTypes); } + public boolean checkConstraintPossible() throws ImpossibleSubTypeException { if (this.subtype instanceof RefType && this.supertype instanceof RefType) { diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java index 446b5b34..fb9fb142 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java @@ -1,17 +1,18 @@ package de.dhbwstuttgart.syntaxtree.type; +import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.strucTypes.visitor.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; -public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric +public class GenericRefType extends RefType { private String name; public GenericRefType(String name, Token offset) { - super(offset); + super(new JavaClassName(name),offset); this.name = name; } diff --git a/test/strucType/TestPaperExample.java b/test/strucType/TestPaperExample.java index c6987b4a..63418340 100644 --- a/test/strucType/TestPaperExample.java +++ b/test/strucType/TestPaperExample.java @@ -25,16 +25,17 @@ import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; public class TestPaperExample { - public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/"; + public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/"; public final PrintConstraints printConstraints = new PrintConstraints(); @org.junit.Test public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException { ArrayList files = new ArrayList<>(); - files.add(new File(rootDirectory + "testPaperExample.jav")); - trans(files); - files.clear(); - files.add(new File(rootDirectory + "testMain.jav")); +// files.add(new File(rootDirectory + "testPaperExample.jav")); +// trans(files); +// files.clear(); +// files.add(new File(rootDirectory + "constructed/A.java")); + files.add(new File(rootDirectory + "javFiles/testMain.jav")); trans(files); } @@ -82,6 +83,7 @@ public class TestPaperExample { solvedClass.accept(syntaxtreeprinter); System.out.println("\nRemaining Constraints:"); printConstraints.printSubTypeConstraints(solvedClass.getConstraints()); + PrintInferredTypes.print(inferredTypesConstruct); System.out.println("____________________________________________________________________________"); } diff --git a/test/strucType/constructed/A.java b/test/strucType/constructed/A.java index 1d36e64e..d16cdc15 100644 --- a/test/strucType/constructed/A.java +++ b/test/strucType/constructed/A.java @@ -1,6 +1,6 @@ package strucType.constructed; -public class A, U>, M extends U, P, N extends V, V, AD extends O, U> { +public class A, M extends U, P, N extends V, V, AD extends O, U> { public A() { } diff --git a/test/strucType/typedtestclasses/MyInteger.java b/test/strucType/typedtestclasses/MyInteger.java index ed8ce447..d8b8a5db 100644 --- a/test/strucType/typedtestclasses/MyInteger.java +++ b/test/strucType/typedtestclasses/MyInteger.java @@ -5,11 +5,12 @@ import strucType.constructed.O; public class MyInteger implements L, O { + public Integer i; + public MyInteger(Integer i){ this.i = i; } - Integer i; @Override public MyInteger sub(MyInteger x) {