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.

transShufflevectorExpr

Translator.transShufflevectorExpr
fn transShufflevectorExpr(
    t: *Translator,
    scope: *Scope,
    shufflevector: Node.Shufflevector,
) TransError!ZigNode

File

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

Code

fn transShufflevectorExpr(
    t: *Translator,
    scope: *Scope,
    shufflevector: Node.Shufflevector,
) TransError!ZigNode {
    if (shufflevector.indexes.len == 0) {
        return t.fail(error.UnsupportedTranslation, shufflevector.builtin_tok, "@shuffle needs at least 1 index", .{});
    }

    const a = try t.transExpr(scope, shufflevector.lhs, .used);
    const b = try t.transExpr(scope, shufflevector.rhs, .used);

    // First two arguments to __builtin_shufflevector must be the same type
    const vector_child_type = try t.vectorTypeInfo(a, "child");
    const vector_len = try t.vectorTypeInfo(a, "len");
    const shuffle_mask = blk: {
        const mask_len = shufflevector.indexes.len;

        const mask_type = try ZigTag.vector.create(t.arena, .{
            .lhs = try t.createNumberNode(mask_len),
            .rhs = try ZigTag.type.create(t.arena, "i32"),
        });

        const init_list = try t.arena.alloc(ZigNode, mask_len);
        for (init_list, shufflevector.indexes) |*init, index| {
            const index_expr = try t.transExprCoercing(scope, index, .used);
            const converted_index = try t.createHelperCallNode(.shuffleVectorIndex, &.{ index_expr, vector_len });
            init.* = converted_index;
        }

        break :blk try ZigTag.array_init.create(t.arena, .{
            .cond = mask_type,
            .cases = init_list,
        });
    };

    return ZigTag.shuffle.create(t.arena, .{
        .element_type = vector_child_type,
        .a = a,
        .b = b,
        .mask_vector = shuffle_mask,
    });
}