56 lines
2.1 KiB
Java
Raw Normal View History

2014-09-02 10:33:54 +02:00
package de.dhbwstuttgart.syntaxtree.statement;
2017-03-16 20:02:53 +01:00
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import org.antlr.v4.runtime.Token;
2014-09-02 10:33:54 +02:00
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
2013-10-18 13:33:46 +02:00
2017-03-16 20:02:53 +01:00
import java.util.HashSet;
import java.util.Set;
2013-10-18 13:33:46 +02:00
2017-03-06 17:59:01 +01:00
public class MethodCall extends Statement
2013-10-18 13:33:46 +02:00
{
2017-03-06 17:59:01 +01:00
private final String name;
2013-10-18 13:33:46 +02:00
private Receiver receiver;
private ArgumentList arglist;
2014-02-22 04:58:49 +01:00
2017-03-06 17:59:01 +01:00
public MethodCall(Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
super(TypePlaceholder.fresh(offset),offset);
this.arglist = argumentList;
this.name = methodName;
2017-03-15 16:17:07 +01:00
this.receiver = receiver;
2017-03-06 17:59:01 +01:00
}
2013-10-18 13:33:46 +02:00
2017-03-06 17:59:01 +01:00
@Override
2017-03-16 20:02:53 +01:00
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
2017-03-06 17:59:01 +01:00
ConstraintSet ret = receiver.getConstraints(info);
2017-03-22 16:05:59 +01:00
ret.addAll(receiver.getConstraints(info));
for(int i = 0;i<arglist.getArguments().size();i++){
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
}
2017-03-06 17:59:01 +01:00
//Overloading:
2017-03-16 20:02:53 +01:00
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){
2017-03-16 20:02:53 +01:00
Constraint methodConstraint = new Constraint();
methodConstraint.add(new Pair(receiver.getType(), m.getReceiverType()));
methodConstraint.add(new Pair(m.getReturnType(), this.getType()));
for(int i = 0;i<arglist.getArguments().size();i++){
2017-03-16 20:02:53 +01:00
methodConstraint.add(new Pair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType()));
}
2017-03-16 20:02:53 +01:00
methodConstraints.add(methodConstraint);
2017-03-06 17:59:01 +01:00
}
2017-03-16 20:02:53 +01:00
if(methodConstraints.size()<1){
throw new TypeinferenceException("Methode "+name+" ist nicht vorhanden!",getOffset());
}
ret.addOderConstraint(methodConstraints);
2017-03-06 17:59:01 +01:00
return ret;
2013-10-18 13:33:46 +02:00
}
}