Umstellung auf Visitor Pattern des TYPE-Algos

This commit is contained in:
JanUlrich 2017-05-15 13:15:49 +02:00
parent 058402e056
commit 2c999d0eb6
31 changed files with 198 additions and 310 deletions

View File

@ -50,7 +50,7 @@ public class JavaTXCompiler {
System.out.println(xConsSet); System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure); Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result); System.out.println("RESULT: " + result);
results.addAll(result); results.addAll(result);
} }
return new ResultSet(results); return new ResultSet(results);

View File

@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.typeAlgo.TYPE;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.core.IItemWithOffset;
@ -36,14 +37,11 @@ public class Method extends Field implements IItemWithOffset, TypeScope
} }
public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { 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); TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this);
ret.addAll(block.getConstraints(blockInfo)); TYPE methodScope = new TYPE(blockInfo);
/* block.accept(methodScope);
ret.addUndConstraint( return methodScope.getConstraints();
ConstraintsFactory.createPair(block.getType(), this.getType(), blockInfo));
*/
return ret;
} }
public ParameterList getParameterList() { public ParameterList getParameterList() {

View File

@ -11,8 +11,8 @@ import org.antlr.v4.runtime.Token;
public class Assign extends Statement public class Assign extends Statement
{ {
private final Expression rightSide; public final Expression rightSide;
private final LocalVar lefSide; public final LocalVar lefSide;
public Assign(LocalVar leftHandSide, Expression value, Token offset) { public Assign(LocalVar leftHandSide, Expression value, Token offset) {
super(leftHandSide.getType(), offset); super(leftHandSide.getType(), offset);
@ -20,15 +20,6 @@ public class Assign extends Statement
this.lefSide = leftHandSide; 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -16,11 +16,6 @@ public class Binary extends Expression
super(null, null); super(null, null);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -24,15 +24,6 @@ public class Block extends Statement
return statements; return statements;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
for(Statement stmt : getStatements()){
ret.addAll(stmt.getConstraints(info));
}
return ret;
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -18,11 +18,6 @@ public class CastExpr extends Expression
public Expression expr; public Expression expr;
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -15,12 +15,6 @@ public class EmptyStmt extends Statement
super(new Void(offset),offset); super(new Void(offset),offset);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return new ConstraintSet();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -22,7 +22,5 @@ public abstract class Expression extends SyntaxTreeNode
return type; return type;
} }
public abstract ConstraintSet getConstraints(TypeInferenceBlockInformation info);
public abstract void accept(StatementVisitor visitor); public abstract void accept(StatementVisitor visitor);
} }

View File

@ -16,8 +16,8 @@ import java.util.Set;
public class FieldVar extends Expression { public class FieldVar extends Expression {
private String fieldVarName; public final String fieldVarName;
public Expression receiver; public final Expression receiver;
public FieldVar(Expression receiver, String fieldVarName, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { public FieldVar(Expression receiver, String fieldVarName, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset); super(type, offset);
@ -25,25 +25,6 @@ public class FieldVar extends Expression {
this.receiver = receiver; this.receiver = receiver;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
Set<Constraint> 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -22,12 +22,6 @@ public class ForStmt extends Statement
super(null,null); super(null,null);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -18,11 +18,6 @@ public class IfStmt extends Statement
public Block then_block; public Block then_block;
public Block else_block; public Block else_block;
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return null;
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -17,11 +17,6 @@ public class InstVar extends Expression
expr = instanz; expr = instanz;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -18,11 +18,6 @@ public class InstanceOf extends BinaryExpr
// #JB# 20.04.2005 // #JB# 20.04.2005
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -13,8 +13,8 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.ArrayList; import java.util.ArrayList;
public class LambdaExpression extends Expression implements TypeScope { public class LambdaExpression extends Expression implements TypeScope {
private Block methodBody; public final Block methodBody;
private ParameterList params; public final ParameterList params;
public LambdaExpression(RefTypeOrTPHOrWildcardOrGeneric type, ParameterList params, Block methodBody, Token offset) { public LambdaExpression(RefTypeOrTPHOrWildcardOrGeneric type, ParameterList params, Block methodBody, Token offset) {
super(type,offset); super(type,offset);
@ -22,14 +22,6 @@ public class LambdaExpression extends Expression implements TypeScope {
this.params = params; 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -24,12 +24,6 @@ public class LocalVar extends Statement{
this.expression = access; this.expression = access;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
//throw new NotImplementedException();
return new ConstraintSet();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -15,11 +15,6 @@ public class LocalVarBunchDeclaration extends Statement {
super(new Void(start), start); super(new Void(start), start);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -20,11 +20,6 @@ public class LocalVarDecl extends Statement
this.name = name; this.name = name;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -20,9 +20,10 @@ import java.util.*;
public class MethodCall extends Statement public class MethodCall extends Statement
{ {
private final String name; public final String name;
private Receiver receiver; public final Receiver receiver;
private ArgumentList arglist; public final ArgumentList arglist;
private ArgumentList argumentList;
public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, Token offset){ public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
super(retType,offset); super(retType,offset);
@ -31,77 +32,12 @@ public class MethodCall extends Statement
this.receiver = receiver; this.receiver = receiver;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = receiver.getConstraints(info);
ret.addAll(this.getArgumentListConstraints(info));
//Overloading:
Set<Constraint> 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);
} }
protected Constraint<Pair> 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<Pair> generateParameterConstraints(MethodAssumption forMethod, TypeInferenceBlockInformation info) {
Set<Pair> ret = new HashSet<>();
for(int i = 0;i<arglist.getArguments().size();i++){
ret.add(ConstraintsFactory.createPair(forMethod.getArgTypes().get(i),
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
return ret;
}
public static List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
List<MethodAssumption> 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<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(FormalParameter fp : parameterList.getFormalparalist()){
params.add(info.checkGTV(fp.getType()));
}
return params;
}
public ArgumentList getArgumentList() { public ArgumentList getArgumentList() {
return arglist; return argumentList;
}
public ConstraintSet getArgumentListConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
for(int i = 0;i<arglist.getArguments().size();i++){
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
}
return ret;
} }
} }

View File

@ -17,12 +17,6 @@ public class NewArray extends Expression
private RefTypeOrTPHOrWildcardOrGeneric type; private RefTypeOrTPHOrWildcardOrGeneric type;
public List<Expression> expr; public List<Expression> expr;
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -34,44 +34,6 @@ public class NewClass extends MethodCall
super(newClass, new Receiver(new EmptyStmt(start)), "new "+newClass.getName().toString(), args, start); super(newClass, new Receiver(new EmptyStmt(start)), "new "+newClass.getName().toString(), args, start);
} }
public List<MethodAssumption> getConstructors(TypeInferenceBlockInformation info, RefType ofType, ArgumentList argList){
List<MethodAssumption> 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<Pair> 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<Constraint> 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -12,8 +12,4 @@ public class PostIncExpr extends UnaryExpr
super(null); super(null);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
} }

View File

@ -8,7 +8,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
public class Receiver extends Expression public class Receiver extends Expression
{ {
private Expression expr; public Expression expr;
/** /**
* Autor: ¯Â¿Â½rg ¯Â¿Â½uerle * Autor: ¯Â¿Â½rg ¯Â¿Â½uerle
* @param expr * @param expr
@ -19,11 +19,6 @@ public class Receiver extends Expression
this.expr = expr; this.expr = expr;
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return expr.getConstraints(info);
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -9,7 +9,7 @@ import org.antlr.v4.runtime.Token;
public class Return extends Statement public class Return extends Statement
{ {
private Expression retexpr; public final Expression retexpr;
public Return(Expression retExpr, Token offset) public Return(Expression retExpr, Token offset)
{ {
@ -17,14 +17,6 @@ public class Return extends Statement
this.retexpr = retExpr; 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -13,11 +13,6 @@ public class StaticClassName extends Expression {
super(new RefType(className, offset), offset); super(new RefType(className, offset), offset);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return new ConstraintSet();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -14,11 +14,6 @@ public class Super extends Expression
super(null,null); super(null,null);
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -23,13 +23,6 @@ public class This extends Expression
public ArgumentList arglist; 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 @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -13,11 +13,6 @@ public class UnaryPlus extends Expression
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -26,12 +26,6 @@ public class WhileStmt extends Statement
return "WHILE " + loop_block.toString(); return "WHILE " + loop_block.toString();
} }
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
throw new NotImplementedException();
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -1,144 +1,287 @@
package de.dhbwstuttgart.typeinference.typeAlgo; 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.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null; 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{ 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 @Override
public void visit(LambdaExpression lambdaExpression) { public void visit(LambdaExpression lambdaExpression) {
TYPE lambdaScope = new TYPE(new TypeInferenceBlockInformation(info, lambdaExpression));
lambdaExpression.methodBody.accept(lambdaScope);
constraintsSet.addAll(lambdaScope.getConstraints());
} }
@Override @Override
public void visit(Assign assign) { 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 @Override
public void visit(Binary binary) { public void visit(Binary binary) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(Block block) { public void visit(Block block) {
for(Statement stmt : block.getStatements()){
stmt.accept(this);
}
} }
@Override @Override
public void visit(CastExpr castExpr) { public void visit(CastExpr castExpr) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(EmptyStmt emptyStmt) { public void visit(EmptyStmt emptyStmt) {
//Nothing :)
} }
@Override @Override
public void visit(FieldVar fieldVar) { public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
Set<Constraint> 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 @Override
public void visit(ForStmt forStmt) { public void visit(ForStmt forStmt) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(IfStmt ifStmt) { public void visit(IfStmt ifStmt) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(InstanceOf instanceOf) { public void visit(InstanceOf instanceOf) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(InstVar instVar) { public void visit(InstVar instVar) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(LocalVar localVar) { public void visit(LocalVar localVar) {
// Es werden nur bei Feldvariablen Constraints generiert. Lokale Variablen sind eindeutig
} }
@Override @Override
public void visit(LocalVarDecl localVarDecl) { public void visit(LocalVarDecl localVarDecl) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(MethodCall methodCall) { public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this);
constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info));
//Overloading:
Set<Constraint> 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 @Override
public void visit(NewClass methodCall) { public void visit(NewClass methodCall) {
constraintsSet.addAll(this.getArgumentListConstraints(methodCall, info));
//Overloading:
Set<Constraint> 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 @Override
public void visit(NewArray newArray) { public void visit(NewArray newArray) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(Receiver receiver) { public void visit(Receiver receiver) {
receiver.expr.accept(this);
} }
@Override @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 @Override
public void visit(ReturnVoid aReturn) { public void visit(ReturnVoid aReturn) {
visit((Return) aReturn);
} }
@Override @Override
public void visit(StaticClassName staticClassName) { public void visit(StaticClassName staticClassName) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(Super aSuper) { public void visit(Super aSuper) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(This aThis) { public void visit(This aThis) {
constraintsSet.addUndConstraint(ConstraintsFactory.createPair( aThis.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info));
} }
@Override @Override
public void visit(UnaryPlus unaryPlus) { public void visit(UnaryPlus unaryPlus) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(WhileStmt whileStmt) { public void visit(WhileStmt whileStmt) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(LocalVarBunchDeclaration localVarBunchDeclaration) { public void visit(LocalVarBunchDeclaration localVarBunchDeclaration) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(Null aNull) { public void visit(Null aNull) {
throw new NotImplementedException();
} }
@Override @Override
public void visit(Literal literal) { public void visit(Literal literal) {
throw new NotImplementedException();
} }
/*
METHOD CALL Section:
*/
protected Constraint<Pair> 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<Pair> generateParameterConstraints(MethodCall foMethod, MethodAssumption assumption, TypeInferenceBlockInformation info) {
Set<Pair> ret = new HashSet<>();
for(int i = 0;i<foMethod.arglist.getArguments().size();i++){
ret.add(ConstraintsFactory.createPair(assumption.getArgTypes().get(i),
foMethod.arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
return ret;
}
public static List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
List<MethodAssumption> 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<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){
List<RefTypeOrTPHOrWildcardOrGeneric> 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<forMethod.arglist.getArguments().size();i++){
forMethod.arglist.getArguments().get(i).accept(ret);
}
return ret.constraintsSet;
}
public List<MethodAssumption> getConstructors(TypeInferenceBlockInformation info, RefType ofType, ArgumentList argList){
List<MethodAssumption> 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<Pair> 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;
}
} }

View File

@ -3,10 +3,10 @@ class Lambda{
String var; String var;
methode(){ methode(){
return () -> (f) -> { return () -> (f) -> {
f.apply(this, var); f.apply(this, var);
return var; return var;
}; };
} }
} }

View File

@ -17,10 +17,10 @@ public class JavaTXCompilerTest {
@Test @Test
public void test() throws IOException, ClassNotFoundException { public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(); 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+"Generics.jav"));
//compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
compiler.parse(new File(rootDirectory+"Lambda.jav")); //compiler.parse(new File(rootDirectory+"Lambda.jav"));
compiler.typeInference(); compiler.typeInference();
} }
} }