Fix null literal

This commit is contained in:
Daniel Holle 2024-02-05 14:46:00 +01:00
parent a035589647
commit dcfafe5995
6 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,3 @@
public class Literal {
m() { return null; }
}

View File

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

View File

@ -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();
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}

View File

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