From c24a483880a3e801dc1c1a8bab5cabaf49ad02af Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Mon, 1 Jul 2024 13:23:22 +0200 Subject: [PATCH] Fixed complex return types of methods --- .../abstractSyntaxTree/Class/MethodDecl.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index e87d570..da253fb 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -49,13 +49,14 @@ public class MethodDecl implements Node { if(!Objects.equals(this.returnType, codeBlockType)) throw new TypeCheckException("Method returns " + codeBlockType + ", but should return " + this.returnType + ". "); + result.type = codeBlock.returnType; return result; } //Need to get the returnType of the method if it is an object // methodContext (class, (identifier, (returnType, parameter))) - // typeContext (class, (type, identifier)) + // typeContext (class, (identifier, type)) public void codeGen(ClassWriter cw, HashMap>> methodContext, HashMap> typeContext, List fieldDecls) throws Exception { localVars.put("this", classThatContainsMethod); @@ -65,7 +66,7 @@ public class MethodDecl implements Node { // check if the method is a constructor if (classThatContainsMethod.equals(name) && returnType == null) { - String descriptor = getMethodDescriptor(methodContext); + String descriptor = getMethodDescriptor(methodContext, typeContext); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", descriptor, null, null); @@ -127,7 +128,7 @@ public class MethodDecl implements Node { mv.visitEnd(); } else { - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(methodContext), null, null); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(methodContext, typeContext), null, null); mv.visitCode(); codeBlock.codeGen(mv, localVars, typeContext, methodContext); @@ -142,7 +143,7 @@ public class MethodDecl implements Node { } } - private String getMethodDescriptor(HashMap>> methodContext) { + private String getMethodDescriptor(HashMap>> methodContext, HashMap> typeContext) { // get the method descriptor StringBuilder descriptor = new StringBuilder("("); @@ -155,8 +156,18 @@ public class MethodDecl implements Node { case "void" -> descriptor.append("V"); default -> { // object - //TODO: This is not finished for objects --> classes and methods - if (returnType != null) descriptor.append("L").append(returnType).append(";"); + if (param.type != null) { + String paramType = param.type; + // If it is a class reference replace the "." with "/" and return it + HashMap classTypes = typeContext.get(classThatContainsMethod); + if (classTypes != null) { + if (classTypes.containsKey(paramType)) { + paramType = classTypes.get(paramType); + paramType.replaceAll("\\.", "/"); + } + } + descriptor.append("L").append(paramType).append(";"); + } } } }