Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

transBoolExpr

Translator.transBoolExpr
fn transBoolExpr(t: *Translator, scope: *Scope, expr: Node.Index) TransError!ZigNode

File

lib/compiler/translate-c/Translator.zig:2406

Code

fn transBoolExpr(t: *Translator, scope: *Scope, expr: Node.Index) TransError!ZigNode {
    switch (expr.get(t.tree)) {
        .int_literal => {
            const int_val = t.tree.value_map.get(expr).?;
            return if (int_val.isZero(t.comp))
                ZigTag.false_literal.init()
            else
                ZigTag.true_literal.init();
        },
        .cast => |cast| switch (cast.kind) {
            .bool_to_int => return t.transExpr(scope, cast.operand, .used),
            .array_to_pointer => {
                const operand = cast.operand.get(t.tree);
                if (operand == .string_literal_expr) {
                    // @intFromPtr("foo") != 0, always true
                    const str = try t.transStringLiteral(scope, cast.operand, operand.string_literal_expr);
                    const int_from_ptr = try ZigTag.int_from_ptr.create(t.arena, str);
                    return ZigTag.not_equal.create(t.arena, .{ .lhs = int_from_ptr, .rhs = ZigTag.zero_literal.init() });
                }
            },
            else => {},
        },
        else => {},
    }

    const maybe_bool_res = try t.transExpr(scope, expr, .used);
    if (maybe_bool_res.isBoolRes()) {
        return maybe_bool_res;
    }

    return t.finishBoolExpr(expr.qt(t.tree), maybe_bool_res);
}