| ... | ... | @@ -8315,216 +8315,15 @@ pub const TypeId = enum { |
| 8315 | 8315 | {#header_close#} |
| 8316 | 8316 | |
| 8317 | 8317 | {#header_open|@typeInfo#} |
| 8318 | | <pre>{#syntax#}@typeInfo(comptime T: type) @import("builtin").TypeInfo{#endsyntax#}</pre> |
| 8318 | <pre>{#syntax#}@typeInfo(comptime T: type) @import("std").builtin.TypeInfo{#endsyntax#}</pre> |
| 8319 | 8319 | <p> |
| 8320 | | Returns information on the type. Returns a value of the following union: |
| 8320 | Provides type reflection. |
| 8321 | 8321 | </p> |
| 8322 | | {#code_begin|syntax#} |
| 8323 | | pub const TypeInfo = union(TypeId) { |
| 8324 | | Type: void, |
| 8325 | | Void: void, |
| 8326 | | Bool: void, |
| 8327 | | NoReturn: void, |
| 8328 | | Int: Int, |
| 8329 | | Float: Float, |
| 8330 | | Pointer: Pointer, |
| 8331 | | Array: Array, |
| 8332 | | Struct: Struct, |
| 8333 | | ComptimeFloat: void, |
| 8334 | | ComptimeInt: void, |
| 8335 | | Undefined: void, |
| 8336 | | Null: void, |
| 8337 | | Optional: Optional, |
| 8338 | | ErrorUnion: ErrorUnion, |
| 8339 | | ErrorSet: ErrorSet, |
| 8340 | | Enum: Enum, |
| 8341 | | Union: Union, |
| 8342 | | Fn: Fn, |
| 8343 | | BoundFn: Fn, |
| 8344 | | Opaque: void, |
| 8345 | | Promise: Promise, |
| 8346 | | Vector: Vector, |
| 8347 | | EnumLiteral: void, |
| 8348 | | |
| 8349 | | |
| 8350 | | pub const Int = struct { |
| 8351 | | is_signed: bool, |
| 8352 | | bits: comptime_int, |
| 8353 | | }; |
| 8354 | | |
| 8355 | | pub const Float = struct { |
| 8356 | | bits: comptime_int, |
| 8357 | | }; |
| 8358 | | |
| 8359 | | pub const Pointer = struct { |
| 8360 | | size: Size, |
| 8361 | | is_const: bool, |
| 8362 | | is_volatile: bool, |
| 8363 | | alignment: comptime_int, |
| 8364 | | child: type, |
| 8365 | | is_allowzero: bool, |
| 8366 | | sentinel: var, |
| 8367 | | |
| 8368 | | pub const Size = enum { |
| 8369 | | One, |
| 8370 | | Many, |
| 8371 | | Slice, |
| 8372 | | C, |
| 8373 | | }; |
| 8374 | | }; |
| 8375 | | |
| 8376 | | pub const Array = struct { |
| 8377 | | len: comptime_int, |
| 8378 | | child: type, |
| 8379 | | sentinel: var, |
| 8380 | | }; |
| 8381 | | |
| 8382 | | pub const ContainerLayout = enum { |
| 8383 | | Auto, |
| 8384 | | Extern, |
| 8385 | | Packed, |
| 8386 | | }; |
| 8387 | | |
| 8388 | | pub const StructField = struct { |
| 8389 | | name: []const u8, |
| 8390 | | offset: ?comptime_int, |
| 8391 | | field_type: type, |
| 8392 | | }; |
| 8393 | | |
| 8394 | | pub const Struct = struct { |
| 8395 | | layout: ContainerLayout, |
| 8396 | | fields: []StructField, |
| 8397 | | decls: []Declaration, |
| 8398 | | }; |
| 8399 | | |
| 8400 | | pub const Optional = struct { |
| 8401 | | child: type, |
| 8402 | | }; |
| 8403 | | |
| 8404 | | pub const ErrorUnion = struct { |
| 8405 | | error_set: type, |
| 8406 | | payload: type, |
| 8407 | | }; |
| 8408 | | |
| 8409 | | pub const Error = struct { |
| 8410 | | name: []const u8, |
| 8411 | | value: comptime_int, |
| 8412 | | }; |
| 8413 | | |
| 8414 | | pub const ErrorSet = ?[]Error; |
| 8415 | | |
| 8416 | | pub const EnumField = struct { |
| 8417 | | name: []const u8, |
| 8418 | | value: comptime_int, |
| 8419 | | }; |
| 8420 | | |
| 8421 | | pub const Enum = struct { |
| 8422 | | layout: ContainerLayout, |
| 8423 | | tag_type: type, |
| 8424 | | fields: []EnumField, |
| 8425 | | decls: []Declaration, |
| 8426 | | }; |
| 8427 | | |
| 8428 | | pub const UnionField = struct { |
| 8429 | | name: []const u8, |
| 8430 | | enum_field: ?EnumField, |
| 8431 | | field_type: type, |
| 8432 | | }; |
| 8433 | | |
| 8434 | | pub const Union = struct { |
| 8435 | | layout: ContainerLayout, |
| 8436 | | tag_type: ?type, |
| 8437 | | fields: []UnionField, |
| 8438 | | decls: []Declaration, |
| 8439 | | }; |
| 8440 | | |
| 8441 | | pub const CallingConvention = enum { |
| 8442 | | Unspecified, |
| 8443 | | C, |
| 8444 | | Cold, |
| 8445 | | Naked, |
| 8446 | | Stdcall, |
| 8447 | | Async, |
| 8448 | | }; |
| 8449 | | |
| 8450 | | pub const FnArg = struct { |
| 8451 | | is_generic: bool, |
| 8452 | | is_noalias: bool, |
| 8453 | | arg_type: ?type, |
| 8454 | | }; |
| 8455 | | |
| 8456 | | pub const Fn = struct { |
| 8457 | | calling_convention: CallingConvention, |
| 8458 | | is_generic: bool, |
| 8459 | | is_var_args: bool, |
| 8460 | | return_type: ?type, |
| 8461 | | async_allocator_type: ?type, |
| 8462 | | args: []FnArg, |
| 8463 | | }; |
| 8464 | | |
| 8465 | | pub const Promise = struct { |
| 8466 | | child: ?type, |
| 8467 | | }; |
| 8468 | | |
| 8469 | | pub const Vector = struct { |
| 8470 | | len: comptime_int, |
| 8471 | | child: type, |
| 8472 | | }; |
| 8473 | | |
| 8474 | | pub const Declaration = struct { |
| 8475 | | name: []const u8, |
| 8476 | | is_pub: bool, |
| 8477 | | data: Data, |
| 8478 | | |
| 8479 | | pub const Data = union(enum) { |
| 8480 | | Type: type, |
| 8481 | | Var: type, |
| 8482 | | Fn: FnDecl, |
| 8483 | | |
| 8484 | | pub const FnDecl = struct { |
| 8485 | | fn_type: type, |
| 8486 | | inline_type: Inline, |
| 8487 | | calling_convention: CallingConvention, |
| 8488 | | is_var_args: bool, |
| 8489 | | is_extern: bool, |
| 8490 | | is_export: bool, |
| 8491 | | lib_name: ?[]const u8, |
| 8492 | | return_type: type, |
| 8493 | | arg_names: [][] const u8, |
| 8494 | | |
| 8495 | | pub const Inline = enum { |
| 8496 | | Auto, |
| 8497 | | Always, |
| 8498 | | Never, |
| 8499 | | }; |
| 8500 | | }; |
| 8501 | | }; |
| 8502 | | }; |
| 8503 | | }; |
| 8504 | | {#code_end#} |
| 8505 | 8322 | <p> |
| 8506 | 8323 | For {#link|structs|struct#}, {#link|unions|union#}, {#link|enums|enum#}, and |
| 8507 | 8324 | {#link|error sets|Error Set Type#}, the fields are guaranteed to be in the same |
| 8508 | 8325 | order as declared. For declarations, the order is unspecified. |
| 8509 | 8326 | </p> |
| 8510 | | <p> |
| 8511 | | Note that the {#syntax#}sentinel{#endsyntax#} field in {#syntax#}TypeInfo.Pointer{#endsyntax#} |
| 8512 | | and {#syntax#}TypeInfo.Array{#endsyntax#} is of type {#syntax#}var{#endsyntax#} rather |
| 8513 | | than {#syntax#}?child{#endsyntax#}. {#syntax#}TypeInfo.Pointer{#endsyntax#} |
| 8514 | | and {#syntax#}TypeInfo.Array{#endsyntax#} can be constructed with any comptime value for the |
| 8515 | | {#syntax#}sentinel{#endsyntax#} field. However, the {#syntax#}@Type{#endsyntax#} builtin will |
| 8516 | | only accept the TypeInfo struct if the sentinel value is implicitly convertible to |
| 8517 | | {#syntax#}child{#endsyntax#}. Furthermore, any {#syntax#}TypeInfo.Pointer{#endsyntax#} |
| 8518 | | or {#syntax#}TypeInfo.Array{#endsyntax#} retreived from {#syntax#}@typeInfo{#endsyntax#} will |
| 8519 | | guarantee that {#syntax#}@typeOf(sentinel){#endsyntax#} is equal to |
| 8520 | | {#syntax#}?child{#endsyntax#}. For example, {#syntax#}@typeOf(sentinel){#endsyntax#} for a |
| 8521 | | {#syntax#}TypeInfo.Pointer{#endsyntax#} constructed with |
| 8522 | | {#syntax#}TypeInfo.Pointer { ... .sentinel = 0; ... }{#endsyntax#} would be |
| 8523 | | {#syntax#}comptime_int{#endsyntax#} rather than {#syntax#}?u8{#endsyntax#}. However, if you |
| 8524 | | passed that {#syntax#}TypeInfo.Pointer{#endsyntax#} struct to |
| 8525 | | {#syntax#}@typeInfo(@Type(myPointerInfo)){#endsyntax#} then {#syntax#}@typeOf(sentinel){#endsyntax#} |
| 8526 | | would be {#syntax#}?u8{#endsyntax#}. |
| 8527 | | </p> |
| 8528 | 8327 | {#header_close#} |
| 8529 | 8328 | |
| 8530 | 8329 | {#header_open|@typeName#} |