// ino.module.IfStmt.8632.package package de.dhbwstuttgart.syntaxtree.statement; // ino.end // ino.module.IfStmt.8632.import import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import org.apache.commons.bcel6.Constants; import org.apache.commons.bcel6.generic.ALOAD; import org.apache.commons.bcel6.generic.ASTORE; import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; import org.apache.commons.bcel6.generic.GOTO; import org.apache.commons.bcel6.generic.GotoInstruction; import org.apache.commons.bcel6.generic.IFEQ; import org.apache.commons.bcel6.generic.IFNONNULL; import org.apache.commons.bcel6.generic.IfInstruction; import org.apache.commons.bcel6.generic.Instruction; import org.apache.commons.bcel6.generic.InstructionFactory; import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.SCExcept; import de.dhbwstuttgart.myexception.SCStatementException; import de.dhbwstuttgart.syntaxtree.Class; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.operator.LogOp; import de.dhbwstuttgart.syntaxtree.operator.Operator; import de.dhbwstuttgart.syntaxtree.operator.RelOp; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.Unify; // 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.wandleRefTypeAttributes2GenericAttributes.25349.definition public void wandleRefTypeAttributes2GenericAttributes(Menge paralist, Menge 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 @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { ConstraintsSet ret = new ConstraintsSet(); this.setType(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.getType() instanceof Void))ret.add(ConstraintsSet.createSingleConstraint(else_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this))); } ret.add(ConstraintsSet.createSingleConstraint(expr.getType().TYPE(assumptions, this),new RefType("Boolean",this,0).TYPE(assumptions, this))); //(expressionDesIfStmt)<.boolean if(!(then_block.getType() instanceof Void))ret.add(ConstraintsSet.createSingleConstraint(then_block.getType().TYPE(assumptions, this),this.getType().TYPE(assumptions, this))); if(then_block.getType() instanceof Void && (else_block == null || else_block.getType() instanceof Void))this.setType(new Void(this,this.getOffset())); return ret; } 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; } @Override public Menge getChildren() { Menge ret = new Menge(); if(this.expr!=null)ret.add(this.expr); if(this.else_block!=null)ret.add(this.else_block); if(this.then_block!=null)ret.add(this.then_block); return ret; } @Override public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs) { InstructionFactory _factory = _cg.getInstructionFactory(); InstructionList il = new InstructionList(); IfInstruction ifInstruction = new IFEQ(null); il.append(expr.genByteCode(_cg, rs)); //Anmerkung: expr ist vom Typ java.lang.Boolean (per Definition) il.append(_factory.createInvoke("java.lang.Boolean", "booleanValue", org.apache.commons.bcel6.generic.Type.BOOLEAN, org.apache.commons.bcel6.generic.Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(ifInstruction); il.append(then_block.genByteCode(_cg, rs)); if(else_block != null && !(else_block instanceof EmptyStmt)){ //GotoInstruction gotoInstruction = new GOTO(null); //il.append(gotoInstruction); ifInstruction.setTarget(il.append(else_block.genByteCode(_cg, rs))); //gotoInstruction.setTarget(il.append(new NOP())); }else{ ifInstruction.setTarget(il.append(new NOP())); } return il; } } // ino.end