Operator mit TypeExpr versehen

This commit is contained in:
JanUlrich 2014-04-23 17:59:39 +02:00
parent c9477705cc
commit fd5aafb0eb
14 changed files with 144 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -180,7 +180,7 @@ public class BoolLiteral extends Literal
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
this.type = new BooleanType();
return new ConstraintsSet();
}

View File

@ -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) {

View File

@ -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<Type> argumentTypeList = new Vector<Type>();
//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<cA.getParaCount();i++){
ret.add(this.arglist.expr.elementAt(i).TYPEExpr(assumptions));
callConstraints.addConstraint( this.arglist.expr.elementAt(i).getType(), cA.getParameterType(i));
}
//if(this.arglist != null && this.arglist.expr != null)for(Expr arg : this.arglist.expr){
// ret.add(arg.TYPEExpr(assumptions));
//}
this.setType(assumptions.getTypeFor(new RefType(this.get_Name(),0)));
/*
//Ein new-Aufruf ist nichts anderes als ein MethodCall der Methode <init>
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("<init>");
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<Expr> it = this.arglist.expr.iterator();

View File

@ -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

View File

@ -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);
}
}

View File

@ -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.

View File

@ -3,5 +3,6 @@ package typinferenz.exceptions;
public class DebugException extends RuntimeException {
public DebugException(String message) {
System.err.print(message);
}
}