From 45c85b76861578cf027bb0aecbfdf7c22afe6de3 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 28 Aug 2017 15:42:51 +0200 Subject: [PATCH] Type refactoring --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 7 +- .../syntaxtree/ClassOrInterface.java | 8 - .../dhbwstuttgart/syntaxtree/Constructor.java | 12 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 9 +- .../dhbwstuttgart/syntaxtree/SourceFile.java | 21 +- .../typeinference/typeAlgo/TYPE.java | 334 +++--------------- .../typeinference/typeAlgo/TYPEStmt.java | 322 +++++++++++++++++ test/javFiles/Matrix.jav | 19 +- test/typeinference/JavaTXCompilerTest.java | 19 +- 9 files changed, 390 insertions(+), 361 deletions(-) create mode 100644 src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 34e821f1..e8929729 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -14,6 +14,7 @@ import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; @@ -29,15 +30,13 @@ public class JavaTXCompiler { protected List sourceFiles = new ArrayList<>(); public ResultSet typeInference(){ - ConstraintSet cons = new ConstraintSet<>(); List allClasses = new ArrayList<>(); for(SourceFile sf : sourceFiles){ allClasses.addAll(sf.getClasses()); } FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); - for(SourceFile sf : sourceFiles){ - cons.addAll(sf.getConstraints(sf.getTypeInferenceInformation(sourceFiles))); - } + ConstraintSet cons = new ConstraintSet<>(); + cons = new TYPE(sourceFiles).getConstraints(); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 1733a3ac..8c8dd698 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -58,14 +58,6 @@ public class ClassOrInterface extends SyntaxTreeNode { return this.methods; } - public ConstraintSet getConstraints(TypeInferenceInformation info) { - ConstraintSet ret = new ConstraintSet(); - for(Method m : this.getMethods()){ - ret.addAll(m.getConstraints(info, this)); - } - return ret; - } - public RefType getType() { return generateTypeOfClass(this.getClassName(), this.getGenerics(), this.getOffset()); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 219aeefe..9f085051 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; +import de.dhbwstuttgart.typeinference.typeAlgo.TYPEStmt; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.statement.Block; @@ -19,7 +20,7 @@ public class Constructor extends Method { /** * Das sind die Statements, welche die Felder der zugehörigen Klasse dieses Konstruktor initialisieren */ - private final List fieldInitializations; + public final List fieldInitializations; public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { super(name, returnType, modifiers, parameterList, codeInsideConstructor, gtvDeclarations, offset); @@ -27,15 +28,6 @@ public class Constructor extends Method { this.fieldInitializations = fieldInitializations; } - public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { - TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, null); - TYPE methodScope = new TYPE(blockInfo); - for(Statement stmt : fieldInitializations)stmt.accept(methodScope); - ConstraintSet ret = super.getConstraints(info, currentClass); - ret.addAll(methodScope.getConstraints()); - return ret; - } - @Override public void accept(ASTVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 065945ee..67d81cb4 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -8,6 +8,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; +import de.dhbwstuttgart.typeinference.typeAlgo.TYPEStmt; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.core.IItemWithOffset; @@ -36,14 +37,6 @@ public class Method extends Field implements IItemWithOffset, TypeScope this.generics = gtvDeclarations; } - public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { - if(block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints - TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this); - TYPE methodScope = new TYPE(blockInfo); - block.accept(methodScope); - return methodScope.getConstraints(); - } - public ParameterList getParameterList() { return parameterlist; } diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 2278a00e..91c9b514 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -13,8 +13,8 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; public class SourceFile extends SyntaxTreeNode{ private String pkgName; - private final List KlassenVektor; - private final List imports; + public final List KlassenVektor; + public final List imports; /** * Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei. @@ -36,23 +36,6 @@ public class SourceFile extends SyntaxTreeNode{ return this.imports; } - public ConstraintSet getConstraints(TypeInferenceInformation info) { - ConstraintSet ret = new ConstraintSet(); - for (ClassOrInterface cl : this.KlassenVektor) { - ret.addAll(cl.getConstraints(info)); - } - return ret; - } - - public TypeInferenceInformation getTypeInferenceInformation(List sourceFiles){ - Set classes = new HashSet<>(); - for(SourceFile sourceFile : sourceFiles){ - classes.addAll(sourceFile.KlassenVektor); - } - - return new TypeInferenceInformation(classes); - } - public List getClasses() { return KlassenVektor; } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index eaa57cdd..17da1116 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,318 +1,66 @@ package de.dhbwstuttgart.typeinference.typeAlgo; -//import com.sun.org.apache.xpath.internal.Arg; -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.exceptions.TypeinferenceException; -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; -import de.dhbwstuttgart.syntaxtree.type.FunN; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; -import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; +import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; - import java.util.*; -import java.util.stream.Collectors; -public class TYPE implements StatementVisitor{ +public class TYPE { - private final TypeInferenceBlockInformation info; - private final ConstraintSet constraintsSet = new ConstraintSet(); + private final List sfs; - public TYPE(TypeInferenceBlockInformation info){ - this.info = info; + public TYPE(List sourceFiles){ + sfs = sourceFiles; } + public ConstraintSet getConstraints() { - return constraintsSet; - } - - @Override - public void visit(LambdaExpression lambdaExpression) { - TypePlaceholder tphRetType = TypePlaceholder.fresh(new NullToken()); - List lambdaParams = lambdaExpression.params.getFormalparalist().stream().map((formalParameter -> formalParameter.getType())).collect(Collectors.toList()); - //lambdaParams.add(tphRetType); - lambdaParams.add(0,tphRetType); - constraintsSet.addUndConstraint( - ConstraintsFactory.createPair(lambdaExpression.getType(), - new FunN(lambdaParams),PairOperator.EQUALSDOT,info)); - constraintsSet.addUndConstraint( - ConstraintsFactory.createPair(lambdaExpression.getReturnType(), - tphRetType,info)); - - //Constraints des Bodys generieren: - TYPE lambdaScope = new TYPE(new TypeInferenceBlockInformation(info, lambdaExpression)); - lambdaExpression.methodBody.accept(lambdaScope); - constraintsSet.addAll(lambdaScope.getConstraints()); - } - - @Override - public void visit(Assign assign) { - assign.lefSide.accept(this); - assign.rightSide.accept(this); - constraintsSet.addUndConstraint(ConstraintsFactory.createPair( - assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT, info)); - } - - @Override - public void visit(Binary binary) { - throw new NotImplementedException(); - } - - @Override - public void visit(Block block) { - for(Statement stmt : block.getStatements()){ - stmt.accept(this); - } - } - - @Override - public void visit(CastExpr castExpr) { - throw new NotImplementedException(); - } - - @Override - public void visit(EmptyStmt emptyStmt) { - //Nothing :) - } - - @Override - public void visit(FieldVar fieldVar) { - fieldVar.receiver.accept(this); - Set oderConstraints = new HashSet<>(); - for(FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)){ - Constraint constraint = new Constraint(); - constraint.add(ConstraintsFactory.createPair( - fieldVar.receiver.getType(),fieldAssumption.getReceiverType(), info)); - constraint.add(ConstraintsFactory.createPair( - fieldVar.getType(),fieldAssumption.getType(), info)); - oderConstraints.add(constraint); - } - if(oderConstraints.size() == 0) - throw new TypeinferenceException("Kein Feld "+fieldVar.fieldVarName+ " gefunden", fieldVar.getOffset()); - constraintsSet.addOderConstraint(oderConstraints); - } - - @Override - public void visit(ForStmt forStmt) { - throw new NotImplementedException(); - } - - @Override - public void visit(IfStmt ifStmt) { - throw new NotImplementedException(); - } - - @Override - public void visit(InstanceOf instanceOf) { - throw new NotImplementedException(); - } - - @Override - public void visit(LocalVar localVar) { - // Es werden nur bei Feldvariablen Constraints generiert. Lokale Variablen sind eindeutig - } - - @Override - public void visit(LocalVarDecl localVarDecl) { - //Hier ist nichts zu tun. Allen lokalen Variablen bekommen beim parsen schon den korrekten Typ - } - - @Override - public void visit(MethodCall methodCall) { - methodCall.receiver.accept(this); - constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info)); - //Overloading: - Set methodConstraints = new HashSet<>(); - for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){ - methodConstraints.add(generateConstraint(methodCall, m, info)); - } - if(methodConstraints.size()<1){ - throw new TypeinferenceException("Methode "+methodCall.name+" ist nicht vorhanden!",methodCall.getOffset()); - } - constraintsSet.addOderConstraint(methodConstraints); - } - - @Override - public void visit(NewClass methodCall) { - constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info)); - //Overloading: - Set methodConstraints = new HashSet<>(); - for(MethodAssumption m : this.getConstructors(info, (RefType) methodCall.getType(), methodCall.getArgumentList())){ - methodConstraints.add(generateConstructorConstraint(methodCall, m, info)); - } - if(methodConstraints.size()<1){ - throw new TypeinferenceException("Konstruktor in Klasse "+methodCall.getType().toString()+" ist nicht vorhanden!",methodCall.getOffset()); - } - constraintsSet.addOderConstraint(methodConstraints); - } - - @Override - public void visit(NewArray newArray) { - throw new NotImplementedException(); - } - - @Override - public void visit(Receiver receiver) { - receiver.expr.accept(this); - } - - @Override - public void visit(Return returnExpr) { - returnExpr.retexpr.accept(this); - constraintsSet.addUndConstraint(ConstraintsFactory.createPair( - returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT, info)); - } - - @Override - public void visit(ReturnVoid aReturn) { - visit((Return) aReturn); - } - - @Override - public void visit(StaticClassName staticClassName) { - throw new NotImplementedException(); - } - - @Override - public void visit(Super aSuper) { - throw new NotImplementedException(); - } - - @Override - public void visit(This aThis) { - constraintsSet.addUndConstraint(ConstraintsFactory.createPair( aThis.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info)); - } - - @Override - public void visit(UnaryPlus unaryPlus) { - throw new NotImplementedException(); - } - - @Override - public void visit(WhileStmt whileStmt) { - throw new NotImplementedException(); - } - - @Override - public void visit(DoStmt whileStmt) { - throw new NotImplementedException(); - } - - @Override - public void visit(Null aNull) { - throw new NotImplementedException(); - } - - @Override - public void visit(Literal literal) { - throw new NotImplementedException(); - } - - @Override - public void visit(AssignToField assignLeftSide) { - //Hier ist kein Code nötig. Es werden keine extra Constraints generiert - } - - @Override - public void visit(AssignToLocal assignLeftSide) { - //Hier ist kein Code nötig. Es werden keine extra Constraints generiert - } - - /* - METHOD CALL Section: - */ - - protected Constraint generateConstraint(MethodCall forMethod, MethodAssumption assumption, TypeInferenceBlockInformation info){ - Constraint methodConstraint = new Constraint(); - methodConstraint.add(ConstraintsFactory.createPair(forMethod.receiver.getType(), assumption.getReceiverType(), PairOperator.SMALLERDOT, info)); - methodConstraint.add(ConstraintsFactory.createPair(assumption.getReturnType(), forMethod.getType(), PairOperator.EQUALSDOT, info)); - methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info)); - return methodConstraint; - } - - protected Set generateParameterConstraints(MethodCall foMethod, MethodAssumption assumption, TypeInferenceBlockInformation info) { - Set ret = new HashSet<>(); - for(int i = 0;i getMethods(String name, int numArgs, TypeInferenceBlockInformation info) { - List ret = new ArrayList<>(); - if(name.equals("apply")){ - List funNParams = new ArrayList<>(); - for(int i = 0; i< numArgs + 1 ; i++){ - funNParams.add(TypePlaceholder.fresh(new NullToken())); - } - ret.add(new MethodAssumption(new FunN(funNParams), funNParams.get(0), funNParams.subList(1, funNParams.size()))); - } - for(ClassOrInterface cl : info.getAvailableClasses()){ - for(Method m : cl.getMethods()){ - if(m.getName().equals(name) && - m.getParameterList().getFormalparalist().size() == numArgs){ - RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); - - ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); - } - } + private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) { + ConstraintSet ret = new ConstraintSet(); + for(Method m : cl.getMethods()){ + ret.addAll(getConstraintsMethod(m,info, cl)); } return ret; } - public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { - return getMethods(name, arglist.getArguments().size(), info); - } - - protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ - List params = new ArrayList<>(); - for(FormalParameter fp : parameterList.getFormalparalist()){ - params.add(info.checkGTV(fp.getType())); - } - return params; - } - - public ConstraintSet getArgumentListConstraints(MethodCall forMethod, TypeInferenceBlockInformation info) { - TYPE ret = new TYPE(info); - for(int i = 0;i getConstructors(TypeInferenceBlockInformation info, RefType ofType, ArgumentList argList){ - List ret = new ArrayList<>(); - for(ClassOrInterface cl : info.getAvailableClasses()){ - if(cl.getClassName().equals(ofType.getName())){ - for(Method m : cl.getConstructors()){ - if(m.getParameterList().getFormalparalist().size() == argList.getArguments().size()){ - ret.add(new MethodAssumption(ofType, ofType, convertParams(m.getParameterList(), info))); - } - } + private TypeInferenceInformation getTypeInferenceInformation(){ + Set classes = new HashSet<>(); + for(SourceFile sourceFile : sfs){ + for(JavaClassName importName : sourceFile.imports){ + System.out.println(importName); } + classes.addAll(sourceFile.KlassenVektor); } + + return new TypeInferenceInformation(classes); + } + + private ConstraintSet getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) { + if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints + TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m); + TYPEStmt methodScope = new TYPEStmt(blockInfo); + m.block.accept(methodScope); + return methodScope.getConstraints(); + } + + private ConstraintSet getConstraintsConstructor(Constructor m, TypeInferenceInformation info, ClassOrInterface currentClass) { + TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, null); + TYPEStmt methodScope = new TYPEStmt(blockInfo); + for(Statement stmt : m.fieldInitializations)stmt.accept(methodScope); + ConstraintSet ret = this.getConstraintsMethod(m, info, currentClass); + ret.addAll(methodScope.getConstraints()); return ret; } - - protected Constraint generateConstructorConstraint(NewClass forConstructor, MethodAssumption assumption, TypeInferenceBlockInformation info){ - Constraint methodConstraint = new Constraint(); - methodConstraint.add(ConstraintsFactory.createPair(assumption.getReturnType(), forConstructor.getType(), PairOperator.SMALLERDOT, info)); - methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info)); - return methodConstraint; - } - } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java new file mode 100644 index 00000000..34f17a56 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -0,0 +1,322 @@ +package de.dhbwstuttgart.typeinference.typeAlgo; + +//import com.sun.org.apache.xpath.internal.Arg; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.exceptions.TypeinferenceException; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.*; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.type.FunN; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; +import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class TYPEStmt implements StatementVisitor{ + + private final TypeInferenceBlockInformation info; + private final ConstraintSet constraintsSet = new ConstraintSet(); + + public TYPEStmt(TypeInferenceBlockInformation info){ + this.info = info; + } + + public ConstraintSet getConstraints() { + return constraintsSet; + } + + @Override + public void visit(LambdaExpression lambdaExpression) { + TypePlaceholder tphRetType = TypePlaceholder.fresh(new NullToken()); + List lambdaParams = lambdaExpression.params.getFormalparalist().stream().map((formalParameter -> formalParameter.getType())).collect(Collectors.toList()); + //lambdaParams.add(tphRetType); + lambdaParams.add(0,tphRetType); + constraintsSet.addUndConstraint( + ConstraintsFactory.createPair(lambdaExpression.getType(), + new FunN(lambdaParams),PairOperator.EQUALSDOT,info)); + constraintsSet.addUndConstraint( + ConstraintsFactory.createPair(lambdaExpression.getReturnType(), + tphRetType,info)); + + //Constraints des Bodys generieren: + TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression)); + lambdaExpression.methodBody.accept(lambdaScope); + constraintsSet.addAll(lambdaScope.getConstraints()); + } + + @Override + public void visit(Assign assign) { + assign.lefSide.accept(this); + assign.rightSide.accept(this); + constraintsSet.addUndConstraint(ConstraintsFactory.createPair( + assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT, info)); + } + + @Override + public void visit(Binary binary) { + throw new NotImplementedException(); + } + + @Override + public void visit(Block block) { + for(Statement stmt : block.getStatements()){ + stmt.accept(this); + } + } + + @Override + public void visit(CastExpr castExpr) { + throw new NotImplementedException(); + } + + @Override + public void visit(EmptyStmt emptyStmt) { + //Nothing :) + } + + @Override + public void visit(FieldVar fieldVar) { + fieldVar.receiver.accept(this); + Set oderConstraints = new HashSet<>(); + for(FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)){ + Constraint constraint = new Constraint(); + constraint.add(ConstraintsFactory.createPair( + fieldVar.receiver.getType(),fieldAssumption.getReceiverType(), info)); + constraint.add(ConstraintsFactory.createPair( + fieldVar.getType(),fieldAssumption.getType(), info)); + oderConstraints.add(constraint); + } + if(oderConstraints.size() == 0) + throw new TypeinferenceException("Kein Feld "+fieldVar.fieldVarName+ " gefunden", fieldVar.getOffset()); + constraintsSet.addOderConstraint(oderConstraints); + } + + @Override + public void visit(ForStmt forStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(IfStmt ifStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(InstanceOf instanceOf) { + throw new NotImplementedException(); + } + + @Override + public void visit(LocalVar localVar) { + // Es werden nur bei Feldvariablen Constraints generiert. Lokale Variablen sind eindeutig + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + //Hier ist nichts zu tun. Allen lokalen Variablen bekommen beim parsen schon den korrekten Typ + } + + @Override + public void visit(MethodCall methodCall) { + methodCall.receiver.accept(this); + constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info)); + //Overloading: + Set methodConstraints = new HashSet<>(); + for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){ + methodConstraints.add(generateConstraint(methodCall, m, info)); + } + if(methodConstraints.size()<1){ + throw new TypeinferenceException("Methode "+methodCall.name+" ist nicht vorhanden!",methodCall.getOffset()); + } + constraintsSet.addOderConstraint(methodConstraints); + } + + @Override + public void visit(NewClass methodCall) { + constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info)); + //Overloading: + Set methodConstraints = new HashSet<>(); + for(MethodAssumption m : this.getConstructors(info, (RefType) methodCall.getType(), methodCall.getArgumentList())){ + methodConstraints.add(generateConstructorConstraint(methodCall, m, info)); + } + if(methodConstraints.size()<1){ + throw new TypeinferenceException("Konstruktor in Klasse "+methodCall.getType().toString()+" ist nicht vorhanden!",methodCall.getOffset()); + } + constraintsSet.addOderConstraint(methodConstraints); + } + + @Override + public void visit(NewArray newArray) { + throw new NotImplementedException(); + } + + @Override + public void visit(Receiver receiver) { + receiver.expr.accept(this); + } + + @Override + public void visit(Return returnExpr) { + returnExpr.retexpr.accept(this); + constraintsSet.addUndConstraint(ConstraintsFactory.createPair( + returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT, info)); + } + + @Override + public void visit(ReturnVoid aReturn) { + visit((Return) aReturn); + } + + @Override + public void visit(StaticClassName staticClassName) { + throw new NotImplementedException(); + } + + @Override + public void visit(Super aSuper) { + throw new NotImplementedException(); + } + + @Override + public void visit(This aThis) { + constraintsSet.addUndConstraint(ConstraintsFactory.createPair( aThis.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info)); + } + + @Override + public void visit(UnaryPlus unaryPlus) { + throw new NotImplementedException(); + } + + @Override + public void visit(WhileStmt whileStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(DoStmt whileStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(Null aNull) { + throw new NotImplementedException(); + } + + @Override + public void visit(Literal literal) { + throw new NotImplementedException(); + } + + @Override + public void visit(AssignToField assignLeftSide) { + //Hier ist kein Code nötig. Es werden keine extra Constraints generiert + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + //Hier ist kein Code nötig. Es werden keine extra Constraints generiert + } + + /* + METHOD CALL Section: + */ + + protected Constraint generateConstraint(MethodCall forMethod, MethodAssumption assumption, TypeInferenceBlockInformation info){ + Constraint methodConstraint = new Constraint(); + methodConstraint.add(ConstraintsFactory.createPair(forMethod.receiver.getType(), assumption.getReceiverType(), PairOperator.SMALLERDOT, info)); + methodConstraint.add(ConstraintsFactory.createPair(assumption.getReturnType(), forMethod.getType(), PairOperator.EQUALSDOT, info)); + methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info)); + return methodConstraint; + } + + protected Set generateParameterConstraints(MethodCall foMethod, MethodAssumption assumption, TypeInferenceBlockInformation info) { + Set ret = new HashSet<>(); + for(int i = 0;i getMethods(String name, int numArgs, TypeInferenceBlockInformation info) { + List ret = new ArrayList<>(); + if(name.equals("apply")){ + List funNParams = new ArrayList<>(); + for(int i = 0; i< numArgs + 1 ; i++){ + funNParams.add(TypePlaceholder.fresh(new NullToken())); + } + ret.add(new MethodAssumption(new FunN(funNParams), funNParams.get(0), funNParams.subList(1, funNParams.size()))); + } + for(ClassOrInterface cl : info.getAvailableClasses()){ + for(Method m : cl.getMethods()){ + if(m.getName().equals(name) && + m.getParameterList().getFormalparalist().size() == numArgs){ + RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); + + ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); + } + } + } + return ret; + } + + public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { + return getMethods(name, arglist.getArguments().size(), info); + } + + protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ + List params = new ArrayList<>(); + for(FormalParameter fp : parameterList.getFormalparalist()){ + params.add(info.checkGTV(fp.getType())); + } + return params; + } + + public ConstraintSet getArgumentListConstraints(MethodCall forMethod, TypeInferenceBlockInformation info) { + TYPEStmt ret = new TYPEStmt(info); + for(int i = 0;i getConstructors(TypeInferenceBlockInformation info, RefType ofType, ArgumentList argList){ + List ret = new ArrayList<>(); + for(ClassOrInterface cl : info.getAvailableClasses()){ + if(cl.getClassName().equals(ofType.getName())){ + for(Method m : cl.getConstructors()){ + if(m.getParameterList().getFormalparalist().size() == argList.getArguments().size()){ + ret.add(new MethodAssumption(ofType, ofType, convertParams(m.getParameterList(), info))); + } + } + } + } + return ret; + } + + protected Constraint generateConstructorConstraint(NewClass forConstructor, MethodAssumption assumption, TypeInferenceBlockInformation info){ + Constraint methodConstraint = new Constraint(); + methodConstraint.add(ConstraintsFactory.createPair(assumption.getReturnType(), forConstructor.getType(), PairOperator.SMALLERDOT, info)); + methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info)); + return methodConstraint; + } + +} diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index 7096ba0c..65dc465a 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,14 +1,13 @@ -class Matrix extends Menge> { +import java.util.Vector; + +class Matrix extends Vector> { Matrix mul_rec(Matrix m) { - v1; - v1 = new Menge(); - v2; - v2 = new Menge(); - i; - i = 0; + auto v1 = new Vector(); + auto v2 = new Vector(); + auto i = 0; while(i < m.size()) { - v; + auto v; v = m.elementAt(i); v2.addElement(v.remove(v.size()-1)); i++; @@ -21,14 +20,14 @@ class Matrix extends Menge> { ret = new Matrix(); i = 0; while (i < this.size()) { - ret.addElement(new Menge()); + ret.addElement(new Vector()); i++; } } i = 0; while (i < this.size()) { int erg = 0; - j; + auto j; j = 0; while (j < v2.size()) { erg = erg + this.elementAt(i).elementAt(j).intValue() diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 2d44c2bb..fb0bd1b8 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -33,15 +33,16 @@ public class JavaTXCompilerTest extends JavaTXCompiler { @Test public void test() throws IOException, java.lang.ClassNotFoundException { - filesToTest.add(new File(rootDirectory+"Faculty.jav")); - filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - filesToTest.add(new File(rootDirectory+"Lambda.jav")); - filesToTest.add(new File(rootDirectory+"Lambda2.jav")); - filesToTest.add(new File(rootDirectory+"Lambda3.jav")); - filesToTest.add(new File(rootDirectory+"Vector.jav")); - filesToTest.add(new File(rootDirectory+"Generics.jav")); - filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); - filesToTest.add(new File(rootDirectory+"Matrix.jav")); + //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); + //filesToTest.add(new File(rootDirectory+"Vector.jav")); + //filesToTest.add(new File(rootDirectory+"Generics.jav")); + //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); + //filesToTest.add(new File(rootDirectory+"Matrix.jav")); + filesToTest.add(new File(rootDirectory+"Import.jav")); for(File f : filesToTest){ SourceFile sf = this.parse(f); System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));