From dcfafe59956e493f4bcd41adb2bddc02b735afc2 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 5 Feb 2024 14:46:00 +0100 Subject: [PATCH] Fix null literal --- resources/bytecode/javFiles/Literal.jav | 3 +++ src/main/java/de/dhbwstuttgart/bytecode/Codegen.java | 1 + .../target/generate/StatementToTargetExpression.java | 2 ++ .../target/tree/expression/TargetLiteral.java | 12 ++++++++++++ .../typeinference/typeAlgo/TYPEStmt.java | 3 ++- src/test/java/TestComplete.java | 9 +++++++++ 6 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/Literal.jav diff --git a/resources/bytecode/javFiles/Literal.jav b/resources/bytecode/javFiles/Literal.jav new file mode 100644 index 00000000..1d081e27 --- /dev/null +++ b/resources/bytecode/javFiles/Literal.jav @@ -0,0 +1,3 @@ +public class Literal { + m() { return null; } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 630982b2..aef180d3 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -806,6 +806,7 @@ public class Codegen { case StringLiteral stringLiteral -> mv.visitLdcInsn(stringLiteral.value()); case CharLiteral charLiteral -> mv.visitIntInsn(BIPUSH, charLiteral.value()); case DoubleLiteral doubleLiteral -> mv.visitLdcInsn(doubleLiteral.value()); + case Null ignored -> mv.visitInsn(ACONST_NULL); case BooleanLiteral booleanLiteral -> { if (booleanLiteral.value()) { mv.visitInsn(ICONST_1); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index c79dd3bb..702432a0 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -320,6 +320,8 @@ public class StatementToTargetExpression implements ASTVisitor { result = new TargetLiteral.StringLiteral((String) literal.value); } else if (literal.value instanceof Boolean) { result = new TargetLiteral.BooleanLiteral((boolean) literal.value); + } else if (literal.value == null) { + result = new TargetLiteral.Null(); } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java index c4673682..c120f966 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java @@ -53,4 +53,16 @@ public sealed interface TargetLiteral extends TargetExpression { return TargetType.String; } } + + record Null() implements TargetLiteral { + @Override + public TargetType type() { + return TargetType.Object; + } + + @Override + public Object value() { + return null; + } + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 5d6ce725..440bee30 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -451,7 +451,8 @@ public class TYPEStmt implements StatementVisitor { if (literal.value instanceof Boolean) { constraintsSet.addUndConstraint(new Pair(literal.getType(), bool, PairOperator.EQUALSDOT)); return; - } else { + } + if (literal.value != null) { throw new NotImplementedException(); } } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 784df1fc..56dc7aa5 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -816,4 +816,13 @@ public class TestComplete { } } } + + @Test + public void testLiteral() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Literal.jav"); + var clazz = classFiles.get("Literal"); + var instance = clazz.getDeclaredConstructor().newInstance(); + + assertNull(clazz.getDeclaredMethod("m").invoke(instance)); + } }