zig/lib/std / builtin.zig

explicit_subsystem is missing when the subsystem is automatically detected, so Zig standard library has the subsystem detection logic here. This should generally be used rather than explicit_subsystem. On non-Windows targets, this is null.

const builtin = @import("builtin");

subsystem:

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// `explicit_subsystem` is missing when the subsystem is automatically detected,
/// so Zig standard library has the subsystem detection logic here. This should generally be
/// used rather than `explicit_subsystem`.
/// On non-Windows targets, this is `null`.
pub const subsystem: ?std.Target.SubSystem = blk: {
    if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem;
    switch (builtin.os.tag) {
        .windows => {
            if (builtin.is_test) {
                break :blk std.Target.SubSystem.Console;
            }
            if (@hasDecl(root, "main") or
                @hasDecl(root, "WinMain") or
                @hasDecl(root, "wWinMain") or
                @hasDecl(root, "WinMainCRTStartup") or
                @hasDecl(root, "wWinMainCRTStartup"))
            {
                break :blk std.Target.SubSystem.Windows;
            } else {
                break :blk std.Target.SubSystem.Console;
            }
        },
        else => break :blk null,
    }
};

StackTrace

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const StackTrace = struct {
    index: usize,
    instruction_addresses: []usize,

format()

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    pub fn format(
        self: StackTrace,
        comptime fmt: []const u8,
        options: std.fmt.FormatOptions,
        writer: anytype,
    ) !void {
        if (fmt.len != 0) std.fmt.invalidFmtError(fmt, self);

GlobalLinkage

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


        // TODO: re-evaluate whether to use format() methods at all.
        // Until then, avoid an error when using GeneralPurposeAllocator with WebAssembly
        // where it tries to call detectTTYConfig here.
        if (builtin.os.tag == .freestanding) return;

SymbolVisibility

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


        _ = options;
        var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
        defer arena.deinit();
        const debug_info = std.debug.getSelfDebugInfo() catch |err| {
            return writer.print("\nUnable to print stack trace: Unable to open debug info: {s}\n", .{@errorName(err)});
        };
        const tty_config = std.io.tty.detectConfig(std.io.getStdErr());
        try writer.writeAll("\n");
        std.debug.writeStackTrace(self, writer, arena.allocator(), debug_info, tty_config) catch |err| {
            try writer.print("Unable to print stack trace: {s}\n", .{@errorName(err)});
        };
    }
};

AtomicOrder

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const GlobalLinkage = enum {
    Internal,
    Strong,
    Weak,
    LinkOnce,
};

ReduceOp

The code model puts constraints on the location of symbols and the size of code and data. The selection of a code model is a trade off on speed and restrictions that needs to be selected on a per application basis to meet its requirements. A slightly more detailed explanation can be found in (for example) the [System V Application Binary Interface (x86_64)](https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf) 3.5.1. This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const SymbolVisibility = enum {
    default,
    hidden,
    protected,
};

AtomicRmwOp

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const AtomicOrder = enum {
    Unordered,
    Monotonic,
    Acquire,
    Release,
    AcqRel,
    SeqCst,
};

CodeModel

Deprecated; use OptimizeMode.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const ReduceOp = enum {
    And,
    Or,
    Xor,
    Min,
    Max,
    Add,
    Mul,
};

OptimizeMode

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const AtomicRmwOp = enum {
    Xchg,
    Add,
    Sub,
    And,
    Nand,
    Or,
    Xor,
    Max,
    Min,
};

Mode

This is the default Zig calling convention used when not using export on fn and no other calling convention is specified.


/// The code model puts constraints on the location of symbols and the size of code and data.
/// The selection of a code model is a trade off on speed and restrictions that needs to be selected on a per application basis to meet its requirements.
/// A slightly more detailed explanation can be found in (for example) the [System V Application Binary Interface (x86_64)](https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf) 3.5.1.
///
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const CodeModel = enum {
    default,
    tiny,
    small,
    kernel,
    medium,
    large,
};

CallingConvention

Matches the C ABI for the target. This is the default calling convention when using export on fn and no other calling convention is specified.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const OptimizeMode = enum {
    Debug,
    ReleaseSafe,
    ReleaseFast,
    ReleaseSmall,
};

AddressSpace

This makes a function not have any function prologue or epilogue, making the function itself uncallable in regular Zig code. This can be useful when integrating with assembly.


/// Deprecated; use OptimizeMode.
pub const Mode = OptimizeMode;

SourceLocation

Functions with this calling convention are called asynchronously, as if called as async function().


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const CallingConvention = enum(u8) {
    /// This is the default Zig calling convention used when not using `export` on `fn`
    /// and no other calling convention is specified.
    Unspecified,
    /// Matches the C ABI for the target.
    /// This is the default calling convention when using `export` on `fn`
    /// and no other calling convention is specified.
    C,
    /// This makes a function not have any function prologue or epilogue,
    /// making the function itself uncallable in regular Zig code.
    /// This can be useful when integrating with assembly.
    Naked,
    /// Functions with this calling convention are called asynchronously,
    /// as if called as `async function()`.
    Async,
    /// Functions with this calling convention are inlined at all call sites.
    Inline,
    /// x86-only.
    Interrupt,
    Signal,
    /// x86-only.
    Stdcall,
    /// x86-only.
    Fastcall,
    /// x86-only.
    Vectorcall,
    /// x86-only.
    Thiscall,
    /// ARM Procedure Call Standard (obsolete)
    /// ARM-only.
    APCS,
    /// ARM Architecture Procedure Call Standard (current standard)
    /// ARM-only.
    AAPCS,
    /// ARM Architecture Procedure Call Standard Vector Floating-Point
    /// ARM-only.
    AAPCSVFP,
    /// x86-64-only.
    SysV,
    /// x86-64-only.
    Win64,
    /// AMD GPU, NVPTX, or SPIR-V kernel
    Kernel,
};

TypeId

Functions with this calling convention are inlined at all call sites.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const AddressSpace = enum(u5) {
    // CPU address spaces.
    generic,
    gs,
    fs,
    ss,

Type

x86-only.


    // GPU address spaces.
    global,
    constant,
    param,
    shared,
    local,

Int

x86-only.


    // AVR address spaces.
    flash,
    flash1,
    flash2,
    flash3,
    flash4,
    flash5,
};

Float

x86-only.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const SourceLocation = struct {
    file: [:0]const u8,
    fn_name: [:0]const u8,
    line: u32,
    column: u32,
};

Pointer

x86-only.


pub const TypeId = std.meta.Tag(Type);

Size

x86-only.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Type = union(enum) {
    Type: void,
    Void: void,
    Bool: void,
    NoReturn: void,
    Int: Int,
    Float: Float,
    Pointer: Pointer,
    Array: Array,
    Struct: Struct,
    ComptimeFloat: void,
    ComptimeInt: void,
    Undefined: void,
    Null: void,
    Optional: Optional,
    ErrorUnion: ErrorUnion,
    ErrorSet: ErrorSet,
    Enum: Enum,
    Union: Union,
    Fn: Fn,
    Opaque: Opaque,
    Frame: Frame,
    AnyFrame: AnyFrame,
    Vector: Vector,
    EnumLiteral: void,

Array

ARM Procedure Call Standard (obsolete) ARM-only.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Int = struct {
        signedness: Signedness,
        bits: u16,
    };

ContainerLayout

ARM Architecture Procedure Call Standard (current standard) ARM-only.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Float = struct {
        bits: u16,
    };

StructField

ARM Architecture Procedure Call Standard Vector Floating-Point ARM-only.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Pointer = struct {
        size: Size,
        is_const: bool,
        is_volatile: bool,
        /// TODO make this u16 instead of comptime_int
        alignment: comptime_int,
        address_space: AddressSpace,
        child: type,
        is_allowzero: bool,

Struct

x86-64-only.


        /// The type of the sentinel is the element type of the pointer, which is
        /// the value of the `child` field in this struct. However there is no way
        /// to refer to that type here, so we use pointer to `anyopaque`.
        sentinel: ?*const anyopaque,

Optional

x86-64-only.


        /// This data structure is used by the Zig language code generation and
        /// therefore must be kept in sync with the compiler implementation.
        pub const Size = enum(u2) {
            One,
            Many,
            Slice,
            C,
        };
    };

ErrorUnion

AMD GPU, NVPTX, or SPIR-V kernel


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Array = struct {
        len: comptime_int,
        child: type,

Error

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


        /// The type of the sentinel is the element type of the array, which is
        /// the value of the `child` field in this struct. However there is no way
        /// to refer to that type here, so we use pointer to `anyopaque`.
        sentinel: ?*const anyopaque,
    };

ErrorSet

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const ContainerLayout = enum(u2) {
        Auto,
        Extern,
        Packed,
    };

EnumField

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const StructField = struct {
        name: []const u8,
        type: type,
        default_value: ?*const anyopaque,
        is_comptime: bool,
        alignment: comptime_int,
    };

Enum

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Struct = struct {
        layout: ContainerLayout,
        /// Only valid if layout is .Packed
        backing_integer: ?type = null,
        fields: []const StructField,
        decls: []const Declaration,
        is_tuple: bool,
    };

UnionField

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Optional = struct {
        child: type,
    };

Union

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const ErrorUnion = struct {
        error_set: type,
        payload: type,
    };

Fn

TODO make this u16 instead of comptime_int


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Error = struct {
        name: []const u8,
    };

Param

The type of the sentinel is the element type of the pointer, which is the value of the child field in this struct. However there is no way to refer to that type here, so we use pointer to anyopaque.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const ErrorSet = ?[]const Error;

Opaque

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const EnumField = struct {
        name: []const u8,
        value: comptime_int,
    };

Frame

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Enum = struct {
        tag_type: type,
        fields: []const EnumField,
        decls: []const Declaration,
        is_exhaustive: bool,
    };

AnyFrame

The type of the sentinel is the element type of the array, which is the value of the child field in this struct. However there is no way to refer to that type here, so we use pointer to anyopaque.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const UnionField = struct {
        name: []const u8,
        type: type,
        alignment: comptime_int,
    };

Vector

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Union = struct {
        layout: ContainerLayout,
        tag_type: ?type,
        fields: []const UnionField,
        decls: []const Declaration,
    };

Declaration

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Fn = struct {
        calling_convention: CallingConvention,
        alignment: comptime_int,
        is_generic: bool,
        is_var_args: bool,
        /// TODO change the language spec to make this not optional.
        return_type: ?type,
        params: []const Param,

FloatMode

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


        /// This data structure is used by the Zig language code generation and
        /// therefore must be kept in sync with the compiler implementation.
        pub const Param = struct {
            is_generic: bool,
            is_noalias: bool,
            type: ?type,
        };
    };

Endian

Only valid if layout is .Packed


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Opaque = struct {
        decls: []const Declaration,
    };

Signedness

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Frame = struct {
        function: *const anyopaque,
    };

OutputMode

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const AnyFrame = struct {
        child: ?type,
    };

LinkMode

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Vector = struct {
        len: comptime_int,
        child: type,
    };

WasiExecModel

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// This data structure is used by the Zig language code generation and
    /// therefore must be kept in sync with the compiler implementation.
    pub const Declaration = struct {
        name: []const u8,
    };
};

CallModifier

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const FloatMode = enum {
    Strict,
    Optimized,
};

VaListAarch64

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Endian = enum {
    Big,
    Little,
};

VaListHexagon

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Signedness = enum {
    signed,
    unsigned,
};

VaListPowerPc

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const OutputMode = enum {
    Exe,
    Lib,
    Obj,
};

VaListS390x

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const LinkMode = enum {
    Static,
    Dynamic,
};

VaListX86_64

TODO change the language spec to make this not optional.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const WasiExecModel = enum {
    command,
    reactor,
};

VaList

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const CallModifier = enum {
    /// Equivalent to function call syntax.
    auto,

PrefetchOptions

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Equivalent to async keyword used with function call syntax.
    async_kw,

Rw

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Prevents tail call optimization. This guarantees that the return
    /// address will point to the callsite, as opposed to the callsite's
    /// callsite. If the call is otherwise required to be tail-called
    /// or inlined, a compile error is emitted instead.
    never_tail,

Cache

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Guarantees that the call will not be inlined. If the call is
    /// otherwise required to be inlined, a compile error is emitted instead.
    never_inline,

ExportOptions

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Asserts that the function call will not suspend. This allows a
    /// non-async function to call an async function.
    no_async,

ExternOptions

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Guarantees that the call will be generated with tail call optimization.
    /// If this is not possible, a compile error is emitted instead.
    always_tail,

CompilerBackend

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Guarantees that the call will be inlined at the callsite.
    /// If this is not possible, a compile error is emitted instead.
    always_inline,

TestFn

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    /// Evaluates the call at compile-time. If the call cannot be completed at
    /// compile-time, a compile error is emitted instead.
    compile_time,
};

PanicFn

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaListAarch64 = extern struct {
    __stack: *anyopaque,
    __gr_top: *anyopaque,
    __vr_top: *anyopaque,
    __gr_offs: c_int,
    __vr_offs: c_int,
};

panic:

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaListHexagon = extern struct {
    __gpr: c_long,
    __fpr: c_long,
    __overflow_arg_area: *anyopaque,
    __reg_save_area: *anyopaque,
};

default_panic()

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaListPowerPc = extern struct {
    gpr: u8,
    fpr: u8,
    reserved: c_ushort,
    overflow_arg_area: *anyopaque,
    reg_save_area: *anyopaque,
};

create_exit_data()

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaListS390x = extern struct {
    __current_saved_reg_area_pointer: *anyopaque,
    __saved_reg_area_end_pointer: *anyopaque,
    __overflow_area_pointer: *anyopaque,
};

checkNonScalarSentinel()

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaListX86_64 = extern struct {
    gp_offset: c_uint,
    fp_offset: c_uint,
    overflow_arg_area: *anyopaque,
    reg_save_area: *anyopaque,
};

panicSentinelMismatch()

Equivalent to function call syntax.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const VaList = switch (builtin.cpu.arch) {
    .aarch64, .aarch64_be => switch (builtin.os.tag) {
        .windows => *u8,
        .ios, .macos, .tvos, .watchos => *u8,
        else => @compileError("disabled due to miscompilations"), // VaListAarch64,
    },
    .arm => switch (builtin.os.tag) {
        .ios, .macos, .tvos, .watchos => *u8,
        else => *anyopaque,
    },
    .amdgcn => *u8,
    .avr => *anyopaque,
    .bpfel, .bpfeb => *anyopaque,
    .hexagon => if (builtin.target.isMusl()) VaListHexagon else *u8,
    .mips, .mipsel, .mips64, .mips64el => *anyopaque,
    .riscv32, .riscv64 => *anyopaque,
    .powerpc, .powerpcle => switch (builtin.os.tag) {
        .ios, .macos, .tvos, .watchos, .aix => *u8,
        else => VaListPowerPc,
    },
    .powerpc64, .powerpc64le => *u8,
    .sparc, .sparcel, .sparc64 => *anyopaque,
    .spirv32, .spirv64 => *anyopaque,
    .s390x => VaListS390x,
    .wasm32, .wasm64 => *anyopaque,
    .x86 => *u8,
    .x86_64 => switch (builtin.os.tag) {
        .windows => @compileError("disabled due to miscompilations"), // *u8,
        else => VaListX86_64,
    },
    else => @compileError("VaList not supported for this target yet"),
};

panicUnwrapError()

Equivalent to async keyword used with function call syntax.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const PrefetchOptions = struct {
    /// Whether the prefetch should prepare for a read or a write.
    rw: Rw = .read,
    /// The data's locality in an inclusive range from 0 to 3.
    ///
    /// 0 means no temporal locality. That is, the data can be immediately
    /// dropped from the cache after it is accessed.
    ///
    /// 3 means high temporal locality. That is, the data should be kept in
    /// the cache as it is likely to be accessed again soon.
    locality: u2 = 3,
    /// The cache that the prefetch should be performed on.
    cache: Cache = .data,

panicOutOfBounds()

Prevents tail call optimization. This guarantees that the return address will point to the callsite, as opposed to the callsite's callsite. If the call is otherwise required to be tail-called or inlined, a compile error is emitted instead.


    pub const Rw = enum(u1) {
        read,
        write,
    };

panicStartGreaterThanEnd()

Guarantees that the call will not be inlined. If the call is otherwise required to be inlined, a compile error is emitted instead.


    pub const Cache = enum(u1) {
        instruction,
        data,
    };
};

panicInactiveUnionField()

Asserts that the function call will not suspend. This allows a non-async function to call an async function.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const ExportOptions = struct {
    name: []const u8,
    linkage: GlobalLinkage = .Strong,
    section: ?[]const u8 = null,
    visibility: SymbolVisibility = .default,
};

panic_messages

Guarantees that the call will be generated with tail call optimization. If this is not possible, a compile error is emitted instead.


/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const ExternOptions = struct {
    name: []const u8,
    library_name: ?[]const u8 = null,
    linkage: GlobalLinkage = .Strong,
    is_thread_local: bool = false,
};

unreach

Guarantees that the call will be inlined at the callsite. If this is not possible, a compile error is emitted instead.


/// This enum is set by the compiler and communicates which compiler backend is
/// used to produce machine code.
/// Think carefully before deciding to observe this value. Nearly all code should
/// be agnostic to the backend that implements the language. The use case
/// to use this value is to **work around problems with compiler implementations.**
///
/// Avoid failing the compilation if the compiler backend does not match a
/// whitelist of backends; rather one should detect that a known problem would
/// occur in a blacklist of backends.
///
/// The enum is nonexhaustive so that alternate Zig language implementations may
/// choose a number as their tag (please use a random number generator rather
/// than a "cute" number) and codebases can interact with these values even if
/// this upstream enum does not have a name for the number. Of course, upstream
/// is happy to accept pull requests to add Zig implementations to this enum.
///
/// This data structure is part of the Zig language specification.
pub const CompilerBackend = enum(u64) {
    /// It is allowed for a compiler implementation to not reveal its identity,
    /// in which case this value is appropriate. Be cool and make sure your
    /// code supports `other` Zig compilers!
    other = 0,
    /// The original Zig compiler created in 2015 by Andrew Kelley. Implemented
    /// in C++. Used LLVM. Deleted from the ZSF ziglang/zig codebase on
    /// December 6th, 2022.
    stage1 = 1,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// LLVM backend.
    stage2_llvm = 2,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// backend that generates C source code.
    /// Note that one can observe whether the compilation will output C code
    /// directly with `object_format` value rather than the `compiler_backend` value.
    stage2_c = 3,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// WebAssembly backend.
    stage2_wasm = 4,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// arm backend.
    stage2_arm = 5,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// x86_64 backend.
    stage2_x86_64 = 6,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// aarch64 backend.
    stage2_aarch64 = 7,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// x86 backend.
    stage2_x86 = 8,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// riscv64 backend.
    stage2_riscv64 = 9,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// sparc64 backend.
    stage2_sparc64 = 10,
    /// The reference implementation self-hosted compiler of Zig, using the
    /// spirv backend.
    stage2_spirv64 = 11,

unwrap_null

Evaluates the call at compile-time. If the call cannot be completed at compile-time, a compile error is emitted instead.


    _,
};

cast_to_null

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This function type is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const TestFn = struct {
    name: []const u8,
    func: *const fn () anyerror!void,
    async_frame_size: ?usize,
};

incorrect_alignment

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This function type is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const PanicFn = fn ([]const u8, ?*StackTrace, ?usize) noreturn;

invalid_error_code

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This function is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const panic: PanicFn = if (@hasDecl(root, "panic"))
    root.panic
else if (@hasDecl(root, "os") and @hasDecl(root.os, "panic"))
    root.os.panic
else
    default_panic;

cast_truncated_data

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


/// This function is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace, ret_addr: ?usize) noreturn {
    @setCold(true);

negative_to_unsigned

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


    // For backends that cannot handle the language features depended on by the
    // default panic handler, we have a simpler panic handler:
    if (builtin.zig_backend == .stage2_wasm or
        builtin.zig_backend == .stage2_arm or
        builtin.zig_backend == .stage2_aarch64 or
        builtin.zig_backend == .stage2_x86_64 or
        builtin.zig_backend == .stage2_x86 or
        builtin.zig_backend == .stage2_riscv64 or
        builtin.zig_backend == .stage2_sparc64 or
        builtin.zig_backend == .stage2_spirv64)
    {
        while (true) {
            @breakpoint();
        }
    }
    switch (builtin.os.tag) {
        .freestanding => {
            while (true) {
                @breakpoint();
            }
        },
        .wasi => {
            std.debug.print("{s}", .{msg});
            std.os.abort();
        },
        .uefi => {
            const uefi = std.os.uefi;

integer_overflow

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


            const ExitData = struct {
                pub fn create_exit_data(exit_msg: []const u8, exit_size: *usize) ![*:0]u16 {
                    // Need boot services for pool allocation
                    if (uefi.system_table.boot_services == null) {
                        return error.BootServicesUnavailable;
                    }

shl_overflow

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


                    // ExitData buffer must be allocated using boot_services.allocatePool
                    var utf16: []u16 = try uefi.raw_pool_allocator.alloc(u16, 256);
                    errdefer uefi.raw_pool_allocator.free(utf16);

shr_overflow

Whether the prefetch should prepare for a read or a write.


                    if (exit_msg.len > 255) {
                        return error.MessageTooLong;
                    }

divide_by_zero

The data's locality in an inclusive range from 0 to 3. 0 means no temporal locality. That is, the data can be immediately dropped from the cache after it is accessed. 3 means high temporal locality. That is, the data should be kept in the cache as it is likely to be accessed again soon.


                    var fmt: [256]u8 = undefined;
                    var slice = try std.fmt.bufPrint(&fmt, "\r\nerr: {s}\r\n", .{exit_msg});

exact_division_remainder

The cache that the prefetch should be performed on.


                    var len = try std.unicode.utf8ToUtf16Le(utf16, slice);

inactive_union_field

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


                    utf16[len] = 0;

integer_part_out_of_bounds

This data structure is used by the Zig language code generation and therefore must be kept in sync with the compiler implementation.


                    exit_size.* = 256;

corrupt_switch

This enum is set by the compiler and communicates which compiler backend is used to produce machine code. Think carefully before deciding to observe this value. Nearly all code should be agnostic to the backend that implements the language. The use case to use this value is to **work around problems with compiler implementations.** Avoid failing the compilation if the compiler backend does not match a whitelist of backends; rather one should detect that a known problem would occur in a blacklist of backends. The enum is nonexhaustive so that alternate Zig language implementations may choose a number as their tag (please use a random number generator rather than a "cute" number) and codebases can interact with these values even if this upstream enum does not have a name for the number. Of course, upstream is happy to accept pull requests to add Zig implementations to this enum. This data structure is part of the Zig language specification.


                    return @as([*:0]u16, @ptrCast(utf16.ptr));
                }
            };

shift_rhs_too_big

It is allowed for a compiler implementation to not reveal its identity, in which case this value is appropriate. Be cool and make sure your code supports other Zig compilers!


            var exit_size: usize = 0;
            var exit_data = ExitData.create_exit_data(msg, &exit_size) catch null;

invalid_enum_value

The original Zig compiler created in 2015 by Andrew Kelley. Implemented in C++. Used LLVM. Deleted from the ZSF ziglang/zig codebase on December 6th, 2022.


            if (exit_data) |data| {
                if (uefi.system_table.std_err) |out| {
                    _ = out.setAttribute(uefi.protocols.SimpleTextOutputProtocol.red);
                    _ = out.outputString(data);
                    _ = out.setAttribute(uefi.protocols.SimpleTextOutputProtocol.white);
                }
            }

sentinel_mismatch

The reference implementation self-hosted compiler of Zig, using the LLVM backend.


            if (uefi.system_table.boot_services) |bs| {
                _ = bs.exit(uefi.handle, .Aborted, exit_size, exit_data);
            }

unwrap_error

The reference implementation self-hosted compiler of Zig, using the backend that generates C source code. Note that one can observe whether the compilation will output C code directly with object_format value rather than the compiler_backend value.


            // Didn't have boot_services, just fallback to whatever.
            std.os.abort();
        },
        .cuda, .amdhsa => std.os.abort(),
        else => {
            const first_trace_addr = ret_addr orelse @returnAddress();
            std.debug.panicImpl(error_return_trace, first_trace_addr, msg);
        },
    }
}

index_out_of_bounds

The reference implementation self-hosted compiler of Zig, using the WebAssembly backend.


pub fn checkNonScalarSentinel(expected: anytype, actual: @TypeOf(expected)) void {
    if (!std.meta.eql(expected, actual)) {
        panicSentinelMismatch(expected, actual);
    }
}

start_index_greater_than_end

The reference implementation self-hosted compiler of Zig, using the arm backend.


pub fn panicSentinelMismatch(expected: anytype, actual: @TypeOf(expected)) noreturn {
    @setCold(true);
    std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ expected, actual });
}

for_len_mismatch

The reference implementation self-hosted compiler of Zig, using the x86_64 backend.


pub fn panicUnwrapError(st: ?*StackTrace, err: anyerror) noreturn {
    @setCold(true);
    std.debug.panicExtra(st, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)});
}

memcpy_len_mismatch

The reference implementation self-hosted compiler of Zig, using the aarch64 backend.


pub fn panicOutOfBounds(index: usize, len: usize) noreturn {
    @setCold(true);
    std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len });
}

memcpy_alias

The reference implementation self-hosted compiler of Zig, using the x86 backend.


pub fn panicStartGreaterThanEnd(start: usize, end: usize) noreturn {
    @setCold(true);
    std.debug.panicExtra(null, @returnAddress(), "start index {d} is larger than end index {d}", .{ start, end });
}

noreturn_returned

The reference implementation self-hosted compiler of Zig, using the riscv64 backend.


pub fn panicInactiveUnionField(active: anytype, wanted: @TypeOf(active)) noreturn {
    @setCold(true);
    std.debug.panicExtra(null, @returnAddress(), "access of union field '{s}' while field '{s}' is active", .{ @tagName(wanted), @tagName(active) });
}

addErrRetTraceAddr()

The reference implementation self-hosted compiler of Zig, using the sparc64 backend.


pub const panic_messages = struct {
    pub const unreach = "reached unreachable code";
    pub const unwrap_null = "attempt to use null value";
    pub const cast_to_null = "cast causes pointer to be null";
    pub const incorrect_alignment = "incorrect alignment";
    pub const invalid_error_code = "invalid error code";
    pub const cast_truncated_data = "integer cast truncated bits";
    pub const negative_to_unsigned = "attempt to cast negative value to unsigned integer";
    pub const integer_overflow = "integer overflow";
    pub const shl_overflow = "left shift overflowed bits";
    pub const shr_overflow = "right shift overflowed bits";
    pub const divide_by_zero = "division by zero";
    pub const exact_division_remainder = "exact division produced remainder";
    pub const inactive_union_field = "access of inactive union field";
    pub const integer_part_out_of_bounds = "integer part of floating point value out of bounds";
    pub const corrupt_switch = "switch on corrupt value";
    pub const shift_rhs_too_big = "shift amount is greater than the type size";
    pub const invalid_enum_value = "invalid enum value";
    pub const sentinel_mismatch = "sentinel mismatch";
    pub const unwrap_error = "attempt to unwrap error";
    pub const index_out_of_bounds = "index out of bounds";
    pub const start_index_greater_than_end = "start index is larger than end index";
    pub const for_len_mismatch = "for loop over objects with non-equal lengths";
    pub const memcpy_len_mismatch = "@memcpy arguments have non-equal lengths";
    pub const memcpy_alias = "@memcpy arguments alias";
    pub const noreturn_returned = "'noreturn' function returned";
};

pub noinline fn returnError(st: *StackTrace) void {
    @setCold(true);
    @setRuntimeSafety(false);
    addErrRetTraceAddr(st, @returnAddress());
}

pub inline fn addErrRetTraceAddr(st: *StackTrace, addr: usize) void {
    if (st.index < st.instruction_addresses.len)
        st.instruction_addresses[st.index] = addr;

    st.index += 1;
}

const std = @import("std.zig");
const root = @import("root");