feature. See also
. The project being documented here (as the example) is the Zig library itself.
Translator.transCharLiteral
fn transCharLiteral(
t: *Translator,
scope: *Scope,
literal_index: Node.Index,
used: ResultUsed,
suppress_as: SuppressCast,
) TransError!ZigNode
File
Code
fn transCharLiteral(
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 char_literal = literal_index.get(t.tree).char_literal;
const narrow = char_literal.kind == .ascii or char_literal.kind == .utf8;
// e.g. 'abcd'
const int_value = val.toInt(u32, t.comp).?;
const int_lit_node = if (char_literal.kind == .ascii and int_value > 255)
try t.createNumberNode(int_value)
else
try t.createCharLiteralNode(narrow, int_value);
if (suppress_as == .no_as) {
return t.maybeSuppressResult(used, int_lit_node);
}
// @as(T, x)
const as_node = try ZigTag.as.create(t.arena, .{
.lhs = try t.transType(scope, char_literal.qt, char_literal.literal_tok),
.rhs = int_lit_node,
});
return t.maybeSuppressResult(used, as_node);
}