Umstellung auf Visitor Pattern des TYPE-Algos
This commit is contained in:
parent
058402e056
commit
2c999d0eb6
@ -50,7 +50,7 @@ public class JavaTXCompiler {
|
||||
|
||||
System.out.println(xConsSet);
|
||||
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
||||
System.out.println(result);
|
||||
System.out.println("RESULT: " + result);
|
||||
results.addAll(result);
|
||||
}
|
||||
return new ResultSet(results);
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -22,7 +22,5 @@ public abstract class Expression extends SyntaxTreeNode
|
||||
return type;
|
||||
}
|
||||
|
||||
public abstract ConstraintSet getConstraints(TypeInferenceBlockInformation info);
|
||||
|
||||
public abstract void accept(StatementVisitor visitor);
|
||||
}
|
||||
|
@ -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<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
|
||||
public void accept(StatementVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<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
|
||||
public void accept(StatementVisitor visitor) {
|
||||
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() {
|
||||
return arglist;
|
||||
}
|
||||
|
||||
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;
|
||||
return argumentList;
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,6 @@ public class NewArray extends Expression
|
||||
private RefTypeOrTPHOrWildcardOrGeneric type;
|
||||
public List<Expression> expr;
|
||||
|
||||
|
||||
@Override
|
||||
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(StatementVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
|
@ -34,44 +34,6 @@ public class NewClass extends MethodCall
|
||||
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
|
||||
public void accept(StatementVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
|
@ -12,8 +12,4 @@ public class PostIncExpr extends UnaryExpr
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<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
|
||||
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<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
|
||||
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
|
||||
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<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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ class Lambda{
|
||||
String var;
|
||||
|
||||
methode(){
|
||||
return () -> (f) -> {
|
||||
return () -> (f) -> {
|
||||
f.apply(this, var);
|
||||
return var;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user