diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index a994fcb..26085e1 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -117,6 +117,23 @@ public class TypedClass implements TypedNode { return null; } + public boolean isMethodOfCurrentClass(String methodName) { + for (TypedMethod m : typedMethods) { + if (m.getName().equals(methodName)) { + return true; + } + } + return false; + } + public Type getMethodType(String methodName) { + for (TypedMethod m : typedMethods) { + if (m.getName().equals(methodName)) { + return m.getReturnType(); + } + } + return null; + } + public Type getParameterTypeInCurrentConstructor(String parameterName) { for (TypedParameter p : currentConstructor.getTypedParameters()) { if (p.getParaName().equals(parameterName)) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index a575e85..337375b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -32,17 +32,20 @@ public class TypedFieldVarAccess implements TypedExpression { @Override public Type typeCheck(TypedProgram typedProgram) { if (field) { - type = checkFieldType(typedProgram); + type = checkFieldOrMethodType(typedProgram); return type; } else { type = checkVariableType(typedProgram); return type; } } - private Type checkFieldType(TypedProgram typedProgram) { + private Type checkFieldOrMethodType(TypedProgram typedProgram) { if (typedProgram.getCurrentClass().isThereField(name)) { type = typedProgram.getCurrentClass().getFieldType(name); return type; + } else if(typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) { + type = typedProgram.getCurrentClass().getMethodType(name); + return type; } else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) { type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName()); return type; @@ -66,7 +69,7 @@ public class TypedFieldVarAccess implements TypedExpression { } else if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariablePresent(name)) { type = typedProgram.getCurrentClass().getCurrentConstructor().getLocalVariableType(name); } else { - return checkFieldOrRecursiveType(typedProgram); + return checkFieldOrMethodOrRecursiveType(typedProgram); } return type; } @@ -77,15 +80,18 @@ public class TypedFieldVarAccess implements TypedExpression { } else if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariablePresent(name)) { type = typedProgram.getCurrentClass().getCurrentMethod().getLocalVariableType(name); } else { - return checkFieldOrRecursiveType(typedProgram); + return checkFieldOrMethodOrRecursiveType(typedProgram); } return type; } - private Type checkFieldOrRecursiveType(TypedProgram typedProgram) { + private Type checkFieldOrMethodOrRecursiveType(TypedProgram typedProgram) { if (typedProgram.getCurrentClass().isThereField(name)) { type = typedProgram.getCurrentClass().getFieldType(name); - } else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) { + }else if(typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) { + type = typedProgram.getCurrentClass().getMethodType(name); + } + else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) { type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName()); } else { throw new RuntimeException("Variable " + name + " not declared");