From 2c999d0eb6d242de1fdfa28812ab9f3561aaab32 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 15 May 2017 13:15:49 +0200 Subject: [PATCH] Umstellung auf Visitor Pattern des TYPE-Algos --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 12 +- .../syntaxtree/statement/Assign.java | 13 +- .../syntaxtree/statement/Binary.java | 5 - .../syntaxtree/statement/Block.java | 9 - .../syntaxtree/statement/CastExpr.java | 5 - .../syntaxtree/statement/EmptyStmt.java | 6 - .../syntaxtree/statement/Expression.java | 2 - .../syntaxtree/statement/FieldVar.java | 23 +- .../syntaxtree/statement/ForStmt.java | 6 - .../syntaxtree/statement/IfStmt.java | 5 - .../syntaxtree/statement/InstVar.java | 5 - .../syntaxtree/statement/InstanceOf.java | 5 - .../statement/LambdaExpression.java | 12 +- .../syntaxtree/statement/LocalVar.java | 6 - .../statement/LocalVarBunchDeclaration.java | 5 - .../syntaxtree/statement/LocalVarDecl.java | 5 - .../syntaxtree/statement/MethodCall.java | 74 +------ .../syntaxtree/statement/NewArray.java | 6 - .../syntaxtree/statement/NewClass.java | 38 ---- .../syntaxtree/statement/PostIncExpr.java | 4 - .../syntaxtree/statement/Receiver.java | 7 +- .../syntaxtree/statement/Return.java | 10 +- .../syntaxtree/statement/StaticClassName.java | 5 - .../syntaxtree/statement/Super.java | 5 - .../syntaxtree/statement/This.java | 7 - .../syntaxtree/statement/UnaryPlus.java | 5 - .../syntaxtree/statement/WhileStmt.java | 6 - .../typeinference/typeAlgo/TYPE.java | 201 +++++++++++++++--- test/javFiles/Lambda.jav | 8 +- test/typeinference/JavaTXCompilerTest.java | 6 +- 31 files changed, 198 insertions(+), 310 deletions(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 31271ffd..644865d6 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -50,7 +50,7 @@ public class JavaTXCompiler { System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); - System.out.println(result); + System.out.println("RESULT: " + result); results.addAll(result); } return new ResultSet(results); diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 7b9480e4..350d8b15 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; 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 org.antlr.v4.runtime.Token; import de.dhbwstuttgart.core.IItemWithOffset; @@ -36,14 +37,11 @@ public class Method extends Field implements IItemWithOffset, TypeScope } public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { - ConstraintSet ret = new ConstraintSet(); + if(block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this); - ret.addAll(block.getConstraints(blockInfo)); - /* - ret.addUndConstraint( - ConstraintsFactory.createPair(block.getType(), this.getType(), blockInfo)); - */ - return ret; + TYPE methodScope = new TYPE(blockInfo); + block.accept(methodScope); + return methodScope.getConstraints(); } public ParameterList getParameterList() { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 0e2f49f2..e5827e02 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -11,8 +11,8 @@ import org.antlr.v4.runtime.Token; public class Assign extends Statement { - private final Expression rightSide; - private final LocalVar lefSide; + public final Expression rightSide; + public final LocalVar lefSide; public Assign(LocalVar leftHandSide, Expression value, Token offset) { super(leftHandSide.getType(), offset); @@ -20,15 +20,6 @@ public class Assign extends Statement this.lefSide = leftHandSide; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = lefSide.getConstraints(info); - ret.addAll(rightSide.getConstraints(info)); - ret.addUndConstraint(ConstraintsFactory.createPair( - rightSide.getType(), lefSide.getType(), PairOperator.SMALLERDOT, info)); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index 766ce0c7..321509f9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -16,11 +16,6 @@ public class Binary extends Expression super(null, null); } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index 504b94b3..73a92c7e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -24,15 +24,6 @@ public class Block extends Statement return statements; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = new ConstraintSet(); - for(Statement stmt : getStatements()){ - ret.addAll(stmt.getConstraints(info)); - } - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java index 158cdf88..15fedbbc 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java @@ -18,11 +18,6 @@ public class CastExpr extends Expression public Expression expr; - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java index 4dced540..ff80e47f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java @@ -15,12 +15,6 @@ public class EmptyStmt extends Statement super(new Void(offset),offset); } - - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - return new ConstraintSet(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java b/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java index 23fb0e5d..07db361e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java @@ -22,7 +22,5 @@ public abstract class Expression extends SyntaxTreeNode return type; } - public abstract ConstraintSet getConstraints(TypeInferenceBlockInformation info); - public abstract void accept(StatementVisitor visitor); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java index b782f9a0..ba56fc5a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java @@ -16,8 +16,8 @@ import java.util.Set; public class FieldVar extends Expression { - private String fieldVarName; - public Expression receiver; + public final String fieldVarName; + public final Expression receiver; public FieldVar(Expression receiver, String fieldVarName, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(type, offset); @@ -25,25 +25,6 @@ public class FieldVar extends Expression { this.receiver = receiver; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = new ConstraintSet(); - Set oderConstraints = new HashSet<>(); - ret.addAll(receiver.getConstraints(info)); - for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){ - Constraint constraint = new Constraint(); - constraint.add(ConstraintsFactory.createPair( - receiver.getType(),fieldAssumption.getReceiverType(), info)); - constraint.add(ConstraintsFactory.createPair( - this.getType(),fieldAssumption.getType(), info)); - oderConstraints.add(constraint); - } - if(oderConstraints.size() == 0) - throw new TypeinferenceException("Kein Feld "+fieldVarName+ " gefunden", getOffset()); - ret.addOderConstraint(oderConstraints); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 703958dd..1f933366 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -22,12 +22,6 @@ public class ForStmt extends Statement super(null,null); } - - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 858ae298..090e954f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -18,11 +18,6 @@ public class IfStmt extends Statement public Block then_block; public Block else_block; - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - return null; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/InstVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/InstVar.java index 21270e7d..021b5039 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/InstVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/InstVar.java @@ -17,11 +17,6 @@ public class InstVar extends Expression expr = instanz; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index 2b816e81..154b5980 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -18,11 +18,6 @@ public class InstanceOf extends BinaryExpr // #JB# 20.04.2005 } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index cb663efe..85624693 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -13,8 +13,8 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException; import java.util.ArrayList; public class LambdaExpression extends Expression implements TypeScope { - private Block methodBody; - private ParameterList params; + public final Block methodBody; + public final ParameterList params; public LambdaExpression(RefTypeOrTPHOrWildcardOrGeneric type, ParameterList params, Block methodBody, Token offset) { super(type,offset); @@ -22,14 +22,6 @@ public class LambdaExpression extends Expression implements TypeScope { this.params = params; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - TypeInferenceBlockInformation lambdaScope = new TypeInferenceBlockInformation(info, this); - ConstraintSet ret = methodBody.getConstraints(lambdaScope); - throw new NotImplementedException(); - //return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index 23d34347..ea2b5bea 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -24,12 +24,6 @@ public class LocalVar extends Statement{ this.expression = access; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - //throw new NotImplementedException(); - return new ConstraintSet(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarBunchDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarBunchDeclaration.java index 54adbb5a..fa62cbfd 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarBunchDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarBunchDeclaration.java @@ -15,11 +15,6 @@ public class LocalVarBunchDeclaration extends Statement { super(new Void(start), start); } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java index f29e9976..7f84cf66 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java @@ -20,11 +20,6 @@ public class LocalVarDecl extends Statement this.name = name; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index 97fb6662..94b488af 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -20,9 +20,10 @@ import java.util.*; public class MethodCall extends Statement { - private final String name; - private Receiver receiver; - private ArgumentList arglist; + public final String name; + public final Receiver receiver; + public final ArgumentList arglist; + private ArgumentList argumentList; public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, Token offset){ super(retType,offset); @@ -31,77 +32,12 @@ public class MethodCall extends Statement this.receiver = receiver; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = receiver.getConstraints(info); - ret.addAll(this.getArgumentListConstraints(info)); - //Overloading: - Set methodConstraints = new HashSet<>(); - for(MethodAssumption m : this.getMethods(name, arglist, info)){ - methodConstraints.add(generateConstraint(m, info)); - } - if(methodConstraints.size()<1){ - throw new TypeinferenceException("Methode "+name+" ist nicht vorhanden!",getOffset()); - } - ret.addOderConstraint(methodConstraints); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); } - protected Constraint generateConstraint(MethodAssumption forMethod, TypeInferenceBlockInformation info){ - Constraint methodConstraint = new Constraint(); - methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), forMethod.getReceiverType(), PairOperator.SMALLERDOT, info)); - methodConstraint.add(ConstraintsFactory.createPair(forMethod.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info)); - methodConstraint.addAll(generateParameterConstraints(forMethod, info)); - return methodConstraint; - } - - protected Set generateParameterConstraints(MethodAssumption forMethod, TypeInferenceBlockInformation info) { - Set ret = new HashSet<>(); - for(int i = 0;i getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { - List ret = new ArrayList<>(); - for(ClassOrInterface cl : info.getAvailableClasses()){ - for(Method m : cl.getMethods()){ - if(m.getName().equals(name) && - m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ - RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); - - ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); - } - } - } - return ret; - } - - 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 ArgumentList getArgumentList() { - return arglist; - } - - public ConstraintSet getArgumentListConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = new ConstraintSet(); - for(int i = 0;i expr; - - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index f5ed5e5c..911fd056 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -34,44 +34,6 @@ public class NewClass extends MethodCall super(newClass, new Receiver(new EmptyStmt(start)), "new "+newClass.getName().toString(), args, start); } - public List 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; - } - - @Override - protected Constraint generateConstraint(MethodAssumption forMethod, TypeInferenceBlockInformation info){ - Constraint methodConstraint = new Constraint(); - methodConstraint.add(ConstraintsFactory.createPair(forMethod.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info)); - methodConstraint.addAll(generateParameterConstraints(forMethod, info)); - return methodConstraint; - } - - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = new ConstraintSet(); - ret.addAll(this.getArgumentListConstraints(info)); - //Overloading: - Set methodConstraints = new HashSet<>(); - for(MethodAssumption m : this.getConstructors(info, (RefType) this.getType(), this.getArgumentList())){ - methodConstraints.add(generateConstraint(m, info)); - } - if(methodConstraints.size()<1){ - throw new TypeinferenceException("Konstruktor in Klasse "+this.getType().toString()+" ist nicht vorhanden!",getOffset()); - } - ret.addOderConstraint(methodConstraints); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java index 350c748f..1292192b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java @@ -12,8 +12,4 @@ public class PostIncExpr extends UnaryExpr super(null); } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java b/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java index ac6c043d..09bc191c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Receiver.java @@ -8,7 +8,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; public class Receiver extends Expression { - private Expression expr; + public Expression expr; /** * Autor: J�rg B�uerle * @param expr @@ -19,11 +19,6 @@ public class Receiver extends Expression this.expr = expr; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - return expr.getConstraints(info); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index 59cbf0ab..434f1ce9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -9,7 +9,7 @@ import org.antlr.v4.runtime.Token; public class Return extends Statement { - private Expression retexpr; + public final Expression retexpr; public Return(Expression retExpr, Token offset) { @@ -17,14 +17,6 @@ public class Return extends Statement this.retexpr = retExpr; } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = retexpr.getConstraints(info); - ret.addUndConstraint(ConstraintsFactory.createPair( - this.getType(),info.getCurrentTypeScope().getReturnType(), info)); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java index 883e89b2..536b6d41 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java @@ -13,11 +13,6 @@ public class StaticClassName extends Expression { super(new RefType(className, offset), offset); } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - return new ConstraintSet(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Super.java b/src/de/dhbwstuttgart/syntaxtree/statement/Super.java index 5f9e40cb..fbeba99b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Super.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Super.java @@ -14,11 +14,6 @@ public class Super extends Expression super(null,null); } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/This.java b/src/de/dhbwstuttgart/syntaxtree/statement/This.java index c4ad1cf3..f3084777 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/This.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/This.java @@ -23,13 +23,6 @@ public class This extends Expression public ArgumentList arglist; - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - ConstraintSet ret = new ConstraintSet(); - ret.addUndConstraint(ConstraintsFactory.createPair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info)); - return ret; - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java index db4b9aec..b374166f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java @@ -13,11 +13,6 @@ public class UnaryPlus extends Expression } - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java index 9edc43f0..351133e6 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java @@ -26,12 +26,6 @@ public class WhileStmt extends Statement return "WHILE " + loop_block.toString(); } - - @Override - public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { - throw new NotImplementedException(); - } - @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index f75d6222..59d89e0d 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,144 +1,287 @@ package de.dhbwstuttgart.typeinference.typeAlgo; -import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.exceptions.TypeinferenceException; +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.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +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; public class TYPE implements StatementVisitor{ + private final TypeInferenceBlockInformation info; + private final ConstraintSet constraintsSet = new ConstraintSet(); + + public TYPE(TypeInferenceBlockInformation info){ + this.info = info; + } + + public ConstraintSet getConstraints() { + return constraintsSet; + } + @Override public void visit(LambdaExpression lambdaExpression) { - + 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(InstVar instVar) { - + 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) { - + throw new NotImplementedException(); } @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 aReturn) { - + public void visit(Return returnExpr) { + returnExpr.retexpr.accept(this); + constraintsSet.addUndConstraint(ConstraintsFactory.createPair( + returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), 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(LocalVarBunchDeclaration localVarBunchDeclaration) { - + throw new NotImplementedException(); } @Override public void visit(Null aNull) { - + throw new NotImplementedException(); } @Override public void visit(Literal literal) { - + throw new NotImplementedException(); } + + /* + 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.SMALLERDOT, 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, ArgumentList arglist, TypeInferenceBlockInformation info) { + List ret = new ArrayList<>(); + for(ClassOrInterface cl : info.getAvailableClasses()){ + for(Method m : cl.getMethods()){ + if(m.getName().equals(name) && + m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ + RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); + + ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); + } + } + } + return ret; + } + + 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))); + } + } + } + } + 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/Lambda.jav b/test/javFiles/Lambda.jav index 386ae7f7..998dbe3b 100644 --- a/test/javFiles/Lambda.jav +++ b/test/javFiles/Lambda.jav @@ -3,10 +3,10 @@ class Lambda{ String var; methode(){ -return () -> (f) -> { - f.apply(this, var); - return var; -}; + return () -> (f) -> { + f.apply(this, var); + return var; + }; } } diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 4df1c29d..2f1f7293 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -17,10 +17,10 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(); - //compiler.parse(new File(rootDirectory+"Methods.jav")); + compiler.parse(new File(rootDirectory+"Methods.jav")); //compiler.parse(new File(rootDirectory+"Generics.jav")); - //compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); - compiler.parse(new File(rootDirectory+"Lambda.jav")); + compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); + //compiler.parse(new File(rootDirectory+"Lambda.jav")); compiler.typeInference(); } } \ No newline at end of file