JavaPatternMatching/src/mycompiler/mystatement/IfStmt.java

626 lines
26 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.IfStmt.8632.package
package mycompiler.mystatement;
// ino.end
// ino.module.IfStmt.8632.import
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import mycompiler.mybytecode.ClassFile;
import mycompiler.mybytecode.CodeAttribute;
import mycompiler.mybytecode.JVMCode;
import mycompiler.myclass.Class;
import mycompiler.myexception.CTypeReconstructionException;
import mycompiler.myexception.JVMCodeException;
import mycompiler.myexception.SCExcept;
import mycompiler.myexception.SCStatementException;
import mycompiler.myoperator.LogOp;
import mycompiler.myoperator.Operator;
import mycompiler.myoperator.RelOp;
import mycompiler.mytype.BooleanType;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytype.Void;
import mycompiler.mytypereconstruction.CSubstitution;
import mycompiler.mytypereconstruction.CSupportData;
import mycompiler.mytypereconstruction.CTriple;
import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent;
import mycompiler.mytypereconstruction.set.CSubstitutionSet;
import mycompiler.mytypereconstruction.set.CTripleSet;
import mycompiler.mytypereconstruction.set.CTypeAssumptionSet;
import mycompiler.mytypereconstruction.typeassumption.CTypeAssumption;
import mycompiler.mytypereconstruction.unify.MUB;
import mycompiler.mytypereconstruction.unify.Unify;
import org.apache.log4j.Logger;
// ino.end
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import typinferenz.JavaCodeResult;
import typinferenz.SingleConstraint;
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.ResultSet;
import typinferenz.TypeAssumptions;
// ino.class.IfStmt.25300.declaration
public class IfStmt extends Statement
// ino.end
// ino.class.IfStmt.25300.body
{
// ino.method.IfStmt.25304.definition
public IfStmt(int offset, int variableLength)
// ino.end
// ino.method.IfStmt.25304.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.hamaDebug.25307.declaration
public boolean hamaDebug = true; //hama: Debug Ausgaben von mir ein- bzw. ausschalten
// ino.end
// ino.attribute.expr.25310.declaration
public Expr expr;
// ino.end
// ino.attribute.then_block.25313.declaration
public Statement then_block;
// ino.end
// ino.attribute.else_block.25316.declaration
public Statement else_block;
// ino.end
// ino.attribute.parserlog.25319.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.set_Expr.25322.definition
public void set_Expr(Expr exp)
// ino.end
// ino.method.set_Expr.25322.body
{
this.expr = exp;
}
// ino.end
// ino.method.set_Then_block.25325.definition
public void set_Then_block(Statement blk)
// ino.end
// ino.method.set_Then_block.25325.body
{
this.then_block = blk;
}
// ino.end
// ino.method.set_Else_block.25328.definition
public void set_Else_block(Statement blk)
// ino.end
// ino.method.set_Else_block.25328.body
{
this.else_block = blk;
}
// ino.end
// ino.method.sc_check.25331.definition
void sc_check(Vector<Class> classname, Hashtable ch, Hashtable<String, String> bh, boolean ext, Hashtable parach, Hashtable<String, Hashtable> parabh)
throws SCStatementException
// ino.end
// ino.method.sc_check.25331.body
{
if(ext)
{
parserlog.debug(" ---IfStmt---");
}
SCStatementException exc = null;
try
{
expr.sc_check(classname,ch,bh,ext,parach, parabh);
}
catch(SCStatementException ex)
{
exc = ex;
}
try
{
then_block.sc_check(classname,ch,(Hashtable) bh.clone(), ext, parach, (Hashtable)parabh.clone());
}
catch(SCStatementException ex)
{
if(exc == null)
{
exc = ex;
}
else
{
Vector<SCExcept> v=ex.get_exlist();
SCExcept hilfe;
for(Enumeration<SCExcept> el = v.elements() ; el.hasMoreElements() ; )
{
hilfe=el.nextElement();
exc.addException(hilfe);
}
}
}
if(else_block != null)
{
try
{
else_block.sc_check(classname,ch,(Hashtable) bh.clone(), ext, parach, (Hashtable)parabh.clone());
}
catch(SCStatementException ex)
{
if(exc == null)
{
exc=ex;
}
else
{
Vector<SCExcept> v=ex.get_exlist();
SCExcept hilfe;
for(Enumeration<SCExcept> el=v.elements() ; el.hasMoreElements() ; )
{
hilfe=el.nextElement();
exc.addException(hilfe);
}
}
}
}
if(exc != null)
{
throw exc;
}
}
// ino.end
// ino.method.codegen.25334.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.25334.body
{
if_codegen(classfile, code, false, paralist);
}
// ino.end
// ino.method.if_codegen.25337.definition
public void if_codegen(ClassFile classfile, CodeAttribute code, boolean not, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.if_codegen.25337.body
{
if(expr instanceof NotExpr)
{
expr = ((NotExpr)expr).get_Expr();
if_codegen(classfile, code, !not, paralist);
}
else
{
if(expr instanceof Binary)
{
Operator op = ((Binary)expr).get_Operator();
if(op instanceof LogOp)
{
((LogOp)op).if_codegen(classfile, code, not, expr, then_block, else_block, paralist);
}
else
{
if(op instanceof RelOp)
{
Expr expr1 = ((Binary)expr).get_Expr1();
Expr expr2 = ((Binary)expr).get_Expr2();
expr1.codegen(classfile, code, paralist);
expr2.codegen(classfile, code, paralist);
if(expr1 instanceof Null || expr2 instanceof Null)
{
((RelOp)op).if_codegen(classfile, code, "null", not);
}
else
{
((RelOp)op).if_codegen(classfile, code, expr1.getTypeName(), not);
}
int breakpoint1 = code.get_code_length();
code.add_code_short(0);
if(then_block!=null)
{
then_block.codegen(classfile, code, paralist);
code.set_code_short(code.get_code_length() + 1 - breakpoint1, breakpoint1);
}
int breakpoint2 = code.get_code_length();
if(else_block != null)
{
code.add_code(JVMCode.goto_);
code.add_code_short(0); // replaced later
else_block.codegen(classfile, code, paralist);
code.set_code_short(breakpoint2 + 4 - breakpoint1, breakpoint1);
code.set_code_short(code.get_code_length() - breakpoint2, breakpoint2 + 1);
}
}
else
{
throw new JVMCodeException("JVMCodeException: IfStmt: void if_codegen(ClassFile classfile, Code_attribute code, boolean not)");
}
}
}
else
{
expr.codegen(classfile, code, paralist);
if(!not)
{
code.add_code(JVMCode.ifeq);
}
else
{
code.add_code(JVMCode.ifne);
}
int breakpoint1 = code.get_code_length();
code.add_code_short(0);
if(then_block!=null)
{
then_block.codegen(classfile, code, paralist);
code.set_code_short(code.get_code_length() + 1 - breakpoint1, breakpoint1);
}
int breakpoint2 = code.get_code_length();
if(!(else_block==null || else_block instanceof EmptyStmt))
{
//hama: Die zwei folgenen if-Abfragen wurden eingef<65>gt und beheben den Bug,
//der zuerst im Use Case Toggle1 bemerkt wurde. Hier wird nun gepr<70>ft ob der
//letzte Befehl in einem if-Block ein return war. Trifft dieser Fall zu,
//so wird kein goto Befehl (f<>r das <20>berspringen des else-Blocks eingef<65>gt)
//-> das verlangt die vm ->der code w<>re nicht erreichbar.
//Allerdings k<>nnte das return-Statement nat<61>rlich auch an einer anderen
//Stelle (nicht als letzter Befehl) stehen, f<>r diesen Fall d<>rfte die
//Codegenerierung noch nicht funktionieren. Hab versucht diesen Fall mit
//Toggle3 zu erzeugen - wird aber richtig generiert.
//letztes Statement im Vector in s schreiben
//Statement s = (Statement)(((Block)this.then_block).statements.lastElement());
//Instanz von Return zum pr<70>fen anlegen
Return r = new Return(getOffset(),getVariableLength());
if( !(((Block)this.then_block).statements.lastElement()).getClass().equals(r.getClass()) )
{
code.add_code(JVMCode.goto_);
code.add_code_short(0);
}
else_block.codegen(classfile, code, paralist);
if( !(((Block)this.then_block).statements.lastElement()).getClass().equals(r.getClass()) )
{
code.set_code_short(breakpoint2 + 4 - breakpoint1, breakpoint1);
code.set_code_short(code.get_code_length()-breakpoint2, breakpoint2 + 1);
}
}
}
}
}
// ino.end
// ino.method.TRStatement.25340.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.21 von Martin Pl<EFBFBD>micke
* <br/>Achtung Workaround: RefType "Boolean" durch BaseType \code{BooleanType}
* ersetzen. <br>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param sigma
* @param V
* @param supportData
* @return
*/
// ino.end
// ino.method.TRStatement.25340.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
throws CTypeReconstructionException
// ino.end
// ino.method.TRStatement.25340.body
{
CTripleSet returnSet = new CTripleSet();
// --------------------------
// Bedingungsausdruck rekonstruieren:
// --------------------------
CTripleSet exprTSet = this.expr.TRExp(sigma, V, supportData);
// --------------------------
// Bedingungs-Triple durchgehen:
// --------------------------
Iterator<CTriple> exprTIt = exprTSet.getIterator();
while(exprTIt.hasNext()){
CTriple exprTriple = exprTIt.next();
// --------------------------
// ReturnType mit Boolean unifizieren:
// --------------------------
// Sollte doch mit "boolean" statt "Boolean" unifiziert werden?
// Vector<Vector<Pair>> unifierPossibilities1 = Unify.unify(exprTriple.getResultType(), new BooleanType()/*new RefType("Boolean")*/, supportData.getFiniteClosure());
Vector<Vector<Pair>> unifierPossibilities1 = Unify.unify(exprTriple.getResultType(), new RefType("java.lang.Boolean",getOffset()), supportData.getFiniteClosure());
// --------------------------
// Wenn Unifier vorhanden, dann anwenden:
// --------------------------
if(unifierPossibilities1.size()!=0){
// --------------------------
// Alle m<>glichen Unifier anwenden:
// --------------------------
Vector<CTypeReconstructionException> exceptions=new Vector<CTypeReconstructionException>();
int successfulls=0;
for(int i=0; i<unifierPossibilities1.size(); i++){
try{
CSubstitutionSet unifier1 = new CSubstitutionSet(unifierPossibilities1.elementAt(i));
CTriple boolTriple = exprTriple.cloneAndApplyUnify(unifier1);
// --------------------------
// Then-Zweig rekonstruieren:
// --------------------------
CTripleSet thenTSet = this.then_block.TRStatement(boolTriple.getSubstitutions(), boolTriple.getAssumptionSet(), supportData);
// --------------------------
// ReturnType <20>berpr<70>fen:
// --------------------------
boolean isThenBlockVoid = false;
if(thenTSet.getCardinality()!=0){
isThenBlockVoid = (thenTSet.getVector().firstElement().getResultType() instanceof Void);
}
// --------------------------
// Then-Triples durchgehen:
// --------------------------
Iterator<CTriple> thenTIt = thenTSet.getIterator();
while(thenTIt.hasNext()){
CTriple thenTriple = thenTIt.next();
// --------------------------
// Else-Zweig rekonstruieren:
// --------------------------
if(this.else_block==null){
returnSet.addElement(thenTriple);
continue;
}
CTripleSet elseTSet = this.else_block.TRStatement(thenTriple.getSubstitutions(), thenTriple.getAssumptionSet(), supportData);
// --------------------------
// ReturnType <20>berpr<70>fen:
// --------------------------
//boolean isElseBlockVoid = false;
if(elseTSet.getCardinality()!=0){
///*isElseBlockVoid =*/ (((CTriple)elseTSet.getVector().firstElement()).getResultType() instanceof Void);
}
// --------------------------
// Else-Triples durchgehen:
// --------------------------
Iterator<CTriple> elseTIt = elseTSet.getIterator();
while(elseTIt.hasNext()){
CTriple elseTriple = elseTIt.next();
// --------------------------
// Wenn Void, Typannahmen zur<75>ckliefern:
// --------------------------
if(isThenBlockVoid){
/*
Urspr<EFBFBD>nglich nach Pl<EFBFBD>micke:
if(isElseBlockVoid){
returnSet.addElement(new CTriple(elseTriple.getSubstitutions(), elseTriple.getResultType(), elseTriple.getAssumptionSet()));
}
else {
throw new CTypeReconstructionException("IfStmt.TRStatement(): Typen nicht vereinbar - Then-Zweig hat ReturnType, Else-Zweig ist Void");
} */ // Korrekt aber so:
returnSet.addElement(new CTriple(elseTriple.getSubstitutions(), elseTriple.getResultType(), elseTriple.getAssumptionSet()));
}
// --------------------------+
// Ansonsten ReturnTypes-unifizieren:
// --------------------------
else {
// --------------------------
// MUB berechen
// --------------------------
MUB mub = Unify.unify_Mub(thenTriple.getResultType(), elseTriple.getResultType(), supportData.getFiniteClosure());
// --------------------------
// Unifier anwenden:
// --------------------------
this.makeNewResult(mub, elseTriple, elseTriple.getAssumptionSet(), returnSet);
}
}
}
successfulls++;
}catch(CTypeReconstructionException tre){
exceptions.addElement(tre);
}
}
if(successfulls==0){
if(exceptions.size()==1){
throw exceptions.elementAt(0);
}
throw new CTypeReconstructionException("IfStmt: Es konnte keine Assumption gefunden werden, die auf die Anforderung passt.",exceptions,this);
}
}else {
throw new CTypeReconstructionException("IfStmt.TRStatement(): Bedingung muss boolean sein!",this);
}
}
return returnSet;
}
// ino.end
// ino.method.makeNewResult.25343.definition
private void makeNewResult(MUB Mub, CTriple triple, CTypeAssumptionSet V, CTripleSet returnSet)
throws CTypeReconstructionException
// ino.end
// ino.method.makeNewResult.25343.body
{
CSubstitutionSet unifier = new CSubstitutionSet(Mub.getUnifier());
// --------------------------
// Typannahmen bauen:
// --------------------------
CTypeAssumptionSet V_substituted = V.deepCopy();
V_substituted.sub(unifier);
// --------------------------
// Substitutionen bauen:
// --------------------------
CSubstitutionSet substSet = triple.getSubstitutions().deepCopy();
substSet.applyUnifier(unifier);
substSet.unite(unifier);
// --------------------------
// R<>ckgabetyp bauen:
// --------------------------
Iterator<? extends Type> setMubIt = Mub.getMub().iterator();
while(setMubIt.hasNext()) {
Type retType = setMubIt.next();
Type retType2 = substSet.applyThisSubstitutionSet(retType.clone());
// Muesste eigentlich cloneAndApplyUnify machen PL 06-03-18
// If(Rettype Instanceof TypePlaceholders){
// Iterator<Csubstitution> Pairit = Unifier.Getiterator();
// While(Pairit.Hasnext()){
// Csubstitution Pair = Pairit.Next();
// If(Pair.Gettypevar().Getname().Equals(Rettype.Getname())){
// Rettype = Pair.Gettype();
// Break;
// }
// }
// }
// --------------------------
CTriple resultTriple = new CTriple(substSet, retType2, V_substituted);
//CSubstitutionSet CSubstUnifier = new CSubstitutionSet(unifier);
//CTriple resultTriple2 = retTriple.cloneAndApplyUnify(CSubstUnifier);
// --------------------------
// Triple zu R<>ckgabemenge hinzuf<75>gen
// --------------------------
if(!returnSet.contains(resultTriple)){
returnSet.addElement(resultTriple);
}
}
}
// ino.end
// ino.method.applyUnifier.25346.definition
private void applyUnifier(Vector<Vector<Pair>> unifierPossibilities, CTriple triple, CTypeAssumptionSet V, CTripleSet returnSet)
throws CTypeReconstructionException
// ino.end
// ino.method.applyUnifier.25346.body
{
// --------------------------
// Wenn Unifier vorhanden, dann anwenden:
// --------------------------
if(unifierPossibilities.size()!=0){
// --------------------------
// Alle m<>glichen Unifier auf V_i,j anwenden:
// --------------------------
for(int k=0; k<unifierPossibilities.size(); k++){
CSubstitutionSet unifier = new CSubstitutionSet(unifierPossibilities.elementAt(k));
// --------------------------
// Typannahmen bauen:
// --------------------------
CTypeAssumptionSet V_substituted = V.deepCopy();
V_substituted.sub(unifier);
// --------------------------
// Substitutionen bauen:
// --------------------------
CSubstitutionSet substSet = triple.getSubstitutions().deepCopy();
substSet.applyUnifier(unifier);
substSet.unite(unifier);
// --------------------------
// R<>ckgabetyp bauen:
// --------------------------
Type retType = triple.getResultType();
if(retType instanceof TypePlaceholder){
Iterator<CSubstitution> pairIt = unifier.getIterator();
while(pairIt.hasNext()){
CSubstitution pair = pairIt.next();
if(pair.getTypeVar().getName().equals(retType.getName())){
retType = pair.getType();
break;
}
}
}
// --------------------------
CTriple resultTriple = new CTriple(substSet, retType, V_substituted);
// --------------------------
// Triple zu R<>ckgabemenge hinzuf<75>gen
// --------------------------
if(!returnSet.contains(resultTriple)){
returnSet.addElement(resultTriple);
}
}
}
// --------------------------
// Ansonsten Fehlermeldung:
// --------------------------
else {
throw new CTypeReconstructionException("IfStmt.TRStatement(): Block-Typen lassen sich nicht unifizieren.",this);
}
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25349.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25349.body
{
if(then_block!=null){
then_block.wandleRefTypeAttributes2GenericAttributes(paralist,genericMethodParameters);
}
if(else_block!=null){
else_block.wandleRefTypeAttributes2GenericAttributes(paralist,genericMethodParameters);
}
}
// ino.end
public boolean addOffsetsToStatement(CTypeAssumption localAssumption, String NameVariable, boolean isMemberVariable)
{
expr.addOffsetsToExpression(localAssumption,NameVariable,isMemberVariable);
else_block.addOffsetsToStatement(localAssumption,NameVariable,isMemberVariable);
then_block.addOffsetsToStatement(localAssumption,NameVariable,isMemberVariable);
return true;
}
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
this.setTypeVariable(TypePlaceholder.fresh(this));
ret.add(expr.TYPEExpr(assumptions)); // die Constraints f<>r (expressionDesIfStmt)
ret.add(this.then_block.TYPEStmt(assumptions));
if(else_block!=null){
ret.add(this.else_block.TYPEStmt(assumptions));
if(!(else_block.getTypeVariable() instanceof Void))ret.add(new SingleConstraint(else_block.getTypeVariable(),this.getTypeVariable()));
}
ret.add(new SingleConstraint(expr.getTypeVariable(),new RefType("boolean",0))); //(expressionDesIfStmt)<.boolean
if(!(then_block.getTypeVariable() instanceof Void))ret.add(new SingleConstraint(then_block.getTypeVariable(),this.getTypeVariable()));
if(then_block.getTypeVariable() instanceof Void &&
(else_block == null || else_block.getTypeVariable() instanceof Void))this.setTypeVariable(new Void(this.getOffset()));
return ret;
}
public void replaceType(CReplaceTypeEvent e) {
// TODO Auto-generated method stub
throw new NotImplementedException();
}
public int getTypeLineNumber() {
throw new NotImplementedException();
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult("If(").attach(this.expr.printJavaCode(resultSet)).attach("){\n");
if(this.then_block!=null)ret.attach(this.then_block.printJavaCode(resultSet));
ret.attach("\n}else{\n");
if(this.else_block!=null)ret.attach(this.else_block.printJavaCode(resultSet));
ret.attach("\n}");
return ret;
}
}
// ino.end