2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree ;
2013-10-18 11:33:46 +00:00
2016-07-21 14:36:33 +00:00
import org.apache.bcel.Constants ;
import org.apache.bcel.generic.InstructionList ;
2014-02-19 04:20:54 +00:00
2015-09-02 15:11:32 +00:00
import de.dhbwstuttgart.bytecode.ClassGenerator ;
2015-09-10 14:21:30 +00:00
import de.dhbwstuttgart.bytecode.DHBWConstantPoolGen ;
2015-09-23 13:30:35 +00:00
import de.dhbwstuttgart.bytecode.MethodGenerator ;
2016-12-15 14:45:23 +00:00
import de.dhbwstuttgart.typecheck.JavaClassName ;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.statement.Block ;
2015-06-17 10:03:54 +00:00
import de.dhbwstuttgart.syntaxtree.statement.SuperCall ;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar ;
import de.dhbwstuttgart.syntaxtree.type.Type ;
2016-10-07 12:12:18 +00:00
import de.dhbwstuttgart.typeinference.TypeinferenceResults ;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption ;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions ;
2015-09-17 18:57:33 +00:00
import de.dhbwstuttgart.typeinference.exceptions.DebugException ;
2015-02-25 17:51:48 +00:00
import de.dhbwstuttgart.syntaxtree.type.Void ;
2014-02-19 04:20:54 +00:00
2014-02-14 16:31:55 +00:00
public class Constructor extends Method {
/ * *
* Parser kann nicht zwischen einem Konstruktor und einer Methode unterscheiden .
* Diese Klasse beherbegt den als Methode geparsten Konstruktor und wandelt sein verhalten zu dem eines Konstruktors ab .
* /
2015-08-26 12:48:51 +00:00
public Constructor ( Method methode , Class parent ) {
2016-09-16 11:25:20 +00:00
super ( methode . get_Name ( ) , methode . getType ( ) , methode . getParameterList ( ) ,
methode . get_Block ( ) , methode . getGenericDeclarationList ( ) , methode . getOffset ( ) ) ;
2015-06-17 10:03:54 +00:00
//Sicherstellen, dass das erste Statement in der Methode ein SuperCall ist:
2016-09-16 11:25:20 +00:00
if ( this . get_Block ( ) . get_Statement ( ) . size ( ) < 1 | | ! ( this . get_Block ( ) . get_Statement ( ) . get ( 0 ) instanceof SuperCall ) ) {
2015-06-17 10:03:54 +00:00
this . get_Block ( ) . statements . add ( 0 , new SuperCall ( this . get_Block ( ) ) ) ;
2015-08-26 12:48:51 +00:00
this . parserPostProcessing ( parent ) ;
2015-06-17 10:03:54 +00:00
}
2015-02-25 17:29:44 +00:00
}
@Override
public TypeAssumptions createTypeAssumptions ( Class classmember ) {
this . parent = classmember ;
2016-09-16 11:25:20 +00:00
Class parentClass = classmember ;
2015-02-25 17:29:44 +00:00
TypeAssumptions ret = new TypeAssumptions ( ) ;
ret . addAssumption ( new ConstructorAssumption ( this , parentClass ) ) ;
return ret ;
}
2015-05-27 13:57:15 +00:00
2015-09-02 15:11:32 +00:00
public void genByteCode ( ClassGenerator cg , InstructionList fieldInitializations ) {
2015-09-10 14:21:30 +00:00
DHBWConstantPoolGen _cp = cg . getConstantPool ( ) ;
2015-06-16 12:58:27 +00:00
InstructionList il = new InstructionList ( ) ; //sollte nicht new sein sondern aus Block kommen
2015-12-08 12:34:16 +00:00
2016-09-16 11:25:20 +00:00
MethodGenerator method = new MethodGenerator ( Constants . ACC_PUBLIC , this . getType ( ) . getBytecodeType ( cg , null ) ,
this . parameterlist . getBytecodeTypeList ( cg , null ) , this . parameterlist . getParameterNameArray ( ) ,
" <init> " , cg . getClassName ( ) , il , _cp ) ;
2015-06-17 10:03:54 +00:00
2015-09-23 13:30:35 +00:00
//FieldInitializations an Block anfügen
2015-06-18 09:17:42 +00:00
Block block = this . get_Block ( ) ;
2016-09-16 11:25:20 +00:00
if ( ! ( block . statements . get ( 0 ) instanceof SuperCall ) ) {
2015-09-17 18:57:33 +00:00
throw new DebugException ( " Fehlender SuperCall im Konstruktor " ) ;
2015-09-02 15:11:32 +00:00
}
2015-06-17 10:03:54 +00:00
2015-09-23 13:30:35 +00:00
//method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
2015-06-17 10:03:54 +00:00
2015-12-05 14:15:28 +00:00
cg . addMethod ( method . createMethod ( cg , getParameterList ( ) , this . getType ( ) , get_Block ( ) , null ) ) ;
2015-05-27 13:57:15 +00:00
}
2015-09-02 15:11:32 +00:00
@Override
2016-10-07 12:12:18 +00:00
public void genByteCode ( ClassGenerator cg , Class classObj , TypeinferenceResults resultSets ) {
2015-09-02 15:11:32 +00:00
this . genByteCode ( cg , new InstructionList ( ) ) ;
}
2015-06-16 09:55:15 +00:00
// super statement muss drin sein
// stmt genByteCode + im block genByteCode implementieren & dann Hierarchie ausprobieren
// de.dhbw.systanxtree.stmts supercall
2015-06-16 15:44:27 +00:00
// Aufrufhierarchie: Class->Felder->Konstruktor->Methode innerhalb Konstruktor->Block->Statements (in diesem Fall nur super())->hier wird bytecode f<> r superaufruf generiert
2015-06-16 09:55:15 +00:00
2015-05-27 13:57:15 +00:00
2015-02-25 17:29:44 +00:00
@Override
public void parserPostProcessing ( SyntaxTreeNode parent ) {
2015-06-16 20:08:17 +00:00
super . parserPostProcessing ( parent ) ;
2015-02-25 17:29:44 +00:00
if ( this . parameterlist ! = null ) {
for ( FormalParameter fp : this . parameterlist ) {
fp . parserPostProcessing ( this ) ;
}
}
for ( GenericTypeVar gtv : this . getGenericParameter ( ) ) {
gtv . parserPostProcessing ( this ) ;
}
}
2014-02-19 04:20:54 +00:00
@Override
2014-09-02 16:49:19 +00:00
public JavaClassName getTypeName ( ) {
2014-02-19 04:20:54 +00:00
2014-02-19 16:32:43 +00:00
return this . getType ( ) . getName ( ) ;
2014-02-19 04:20:54 +00:00
}
2015-08-26 12:48:51 +00:00
2013-10-18 11:33:46 +00:00
}