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.

renderPtrType

Render.renderPtrType
fn renderPtrType(r: *Render, ptr_type: Ast.full.PtrType, space: Space) Error!void

File

lib/std/zig/Ast/Render.zig:1024

Code

fn renderPtrType(r: *Render, ptr_type: Ast.full.PtrType, space: Space) Error!void {
    const tree = r.tree;
    const main_token = ptr_type.ast.main_token;

    switch (ptr_type.size) {
        .one => {
            try renderToken(r, main_token, .none); // asterisk
        },
        .many => {
            if (ptr_type.ast.sentinel.unwrap()) |sentinel| {
                try renderToken(r, main_token, .none); // lbracket
                try renderToken(r, main_token + 1, .none); // asterisk
                try renderToken(r, main_token + 2, .none); // colon
                try renderExpression(r, sentinel, .none);
                try renderToken(r, tree.lastToken(sentinel) + 1, .none); // rbracket
            } else {
                try renderToken(r, main_token, .none); // lbracket
                try renderToken(r, main_token + 1, .none); // asterisk
                try renderToken(r, main_token + 2, .none); // rbracket
            }
        },
        .c => {
            try renderToken(r, main_token, .none); // lbracket
            try renderToken(r, main_token + 1, .none); // asterisk
            try renderToken(r, main_token + 2, .none); // c
            try renderToken(r, main_token + 3, .none); // rbracket
        },
        .slice => {
            if (ptr_type.ast.sentinel.unwrap()) |sentinel| {
                try renderToken(r, main_token, .none); // lbracket
                try renderToken(r, main_token + 1, .none); // colon
                try renderExpression(r, sentinel, .none);
                try renderToken(r, tree.lastToken(sentinel) + 1, .none); // rbracket
            } else {
                try renderToken(r, main_token, .none); // lbracket
                try renderToken(r, main_token + 1, .none); // rbracket
            }
        },
    }

    // .maybe_space cannot be used at the end of each qualifier since they may be reordered
    const final_qual: enum {
        @"volatile",
        @"const",
        @"addrspace",
        @"align",
        @"allowzero",
        none,
    } = if (ptr_type.volatile_token != null)
        .@"volatile"
    else if (ptr_type.const_token != null)
        .@"const"
    else if (ptr_type.ast.addrspace_node != .none)
        .@"addrspace"
    else if (ptr_type.ast.align_node != .none)
        .@"align"
    else if (ptr_type.allowzero_token != null)
        .@"allowzero"
    else
        .none;
    const final_qual_space: Space = if (tree.tokenTag(tree.firstToken(ptr_type.ast.child_type)) !=
        .multiline_string_literal_line) .space else .none;

    if (ptr_type.allowzero_token) |allowzero_token| {
        const this_space: Space = if (final_qual == .@"allowzero") final_qual_space else .space;
        try renderToken(r, allowzero_token, this_space);
    }

    if (ptr_type.ast.align_node.unwrap()) |align_node| {
        const this_space: Space = if (final_qual == .@"align") final_qual_space else .space;
        const align_first = tree.firstToken(align_node);
        try renderToken(r, align_first - 2, .none); // align
        try renderToken(r, align_first - 1, .none); // lparen
        try renderExpression(r, align_node, .none);
        if (ptr_type.ast.bit_range_start.unwrap()) |bit_range_start| {
            const bit_range_end = ptr_type.ast.bit_range_end.unwrap().?;
            try renderToken(r, tree.firstToken(bit_range_start) - 1, .none); // colon
            try renderExpression(r, bit_range_start, .none);
            try renderToken(r, tree.firstToken(bit_range_end) - 1, .none); // colon
            try renderExpression(r, bit_range_end, .none);
            try renderToken(r, tree.lastToken(bit_range_end) + 1, this_space); // rparen
        } else {
            try renderToken(r, tree.lastToken(align_node) + 1, this_space); // rparen
        }
    }

    if (ptr_type.ast.addrspace_node.unwrap()) |addrspace_node| {
        const this_space: Space = if (final_qual == .@"addrspace") final_qual_space else .space;
        const addrspace_first = tree.firstToken(addrspace_node);
        try renderToken(r, addrspace_first - 2, .none); // addrspace
        try renderToken(r, addrspace_first - 1, .none); // lparen
        try renderExpression(r, addrspace_node, .none);
        try renderToken(r, tree.lastToken(addrspace_node) + 1, this_space); // rparen
    }

    if (ptr_type.const_token) |const_token| {
        const this_space: Space = if (final_qual == .@"const") final_qual_space else .space;
        try renderToken(r, const_token, this_space);
    }

    if (ptr_type.volatile_token) |volatile_token| {
        const this_space: Space = if (final_qual == .@"volatile") final_qual_space else unreachable;
        try renderToken(r, volatile_token, this_space);
    }

    try renderExpression(r, ptr_type.ast.child_type, space);
}