From 38aeaa657dc1f1534127773d0a8858b48b4e7816 Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Mon, 1 Jul 2024 23:35:24 +0200 Subject: [PATCH] First step to fixing methodCall --- .../Expression/InstVarExpression.java | 2 ++ .../MethodCallStatementExpression.java | 30 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index dffe91a..72f51ce 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -37,6 +37,8 @@ public class InstVarExpression extends AbstractType implements IExpression{ } TypeCheckResult result = new TypeCheckResult(); result.type = varType; + //TODO: Needed for methodCall codeGen + setTypeCheckResult(result); return result; } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 9dae4d8..75d04b6 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -119,30 +119,30 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr } else { throw new ExecutionControl.NotImplementedException("Receiver is null."); } - /* - else { - - String type = localVars.get(receiver.identifier); - if (type == null) { - type = typeContext.get(thisClass).get(receiver.identifier); - } - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitFieldInsn(Opcodes.GETFIELD, thisClass, receiver.identifier, "L" + type + ";"); - } - */ // Generate the code for each argument expression for (IExpression argument : arguments) { argument.codeGen(mv, localVars, typeContext, methodContext); } - // Invoke the method - String descriptor = getMethodDescriptor(localVars, methodContext); - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, methodName, descriptor, false); + // 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; + + // Generate the code for each argument expression for the current method + for (IExpression argument : receivingMethod.arguments) { + argument.codeGen(mv, localVars, typeContext, methodContext); + } + + // Invoke the current method + String descriptor = getMethodDescriptor(receivingMethod.methodName, localVars, methodContext, receivingMethod.arguments); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, receivingMethod.methodName, descriptor, false); + } } // ()I - private String getMethodDescriptor(LinkedHashMap localVars, HashMap>> methodContext) { + private String getMethodDescriptor(String methodName, LinkedHashMap localVars, HashMap>> methodContext, List arguments) { StringBuilder descriptor = new StringBuilder("("); for (IExpression argument : arguments) {