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.

transDeclRefExpr

Translator.transDeclRefExpr
fn transDeclRefExpr(t: *Translator, scope: *Scope, decl_ref: Node.DeclRef) TransError!ZigNode

File

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

Code

fn transDeclRefExpr(t: *Translator, scope: *Scope, decl_ref: Node.DeclRef) TransError!ZigNode {
    if (t.wip_var_inits.contains(decl_ref.decl)) return error.SelfReferential;

    const name = t.tree.tokSlice(decl_ref.name_tok);
    const maybe_alias = scope.getAlias(name);
    const mangled_name = maybe_alias orelse name;

    switch (decl_ref.decl.get(t.tree)) {
        .function => |function| if (function.definition == null and function.body == null) {
            // Try translating the decl again in case of out of scope declaration.
            try t.transFnDecl(scope, function);
        },
        else => {},
    }

    const decl = decl_ref.decl.get(t.tree);
    const ref_expr = blk: {
        const identifier = try ZigTag.identifier.create(t.arena, mangled_name);
        if (decl_ref.qt.is(t.comp, .func) and maybe_alias != null) {
            break :blk try ZigTag.field_access.create(t.arena, .{
                .lhs = identifier,
                .field_name = name,
            });
        }
        if (decl == .variable and maybe_alias != null) {
            switch (decl.variable.storage_class) {
                .@"extern", .static => {
                    break :blk try ZigTag.field_access.create(t.arena, .{
                        .lhs = identifier,
                        .field_name = name,
                    });
                },
                else => {},
            }
        }
        break :blk identifier;
    };

    scope.skipVariableDiscard(mangled_name);
    return ref_expr;
}