From e6e5433a913dd55c968209ad60c410857865cd34 Mon Sep 17 00:00:00 2001 From: ahmad Date: Sat, 29 Jun 2024 14:27:37 +0200 Subject: [PATCH] Added check for field of other classes --- .../typedclass/TypedFieldVarAccess.java | 22 ++++++++++++++++--- .../typedast/typedclass/TypedProgram.java | 7 ++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 3ab9a9e..bcc0a07 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -49,12 +49,20 @@ public class TypedFieldVarAccess implements TypedExpression { private Type checkFieldOrMethodType(TypedProgram typedProgram) { if (typedProgram.getCurrentClass().isThereField(name)) { - type = typedProgram.getCurrentClass().getFieldType(name); - return type; + return checkTypeField(typedProgram); } else if (typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) { type = typedProgram.getCurrentClass().getMethodType(name); return type; } else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) { + + if (typedProgram.isClassWithNamePresent(recursiveOwnerChain.getType().getReference())) { + Type typeofFieldNameInClass = typedProgram.getTypeOfFieldNameInClass(recursiveOwnerChain.getType().getReference(), name); + if(typeofFieldNameInClass != null){ + return typeofFieldNameInClass; + }else{ + throw new RuntimeException("Field " + name + " not declared in class " + recursiveOwnerChain.getType().getReference()); + } + } type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName()); return type; } else { @@ -106,7 +114,15 @@ public class TypedFieldVarAccess implements TypedExpression { } return type; } - + private Type checkTypeField(TypedProgram typedProgram) { + if (recursiveOwnerChain != null) { + if (recursiveOwnerChain.getType() != null) { + return typedProgram.getTypeOfFieldNameInClass(recursiveOwnerChain.getType().getReference(), name); + } + } + type = typedProgram.getCurrentClass().getFieldType(name); + return type; + } @Override public void codeGen(MethodContext ctx) { if (recursiveOwnerChain != null) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java index 5f8db1b..8a8571e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Program; +import de.maishai.typedast.Type; import lombok.AllArgsConstructor; import lombok.Getter; @@ -43,6 +44,12 @@ public class TypedProgram { } } + public boolean isClassWithNamePresent(String className) { + return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className)); + } + public Type getTypeOfFieldNameInClass(String className, String fieldName) { + return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName); + } public TypedClass getTypedClass(String className) { return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get(); }