From fd5aafb0eb5d4245f9305486469cc333257bcbd5 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 23 Apr 2014 17:59:39 +0200 Subject: [PATCH] Operator mit TypeExpr versehen --- src/mycompiler/myoperator/AddOp.java | 16 ++++++++ src/mycompiler/myoperator/AndOp.java | 10 +++++ src/mycompiler/myoperator/LogOp.java | 14 +++++++ src/mycompiler/myoperator/Operator.java | 20 ++++++++++ src/mycompiler/myoperator/RelOp.java | 10 +++++ src/mycompiler/mystatement/Binary.java | 14 ++++++- src/mycompiler/mystatement/BinaryExpr.java | 6 ++- src/mycompiler/mystatement/BoolLiteral.java | 2 +- src/mycompiler/mystatement/LocalVarDecl.java | 6 ++- src/mycompiler/mystatement/NewClass.java | 37 ++++++++++++++----- src/mycompiler/mystatement/NotExpr.java | 11 +++++- .../assumptions/ConstructorAssumption.java | 2 +- .../assumptions/TypeAssumptions.java | 15 +++++++- .../exceptions/DebugException.java | 1 + 14 files changed, 144 insertions(+), 20 deletions(-) diff --git a/src/mycompiler/myoperator/AddOp.java b/src/mycompiler/myoperator/AddOp.java index 0a4fc88c..3e9a3024 100755 --- a/src/mycompiler/myoperator/AddOp.java +++ b/src/mycompiler/myoperator/AddOp.java @@ -5,14 +5,21 @@ package mycompiler.myoperator; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; + +import typinferenz.ConstraintsSet; +import typinferenz.SingleConstraint; +import typinferenz.assumptions.TypeAssumptions; +import typinferenz.exceptions.DebugException; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.myexception.CTypeReconstructionException; import mycompiler.myexception.JVMCodeException; import mycompiler.mystatement.Binary; import mycompiler.mystatement.Expr; +import mycompiler.mytype.IntegerType; import mycompiler.mytype.Pair; import mycompiler.mytype.RefType; +import mycompiler.mytype.Type; import mycompiler.mytypereconstruction.CSupportData; import mycompiler.mytypereconstruction.CTriple; import mycompiler.mytypereconstruction.set.CSubstitutionSet; @@ -56,5 +63,14 @@ public abstract class AddOp extends Operator return types; } + + + @Override + public Type getReturnType(TypeAssumptions ass) { + Type ret = ass.getTypeFor(new RefType("java.lang.Integer",-1)); + if(ret == null)throw new DebugException("java.lang.Integer kann nicht aufgelöst werden"); + return ret; + } + } // ino.end diff --git a/src/mycompiler/myoperator/AndOp.java b/src/mycompiler/myoperator/AndOp.java index ef7b80d9..91926d7d 100755 --- a/src/mycompiler/myoperator/AndOp.java +++ b/src/mycompiler/myoperator/AndOp.java @@ -1,5 +1,13 @@ // ino.module.AndOp.8595.package package mycompiler.myoperator; + +import mycompiler.mystatement.Expr; +import mycompiler.mytype.BooleanType; +import mycompiler.mytype.IntegerType; +import mycompiler.mytype.Type; +import typinferenz.ConstraintsSet; +import typinferenz.SingleConstraint; +import typinferenz.assumptions.TypeAssumptions; // ino.end // ino.class.AndOp.24101.declaration @@ -16,5 +24,7 @@ public class AndOp extends LogOp super(offset,variableLength); } // ino.end + + } // ino.end diff --git a/src/mycompiler/myoperator/LogOp.java b/src/mycompiler/myoperator/LogOp.java index 4d3c4203..a7018b11 100755 --- a/src/mycompiler/myoperator/LogOp.java +++ b/src/mycompiler/myoperator/LogOp.java @@ -5,6 +5,11 @@ package mycompiler.myoperator; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; + +import typinferenz.ConstraintsSet; +import typinferenz.SingleConstraint; +import typinferenz.assumptions.TypeAssumptions; +import typinferenz.exceptions.DebugException; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.mybytecode.JVMCode; @@ -15,8 +20,10 @@ import mycompiler.mystatement.Expr; import mycompiler.mystatement.NotExpr; import mycompiler.mystatement.Null; import mycompiler.mystatement.Statement; +import mycompiler.mytype.BooleanType; import mycompiler.mytype.Pair; import mycompiler.mytype.RefType; +import mycompiler.mytype.Type; import mycompiler.mytypereconstruction.CSupportData; import mycompiler.mytypereconstruction.CTriple; import mycompiler.mytypereconstruction.set.CSubstitutionSet; @@ -226,6 +233,13 @@ public abstract class LogOp extends Operator return types; } + + @Override + public Type getReturnType(TypeAssumptions ass) { + Type ret = ass.getTypeFor(new RefType("java.lang.Boolean",-1)); + if(ret == null)throw new DebugException("java.lang.Boolean kann nicht aufgelöst werden"); + return ret; + } } // ino.end diff --git a/src/mycompiler/myoperator/Operator.java b/src/mycompiler/myoperator/Operator.java index e12c92f7..2e47751a 100755 --- a/src/mycompiler/myoperator/Operator.java +++ b/src/mycompiler/myoperator/Operator.java @@ -5,6 +5,10 @@ package mycompiler.myoperator; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; + +import typinferenz.ConstraintsSet; +import typinferenz.SingleConstraint; +import typinferenz.assumptions.TypeAssumptions; import mycompiler.IItemWithOffset; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; @@ -15,6 +19,7 @@ import mycompiler.mystatement.Binary; import mycompiler.mystatement.Expr; import mycompiler.mytype.Pair; import mycompiler.mytype.RefType; +import mycompiler.mytype.Type; import mycompiler.mytypereconstruction.CSupportData; import mycompiler.mytypereconstruction.CTriple; import mycompiler.mytypereconstruction.set.CSubstitutionSet; @@ -111,5 +116,20 @@ public abstract class Operator implements IItemWithOffset } // ino.end + /** + * Berechnet die Constraints dieses Operators für die 2 gegebenen Parameter + * @param expr1 + * @param expr2 + * @return + */ + public ConstraintsSet TYPEExpr(Expr expr1, Expr expr2, TypeAssumptions ass) { + ConstraintsSet ret = new ConstraintsSet(); + ret.add(new SingleConstraint(expr1.getType(), this.getReturnType(ass))); + ret.add(new SingleConstraint(expr2.getType(), this.getReturnType(ass))); + return ret; + } + + public abstract Type getReturnType(TypeAssumptions ass); + } // ino.end diff --git a/src/mycompiler/myoperator/RelOp.java b/src/mycompiler/myoperator/RelOp.java index ed320bb9..ab55a08b 100755 --- a/src/mycompiler/myoperator/RelOp.java +++ b/src/mycompiler/myoperator/RelOp.java @@ -6,6 +6,9 @@ package mycompiler.myoperator; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; + +import typinferenz.assumptions.TypeAssumptions; +import typinferenz.exceptions.DebugException; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.myexception.CTypeReconstructionException; @@ -13,6 +16,7 @@ import mycompiler.myexception.JVMCodeException; import mycompiler.mystatement.Binary; import mycompiler.mytype.Pair; import mycompiler.mytype.RefType; +import mycompiler.mytype.Type; import mycompiler.mytypereconstruction.CSupportData; import mycompiler.mytypereconstruction.CTriple; import mycompiler.mytypereconstruction.set.CSubstitutionSet; @@ -52,6 +56,12 @@ public abstract class RelOp extends Operator return types; } + @Override + public Type getReturnType(TypeAssumptions ass){ + Type ret = ass.getTypeFor(new RefType("java.lang.Boolean",-1)); + if(ret == null)throw new DebugException("java.lang.Boolean kann nicht aufgelöst werden"); + return ret; + } } // ino.end diff --git a/src/mycompiler/mystatement/Binary.java b/src/mycompiler/mystatement/Binary.java index effe3e42..eaffa3f3 100755 --- a/src/mycompiler/mystatement/Binary.java +++ b/src/mycompiler/mystatement/Binary.java @@ -33,10 +33,12 @@ import org.apache.log4j.Logger; + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.ConstraintsSet; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; +import typinferenz.SingleConstraint; import typinferenz.assumptions.TypeAssumptions; @@ -255,7 +257,11 @@ public class Binary extends BinaryExpr @Override public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { ConstraintsSet ret = new ConstraintsSet(); - // TODO Implement Method stub + ret.add(this.expr1.TYPEExpr(assumptions)); + ret.add(this.expr2.TYPEExpr(assumptions)); + //Der Operator generiert die fehlenden Constraints: + ret.add(this.op.TYPEExpr(expr1, expr2, assumptions)); + this.set_Type(this.op.getReturnType(assumptions)); return ret; } @@ -263,7 +269,11 @@ public class Binary extends BinaryExpr @Override public JavaCodeResult printJavaCode(ResultSet resultSet) { - throw new NotImplementedException(); + JavaCodeResult ret = new JavaCodeResult(); + ret.attach(this.expr1.printJavaCode(resultSet)).attach(" "); + ret.attach(this.op.toString()+" "); + ret.attach(this.expr2.printJavaCode(resultSet)); + return ret; } diff --git a/src/mycompiler/mystatement/BinaryExpr.java b/src/mycompiler/mystatement/BinaryExpr.java index 8af06a31..a8e4ca28 100755 --- a/src/mycompiler/mystatement/BinaryExpr.java +++ b/src/mycompiler/mystatement/BinaryExpr.java @@ -1,5 +1,8 @@ // ino.module.BinaryExpr.8624.package package mycompiler.mystatement; + +import typinferenz.ConstraintsSet; +import typinferenz.assumptions.TypeAssumptions; // ino.end @@ -17,7 +20,8 @@ public abstract class BinaryExpr extends Expr super(offset,variableLength); } // ino.end - + + // abstract public void if_codegen(ClassFile classfile, Code_attribute code, boolean sw) throws jvmCode_Exception; // abstract public void not_codegen(ClassFile classfile, Code_attribute code) throws jvmCode_Exception; diff --git a/src/mycompiler/mystatement/BoolLiteral.java b/src/mycompiler/mystatement/BoolLiteral.java index 10b6e39b..128d7683 100755 --- a/src/mycompiler/mystatement/BoolLiteral.java +++ b/src/mycompiler/mystatement/BoolLiteral.java @@ -180,7 +180,7 @@ public class BoolLiteral extends Literal @Override public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { - + this.type = new BooleanType(); return new ConstraintsSet(); } diff --git a/src/mycompiler/mystatement/LocalVarDecl.java b/src/mycompiler/mystatement/LocalVarDecl.java index a96de1d0..049bc521 100755 --- a/src/mycompiler/mystatement/LocalVarDecl.java +++ b/src/mycompiler/mystatement/LocalVarDecl.java @@ -493,8 +493,10 @@ public class LocalVarDecl extends Statement implements TypeInsertable @Override public JavaCodeResult printJavaCode(ResultSet resultSet) { - return new JavaCodeResult().attach(getType().printJavaCode(resultSet)) .attach( " "+this.get_Name()+";"); - } + JavaCodeResult ret = new JavaCodeResult(); + if(this.getType()!=null)ret.attach(getType().printJavaCode(resultSet)).attach(" "); + ret.attach(this.get_Name()+";"); + return ret;} @Override public void setOffset(int offset) { diff --git a/src/mycompiler/mystatement/NewClass.java b/src/mycompiler/mystatement/NewClass.java index afc7c20b..dddfb35d 100755 --- a/src/mycompiler/mystatement/NewClass.java +++ b/src/mycompiler/mystatement/NewClass.java @@ -38,6 +38,9 @@ import org.apache.log4j.Logger; + + + import typinferenz.JavaCodeResult; import typinferenz.Overloading; import typinferenz.SingleConstraint; @@ -45,7 +48,10 @@ import typinferenz.ConstraintsSet; import typinferenz.FreshTypeVariable; import typinferenz.FunN; import typinferenz.ResultSet; +import typinferenz.UndConstraint; +import typinferenz.assumptions.ConstructorAssumption; import typinferenz.assumptions.TypeAssumptions; +import typinferenz.exceptions.TypeinferenceException; @@ -226,8 +232,7 @@ public class NewClass extends Expr public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { //TODO: Das hier noch vervollständigen ConstraintsSet ret = new ConstraintsSet(); - - this.set_Type(TypePlaceholder.fresh(this)); + UndConstraint callConstraints = new UndConstraint(); //Die Auskommentierten Zeilen gehören zu MethodRefNew //Vector argumentTypeList = new Vector(); //for(Expr expr : this.arglist.expr){ @@ -237,19 +242,31 @@ public class NewClass extends Expr //Constraint newClassTypeConstraint = new Constraint(null,null); //ret.add(newClassTypeConstraint); + int numArgs = 0; + if(this.arglist != null)numArgs = this.arglist.size(); + ConstructorAssumption cA = assumptions.getConstructorAssumption(this.get_Name(), numArgs); + if(cA == null)throw new TypeinferenceException("Der Konstruktor "+this.get_Name()+" mit "+numArgs+" Parametern ist nicht vorhanden.", this); - if(this.arglist != null && this.arglist.expr != null)for(Expr arg : this.arglist.expr){ - ret.add(arg.TYPEExpr(assumptions)); + for(int i=0; i MethodCall newAufruf = new MethodCall(0,0); - this.setType(new RefType(this.get_Name(),0)); - newAufruf.type = new RefType(this.get_Name(),0); - newAufruf.set_Name(this.get_Name()); + this.setType(assumptions.getTypeFor(new RefType(this.get_Name(),0))); + newAufruf.type = this.getType(); + newAufruf.set_Name(""); newAufruf.set_Receiver(null); ret.add(new Overloading(assumptions, newAufruf, this.getType()).generateConsstraints()); - + */ return ret; } @@ -262,14 +279,14 @@ public class NewClass extends Expr @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){ ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen - this.set_Type(new Void(0)); //Typ des Statments auf Void setzen. + this.setType(new Void(0)); //Typ des Statments auf Void setzen. return ret; } @Override public JavaCodeResult printJavaCode(ResultSet resultSet) { JavaCodeResult ret = new JavaCodeResult("new "); - ret.attach(this.getType().printJavaCode(resultSet)); + ret.attach(this.get_Name()); ret.attach("("); if(this.arglist!=null && this.arglist.expr != null){ Iterator it = this.arglist.expr.iterator(); diff --git a/src/mycompiler/mystatement/NotExpr.java b/src/mycompiler/mystatement/NotExpr.java index e7389d60..607d25d4 100755 --- a/src/mycompiler/mystatement/NotExpr.java +++ b/src/mycompiler/mystatement/NotExpr.java @@ -13,6 +13,7 @@ import mycompiler.myclass.Class; import mycompiler.myexception.CTypeReconstructionException; import mycompiler.myexception.JVMCodeException; import mycompiler.myexception.SCStatementException; +import mycompiler.mytype.BooleanType; import mycompiler.mytype.GenericTypeVar; import mycompiler.mytype.Pair; import mycompiler.mytype.RefType; @@ -31,8 +32,11 @@ import org.apache.log4j.Logger; + + import typinferenz.ConstraintsSet; import typinferenz.JavaCodeResult; +import typinferenz.OderConstraint; import typinferenz.ResultSet; import typinferenz.assumptions.TypeAssumptions; @@ -155,8 +159,11 @@ public class NotExpr extends UnaryExpr @Override public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { - // TODO Auto-generated method stub - return null; + ConstraintsSet ret = new ConstraintsSet(); + OderConstraint constraint = new OderConstraint(); + constraint.addConstraint(new Pair(this.getType(), new BooleanType())); + ret.add(constraint); + return ret; } @Override diff --git a/src/typinferenz/assumptions/ConstructorAssumption.java b/src/typinferenz/assumptions/ConstructorAssumption.java index d0801d3f..855c40f9 100644 --- a/src/typinferenz/assumptions/ConstructorAssumption.java +++ b/src/typinferenz/assumptions/ConstructorAssumption.java @@ -3,6 +3,7 @@ package typinferenz.assumptions; import mycompiler.myclass.Class; import mycompiler.myclass.Field; import mycompiler.myclass.Method; +import mycompiler.mystatement.ArgumentList; public class ConstructorAssumption extends MethodAssumption{ @@ -15,7 +16,6 @@ public class ConstructorAssumption extends MethodAssumption{ if(!(obj instanceof ConstructorAssumption))return false; return super.equals(obj); } - } diff --git a/src/typinferenz/assumptions/TypeAssumptions.java b/src/typinferenz/assumptions/TypeAssumptions.java index b171ff26..45d885b0 100755 --- a/src/typinferenz/assumptions/TypeAssumptions.java +++ b/src/typinferenz/assumptions/TypeAssumptions.java @@ -300,7 +300,7 @@ public class TypeAssumptions { }else if(names.length == 0 || names.length != assNames.length){ match = false; }else for(int i = names.length-1; i>-1;i--){ - if(!names.equals(assNames))match = false; + if(!names[i].equals(assNames[i]))match = false; } if(match){ RefType ret = ass.getAssumedClass().getType(); //Dadurch erhält der RefType den vollen Namen (bsp. java.lang.Integer) @@ -322,6 +322,19 @@ public class TypeAssumptions { this.classAssumptions.add(classAssumption); } + /** + * + * @param name + * @param size + * @return Null, falls kein Konstruktor vorhanden. + */ + public ConstructorAssumption getConstructorAssumption(String name, int size) { + for(ConstructorAssumption ca : this.constructorAssumptions){ + if(ca.getParaCount()==size && ca.getIdentifier().equals(name))return ca; + } + return null; + } + /** * Prüft einen Typ auf das vorhandensein in den BasicAssumptions. * Dabei werden alle Konstruktoren nach diesem Typ durchsucht. Denn jede Klasse hat einen Konstruktor und der muss in den TypeAssumptions vorhanden sein. diff --git a/src/typinferenz/exceptions/DebugException.java b/src/typinferenz/exceptions/DebugException.java index b7c082d3..2043320a 100644 --- a/src/typinferenz/exceptions/DebugException.java +++ b/src/typinferenz/exceptions/DebugException.java @@ -3,5 +3,6 @@ package typinferenz.exceptions; public class DebugException extends RuntimeException { public DebugException(String message) { + System.err.print(message); } }