2014-09-02 10:33:54 +02:00
|
|
|
package de.dhbwstuttgart.syntaxtree.statement;
|
2014-02-09 16:07:31 +01:00
|
|
|
|
2017-03-16 20:02:53 +01:00
|
|
|
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
2017-04-18 21:06:04 +02:00
|
|
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.Method;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.ParameterList;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
2017-03-16 20:02:53 +01:00
|
|
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
|
|
|
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
|
|
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
2017-03-29 17:28:29 +02:00
|
|
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
|
2017-03-08 03:43:47 +01:00
|
|
|
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
|
2017-04-18 21:06:04 +02:00
|
|
|
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
2017-03-29 17:28:29 +02:00
|
|
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
2017-02-17 16:34:40 +01:00
|
|
|
import org.antlr.v4.runtime.Token;
|
2015-06-16 14:58:27 +02:00
|
|
|
|
2014-09-02 10:33:54 +02:00
|
|
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
2013-10-18 13:33:46 +02:00
|
|
|
|
2017-04-18 21:06:04 +02:00
|
|
|
import java.util.*;
|
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;
|
2016-12-16 00:00:37 +01:00
|
|
|
private ArgumentList arglist;
|
2014-02-22 04:58:49 +01:00
|
|
|
|
2017-04-18 21:06:04 +02:00
|
|
|
public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
|
|
|
|
super(retType,offset);
|
2017-03-06 17:59:01 +01:00
|
|
|
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-04-18 21:06:04 +02:00
|
|
|
ret.addAll(this.getArgumentListConstraints(info));
|
2017-03-06 17:59:01 +01:00
|
|
|
//Overloading:
|
2017-03-16 20:02:53 +01:00
|
|
|
Set<Constraint> methodConstraints = new HashSet<>();
|
2017-04-18 21:06:04 +02:00
|
|
|
for(MethodAssumption m : this.getMethods(name, arglist, info)){
|
|
|
|
methodConstraints.add(generateConstraint(m, info));
|
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
|
|
|
}
|
2017-04-18 21:06:04 +02:00
|
|
|
|
|
|
|
protected Constraint<Pair> generateConstraint(MethodAssumption forMethod, TypeInferenceBlockInformation info){
|
|
|
|
Constraint methodConstraint = new Constraint();
|
|
|
|
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), forMethod.getReceiverType(), PairOperator.SMALLERDOT, info));
|
|
|
|
methodConstraint.add(ConstraintsFactory.createPair(forMethod.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
|
|
|
|
methodConstraint.addAll(generateParameterConstraints(forMethod, info));
|
|
|
|
return methodConstraint;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Set<Pair> generateParameterConstraints(MethodAssumption forMethod, TypeInferenceBlockInformation info) {
|
|
|
|
Set<Pair> ret = new HashSet<>();
|
|
|
|
for(int i = 0;i<arglist.getArguments().size();i++){
|
|
|
|
ret.add(ConstraintsFactory.createPair(forMethod.getArgTypes().get(i),
|
|
|
|
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
|
|
|
|
List<MethodAssumption> ret = new ArrayList<>();
|
|
|
|
for(ClassOrInterface cl : info.getAvailableClasses()){
|
|
|
|
for(Method m : cl.getMethods()){
|
|
|
|
if(m.getName().equals(name) &&
|
|
|
|
m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){
|
|
|
|
RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType());
|
|
|
|
|
|
|
|
ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){
|
|
|
|
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
|
|
|
for(FormalParameter fp : parameterList.getFormalparalist()){
|
|
|
|
params.add(info.checkGTV(fp.getType()));
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArgumentList getArgumentList() {
|
|
|
|
return arglist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConstraintSet getArgumentListConstraints(TypeInferenceBlockInformation info) {
|
|
|
|
ConstraintSet ret = new ConstraintSet();
|
|
|
|
for(int i = 0;i<arglist.getArguments().size();i++){
|
|
|
|
ret.addAll(arglist.getArguments().get(i).getConstraints(info));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-18 13:33:46 +02:00
|
|
|
}
|