JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java

539 lines
17 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.LocalVarDecl.8638.package
2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree.statement;
2013-10-18 11:33:46 +00:00
// 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.myexception.JVMCodeException;
import mycompiler.myexception.SCExcept;
import mycompiler.myexception.SCStatementException;
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
2014-02-19 04:20:54 +00:00
2014-02-22 03:58:49 +00:00
2014-04-15 12:56:20 +00:00
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.ClassHelper;
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
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.FreshTypeVariable;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.assumptions.LocalVarAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;
2013-10-18 11:33:46 +00:00
// ino.class.LocalVarDecl.25540.declaration
2014-02-19 04:20:54 +00:00
public class LocalVarDecl extends Statement implements TypeInsertable
2013-10-18 11:33:46 +00:00
// 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.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.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.getType(), paralist);
2013-10-18 11:33:46 +00:00
}
// 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.getType())){
2013-10-18 11:33:46 +00:00
inferencelog.debug("Ersetze Typ in LocalVarDecl \""+this.get_Name()+"\"\n");
2014-03-20 01:42:44 +00:00
if(this.getType() instanceof TypePlaceholder){
((TypePlaceholder)this.getType()).removeReplacementListener(this);
2013-10-18 11:33:46 +00:00
}
this.setType(e.getNewType());
2013-10-18 11:33:46 +00:00
}
}
// 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.getType(), this.getLineNumber(),this.getOffset(),new Vector<Integer>());
2013-10-18 11:33:46 +00:00
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
{
2014-03-20 01:42:44 +00:00
if(this.getType() == null)return "no type " + declid.toString();
return this.getType().toString() + " " + declid.toString();
2013-10-18 11:33:46 +00:00
}
// 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=getType();
2013-10-18 11:33:46 +00:00
// Nur wenn es sich um ein RefType-Field handelt
Type pendantPara = null;
if(fpType instanceof RefType)
pendantPara = ((RefType)fpType).findGenericType(paralist, new Vector<GenericTypeVar>());
//GenericTypeVar pendantPara=ClassHelper.findGenericType(fpType,paralist,genericMethodParameters);
2013-10-18 11:33:46 +00:00
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();
2014-04-15 12:56:20 +00:00
if((this.getType() instanceof RefType)){
Type replaceType = null;
2014-08-11 12:35:33 +00:00
replaceType = assumptions.getTypeFor((RefType)this.getType(), this);
if(replaceType == null)
throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
2014-04-15 12:56:20 +00:00
this.setType(replaceType);
}
assumptions.addAssumption(new LocalVarAssumption(this, this.getType())); //Bevor der Typ auf Void gesetzt wird.
ret.add(new SingleConstraint(this.getType(), this.getType()));
2013-10-18 11:33:46 +00:00
//assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden.
this.setReturnType(new Void(0)); //Return typ einer Variablendeklaration ist Void
2013-10-18 11:33:46 +00:00
return ret;
}
2014-04-03 08:35:25 +00:00
@Override
public void parserPostProcessing(SyntaxTreeNode parent) {
super.parserPostProcessing(parent);
if(this.getType()==null || this.getType() instanceof TypePlaceholder)this.setType(TypePlaceholder.fresh(this));
}
2013-10-18 11:33:46 +00:00
@Override
public String getTypeInformation(){
String ret = "VarDeclaration ";
if(this.getType()!=null)ret+= this.getType().toString()+" ";
2013-10-18 11:33:46 +00:00
ret+=this.get_Name();
return ret;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
2014-04-23 15:59:39 +00:00
JavaCodeResult ret = new JavaCodeResult();
if(this.getType()!=null)ret.attach(getType().printJavaCode(resultSet)).attach(" ");
ret.attach(this.get_Name()+";");
return ret;}
2014-02-19 04:20:54 +00:00
@Override
public void setOffset(int offset) {
this.offset = offset;
}
@Override
public String getIdentifier() {
return this.get_Name();
}
2013-10-18 11:33:46 +00:00
2014-02-22 03:58:49 +00:00
@Override
public Vector<SyntaxTreeNode> getChildren() {
Vector<SyntaxTreeNode> ret = new Vector<SyntaxTreeNode>();
return ret;
}
@Override
public TypeInsertPoint createTypeInsertPoint(TypePlaceholder tph,
ResultSet resultSet) {
return new TypeInsertPoint(this, this, resultSet.getTypeEqualTo(tph),resultSet);
}
2013-10-18 11:33:46 +00:00
}
// ino.end