JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java
2016-04-01 16:12:30 +02:00

220 lines
7.0 KiB
Java
Executable File

// ino.module.NewClass.8642.package
package de.dhbwstuttgart.syntaxtree.statement;
// ino.end
// ino.module.NewClass.8642.import
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.generic.ClassGen;
import org.apache.commons.bcel6.generic.InstructionConstants;
import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.ObjectType;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.myexception.NotImplementedException;
import de.dhbwstuttgart.myexception.SCExcept;
import de.dhbwstuttgart.myexception.SCStatementException;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.syntaxtree.type.FunN;
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.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
// ino.class.NewClass.25830.declaration
public class NewClass extends Expr
// ino.end
// ino.class.NewClass.25830.body
{
// ino.method.NewClass.25834.definition
public NewClass(int offset,int variableLength)
// ino.end
// ino.method.NewClass.25834.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.arglist.25837.declaration
private ArgumentList arglist;
private boolean isStatement = false;
// ino.end
// ino.attribute.parserlog.25840.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.getArgumentList.25843.definition
public ArgumentList getArgumentList()
// ino.end
// ino.method.getArgumentList.25843.body
{
return this.arglist;
}
// ino.end
// ino.method.get_Name.25846.definition
public String get_Name()
// ino.end
// ino.method.get_Name.25846.body
{
return this.usedid.get_Name_1Element();
}
// ino.end
// ino.method.set_UsedId.25849.definition
public void set_UsedId( UsedId t)
// ino.end
// ino.method.set_UsedId.25849.body
{
this.usedid = t;
}
// ino.end
// ino.method.set_ArgumentList.25855.definition
public void set_ArgumentList(ArgumentList al)
// ino.end
// ino.method.set_ArgumentList.25855.body
{
this.arglist = al;
}
// ino.end
// ino.method.toString.25867.defdescription type=javadoc
/**
* <br/>Author: Martin Pl�micke
* @return
*/
// ino.end
// ino.method.toString.25867.definition
public String toString()
// ino.end
// ino.method.toString.25867.body
{
String ret = "NEW ";
Type thisType = getType();
if(thisType != null)ret += thisType.toString();
else ret += this.get_Name();
return ret;
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25870.definition
public void wandleRefTypeAttributes2GenericAttributes(Menge<Type> paralist, Menge<GenericTypeVar> genericMethodParameters)
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25870.body
{
}
// ino.end
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
//TODO: Das hier noch vervollständigen
ConstraintsSet ret = new ConstraintsSet();
UndConstraint callConstraints = new UndConstraint();
int numArgs = 0;
if(this.arglist != null)numArgs = this.arglist.size();
ConstructorAssumption cA = assumptions.getConstructorAssumption(this.get_Name(), numArgs);
if(cA == null)throw new TypeinferenceException("Der Konstruktor "+this.get_Name()+" mit "+numArgs+" Parametern ist nicht vorhanden.", this);
for(int i=0; i<cA.getParaCount();i++){
ret.add(this.arglist.expr.elementAt(i).TYPEExpr(assumptions));
callConstraints.addConstraint( this.arglist.expr.elementAt(i).getType().TYPE(assumptions, this), cA.getParameterType(i).TYPE(assumptions, this));
}
Type thisT = assumptions.checkType(new RefType(this.get_Name(),this,0), (SyntaxTreeNode)this);
this.setType(thisT);
return ret;
}
/**
* Spezifikation:
* TYPEStmt( Ass, stmt ) =
* let (stmt : rty, ConS) = TYPEExpr( Ass, stmt )
* in (stmt : Void, ConS)
*/
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){
ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen
this.setType(new Void(this,0)); //Typ des Statments auf Void setzen.
this.isStatement = true;
return ret;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult("new ");
ret.attach(this.get_Name());
ret.attach("(");
if(this.arglist!=null && this.arglist.expr != null){
Iterator<Expr> it = this.arglist.expr.iterator();
while(it.hasNext()){
Expr arg = it.next();
ret.attach(arg.printJavaCode(resultSet));
if(it.hasNext())ret.attach(", ");
}
//ret = ret.substring(0, ret.length()-2);
}
ret.attach(")");
return ret;
}
@Override
public Menge<SyntaxTreeNode> getChildren() {
Menge<SyntaxTreeNode> ret = new Menge<SyntaxTreeNode>();
if(this.arglist!=null)for(Expr e : this.arglist.expr){
ret.add(e);
}
return ret;
}
@Override
public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs) {
InstructionList il = new InstructionList();
//il.append(_cg.getInstructionFactory().createNew(get_Name()));
il.append(_cg.getInstructionFactory().createNew(this.getType().getBytecodeType(_cg, rs).toString()));
il.append(InstructionConstants.DUP);
if(arglist!=null){
il.append(arglist.generateBytecode(_cg, rs));
il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(_cg, rs), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
this.arglist.getBytecodeTypeList(_cg, rs), Constants.INVOKESPECIAL));
}else{
il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(_cg, rs), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL));
}
if(this.isStatement){
il.append(InstructionConstants.POP);
}
return il;
}
}
// ino.end