Updated the TypedFieldVarAccess

This commit is contained in:
ahmad 2024-05-15 18:42:49 +02:00
parent bac72bdc81
commit 0215d38aa1
2 changed files with 29 additions and 6 deletions

View File

@ -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)) {

View File

@ -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");