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.

parseCSpecifierQualifierList

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

File

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

Code

fn parseCSpecifierQualifierList(mt: *MacroTranslator, scope: *Scope) ParseError!?ZigNode {
    const tok = mt.peek();
    switch (tok) {
        .macro_param, .macro_param_no_expand => {
            const param = mt.macro.params[mt.tokens[mt.i].end];

            // Assume that this is only a cast if the next token is ')'
            // e.g. param)identifier
            if (mt.macro.tokens.len < mt.i + 3 or
                mt.macro.tokens[mt.i + 1].id != .r_paren or
                mt.macro.tokens[mt.i + 2].id != .identifier)
                return null;

            mt.i += 1;
            const mangled_name = scope.getAlias(param) orelse param;
            return try ZigTag.identifier.create(mt.t.arena, mangled_name);
        },
        .identifier, .extended_identifier => {
            const slice = mt.tokSlice();
            const mangled_name = scope.getAlias(slice) orelse slice;

            if (mt.t.global_scope.blank_macros.contains(slice)) {
                mt.i += 1;
                return try mt.parseCSpecifierQualifierList(scope);
            }

            if (mt.t.typedefs.contains(mangled_name)) {
                mt.i += 1;
                if (Translator.builtin_typedef_map.get(mangled_name)) |ty| {
                    return try ZigTag.type.create(mt.t.arena, ty);
                }
                if (builtins.map.get(mangled_name)) |builtin| {
                    const builtin_identifier = try ZigTag.identifier.create(mt.t.arena, "__builtin");
                    return try ZigTag.field_access.create(mt.t.arena, .{
                        .lhs = builtin_identifier,
                        .field_name = builtin.name,
                    });
                }

                return try ZigTag.identifier.create(mt.t.arena, mangled_name);
            }
        },
        .keyword_void => {
            mt.i += 1;
            return try ZigTag.type.create(mt.t.arena, "anyopaque");
        },
        .keyword_bool => {
            mt.i += 1;
            return try ZigTag.type.create(mt.t.arena, "bool");
        },
        .keyword_char,
        .keyword_int,
        .keyword_short,
        .keyword_long,
        .keyword_float,
        .keyword_double,
        .keyword_signed,
        .keyword_unsigned,
        .keyword_complex,
        => return try mt.parseCNumericType(),
        .keyword_enum, .keyword_struct, .keyword_union => {
            const tag_name = mt.tokSlice();
            mt.i += 1;
            if (mt.peek() != .identifier) {
                mt.i -= 1;
                return null;
            }

            // struct Foo will be declared as struct_Foo by transRecordDecl
            const identifier = mt.tokSlice();
            try mt.expect(.identifier);

            const name = try std.fmt.allocPrint(mt.t.arena, "{s}_{s}", .{ tag_name, identifier });
            if (!mt.t.global_scope.contains(name)) {
                try mt.fail("unable to translate C expr: '{s}' not found", .{name});
                return error.ParseError;
            }

            return try ZigTag.identifier.create(mt.t.arena, name);
        },
        else => {},
    }

    return null;
}