Fixed complex return types of methods
This commit is contained in:
parent
aa7d82b9ac
commit
c24a483880
@ -49,13 +49,14 @@ public class MethodDecl implements Node {
|
|||||||
|
|
||||||
if(!Objects.equals(this.returnType, codeBlockType))
|
if(!Objects.equals(this.returnType, codeBlockType))
|
||||||
throw new TypeCheckException("Method returns " + codeBlockType + ", but should return " + this.returnType + ". ");
|
throw new TypeCheckException("Method returns " + codeBlockType + ", but should return " + this.returnType + ". ");
|
||||||
|
|
||||||
result.type = codeBlock.returnType;
|
result.type = codeBlock.returnType;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Need to get the returnType of the method if it is an object
|
//Need to get the returnType of the method if it is an object
|
||||||
// methodContext (class, (identifier, (returnType, parameter)))
|
// methodContext (class, (identifier, (returnType, parameter)))
|
||||||
// typeContext (class, (type, identifier))
|
// typeContext (class, (identifier, type))
|
||||||
public void codeGen(ClassWriter cw, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, List<FieldDecl> fieldDecls) throws Exception {
|
public void codeGen(ClassWriter cw, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, List<FieldDecl> fieldDecls) throws Exception {
|
||||||
|
|
||||||
localVars.put("this", classThatContainsMethod);
|
localVars.put("this", classThatContainsMethod);
|
||||||
@ -65,7 +66,7 @@ public class MethodDecl implements Node {
|
|||||||
|
|
||||||
// check if the method is a constructor
|
// check if the method is a constructor
|
||||||
if (classThatContainsMethod.equals(name) && returnType == null) {
|
if (classThatContainsMethod.equals(name) && returnType == null) {
|
||||||
String descriptor = getMethodDescriptor(methodContext);
|
String descriptor = getMethodDescriptor(methodContext, typeContext);
|
||||||
|
|
||||||
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor, null, null);
|
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor, null, null);
|
||||||
|
|
||||||
@ -127,7 +128,7 @@ public class MethodDecl implements Node {
|
|||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
|
|
||||||
} else {
|
} 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();
|
mv.visitCode();
|
||||||
codeBlock.codeGen(mv, localVars, typeContext, methodContext);
|
codeBlock.codeGen(mv, localVars, typeContext, methodContext);
|
||||||
@ -142,7 +143,7 @@ public class MethodDecl implements Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getMethodDescriptor(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) {
|
private String getMethodDescriptor(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) {
|
||||||
// get the method descriptor
|
// get the method descriptor
|
||||||
StringBuilder descriptor = new StringBuilder("(");
|
StringBuilder descriptor = new StringBuilder("(");
|
||||||
|
|
||||||
@ -155,8 +156,18 @@ public class MethodDecl implements Node {
|
|||||||
case "void" -> descriptor.append("V");
|
case "void" -> descriptor.append("V");
|
||||||
default -> {
|
default -> {
|
||||||
// object
|
// object
|
||||||
//TODO: This is not finished for objects --> classes and methods
|
if (param.type != null) {
|
||||||
if (returnType != null) descriptor.append("L").append(returnType).append(";");
|
String paramType = param.type;
|
||||||
|
// If it is a class reference replace the "." with "/" and return it
|
||||||
|
HashMap<String, String> classTypes = typeContext.get(classThatContainsMethod);
|
||||||
|
if (classTypes != null) {
|
||||||
|
if (classTypes.containsKey(paramType)) {
|
||||||
|
paramType = classTypes.get(paramType);
|
||||||
|
paramType.replaceAll("\\.", "/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
descriptor.append("L").append(paramType).append(";");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user