JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java

318 lines
10 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.MethodCall.8639.package
2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree.statement;
2013-10-18 11:33:46 +00:00
// ino.end
// ino.module.MethodCall.8639.import
import java.util.Hashtable;
import de.dhbwstuttgart.typeinference.Menge;
2014-10-09 10:01:16 +00:00
import de.dhbwstuttgart.logger.Logger;
2014-09-04 14:35:44 +00:00
import de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.bytecode.CodeAttribute;
import de.dhbwstuttgart.myexception.JVMCodeException;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.Method;
2014-09-04 14:35:44 +00:00
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.Void;
2015-02-26 14:50:23 +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;
2015-02-26 14:50:23 +00:00
import de.dhbwstuttgart.typeinference.OderConstraint;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.typeinference.ResultSet;
2015-02-26 14:50:23 +00:00
import de.dhbwstuttgart.typeinference.UndConstraint;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
2015-02-26 14:50:23 +00:00
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
2013-10-18 11:33:46 +00:00
// ino.class.MethodCall.25623.declaration
public class MethodCall extends Expr
// ino.end
// ino.class.MethodCall.25623.body
{
public MethodCall(Receiver receiver, String methodName, ArgumentList argumentList, int offset){
this(offset, 0);
this.set_Receiver(receiver);
this.set_Name(methodName);
this.set_ArgumentList(argumentList);
}
2013-10-18 11:33:46 +00:00
// ino.method.MethodCall.25627.definition
public MethodCall(int offset, int variableLength)
// ino.end
// ino.method.MethodCall.25627.body
{
super(offset,variableLength);
}
// ino.end
// ino.attribute.receiver.25639.declaration
/**
* Diese Variable speichert die Expression, welche die Klasse von welcher die Methode aufgerufen wird darstellt.
*/
private Receiver receiver;
// ino.end
// ino.attribute.arglist.25642.declaration
2014-02-22 03:58:49 +00:00
private ArgumentList arglist=new ArgumentList();
2013-10-18 11:33:46 +00:00
// ino.attribute.parserlog.25663.declaration
protected static Logger parserlog = Logger.getLogger("parser");
// ino.end
// ino.method.set_ArgumentList.25666.definition
public void set_ArgumentList(ArgumentList al)
// ino.end
// ino.method.set_ArgumentList.25666.body
{
this.arglist = al;
}
// ino.end
// ino.method.getArgumentList.25669.definition
public ArgumentList getArgumentList()
// ino.end
// ino.method.getArgumentList.25669.body
{
if(this.arglist==null)return this.arglist = new ArgumentList();
return this.arglist;
}
// ino.end
// ino.method.get_Receiver.25672.definition
public Receiver get_Receiver()
// ino.end
// ino.method.get_Receiver.25672.body
{
return receiver;
}
// ino.end
// ino.method.get_Name.25675.definition
public String get_Name()
// ino.end
// ino.method.get_Name.25675.body
{
return this.usedid.name.firstElement();
}
// ino.end
public void set_Name(String name){
this.usedid = new UsedId(name, 0);
}
// ino.method.set_Receiver.25693.definition
public void set_Receiver(Receiver rec)
// ino.end
// ino.method.set_Receiver.25693.body
{
receiver=rec;
}
// ino.end
// ino.method.set_UsedId.25696.definition
public void set_UsedId(UsedId u)
// ino.end
// ino.method.set_UsedId.25696.body
{
usedid=u;
}
// ino.end
// ino.method.set_Expr_Menge.25699.definition
public void set_Expr_Menge(Menge<Expr> v)
2013-10-18 11:33:46 +00:00
// ino.end
// ino.method.set_Expr_Menge.25699.body
2013-10-18 11:33:46 +00:00
{
arglist.expr=v;
}
// ino.end
// ino.method.add_Expr.25702.definition
public void add_Expr(Expr e)
// ino.end
// ino.method.add_Expr.25702.body
{
arglist.expr.addElement(e);
}
// ino.end
2013-10-18 11:33:46 +00:00
// ino.method.toString.25738.defdescription type=javadoc
/**
* <br/>Author: Martin Pl<EFBFBD>micke
* @return
*/
// ino.end
// ino.method.toString.25738.definition
public String toString()
// ino.end
// ino.method.toString.25738.body
{
//return receiver/*.toString()*/ + " " + usedid.toString();
return type + " (" + this.get_Receiver() + "." + this.get_Name() +"(" + this.getArgumentList() + "))";
}
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25741.definition
public void wandleRefTypeAttributes2GenericAttributes(Menge<Type> paralist, Menge<GenericTypeVar> genericMethodParameters)
2013-10-18 11:33:46 +00:00
// ino.end
// ino.method.wandleRefTypeAttributes2GenericAttributes.25741.body
{
}
// ino.end
2013-10-18 11:33:46 +00:00
/**
* @author Andreas Stadelmeier, a10023
* @return der Name der Methode, welcher dieser MethodCall aufruft.
*/
public String getName(){
return this.usedid.toString();
}
/**
* @author AI10023 - Andreas Stadelmeier
*
* M<EFBFBD>gliche Probleme:
* Wenn die Methode ohne Angabe eines Receivers im Quelltext steht:
* methodCall(param); -> (bedeutet:) this.methodCall(param);
2014-09-09 09:15:10 +00:00
* Parser m<EFBFBD>glicherweise anpassen (siehe JavaParser.jay Zeile 1858 ff)
2013-10-18 11:33:46 +00:00
*/
@Override
public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) {
//Hier der Ablauf f<>r einen Methodenaufruf:
ConstraintsSet ret = new ConstraintsSet();
//Der Return-Type des MEthodenaufrufs ist zun<75>chst unbekannt:
this.setType(TypePlaceholder.fresh(this));
2013-10-18 11:33:46 +00:00
//Berechne die Constraints des Receivers
if(receiver == null){
receiver = new Receiver(new This(this));
2013-10-18 11:33:46 +00:00
}
ret.add(receiver.get_Expr().TYPEExpr(assumptions));
//Berechne die Constraints der Argumente aus der Argumentlist (also der Parameter, welche der Funktion <20>bergeben wurden)
if(this.arglist != null)for(Expr arg : this.arglist.expr){
ret.add(arg.TYPEExpr(assumptions));
}
//Noch das Overloading-Constraint anh<6E>ngen:
2015-02-26 14:50:23 +00:00
ret.add(overloading(assumptions));
2013-10-18 11:33:46 +00:00
return ret;
}
2015-02-26 14:50:23 +00:00
/**
* Erstellt die Constraints f<EFBFBD>r den eigentlichen Methodenaufruf.
* Sucht in den Assumptions nach passenden Methoden und erstellt ein OderConstraintSet.
* @param assumptions
* @return
*/
2015-02-26 16:41:29 +00:00
public ConstraintsSet overloading(TypeAssumptions assumptions){
2015-02-26 14:50:23 +00:00
ConstraintsSet ret = new ConstraintsSet();
//ret.add(new Overloading(assumptions, this, this.getType()).generateConsstraints());
OderConstraint oCons = new OderConstraint();
Menge<MethodAssumption> methodAssumptions = assumptions.getMethodAssumptions(this.getName(), this.getArgumentList().size());
2015-02-26 14:50:23 +00:00
if(methodAssumptions.size()==0)throw new TypeinferenceException("Eine Methode "+this.get_Name()+" ist in den Assumptions nicht vorhanden", this);
//Alle m<>glichen Methoden durchgehen:
for(MethodAssumption methodAssumption : methodAssumptions){
//Constraint nicht erstellen, falls R<>ckgabetyp von vorne herein nicht <20>bereinstimmt:
if(!(this.type instanceof TypePlaceholder) && !this.type.equals(methodAssumption.getAssumedType()))break;
oCons.addConstraint(constraintsFromMethodAssumption(methodAssumption, assumptions));
}
ret.add(oCons);
2015-02-26 14:50:23 +00:00
return ret;
}
/**
* Spezifikation:
* overloading determines for all possible overloadings and overridings
* of a method the constraints, where constraints itself forms
* the constraints from the receiver type, the argument types, the return
* type and a given type assumption for the method. If it is a
* method from a class, which is not the actual class (this), all type
* variables are replaced by fresh type variables (fresh), as different
* instances can occur. sargs determines all type assumptions of a
* method, where the argument types are supertypes of a minimal type
* assumption.
*
* @TODO: wenn es sich um eine Methode einer anderen Klasse handelt, m<EFBFBD>ssen neue TPH vergeben werden und nicht die der Assumption verwendet werden.
*
* @return
*/
2015-02-26 16:41:29 +00:00
public UndConstraint constraintsFromMethodAssumption(MethodAssumption methodAssumption, TypeAssumptions assumptions){
2015-02-26 14:50:23 +00:00
UndConstraint methodConstraint = new UndConstraint();
//Ein Constraint f<>r den ReturnType der Methode...
methodConstraint.addConstraint(methodAssumption.getAssumedType().TYPE(assumptions, this), this.getType().TYPE(assumptions, this));
2015-02-26 14:50:23 +00:00
//Ein Constraint f<>r die Parameter der Methode...
for(int i=0; i<methodAssumption.getParaCount();i++){
//Type der Argument Expressions <. AssumedType der Parameter
ConstraintType ct1 = this.getArgumentList().argumentAt(i).getType().TYPE(assumptions, this);
ConstraintType ct2 = methodAssumption.getParameterType(i).TYPE(assumptions, this);
methodConstraint.addConstraint(ct1 , ct2);
}
//Ein Constraint f<>r den Receiver der Methode (falls vorhanden)...
if(this.get_Receiver() != null && this.get_Receiver().get_Expr() != null){
//TODO: FunN-MethodAssumption darf keine Klasse (Class) als ParentClass besitzen. Denn der Typ der Klasse steht noch nicht fest (bisher ist es immer "FunN").
methodConstraint.addConstraint(this.get_Receiver().get_Expr().getType().TYPE(assumptions, this), methodAssumption.getParentClassType().TYPE(assumptions, this));
}
return methodConstraint;
}
2013-10-18 11:33:46 +00:00
/**
* 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, da als alleinstehendes Statement
2013-10-18 11:33:46 +00:00
return ret;
}
@Override
public JavaCodeResult printJavaCode(ResultSet resultSet) {
JavaCodeResult ret = new JavaCodeResult();
if(this.receiver != null)ret.attach( this.receiver.printJavaCode(resultSet)).attach(".");
ret .attach( this.get_Name());
ret .attach( "(" ).attach( this.getArgumentList().printJavaCode(resultSet)).attach( ")");
return ret.attach(";");
}
2014-02-22 03:58:49 +00:00
@Override
public Menge<SyntaxTreeNode> getChildren() {
Menge<SyntaxTreeNode> ret = new Menge<SyntaxTreeNode>();
2014-02-22 03:58:49 +00:00
for(Expr e : this.arglist.expr){
ret.add(e);
}
if(this.receiver!=null)ret.add(this.receiver.get_Expr());
return ret;
}
@Override
public void parserPostProcessing(SyntaxTreeNode parent) {
super.parserPostProcessing(parent);
}
@Override
public void codegen(ClassFile classfile, CodeAttribute code, Menge paralist)
throws JVMCodeException {
// TODO Auto-generated method stub
}
2014-02-22 03:58:49 +00:00
2013-10-18 11:33:46 +00:00
}
// ino.end