From 2537051668229b108fb02b8b490d8cd8f0cc86bb Mon Sep 17 00:00:00 2001 From: Bruder John Date: Tue, 2 Jul 2024 14:31:44 +0200 Subject: [PATCH] fix if field and parameter have same identifier --- src/main/java/semantic/SemanticAnalyzer.java | 6 +- .../java/semantic/context/FieldContext.java | 4 +- .../typedAstFeaturesTests/CorrectTest.java | 60 +------------------ 3 files changed, 11 insertions(+), 59 deletions(-) diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 5906d16..c849067 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -535,6 +535,10 @@ public class SemanticAnalyzer implements SemanticVisitor { ITypeNode currentType = null; + if (memberAccessNode.thisExpr) { + currentType = new ReferenceType(currentClass.identifier); + } + for (String s : memberAccessNode.identifiers) { if (currentType == null) { if (currentScope.getLocalVar(s) != null) { @@ -552,7 +556,7 @@ public class SemanticAnalyzer implements SemanticVisitor { var currentTypeClass = context.getClass(reference.getIdentifier()); var currentField = currentTypeClass.getField(s); - if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) { + if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC || memberAccessNode.thisExpr) { currentType = currentField.getType(); } else { errors.add(new NotVisibleException("This field is not visible")); diff --git a/src/main/java/semantic/context/FieldContext.java b/src/main/java/semantic/context/FieldContext.java index aba5ba0..3869bb1 100644 --- a/src/main/java/semantic/context/FieldContext.java +++ b/src/main/java/semantic/context/FieldContext.java @@ -4,13 +4,15 @@ import ast.members.FieldNode; import ast.type.*; import ast.type.type.*; +import java.util.Objects; + public class FieldContext { private AccessModifierNode accessModifier; private ITypeNode type; public FieldContext(FieldNode field) { - accessModifier = field.accessTypeNode; + accessModifier = Objects.requireNonNullElseGet(field.accessTypeNode, () -> new AccessModifierNode("private")); type = field.type; } diff --git a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java index 5c3a97b..c6d54fb 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -1,63 +1,9 @@ public class AllFeaturesClassExample { - int a; - boolean b; - char c; + int x; - public void controlStructures(int adf, boolean bool) { - if (a > (10 + 8)) { - } else { - } - - - while (a > adf) { - a--; - } - - for (int i = 0; i < 5; i++) { - } - - - } - -// void logicalOperations() { -// // Logische UND-Operation -// if (b && a > 5) { -//// System.out.println("a ist größer als 5 und b ist wahr"); -// } -// -// // Logische ODER-Operation -// if (b || a < 5) { -//// System.out.println("b ist wahr oder a ist kleiner als 5"); -// } -// } - -// public static void main(String[] args) { -// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a'); -// obj.controlStructures(); -// } -} - -public class Test { - - public Car c; - - public int test(boolean b, int x) { - if (b == true) { - return c.getSpeed(); - } else { - return x; - } - } - -} - -public class Car { - - private int speed; - - public int getSpeed() { - return speed; + public boolean test(boolean x){ + return x; } }