fixed MethodCallStatementExpression maybe

This commit is contained in:
Jochen Seyfried 2024-07-02 15:21:32 +02:00
parent 10f5dc692d
commit d5b526b8fc

View File

@ -56,6 +56,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
receiver.instVarExpression.thisClass = typeOfSubreceiver;
receiver.instVarExpression.typeCheck(methodContext, typeContext, localVars);
currentType = typeContext.get(typeOfSubreceiver).get(mostLeftField);
//currentType = typeContext.get(receiver.instVarExpression.getTypeCheckResult().type).get(mostLeftField);
} else {
currentType = classToSearchMethodIn;
}
@ -70,6 +71,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
if (currentType == null)
throw new TypeCheckException("The method " + methodName + " was not found in " + classToSearchMethodIn + ".");
receivingMethods.get(i).thisClass = this.thisClass;
// currentType = return type
// ThisClass = class von methode
receivingMethods.get(i).checkParameters(methodContext, typeContext, localVars);
}
currentType = (String) methodContext.get(currentType).get(methodName).keySet().toArray()[0];
@ -119,16 +123,15 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
} else {
throw new ExecutionControl.NotImplementedException("Receiver is null.");
}
// Generate the code for each argument expression
for (IExpression argument : arguments) {
argument.codeGen(mv, localVars, typeContext, methodContext);
}
String returnOfPreviousMethod = null;
// Invoke the method for each receiving method in the chain
for (ReceivingMethod receivingMethod : receivingMethods) {
// Get the owner class for the current method in the chain
owner = receivingMethod.thisClass;
if (returnOfPreviousMethod == null)
owner = thisClass;
else
// If the return of the previous method is not null, use it as the owner class for the current method in the chain
owner = returnOfPreviousMethod;
// Generate the code for each argument expression for the current method
for (IExpression argument : receivingMethod.arguments) {
@ -136,13 +139,25 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
}
// Invoke the current method
String descriptor = getMethodDescriptor(receivingMethod.methodName, localVars, methodContext, receivingMethod.arguments);
String descriptor = getMethodDescriptor(receivingMethod.methodName, localVars, methodContext, receivingMethod.arguments, returnOfPreviousMethod);
returnOfPreviousMethod = methodContext.get(owner).get(receivingMethod.methodName).keySet().toArray()[0].toString();
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, receivingMethod.methodName, descriptor, false);
}
// Invoke the final method
// Generate the code for each argument expression
for (IExpression argument : arguments) {
argument.codeGen(mv, localVars, typeContext, methodContext);
}
String descriptor = getMethodDescriptor(methodName, localVars, methodContext, arguments, returnOfPreviousMethod);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, methodName, descriptor, false);
}
// ()I
private String getMethodDescriptor(String methodName, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, List<IExpression> arguments) {
private String getMethodDescriptor(String methodName, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, List<IExpression> arguments, String returnOfPreviousMethod) {
StringBuilder descriptor = new StringBuilder("(");
for (IExpression argument : arguments) {
@ -173,8 +188,13 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
//Return Type
String classToSearchMethodIn = localVars.get(receiver.identifier);
if (classToSearchMethodIn == null)
if (classToSearchMethodIn == null) {
classToSearchMethodIn = returnOfPreviousMethod;
}
if (classToSearchMethodIn == null) {
classToSearchMethodIn = thisClass;
}
String returnType = methodContext.get(classToSearchMethodIn).get(methodName).keySet().toArray()[0].toString();