2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree ;
2014-02-11 01:47:39 +00:00
2015-05-27 13:57:15 +00:00
import org.apache.bcel.generic.ClassGen ;
2014-02-11 01:47:39 +00:00
2015-05-27 13:57:15 +00:00
import de.dhbwstuttgart.typeinference.Menge ;
2014-09-04 14:35:44 +00:00
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 ;
2014-09-08 13:12:47 +00:00
import de.dhbwstuttgart.typeinference.ConstraintType ;
2014-09-02 08:33:54 +00:00
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
/ * *
2015-05-12 17:49:27 +00:00
* Eine Feldinitialisation steht fà ¼ r eine Felddeklaration mit gleichzeitiger Wertzuweisung
2014-02-12 01:12:12 +00:00
* 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;
2015-04-22 19:40:22 +00:00
//private Menge<GenericTypeVar> parameter;
2014-02-11 01:47:39 +00:00
2014-04-03 08:35:25 +00:00
/ * *
2015-05-12 17:49:27 +00:00
* Dieser Konstruktor der FieldDeclaration erstellt den Syntaxknoten vollstà ¤ ndig .
* Kein nachtrà ¤ gliches hinzfà ¼ gen von Informationen oder aufrufen von parserPostProcessing ist notwendig .
2014-04-03 08:35:25 +00:00
* /
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 ) ) ;
}
2014-03-09 11:10:03 +00:00
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 ;
}
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 ( ) ) ;
2014-02-12 21:10:33 +00:00
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 ( ) ;
/ *
2015-05-12 17:49:27 +00:00
* 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 .
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 ) ;
2014-02-19 13:16:28 +00:00
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
2015-04-22 19:40:22 +00:00
public Menge < SyntaxTreeNode > getChildren ( ) {
Menge < SyntaxTreeNode > ret = super . getChildren ( ) ;
2014-02-12 21:10:33 +00:00
if ( this . wert ! = null ) ret . add ( this . wert ) ;
2014-02-12 01:12:12 +00:00
return ret ;
}
2014-02-12 21:10:33 +00:00
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 ) )
2015-05-12 17:49:27 +00:00
throw new TypeinferenceException ( " Typlose Felder müssen mit Wert initialisiert werden " , this ) ;
2014-02-12 21:10:33 +00:00
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-09-08 13:12:47 +00:00
ConstraintType thisType = this . getType ( ) . TYPE ( localAssumptions , this ) ;
2015-02-24 14:25:56 +00:00
this . setType ( thisType . getType ( ) ) ;
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
2014-09-08 13:12:47 +00:00
SingleConstraint c1 = new SingleConstraint ( thisType , thisType ) ;
2014-02-12 21:10:33 +00:00
ret . add ( c1 ) ; //Damit die TypVariable des Felds in den Constraints auftaucht
if ( this . wert ! = null ) {
2015-05-12 17:49:27 +00:00
//Falls bei der Deklaration ein Wert zugewiesen wird, verhält sich das Constraintserzeugen wie bei dem Assign-Statement:
2014-07-16 16:38:55 +00:00
ret . add ( this . wert . TYPEExpr ( localAssumptions ) ) ;
2014-09-08 13:12:47 +00:00
ret . add ( new SingleConstraint ( this . wert . getType ( ) . TYPE ( localAssumptions , this ) , thisType ) ) ;
2014-02-12 21:10:33 +00:00
}
return ret ;
}
2014-02-19 04:20:54 +00:00
2014-03-27 15:43:07 +00:00
@Override
2015-04-22 19:40:22 +00:00
public void wandleRefTypeAttributes2GenericAttributes ( Menge < Type > paralist ) {
2014-03-27 15:43:07 +00:00
super . wandleRefTypeAttributes2GenericAttributes ( paralist ) ;
2015-05-12 17:49:27 +00:00
if ( this . getWert ( ) ! = null ) this . getWert ( ) . wandleRefTypeAttributes2GenericAttributes ( paralist , new Menge < GenericTypeVar > ( ) ) ; //FieldDeclaration hat keine Generischen Variablen, daher leere Liste übergeben
2014-03-27 15:43:07 +00:00
}
2014-02-11 01:47:39 +00:00
2015-05-27 13:57:15 +00:00
@Override
public void genByteCode ( ClassGen cg ) {
// TODO Auto-generated method stub
}
2014-02-11 01:47:39 +00:00
}