JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java

167 lines
5.8 KiB
Java
Raw Normal View History

2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree;
2014-02-11 01:47:39 +00:00
import java.util.Vector;
2014-09-04 14:35:44 +00:00
import de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.myexception.JVMCodeException;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
import de.dhbwstuttgart.syntaxtree.statement.Expr;
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.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.OderConstraint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
2014-02-11 01:47:39 +00:00
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;
//private Vector<GenericTypeVar> parameter;
2014-02-11 01:47:39 +00:00
2014-04-03 08:35:25 +00:00
/**
* Dieser Konstruktor der FieldDeclaration erstellt den Syntaxknoten vollst<EFBFBD>ndig.
* Kein nachtr<EFBFBD>gliches hinzf<EFBFBD>gen von Informationen oder aufrufen von parserPostProcessing ist notwendig.
*/
public FieldDeclaration(String name, Type typ){
super(0);//Dieser Deklarator wird nicht vom Parser aufgerufen. Dadurch gibt es auch keinen Offset
this.setType(typ);
this.set_DeclId(new DeclId(name));
}
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();
/*
2014-03-18 19:18:57 +00:00
* 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.
2014-02-12 01:12:12 +00:00
* Wird das Feld mit einem Typ initialisiert so muss dieser auch in die Assumptions.
*/
2014-04-15 12:56:20 +00:00
if(this.getType() == null)throw new TypeinferenceException("Der Typ eines Feldes darf nicht null sein", this);
2014-02-19 04:20:54 +00:00
//assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType()));
2014-04-09 12:12:55 +00:00
assumptions.addAssumption(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;
}
public int getVariableLength()
{
return declid.elementAt(0).get_Name().length();
}
@Override
public ConstraintsSet TYPE(TypeAssumptions publicAssumptions) {
2014-08-28 16:42:40 +00:00
if(this.wert == null && (this.getType() == null || this.getType() instanceof TypePlaceholder))
throw new TypeinferenceException("Typlose Felder m<>ssen mit Wert initialisiert werden", this);
ConstraintsSet ret = new ConstraintsSet();
2014-07-16 16:38:55 +00:00
TypeAssumptions localAssumptions = publicAssumptions.clone();
for(GenericTypeVar gp : this.getGenericParameter()){
localAssumptions.add(gp.createAssumptions());
}
for(GenericTypeVar gp : this.getGenericParameter()){
gp.TYPE(localAssumptions);
}
2014-04-09 12:12:55 +00:00
/*
if(this.getType() instanceof GenericTypeVar){
//Falls Typ ein GTV ist muss er syntaktisch kontrolliert werden...
GenericTypeVar gtv = (GenericTypeVar) this.getType();
}
*/
2014-04-15 12:56:20 +00:00
//TypeCheck, falls es sich um einen RefType handelt:
2014-07-16 16:38:55 +00:00
this.getType().checkType(localAssumptions, this);
2014-06-18 09:30:14 +00:00
/*
2014-04-15 12:56:20 +00:00
if(this.getType()!=null && (this.getType() instanceof RefType)){
Type replaceType = null;
replaceType = publicAssumptions.getTypeFor((RefType)this.getType());
if(replaceType == null)throw new TypeinferenceException("Der Typ "+this.getType().getName()+" ist nicht korrekt",this);
this.setType(replaceType);
}
2014-06-18 09:30:14 +00:00
*/
2014-04-15 12:56:20 +00:00
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:
2014-07-16 16:38:55 +00:00
ret.add(this.wert.TYPEExpr(localAssumptions));
ret.add(new SingleConstraint(this.wert.getType(), this.getType()));
}
return ret;
}
2014-02-19 04:20:54 +00:00
@Override
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist){
super.wandleRefTypeAttributes2GenericAttributes(paralist);
if(this.getWert()!=null)this.getWert().wandleRefTypeAttributes2GenericAttributes(paralist, new Vector<GenericTypeVar>()); //FieldDeclaration hat keine Generischen Variablen, daher leere Liste <20>bergeben
}
2014-02-11 01:47:39 +00:00
}