JavaPatternMatching/src/mycompiler/mystatement/WhileStmt.java
2013-10-18 13:33:46 +02:00

316 lines
11 KiB
Java
Executable File
Raw Blame History

// ino.module.WhileStmt.8659.package
package mycompiler.mystatement;
// ino.end
// ino.module.WhileStmt.8659.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.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.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.ResultSet;
import typinferenz.TypeAssumptions;
// ino.class.WhileStmt.26326.declaration
public class WhileStmt extends Statement
// ino.end
// ino.class.WhileStmt.26326.body
{
// ino.method.WhileStmt.26330.definition
public WhileStmt(int offset, int variableLength)
// ino.end
// ino.method.WhileStmt.26330.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.expr.26333.declaration
public Expr expr;
// ino.end
// ino.attribute.loop_block.26336.declaration
public Statement loop_block;
// ino.end
// ino.attribute.parserlog.26339.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.set_Expr.26342.definition
public void set_Expr(Expr exp)
// ino.end
// ino.method.set_Expr.26342.body
{
this.expr = exp;
}
// ino.end
// ino.method.set_Loop_block.26345.definition
public void set_Loop_block(Statement blk)
// ino.end
// ino.method.set_Loop_block.26345.body
{
this.loop_block = blk;
}
// ino.end
// ino.method.sc_check.26348.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.26348.body
{
if(ext)
parserlog.debug(" ---WhileStmt---");
SCStatementException exc=null;
if(ext)
{
parserlog.debug("Semantik-Check ist in der Klasse WhileStmt");
parserlog.debug("Semantik-Check ueberprueft While-Bedingung-Expression.");
}
try
{
expr.sc_check(classname,ch,bh,ext,parach,parabh);
}
catch (SCStatementException ex)
{
exc=ex;
}
try
{
loop_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;
SCExcept hilf;
v=ex.get_exlist();
for(Enumeration<SCExcept> el=v.elements();el.hasMoreElements();)
{
hilf=el.nextElement();
exc.addException(hilf);
}
}
}
if(exc!=null)
throw exc;
}
// ino.end
// ino.method.codegen.26351.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.26351.body
{
if(loop_block!=null && expr!=null) {
code.add_code(JVMCode.goto_);
int breakpoint = code.get_code_length();
code.add_code_short(0);
loop_block.codegen(classfile, code, paralist);
code.set_code_short(code.get_code_length() + 1 - breakpoint, breakpoint);
loop_codegen(classfile, code, breakpoint, false, paralist);
}
}
// ino.end
// ino.method.loop_codegen.26354.definition
public void loop_codegen(ClassFile classfile, CodeAttribute code, int breakpoint, boolean not, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.loop_codegen.26354.body
{
if(expr instanceof NotExpr) {
expr = ((NotExpr)expr).get_Expr();
loop_codegen(classfile, code, breakpoint, !not, paralist);
}
else if(expr instanceof Binary) {
Operator op = ((Binary)expr).get_Operator();
if(op instanceof LogOp) ((LogOp)op).loop_codegen(classfile, code, expr, breakpoint, !not, 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);
code.add_code_short(breakpoint + 3 - code.get_code_length());
}
else throw new JVMCodeException("JVMCodeException: WhileStmt: void loop_codegen(ClassFile classfile, Code_attribute code, int breakpoint, boolean not)");
}
else {
expr.codegen(classfile, code, paralist);
if(!not) code.add_code(JVMCode.ifne);
else code.add_code(JVMCode.ifeq);
code.add_code_short(breakpoint + 3 - code.get_code_length());
}
}
// ino.end
// ino.method.TRStatement.26357.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.23 von Martin Pl<50>micke
* <br/>Achtung Workaround: RefType "Boolean" muss noch durch BaseType
* "BooleanType" ersetzt werden.
* <br>Author: J<>rg B<>uerle
* @param sigma
* @param V
* @param supportData
* @return
*/
// ino.end
// ino.method.TRStatement.26357.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRStatement.26357.body
{
CTripleSet returnSet = new CTripleSet();
CTripleSet tripleSet = expr.TRExp(sigma, V, supportData);
Iterator<CTriple> exprTIt = tripleSet.getIterator();
while(exprTIt.hasNext()){
CTriple exprTriple = exprTIt.next();
// --------------------------
// ReturnType mit Boolean unifizieren:
// --------------------------
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:
// --------------------------
int successfulls=0;
Vector<CTypeReconstructionException> exceptions=new Vector<CTypeReconstructionException>();
for(int i=0; i<unifierPossibilities1.size(); i++){
try{
CSubstitutionSet unifier1 = new CSubstitutionSet(unifierPossibilities1.elementAt(i));
CTriple boolTriple = exprTriple.cloneAndApplyUnify(unifier1);
returnSet.unite(this.loop_block.TRStatement(boolTriple.getSubstitutions(), boolTriple.getAssumptionSet(), supportData));
successfulls++;
}catch(CTypeReconstructionException tre){
exceptions.addElement(tre);
}
if(successfulls==0){
if(exceptions.size()==1){
throw exceptions.elementAt(0);
}
throw new CTypeReconstructionException("WhileStmt: Es konnte keine Assumption gefunden werden, die auf die Anforderung passt.",exceptions,this);
}
}
}
else {
throw new CTypeReconstructionException("WhileStmt.TRStatement(): Bedingung muss boolean sein!",this);
}
}
return returnSet;
}
// ino.end
// ino.method.toString.26360.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<50>micke
* @return
*/
// ino.end
// ino.method.toString.26360.definition
public String toString()
// ino.end
// ino.method.toString.26360.body
{
return "WHILE " + loop_block.toString();
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.26363.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.26363.body
{
if(loop_block!=null){
loop_block.wandleRefTypeAttributes2GenericAttributes(paralist,genericMethodParameters);
}
}
// ino.end
public boolean addOffsetsToStatement(CTypeAssumption localAssumption, String NameVariable, boolean isMemberVariable)
{
expr.addOffsetsToExpression(localAssumption,NameVariable,isMemberVariable);
loop_block.addOffsetsToStatement(localAssumption,NameVariable,isMemberVariable);
return true;
}
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
ret.add(expr.TYPEExpr(assumptions));
SingleConstraint exprMustBeBool = new SingleConstraint(expr.getTypeVariable(), new RefType("boolean", 0)); // while(expr){}; expr <. boolean
ret.add(exprMustBeBool);
ret.add(this.loop_block.TYPEStmt(assumptions));
this.setTypeVariable(loop_block.getTypeVariable());
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) {
return new JavaCodeResult().attach("while(").attach(this.expr.printJavaCode(resultSet)).attach(")").attach(this.loop_block.printJavaCode(resultSet));
}
}
// ino.end