From 7f2d64e73bd91c9c37e608108d2afdcd1262c7ef Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 11 Mar 2015 12:39:08 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=C3=84nderung=20an=20der=20Constraint=20Ers?= =?UTF-8?q?tellung=20f=C3=BCr=20LambdaExpressions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/documentation.md | 11 ++++++++ .../statement/LambdaExpression.java | 27 +++++++------------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/doc/documentation.md b/doc/documentation.md index 74c5add5..1609c390 100644 --- a/doc/documentation.md +++ b/doc/documentation.md @@ -40,6 +40,17 @@ Die einzusetzenden Generischen Variablen werden erst beim Einsetzen eines Typs g * 5. Alle Unbekannten TPHs herausfiltern (von den Pairs nur TA2) * 6. Alle unbekannten TPHs + Pairs als GenericTypeInsertPoint deklarieren. +## Lambda Ausdrücke und FunN +### Erstellen der Typconstraints für einen Lambda Ausdruck +* Return-Type des Lambda Ausdrucks: retT +* Parameter Typen: pT1 ... pTN +* Für jeden Typ einen TPH generieren + * für Return Typ: TPH R + * für Parameter Typen: TPH P1 ... TPH PN +* Es gilt: + * TPH R < retT + * pTN < TPH PN + ## Ablauf Typinferenz: 1. Parsen diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index 57af4a02..132fa399 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -152,27 +152,20 @@ public class LambdaExpression extends Expr{ //ArgumentAssumptions + assumptions ergeben die Assumptions für die Statements innerhalb des Lambda-Bodys: ret.add(method_body.TYPEStmt(ArgumentAssumptions.add(assumptions))); //Es gibt die LambdaExpression nur mit einem Block als Method Body, nicht mit einer einzelnen Expression - //TODO: OderConstraint erstellen mit normalem Typ und ? extemds/super Type - //Die Typen innerhalb von FunN anpassen: - Vector superParamTypes = new Vector<>(); + //Die Constraints für ParameterTypen und Ret Typ erstellen: + Vector tphParamTypes = new Vector<>(); for(Type pT : paramTypes){ - if(pT instanceof ObjectType){ - superParamTypes.add(new SuperWildcardType(pT.getOffset(), (ObjectType) pT)); - }else{ //pT ist weder Void noch ein ObjectType (TPH, GTV, RefType) - superParamTypes.add(pT); - } + TypePlaceholder tph = TypePlaceholder.fresh(this); + tphParamTypes.add(tph); + // PN < TPH PN + ret.add(new SingleConstraint(tph.TYPE(assumptions, this), pT.TYPE(assumptions, this))); } Type retType = method_body.getType(); - Type extRetType = retType; - if(!(retType instanceof de.dhbwstuttgart.syntaxtree.type.Void)){ - if(retType instanceof ObjectType){ - extRetType = new ExtendsWildcardType(retType.getOffset(), (ObjectType) retType); - }else{ //retType ist weder Void noch ein ObjectType (TPH, GTV, RefType) - extRetType = retType; - } - } + Type tphRetType = TypePlaceholder.fresh(this); + // PN < TPH PN + ret.add(new SingleConstraint(retType.TYPE(assumptions, this), tphRetType.TYPE(assumptions, this))); - ret.add(new SingleConstraint(new FunN(extRetType, superParamTypes).TYPE(assumptions, this),this.getType().TYPE(assumptions, this))); + ret.add(new SingleConstraint(new FunN(tphRetType, tphParamTypes).TYPE(assumptions, this),this.getType().TYPE(assumptions, this))); return ret; } From 2c6bce344db76457f4ba2a371a0e6d04e334403c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 12 Mar 2015 12:27:43 +0100 Subject: [PATCH 2/2] TYPE erstellt bei LambdaExpression nun zwei constraints, falls die Parameter/RetType nicht vom Typ ? extends sind. Dann werden die Constraints einmal mit dem eigentlichen Typ T und einem ? extends/super T -Typ generiert --- .../statement/LambdaExpression.java | 26 +++++++++++++++++-- .../TypeInsertTests/BoundedGenericTest.jav | 4 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index 132fa399..055f8041 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -22,9 +22,11 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.WildcardType; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.FunN; 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; @@ -158,12 +160,32 @@ public class LambdaExpression extends Expr{ TypePlaceholder tph = TypePlaceholder.fresh(this); tphParamTypes.add(tph); // PN < TPH PN - ret.add(new SingleConstraint(tph.TYPE(assumptions, this), pT.TYPE(assumptions, this))); + if(pT instanceof WildcardType){ + ret.add(new SingleConstraint(tph.TYPE(assumptions, this), pT.TYPE(assumptions, this))); + }else{ + OderConstraint orCons = new OderConstraint(); + SuperWildcardType superpT = new SuperWildcardType(pT.getOffset(), (ObjectType) pT); + SingleConstraint cons1 = new SingleConstraint(tph.TYPE(assumptions, this), superpT.TYPE(assumptions, this)); + SingleConstraint cons2 = new SingleConstraint(tph.TYPE(assumptions, this), pT.TYPE(assumptions, this)); + orCons.addConstraint(cons1); + orCons.addConstraint(cons2); + ret.add(orCons); + } } Type retType = method_body.getType(); Type tphRetType = TypePlaceholder.fresh(this); // PN < TPH PN - ret.add(new SingleConstraint(retType.TYPE(assumptions, this), tphRetType.TYPE(assumptions, this))); + if(retType instanceof WildcardType){ + ret.add(new SingleConstraint(retType.TYPE(assumptions, this), tphRetType.TYPE(assumptions, this))); + }else{ + OderConstraint orCons = new OderConstraint(); + SuperWildcardType superretType = new SuperWildcardType(retType.getOffset(), (ObjectType) retType); + SingleConstraint cons1 = new SingleConstraint(tphRetType.TYPE(assumptions, this), superretType.TYPE(assumptions, this)); + SingleConstraint cons2 = new SingleConstraint(tphRetType.TYPE(assumptions, this), retType.TYPE(assumptions, this)); + orCons.addConstraint(cons1); + orCons.addConstraint(cons2); + ret.add(orCons); + } ret.add(new SingleConstraint(new FunN(tphRetType, tphParamTypes).TYPE(assumptions, this),this.getType().TYPE(assumptions, this))); return ret; diff --git a/test/plugindevelopment/TypeInsertTests/BoundedGenericTest.jav b/test/plugindevelopment/TypeInsertTests/BoundedGenericTest.jav index 68703a12..2fbcc715 100755 --- a/test/plugindevelopment/TypeInsertTests/BoundedGenericTest.jav +++ b/test/plugindevelopment/TypeInsertTests/BoundedGenericTest.jav @@ -1,6 +1,6 @@ -class BoundedGenericTest{ +class BoundedGenericTest{ var = "test"; - B methode(){ + methode(){ return var; } } \ No newline at end of file