JavaPatternMatching/src/mycompiler/myclass/FieldDeclaration.java

133 lines
3.9 KiB
Java
Raw Normal View History

2014-02-11 01:47:39 +00:00
package mycompiler.myclass;
import java.util.Vector;
import typinferenz.ConstraintsSet;
2014-02-11 01:47:39 +00:00
import typinferenz.JavaCodeResult;
import typinferenz.OderConstraint;
2014-02-11 01:47:39 +00:00
import typinferenz.ResultSet;
import typinferenz.SingleConstraint;
import typinferenz.TypinferenzException;
import typinferenz.assumptions.FieldAssumption;
2014-02-11 01:47:39 +00:00
import typinferenz.assumptions.TypeAssumptions;
2014-02-12 01:12:12 +00:00
import mycompiler.SyntaxTreeNode;
2014-02-11 01:47:39 +00:00
import mycompiler.mybytecode.ClassFile;
import mycompiler.myexception.JVMCodeException;
import mycompiler.mystatement.Expr;
import mycompiler.mytype.Type;
2014-02-12 01:12:12 +00:00
import mycompiler.mytype.TypePlaceholder;
2014-02-11 01:47:39 +00:00
import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent;
2014-02-12 01:12:12 +00:00
/**
* Eine Feldinitialisation steht f<EFBFBD>r eine Felddeklaration mit gleichzeitiger Wertzuweisung
* Beispiel: 'public Feld FeldVar = FeldWert;'
* @author janulrich
*
*/
2014-02-11 01:47:39 +00:00
public class FieldDeclaration extends Field{
2014-02-12 01:12:12 +00:00
private Expr wert;
2014-02-11 01:47:39 +00:00
//private Type type;
public FieldDeclaration(int offset){
super(offset);
}
2014-02-11 01:47:39 +00:00
public void setWert(Expr initialExpression){
this.wert = initialExpression;
}
public Expr getWert(){
return this.wert;
}
2014-02-19 04:20:54 +00:00
public String getIdentifier(){
2014-02-11 01:47:39 +00:00
return this.get_Name().elementAt(0).name;
}
@Override
2014-02-12 01:12:12 +00:00
public void codegen(ClassFile classfile, Vector paralist)
throws JVMCodeException {
2014-02-11 01:47:39 +00:00
// TODO Auto-generated method stub
2014-02-12 01:12:12 +00:00
}
2014-02-11 01:47:39 +00:00
@Override
2014-02-12 01:12:12 +00:00
public String toString()
{
2014-03-17 16:55:55 +00:00
if(getWert()!=null)return super.toString() + "=" + getWert().toString();
return super.toString();
2014-02-12 01:12:12 +00:00
}
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult();
2014-02-19 04:20:54 +00:00
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);
2014-02-12 01:12:12 +00:00
return ret;
2014-02-11 01:47:39 +00:00
}
2014-02-12 01:12:12 +00:00
@Override
public TypeAssumptions createTypeAssumptions(Class classmember) {
//////////////////////////////
//Felder:
//////////////////////////////
TypeAssumptions assumptions = new TypeAssumptions();
/*
* TODO: Der Feld-Assumption muss ein TPH als Typ hinzugef<EFBFBD>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");
2014-02-19 04:20:54 +00:00
//assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType()));
assumptions.addFieldAssumption(new FieldAssumption(this,classmember));
2014-02-12 01:12:12 +00:00
return assumptions;
}
2014-02-11 01:47:39 +00:00
@Override
2014-02-12 01:12:12 +00:00
public void parserPostProcessing(SyntaxTreeNode parent){
super.parserPostProcessing(parent);
if(this.getType() == null)this.setType(TypePlaceholder.fresh(this));
2014-02-11 01:47:39 +00:00
}
2014-02-12 01:12:12 +00:00
2014-02-11 01:47:39 +00:00
@Override
2014-02-12 01:12:12 +00:00
public Vector<SyntaxTreeNode> getChildren() {
Vector<SyntaxTreeNode> ret = new Vector<SyntaxTreeNode>();
if(this.wert!=null)ret.add(this.wert);
2014-02-12 01:12:12 +00:00
return ret;
}
@Override
public void replaceType(CReplaceTypeEvent e) {
2014-02-11 01:47:39 +00:00
// TODO Auto-generated method stub
2014-02-12 01:12:12 +00:00
2014-02-11 01:47:39 +00:00
}
@Override
2014-02-12 01:12:12 +00:00
public int getTypeLineNumber() {
2014-02-11 01:47:39 +00:00
// TODO Auto-generated method stub
2014-02-12 01:12:12 +00:00
return 0;
2014-02-11 01:47:39 +00:00
}
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<72>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;
}
2014-02-19 04:20:54 +00:00
2014-02-11 01:47:39 +00:00
}