From 327f36f1c2cb345c5ceef23907b1c659d44b9099 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 28 Feb 2018 15:21:12 +0100 Subject: [PATCH 1/6] =?UTF-8?q?Faculty=20Test=20=C3=A4ndern.=20UnifyTypeFa?= =?UTF-8?q?ctory=20konvertiert=20automatisch=20FunN=20Typen=20korrekt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/factory/UnifyTypeFactory.java | 15 +++++++++++++++ test/javFiles/Faculty.jav | 6 +----- test/javFiles/FacultyTyped.jav | 19 +++++++++++++++++++ test/typeinference/JavaTXCompilerTest.java | 4 ++++ 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 test/javFiles/FacultyTyped.jav diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 43fcf1b4..e58ce50f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -1,12 +1,17 @@ package de.dhbwstuttgart.syntaxtree.factory; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.Void; @@ -72,6 +77,16 @@ public class UnifyTypeFactory { } public static UnifyType convert(RefType t){ + //Check if it is a FunN Type: + Pattern p = Pattern.compile("Fun(\\d+)"); + Matcher m = p.matcher(t.getName().toString()); + boolean b = m.matches(); + if(b){ + Integer N = Integer.valueOf(m.group(1)); + if((N + 1) == t.getParaList().size()){ + return convert(new FunN(t.getParaList())); + } + } UnifyType ret; if(t.getParaList() != null && t.getParaList().size() > 0){ List params = new ArrayList<>(); diff --git a/test/javFiles/Faculty.jav b/test/javFiles/Faculty.jav index 089d2f33..ca539add 100644 --- a/test/javFiles/Faculty.jav +++ b/test/javFiles/Faculty.jav @@ -6,14 +6,10 @@ class Faculty { return x; } - Fun1 m () { + m () { var fact = (Integer x) -> { return mul(x, fact.apply(x)); }; return fact; } -} - -interface Fun1{ - B apply(A a); } \ No newline at end of file diff --git a/test/javFiles/FacultyTyped.jav b/test/javFiles/FacultyTyped.jav new file mode 100644 index 00000000..089d2f33 --- /dev/null +++ b/test/javFiles/FacultyTyped.jav @@ -0,0 +1,19 @@ +import java.lang.Integer; + +class Faculty { + + Integer mul(Integer x, Integer y) { + return x; + } + + Fun1 m () { + var fact = (Integer x) -> { + return mul(x, fact.apply(x)); + }; + return fact; + } +} + +interface Fun1{ + B apply(A a); +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 3fe7c17d..bf48a537 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -53,6 +53,10 @@ public class JavaTXCompilerTest { execute(new File(rootDirectory+"Faculty.jav")); } @Test + public void facultyTyped() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"FacultyTyped.jav")); + } + @Test public void matrix() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Matrix.jav")); } From 99ce7c1122f51a292fac5e34849a61421e1f76c8 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 28 Feb 2018 16:33:18 +0100 Subject: [PATCH 2/6] =?UTF-8?q?<,=20>=20Operatoren=20einf=C3=BChren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../typeinference/typeAlgo/TYPEStmt.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index e6b82a10..8ed2343c 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -187,6 +187,7 @@ public class TYPEStmt implements StatementVisitor{ private final RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), new NullToken()); private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken()); + private final RefType bool = new RefType(ASTFactory.createClass(Boolean.class).getClassName(), new NullToken()); @Override public void visit(UnaryExpr unaryExpr) { if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT || @@ -229,7 +230,15 @@ public class TYPEStmt implements StatementVisitor{ numericAdditionOrStringConcatenation.add(stringConcat); } constraintsSet.addOderConstraint(numericAdditionOrStringConcatenation); - }else { + }else if(binary.operation.equals(BinaryExpr.Operator.LESSEQUAL) || + binary.operation.equals(BinaryExpr.Operator.BIGGEREQUAL) || + binary.operation.equals(BinaryExpr.Operator.BIGGERTHAN) || + binary.operation.equals(BinaryExpr.Operator.LESSTHAN)){ + constraintsSet.addUndConstraint(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); + constraintsSet.addUndConstraint(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); + //Rückgabetyp ist Boolean + constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT)); + }else{ throw new NotImplementedException(); } } From 53087a8e048235047cddf8ac14c4ba8c16f981b4 Mon Sep 17 00:00:00 2001 From: Pluemicke Martin Date: Wed, 28 Feb 2018 17:11:50 +0100 Subject: [PATCH 3/6] result.add wieder aktiviert --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 8d381e37..9a66543a 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -103,7 +103,7 @@ public class JavaTXCompiler { System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); System.out.println("RESULT: " + result.size()); - //results.addAll(result); + results.addAll(result); } return results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); From 9840281d836564329d28087dda97d22b044998cc Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 1 Mar 2018 12:31:56 +0100 Subject: [PATCH 4/6] =?UTF-8?q?Test=20f=C3=BCr=20ASP=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/factory/ASTFactory.java | 2 + .../syntaxtree/type/SuperWildcardType.java | 2 +- test/asp/ClingoTest.java | 1 - test/asp/UnifyWithoutWildcards.java | 91 ++++++++++++++++--- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index b0683569..8dc34ecf 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -43,6 +43,8 @@ public class ASTFactory { } int modifier = jreClass.getModifiers(); boolean isInterface = jreClass.isInterface(); + //see: https://stackoverflow.com/questions/9934774/getting-generic-parameter-from-supertype-class + //ParameterizedType parameterSuperClass = (ParameterizedType) jreClass.getGenericSuperclass(); java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; if(superjreClass != null){ diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index f630583e..740c80e5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -30,7 +30,7 @@ public class SuperWildcardType extends WildcardType{ * Beispiel: ? super Integer. * Integer wird zurückgegeben. */ - public RefTypeOrTPHOrWildcardOrGeneric get_SuperType() + public RefTypeOrTPHOrWildcardOrGeneric getInnerType() { return this.innerType; } diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java index d7656d8a..f47b644c 100644 --- a/test/asp/ClingoTest.java +++ b/test/asp/ClingoTest.java @@ -20,7 +20,6 @@ import java.io.*; import java.util.*; public class ClingoTest { - public static final String rootDirectory = "~/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards"; public static final String tempDirectory = "/tmp/"; private final TypePlaceholder testType = TypePlaceholder.fresh(new NullToken()); @Test diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 9e7c867d..6fd6373e 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -1,29 +1,98 @@ package asp; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResultSet; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Ref; +import java.util.*; public class UnifyWithoutWildcards { - public Collection getFC() { - Set ret = new HashSet<>(); - ret.add(ASTFactory.createObjectClass()); - ret.add(ASTFactory.createClass(java.util.List.class)); + public static final String tempDirectory = "/tmp/"; + + @Test + public void adapt() throws InterruptedException, IOException, ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("Matrix"), new NullToken()); + RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + run(testSet); + } + + public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { + String content = ""; + content = new ASPGenerator(toTest, this.getFC()).getASP(); + + PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); + writer.println(content); + writer.close(); + Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); + String result = clingo.runClingo(); + ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest)); + return resultSet; + } + + private static class TPHExtractor implements TypeVisitor>{ + @Override + public List visit(RefType refType) { + ArrayList ret = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + ret.addAll(param.acceptTV(this)); + } + return ret; + } + + @Override + public List visit(SuperWildcardType superWildcardType) { + return superWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(TypePlaceholder typePlaceholder) { + return Arrays.asList(typePlaceholder); + } + + @Override + public List visit(ExtendsWildcardType extendsWildcardType) { + return extendsWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(GenericRefType genericRefType) { + return new ArrayList<>(); + } + } + private Collection getInvolvedTPHS(ConstraintSet toTest) { + List ret = new ArrayList<>(); + toTest.map((Pair p)-> { + ret.addAll(p.TA1.acceptTV(new TPHExtractor())); + ret.addAll(p.TA2.acceptTV(new TPHExtractor())); + return p; + }); return ret; } - public ConstraintSet getPairs() { - ConstraintSet ret = new ConstraintSet<>(); - ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + private Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createClass(Matrix.class)); + //ret.add(ASTFactory.createObjectClass()); + //ret.add(ASTFactory.createClass(java.util.List.class)); return ret; } + private class Matrix extends Vector>{} } From 18545cff9a434bc6c307ffc86879e387e606a1f5 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 1 Mar 2018 13:25:03 +0100 Subject: [PATCH 5/6] =?UTF-8?q?GenericRefType=20umgestalten.=20Unn=C3=B6ti?= =?UTF-8?q?ge=20Backreferenzen=20entfernen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/signature/Signature.java | 6 ++-- .../SyntaxTreeGenerator/FCGenerator.java | 11 +++---- .../SyntaxTreeGenerator/GenericContext.java | 4 +-- .../SyntaxTreeGenerator/TypeGenerator.java | 5 ++- .../parser/scope/GenericTypeName.java | 26 --------------- .../sat/asp/writer/ASPGenerator.java | 4 +-- .../syntaxtree/GenericTypeVar.java | 15 +++------ .../syntaxtree/factory/ASTFactory.java | 32 +++++++++++++------ .../syntaxtree/type/GenericRefType.java | 10 ++---- .../syntaxtree/visual/OutputGenerator.java | 3 +- .../typedeployment/TypeInsertFactory.java | 2 +- .../typeinference/assumptions/FunNClass.java | 5 +-- .../assumptions/MethodAssumption.java | 3 -- .../typeinference/typeAlgo/TYPEStmt.java | 3 +- test/asp/UnifyWithoutWildcards.java | 2 +- 15 files changed, 47 insertions(+), 84 deletions(-) delete mode 100644 src/de/dhbwstuttgart/parser/scope/GenericTypeName.java diff --git a/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index f9bcb369..46202cb1 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -10,12 +10,10 @@ import org.objectweb.asm.signature.SignatureWriter; import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; -import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; -import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -165,7 +163,7 @@ public class Signature { * @param genAndBounds */ private void getBoundsOfTypeVar(GenericTypeVar g, HashMap genAndBounds) { - sw.visitFormalTypeParameter(g.getParsedName()); + sw.visitFormalTypeParameter(g.getName()); Iterator bItr = g.getBounds().iterator(); while(bItr.hasNext()) { @@ -174,7 +172,7 @@ public class Signature { // System.out.println("GetBounds: " + boundDesc); // Ensure that <...> extends java.lang.Object OR ... sw.visitClassBound().visitClassType(boundDesc); - genAndBounds.put(g.getParsedName(), boundDesc); + genAndBounds.put(g.getName(), boundDesc); } sw.visitClassBound().visitEnd(); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index ea2b17a6..24abec67 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -1,7 +1,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.DebugException; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -51,11 +50,11 @@ public class FCGenerator { HashMap newGTVs = new HashMap<>(); //Generics mit gleichem Namen müssen den selben TPH bekommen for(GenericTypeVar gtv : forType.getGenerics()){ - if(!gtvs.containsKey(gtv.getParsedName())){ - gtvs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); - newGTVs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); + if(!gtvs.containsKey(gtv.getName())){ + gtvs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken())); + newGTVs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken())); } - params.add(gtvs.get(gtv.getParsedName())); + params.add(gtvs.get(gtv.getName())); } Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); @@ -79,7 +78,7 @@ public class FCGenerator { RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: RefTypeOrTPHOrWildcardOrGeneric setSetType = setType.acceptTV(new TypeExchanger(gtvs)); - newGTVs.put(itGenParams.next().getParsedName(), setSetType); + newGTVs.put(itGenParams.next().getName(), setSetType); } RefTypeOrTPHOrWildcardOrGeneric superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java index 6e3ce7d9..0cea06ed 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java @@ -3,8 +3,8 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; public class GenericContext { - public final String parentMethod; - public final JavaClassName parentClass; + private final String parentMethod; + private final JavaClassName parentClass; public GenericContext(JavaClassName parentClass, String parentMethod) { if(parentMethod == null)parentMethod = ""; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index ccc7b327..38399bf0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.antlr.Java8Parser; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; @@ -75,7 +74,7 @@ public class TypeGenerator { //Problem ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen List bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics); - GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop()); + GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop()); generics.put(name, new GenericContext(parentClass, parentMethod)); return ret; } @@ -133,7 +132,7 @@ public class TypeGenerator { String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein if(generics.contains(name)){ - return new GenericRefType(new GenericTypeName(generics.get(name),name), offset); + return new GenericRefType(name, offset); }else{ throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset); } diff --git a/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java b/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java deleted file mode 100644 index c5355ebd..00000000 --- a/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.dhbwstuttgart.parser.scope; - -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; - -public class GenericTypeName extends JavaClassName { - private final static String DELIMITER = "%"; - - private final JavaClassName parentClass; - private final String methodName; - - public GenericTypeName(GenericContext genericContext, String name) { - super(name); - this.parentClass = genericContext.parentClass; - this.methodName = genericContext.parentMethod; - } - - public String getUniqueIdentifier() { - return parentClass.toString() - + DELIMITER + methodName - + DELIMITER + super.toString(); - } - - public JavaClassName getParentClass() { - return parentClass; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index b1483423..676aa33f 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -2,12 +2,10 @@ package de.dhbwstuttgart.sat.asp.writer; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; -import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -100,7 +98,7 @@ public class ASPGenerator { @Override public ASPType visit(GenericRefType genericRefType) { - return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getName()), + return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getParsedName()), new ASPParameterList(new ArrayList<>(), writer)); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java index 02042ca3..6ee03b60 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java @@ -1,7 +1,5 @@ package de.dhbwstuttgart.syntaxtree; -import de.dhbwstuttgart.parser.scope.GenericTypeName; -import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -26,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode */ List bounds=new ArrayList(); private Token endOffset; - private GenericTypeName name; + private String name; - public GenericTypeVar(GenericTypeName s, List bounds, Token offset, Token endOffset) + public GenericTypeVar(String s, List bounds, Token offset, Token endOffset) { super(offset); name = s; @@ -50,18 +48,15 @@ public class GenericTypeVar extends SyntaxTreeNode return "BoGTV " + this.name; } - public GenericTypeName getName() { - return name; - } - - public String getParsedName(){ + public String getName(){ return name.toString(); } + /* public JavaClassName definingClass(){ return name.getParentClass(); } - +*/ @Override public void accept(ASTVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 8dc34ecf..fa930f58 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -8,7 +8,6 @@ import java.util.List; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.Field; @@ -44,13 +43,16 @@ public class ASTFactory { int modifier = jreClass.getModifiers(); boolean isInterface = jreClass.isInterface(); //see: https://stackoverflow.com/questions/9934774/getting-generic-parameter-from-supertype-class - //ParameterizedType parameterSuperClass = (ParameterizedType) jreClass.getGenericSuperclass(); + ParameterizedType parameterSuperClass = null; + Type tempSuperClass = jreClass.getGenericSuperclass(); + if(tempSuperClass != null && tempSuperClass instanceof ParameterizedType) + parameterSuperClass = (ParameterizedType) tempSuperClass; java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; if(superjreClass != null){ List params = new ArrayList<>(); for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); + params.add(new GenericRefType(tv.getName(), new NullToken())); } superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) @@ -67,6 +69,22 @@ public class ASTFactory { return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); } + private static RefTypeOrTPHOrWildcardOrGeneric convertType(Type type){ + JavaClassName name = null; + List params = new ArrayList<>(); + if(type instanceof ParameterizedType){ + for(Type paramType : ((ParameterizedType)type).getActualTypeArguments()){ + params.add(convertType(paramType)); + } + }else if(type instanceof TypeVariable){ + + }else if(type instanceof Class){ + Class paramClass = (Class) type; + //params.add(new RefType(paramClass.getName()) + }else throw new NotImplementedException(); + return new RefType(name, params, new NullToken()); + } + private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { return new Field(field.getName(), createType(field.getType(), jreClass, null), field.getModifiers(), new NullToken()); } @@ -155,9 +173,7 @@ public class ASTFactory { }else{ if(type instanceof TypeVariable){ //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" - return new GenericRefType( - new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()), - new NullToken()); + return new GenericRefType(type.getTypeName(), new NullToken()); } List params = new ArrayList<>(); if(type instanceof ParameterizedType){ @@ -179,9 +195,7 @@ public class ASTFactory { genericBounds.add((RefType) createType(bound, parentClass, parentMethod)); } } - return new de.dhbwstuttgart.syntaxtree.GenericTypeVar( - new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName) - , genericBounds, new NullToken(), new NullToken()); + return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(jreTVName, genericBounds, new NullToken(), new NullToken()); } public static ClassOrInterface createObjectClass() { diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java index a5d394aa..a14813e7 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java @@ -1,25 +1,19 @@ package de.dhbwstuttgart.syntaxtree.type; -import de.dhbwstuttgart.parser.scope.GenericTypeName; -import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric { - private GenericTypeName name; + private String name; - public GenericRefType(GenericTypeName name, Token offset) + public GenericRefType(String name, Token offset) { super(offset); this.name = name; } - public GenericTypeName getName(){ - return name; - } - public String getParsedName(){ return name.toString(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index c7a06502..0c623548 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.syntaxtree.visual; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; @@ -171,7 +170,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(GenericRefType genericRefType) { - out.append(genericRefType.getName().toString()); + out.append(genericRefType.getParsedName().toString()); } @Override diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 29492c31..8834f859 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -126,7 +126,7 @@ class TypeToInsertString implements ResultSetVisitor{ @Override public void visit(GenericRefType genericRefType) { - insert += genericRefType.getName(); + insert += genericRefType.getParsedName(); } @Override diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java index a04ce9bb..3518c16a 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java @@ -1,8 +1,6 @@ package de.dhbwstuttgart.typeinference.assumptions; import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; @@ -29,8 +27,7 @@ public class FunNClass extends ClassOrInterface { private static GenericDeclarationList createGenerics(List funNParams) { List generics = new ArrayList<>(); for(RefTypeOrTPHOrWildcardOrGeneric param : funNParams){ - generics.add(new GenericTypeVar(new GenericTypeName(new GenericContext( - new JavaClassName("Fun"+(funNParams.size()-1)), null), NameGenerator.makeNewName()), + generics.add(new GenericTypeVar(NameGenerator.makeNewName(), new ArrayList<>(), new NullToken(), new NullToken())); } return new GenericDeclarationList(generics, new NullToken()); diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java index 066c3246..4656318c 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java @@ -3,9 +3,7 @@ package de.dhbwstuttgart.typeinference.assumptions; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.TypeScope; -import de.dhbwstuttgart.syntaxtree.statement.Assign; import de.dhbwstuttgart.syntaxtree.type.FunN; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; @@ -14,7 +12,6 @@ import de.dhbwstuttgart.typeinference.constraints.GenericsResolver; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class MethodAssumption extends Assumption{ private ClassOrInterface receiver; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 8ed2343c..1fc2c3d8 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -4,7 +4,6 @@ import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; -import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.*; @@ -339,7 +338,7 @@ public class TYPEStmt implements StatementVisitor{ List params = new ArrayList<>(); for(GenericTypeVar gtv : receiverCl.getGenerics()){ //Die Generics werden alle zu TPHs umgewandelt. - params.add(resolver.resolve(gtv.getParsedName())); + params.add(resolver.resolve(gtv.getName())); } RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset()); diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 6fd6373e..db697312 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -94,5 +94,5 @@ public class UnifyWithoutWildcards { //ret.add(ASTFactory.createClass(java.util.List.class)); return ret; } - private class Matrix extends Vector>{} + private class Matrix extends HashMap>{} } From 725b071d28c0962577e84df3b3a4ade3495db5ac Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 08:47:43 +0100 Subject: [PATCH 6/6] Problem mit BinaryExpr gefixt --- .../dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 10 +++++++--- test/javFiles/AddLong.jav | 9 +++++++++ test/typeinference/JavaTXCompilerTest.java | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/javFiles/AddLong.jav diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 1fc2c3d8..8d87218d 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -205,6 +205,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(BinaryExpr binary) { + if(binary.operation.equals(BinaryExpr.Operator.DIV) || binary.operation.equals(BinaryExpr.Operator.MUL)|| binary.operation.equals(BinaryExpr.Operator.MOD)|| @@ -216,9 +217,12 @@ public class TYPEStmt implements StatementVisitor{ //Expression muss zu Numeric Convertierbar sein. also von Numeric erben numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); - //The type of a multiplicative expression is the promoted type of its operands. - numeric.add(new Pair(binary.rexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.lexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); + /* + In Java passiert bei den binären Operatoren eine sogenannte Type Promotion: + https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2 + Das bedeutet, dass Java die Typen je nach belieben castet, so lange sie nur von Number erben + */ + numeric.add(new Pair(binary.getType(), number, PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); if(binary.operation.equals(BinaryExpr.Operator.ADD)) { //Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2) diff --git a/test/javFiles/AddLong.jav b/test/javFiles/AddLong.jav new file mode 100644 index 00000000..ceb31228 --- /dev/null +++ b/test/javFiles/AddLong.jav @@ -0,0 +1,9 @@ +import java.lang.Integer; +import java.lang.Long; + +public class AddLong{ + add(Integer a, Long b) { + Long c = a+b; + return c; + } +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index bf48a537..87551476 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -72,6 +72,10 @@ public class JavaTXCompilerTest { public void expressions() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Expressions.jav")); } + @Test + public void addLong() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"AddLong.jav")); + } private static class TestResultSet{