Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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 standard library. Public Enum

Index

Public Enum
std.zig.llvm.Builder.Global.Index
pub const Index = enum(u32)

Structs

Types

Functions

pub fn unwrap(orig_index: Index, builder: *const Builder) Index
pub fn eql(self: Index, other: Index, builder: *const Builder) bool
pub fn ptr(self: Index, builder: *Builder) *Global
pub fn ptrConst(self: Index, builder: *const Builder) *const Global
pub fn name(self: Index, builder: *const Builder) StrtabString
pub fn typeOf(self: Index, builder: *const Builder) Type
pub fn toConst(global: Index) Constant
pub fn toValue(global: Index) Value
pub fn setLinkage(self: Index, linkage: Linkage, builder: *Builder) void
pub fn setVisibility(self: Index, visibility: Visibility, builder: *Builder) void
pub fn setDllStorageClass(self: Index, class: DllStorageClass, builder: *Builder) void
pub fn setUnnamedAddr(self: Index, unnamed_addr: UnnamedAddr, builder: *Builder) void
pub fn setDebugMetadata(self: Index, dbg: Metadata, builder: *Builder) void
pub fn getDebugMetadata(self: Index, builder: *const Builder) Metadata.Optional
fn format(data: FormatData, w: *Writer) Writer.Error!void
pub fn fmt(self: Index, builder: *const Builder) std.fmt.Alt(FormatData, format)
pub fn rename(self: Index, new_name: StrtabString, builder: *Builder) Allocator.Error!void
pub fn takeName(self: Index, other: Index, builder: *Builder) Allocator.Error!void
pub fn replace(self: Index, other: Index, builder: *Builder) Allocator.Error!void
pub fn delete(self: Index, builder: *Builder) void
pub fn toNewFunction(global: Index, builder: *Builder) Allocator.Error!Function.Index

Replaces whatever this Global currently contains with a new Function. Similar to Builder.addFunction, but the same Global is reused.

pub fn toNewVariable(global: Index, builder: *Builder) Allocator.Error!Variable.Index

Replaces whatever this Global currently contains with a new Variable. Similar to Builder.addVariable, but the same Global is reused.

pub fn toNewAlias(global: Index, builder: *Builder) Allocator.Error!Alias.Index

Replaces whatever this Global currently contains with a new Alias. Similar to Builder.addAlias, but the same Global is reused.

fn updateDsoLocal(self: Index, builder: *Builder) void
fn renameAssumeCapacity(self: Index, new_name: StrtabString, builder: *Builder) void
fn takeNameAssumeCapacity(self: Index, other: Index, builder: *Builder) void
fn replaceAssumeCapacity(self: Index, other: Index, builder: *Builder) void

File

zig/llvm/Builder.zig:3288
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
    pub const Index = enum(u32) {
        none = maxInt(u32),
        _,

        pub fn unwrap(orig_index: Index, builder: *const Builder) Index {
            var cur = orig_index;
            while (true) {
                switch (builder.globals.values()[@backingInt(cur)].kind) {
                    .replaced => |replacement| cur = replacement,
                    else => return cur,
                }
            }
        }

        pub fn eql(self: Index, other: Index, builder: *const Builder) bool {
            return self.unwrap(builder) == other.unwrap(builder);
        }

        pub fn ptr(self: Index, builder: *Builder) *Global {
            return &builder.globals.values()[@backingInt(self.unwrap(builder))];
        }

        pub fn ptrConst(self: Index, builder: *const Builder) *const Global {
            return &builder.globals.values()[@backingInt(self.unwrap(builder))];
        }

        pub fn name(self: Index, builder: *const Builder) StrtabString {
            return builder.globals.keys()[@backingInt(self.unwrap(builder))];
        }

        pub fn strtab(self: Index, builder: *const Builder) struct {
            offset: u32,
            size: u32,
        } {
            const name_index = self.name(builder).toIndex() orelse return .{
                .offset = 0,
                .size = 0,
            };

            return .{
                .offset = builder.strtab_string_indices.items[name_index],
                .size = builder.strtab_string_indices.items[name_index + 1] -
                    builder.strtab_string_indices.items[name_index],
            };
        }

        pub fn typeOf(self: Index, builder: *const Builder) Type {
            return self.ptrConst(builder).type;
        }

        pub fn toConst(global: Index) Constant {
            return @fromBackingInt(@backingInt(Constant.first_global) + @backingInt(global));
        }

        pub fn toValue(global: Index) Value {
            return global.toConst().toValue();
        }

        pub fn setLinkage(self: Index, linkage: Linkage, builder: *Builder) void {
            self.ptr(builder).linkage = linkage;
            self.updateDsoLocal(builder);
        }

        pub fn setVisibility(self: Index, visibility: Visibility, builder: *Builder) void {
            self.ptr(builder).visibility = visibility;
            self.updateDsoLocal(builder);
        }

        pub fn setDllStorageClass(self: Index, class: DllStorageClass, builder: *Builder) void {
            self.ptr(builder).dll_storage_class = class;
        }

        pub fn setUnnamedAddr(self: Index, unnamed_addr: UnnamedAddr, builder: *Builder) void {
            self.ptr(builder).unnamed_addr = unnamed_addr;
        }

        pub fn setDebugMetadata(self: Index, dbg: Metadata, builder: *Builder) void {
            self.ptr(builder).dbg = dbg.toOptional();
        }

        pub fn getDebugMetadata(self: Index, builder: *const Builder) Metadata.Optional {
            return self.ptrConst(builder).dbg;
        }

        const FormatData = struct {
            global: Index,
            builder: *const Builder,
        };
        fn format(data: FormatData, w: *Writer) Writer.Error!void {
            try w.print("@{f}", .{
                data.global.unwrap(data.builder).name(data.builder).fmt(data.builder, .quote_unless_valid_identifier),
            });
        }
        pub fn fmt(self: Index, builder: *const Builder) std.fmt.Alt(FormatData, format) {
            return .{ .data = .{ .global = self, .builder = builder } };
        }

        pub fn rename(self: Index, new_name: StrtabString, builder: *Builder) Allocator.Error!void {
            try builder.ensureUnusedGlobalCapacity(new_name);
            self.renameAssumeCapacity(new_name, builder);
        }

        pub fn takeName(self: Index, other: Index, builder: *Builder) Allocator.Error!void {
            try builder.ensureUnusedGlobalCapacity(.empty);
            self.takeNameAssumeCapacity(other, builder);
        }

        pub fn replace(self: Index, other: Index, builder: *Builder) Allocator.Error!void {
            try builder.ensureUnusedGlobalCapacity(.empty);
            self.replaceAssumeCapacity(other, builder);
        }

        pub fn delete(self: Index, builder: *Builder) void {
            self.ptr(builder).kind = .{ .replaced = .none };
        }

        /// Replaces whatever this `Global` currently contains with a new `Function`. Similar to
        /// `Builder.addFunction`, but the same `Global` is reused.
        pub fn toNewFunction(global: Index, builder: *Builder) Allocator.Error!Function.Index {
            try builder.functions.ensureUnusedCapacity(builder.gpa, 1);
            errdefer comptime unreachable;
            const function: Function.Index = @fromBackingInt(@intCast(builder.functions.items.len));
            builder.functions.appendAssumeCapacity(.{
                .global = global,
                .strip = undefined,
            });
            global.ptr(builder).kind = .{ .function = function };
            return function;
        }

        /// Replaces whatever this `Global` currently contains with a new `Variable`. Similar to
        /// `Builder.addVariable`, but the same `Global` is reused.
        pub fn toNewVariable(global: Index, builder: *Builder) Allocator.Error!Variable.Index {
            try builder.variables.ensureUnusedCapacity(builder.gpa, 1);
            errdefer comptime unreachable;
            const variable: Variable.Index = @fromBackingInt(@intCast(builder.variables.items.len));
            builder.variables.appendAssumeCapacity(.{ .global = global });
            global.ptr(builder).kind = .{ .variable = variable };
            return variable;
        }

        /// Replaces whatever this `Global` currently contains with a new `Alias`. Similar to
        /// `Builder.addAlias`, but the same `Global` is reused.
        pub fn toNewAlias(global: Index, builder: *Builder) Allocator.Error!Alias.Index {
            try builder.aliases.ensureUnusedCapacity(builder.gpa, 1);
            errdefer comptime unreachable;
            const alias: Alias.Index = @fromBackingInt(@intCast(builder.aliases.items.len));
            builder.aliass.appendAssumeCapacity(.{ .global = global, .aliasee = .none });
            global.ptr(builder).kind = .{ .alias = alias };
            return alias;
        }

        fn updateDsoLocal(self: Index, builder: *Builder) void {
            const self_ptr = self.ptr(builder);
            switch (self_ptr.linkage) {
                .private, .internal => {
                    self_ptr.visibility = .default;
                    self_ptr.dll_storage_class = .default;
                    self_ptr.preemption = .implicit_dso_local;
                },
                .extern_weak => if (self_ptr.preemption == .implicit_dso_local) {
                    self_ptr.preemption = .dso_local;
                },
                else => switch (self_ptr.visibility) {
                    .default => if (self_ptr.preemption == .implicit_dso_local) {
                        self_ptr.preemption = .dso_local;
                    },
                    else => self_ptr.preemption = .implicit_dso_local,
                },
            }
        }

        fn renameAssumeCapacity(self: Index, new_name: StrtabString, builder: *Builder) void {
            const old_name = self.name(builder);
            if (new_name == old_name) return;
            const index = @backingInt(self.unwrap(builder));
            builder.globals.setKey(index, new_name);
            if (!old_name.isAnon()) return;
            builder.next_unnamed_global = @fromBackingInt(@backingInt(builder.next_unnamed_global) - 1);
            if (builder.next_unnamed_global == old_name) return;
            builder.getGlobal(builder.next_unnamed_global).?.renameAssumeCapacity(old_name, builder);
        }

        fn takeNameAssumeCapacity(self: Index, other: Index, builder: *Builder) void {
            const other_name = other.name(builder);
            other.renameAssumeCapacity(.empty, builder);
            self.renameAssumeCapacity(other_name, builder);
        }

        fn replaceAssumeCapacity(self: Index, other: Index, builder: *Builder) void {
            if (self.eql(other, builder)) return;
            builder.next_replaced_global = @fromBackingInt(@backingInt(builder.next_replaced_global) - 1);
            self.renameAssumeCapacity(builder.next_replaced_global, builder);
            self.ptr(builder).kind = .{ .replaced = other.unwrap(builder) };
        }
    }