Null type in Truffle
Dain Sundstrom
dain at iq80.com
Sat Aug 3 14:26:55 PDT 2013
Another long question :)
How do I add a null type into the type system (e.g., Ruby nil, Python none, or SQL unknown)?
Here is what I have tried for SQL unknown. In SQL you can have expressions like "42 + null" where the null doesn't really have a type, but the full expression is numeric type with a null value. To make this construct work, I created a class Unknown with no implementations. I added Unknown as the first element in @TypeSystem. Then I was forced to add a @TypeCheck:
@TypeCheck
public boolean isUnknown(Object value)
{
return value == null;
}
Then added this method to my base ExpressionNode:
public Unknown executeUnknown(VirtualFrame frame)
throws UnexpectedResultException
{
return SqlTypesGen.SQLTYPES.expectUnknown(execute(frame));
}
I added a UnknownLiteral node:
public static class UnknownLiteral
extends LiteralNode
{
public Unknown execute(VirtualFrame frame)
{
return null;
}
}
And finally I added these methods to my BinaryNode:
@Specialization(order = 100)
public Unknown unknownAndAnything(Unknown left, Object right)
{
return null;
}
@Specialization(order = 101)
public Unknown anythingAndUnknown(Object left, Unknown right)
{
return null;
}
@Specialization(order = 102)
public Unknown unknownAndUnknown(Unknown left, Unknown right)
{
return null;
}
The order parameter is there because the DSL is now forcing met to add order to all of my methods like this in my Add node:
@Specialization(order = 1, rewriteOn = ArithmeticException.class)
public long addLong(long left, long right)
{
return ExactMath.addExact(left, right);
}
In general, the isUnknown() type check method doesn't seem right, and the order declaration is really annoying. Is there an easier way?
-dain
More information about the graal-dev
mailing list