JavaPatternMatching/src/mycompiler/mystatement/LocalVarDecl.java

506 lines
16 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.LocalVarDecl.8638.package
package mycompiler.mystatement;
// ino.end
// ino.module.LocalVarDecl.8638.import
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
2013-10-18 11:33:46 +00:00
import mycompiler.mybytecode.ClassFile;
import mycompiler.mybytecode.CodeAttribute;
import mycompiler.myclass.Class;
import mycompiler.myclass.ClassHelper;
import mycompiler.myclass.DeclId;
import mycompiler.MyCompiler;
import mycompiler.myexception.JVMCodeException;
import mycompiler.myexception.SCExcept;
import mycompiler.myexception.SCStatementException;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.RefType;
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.replacementlistener.ITypeReplacementListener;
import mycompiler.mytypereconstruction.set.CSubstitutionSet;
import mycompiler.mytypereconstruction.set.CTripleSet;
import mycompiler.mytypereconstruction.set.CTypeAssumptionSet;
import mycompiler.mytypereconstruction.typeassumption.CLocalVarTypeAssumption;
import mycompiler.mytypereconstruction.typeassumption.CTypeAssumption;
import org.apache.log4j.Logger;
// ino.end
2013-10-18 11:33:46 +00:00
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.JavaCodeResult;
import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions;
2013-10-18 11:33:46 +00:00
// ino.class.LocalVarDecl.25540.declaration
public class LocalVarDecl extends Statement implements ITypeReplacementListener
// ino.end
// ino.class.LocalVarDecl.25540.body
{
// ino.method.LocalVarDecl.25545.definition
public LocalVarDecl(int offset,int variableLength)
// ino.end
// ino.method.LocalVarDecl.25545.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.declid.25548.declaration
private Vector<DeclId> declid = new Vector<DeclId>();
// ino.end
// ino.attribute.type.25551.declaration
private Type declType;
// ino.end
// ino.attribute.paralist.25554.declaration
private Vector paralist = null;
// ino.end
// ino.attribute.parahash.25557.declaration
private Hashtable<String,Type> parahash = null;
// ino.end
// ino.attribute.inferencelog.25560.declaration
protected static Logger inferencelog = Logger.getLogger("inference");
// ino.end
// ino.attribute.parserlog.25563.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.attribute.block.25566.declaration
public Block block;
// ino.end
// ino.method.setType.25569.definition
public void setDeclType(Type t)
// ino.end
// ino.method.setType.25569.body
{
if(this.declType instanceof TypePlaceholder){
((TypePlaceholder)this.declType).removeReplacementListener(this);
}
if(t instanceof TypePlaceholder){
((TypePlaceholder)t).addReplacementListener(this);
}
this.declType=t;
}
// ino.end
// ino.method.getType.25572.definition
public Type getDeclType()
// ino.end
// ino.method.getType.25572.body
{
return this.declType;
}
// ino.end
// ino.method.set_DeclId.25575.definition
public void set_DeclId(DeclId did)
// ino.end
// ino.method.set_DeclId.25575.body
{
this.declid.addElement(did);
}
// ino.end
// ino.method.get_Name.25578.definition
public String get_Name()
// ino.end
// ino.method.get_Name.25578.body
{
DeclId hilf;
hilf=declid.elementAt(0);
return hilf.get_Name();
}
// ino.end
// ino.method.set_ParaList.25581.definition
public void set_ParaList(Vector pl)
// ino.end
// ino.method.set_ParaList.25581.body
{
this.paralist = pl;
}
// ino.end
// ino.method.is_declared.25587.definition
public void is_declared(Type t, Vector<Class> classlist)
throws SCStatementException
// ino.end
// ino.method.is_declared.25587.body
{
boolean flag=false;
for(Enumeration<Class> e = classlist.elements();e.hasMoreElements();){
flag = false;
Class c = e.nextElement();
// System.out.println("is_init: vergleiche "+t.get_Type_()+" mit "+c.get_classname());
if(c.getName().equals(t.getName())){
// System.out.println("Klasse "+t.get_Type()+" im Vector classlist gefunden.");
flag = true;
break;
}
}
if ( t instanceof RefType )
{
if(((RefType)t).get_ParaList()!=null)
{
if( ((RefType)t).get_ParaList().size()>0)
{
for(Enumeration e1 = ((RefType)t).get_ParaList().elements();e1.hasMoreElements();)
{
try
{
is_declared((Type)e1.nextElement(),classlist);
}
catch(SCStatementException ex)
{
throw ex;
}
}
}
}
}
if(!flag)
{
SCStatementException ex = new SCStatementException();
SCExcept e = new SCExcept();
e.set_error("unbekannte Klasse "+t.getName()+".");
e.set_function("complete_parahashtable() --> is_declared()");
e.set_statement(t.getName());
ex.addException(e);
throw ex;
}
}
// ino.end
// ino.method.check_anz.25590.definition
public void check_anz(Type type, Vector paralist, Vector<Class> classlist)
throws SCStatementException
// ino.end
// ino.method.check_anz.25590.body
{
boolean flag = false;
for(Enumeration<Class> e2 = classlist.elements();e2.hasMoreElements();){
Class c = e2.nextElement();
// System.out.println("vergleiche "+type.get_Type_()+" mit "+c.getName());
if(c.getName().equals(type.getName())){
// System.out.println("if(c.getName().equals(type.get_Type())){");
if(c.get_ParaList().size()>0 && paralist!=null){
parserlog.debug("Type "+type.getName()+" in classlist gefunden!");
parserlog.debug("Anzahl der localvar.paralist:"+ paralist.size()+" class.paralist: (soll) "+c.get_ParaList().size());
if(paralist.size()==c.get_ParaList().size()){
for(Enumeration e3=paralist.elements();e3.hasMoreElements();){
Type t = (Type)e3.nextElement();
parserlog.debug("Type "+t.getName()+" wird gerade untersucht");
try
{
if ( t instanceof RefType )
{
check_anz(t, ((RefType)t).get_ParaList(),classlist);
}
}
catch(SCStatementException ex)
{
throw ex;
}
}
flag = true;
parserlog.debug("flag gesetzt");
break;
}
else{
SCStatementException ex = new SCStatementException();
SCExcept e = new SCExcept();
e.set_error("Klasse "+c.getName()+" ist falsch parametrisiert!");
e.set_function("complete_parahashtable() --> check_anz()");
e.set_statement(type.getName());
ex.addException(e);
throw ex;
}
}
else if(c.get_ParaList().size()==0 && paralist!=null){
SCStatementException ex = new SCStatementException();
SCExcept e = new SCExcept();
e.set_error("Klasse "+c.getName()+" ist nicht parametrisierbar!");
e.set_function("complete_parahashtable() --> check_anz()");
e.set_statement(type.getName());
ex.addException(e);
throw ex;
}
else if(paralist==null && c.get_ParaList().size()>0){
parserlog.debug(paralist);
parserlog.debug(c.getName()+" "+c.get_ParaList().size());
SCStatementException ex = new SCStatementException();
SCExcept e = new SCExcept();
e.set_error("Klasse "+c.getName()+" <20>berhaupt garnicht parametrisiert!");
e.set_function("complete_parahashtable() --> check_anz()");
e.set_statement(type.getName());
ex.addException(e);
throw ex;
}
else{
flag = true;
parserlog.debug(type.getName()+" nicht parametrisierbar");
break;
}
}
}
if(!flag)
{
SCStatementException ex = new SCStatementException();
SCExcept e = new SCExcept();
if ( type instanceof RefType )
{
e.set_error("Type " + type.getName()+ " falsche Parameter-Anzahl " + ((RefType)type).get_ParaList().size());
}
e.set_function("complete_parahashtable() --> check_anz()");
e.set_statement(type.getName());
ex.addException(e);
throw ex;
}
}
// ino.end
// ino.method.codegen.25593.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.25593.body
{
if(declid != null)
for(int i = 0; i < declid.size(); i++)
declid.elementAt(i).codegen_LocalVarDecl(classfile, code, this.getDeclType(), paralist);
}
// ino.end
// ino.method.getDeclidVector.25596.definition
public Vector<DeclId> getDeclidVector()
// ino.end
// ino.method.getDeclidVector.25596.body
{
return declid;
}
// ino.end
// ino.method.setDeclidVector.25599.definition
public void setDeclidVector(Vector<DeclId> declid)
// ino.end
// ino.method.setDeclidVector.25599.body
{
this.declid = declid;
}
// ino.end
// ino.method.getLineNumber.25602.defdescription type=javadoc
/**
* <br/>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @return
*/
// ino.end
// ino.method.getLineNumber.25602.definition
public int getLineNumber()
// ino.end
// ino.method.getLineNumber.25602.body
{
if(declid.size()<=0)
return MyCompiler.NO_LINENUMBER;
else{
return declid.elementAt(0).getLineNumber();
}
}
// ino.end
// ino.method.getOffset.25605.defdescription type=javadoc
/**
* <br/>Author: Thomas Hornberger
* @return
*/
// ino.end
// ino.method.getOffset.25605.definition
public int getOffset()
// ino.end
// ino.method.getOffset.25605.body
{
if(declid.size()<=0)
return MyCompiler.NO_LINENUMBER;
else{
return declid.elementAt(0).getOffset();
}
}
// ino.end
// ino.method.replaceType.25608.defdescription type=javadoc
/**
* <br>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param e
*/
// ino.end
// ino.method.replaceType.25608.definition
public void replaceType(CReplaceTypeEvent e)
// ino.end
// ino.method.replaceType.25608.body
{
if(e.getOldType().equals(this.getType())){
2013-10-18 11:33:46 +00:00
inferencelog.debug("Ersetze Typ in LocalVarDecl \""+this.get_Name()+"\"\n");
if(getType() instanceof TypePlaceholder){
((TypePlaceholder)getType()).removeReplacementListener(this);
2013-10-18 11:33:46 +00:00
}
this.setType(e.getNewType());
2013-10-18 11:33:46 +00:00
}
if(e.getOldType().equals(this.getDeclType())){
inferencelog.debug("Ersetze Typ in LocalVarDecl \""+this.get_Name()+"\"\n");
if(declType instanceof TypePlaceholder){
((TypePlaceholder)declType).removeReplacementListener(this);
}
this.setDeclType(e.getNewType());
}
}
// ino.end
// ino.method.getTypeLineNumber.25611.defdescription type=javadoc
/**
* <br>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @return
*/
// ino.end
// ino.method.getTypeLineNumber.25611.definition
public int getTypeLineNumber()
// ino.end
// ino.method.getTypeLineNumber.25611.body
{
return this.getLineNumber();
}
// ino.end
// ino.method.TRStatement.25614.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.24 von Martin Pl<EFBFBD>micke
* <br>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param sigma
* @param V
* @param supportData
* @return
*/
// ino.end
// ino.method.TRStatement.25614.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRStatement.25614.body
{
CTripleSet resultSet = new CTripleSet();
V = V.shallowCopy();
CTypeAssumptionSet localSet = new CTypeAssumptionSet();
CLocalVarTypeAssumption varAssum = new CLocalVarTypeAssumption(supportData.getCurrentClass(), supportData.getCurrentMethod(), supportData.getCurrentMethodParaCount(), supportData.getCurrentMethodOverloadedID(), supportData.getCurrentBlockId(), this.get_Name() ,this.getDeclType(), this.getLineNumber(),this.getOffset(),new Vector<Integer>());
Class.isFirstLocalVarDecl=true;
if(this.block != null)
this.block.addOffsetsToAssumption(varAssum,this.get_Name(),false);//hinzugef<65>gt hoth: 07.04.2006
localSet.addElement(varAssum);
V.unite(localSet);
resultSet.addElement(new CTriple(sigma, new Void(getOffset()), V));
return resultSet;
}
// ino.end
// ino.method.toString.25617.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<EFBFBD>micke
* @return
*/
// ino.end
// ino.method.toString.25617.definition
public String toString()
// ino.end
// ino.method.toString.25617.body
{
if(declType == null)return "no type " + declid.toString();
return declType.toString() + " " + declid.toString();
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25620.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25620.body
{
Type fpType=getDeclType();
// Nur wenn es sich um ein RefType-Field handelt
GenericTypeVar pendantPara=ClassHelper.findGenericType(fpType,paralist,genericMethodParameters);
if(pendantPara!=null){ //Wenn generisch, dann modifizieren
setType(pendantPara);
}
}
// ino.end
public boolean addOffsetsToStatement(CTypeAssumption localAssumption, String NameVariable, boolean isMemberVariable)
{
isMemberVariable=true;//hoth 02.05.06
if(isMemberVariable)//Wenn Objektvariable
{
if(this.get_Name().equals(NameVariable))
{
if(Class.isFirstLocalVarDecl==false)
{return false;}//Wenn jetzt lokale Variable kommt, dann springe raus
else
{Class.isFirstLocalVarDecl=false;}
}
}
return true;
}
/**
* @author Andreas Stadelmeier, a10023
* Der Typ der Variablendeklaration wird den Assumptions angef<EFBFBD>gt.
* Bei einer Deklaration ohne Typangabe wird ein TypePlaceholder den Assumptions hinzugef<EFBFBD>gt.
*/
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
if(this.getDeclType()==null || this.getDeclType() instanceof TypePlaceholder)this.setDeclType(TypePlaceholder.fresh(this));
assumptions.addInstVarAssumption(assumptions.getThisValue().getName(), this.get_Name(), this.getDeclType());
//assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden.
this.setType(new Void(0)); //Return typ einer Variablendeklaration ist Void
2013-10-18 11:33:46 +00:00
return ret;
}
@Override
public String getTypeInformation(){
String ret = "VarDeclaration ";
if(this.getDeclType()!=null)ret+= this.getDeclType().toString()+" ";
ret+=this.get_Name();
return ret;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult().attach(getDeclType().printJavaCode(resultSet)) .attach( " "+this.get_Name()+";");
}
}
// ino.end