Neuen Unify Algorithmus eingebaut

This commit is contained in:
JanUlrich 2016-04-01 16:12:30 +02:00
parent 4deb91b99f
commit 4cdcde8fef
40 changed files with 269 additions and 177 deletions

View File

@ -1,7 +1,6 @@
eclipse.preferences.version=1
encoding//src/de/dhbwstuttgart/core/MyCompiler.java=UTF-8
encoding//src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java=UTF-8
encoding//src/de/dhbwstuttgart/typeinference/SingleConstraint.java=UTF-8
encoding//src/de/dhbwstuttgart/typeinference/UndConstraint.java=UTF-8
encoding//src/de/dhbwstuttgart/typeinference/UnifySingleConstraint.java=UTF-8
encoding//src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java=UTF-8

View File

@ -28,7 +28,6 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;

View File

@ -21,8 +21,8 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
@ -148,13 +148,13 @@ public class FieldDeclaration extends Field{
}
*/
SingleConstraint c1 = new SingleConstraint(thisType, thisType);
UndConstraint c1 = ConstraintsSet.createSingleConstraint(thisType, thisType);
ret.add(c1); //Damit die TypVariable des Felds in den Constraints auftaucht
if(this.wert!=null){
//Falls bei der Deklaration ein Wert zugewiesen wird, verhält sich das Constraintserzeugen wie bei dem Assign-Statement:
ret.add(this.wert.TYPEExpr(localAssumptions));
ret.add(new SingleConstraint(this.wert.getType().TYPE(localAssumptions,this), thisType));
ret.add(ConstraintsSet.createSingleConstraint(this.wert.getType().TYPE(localAssumptions,this), thisType));
}
return ret;
}

View File

@ -46,9 +46,9 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.assumptions.ParameterAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
@ -499,7 +499,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
ret.add(this.block.TYPEStmt(localAss));
// eine Verknüpfung mit der Type Assumption aus dem Assumption Set
// und dem ermittelten Typ der Methode:
ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this)));
ret.add(ConstraintsSet.createSingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this)));
return ret;
}

View File

@ -0,0 +1,26 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.typeinference.Menge;
/**
* Dieser SyntaxTreeNode kann anstelle von null in einem Syntaxbaum eingesetzt werden.
* Vorsicht: Als Offset wird dann immer 0 zurück gegeben.
*/
public class NullSyntaxTreeNode extends SyntaxTreeNode {
@Override
public int getOffset() {
return 0;
}
@Override
public int getVariableLength() {
return 0;
}
@Override
public Menge<? extends SyntaxTreeNode> getChildren() {
return new Menge<>();
}
}

View File

@ -55,7 +55,7 @@ public class ASTFactory {
return new Constructor(method, superClass);
}
public static Class createClass(String className, Type type, Modifiers modifiers, Menge supertypeGenPara, SourceFile parent) {
public static Class createClass(String className, RefType type, Modifiers modifiers, Menge supertypeGenPara, SourceFile parent) {
// TODO bytecode createClass
//String name, RefType superClass, Modifiers modifiers, Menge<String> supertypeGenPara
Class generatedClass = new Class(className, type, modifiers, supertypeGenPara);

View File

@ -3,7 +3,7 @@ package de.dhbwstuttgart.syntaxtree.factory;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
public class UnifyPairMengenBuilder {

View File

@ -4,6 +4,7 @@ import java.util.HashSet;
import java.util.logging.Logger;
import de.dhbwstuttgart.myexception.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.NullSyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
@ -36,8 +37,11 @@ import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
public class UnifyTypeFactory {
private final static NullSyntaxTreeNode NULL_NODE = new NullSyntaxTreeNode();
public static FiniteClosure generateFC(TypeAssumptions fromAss){
HashSet<MPair> pairs = new HashSet<>();
for(ClassAssumption cAss : fromAss.getClassAssumptions()){
@ -63,6 +67,10 @@ public class UnifyTypeFactory {
return UnifyTypeFactory.convert((RefType)t);
}else if(t instanceof TypePlaceholder){
return UnifyTypeFactory.convert((TypePlaceholder)t);
}else if(t instanceof ExtendsWildcardType){
return UnifyTypeFactory.convert((ExtendsWildcardType)t);
}else if(t instanceof SuperWildcardType){
return UnifyTypeFactory.convert((SuperWildcardType)t);
}
throw new NotImplementedException("Der Typ "+t+" kann nicht umgewandelt werden");
}
@ -105,7 +113,7 @@ public class UnifyTypeFactory {
return ret;
}
private static UnifyOderConstraint convert(OderConstraint set) {
public static UnifyOderConstraint convert(OderConstraint set) {
UnifyOderConstraint ret = new UnifyOderConstraint();
for(UndConstraint oC : set.getUndConstraints()){
ret.addConstraint(UnifyTypeFactory.convert(oC));
@ -113,7 +121,7 @@ public class UnifyTypeFactory {
return ret;
}
private static UnifyUndConstraint convert(UndConstraint set) {
public static UnifyUndConstraint convert(UndConstraint set) {
UnifyUndConstraint ret = new UnifyUndConstraint();
for(EinzelElement<Pair> oC : set.getPairs()){
ret.add(UnifyTypeFactory.convert(oC));
@ -121,10 +129,14 @@ public class UnifyTypeFactory {
return ret;
}
private static MPair convert(EinzelElement<Pair> p) {
if(!p.getItem().OperatorSmaller())throw new NotImplementedException();
MPair ret = smaller(UnifyTypeFactory.convert(p.getItem().TA1)
, UnifyTypeFactory.convert(p.getItem().TA2));
public static MPair convert(EinzelElement<Pair> p) {
return convert(p.getItem());
}
public static MPair convert(Pair p) {
if(!p.OperatorSmaller())throw new NotImplementedException();
MPair ret = smaller(UnifyTypeFactory.convert(p.TA1)
, UnifyTypeFactory.convert(p.TA2));
return ret;
}
@ -139,12 +151,12 @@ public class UnifyTypeFactory {
}
public static Type convert(SuperType t) {
RefType innerType = new RefType(t.getSuperedType().getName(),null,0);
RefType innerType = new RefType(t.getSuperedType().getName(),NULL_NODE,0);
return new SuperWildcardType(innerType);
}
public static Type convert(ExtendsType t) {
RefType innerType = new RefType(t.getExtendedType().getName(),null,0);
RefType innerType = new RefType(t.getExtendedType().getName(),NULL_NODE,0);
return new ExtendsWildcardType(innerType);
}
@ -153,6 +165,10 @@ public class UnifyTypeFactory {
}
public static Type convert(UnifyType t) {
if(t instanceof SimpleType)return convert((SimpleType) t);
if(t instanceof SuperType)return convert((SuperType) t);
if(t instanceof ExtendsType)return convert((ExtendsType) t);
if(t instanceof PlaceholderType)return convert((PlaceholderType) t);
throw new NotImplementedException("Der Typ "+t+" kann nicht umgewandelt werden");
}
}

View File

@ -2,7 +2,6 @@ package de.dhbwstuttgart.syntaxtree.factory;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.unify.FC_TTO;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;

View File

@ -15,7 +15,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.typeinference.unify.Unify;

View File

@ -7,7 +7,6 @@ import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.statement.Expr;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
// ino.class.AndOp.24101.declaration

View File

@ -18,7 +18,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.typeinference.unify.Unify;

View File

@ -19,54 +19,32 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.unify.Unify;
// ino.class.Operator.24257.declaration
public abstract class Operator extends SyntaxTreeNode
// ino.end
// ino.class.Operator.24257.body
{
// ino.attribute.offset.24261.declaration
private int offset;
// ino.end
// ino.attribute.variableLength.24264.declaration
private int variableLength;
// ino.end
// ino.method.Operator.24267.definition
public Operator(int offset,int variableLength)
// ino.end
// ino.method.Operator.24267.body
{
this.offset=offset;
this.variableLength=variableLength;
}
// ino.end
// ino.method.getOffset.24270.definition
public int getOffset()
// ino.end
// ino.method.getOffset.24270.body
{
return offset;
}
// ino.end
// ino.method.getVariableLength.24273.definition
public int getVariableLength()
// ino.end
// ino.method.getVariableLength.24273.body
{
return variableLength;
}
// ino.end
/**

View File

@ -33,7 +33,6 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.unify.Unify;
@ -107,8 +106,8 @@ public class Assign extends Expr
ret.add(expr2.TYPEExpr(assumptions));
//this.setTypeVariable( TypePlaceholder.fresh(this));
this.setType(TypePlaceholder.fresh(this));
ret.add(new SingleConstraint(expr2.getType().TYPE(assumptions, this), expr1.getType().TYPE(assumptions, this))); //expr2.type <. expr1.type
ret.add(new SingleConstraint(expr1.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
ret.add(ConstraintsSet.createSingleConstraint(expr2.getType().TYPE(assumptions, this), expr1.getType().TYPE(assumptions, this))); //expr2.type <. expr1.type
ret.add(ConstraintsSet.createSingleConstraint(expr1.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
return ret;
}

View File

@ -38,7 +38,6 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;

View File

@ -32,7 +32,6 @@ import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
@ -187,8 +186,8 @@ public class Block extends Statement
}
else {
TypePlaceholder tph = TypePlaceholder.fresh(this);
ret.add(new SingleConstraint(this.getType().TYPE(assumptions, this), tph));
ret.add(new SingleConstraint(stmt.getType().TYPE(assumptions, this), tph));
ret.add(ConstraintsSet.createSingleConstraint(this.getType().TYPE(assumptions, this), tph));
ret.add(ConstraintsSet.createSingleConstraint(stmt.getType().TYPE(assumptions, this), tph));
this.setType(tph);
}
}

View File

@ -33,7 +33,6 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;

View File

@ -44,7 +44,6 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
@ -141,10 +140,10 @@ public class IfStmt extends Statement
ret.add(this.then_block.TYPEStmt(assumptions));
if(else_block!=null){
ret.add(this.else_block.TYPEStmt(assumptions));
if(!(else_block.getType() instanceof Void))ret.add(new SingleConstraint(else_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
if(!(else_block.getType() instanceof Void))ret.add(ConstraintsSet.createSingleConstraint(else_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
}
ret.add(new SingleConstraint(expr.getType().TYPE(assumptions, this),new RefType("Boolean",this,0).TYPE(assumptions, this))); //(expressionDesIfStmt)<.boolean
if(!(then_block.getType() instanceof Void))ret.add(new SingleConstraint(then_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
ret.add(ConstraintsSet.createSingleConstraint(expr.getType().TYPE(assumptions, this),new RefType("Boolean",this,0).TYPE(assumptions, this))); //(expressionDesIfStmt)<.boolean
if(!(then_block.getType() instanceof Void))ret.add(ConstraintsSet.createSingleConstraint(then_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
if(then_block.getType() instanceof Void &&
(else_block == null || else_block.getType() instanceof Void))this.setType(new Void(this,this.getOffset()));
return ret;

View File

@ -41,7 +41,6 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.Typeable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.ParameterAssumption;
@ -198,7 +197,7 @@ public class LambdaExpression extends Expr{
}else{
this.lambdaType = new FunN(retType, modifiedParamTypes);
}
ret.add(new SingleConstraint(lambdaType.TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
ret.add(ConstraintsSet.createSingleConstraint(lambdaType.TYPE(assumptions, this),this.getType().TYPE(assumptions, this)));
return ret;
}

View File

@ -28,7 +28,6 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;

View File

@ -27,7 +27,6 @@ import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.LocalVarAssumption;
@ -371,7 +370,7 @@ public class LocalVarDecl extends Statement implements TypeInsertable
}
assumptions.addAssumption(new LocalVarAssumption(this, this.getType())); //Bevor der Typ auf Void gesetzt wird.
//if(this.getType() == null)throw new DebugException("Parser Post Processing nicht aufgerufen");
ret.add(new SingleConstraint(this.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
ret.add(ConstraintsSet.createSingleConstraint(this.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
//assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden.
this.setReturnType(new Void(this,0)); //Return typ einer Variablendeklaration ist Void
return ret;

View File

@ -34,7 +34,6 @@ import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;

View File

@ -24,7 +24,6 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
@ -95,7 +94,7 @@ public class Return extends Statement
ret.add(this.retexpr.TYPEExpr(assumptions));
//this.setTypeVariable(TypePlaceholder.fresh("Return Type"));
this.setType(TypePlaceholder.fresh(this));
ret.add(new SingleConstraint(retexpr.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
ret.add(ConstraintsSet.createSingleConstraint(retexpr.getType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this)));
return ret;
}

View File

@ -28,8 +28,8 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.unify.Unify;
@ -112,7 +112,7 @@ public class WhileStmt extends Statement
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
ret.add(expr.TYPEExpr(assumptions));
SingleConstraint exprMustBeBool = new SingleConstraint(expr.getType().TYPE(assumptions, this), new RefType("Boolean",this, 0).TYPE(assumptions, this)); // while(expr){}; expr <. boolean
UndConstraint exprMustBeBool = ConstraintsSet.createSingleConstraint(expr.getType().TYPE(assumptions, this), new RefType("Boolean",this, 0).TYPE(assumptions, this)); // while(expr){}; expr <. boolean
ret.add(exprMustBeBool);
ret.add(this.loop_block.TYPEStmt(assumptions));
this.setType(loop_block.getType());

View File

@ -9,7 +9,6 @@ import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;

View File

@ -163,4 +163,10 @@ public class ExtendsWildcardType extends WildcardType implements ITypeContainer,
return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "+");
}
*/
@Override
public String get_Name() {
return "? extends "+this.innerType.get_Name();
}
}

View File

@ -11,7 +11,6 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.parser.JavaClassName;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -18,7 +19,7 @@ import de.dhbwstuttgart.typeinference.exceptions.DebugException;
*/
public class SuperWildcardType extends WildcardType implements ITypeContainer, IMatchable{
public SuperWildcardType(ObjectType innerType){
this(innerType.getOffset(), innerType);
}
@ -168,5 +169,9 @@ public class SuperWildcardType extends WildcardType implements ITypeContainer, I
return "-" + this.innerType.getBytecodeSignature(cg, rs);
}
@Override
public String get_Name() {
return "? super "+this.innerType.get_Name();
}
}

View File

@ -43,7 +43,7 @@ public class TypePlaceholder extends ObjectType
private static String strNextName = "A";
// ino.end
// ino.attribute.m_TypePlaceholdersRegistry.26788.declaration
private static Hashtable<JavaClassName, TypePlaceholder> m_TypePlaceholdersRegistry = new Hashtable<JavaClassName, TypePlaceholder>();
private static Hashtable<String, TypePlaceholder> m_TypePlaceholdersRegistry = new Hashtable<String, TypePlaceholder>();
// ino.end
private SyntaxTreeNode parent;
@ -102,10 +102,10 @@ public class TypePlaceholder extends ObjectType
// ino.method.fresh.26800.body
{
TypePlaceholder typeVar = new TypePlaceholder(name, parent);
TypePlaceholder oldTPH = m_TypePlaceholdersRegistry.put(typeVar.getName(), typeVar);
TypePlaceholder oldTPH = m_TypePlaceholdersRegistry.put(typeVar.getName().toString(), typeVar);
if(oldTPH != null){
oldTPH.name = new JavaClassName(makeNewName());
m_TypePlaceholdersRegistry.put(oldTPH.getName(), oldTPH);
m_TypePlaceholdersRegistry.put(oldTPH.getName().toString(), oldTPH);
}
return typeVar;
}
@ -121,7 +121,7 @@ public class TypePlaceholder extends ObjectType
*/
public static TypePlaceholder fresh(SyntaxTreeNode parent){
TypePlaceholder ret= new TypePlaceholder(makeNewName(), parent);
m_TypePlaceholdersRegistry.put(ret.getName(), ret);
m_TypePlaceholdersRegistry.put(ret.getName().toString(), ret);
return ret;
}
@ -260,7 +260,7 @@ public class TypePlaceholder extends ObjectType
// ino.method.deleteRegistry.26839.body
{
m_TypePlaceholdersRegistry.clear();
m_TypePlaceholdersRegistry = new Hashtable<JavaClassName, TypePlaceholder>();
m_TypePlaceholdersRegistry = new Hashtable<String, TypePlaceholder>();
}
// ino.end
@ -328,7 +328,7 @@ public class TypePlaceholder extends ObjectType
//auf den CSubstitution nicht registrierte Variablen zu
//Exceptions fuehrt
TypePlaceholder typeVar = new TypePlaceholder(makeNewName(), null);
m_TypePlaceholdersRegistry.put(typeVar.getName(), typeVar);
m_TypePlaceholdersRegistry.put(typeVar.getName().toString(), typeVar);
return typeVar;
//return new TypePlaceholder(makeNewName());
@ -378,7 +378,7 @@ public class TypePlaceholder extends ObjectType
//auf den CSubstitution nicht registrierte Variablen zu
//Exceptions fuehrt
TypePlaceholder typeVar = new TypePlaceholder(name, null);
m_TypePlaceholdersRegistry.put(typeVar.getName(), typeVar);
m_TypePlaceholdersRegistry.put(typeVar.getName().toString(), typeVar);
return typeVar;
//return new TypePlaceholder(name);

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.parser.JavaClassName;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -111,6 +112,9 @@ public class WildcardType extends Type{
return this.innerType.getBytecodeSignature(cg, rs);
}
@Override
public JavaClassName getName() {
return new JavaClassName(this.get_Name());
}
}

View File

@ -4,6 +4,7 @@ import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.logger.*;
import de.dhbwstuttgart.typeinference.unify.Unifier;
@ -98,4 +99,14 @@ public class ConstraintsSet extends UndMenge<Pair> implements Iterable<OderConst
public Menge<OderConstraint> getOderConstraints() {
return this.constraintsSet;
}
public static UndConstraint createSingleConstraint(Type t1, Type t2){
UndConstraint ret = new UndConstraint();
ret.addConstraint(t1, t2);
return ret;
}
public static UndConstraint createSingleConstraint(Pair pair) {
return createSingleConstraint(pair.TA1, pair.TA2);
}
}

View File

@ -1,71 +0,0 @@
package de.dhbwstuttgart.typeinference;
import java.util.Vector;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
/**
* Beschreibung von Herrn Plümicke:
* "The set of constraints consists of constraints of the form θ R θ' , where θ and
* θ' are Java types and R (R { < , <? , = }) is a subtyping condition."
*
* @author AI10023 - Andreas Stadelmeier
*
* Die Klasse stellt ein OderConstraint-Set dar, welches nur aus einem Constraint besteht.
*
*/
public class SingleConstraint extends UndConstraint{
private Pair constraintPair; //entspricht θ condition θ'
//private R condition; //entspricht der condition (R)
public SingleConstraint(Type p1, Type p2){
//super(p1,p2);
if(p1 == null || p2 == null)throw new NullPointerException();
Pair constraintPair = new Pair(p1,p2);//super.getConstraintPairs().firstElement();
this.addConstraint(constraintPair);
}
@Override
public Menge<? extends KomplexeMenge<Pair>> getSet() {
Menge<EinzelElement<Pair>> ret = new Menge<>();
ret.add(new EinzelElement<>(constraintPair));
return ret;
}
public SingleConstraint(Pair toAdd) {
this.addConstraint(toAdd);
}
public Pair getPair(){
return constraintPair;
}
@Override //Methode überschreiben, damit immer nur ein Vector mit nur einem Element zurückgeliefert wird.
public Menge<Pair> getConstraintPairs(){
Menge<Pair> ret = new Menge<Pair>();
ret.add(constraintPair);
return ret;
}
public void addConstraint(Pair toAdd){
if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich.");
Type p1 = toAdd.TA1;
Type p2 = toAdd.TA2;
if(p1==null || p2 == null)throw new NullPointerException();
constraintPair = new Pair(p1,p2);
}
@Override
public String toString(){
return ""+constraintPair.TA1.toString()+" < "+constraintPair.TA2.toString();
}
}

View File

@ -15,7 +15,6 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
import de.dhbwstuttgart.typeinference.unify.CSubstitutionSet;
// ino.class.CTypeReconstructionResult.27238.description type=javadoc
/**

View File

@ -41,4 +41,5 @@ public class UnifyUndConstraint extends UndMenge<MPair> {
public void add(MPair pair){
set.add(new EinzelElement<>(pair));
}
}

View File

@ -6,6 +6,8 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
public class Mapping {
private HashMap<de.dhbwstuttgart.typeinference.unify.model.UnifyType, de.dhbwstuttgart.syntaxtree.type.Type> backwardMap = new HashMap<>();
@ -56,35 +58,14 @@ public class Mapping {
return result.size() == pairs.size() ? Optional.of(result) : Optional.empty();
}
private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) {
/*
* TODO
* Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt?
*/
switch(op) {
case Equal:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
case Smaller:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER;
case SmallerExtends:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT;
default:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
}
private PairOperator mapOp(PairOperator op) {
//TODO: Methode kann entfernt werden:
return op;
}
private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) {
switch(op) {
case EQUALS:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
case SMALLER:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
case SMALLERDOT:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends;
default:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
}
private PairOperator unmapOp(PairOperator op) {
//TODO: Methode kann entfernt werden:
return op;
}
}

View File

@ -22,7 +22,7 @@ import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
public class RuleSet implements IRuleSet{

View File

@ -24,7 +24,7 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.Unifier;

View File

@ -74,4 +74,5 @@ public final class ExtendsType extends UnifyType {
public String toString() {
return "? extends " + extendedType;
}
}

View File

@ -60,4 +60,5 @@ public final class SuperType extends UnifyType {
SuperType other = (SuperType) obj;
return other.getSuperedType().equals(superedType);
}
}

View File

@ -0,0 +1,152 @@
package unify;
import static org.junit.Assert.*;
import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.syntaxtree.NullSyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.FunN;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.UnifyConstraintsSet;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
public class UnifyTypeFactoryTest {
private static TypeFactory tf = new TypeFactory();
@Test
public void convertConstraintSet(){
ConstraintsSet cons = new ConstraintsSet();
OderConstraint oCons = new OderConstraint();
UndConstraint uCons = new UndConstraint();
uCons.addConstraint(getSimpleRefType(), getSimpleRefType());
oCons.addConstraint(uCons);
oCons.addConstraint(ConstraintsSet.createSingleConstraint(getPair()));
cons.add(oCons);
checkConvertConstraintsSet(cons);
}
@Test
public void convertMPair() {
MPair mp1 = getSimpleMPair();
checkConvertMPair(mp1);
TypePlaceholder tph1 = TypePlaceholder.fresh(new NullSyntaxTreeNode());
TypePlaceholder tph2 = TypePlaceholder.fresh(new NullSyntaxTreeNode());
MPair mp2 = new MPair(getSimpleType(), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT);
checkConvertMPair(mp2);
MPair mp3 = new MPair(tf.getSuperType(getSimpleType()), getSimpleType(), PairOperator.SMALLERDOT);
checkConvertMPair(mp3);
MPair mp4 = new MPair(tf.getPlaceholderType(tph2.getName().toString()), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT);
checkConvertMPair(mp4);
SimpleType typeWithParams = tf.getSimpleType("Test", getSimpleType());
SimpleType typeWithTPHParams = tf.getSimpleType("Test", tf.getPlaceholderType("Test"));
MPair mp5 = new MPair(typeWithTPHParams, typeWithParams, PairOperator.SMALLERDOT);
checkConvertMPair(mp5);
}
@Test
public void convertPair(){
TypePlaceholder tph1 = TypePlaceholder.fresh(new NullSyntaxTreeNode());
TypePlaceholder tph2 = TypePlaceholder.fresh(new NullSyntaxTreeNode());
Pair p1 = new Pair(getSimpleRefType(), getSimpleRefType());
checkConvertPair(p1);
Pair p2 = new Pair(tph1, tph2);
checkConvertPair(p1);
ExtendsWildcardType ext = new ExtendsWildcardType(getSimpleRefType());
SuperWildcardType sup = new SuperWildcardType(getSimpleRefType());
Pair p3 = new Pair(ext, sup);
checkConvertPair(p1);
GenericTypeVar gt = new GenericTypeVar("A", new NullSyntaxTreeNode(), 0);
de.dhbwstuttgart.syntaxtree.type.Void voidType = new de.dhbwstuttgart.syntaxtree.type.Void(new NullSyntaxTreeNode(), 0);
Pair p4 = new Pair(gt, voidType);
checkConvertPair(p1);
Pair p5 = new Pair(new FunN(getSimpleRefType(), new Menge<>()), tph1);
checkConvertPair(p5);
}
// Hilfsfunktionen:
private static void checkConvertConstraintsSet(ConstraintsSet cons){
Set<Set<Pair>> check = cons.cartesianProduct();
UnifyConstraintsSet converted = UnifyTypeFactory.convert(cons);
Set<Set<MPair>> cartesianProductOfConverted = converted.cartesianProduct();
assertTrue(cartesianProductOfConverted.size()==check.size());
assertTrue(cartesianProductOfConverted.iterator().next().size()==check.iterator().next().size());
Set<Set<Pair>> controlSet = new Menge<>();
for(Set<MPair> pairs : cartesianProductOfConverted){
Set<Pair> tmpSet = new Menge<>();
for(MPair mp : pairs){
Pair p = checkConvertMPair(mp);
tmpSet.add(p);
}
controlSet.add(tmpSet);
}
assertTrue(controlSet.equals(check));
}
private static MPair checkConvertPair(Pair p){
System.out.println("Checking Pair: "+p);
MPair mp = UnifyTypeFactory.convert(p);
assertTrue(p.TA1.get_Name().equals(mp.getLhsType().getName()));
assertTrue(p.TA2.get_Name().equals(mp.getRhsType().getName()));
return mp;
}
private static Pair checkConvertMPair(MPair mp){
System.out.println("Checking Pair: "+mp);
Pair p = UnifyTypeFactory.convert(mp);
assertTrue(p.TA1.get_Name().equals(mp.getLhsType().getName()));
assertTrue(p.TA2.get_Name().equals(mp.getRhsType().getName()));
return p;
}
private static SimpleType getSimpleType(){
return tf.getSimpleType("String");
}
private static MPair getSimpleMPair(){
UnifyType lt = tf.getSimpleType("String");
UnifyType rt = tf.getSimpleType("String");
MPair ret = new MPair(lt, rt, PairOperator.SMALLERDOT);
return ret;
}
private static Pair getPair(){
return new Pair(getSimpleRefType(),getSimpleRefType());
}
private static RefType getSimpleRefType(){
NullSyntaxTreeNode nullNode = new NullSyntaxTreeNode();
return new RefType("java.lang.String",nullNode,nullNode.getOffset());
}
}