JavaPatternMatching/src/mycompiler/mystatement/Block.java

473 lines
16 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.Block.8625.package
package mycompiler.mystatement;
// ino.end
// ino.module.Block.8625.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.myclass.Class;
import mycompiler.myexception.CTypeReconstructionException;
import mycompiler.myexception.JVMCodeException;
import mycompiler.myexception.SCExcept;
import mycompiler.myexception.SCStatementException;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytype.Void;
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 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;
import typinferenz.TypinferenzException;
// ino.class.Block.25037.declaration
public class Block extends Statement
// ino.end
// ino.class.Block.25037.body
{
// ino.method.Block.25041.definition
public Block()
// ino.end
// ino.method.Block.25041.body
{
super(-1,-1);
}
// ino.end
// ino.attribute.parserlog.25044.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.attribute.statements.25047.declaration
public Vector<Statement> statements = new Vector<Statement>();
// ino.end
// ino.attribute.HashTabelleKlasse.25050.declaration
private Hashtable HashTabelleKlasse;
// ino.end
// ino.attribute.HashTabelleBlock.25053.declaration
private Hashtable<String,String> HashTabelleBlock;
// ino.end
// ino.attribute.block_para.25056.declaration
private Hashtable<String,Hashtable> block_para;
// ino.end
//private String sc_meth_ret_type;
// ino.attribute.inferencelog.25059.decldescription type=javadoc
/**
* Logger: log4j
*/
// ino.end
// ino.attribute.inferencelog.25059.declaration
protected static Logger inferencelog = Logger.getLogger("inference");
// ino.end
protected static Logger typinferenceLog = Logger.getLogger("Typeinference");
// ino.method.sc_check.25062.definition
public 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.25062.body
{
SCStatementException except = null;
SCExcept hilfex;
Statement tempStatement;
Vector<SCExcept> v;
this.HashTabelleKlasse = ch;
this.HashTabelleBlock = (Hashtable) bh.clone();
this.block_para = (Hashtable) parabh.clone();
// otth: Alle Statements durchgehen und von allen Statements den Semantik-Check machen
parserlog.debug( "Statements (Anzahl): " + statements.size());
parserlog.debug( "Statements: " + statements);
for( Enumeration<Statement> el = this.statements.elements(); el.hasMoreElements(); )
{
tempStatement = el.nextElement();
try
{
// Semantik-Check
tempStatement.sc_check( classname, HashTabelleKlasse, HashTabelleBlock, ext, parach, block_para );
}
catch (SCStatementException ex)
{
// Fehler abfangen
if( except==null )
except=new SCStatementException();
v = ex.get_exlist();
for( Enumeration<SCExcept> el1=v.elements();el1.hasMoreElements(); )
{
hilfex=el1.nextElement();
except.addException(hilfex);
}
}
}
if(except!=null)
{
throw except;
}
parserlog.debug("SC -> SC f<>r Block war erfoglreich!");
parserlog.debug("SC -> Blockhashtabelle: " + HashTabelleBlock);
}
// ino.end
// ino.method.get_Statement.25065.definition
public Vector<Statement> get_Statement()
// ino.end
// ino.method.get_Statement.25065.body
{
return statements;
}
// ino.end
// ino.method.set_Statement.25068.definition
public void set_Statement(Statement s)
// ino.end
// ino.method.set_Statement.25068.body
{
statements.addElement(s);
}
// ino.end
// ino.method.set_Statement_Vector.25071.definition
public void set_Statement_Vector(Vector<Statement> v)
// ino.end
// ino.method.set_Statement_Vector.25071.body
{
statements = v;
}
// ino.end
// ino.method.codegen.25074.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.25074.body
{
for(int i = 0; i < statements.size(); i++)
{
statements.elementAt(i).codegen(classfile, code, paralist);
}
}
// ino.end
/////////////////////////////////////////////////////////////////////////
// TypeReconstructionAlgorithmus
/////////////////////////////////////////////////////////////////////////
// ino.method.TRStatement.25077.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.20 von Martin Pl<EFBFBD>micke
* <br>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param sigma
* @param V
* @param supportData
* @return
* @throws CTypeReconstructionException
*/
// ino.end
// ino.method.TRStatement.25077.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
throws CTypeReconstructionException
// ino.end
// ino.method.TRStatement.25077.body
{
supportData.incBlockDepth();
CTypeAssumptionSet V_Start = V.shallowCopy();
CTripleSet returnSet = this.TRStatements(sigma, V, statements, 0, V_Start, supportData);
supportData.decBlockDepth();
return returnSet;
}
// ino.end
// ino.method.TRStatements.25080.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.19 von Martin Pl<EFBFBD>micke
* <br/>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param sigma
* @param V
* @param statementList
* @param index
* @param V_start
* @param supportData
* @return
* @throws CTypeReconstructionException
*/
// ino.end
// ino.method.TRStatements.25080.definition
public CTripleSet TRStatements(CSubstitutionSet sigma, CTypeAssumptionSet V, Vector<Statement> statementList, int index, CTypeAssumptionSet V_start, CSupportData supportData)
throws CTypeReconstructionException
// ino.end
// ino.method.TRStatements.25080.body
{
CTripleSet returnSet = new CTripleSet();
////////////////////
// Leere Statementsliste:
////////////////////
if(index>statements.size()-1){
// HOTI Bei leeren Bl<42>cken sollte dann schon aber auch was zur<75>ck gegeben werden
if(statements.size()==0){
returnSet.addElement(new CTriple(sigma, new Void(this.getOffset()), V));
}
return returnSet;
}
////////////////////
// Letztes Statement erreicht (Rekursionsanker):
////////////////////
if(index==statements.size()-1){
// TRStatement f<>r aktuelles Statement rufen:
Statement stmnt = statementList.elementAt(index);
returnSet = stmnt.TRStatement(sigma, V, supportData);
inferencelog.debug(stmnt.toString());
// Das L<>schen von Typannahmen f<>r lokale Variablen ist bei dieser Implementierung nicht notwendig.
// Im Gegenteil: Die lokalen Variablen m<>ssen sogar erhalten bleiben.
}
////////////////////
// Statementliste rekursiv durcharbeiten:
////////////////////
else{
// TRStatement f<>r aktuelles Statement rufen:
Statement stmnt = statementList.elementAt(index);
//setze Block f<>r Offset-Setzung f<>r lokale Variablen
if(stmnt instanceof LocalVarDecl)//hinzugef<65>gt hoth: 07.04.2006
{
LocalVarDecl lokaleVar = (LocalVarDecl)stmnt;
lokaleVar.block=this;
}
CTripleSet set = stmnt.TRStatement(sigma, V, supportData);
// Alle m<>glichen Kombinationen herstellen:
Iterator<CTriple> it = set.getIterator();
Vector<CTypeReconstructionException> exceptions=new Vector<CTypeReconstructionException>();
int successfulls=0;
while(it.hasNext()){
CTriple triple = it.next();
if (it.hasNext()) {
inferencelog.debug(stmnt.toString());
}
// Rekursionsaufruf:
if(triple.getResultType() instanceof Void){
// SupportData muss geklont werden, damit BlockID nicht mehrmals hochgez<65>hlt wird...
try{
CTripleSet subset = this.TRStatements(triple.getSubstitutions(),triple.getAssumptionSet(),statementList,index+1,V_start,supportData.clone());
// Rekursions-R<>ckgabewert mit aktueller Menge vereinigen:
returnSet.unite(subset);
successfulls++;
}catch(CTypeReconstructionException tre){
exceptions.addElement(tre);
}
}
else {
throw new CTypeReconstructionException("Block.TRStatements(): Unerreichbare Anweisung",this);
}
}
if(successfulls==0){
if(exceptions.size()==1){
throw exceptions.elementAt(0);
}
throw new CTypeReconstructionException("Block: Es konnte keine Assumption gefunden werden, die auf die Anforderung passt.",exceptions,this);
}
}
return returnSet;
}
// ino.end
// ino.method.toString.25083.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<EFBFBD>micke
* @return
*/
// ino.end
// ino.method.toString.25083.definition
public String toString()
// ino.end
// ino.method.toString.25083.body
{
return this.type + " { " + statements.toString();
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25086.defdescription type=javadoc
/**
* In allen lokalen Variablendeklarationen die "falschen" RefTypes ersetzen
* @param paralist
* @param genericMethodParameters
*/
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25086.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25086.body
{
if(statements==null)
return;
for(int i=0;i<statements.size();i++){
statements.elementAt(i).wandleRefTypeAttributes2GenericAttributes(paralist,genericMethodParameters);
}
}
// ino.end
public void addOffsetsToAssumption(CTypeAssumption localAssumption, String NameVariable,boolean isMemberVariable)
{
for(Object vectorObjekt : this.statements) //durchlaufe alle Statements dieses Blocks
{
if(vectorObjekt instanceof Block) //Bei Block
{
Block b = (Block)vectorObjekt;
b.addOffsetsToAssumption(localAssumption,NameVariable,isMemberVariable);//rekursiver Aufruf
}
else
{
String Name_Superklasse = vectorObjekt.getClass().getSuperclass().getSimpleName();
if(Name_Superklasse.equals("Statement")) //Bei Statement
{
Statement s = (Statement)vectorObjekt;
try{
if(s.addOffsetsToStatement(localAssumption,NameVariable,isMemberVariable)==false)
{break;}}
catch(NullPointerException NPE){}
}
else if(Name_Superklasse.equals("Expr") || Name_Superklasse.equals("BinaryExpr") || Name_Superklasse.equals("UnaryExpr")) //Bei Expression
{
Expr e = (Expr)vectorObjekt;
try{
e.addOffsetsToExpression(localAssumption,NameVariable,isMemberVariable);}
catch(NullPointerException NPE){}
}
}
}
}
public boolean addOffsetsToStatement(CTypeAssumption localAssumption, String NameVariable, boolean isMemberVariable)
{
addOffsetsToAssumption(localAssumption,NameVariable,isMemberVariable);
return true;
}
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
/* this.setTypeVariable(TypePlaceholder.fresh(this)); */
for(Statement stmt : statements){
typinferenceLog.debug("Prozessing statement: "+stmt);
ret.add(stmt.TYPEStmt(assumptions));
/* if((stmt instanceof Return)){
ret.add(new Constraint(stmt.getTypeVariable(), this.getTypeVariable()));//TODO: Dies nochmal prüfen.
}
*/
}
if(statements.size()>0){
Statement stmt = statements.elementAt(statements.size()-1);
typinferenceLog.debug("Prozessing statement: "+stmt);
this.setTypeVariable(stmt.getTypeVariable());
for(int i= statements.size()-2; i >= 0; i--) {
stmt = statements.elementAt(i);
typinferenceLog.debug("Prozessing statement: "+stmt);
if (!(stmt.getTypeVariable() instanceof Void))
if (this.getTypeVariable() instanceof Void) {
//this.setTypeVariable(stmt.getTypeVariable());
throw new TypinferenzException("Falscher Return Type");
}
else {
TypePlaceholder tph = TypePlaceholder.fresh();
ret.add(new SingleConstraint(this.getTypeVariable(), tph));
ret.add(new SingleConstraint(stmt.getTypeVariable(), tph));
this.setTypeVariable(tph);
}
}
if (this.type instanceof TypePlaceholder) {
((TypePlaceholder)this.type).addReplacementListener(this);
}
}else{
this.setType(new Void(0));
}
return ret;
}
/*
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
if(this.getTypeVariable()==null)this.setTypeVariable(TypePlaceholder.fresh(this));
for(Statement stmt : statements){
typinferenceLog.debug("Prozessing statement: "+stmt);
ret.add(stmt.TYPEStmt(assumptions));
if((stmt instanceof Return)){
ret.add(new Constraint(stmt.getTypeVariable(), this.getTypeVariable()));//TODO: Dies nochmal pr<70>fen.
}
}
return ret;
}
*/
public void replaceType(CReplaceTypeEvent e) {
super.replaceType(e);
}
@Override
public String getTypeInformation(){
String ret = "\n";
for(Statement s : this.get_Statement()){
ret += s.getTypeInformation()+"\n";
}
return ret;
}
public int getTypeLineNumber() {
throw new NotImplementedException();
}
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult().attach("{\n");
for(Statement stmt : this.get_Statement()){
ret.attach(stmt.printJavaCode(resultSet));
ret.attach((stmt instanceof ExprStmt ? ";" : "") + "\n");
}
return ret.attach("}");
}
}
// ino.end