diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 9305e36f..1f0adda8 100755 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -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 diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 9859a734..73cfb0f8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 7cc3c54b..d4814cb9 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -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; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index ebed1381..662ac477 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -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; } diff --git a/src/de/dhbwstuttgart/syntaxtree/NullSyntaxTreeNode.java b/src/de/dhbwstuttgart/syntaxtree/NullSyntaxTreeNode.java new file mode 100644 index 00000000..5952cd17 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/NullSyntaxTreeNode.java @@ -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 getChildren() { + return new Menge<>(); + } + +} diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 12c1a654..c02ed64f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -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 supertypeGenPara Class generatedClass = new Class(className, type, modifiers, supertypeGenPara); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyPairMengenBuilder.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyPairMengenBuilder.java index 95fd0897..8caf73cc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyPairMengenBuilder.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyPairMengenBuilder.java @@ -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 { diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 51a895b4..193c8589 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -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 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 oC : set.getPairs()){ ret.add(UnifyTypeFactory.convert(oC)); @@ -121,10 +129,14 @@ public class UnifyTypeFactory { return ret; } - private static MPair convert(EinzelElement 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 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"); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/Unify_FC_TTO_Builder.java b/src/de/dhbwstuttgart/syntaxtree/factory/Unify_FC_TTO_Builder.java index d28992cf..5f10ea6b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/Unify_FC_TTO_Builder.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/Unify_FC_TTO_Builder.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java index c9e95ee3..b1b4e4b6 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/AndOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/AndOp.java index fdb01d2c..09323e48 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/AndOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/AndOp.java @@ -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 diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/LogOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/LogOp.java index afea3229..40c4d0c3 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/LogOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/LogOp.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java b/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java index 8639bb8f..f4534b45 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java @@ -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 /** diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 9e1ade7c..2154d35e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -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; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index d8f836ed..44fc3cc8 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index bba4069a..9931abb4 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -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); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index eeff85ba..7168da1b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 51e535b6..23131b42 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index 5643f26e..4acaa018 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -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; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java index 2c6debd6..9c3c23e4 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java index 544e9fe2..ccda7e96 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index 5b9e35e4..b0542207 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index 73bb9f89..dce35395 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -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; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java index d96ed086..35a18dc7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java @@ -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()); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.java index a815af3f..1dfef648 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/BoundedGenericTypeVar.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index 22abeea0..4ae00f7f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -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(); + } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java index d4e4d52a..c7ced871 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericTypeVar.java @@ -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; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index df10d990..dee1a9d9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -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(); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 33d4babf..aa60576e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -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 m_TypePlaceholdersRegistry = new Hashtable(); + private static Hashtable m_TypePlaceholdersRegistry = new Hashtable(); // 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(); + m_TypePlaceholdersRegistry = new Hashtable(); } // 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); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java index 262e1466..e21302fa 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java @@ -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()); + } } diff --git a/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java b/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java index 7b611e2c..c68e4865 100755 --- a/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java +++ b/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java @@ -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 implements Iterable 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); + } } diff --git a/src/de/dhbwstuttgart/typeinference/SingleConstraint.java b/src/de/dhbwstuttgart/typeinference/SingleConstraint.java deleted file mode 100755 index b8714689..00000000 --- a/src/de/dhbwstuttgart/typeinference/SingleConstraint.java +++ /dev/null @@ -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 ∈ { < , > getSet() { - Menge> 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 getConstraintPairs(){ - Menge ret = new Menge(); - 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(); - } -} diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 913d9d6b..3059529c 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -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 /** diff --git a/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java b/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java index b20cd6e6..f963e12f 100644 --- a/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java @@ -41,4 +41,5 @@ public class UnifyUndConstraint extends UndMenge { public void add(MPair pair){ set.add(new EinzelElement<>(pair)); } + } diff --git a/src/de/dhbwstuttgart/typeinference/unify/Mapping.java b/src/de/dhbwstuttgart/typeinference/unify/Mapping.java index 603fb0f7..07206065 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Mapping.java @@ -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 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; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 0461a0fc..297eadbf 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -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{ diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index cb2cf3c4..891acd59 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -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; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index 4cf3ceef..f4a79794 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -74,4 +74,5 @@ public final class ExtendsType extends UnifyType { public String toString() { return "? extends " + extendedType; } + } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 8283c685..48ed2891 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -60,4 +60,5 @@ public final class SuperType extends UnifyType { SuperType other = (SuperType) obj; return other.getSuperedType().equals(superedType); } + } diff --git a/test/unify/UnifyTypeFactoryTest.java b/test/unify/UnifyTypeFactoryTest.java new file mode 100644 index 00000000..15d6f5f5 --- /dev/null +++ b/test/unify/UnifyTypeFactoryTest.java @@ -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> check = cons.cartesianProduct(); + + UnifyConstraintsSet converted = UnifyTypeFactory.convert(cons); + Set> cartesianProductOfConverted = converted.cartesianProduct(); + + assertTrue(cartesianProductOfConverted.size()==check.size()); + assertTrue(cartesianProductOfConverted.iterator().next().size()==check.iterator().next().size()); + + Set> controlSet = new Menge<>(); + for(Set pairs : cartesianProductOfConverted){ + Set 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()); + } +}