feature. See also
. The project being documented here (as the example) is the Zig library itself.
Translator.transIntLiteral
fn transIntLiteral(
t: *Translator,
scope: *Scope,
literal_index: Node.Index,
used: ResultUsed,
suppress_as: SuppressCast,
) TransError!ZigNode
File
Code
fn transIntLiteral(
t: *Translator,
scope: *Scope,
literal_index: Node.Index,
used: ResultUsed,
suppress_as: SuppressCast,
) TransError!ZigNode {
if (try t.checkLiteralMacro(literal_index.tok(t.tree), used)) |node| return node;
const val = t.tree.value_map.get(literal_index).?;
const int_lit_node = try t.createIntNode(val);
if (suppress_as == .no_as) {
return t.maybeSuppressResult(used, int_lit_node);
}
// For example, this is valid C:
// unsigned char y = 256;
// How this gets evaluated is the 256 is an integer, which gets truncated to signed char, then bit-casted
// to unsigned char, resulting in 0. In order for this to work, we have to emit this zig code:
// var y = @as(u8, @bitCast(@as(i8, @truncate(@as(c_int, 256)))));
// @as(T, x)
const ty_node = try t.transType(scope, literal_index.qt(t.tree), literal_index.tok(t.tree));
const as = try ZigTag.as.create(t.arena, .{ .lhs = ty_node, .rhs = int_lit_node });
return t.maybeSuppressResult(used, as);
}