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.

parseCUnaryExpr

MacroTranslator.parseCUnaryExpr
fn parseCUnaryExpr(mt: *MacroTranslator, scope: *Scope) ParseError!ZigNode

File

lib/compiler/translate-c/MacroTranslator.zig:1366

Code

fn parseCUnaryExpr(mt: *MacroTranslator, scope: *Scope) ParseError!ZigNode {
    switch (mt.peek()) {
        .bang => {
            mt.i += 1;
            const operand = try mt.macroIntToBool(try mt.parseCCastExpr(scope));
            return ZigTag.not.create(mt.t.arena, operand);
        },
        .minus => {
            mt.i += 1;
            const operand = try mt.macroIntFromBool(try mt.parseCCastExpr(scope));
            return ZigTag.negate.create(mt.t.arena, operand);
        },
        .plus => {
            mt.i += 1;
            return try mt.parseCCastExpr(scope);
        },
        .tilde => {
            mt.i += 1;
            const operand = try mt.macroIntFromBool(try mt.parseCCastExpr(scope));
            return ZigTag.bit_not.create(mt.t.arena, operand);
        },
        .asterisk => {
            mt.i += 1;
            const operand = try mt.parseCCastExpr(scope);
            return ZigTag.deref.create(mt.t.arena, operand);
        },
        .ampersand => {
            mt.i += 1;
            const operand = try mt.parseCCastExpr(scope);
            return ZigTag.address_of.create(mt.t.arena, operand);
        },
        .keyword_sizeof => {
            mt.i += 1;
            const operand = if (mt.eat(.l_paren)) blk: {
                const inner = (try mt.parseCTypeName(scope)) orelse try mt.parseCUnaryExpr(scope);
                try mt.expect(.r_paren);
                break :blk inner;
            } else try mt.parseCUnaryExpr(scope);

            return mt.t.createHelperCallNode(.sizeof, &.{operand});
        },
        .keyword_alignof => {
            mt.i += 1;
            // TODO this won't work if using <stdalign.h>'s
            // #define alignof _Alignof
            try mt.expect(.l_paren);
            const operand = (try mt.parseCTypeName(scope)) orelse try mt.parseCUnaryExpr(scope);
            try mt.expect(.r_paren);

            return ZigTag.alignof.create(mt.t.arena, operand);
        },
        .plus_plus, .minus_minus => {
            try mt.fail("TODO unary inc/dec expr", .{});
            return error.ParseError;
        },
        else => {},
    }

    return try mt.parseCPostfixExpr(scope, null);
}