JavaPatternMatching/src/mycompiler/mystatement/LocalVarDecl.java

642 lines
22 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;
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
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.JavaCodeResult;
import typinferenz.ResultSet;
import typinferenz.TypeAssumptions;
// 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.sc_check.25584.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.25584.body
{
parserlog.debug("SC -> Blockhashtab. vor LocalVarDecl.sc_check: " + bh);
DeclId hilf;
ExprStmt hilf2;
for(Enumeration<DeclId> e1=declid.elements(); e1.hasMoreElements();)
{
hilf=e1.nextElement();
//************* Hashtabelle f<>r die LocalVar wird erzeugt
if(hilf.paratyp != null)
{
this.paralist = hilf.get_Paratyp();
parserlog.debug("++++ localvardecl.declid.paralist:");
parserlog.debug(string_rec(this.paralist));
for(Enumeration e11=this.paralist.elements();e11.hasMoreElements();){
Type t = (Type)e11.nextElement();
try{
is_declared(t,classname);
}
catch(SCStatementException ex){
throw ex;
}
}
try{
check_anz(this.getDeclType(),paralist,classname);
}
catch(SCStatementException ex){
throw ex;
}
// Erstellen der Hashtabelle f<>r die Instaz der Klasse mit den zugewiesesen Parameter-Variablen
for(Enumeration<Class> e=classname.elements();e.hasMoreElements();){
Class cl = e.nextElement();
if(this.getDeclType().getName().equals(cl.getName())){
if(ext){
parserlog.debug("++ Klasse verifiziert, parahash und paralist: "+cl.get_ParaHash().toString()+" ");
parserlog.debug(string_rec(this.paralist));
}
String para,var;
Type typ;
Hashtable<String,Type> ph = new Hashtable<String,Type>();
Hashtable<String,String> clph = cl.get_ParaHash();
// System.out.println("*********clph.size() = "+ clph.size()+" paralist.size()= " +paralist.size());
if(clph.size() != paralist.size()){
SCExcept exc = new SCExcept();
exc.set_error("Anzahl der Parameter bei der Instantiierung von \""+ hilf.get_Name() +"\" stimmt nicht!");
exc.set_statement("LocalVarDecl");
SCStatementException ex = new SCStatementException();
ex.addException(exc);
throw ex;
}
//Hashtabelle in Class mu<6D> die gleiche Anzahl an Elementen haben, wie die Parameterliste!! ** noch Abbruchbedingung impl.**
for(Enumeration en = clph.keys();en.hasMoreElements();){
var = (String)en.nextElement();
para= (String)cl.get_ParaHash().get(var); // sucht die Parametervariable f<>r die Variable aus der Parahash-Tabelle...
parserlog.debug("var: "+var+" para: "+para);
//for(Enumeration e3=this.paralist.elements() ;e3.hasMoreElements();){
for(Enumeration e2=cl.get_ParaList().elements(),e3=this.paralist.elements();e2.hasMoreElements();){
Type t1 = (Type)e2.nextElement();
Type t2 = (Type)e3.nextElement();
if(t1.getName().equals(para)){
typ = t2; // bestimmt die neue Zuordnung an der entsprechenden Stelle
ph.put(var,typ); // f<>gt Variable und neuer Typ in die Hashtabelle ein
// System.out.println("++ typ "+typ.get_Type());
}
}
}
this.parahash = ph;
parabh.put(hilf.get_Name(),ph);
if(ext){
parserlog.debug("++ ParaHashtabelle der InstVar: "+this.parahash.toString());
parserlog.debug("++ parach der InstVar: ");
parserlog.debug(string_rec(parach));
parserlog.debug("++ parabh der InstVar: ");
parserlog.debug(string_rec(parabh));
parserlog.debug(string_rec("paralist: ",paralist));
}
}
}
}
//**********************************
bh.put(hilf.get_Name(), this.getDeclType().getName());
hilf2=hilf.get_Wert();
if(hilf2!=null)
{
try
{
hilf2.sc_check(classname,ch,bh,ext, parach, parabh);
}
catch (SCStatementException ex)
{
throw ex;
}
if(!hilf2.getTypeName().equals(this.getDeclType().getName()))
{
if(this.getDeclType().getName().equals("int"))
{
if(hilf2.getTypeName().equals("char"))
{
parserlog.error("ACHTUNG! Hier wird einem int ein char zugewiesen!");
parserlog.error("int "+get_Name());
}
}
else
{
parserlog.error("Falscher Typ wird der neu deklarierten Variable zugewiesen. ---> SCWrongTypeAssignError");
SCExcept exc = new SCExcept();
exc.set_error("Der neu deklarierten Variable "+hilf.get_Name()+" vom Typ "+this.getDeclType().getName()+" soll ein Objekt vom Typ "+hilf2.getTypeName()+" zugewiesen werden. ---> SCWrongTypeAssignError :-)");
exc.set_statement("LocalVarDecl");
SCStatementException ex = new SCStatementException();
ex.addException(exc);
throw ex;
}
}
}
}
parserlog.debug( "SC -> Blockhashtab. nach LocalVarDecl.sc_check: " + bh);
}
// 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.getTypeVariable())){
inferencelog.debug("Ersetze Typ in LocalVarDecl \""+this.get_Name()+"\"\n");
if(getTypeVariable() instanceof TypePlaceholder){
((TypePlaceholder)getTypeVariable()).removeReplacementListener(this);
}
this.setTypeVariable(e.getNewType());
}
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.setTypeVariable(new Void(0)); //Return typ einer Variablendeklaration ist Void
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