ThisCall und SuperCall anfügen

This commit is contained in:
JanUlrich 2015-02-26 17:41:29 +01:00
parent 37e55490e1
commit 4fd30850b5
7 changed files with 1431 additions and 1096 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1231,9 +1231,18 @@ explicitconstructorinvocation : THIS '(' ')' ';'
THCONargl.set_ArgumentList($3);
$$=THCONargl;
}
// |SUPER '(' ')' ';'
// |SUPER '(' argumentlist ')' ';'
|SUPER '(' ')' ';'
{
SuperCall sCall = new SuperCall($1.getOffset(),$1.getLexem().length());
$$=sCall;
}
|SUPER '(' argumentlist ')' ';'
{
SuperCall sCall = new SuperCall($1.getOffset(),$1.getLexem().length());
sCall.set_ArgumentList($3);
$$=sCall;
}
classtypelist : classtype
{
//RefType RT = new RefType(RT.get_UsedId().get_Name_1Element(),null,-1);

View File

@ -0,0 +1,41 @@
package de.dhbwstuttgart.syntaxtree.misc;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
/**
* Diese Klasse stellt den this()-Aufruf dar.
* @author janulrich
*
*/
public class ConstructorCall extends MethodCall
{
public ConstructorCall(Receiver receiver, String methodName, ArgumentList argumentList, int offset){
super(offset, 0);
this.set_Receiver(receiver);
this.set_Name(methodName);
this.set_ArgumentList(argumentList);
}
/*
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
throw new TypeinferenceException("this(...)-Aufruf kann nicht als Ausdruck verwendet werden",this);
}
*/
@Override
public ConstraintsSet overloading(TypeAssumptions assumptions){
ConstraintsSet ret = new ConstraintsSet();
ConstructorAssumption cAss = assumptions.getConstructorAssumption(this.get_Name(), this.getArgumentList().size());
if(cAss == null)throw new TypeinferenceException("Kein Konstruktor mit passender Parameteranzahl vorhanden",this);
ret.add(constraintsFromMethodAssumption(cAss, assumptions));
return ret;
}
}

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.bytecode.CodeAttribute;
import de.dhbwstuttgart.bytecode.JVMCode;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -17,7 +18,7 @@ import de.dhbwstuttgart.typeinference.ResultSet;
// ino.class.ArgumentList.24911.declaration
public class ArgumentList
public class ArgumentList extends SyntaxTreeNode
// ino.end
// ino.class.ArgumentList.24911.body
{
@ -106,6 +107,27 @@ public class ArgumentList
//if(this.expr.size()>0)ret = ret.substring(0, ret.length()-2);
return ret;
}
@Override
public int getOffset() {
return 0;
}
@Override
public int getVariableLength() {
return 0;
}
@Override
public Vector<? extends SyntaxTreeNode> getChildren() {
return expr;
}
}
// ino.end

View File

@ -217,7 +217,7 @@ public class MethodCall extends Expr
* @param assumptions
* @return
*/
ConstraintsSet overloading(TypeAssumptions assumptions){
public ConstraintsSet overloading(TypeAssumptions assumptions){
ConstraintsSet ret = new ConstraintsSet();
//ret.add(new Overloading(assumptions, this, this.getType()).generateConsstraints());
OderConstraint oCons = new OderConstraint();
@ -248,7 +248,7 @@ public class MethodCall extends Expr
*
* @return
*/
UndConstraint constraintsFromMethodAssumption(MethodAssumption methodAssumption, TypeAssumptions assumptions){
public UndConstraint constraintsFromMethodAssumption(MethodAssumption methodAssumption, TypeAssumptions assumptions){
UndConstraint methodConstraint = new UndConstraint();
//Ein Constraint für den ReturnType der Methode...
methodConstraint.addConstraint(methodAssumption.getAssumedType().TYPE(assumptions, this), type.TYPE(assumptions, this));

View File

@ -0,0 +1,108 @@
// ino.module.This.8654.package
package de.dhbwstuttgart.syntaxtree.statement;
// ino.end
// ino.module.This.8654.import
import java.util.Hashtable;
import java.util.Vector;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.bytecode.CodeAttribute;
import de.dhbwstuttgart.bytecode.JVMCode;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.myexception.SCStatementException;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.ConstructorCall;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Overloading;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.unify.CSubstitutionSet;
public class SuperCall extends MethodCall
{
public SuperCall(int offset,int variableLength)
{
super(offset,variableLength);
}
public SuperCall(SyntaxTreeNode parent){
this(0,0);
this.parent = parent;
}
public ArgumentList arglist;
public void set_ArgumentList(ArgumentList al)
{
this.arglist = al;
}
public ArgumentList getArgumentList()
{
return this.arglist;
}
public void set_UsedId(UsedId ui)
{
this.usedid = ui;
}
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
throw new TypeinferenceException("this(...)-Aufruf kann nicht als Ausdruck verwendet werden",this);
}
/**
* This kann auch als Konstruktoraufruf in einem Konstruktor-Block vorkommen.
*/
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
String className = this.getParentClass().getSuperClass().getName().toString();
ConstraintsSet ret = new ConstraintsSet();
this.setType(new Void(this,this.getOffset()));
//Kontrollieren, dass sich this(...) in einem Konstruktor und dort als erstes Statement befindet:
SyntaxTreeNode p = this.getGTVDeclarationContext();
if(p instanceof Constructor &&
((Constructor)p).get_Block().statements.firstElement().equals(this)){
//Constraints generieren:
MethodCall constructorCall = new ConstructorCall(new Receiver(new This(this)), className, arglist, this.getOffset());
ret.add(constructorCall.TYPEStmt(assumptions));
return ret;
}else{
//Ansonsten Fehler ausgeben:
throw new TypeinferenceException("super()-Aufruf hier nicht möglich", this);
}
}
public String toString()
{
//return receiver/*.toString()*/ + " " + usedid.toString();
return "(" + "super" +"(" + this.getArgumentList() + "))";
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult("super("+this.getArgumentList().printJavaCode(resultSet)+")");
}
}

View File

@ -0,0 +1,115 @@
// ino.module.This.8654.package
package de.dhbwstuttgart.syntaxtree.statement;
// ino.end
// ino.module.This.8654.import
import java.util.Hashtable;
import java.util.Vector;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.bytecode.CodeAttribute;
import de.dhbwstuttgart.bytecode.JVMCode;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.myexception.SCStatementException;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.ConstructorCall;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Overloading;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.assumptions.ConstructorAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.unify.CSubstitutionSet;
public class ThisCall extends MethodCall
{
public ThisCall(int offset,int variableLength)
{
super(offset,variableLength);
}
public ThisCall(SyntaxTreeNode parent){
this(0,0);
this.parent = parent;
}
public ArgumentList arglist;
public void set_ArgumentList(ArgumentList al)
{
this.arglist = al;
}
public ArgumentList getArgumentList()
{
return this.arglist;
}
public void set_UsedId(UsedId ui)
{
this.usedid = ui;
}
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
throw new TypeinferenceException("this(...)-Aufruf kann nicht als Ausdruck verwendet werden",this);
}
/**
* This kann auch als Konstruktoraufruf in einem Konstruktor-Block vorkommen.
*/
@Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
String className = this.getParentClass().getName().toString();
ConstraintsSet ret = new ConstraintsSet();
this.setType(new Void(this,this.getOffset()));
//Kontrollieren, dass sich this(...) in einem Konstruktor und dort als erstes Statement befindet:
SyntaxTreeNode p = this.getGTVDeclarationContext();
if(p instanceof Constructor &&
((Constructor)p).get_Block().statements.firstElement().equals(this)){
//Constraints generieren:
ConstructorCall constructorCall = new ConstructorCall(new Receiver(new This(this)), className, arglist, this.getOffset());
ret.add(constructorCall.TYPEStmt(assumptions));
return ret;
}else{
//Ansonsten Fehler ausgeben:
throw new TypeinferenceException("this()-Aufruf hier nicht möglich", this);
}
}
public String toString()
{
//return receiver/*.toString()*/ + " " + usedid.toString();
return "(" + "this" +"(" + this.getArgumentList() + "))";
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
return new JavaCodeResult("this("+this.getArgumentList().printJavaCode(resultSet)+")");
}
@Override
public Vector<SyntaxTreeNode> getChildren() {
Vector<SyntaxTreeNode> ret = new Vector<SyntaxTreeNode>();
if(arglist != null)ret.add(arglist);
return ret;
}
}