Zig 语言提供的类型和值。
builtinstdrootassemblyStackTraceGlobalLinkageSymbolVisibilityAtomicOrderReduceOpAtomicRmwOpCodeModelOptimizeModeOptimizeCallingConventionAddressSpaceSourceLocationTypeIdTypeFloatModeEndianSignednessOutputModeLinkModeUnwindTablesWasiExecModelCallModifierVaListAarch64VaListAlphaVaListArmVaListHexagonVaListPowerPcVaListS390xVaListShVaListX86_64VaListXtensaVaListPrefetchOptionsExportOptionsExternOptionsBranchHintCompilerBackendTestFnpanicreturnError
std.lang.AddressSpace
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
generic
gs
fs
ss
far同时存储段及偏移量,从而允许寻址整个地址空间。
global
constant
param
shared
local
private
input
output
uniform
push_constant
storage_buffer
physical_storage_buffer
flash
flash1
flash2
flash3
flash4
flash5
cog此地址空间仅寻址 cog 本地 RAM。
hub此地址空间仅可寻址共享 hub RAM。
lut此地址空间仅寻址 "lookup" RAM。
externref
funcref
std.lang.assembly
别名
别名目标 std.lang.assembly
std.lang.AtomicOrder
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
unordered
monotonic
acquire
release
acq_rel
seq_cst
std.lang.AtomicRmwOp
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
Xchg交换:原样存储操作数。 支持枚举、整数和浮点数。
Add将操作数加到已有值。 支持整数和浮点数。 整数采用二进制补码回绕。
Sub从现有值减去操作数。 支持整数和浮点数。 对于整数,应用二进制补码回绕。
And对现有值和操作数执行按位与。 支持整数。
Nand对现有值与操作数执行按位与非。 支持整数。
Or对现有值和操作数执行按位或。 支持整数。
Xor将现有值与操作数按位异或。 支持整数。
Max若操作数大于现有值,则存储操作数。 支持整数和浮点数。
Min当操作数小于现有值时存储该操作数。 支持整数和浮点数。
std.lang.BranchHint
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
none等同于未提供提示。
likely此控制流分支比同级分支更可能到达。 优化器应针对到达此分支进行优化。
unlikely此控制流分支比同级分支更不可能到达。 优化器应针对不进入该分支的情况进行优化。
cold此控制流分支极不可能到达。 优化器可将它放在不同内存页中,以优化其他分支。
unpredictable难以预测控制流是否会到达此分支。 优化器应避免产生误预测代价高昂的分支行为。
std.lang.builtin
常量
std.lang.CallingConvention
容器
函数的调用约定规定参数与返回值的传递方式, 以及调用方和被调用方必须遵守的其他要求,例如寄存器保存 和栈对齐。
此数据结构用于 Zig 语言的代码生成, 因此必须与编译器实现保持同步。
字段
auto既未指定 export 也未指定 inline 时的 Zig 默认调用约定。
此调用约定不保证栈对齐、寄存器等条件。
仅可在此 Zig 编译单元内使用。
async可用 async 语法调用的函数的调用约定。对运行时已知函数
进行 async 调用时,目标函数必须具有此调用约定。
采用其他调用约定、但编译期已知的函数可强制转换为此约定。
naked采用此调用约定的函数没有序言或尾声,因而无法从 常规 Zig 代码调用。与汇编集成时可能有用。
@"inline"此调用约定完全等同于在函数定义上使用 inline
关键字。Zig 编译器会在调用位置对其进行语义内联。
指向内联函数的指针仅在编译期可用。
x86_64_sysv: CommonOptions
x86_64_x32: CommonOptions
x86_64_win: CommonOptions
x86_64_regcall_v3_sysv: CommonOptions
x86_64_regcall_v4_win: CommonOptions
x86_64_vectorcall: CommonOptions
x86_64_interrupt: CommonOptions
x86_64_preserve_none: CommonOptions
x86_sysv: X86RegparmOptions
x86_win: X86RegparmOptions
x86_mingw: X86RegparmOptions
x86_stdcall: X86RegparmOptions
x86_fastcall: CommonOptions
x86_thiscall: CommonOptions
x86_thiscall_mingw: CommonOptions
x86_regcall_v3: CommonOptions
x86_regcall_v4_win: CommonOptions
x86_vectorcall: CommonOptions
x86_interrupt: CommonOptions
x86_16_cdecl: CommonOptions
x86_16_stdcall: CommonOptions
x86_16_regparmcall: CommonOptions
x86_16_interrupt: CommonOptions
aarch64_aapcs: CommonOptions
aarch64_aapcs_darwin: CommonOptions
aarch64_aapcs_win: CommonOptions
aarch64_vfabi: CommonOptions
aarch64_vfabi_sve: CommonOptions
aarch64_preserve_none: CommonOptions
alpha_osf: CommonOptions标准 alpha 调用约定。
arm_aapcs: CommonOptionsARM 架构过程调用标准
arm_aapcs_vfp: CommonOptionsARM 架构过程调用标准的向量浮点变体。
arm_interrupt: ArmInterruptOptions
mips64_n64: CommonOptions
mips64_n32: CommonOptions
mips64_interrupt: MipsInterruptOptions
mips_o32: CommonOptions
mips_interrupt: MipsInterruptOptions
riscv64_lp64: CommonOptions
riscv64_lp64_v: CommonOptions
riscv64_interrupt: RiscvInterruptOptions
riscv32_ilp32: CommonOptions
riscv32_ilp32_v: CommonOptions
riscv32_interrupt: RiscvInterruptOptions
sparc64_sysv: CommonOptions
sparc_sysv: CommonOptions
powerpc64_elf: CommonOptions
powerpc64_elf_altivec: CommonOptions
powerpc64_elf_v2: CommonOptions
powerpc_sysv: CommonOptions
powerpc_sysv_altivec: CommonOptions
powerpc_aix: CommonOptions
powerpc_aix_altivec: CommonOptions
wasm_mvp: CommonOptionsWebAssembly 工具约定中规定的标准 wasm32 和 wasm64 调用约定。
arc_sysv: CommonOptions标准 arc/arceb 调用约定。
arc_interrupt: ArcInterruptOptions
avr_gnu
avr_builtin
avr_signal
avr_interrupt
bpf_std: CommonOptions标准 bpfel/bpfeb 调用约定。
csky_sysv: CommonOptions
csky_interrupt: CommonOptions
hexagon_sysv: CommonOptions
hexagon_sysv_hvx: CommonOptions
hppa_elf: CommonOptions标准 hppa 调用约定。
hppa64_elf: CommonOptions标准 hppa64 调用约定。
kvx_lp64: CommonOptions
kvx_ilp32: CommonOptions
lanai_sysv: CommonOptions标准 lanai 调用约定。
loongarch64_lp64: CommonOptions标准 loongarch64 调用约定。
loongarch32_ilp32: CommonOptions标准 loongarch32 调用约定。
m68k_sysv: CommonOptions
m68k_gnu: CommonOptions
m68k_rtd: CommonOptions
m68k_interrupt: CommonOptions
m88k_sysv: CommonOptions
microblaze_std: CommonOptions标准 microblaze/microblazeel 调用约定。
microblaze_interrupt: MicroblazeInterruptOptions
msp430_eabi: CommonOptions标准 msp430 调用约定。
msp430_interrupt: CommonOptions
or1k_sysv: CommonOptions标准 or1k 调用约定。
propeller_sysv: CommonOptions标准 propeller 调用约定。
s390x_sysv: CommonOptions
s390x_sysv_vx: CommonOptions
sh_gnu: CommonOptions
sh_renesas: CommonOptions
sh_interrupt: ShInterruptOptions
ve_sysv: CommonOptions标准 ve 调用约定。
xcore_xs1: CommonOptions
xcore_xs2: CommonOptions
xtensa_call0: CommonOptions
xtensa_windowed: CommonOptions
amdgcn_device: CommonOptions
amdgcn_kernel
amdgcn_cs: CommonOptions
nvptx_device
nvptx_kernel
spirv_device
spirv_vertex
spirv_kernel: SpirvKernelOptions
spirv_fragment: SpirvFragmentOptions
spirv_task: SpirvKernelOptions
spirv_mesh: SpirvMeshOptions
ez80_cet
ez80_tiflags
spork8
std.lang.CallingConvention.archs
函数
返回此 CallingConvention 适用的 std.Target.Cpu.Arch 数组。
断言 cc 不是 .auto、.@"async"、.naked 或 .@"inline"。
pub fn archs(cc: CallingConvention) []const std.Target.Cpu.Arch
参数
std.lang.CallingConvention.ArcInterruptOptions
容器
arc_interrupt 调用约定的选项。
字段
std.lang.CallingConvention.ArcInterruptOptions.InterruptType
容器
字段
ilink1
ilink2
ilink
firq
std.lang.CallingConvention.ArmInterruptOptions
容器
arm_interrupt 调用约定的选项。
字段
incoming_stack_alignment: ?u64 = null调用函数时栈对齐的边界。
null 表示此调用约定的默认值。
type: InterruptType = .generic正在接收的中断类型。
std.lang.CallingConvention.ArmInterruptOptions.InterruptType
容器
字段
generic
irq
fiq
swi
abort
undef
std.lang.CallingConvention.c
常量
此目标默认 C 调用约定的别名。
标记为 extern 或 export 的函数默认使用此调用约定。
std.lang.CallingConvention.CommonOptions
容器
大多数调用约定共享的选项。
字段
incoming_stack_alignment: ?u64 = null调用函数时栈对齐的边界。
null 表示此调用约定的默认值。
std.lang.CallingConvention.eql
函数
pub fn eql(a: CallingConvention, b: CallingConvention) bool
参数
std.lang.CallingConvention.kernel
常量
CallingConvention
std.lang.CallingConvention.MicroblazeInterruptOptions
容器
microblaze_interrupt 调用约定的选项。
字段
std.lang.CallingConvention.MicroblazeInterruptOptions.InterruptType
容器
字段
user用户异常;使用 rtsd 返回。
regular常规中断;使用 rtid 返回。
fast快速中断;使用 rtid 返回。
breakpoint软件断点;通过 rtbd 返回。
std.lang.CallingConvention.MipsInterruptOptions
容器
mips_interrupt 和 mips64_interrupt 调用约定的选项。
字段
std.lang.CallingConvention.MipsInterruptOptions.InterruptMode
容器
字段
eic
sw0
sw1
hw0
hw1
hw2
hw3
hw4
hw5
std.lang.CallingConvention.RiscvInterruptOptions
容器
riscv32_interrupt 和 riscv64_interrupt 调用约定的选项。
字段
std.lang.CallingConvention.RiscvInterruptOptions.PrivilegeMode
容器
字段
supervisor
machine
std.lang.CallingConvention.ShInterruptOptions
容器
sh_interrupt 调用约定的选项。
字段
std.lang.CallingConvention.ShInterruptOptions.SaveBehavior
容器
字段
fpscr仅保存 fpscr(若适用)。
high仅保存高编号寄存器,即不保存 r0 到 r7。
full按通常方式保存所有寄存器。
bank使用 CPU 的快速寄存器组保存所有寄存器。
std.lang.CallingConvention.SpirvFragmentOptions
容器
字段
pixel_centered_integer: bool = false
depth_assumption: DepthAssumption = .none
std.lang.CallingConvention.SpirvFragmentOptions.DepthAssumption
容器
字段
none = 0
greater = 1
less = 2
unchanged = 3
std.lang.CallingConvention.SpirvKernelOptions
容器
字段
x: u32
y: u32
z: u32
std.lang.CallingConvention.SpirvMeshOptions
容器
字段
stage_output: StageOutput = .output_triangles
max_primitives: u32 = 1
max_vertices: u32 = 3
x: u32
y: u32
z: u32
std.lang.CallingConvention.SpirvMeshOptions.StageOutput
容器
字段
output_points = 0
output_lines = 1
output_triangles = 2
std.lang.CallingConvention.Tag
常量
std.lang.CallingConvention.winapi
常量
CallingConvention
std.lang.CallingConvention.withStackAlign
函数
pub fn withStackAlign(cc: CallingConvention, incoming_stack_alignment: u64) CallingConvention
参数
cc: CallingConvention
incoming_stack_alignment: u64
std.lang.CallingConvention.X86RegparmOptions
容器
支持 regparm 属性的 x86 调用约定选项, 该属性可通过寄存器传递部分参数。
字段
incoming_stack_alignment: ?u64 = null调用函数时栈对齐的边界。
null 表示此调用约定的默认值。
register_params: u2 = 0先通过寄存器传递的参数数量;剩余参数
按调用约定传递。
等价于 Clang 和 GCC 中的 __attribute__((regparm(x)))。
std.lang.CallModifier
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
auto等价于函数调用语法。
never_tail阻止尾调用优化。保证返回地址指向 调用位置,而非该调用位置的调用位置。 若该调用另有要求必须进行尾调用 或内联,则产生编译错误。
never_inline保证调用不被内联。若该调用另有要求必须内联, 则改为产生编译错误。
no_suspend断言该函数调用不会挂起。这允许 非异步函数调用异步函数。
always_tail保证生成的调用采用尾调用优化。 如果无法实现,则产生编译错误。
always_inline保证在调用位置内联此调用。 若无法做到,则产生编译错误。
compile_time在编译期对调用求值。若调用无法在编译期完成,则产生编译错误。
std.lang.CodeModel
容器
代码模型约束符号位置以及代码和数据的大小。 选择代码模型需针对各应用的要求,在速度与限制之间权衡。 更详细的说明可参见例如 System V 应用程序二进制接口(x86_64) 第 3.5.1 节。
Zig 语言代码生成会使用此数据结构, 因此必须与编译器实现保持同步。
字段
default
extreme
kernel
large
medany
medium
medlow
medmid
normal
small
tiny
std.lang.CompilerBackend
容器
此枚举由编译器设置,用于表示生成机器码所使用的编译器实现。
理论上,Zig 代码应当与实现该语言的后端无关。观察此值的唯一理由是规避编译器实现中的问题。
常见陷阱是:编译器后端不在白名单中就使编译失败。更稳健的策略是检查后端是否属于已知会触发特定问题的黑名单。
此枚举为非穷尽枚举,因此其他 Zig 语言实现可以随机选取数值作为标签,避免与其他实现冲突;即使上游枚举未给该数值命名,代码库仍可与这些值交互。当然,上游欢迎提交补丁,将其他 Zig 实现添加到此枚举中。
此数据结构是 Zig 语言规范的一部分。
字段
other = 0允许编译器实现不公开自身标识,
此时应使用此值。请确保你的代码
也支持 other Zig 编译器!
stage1 = 1Andrew Kelley 于 2015 年创建的原始 Zig 编译器。 使用 C++ 实现,使用 LLVM。2022 年 12 月 6 日, 从 ZSF 的 ziglang/zig 代码库中删除。
stage2_llvm = 2Zig 的参考实现自举编译器, 使用 LLVM 后端。
stage2_c = 3Zig 的自托管编译器参考实现,使用生成 C 源码的 后端。
注意,可通过 object_format 值(而非 compiler_backend 值)
直接判断编译是否会输出 C 代码。
stage2_wasm = 4Zig 的自托管参考实现编译器,使用 WebAssembly 后端。
stage2_arm = 5Zig 的参考实现自举编译器, 使用 arm 后端。
stage2_x86_64 = 6Zig 的自托管参考实现编译器,使用 x86_64 后端。
stage2_aarch64 = 7Zig 的自托管参考实现编译器,使用 aarch64 后端。
stage2_x86 = 8Zig 的自举编译器参考实现,使用 x86 后端。
stage2_riscv64 = 9Zig 的参考实现自举编译器, 使用 riscv64 后端。
stage2_sparc64 = 10Zig 的自举编译器参考实现,使用 sparc64 后端。
stage2_spirv = 11Zig 的参考实现自举编译器, 使用 spirv 后端。
stage2_powerpc = 12Zig 的自托管参考实现编译器,使用 powerpc 后端。
stage2_loongarch = 13Zig 的自托管参考实现编译器,使用 loongarch 后端。
zsf_spork8 = 14Zig 软件基金会的 Zig 自举实现。此后端 最初由 Ben Anderman 于 2026 年贡献。
_
std.lang.Endian
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
big
little
std.lang.Endian.foreign
常量
Endian
std.lang.Endian.native
常量
std.lang.ExportOptions
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
name: []const u8
linkage: GlobalLinkage = .strong
section: ?[]const u8 = null
visibility: SymbolVisibility = .default
std.lang.ExternOptions
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
name: []const u8
library_name: ?[]const u8 = null
linkage: GlobalLinkage = .strong
visibility: SymbolVisibility = .default
is_thread_local: bool = false将此项设为 true,使 @extern 成为运行期值。
is_dll_import: bool = false
relocation: Relocation = .any
decoration: ?Decoration = null
std.lang.ExternOptions.Decoration
容器
字段
std.lang.ExternOptions.Decoration.Descriptor
容器
字段
set: u32
binding: u32
std.lang.ExternOptions.Relocation
容器
字段
any允许任意类型的重定位。
pcrel需要相对于程序计数器的重定位。
使用此值会使 @extern 成为运行时值。
std.lang.FloatMode
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
strict
optimized
std.lang.GlobalLinkage
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
strong
weak
std.lang.LinkMode
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
static
dynamic
std.lang.Optimize
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
debug启用安全检查。按优先级依次优化错误检测、准确的调试信息 以及编译速度。
safe启用安全检查。针对运行性能进行优化。
fast禁用安全检查。针对运行性能进行优化。
small禁用安全检查。优先优化机器码大小,其次优化运行性能。
std.lang.Optimize.Debug
常量
已弃用,将在 0.18.0 之后移除
@This()
std.lang.Optimize.fromString
函数
已弃用,将在 0.18.0 之后移除
pub fn fromString(s: []const u8) ?@This()
参数
s: []const u8
std.lang.Optimize.ReleaseFast
常量
已弃用,将在 0.18.0 之后移除
@This()
std.lang.Optimize.ReleaseSafe
常量
已弃用,将在 0.18.0 之后移除
@This()
std.lang.Optimize.ReleaseSmall
常量
已弃用,将在 0.18.0 之后移除
@This()
std.lang.Optimize.runtimeSafety
函数
根据给定优化模式,返回 是否启用非法行为安全检查。
pub fn runtimeSafety(o: @This()) bool
参数
o: @This()
std.lang.OptimizeMode
别名
已弃用,将在 0.18.0 之后移除
别名目标 std.lang.Optimize
std.lang.OutputMode
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
Exe
Lib
Obj
std.lang.panic
常量
Zig 编译器使用此命名空间生成各种由安全检查触发的 panic。可以在根源码文件中定义公开的 panic 命名空间
覆盖这些行为。
type
std.lang.panic.panic
函数
fn panic(msg: []const u8, ra: ?usize) noreturn
参数
msg: []const u8
ra: ?usize
std.lang.PrefetchOptions
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
std.lang.PrefetchOptions.Cache
容器
字段
instruction
data
std.lang.PrefetchOptions.Rw
容器
字段
read
write
std.lang.ReduceOp
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
And
Or
Xor
Min
Max
Add
Mul
std.lang.returnError
函数
pub noinline fn returnError() void
std.lang.root
常量
std.lang.Signedness
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
signed
unsigned
std.lang.SourceLocation
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
module: [:0]const u8编译时选定的名称。并非文件路径。
file: [:0]const u8相对于所属模块的根目录。
fn_name: [:0]const u8
line: u32
column: u32
std.lang.StackTrace
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
index: usize
instruction_addresses: []usize
std.lang.std
别名
别名目标 std
std.lang.SymbolVisibility
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
default
hidden
protected
std.lang.TestFn
容器
此函数类型由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
name: []const u8
func: *const fn () anyerror!void
std.lang.Type
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
type
void
bool
noreturn
int: Int
float: Float
pointer: Pointer
array: Array
@"struct": Struct
comptime_float
comptime_int
undefined
null
optional: Optional
error_union: ErrorUnion
error_set: ErrorSet
@"enum": Enum
@"union": Union
@"fn": Fn
@"opaque": Opaque
frame: Frame
@"anyframe": AnyFrame
vector: Vector
enum_literal
spirv: Spirv
std.lang.Type.AnyFrame
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
child: ?type
std.lang.Type.Array
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
len: comptime_int
child: type
sentinel_ptr: ?*const anyopaque哨兵的类型是数组元素类型,也就是此结构体中的
child 字段的值。但此处无法引用该类型,因此使用 *const anyopaque。
另见:sentinel。
std.lang.Type.Array.sentinel
函数
从 sentinel_ptr 加载数组类型的哨兵值。
若数组类型没有哨兵,则返回 null。
pub inline fn sentinel(comptime arr: Array) ?arr.child
参数
arr: Array
std.lang.Type.ContainerLayout
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
auto
@"extern"
@"packed"
std.lang.Type.Enum
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
tag_type: type
mode: Mode
field_names: []const [:0]const u8
field_values: []const comptime_int保证与 field_names 长度相同。
decl_names: []const [:0]const u8
std.lang.Type.Enum.Mode
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
exhaustive
nonexhaustive
std.lang.Type.ErrorSet
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
error_names: ?[]const [:0]const u8
std.lang.Type.ErrorUnion
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
error_set: type
payload: type
std.lang.Type.Float
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
bits: u16
std.lang.Type.Fn
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
attrs: Attributes
is_generic: bool
return_type: ?typenull 表示返回类型是泛型,即依赖函数参数。
param_types: []const ?typenull 元素表示 anytype 参数,或泛型类型参数,即类型依赖先前的函数参数。
param_attrs: []const ParamAttributes保证长度与 param_types 相同。
std.lang.Type.Fn.Attributes
容器
字段
@"callconv": CallingConvention = .auto
varargs: bool = false
std.lang.Type.Fn.ParamAttributes
容器
字段
@"noalias": bool = false
std.lang.Type.Frame
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
function: *const anyopaque
std.lang.Type.Int
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
signedness: Signedness
bits: u16
std.lang.Type.Opaque
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
decl_names: []const [:0]const u8
std.lang.Type.Optional
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
child: type
std.lang.Type.Pointer
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
size: Size
attrs: Attributes
child: type
sentinel_ptr: ?*const anyopaque哨兵类型是指针的元素类型,即此结构体中
child 字段的值。但这里无法
引用该类型,因此使用 *const anyopaque。
另见:sentinel
std.lang.Type.Pointer.Attributes
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
@"const": bool = false
@"volatile": bool = false
@"allowzero": bool = false
@"addrspace": ?AddressSpace = null
@"align": ?usize = null
std.lang.Type.Pointer.sentinel
函数
从 sentinel_ptr 加载指针类型的哨兵值。
如果该指针类型没有哨兵,则返回 null。
pub inline fn sentinel(comptime ptr: Pointer) ?ptr.child
参数
ptr: Pointer
std.lang.Type.Pointer.Size
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
one
many
slice
c
std.lang.Type.Spirv
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
std.lang.Type.Spirv.Image
容器
字段
usage: Usage
format: Format
dim: Dimensionality
depth: Depth
access: Access
arrayed: bool
multisampled: bool
std.lang.Type.Spirv.Image.Access
容器
字段
unknown
read_only
write_only
read_write
std.lang.Type.Spirv.Image.Depth
容器
字段
unknown
depth
not_depth
std.lang.Type.Spirv.Image.Dimensionality
容器
字段
@"1d"
@"2d"
@"3d"
cube
std.lang.Type.Spirv.Image.Format
容器
字段
unknown
rgba32f
rgba32i
rgba32u
rgba16f
rgba16i
rgba16u
rgba8unorm
rgba8snorm
rgba8i
rgba8u
r32f
r32i
r32u
std.lang.Type.Spirv.Image.Usage
容器
字段
unknown: type
sampled: type
storage: type
std.lang.Type.Struct
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
is_tuple: bool
layout: ContainerLayout
backing_integer: ?type始终为 null(当 layout != .@"packed" 时)。
field_names: []const [:0]const u8
field_types: []const type保证与 field_names 长度相同。
field_attrs: []const FieldAttributes保证与 field_names 长度相同。
decl_names: []const [:0]const u8
std.lang.Type.Struct.FieldAttributes
容器
字段
@"comptime": bool = false
@"align": ?usize = nullnull 表示未显式指定字段对齐要求。字段仍至少
按照其类型的 @alignOf 对齐。
default_value_ptr: ?*const anyopaque = null默认值的类型是此结构体字段的类型。但此处 不知道该类型,因此改用类型擦除的指针,必须将其转换为 指向字段类型的指针。
另请参见:defaultValue。
std.lang.Type.Struct.FieldAttributes.defaultValue
函数
从 default_value_ptr 加载字段的默认值。
FieldType 必须与 Struct.field_types 中对应元素完全匹配。
若字段没有默认值,则返回 null。
pub inline fn defaultValue(comptime attrs: FieldAttributes, comptime FieldType: type) ?FieldType
参数
attrs: FieldAttributes
FieldType: type
std.lang.Type.Union
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
layout: ContainerLayout
tag_type: ?type
backing_integer: ?type始终为 null(当 layout != .@"packed" 时)。
field_names: []const [:0]const u8
field_types: []const type保证与 field_names 长度相同。
field_attrs: []const FieldAttributes保证与 field_names 长度相同。
decl_names: []const [:0]const u8
std.lang.Type.Union.FieldAttributes
容器
字段
@"align": ?usize = nullnull 表示未显式指定字段对齐要求。字段仍至少
按照其类型的 @alignOf 对齐。
std.lang.Type.Vector
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
len: comptime_int
child: type
std.lang.TypeId
类型
std.lang.UnwindTables
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
none
sync
async
std.lang.VaList
类型
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
std.lang.VaListAarch64
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__stack: *anyopaque
__gr_top: *anyopaque
__vr_top: *anyopaque
__gr_offs: c_int
__vr_offs: c_int
std.lang.VaListAlpha
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__base: *anyopaque
__offset: c_int
std.lang.VaListArm
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__ap: *anyopaque
std.lang.VaListHexagon
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__current_saved_reg_area_pointer: *anyopaque
__saved_reg_area_end_pointer: *anyopaque
__overflow_area_pointer: *anyopaque
std.lang.VaListPowerPc
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
gpr: u8
fpr: u8
reserved: c_ushort
overflow_arg_area: *anyopaque
reg_save_area: *anyopaque
std.lang.VaListS390x
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__gpr: c_long
__fpr: c_long
__overflow_arg_area: *anyopaque
__reg_save_area: *anyopaque
std.lang.VaListSh
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__va_next_o: *anyopaque
__va_next_o_limit: *anyopaque
__va_next_fp: *anyopaque
__va_next_fp_limit: *anyopaque
__va_next_stack: *anyopaque
std.lang.VaListX86_64
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
gp_offset: c_uint
fp_offset: c_uint
overflow_arg_area: *anyopaque
reg_save_area: *anyopaque
std.lang.VaListXtensa
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
__va_stk: *c_int
__va_reg: *c_int
__va_ndx: c_int
std.lang.WasiExecModel
容器
此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
字段
command
reactor
源码
std/lang.zig
//! Zig 语言提供的类型和值。
const builtin = @import("builtin");
const std = @import("std.zig");
const root = @import("root");
pub const assembly = @import("lang/assembly.zig");
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const StackTrace = struct {
index: usize,
instruction_addresses: []usize,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const GlobalLinkage = enum(u1) {
strong,
weak,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const SymbolVisibility = enum(u2) {
default,
hidden,
protected,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const AtomicOrder = enum {
unordered,
monotonic,
acquire,
release,
acq_rel,
seq_cst,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ReduceOp = enum {
And,
Or,
Xor,
Min,
Max,
Add,
Mul,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const AtomicRmwOp = enum {
/// 交换:原样存储操作数。
/// 支持枚举、整数和浮点数。
Xchg,
/// 将操作数加到已有值。
/// 支持整数和浮点数。
/// 整数采用二进制补码回绕。
Add,
/// 从现有值减去操作数。
/// 支持整数和浮点数。
/// 对于整数,应用二进制补码回绕。
Sub,
/// 对现有值和操作数执行按位与。
/// 支持整数。
And,
/// 对现有值与操作数执行按位与非。
/// 支持整数。
Nand,
/// 对现有值和操作数执行按位或。
/// 支持整数。
Or,
/// 将现有值与操作数按位异或。
/// 支持整数。
Xor,
/// 若操作数大于现有值,则存储操作数。
/// 支持整数和浮点数。
Max,
/// 当操作数小于现有值时存储该操作数。
/// 支持整数和浮点数。
Min,
};
/// 代码模型约束符号位置以及代码和数据的大小。
/// 选择代码模型需针对各应用的要求,在速度与限制之间权衡。
/// 更详细的说明可参见例如 [System V 应用程序二进制接口(x86_64)](https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf) 第 3.5.1 节。
///
/// Zig 语言代码生成会使用此数据结构,
/// 因此必须与编译器实现保持同步。
pub const CodeModel = enum(u4) {
default,
extreme,
kernel,
large,
medany,
medium,
medlow,
medmid,
normal,
small,
tiny,
};
/// 已弃用,将在 0.18.0 之后移除
pub const OptimizeMode = Optimize;
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Optimize = enum {
/// 启用安全检查。按优先级依次优化错误检测、准确的调试信息
/// 以及编译速度。
debug,
/// 启用安全检查。针对运行性能进行优化。
safe,
/// 禁用安全检查。针对运行性能进行优化。
fast,
/// 禁用安全检查。优先优化机器码大小,其次优化运行性能。
small,
/// 已弃用,将在 0.18.0 之后移除
pub const Debug: @This() = .debug;
/// 已弃用,将在 0.18.0 之后移除
pub const ReleaseSafe: @This() = .safe;
/// 已弃用,将在 0.18.0 之后移除
pub const ReleaseFast: @This() = .fast;
/// 已弃用,将在 0.18.0 之后移除
pub const ReleaseSmall: @This() = .small;
/// 已弃用,将在 0.18.0 之后移除
pub fn fromString(s: []const u8) ?@This() {
return std.StaticStringMap(@This()).initComptime(&.{
.{ "Debug", .debug },
.{ "ReleaseSafe", .safe },
.{ "ReleaseFast", .fast },
.{ "ReleaseSmall", .small },
.{ "debug", .debug },
.{ "safe", .safe },
.{ "fast", .fast },
.{ "small", .small },
}).get(s);
}
/// 根据给定优化模式,返回
/// 是否启用非法行为安全检查。
pub fn runtimeSafety(o: @This()) bool {
return switch (o) {
.debug, .safe => true,
.fast, .small => false,
};
}
};
/// 函数的调用约定规定参数与返回值的传递方式,
/// 以及调用方和被调用方必须遵守的其他要求,例如寄存器保存
/// 和栈对齐。
///
/// 此数据结构用于 Zig 语言的代码生成,
/// 因此必须与编译器实现保持同步。
pub const CallingConvention = union(enum(u8)) {
pub const Tag = @typeInfo(CallingConvention).@"union".tag_type.?;
/// 此目标默认 C 调用约定的别名。
/// 标记为 `extern` 或 `export` 的函数默认使用此调用约定。
pub const c = builtin.target.cCallingConvention().?;
pub const winapi: CallingConvention = switch (builtin.target.cpu.arch) {
.x86_64 => .{ .x86_64_win = .{} },
.x86 => .{ .x86_stdcall = .{} },
.aarch64 => .{ .aarch64_aapcs_win = .{} },
.thumb => .{ .arm_aapcs_vfp = .{} },
else => unreachable,
};
pub const kernel: CallingConvention = switch (builtin.target.cpu.arch) {
.amdgcn => .amdgcn_kernel,
.nvptx, .nvptx64 => .nvptx_kernel,
.spirv32, .spirv64 => .{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } },
else => unreachable,
};
/// 既未指定 `export` 也未指定 `inline` 时的 Zig 默认调用约定。
/// 此调用约定不保证栈对齐、寄存器等条件。
/// 仅可在此 Zig 编译单元内使用。
auto,
/// 可用 `async` 语法调用的函数的调用约定。对运行时已知函数
/// 进行 `async` 调用时,目标函数必须具有此调用约定。
/// 采用其他调用约定、但编译期已知的函数可强制转换为此约定。
async,
/// 采用此调用约定的函数没有序言或尾声,因而无法从
/// 常规 Zig 代码调用。与汇编集成时可能有用。
naked,
/// 此调用约定完全等同于在函数定义上使用 `inline`
/// 关键字。Zig 编译器会在调用位置对其进行语义内联。
/// 指向内联函数的指针仅在编译期可用。
@"inline",
// `x86_64` 架构的调用约定。
x86_64_sysv: CommonOptions,
x86_64_x32: CommonOptions,
x86_64_win: CommonOptions,
x86_64_regcall_v3_sysv: CommonOptions,
x86_64_regcall_v4_win: CommonOptions,
x86_64_vectorcall: CommonOptions,
x86_64_interrupt: CommonOptions,
x86_64_preserve_none: CommonOptions,
// `x86` 架构的调用约定。
x86_sysv: X86RegparmOptions,
x86_win: X86RegparmOptions,
x86_mingw: X86RegparmOptions,
x86_stdcall: X86RegparmOptions,
x86_fastcall: CommonOptions,
x86_thiscall: CommonOptions,
x86_thiscall_mingw: CommonOptions,
x86_regcall_v3: CommonOptions,
x86_regcall_v4_win: CommonOptions,
x86_vectorcall: CommonOptions,
x86_interrupt: CommonOptions,
// `x86_16` 架构的调用约定。
x86_16_cdecl: CommonOptions,
x86_16_stdcall: CommonOptions,
x86_16_regparmcall: CommonOptions,
x86_16_interrupt: CommonOptions,
// `aarch64` 和 `aarch64_be` 架构的调用约定。
aarch64_aapcs: CommonOptions,
aarch64_aapcs_darwin: CommonOptions,
aarch64_aapcs_win: CommonOptions,
aarch64_vfabi: CommonOptions,
aarch64_vfabi_sve: CommonOptions,
aarch64_preserve_none: CommonOptions,
/// 标准 `alpha` 调用约定。
alpha_osf: CommonOptions,
// `arm`、`armeb`、`thumb` 和 `thumbeb` 架构的调用约定。
/// ARM 架构过程调用标准
arm_aapcs: CommonOptions,
/// ARM 架构过程调用标准的向量浮点变体。
arm_aapcs_vfp: CommonOptions,
arm_interrupt: ArmInterruptOptions,
// `mips64` 和 `mips64el` 架构的调用约定。
mips64_n64: CommonOptions,
mips64_n32: CommonOptions,
mips64_interrupt: MipsInterruptOptions,
// `mips` 和 `mipsel` 架构的调用约定。
mips_o32: CommonOptions,
mips_interrupt: MipsInterruptOptions,
// `riscv64` 架构的调用约定。
riscv64_lp64: CommonOptions,
riscv64_lp64_v: CommonOptions,
riscv64_interrupt: RiscvInterruptOptions,
// `riscv32` 架构的调用约定。
riscv32_ilp32: CommonOptions,
riscv32_ilp32_v: CommonOptions,
riscv32_interrupt: RiscvInterruptOptions,
// `sparc64` 架构的调用约定。
sparc64_sysv: CommonOptions,
// `sparc` 架构的调用约定。
sparc_sysv: CommonOptions,
// `powerpc64` 和 `powerpc64le` 架构的调用约定。
powerpc64_elf: CommonOptions,
powerpc64_elf_altivec: CommonOptions,
powerpc64_elf_v2: CommonOptions,
// `powerpc` 和 `powerpcle` 架构的调用约定。
powerpc_sysv: CommonOptions,
powerpc_sysv_altivec: CommonOptions,
powerpc_aix: CommonOptions,
powerpc_aix_altivec: CommonOptions,
/// WebAssembly 工具约定中规定的标准 `wasm32` 和 `wasm64` 调用约定。
wasm_mvp: CommonOptions,
/// 标准 `arc`/`arceb` 调用约定。
arc_sysv: CommonOptions,
arc_interrupt: ArcInterruptOptions,
// `avr` 架构的调用约定。
avr_gnu,
avr_builtin,
avr_signal,
avr_interrupt,
/// 标准 `bpfel`/`bpfeb` 调用约定。
bpf_std: CommonOptions,
// `csky` 架构的调用约定。
csky_sysv: CommonOptions,
csky_interrupt: CommonOptions,
// `hexagon` 架构的调用约定。
hexagon_sysv: CommonOptions,
hexagon_sysv_hvx: CommonOptions,
/// 标准 `hppa` 调用约定。
hppa_elf: CommonOptions,
/// 标准 `hppa64` 调用约定。
hppa64_elf: CommonOptions,
kvx_lp64: CommonOptions,
kvx_ilp32: CommonOptions,
/// 标准 `lanai` 调用约定。
lanai_sysv: CommonOptions,
/// 标准 `loongarch64` 调用约定。
loongarch64_lp64: CommonOptions,
/// 标准 `loongarch32` 调用约定。
loongarch32_ilp32: CommonOptions,
// `m68k` 架构的调用约定。
m68k_sysv: CommonOptions,
m68k_gnu: CommonOptions,
m68k_rtd: CommonOptions,
m68k_interrupt: CommonOptions,
m88k_sysv: CommonOptions,
/// 标准 `microblaze`/`microblazeel` 调用约定。
microblaze_std: CommonOptions,
microblaze_interrupt: MicroblazeInterruptOptions,
/// 标准 `msp430` 调用约定。
msp430_eabi: CommonOptions,
msp430_interrupt: CommonOptions,
/// 标准 `or1k` 调用约定。
or1k_sysv: CommonOptions,
/// 标准 `propeller` 调用约定。
propeller_sysv: CommonOptions,
// `s390x` 架构的调用约定。
s390x_sysv: CommonOptions,
s390x_sysv_vx: CommonOptions,
// `sh`/`sheb` 架构的调用约定。
sh_gnu: CommonOptions,
sh_renesas: CommonOptions,
sh_interrupt: ShInterruptOptions,
/// 标准 `ve` 调用约定。
ve_sysv: CommonOptions,
// `xcore` 架构的调用约定。
xcore_xs1: CommonOptions,
xcore_xs2: CommonOptions,
// `xtensa`/`xtensaeb` 架构的调用约定。
xtensa_call0: CommonOptions,
xtensa_windowed: CommonOptions,
// `amdgcn` 架构的调用约定。
amdgcn_device: CommonOptions,
amdgcn_kernel,
amdgcn_cs: CommonOptions,
// `nvptx` 和 `nvptx64` 架构的调用约定。
nvptx_device,
nvptx_kernel,
// `spirv32` 和 `spirv64` 架构上内核及着色器的调用约定。
spirv_device,
spirv_vertex,
spirv_kernel: SpirvKernelOptions,
spirv_fragment: SpirvFragmentOptions,
spirv_task: SpirvKernelOptions,
spirv_mesh: SpirvMeshOptions,
// `ez80` 架构的调用约定。
ez80_cet,
ez80_tiflags,
// [snake2p 示例程序](https://github.com/benanderman/spork-8/blob/1bce10a2c3a3888a3f4ca8208112afbc5973fda4/Code/programs/snake2p.asm)
// 使用的调用约定。
spork8,
/// 大多数调用约定共享的选项。
pub const CommonOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
};
/// 支持 regparm 属性的 x86 调用约定选项,
/// 该属性可通过寄存器传递部分参数。
pub const X86RegparmOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
/// 先通过寄存器传递的参数数量;剩余参数
/// 按调用约定传递。
/// 等价于 Clang 和 GCC 中的 `__attribute__((regparm(x)))`。
register_params: u2 = 0,
};
/// `arc_interrupt` 调用约定的选项。
pub const ArcInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
/// 正在接收的中断类型。
type: InterruptType,
pub const InterruptType = enum(u2) {
ilink1,
ilink2,
ilink,
firq,
};
};
/// `arm_interrupt` 调用约定的选项。
pub const ArmInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
/// 正在接收的中断类型。
type: InterruptType = .generic,
pub const InterruptType = enum(u3) {
generic,
irq,
fiq,
swi,
abort,
undef,
};
};
/// `microblaze_interrupt` 调用约定的选项。
pub const MicroblazeInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
type: InterruptType = .regular,
pub const InterruptType = enum(u2) {
/// 用户异常;使用 `rtsd` 返回。
user,
/// 常规中断;使用 `rtid` 返回。
regular,
/// 快速中断;使用 `rtid` 返回。
fast,
/// 软件断点;通过 `rtbd` 返回。
breakpoint,
};
};
/// `mips_interrupt` 和 `mips64_interrupt` 调用约定的选项。
pub const MipsInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
/// 中断模式。
mode: InterruptMode = .eic,
pub const InterruptMode = enum(u4) {
eic,
sw0,
sw1,
hw0,
hw1,
hw2,
hw3,
hw4,
hw5,
};
};
/// `riscv32_interrupt` 和 `riscv64_interrupt` 调用约定的选项。
pub const RiscvInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
/// 特权模式。
mode: PrivilegeMode,
pub const PrivilegeMode = enum(u2) {
supervisor,
machine,
};
};
/// `sh_interrupt` 调用约定的选项。
pub const ShInterruptOptions = struct {
/// 调用函数时栈对齐的边界。
/// `null` 表示此调用约定的默认值。
incoming_stack_alignment: ?u64 = null,
save: SaveBehavior = .full,
pub const SaveBehavior = enum(u3) {
/// 仅保存 fpscr(若适用)。
fpscr,
/// 仅保存高编号寄存器,即*不*保存 r0 到 r7。
high,
/// 按通常方式保存所有寄存器。
full,
/// 使用 CPU 的快速寄存器组保存所有寄存器。
bank,
};
};
pub const SpirvKernelOptions = struct {
x: u32,
y: u32,
z: u32,
};
pub const SpirvFragmentOptions = struct {
pub const DepthAssumption = enum(u2) {
none = 0,
greater = 1,
less = 2,
unchanged = 3,
};
pixel_centered_integer: bool = false,
depth_assumption: DepthAssumption = .none,
};
pub const SpirvMeshOptions = struct {
pub const StageOutput = enum(u2) {
output_points = 0,
output_lines = 1,
output_triangles = 2,
};
stage_output: StageOutput = .output_triangles,
max_primitives: u32 = 1,
max_vertices: u32 = 3,
x: u32,
y: u32,
z: u32,
};
/// 返回此 `CallingConvention` 适用的 `std.Target.Cpu.Arch` 数组。
/// 断言 `cc` 不是 `.auto`、`.@"async"`、`.naked` 或 `.@"inline"`。
pub fn archs(cc: CallingConvention) []const std.Target.Cpu.Arch {
return std.Target.Cpu.Arch.fromCallingConvention(cc);
}
pub fn eql(a: CallingConvention, b: CallingConvention) bool {
return std.meta.eql(a, b);
}
pub fn withStackAlign(cc: CallingConvention, incoming_stack_alignment: u64) CallingConvention {
const tag: CallingConvention.Tag = cc;
var result = cc;
@field(result, @tagName(tag)).incoming_stack_alignment = incoming_stack_alignment;
return result;
}
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const AddressSpace = enum(u5) {
// CPU 地址空间。
generic,
gs,
fs,
ss,
// x86_16 额外地址空间。
/// 同时存储段及偏移量,从而允许寻址整个地址空间。
far,
// GPU 地址空间。
global,
constant,
param,
shared,
local,
private,
input,
output,
uniform,
push_constant,
storage_buffer,
physical_storage_buffer,
// AVR 地址空间。
flash,
flash1,
flash2,
flash3,
flash4,
flash5,
// Propeller 地址空间。
/// 此地址空间仅寻址 cog 本地 RAM。
cog,
/// 此地址空间仅可寻址共享 hub RAM。
hub,
/// 此地址空间仅寻址 "lookup" RAM。
lut,
// Web Assembly
externref,
funcref,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const SourceLocation = struct {
/// 编译时选定的名称。并非文件路径。
module: [:0]const u8,
/// 相对于所属模块的根目录。
file: [:0]const u8,
fn_name: [:0]const u8,
line: u32,
column: u32,
};
pub const TypeId = std.meta.Tag(Type);
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Type = union(enum) {
type,
void,
bool,
noreturn,
int: Int,
float: Float,
pointer: Pointer,
array: Array,
@"struct": Struct,
comptime_float,
comptime_int,
undefined,
null,
optional: Optional,
error_union: ErrorUnion,
error_set: ErrorSet,
@"enum": Enum,
@"union": Union,
@"fn": Fn,
@"opaque": Opaque,
frame: Frame,
@"anyframe": AnyFrame,
vector: Vector,
enum_literal,
spirv: Spirv,
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Int = struct {
signedness: Signedness,
bits: u16,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Float = struct {
bits: u16,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Pointer = struct {
size: Size,
attrs: Attributes,
child: type,
/// 哨兵类型是指针的元素类型,即此结构体中
/// `child` 字段的值。但这里无法
/// 引用该类型,因此使用 `*const anyopaque`。
/// 另见:`sentinel`
sentinel_ptr: ?*const anyopaque,
/// 从 `sentinel_ptr` 加载指针类型的哨兵值。
/// 如果该指针类型没有哨兵,则返回 `null`。
pub inline fn sentinel(comptime ptr: Pointer) ?ptr.child {
const sp: *const ptr.child = @ptrCast(@alignCast(ptr.sentinel_ptr orelse return null));
return sp.*;
}
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Size = enum(u2) {
one,
many,
slice,
c,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Attributes = struct {
@"const": bool = false,
@"volatile": bool = false,
@"allowzero": bool = false,
@"addrspace": ?AddressSpace = null,
@"align": ?usize = null,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Array = struct {
len: comptime_int,
child: type,
/// 哨兵的类型是数组元素类型,也就是此结构体中的
/// `child` 字段的值。但此处无法引用该类型,因此使用 `*const anyopaque`。
/// 另见:`sentinel`。
///
sentinel_ptr: ?*const anyopaque,
/// 从 `sentinel_ptr` 加载数组类型的哨兵值。
/// 若数组类型没有哨兵,则返回 `null`。
pub inline fn sentinel(comptime arr: Array) ?arr.child {
const sp: *const arr.child = @ptrCast(@alignCast(arr.sentinel_ptr orelse return null));
return sp.*;
}
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ContainerLayout = enum(u2) {
auto,
@"extern",
@"packed",
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Struct = struct {
is_tuple: bool,
layout: ContainerLayout,
/// 始终为 `null`(当 `layout != .@"packed"` 时)。
backing_integer: ?type,
field_names: []const [:0]const u8,
/// 保证与 `field_names` 长度相同。
field_types: []const type,
/// 保证与 `field_names` 长度相同。
field_attrs: []const FieldAttributes,
decl_names: []const [:0]const u8,
pub const FieldAttributes = struct {
@"comptime": bool = false,
/// `null` 表示未显式指定字段对齐要求。字段仍至少
/// 按照其类型的 `@alignOf` 对齐。
@"align": ?usize = null,
/// 默认值的类型是此结构体字段的类型。但此处
/// 不知道该类型,因此改用类型擦除的指针,必须将其转换为
/// 指向字段类型的指针。
///
/// 另请参见:`defaultValue`。
default_value_ptr: ?*const anyopaque = null,
/// 从 `default_value_ptr` 加载字段的默认值。
/// `FieldType` 必须与 `Struct.field_types` 中对应元素完全匹配。
/// 若字段没有默认值,则返回 `null`。
pub inline fn defaultValue(comptime attrs: FieldAttributes, comptime FieldType: type) ?FieldType {
const dp: *const FieldType = @ptrCast(@alignCast(attrs.default_value_ptr orelse return null));
return dp.*;
}
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Optional = struct {
child: type,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ErrorUnion = struct {
error_set: type,
payload: type,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ErrorSet = struct {
error_names: ?[]const [:0]const u8,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Enum = struct {
tag_type: type,
mode: Mode,
field_names: []const [:0]const u8,
/// 保证与 `field_names` 长度相同。
field_values: []const comptime_int,
decl_names: []const [:0]const u8,
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Mode = enum { exhaustive, nonexhaustive };
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Union = struct {
layout: ContainerLayout,
tag_type: ?type,
/// 始终为 `null`(当 `layout != .@"packed"` 时)。
backing_integer: ?type,
field_names: []const [:0]const u8,
/// 保证与 `field_names` 长度相同。
field_types: []const type,
/// 保证与 `field_names` 长度相同。
field_attrs: []const FieldAttributes,
decl_names: []const [:0]const u8,
pub const FieldAttributes = struct {
/// `null` 表示未显式指定字段对齐要求。字段仍至少
/// 按照其类型的 `@alignOf` 对齐。
@"align": ?usize = null,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Fn = struct {
attrs: Attributes,
is_generic: bool,
/// `null` 表示返回类型是泛型,即依赖函数参数。
return_type: ?type,
/// `null` 元素表示 `anytype` 参数,或泛型类型参数,即类型依赖先前的函数参数。
///
param_types: []const ?type,
/// 保证长度与 `param_types` 相同。
param_attrs: []const ParamAttributes,
pub const ParamAttributes = struct {
@"noalias": bool = false,
};
pub const Attributes = struct {
@"callconv": CallingConvention = .auto,
varargs: bool = false,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Spirv = union(enum(u2)) {
sampler,
image: Image,
sampled_image: type,
runtime_array: type,
pub const Image = struct {
usage: Usage,
format: Format,
dim: Dimensionality,
depth: Depth,
access: Access,
arrayed: bool,
multisampled: bool,
pub const Usage = union(enum(u2)) {
unknown: type,
sampled: type,
storage: type,
};
pub const Format = enum(u4) {
unknown,
rgba32f,
rgba32i,
rgba32u,
rgba16f,
rgba16i,
rgba16u,
rgba8unorm,
rgba8snorm,
rgba8i,
rgba8u,
r32f,
r32i,
r32u,
};
pub const Dimensionality = enum(u2) {
@"1d",
@"2d",
@"3d",
cube,
};
pub const Depth = enum(u2) { unknown, depth, not_depth };
pub const Access = enum(u2) { unknown, read_only, write_only, read_write };
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Opaque = struct {
decl_names: []const [:0]const u8,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Frame = struct {
function: *const anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const AnyFrame = struct {
child: ?type,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Vector = struct {
len: comptime_int,
child: type,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const FloatMode = enum {
strict,
optimized,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Endian = enum {
big,
little,
pub const native = builtin.target.cpu.arch.endian();
pub const foreign: Endian = @fromBackingInt(@intCast(1 - @backingInt(native)));
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const Signedness = enum(u1) {
signed,
unsigned,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const OutputMode = enum(u2) {
Exe,
Lib,
Obj,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const LinkMode = enum(u1) {
static,
dynamic,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const UnwindTables = enum {
none,
sync,
async,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const WasiExecModel = enum {
command,
reactor,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const CallModifier = enum {
/// 等价于函数调用语法。
auto,
/// 阻止尾调用优化。保证返回地址指向
/// 调用位置,而非该调用位置的调用位置。
/// 若该调用另有要求必须进行尾调用
/// 或内联,则产生编译错误。
never_tail,
/// 保证调用不被内联。若该调用另有要求必须内联,
/// 则改为产生编译错误。
never_inline,
/// 断言该函数调用不会挂起。这允许
/// 非异步函数调用异步函数。
no_suspend,
/// 保证生成的调用采用尾调用优化。
/// 如果无法实现,则产生编译错误。
always_tail,
/// 保证在调用位置内联此调用。
/// 若无法做到,则产生编译错误。
always_inline,
/// 在编译期对调用求值。若调用无法在编译期完成,则产生编译错误。
///
compile_time,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListAarch64 = extern struct {
__stack: *anyopaque,
__gr_top: *anyopaque,
__vr_top: *anyopaque,
__gr_offs: c_int,
__vr_offs: c_int,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListAlpha = extern struct {
__base: *anyopaque,
__offset: c_int,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListArm = extern struct {
__ap: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListHexagon = extern struct {
__current_saved_reg_area_pointer: *anyopaque,
__saved_reg_area_end_pointer: *anyopaque,
__overflow_area_pointer: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListPowerPc = extern struct {
gpr: u8,
fpr: u8,
reserved: c_ushort,
overflow_arg_area: *anyopaque,
reg_save_area: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListS390x = extern struct {
__gpr: c_long,
__fpr: c_long,
__overflow_arg_area: *anyopaque,
__reg_save_area: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListSh = extern struct {
__va_next_o: *anyopaque,
__va_next_o_limit: *anyopaque,
__va_next_fp: *anyopaque,
__va_next_fp_limit: *anyopaque,
__va_next_stack: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListX86_64 = extern struct {
gp_offset: c_uint,
fp_offset: c_uint,
overflow_arg_area: *anyopaque,
reg_save_area: *anyopaque,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaListXtensa = extern struct {
__va_stk: *c_int,
__va_reg: *c_int,
__va_ndx: c_int,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const VaList = switch (builtin.cpu.arch) {
.amdgcn,
.msp430,
.nvptx,
.nvptx64,
.powerpc64,
.powerpc64le,
.x86,
=> *u8,
.arc,
.arceb,
.avr,
.bpfel,
.bpfeb,
.csky,
.hppa,
.hppa64,
.kvx,
.lanai,
.loongarch32,
.loongarch64,
.m68k,
.microblaze,
.microblazeel,
.mips,
.mipsel,
.mips64,
.mips64el,
.riscv32,
.riscv32be,
.riscv64,
.riscv64be,
.sparc,
.sparc64,
.spirv32,
.spirv64,
.ve,
.wasm32,
.wasm64,
.xcore,
=> *anyopaque,
.aarch64, .aarch64_be => switch (builtin.os.tag) {
.driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos, .windows => *u8,
else => switch (builtin.zig_backend) {
else => VaListAarch64,
.stage2_llvm => @compileError("disabled due to miscompilations"),
},
},
.alpha => VaListAlpha,
.arm, .armeb, .thumb, .thumbeb => VaListArm,
.hexagon => if (builtin.target.abi.isMusl()) VaListHexagon else *u8,
.powerpc, .powerpcle => VaListPowerPc,
.s390x => VaListS390x,
.sh, .sheb => VaListSh, // 这对 `sh_renesas` 不正确:https://github.com/ziglang/zig/issues/24692#issuecomment-3150779829
.x86_64 => switch (builtin.os.tag) {
.uefi, .windows => switch (builtin.zig_backend) {
else => *u8,
.stage2_llvm => @compileError("disabled due to miscompilations"),
},
else => VaListX86_64,
},
.xtensa, .xtensaeb => VaListXtensa,
else => @compileError("VaList not supported for this target yet"),
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const PrefetchOptions = struct {
/// 预取应为读取还是写入做准备。
rw: Rw = .read,
/// 数据的局部性,取值为闭区间 0 到 3。
///
/// 0 表示没有时间局部性,即数据在访问后
/// 可以立即从缓存中丢弃。
///
/// 3 表示时间局部性高,即数据很可能很快再次被访问,
/// 因此应保留在缓存中。
locality: u2 = 3,
/// 执行预取操作所针对的缓存。
cache: Cache = .data,
pub const Rw = enum(u1) {
read,
write,
};
pub const Cache = enum(u1) {
instruction,
data,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ExportOptions = struct {
name: []const u8,
linkage: GlobalLinkage = .strong,
section: ?[]const u8 = null,
visibility: SymbolVisibility = .default,
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const ExternOptions = struct {
name: []const u8,
library_name: ?[]const u8 = null,
linkage: GlobalLinkage = .strong,
visibility: SymbolVisibility = .default,
/// 将此项设为 `true`,使 `@extern` 成为运行期值。
is_thread_local: bool = false,
is_dll_import: bool = false,
relocation: Relocation = .any,
decoration: ?Decoration = null,
pub const Decoration = union(enum) {
location: u32,
flat: u32,
descriptor: Descriptor,
pub const Descriptor = struct {
set: u32,
binding: u32,
};
};
pub const Relocation = enum(u1) {
/// 允许任意类型的重定位。
any,
/// 需要相对于程序计数器的重定位。
/// 使用此值会使 `@extern` 成为运行时值。
pcrel,
};
};
/// 此数据结构由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const BranchHint = enum(u3) {
/// 等同于未提供提示。
none,
/// 此控制流分支比同级分支更可能到达。
/// 优化器应针对到达此分支进行优化。
likely,
/// 此控制流分支比同级分支更不可能到达。
/// 优化器应针对不进入该分支的情况进行优化。
unlikely,
/// 此控制流分支极不可能到达。
/// 优化器可将它放在不同内存页中,以优化其他分支。
cold,
/// 难以预测控制流是否会到达此分支。
/// 优化器应避免产生误预测代价高昂的分支行为。
unpredictable,
};
/// 此枚举由编译器设置,用于表示生成机器码所使用的编译器实现。
///
/// 理论上,Zig 代码应当与实现该语言的后端无关。观察此值的唯一理由是**规避编译器实现中的问题**。
///
/// 常见陷阱是:编译器后端不在白名单中就使编译失败。更稳健的策略是检查后端是否属于已知会触发特定问题的黑名单。
///
/// 此枚举为非穷尽枚举,因此其他 Zig 语言实现可以随机选取数值作为标签,避免与其他实现冲突;即使上游枚举未给该数值命名,代码库仍可与这些值交互。当然,上游欢迎提交补丁,将其他 Zig 实现添加到此枚举中。
///
/// 此数据结构是 Zig 语言规范的一部分。
///
///
///
///
///
///
///
///
///
///
pub const CompilerBackend = enum(u64) {
/// 允许编译器实现不公开自身标识,
/// 此时应使用此值。请确保你的代码
/// 也支持 `other` Zig 编译器!
other = 0,
/// Andrew Kelley 于 2015 年创建的原始 Zig 编译器。
/// 使用 C++ 实现,使用 LLVM。2022 年 12 月 6 日,
/// 从 ZSF 的 ziglang/zig 代码库中删除。
stage1 = 1,
/// Zig 的参考实现自举编译器,
/// 使用 LLVM 后端。
stage2_llvm = 2,
/// Zig 的自托管编译器参考实现,使用生成 C 源码的
/// 后端。
///
/// 注意,可通过 `object_format` 值(而非 `compiler_backend` 值)
/// 直接判断编译是否会输出 C 代码。
stage2_c = 3,
/// Zig 的自托管参考实现编译器,使用 WebAssembly 后端。
///
stage2_wasm = 4,
/// Zig 的参考实现自举编译器,
/// 使用 arm 后端。
stage2_arm = 5,
/// Zig 的自托管参考实现编译器,使用 x86_64 后端。
///
stage2_x86_64 = 6,
/// Zig 的自托管参考实现编译器,使用 aarch64 后端。
///
stage2_aarch64 = 7,
/// Zig 的自举编译器参考实现,使用
/// x86 后端。
stage2_x86 = 8,
/// Zig 的参考实现自举编译器,
/// 使用 riscv64 后端。
stage2_riscv64 = 9,
/// Zig 的自举编译器参考实现,使用
/// sparc64 后端。
stage2_sparc64 = 10,
/// Zig 的参考实现自举编译器,
/// 使用 spirv 后端。
stage2_spirv = 11,
/// Zig 的自托管参考实现编译器,使用 powerpc 后端。
///
stage2_powerpc = 12,
/// Zig 的自托管参考实现编译器,使用 loongarch 后端。
///
stage2_loongarch = 13,
/// Zig 软件基金会的 Zig 自举实现。此后端
/// 最初由 Ben Anderman 于 2026 年贡献。
zsf_spork8 = 14,
_,
};
/// 此函数类型由 Zig 语言代码生成使用,因此必须与编译器实现保持同步。
///
pub const TestFn = struct {
name: []const u8,
func: *const fn () anyerror!void,
};
/// Zig 编译器使用此命名空间生成各种由安全检查触发的 panic。可以在根源码文件中定义公开的 `panic` 命名空间
/// 覆盖这些行为。
///
pub const panic: type = p: {
if (@hasDecl(root, "panic")) {
if (@TypeOf(root.panic) != type) {
// 已弃用;改为将 `panic` 设为命名空间。
break :p std.debug.FullPanic(struct {
fn panic(msg: []const u8, ra: ?usize) noreturn {
root.panic(msg, @errorReturnTrace(), ra);
}
}.panic);
}
break :p root.panic;
}
break :p switch (builtin.zig_backend) {
.stage2_loongarch,
.stage2_powerpc,
.stage2_riscv64,
=> std.debug.simple_panic,
else => std.debug.FullPanic(std.debug.defaultPanic),
};
};
pub noinline fn returnError() void {
@branchHint(.unlikely);
@setRuntimeSafety(false);
const st = @errorReturnTrace().?;
if (st.index < st.instruction_addresses.len)
st.instruction_addresses[st.index] = @returnAddress();
st.index += 1;
}