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.

transCaseStmt

Collects all items for this case, returns the first statement after the labels. If items ends up empty, the prong should be translated as an else.

Translator.transCaseStmt
fn transCaseStmt(
    t: *Translator,
    scope: *Scope,
    stmt: Node.Index,
    items: *std.ArrayList(ZigNode),
) TransError!Node.Index

File

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

Code

fn transCaseStmt(
    t: *Translator,
    scope: *Scope,
    stmt: Node.Index,
    items: *std.ArrayList(ZigNode),
) TransError!Node.Index {
    var sub = stmt;
    var seen_default = false;
    while (true) {
        switch (sub.get(t.tree)) {
            .default_stmt => |default_stmt| {
                seen_default = true;
                items.items.len = 0;
                sub = default_stmt.body;
            },
            .case_stmt => |case_stmt| {
                if (seen_default) {
                    items.items.len = 0;
                    sub = case_stmt.body;
                    continue;
                }

                const expr = if (case_stmt.end) |end| blk: {
                    const start_node = try t.transExpr(scope, case_stmt.start, .used);
                    const end_node = try t.transExpr(scope, end, .used);

                    break :blk try ZigTag.ellipsis3.create(t.arena, .{ .lhs = start_node, .rhs = end_node });
                } else try t.transExpr(scope, case_stmt.start, .used);

                try items.append(t.gpa, expr);
                sub = case_stmt.body;
            },
            else => return sub,
        }
    }
}