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.

transBuiltinCall

Translator.transBuiltinCall
fn transBuiltinCall(
    t: *Translator,
    scope: *Scope,
    call: Node.BuiltinCall,
    used: ResultUsed,
) TransError!ZigNode

File

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

Code

fn transBuiltinCall(
    t: *Translator,
    scope: *Scope,
    call: Node.BuiltinCall,
    used: ResultUsed,
) TransError!ZigNode {
    const builtin_name = t.tree.tokSlice(call.builtin_tok);
    if (std.mem.eql(u8, builtin_name, "__builtin_offsetof")) {
        const res = try t.transOffsetof(scope, call.args[0]);
        return t.maybeSuppressResult(used, res);
    }

    const builtin = builtins.map.get(builtin_name) orelse
        return t.fail(error.UnsupportedTranslation, call.builtin_tok, "TODO implement function '{s}' in std.zig.c_builtins", .{builtin_name});

    if (builtin.tag) |tag| switch (tag) {
        .byte_swap, .ceil, .cos, .sin, .exp, .exp2, .exp10, .abs, .log, .log2, .log10, .round, .sqrt, .trunc, .floor => {
            assert(call.args.len == 1);
            const arg = try t.transExprCoercing(scope, call.args[0], .used);
            const arg_ty = try t.transType(scope, call.args[0].qt(t.tree), call.args[0].tok(t.tree));
            const coerced = try ZigTag.as.create(t.arena, .{ .lhs = arg_ty, .rhs = arg });

            const ptr = try t.arena.create(ast.Payload.UnOp);
            ptr.* = .{ .base = .{ .tag = tag }, .data = coerced };
            return t.maybeSuppressResult(used, ZigNode.initPayload(&ptr.base));
        },
        .@"unreachable" => return ZigTag.@"unreachable".init(),
        else => unreachable,
    };

    const arg_nodes = try t.arena.alloc(ZigNode, call.args.len);
    for (call.args, arg_nodes) |c_arg, *zig_arg| {
        zig_arg.* = try t.transExprCoercing(scope, c_arg, .used);
    }

    const builtin_identifier = try ZigTag.identifier.create(t.arena, "__builtin");
    const field_access = try ZigTag.field_access.create(t.arena, .{
        .lhs = builtin_identifier,
        .field_name = builtin.name,
    });

    const res = try ZigTag.call.create(t.arena, .{
        .lhs = field_access,
        .args = arg_nodes,
    });
    if (call.qt.is(t.comp, .void)) return res;
    return t.maybeSuppressResult(used, res);
}