JavaPatternMatching/src/mycompiler/mystatement/Assign.java

502 lines
18 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.Assign.8622.package
package mycompiler.mystatement;
// ino.end
// ino.module.Assign.8622.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.mybytecode.JVMCode;
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.Pair;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytype.Void;
import mycompiler.mytypereconstruction.CMultiplyTuple;
import mycompiler.mytypereconstruction.CSupportData;
import mycompiler.mytypereconstruction.CTriple;
import mycompiler.mytypereconstruction.set.CMultiplyTupleSet;
import mycompiler.mytypereconstruction.set.CSubstitutionSet;
import mycompiler.mytypereconstruction.set.CTripleSet;
import mycompiler.mytypereconstruction.set.CTypeAssumptionSet;
import mycompiler.mytypereconstruction.typeassumption.CTypeAssumption;
import mycompiler.mytypereconstruction.unify.Unify;
import org.apache.log4j.Logger;
// ino.end
import typinferenz.JavaCodeResult;
import typinferenz.SingleConstraint;
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.ResultSet;
import typinferenz.TypeAssumptions;
// ino.class.Assign.24926.declaration
public class Assign extends Expr
// ino.end
// ino.class.Assign.24926.body
{
// ino.method.Assign.24930.definition
public Assign(int offset,int variableLength)
// ino.end
// ino.method.Assign.24930.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.expr1.24933.declaration
public Expr expr1;
// ino.end
// ino.attribute.expr2.24936.declaration
public Expr expr2;
// ino.end
// ino.attribute.parserlog.24939.decldescription type=javadoc
/**
* Logger log4j
*/
// ino.end
// ino.attribute.parserlog.24939.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.set_Expr.24942.definition
public void set_Expr(Expr expr1,Expr expr2)
// ino.end
// ino.method.set_Expr.24942.body
{
this.expr1 = expr1;
this.expr2 = expr2;
}
// ino.end
// ino.method.sc_check.24945.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.24945.body
{
SCStatementException except = null;
try
{
expr1.sc_check(classname, ch, bh, ext, parach, parabh);
}
catch(SCStatementException ex)
{
except=ex;
}
try
{
expr2.sc_check(classname, ch, bh, ext, parach, parabh);
}
catch(SCStatementException ex)
{
if(except==null)
{
except=ex;
}
else
{
Vector<SCExcept> v = ex.get_exlist();
SCExcept hilf;
for(Enumeration<SCExcept> el=v.elements() ; el.hasMoreElements() ; )
{
hilf = el.nextElement();
except.addException(hilf);
}
}
}
expr1.getTypeName();
expr2.getTypeName();
parserlog.debug( "SC -> Expression linker Typ: " + expr1.getTypeName());
parserlog.debug( "SC -> Expression rechter Typ: " + expr1.getTypeName());
if( expr1.getTypeName() == null || expr2.getTypeName() == null )
{
SCExcept ex = new SCExcept();
ex.set_error("Eine Variable ist ohne gesetzten Typ.");
ex.set_statement("Assign");
if( except == null )
{
except=new SCStatementException();
}
except.addException(ex);
}
else
{
if(expr2.getTypeName().equals("__NULL__"))
{
if(ext)
{
parserlog.debug("Assign setzt Variable auf null");
}
}
else
{
if(!expr1.getTypeName().equals(expr2.getTypeName()))
{
// Linker Typ = Rechter Typ
parserlog.debug( "SC -> Linker Typ = Rechter Typ");
if(expr1.getTypeName().equals("int")&&expr2.getTypeName().equals("char"))
{
parserlog.info("ACHTUNG! Hier wird einem int ein char zugewiesen.");
if(expr1.get_UsedId()==null)
{
parserlog.error("UsedId geht net");
}
parserlog.debug("int "+expr1.get_UsedId().get_Name().toString()+" = char "+expr2.get_UsedId().get_Name().toString());
}
else
{
if(ext)
{
parserlog.error("Typfehler: -->Assign.sc_check() ");
}
SCExcept neu=new SCExcept();
neu.set_error("Typfehler");
neu.set_statement("Assign: "+expr1.getTypeName()+" = "+expr2.getTypeName());
if(except == null)
{
except=new SCStatementException();
}
except.addException(neu);
}
}
}
}
if(except!= null)
{
throw except;
}
}
// ino.end
// ino.method.get_Name.24948.definition
public String get_Name()
// ino.end
// ino.method.get_Name.24948.body
{
return null;
}
// ino.end
// ino.method.codegen.24951.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.24951.body
{
// AutoBoxing-Feature: Literale koennen nur als Objekt zugewiesen werden
if (expr2 instanceof Literal) ((Literal)expr2).setPrimitiveFlag(false);
if(expr1 instanceof LocalOrFieldVar)
{
LocalOrFieldVar local = (LocalOrFieldVar)expr1;
Vector name_vector = local.get_Name_Vector();
Vector type_vector = local.get_Type_Vector();
String local_name = null;
String class_name = null;
String type = null;
for(int i=0; i < name_vector.size()-1; i++)
{
local_name = (String)name_vector.elementAt(i);
type = JVMCode.get_codegen_Type((String)type_vector.elementAt(i), paralist);
int index = code.get_indexOf_Var(local_name);
if(index != -1)
{
// LocalVar
try
{
String local_type = code.get_TypeOf_Var(local_name).getName();
code.add_code(JVMCode.nload_n(local_type, index));
}
catch(JVMCodeException e)
{
// out of nload_n
String local_type = code.get_TypeOf_Var(local_name).getName();
code.add_code(JVMCode.nload(local_type));
code.add_code_byte((byte)index);
}
}
else
{
// FieldVar
code.add_code(JVMCode.aload_0);
code.add_code(JVMCode.getfield);
code.add_code_short(classfile.add_field_ref(local_name, class_name, type));
}
class_name = (String)type_vector.elementAt(i);
}
expr2.codegen(classfile, code, paralist);
local_name = (String)name_vector.lastElement();
int index = code.get_indexOf_Var(local_name);
if(index != -1)
{
// LocalVar
try
{
String local_type = code.get_TypeOf_Var(local_name).getName();
code.add_code(JVMCode.nstore_n(local_type, index));
}
catch(JVMCodeException e)
{
// out of nstore_n
String local_type = code.get_TypeOf_Var(local_name).getName();
code.add_code(JVMCode.nstore(local_type));
code.add_code_byte((byte)index);
}
}
else
{
// FieldVar
code.add_code(JVMCode.putfield);
code.add_code_short(classfile.add_field_ref(local_name, class_name, JVMCode.get_codegen_Type(expr2.getTypeName(), paralist)));
}
}
else if(expr1 instanceof InstVar)
{
InstVar instvar = (InstVar)expr1;
String instvar_name = instvar.get_codegen_UsedId();
code.add_code(JVMCode.aload_0);
expr2.codegen(classfile, code, paralist);
code.add_code(JVMCode.putfield);
code.add_code_short(classfile.add_field_ref(instvar_name, null, null));
}
else throw new JVMCodeException("JVMCodeException: Assign: void test codegen(ClassFile classfile, Code_attribute code)");
}
// ino.end
/**
* @author AI10023 - Andreas Stadelmeier
*/
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
ret.add(expr1.TYPEExpr(assumptions));
ret.add(expr2.TYPEExpr(assumptions));
//this.setTypeVariable( TypePlaceholder.fresh(this));
this.setTypeVariable(TypePlaceholder.fresh(this));
ret.add(new SingleConstraint(expr2.getTypeVariable(), expr1.getTypeVariable())); //expr1.type < expr2.type
ret.add(new SingleConstraint(expr1.getTypeVariable(), this.getTypeVariable()));
return ret;
}
/**
* Spezifikation:
* TYPEStmt( Ass, stmt ) =
* let (stmt : rty, ConS) = TYPEExpr( Ass, stmt )
* in (stmt : Void, ConS)
*/
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){
ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen
this.setTypeVariable(new Void(0)); //Typ des Statments auf Void setzen.
return ret;
}
// ino.method.TRExp.24954.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.30 von Martin Pl<EFBFBD>micke
* <br/>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param sigma
* @param V
* @param supportData
* @return
*/
// ino.end
// ino.method.TRExp.24954.definition
public CTripleSet TRExp(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRExp.24954.body
{
CTripleSet returnSet = new CTripleSet();
Vector<Expr> epressions = new Vector<Expr>();
epressions.addElement(this.expr1);
epressions.addElement(this.expr2);
// --------------------------
// TRTuple rufen:
// --------------------------
CMultiplyTupleSet tupleSet = this.TRTuple(new CMultiplyTuple(sigma, new Vector<Type>(), V), epressions, supportData);
Iterator<CMultiplyTuple> tupleIt = tupleSet.getIterator();
Vector<CTypeReconstructionException> exceptions=new Vector<CTypeReconstructionException>();
while(tupleIt.hasNext()){
CMultiplyTuple tuple = tupleIt.next();
if(tuple.getResultTypes().size()!=2){
throw new CTypeReconstructionException("Assign.TRExp(): Ung<6E>ltige Anzahl von ReturnTypes",this);
}
// --------------------------
// Die beiden ReturnTypes unifizieren:
// --------------------------
Type ty1 = tuple.getResultTypes().elementAt(0);
Type ty2 = tuple.getResultTypes().elementAt(1);
Vector<Vector<Pair>> unifierPossibilities = Unify.unify(ty2, ty1, supportData.getFiniteClosure());
// --------------------------
// Subset bauen:
// --------------------------
CTripleSet subSet = new CTripleSet();
// --------------------------
// Wenn Unifier vorhanden, dann anwenden:
// --------------------------
if(unifierPossibilities.size()!=0){
// --------------------------
// Alle m<>glichen Unifier auf V anwenden:
// --------------------------
for(int k=0; k<unifierPossibilities.size(); k++){
CSubstitutionSet unifier = new CSubstitutionSet(unifierPossibilities.elementAt(k));
// --------------------------
// Typannahmen bauen:
// --------------------------
CTypeAssumptionSet V_substituted = tuple.getAssumptionSet().deepCopy();
V_substituted.sub(unifier);
// --------------------------
// Substitutionen bauen:
// --------------------------
CSubstitutionSet substitutionSet = tuple.getSubstitutions().deepCopy();
substitutionSet.applyUnifier(unifier);
substitutionSet.unite(unifier);
//PL geaendert 05-08-23 applyThisSubstitutionSet eingefuegt
// --------------------------
// R<>ckgabetyp bauen:
// --------------------------
// if(ty1 instanceof TypePlaceholder){
// Iterator<CSubstitution> pairIt = unifier.getIterator();
// while(pairIt.hasNext()){
// CSubstitution pair = pairIt.next();
// if(pair.getTypeVar().getName().equals(ty1.getName())){
// ty1 = pair.getType();
// break;
// }
// }
// }
// --------------------------
CTriple subTriple = new CTriple(substitutionSet, substitutionSet.applyThisSubstitutionSet(ty1), V_substituted);
// --------------------------
// Triple zu R<>ckgabemenge hinzuf<75>gen
// --------------------------
subSet.addElement(subTriple);
}
}
// --------------------------
// Ansonsten Fehlermeldung:
// --------------------------
else {
//MUSS AUSSERHALB DER ITERATOR WHILE-SRHLEIFE PL 07-08-11 STEHEN
//throw new CTypeReconstructionException("Assign.TRExp(): Typen "+ty1+" und "+ty2+" lassen sich nicht unifizieren.",this);
exceptions.add(new CTypeReconstructionException("Assign.TRExp(): Typen "+ty1+" und "+ty2+" lassen sich nicht unifizieren.",this));
}
returnSet.unite(subSet);
}
//CTripleSet returnSet2 = super.unifyAndRegisterType(returnSet, supportData);
CTripleSet returnSet2 = super.registerType(returnSet, supportData);
//eingefuegt PL 07-08-11
//Anfang
if(returnSet.getCardinality()==0){
if (exceptions.size()==1) {
throw exceptions.elementAt(0);
}
else {
throw new CTypeReconstructionException("Assign.TRExp(): Es gibt kein Typpaar das sich unifizieren laesst.",this);
}
}
//Ende
return returnSet2;
}
// ino.end
// ino.method.TRStatement.24957.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.25 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.24957.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRStatement.24957.body
{
CTripleSet resultSet = this.TRExp(sigma, V, supportData);
resultSet = resultSet.deepCopy();
Iterator<CTriple> it = resultSet.getIterator();
while(it.hasNext()){
it.next().setResultType(new Void(getOffset()));
}
return resultSet;
}
// ino.end
// ino.method.toString.24960.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<EFBFBD>micke
* @return
*/
// ino.end
// ino.method.toString.24960.definition
public String toString()
// ino.end
// ino.method.toString.24960.body
{
if(getTypeVariable() == null)return "(" + expr1.toString() + " = " + expr2.toString() + ")";
return getTypeVariable().toString() + "(" + expr1.toString() + " = " + expr2.toString() + ")";
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.24963.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.24963.body
{
}
// ino.end
public void addOffsetsToExpression(CTypeAssumption localAssumption,String NameVariable,boolean isMemberVariable)
{
expr1.addOffsetsToExpression(localAssumption,NameVariable,isMemberVariable);
expr2.addOffsetsToExpression(localAssumption,NameVariable,isMemberVariable);
}
@Override
public String getTypeInformation(){
return "(" + expr1.getTypeInformation() + " = " + expr2.getTypeInformation() + ") : "+this.getTypeVariable();
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet){
JavaCodeResult ret = new JavaCodeResult().attach(this.expr1.printJavaCode(resultSet) ).attach( " = " ).attach( this.expr2.printJavaCode(resultSet));
return ret;
}
}
// ino.end