JavaPatternMatching/src/mycompiler/myclass/FieldDeclaration.java
2014-03-17 17:55:55 +01:00

133 lines
3.9 KiB
Java

package mycompiler.myclass;
import java.util.Vector;
import typinferenz.ConstraintsSet;
import typinferenz.JavaCodeResult;
import typinferenz.OderConstraint;
import typinferenz.ResultSet;
import typinferenz.SingleConstraint;
import typinferenz.TypinferenzException;
import typinferenz.assumptions.FieldAssumption;
import typinferenz.assumptions.TypeAssumptions;
import mycompiler.SyntaxTreeNode;
import mycompiler.mybytecode.ClassFile;
import mycompiler.myexception.JVMCodeException;
import mycompiler.mystatement.Expr;
import mycompiler.mytype.Type;
import mycompiler.mytype.TypePlaceholder;
import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent;
/**
* Eine Feldinitialisation steht für eine Felddeklaration mit gleichzeitiger Wertzuweisung
* Beispiel: 'public Feld FeldVar = FeldWert;'
* @author janulrich
*
*/
public class FieldDeclaration extends Field{
private Expr wert;
//private Type type;
public FieldDeclaration(int offset){
super(offset);
}
public void setWert(Expr initialExpression){
this.wert = initialExpression;
}
public Expr getWert(){
return this.wert;
}
public String getIdentifier(){
return this.get_Name().elementAt(0).name;
}
@Override
public void codegen(ClassFile classfile, Vector paralist)
throws JVMCodeException {
// TODO Auto-generated method stub
}
@Override
public String toString()
{
if(getWert()!=null)return super.toString() + "=" + getWert().toString();
return super.toString();
}
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult();
JavaCodeResult toAttach = this.getType().printJavaCode(resultSet).attach(" ").attach( this.getIdentifier());
if(this.wert!=null)toAttach.attach(" = ").attach(this.getWert().printJavaCode(resultSet) );
toAttach.attach( ";");
ret.attach(toAttach);
return ret;
}
@Override
public TypeAssumptions createTypeAssumptions(Class classmember) {
//////////////////////////////
//Felder:
//////////////////////////////
TypeAssumptions assumptions = new TypeAssumptions();
/*
* TODO: Der Feld-Assumption muss ein TPH als Typ hinzugefügt werden, falls er Typlos initialisiert wurde. Dies kann auch der Type-Algorithmus der Inst/FieldVar - Klasse machen.
* Wird das Feld mit einem Typ initialisiert so muss dieser auch in die Assumptions.
*/
if(this.getType() == null)throw new TypinferenzException("Der Typ eines Feldes darf nicht null sein");
//assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType()));
assumptions.addFieldAssumption(new FieldAssumption(this,classmember));
return assumptions;
}
@Override
public void parserPostProcessing(SyntaxTreeNode parent){
super.parserPostProcessing(parent);
if(this.getType() == null)this.setType(TypePlaceholder.fresh(this));
}
@Override
public Vector<SyntaxTreeNode> getChildren() {
Vector<SyntaxTreeNode> ret = new Vector<SyntaxTreeNode>();
if(this.wert!=null)ret.add(this.wert);
return ret;
}
@Override
public void replaceType(CReplaceTypeEvent e) {
// TODO Auto-generated method stub
}
@Override
public int getTypeLineNumber() {
// TODO Auto-generated method stub
return 0;
}
public int getVariableLength()
{
return declid.elementAt(0).get_Name().length();
}
@Override
public ConstraintsSet TYPE(TypeAssumptions publicAssumptions) {
ConstraintsSet ret = new ConstraintsSet();
SingleConstraint c1 = new SingleConstraint(this.getType(), this.getType());
ret.add(c1); //Damit die TypVariable des Felds in den Constraints auftaucht
if(this.wert!=null){
//Falls bei der Deklaration ein Wert zugewiesen wird, verhält sich das Constraintserzeugen wie bei dem Assign-Statement:
ret.add(this.wert.TYPEExpr(publicAssumptions));
ret.add(new SingleConstraint(this.wert.getType(), this.getType()));
}
return ret;
}
}