JavaPatternMatching/src/mycompiler/mystatement/InstVar.java

380 lines
14 KiB
Java
Executable File
Raw Blame History

// ino.module.InstVar.8634.package
package mycompiler.mystatement;
// ino.end
// ino.module.InstVar.8634.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.myclass.UsedId;
import mycompiler.myexception.CTypeReconstructionException;
import mycompiler.myexception.JVMCodeException;
import mycompiler.mytype.GenericTypeVar;
import mycompiler.mytype.Pair;
import mycompiler.mytype.RefType;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytypereconstruction.CSupportData;
import mycompiler.mytypereconstruction.CTriple;
import mycompiler.mytypereconstruction.TypeinferenceResultSet;
import mycompiler.mytypereconstruction.set.CSubstitutionSet;
import mycompiler.mytypereconstruction.set.CTripleSet;
import mycompiler.mytypereconstruction.set.CTypeAssumptionSet;
import mycompiler.mytypereconstruction.typeassumption.CInstVarTypeAssumption;
import mycompiler.mytypereconstruction.typeassumption.CTypeAssumption;
import mycompiler.mytypereconstruction.typeassumptionkey.CInstVarKey;
import mycompiler.mytypereconstruction.unify.Unify;
import org.apache.log4j.Logger;
// ino.end
import typinferenz.ConstraintsSet;
import typinferenz.FreshTypeVariable;
import typinferenz.JavaCodeResult;
import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions;
// ino.class.InstVar.25392.declaration
public class InstVar extends Expr
// ino.end
// ino.class.InstVar.25392.body
{
// ino.attribute.expr.25396.declaration
private Expr expr;
// ino.end
// ino.attribute.usedid.25399.declaration
private UsedId usedid;
// ino.end
// ino.attribute.type.25402.declaration
protected String type; //???? BRAUCHT MAN DEN???
// ino.end
// ino.attribute.parserlog.25405.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.InstVar.25408.definition
public InstVar(Expr e, String n, int offset)
// ino.end
// ino.method.InstVar.25408.body
{
super(offset,n.length());
expr = e;
usedid = new UsedId(getOffset());
usedid.set_Name(n);
}
// ino.end
// ino.method.InstVar.25411.definition
public InstVar(String n, String t, int offset)
// ino.end
// ino.method.InstVar.25411.body
{
super(offset,n.length());
usedid = new UsedId(getOffset());
usedid.set_Name(n);
type = t;
}
// ino.end
// ino.method.InstVar.25414.defdescription type=javadoc
/**
* macht aus einem UsedId mit einem Vector von Strings eine InstVar
* <br/>Author: Martin Pl<50>micke PL 05-08-17
* @param ui
* @return
*/
// ino.end
// ino.method.InstVar.25414.definition
public InstVar(UsedId ui, int offset,int variableLength)
// ino.end
// ino.method.InstVar.25414.body
{
super(offset,variableLength);
Iterator namen = ui.get_Name().iterator();
LocalOrFieldVar innerLOFV = new LocalOrFieldVar((String)namen.next(),getOffset());
innerLOFV.setType(TypePlaceholder.fresh(this));
InstVar INSTVA = new InstVar(innerLOFV, (String)namen.next(),offset);
INSTVA.setType(TypePlaceholder.fresh(this));
while(namen.hasNext()) {
INSTVA = new InstVar(INSTVA, (String)namen.next(),offset);
INSTVA.setType(TypePlaceholder.fresh(this));
}
expr = INSTVA.expr;
usedid = INSTVA.usedid;
}
// ino.end
// ino.method.sc_check.25417.definition
public void sc_check(Vector<Class> classname, Hashtable ch, Hashtable<String, String> bh, boolean ext, Hashtable parach, Hashtable<String, Hashtable> parabh)
// ino.end
// ino.method.sc_check.25417.body
{
if(ext)
parserlog.debug(" ---InstVar---");
Vector name = usedid.get_Name();
for(Enumeration el = name.elements(); el.hasMoreElements();)
{
String name1 = (String) el.nextElement();
if(bh.containsKey(name1))
{
this.type = (String)bh.get(name1); //type wird von Expr geerbt
if(ext)
parserlog.debug("Variable " + name1 + " im Block gefunden.");
}
else if(ch.containsKey(name1))
{
this.type = (String)ch.get(name1);
if(ext)
parserlog.debug("Variable " + name1 + " in Class gefunden.");
}
else
if(ext)
parserlog.debug("Typfehler: -->InstVar.sc_check() "+ name1);
}
}
// ino.end
// ino.method.get_Name.25420.definition
public String get_Name()
// ino.end
// ino.method.get_Name.25420.body
{
return null;
}
// ino.end
// ino.method.get_codegen_UsedId.25423.definition
public String get_codegen_UsedId()
// ino.end
// ino.method.get_codegen_UsedId.25423.body
{ return usedid.get_codegen_UsedId(); }
// ino.end
// ino.method.codegen.25426.definition
public void codegen(ClassFile classfile, CodeAttribute code, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.25426.body
{
String instvar_name = usedid.get_codegen_UsedId();
code.add_code(JVMCode.aload_0);
code.add_code(JVMCode.getfield);
code.add_code_short(classfile.add_field_ref(instvar_name, null, null));
}
// ino.end
// ino.method.TRStatement.25429.definition
public CTripleSet TRStatement(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRStatement.25429.body
{
throw CTypeReconstructionException.createNotImplementedException();
}
// ino.end
// ino.method.TRExp.25432.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.38 von Martin Pl<50>micke
* <br/>Author: J<>rg B<>uerle
* @param sigma
* @param V
* @param supportData
* @return
*/
// ino.end
// ino.method.TRExp.25432.definition
public CTripleSet TRExp(CSubstitutionSet sigma, CTypeAssumptionSet V, CSupportData supportData)
// ino.end
// ino.method.TRExp.25432.body
{
CTripleSet resultSet = new CTripleSet();
CTripleSet tripleSet = this.expr.TRExp(sigma, V, supportData);
Iterator<CTriple> tripleIt = tripleSet.getIterator();
while(tripleIt.hasNext()){
CTriple triple = tripleIt.next();
CTripleSet instVarSet = this.TRInstVar(triple, supportData);
resultSet.unite(instVarSet);
}
//PL 05-08-17 unifyAndRegisterType eingebaut
//return resultSet;
CTripleSet resultSet2 = super.registerType(resultSet, supportData);
return resultSet2;
}
// ino.end
// ino.method.TRInstVar.25435.defdescription type=javadoc
/**
* Implementierung des Algorithmus 5.38-1 von Martin Pl<50>micke
* <br/>Author: J<>rg B<>uerle
* @param triple
* @param supportData
* @return
*/
// ino.end
// ino.method.TRInstVar.25435.definition
public CTripleSet TRInstVar(CTriple triple, CSupportData supportData)
// ino.end
// ino.method.TRInstVar.25435.body
{
CTripleSet returnSet = new CTripleSet();
// ##########################
// 1. Alle bisherigen Typkombinationen durchgehen:
// ##########################
for(int i=0; i<supportData.getA().size(); i++){
TypeinferenceResultSet possibleComb = supportData.getA().elementAt(i);
// --------------------------
// Alle Klassen durchsuchen:
// --------------------------
for(int j=0; j<possibleComb.getClassNameList().size(); j++){
String className = possibleComb.getClassNameList().elementAt(j); // ReceiverName
// --------------------------
// Typannahme f<>r Methode heraussuchen:
// --------------------------
//PL geaendert 05-08-04 get_Name durch usedid.get_Name_1Element() ersetzt
//CInstVarKey key = new CInstVarKey(className, this.get_Name());
CInstVarKey key = new CInstVarKey(className, this.usedid.get_Name_1Element());
CInstVarTypeAssumption instAssum = (CInstVarTypeAssumption)possibleComb.getFieldAndLocalVarAssumptions().get(key);
// --------------------------
// Typen rekonstruieren:
// --------------------------
if(instAssum!=null){
this.handleInstVarAssum(triple, new RefType(className,getOffset()), instAssum.getAssumedType(), returnSet, supportData);
}
}
}
// ##########################
// 2. Aktuelle Klasse durchsuchen:
// ##########################
// --------------------------
// Typannahme f<>r Methode heraussuchen:
// --------------------------
//PL geaendert 05-08-04 get_Name durch usedid.get_Name_1Element() ersetzt
//CInstVarKey key = new CInstVarKey(supportData.getCurrentClass(), this.get_Name());
CInstVarKey key = new CInstVarKey(supportData.getCurrentClass(), this.usedid.get_Name_1Element());
CInstVarTypeAssumption instAssum = (CInstVarTypeAssumption)triple.getAssumptionSet().getElement(key);
// --------------------------
// Typen rekonstruieren:
// --------------------------
if(instAssum!=null){
//PL 05-08-18 geaendert
this.handleInstVarAssum(triple, new RefType(supportData.getCurrentClass(), supportData.getCurrentClassPara(),getOffset()), instAssum.getAssumedType(), returnSet, supportData);
//this.handleInstVarAssum(triple, new RefType(supportData.getCurrentClass()), instAssum.getAssumedType(), returnSet, supportData);
}
// ##########################
// 3. Ergebnismenge zur<75>ckgeben:
// ##########################
if(returnSet.getCardinality()==0){
throw new CTypeReconstructionException("InstVar.TRInstVar(): Die angegebene Feldvariable " + this.usedid.getQualifiedName() + " konnte mit keiner bekannten Feldvariable unifiziert werden.",this);
}
return returnSet;
}
// ino.end
// ino.method.handleInstVarAssum.25438.defdescription type=javadoc
/**
* Unifiziert den R<>ckgabetyp des Receivers mit dem der gefundenen Klasse
* <br/>Author: J<>rg B<>uerle
* @param triple Triple des Receivers
* @param thetaZero Typ der gefundenen Klasse
* @param thetaC Typ der InstanzVariable
* @param resultSet
* @param supportData
*/
// ino.end
// ino.method.handleInstVarAssum.25438.definition
private void handleInstVarAssum(CTriple triple, Type thetaZero, Type thetaC, CTripleSet resultSet, CSupportData supportData)
// ino.end
// ino.method.handleInstVarAssum.25438.body
{
// --------------------------
// ReturnType mit Integer unifizieren:
// --------------------------
Vector<Vector<Pair>> unifierPossibilities = Unify.unify(triple.getResultType(), thetaZero, supportData.getFiniteClosure());
// --------------------------
// Wenn Unifier vorhanden, dann
// anwenden und Triple hinzuf<75>gen:
// --------------------------
if(unifierPossibilities.size()!=0){
// --------------------------
// Subset bauen:
// --------------------------
CTripleSet subSet = new CTripleSet();
// --------------------------
// Alle m<>glichen Unifier anwenden:
// --------------------------
for(int i=0; i<unifierPossibilities.size(); i++){
CSubstitutionSet unifier = new CSubstitutionSet(unifierPossibilities.elementAt(i));
CTriple subTriple = triple.cloneAndApplyUnify(unifier);
subTriple.setResultType(thetaC);
subSet.addElement(subTriple);
}
resultSet.unite(subSet);
}
}
// ino.end
// ino.method.toString.25441.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<50>micke
* @return
*/
// ino.end
// ino.method.toString.25441.definition
public String toString()
// ino.end
// ino.method.toString.25441.body
{
return super.type.toString() + " " + usedid.toString();
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25444.definition
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist, Vector<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25444.body
{
}
// ino.end
public void addOffsetsToExpression(CTypeAssumption localAssumption,String NameVariable,boolean isMemberVariable)
{
if(this.get_UsedId().get_Name_1Element().equals(NameVariable))
{
//wenn Variable mit gleichem Namen gefunden->schreibe Offset
localAssumption.addOffset(this.get_UsedId().getOffset());
}
}
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
ConstraintsSet ret = new ConstraintsSet();
ret.add(expr.TYPEExpr(assumptions));
this.setTypeVariable(TypePlaceholder.fresh(this));
return null;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult().attach(this.expr.printJavaCode(resultSet)).attach("."+this.usedid.get_Name_1Element());
}
}
// ino.end