diff --git a/doc/langref.html.in b/doc/langref.html.in index 35ca9a13b4..7c1f9b53d9 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4560,6 +4560,19 @@ comptime {
{#see_also|Alignment#} {#header_close#} + + {#header_open|@boolToInt#} +@boolToInt(value: bool) u1
+
+ Converts true to u1(1) and false to
+ u1(0).
+
+ If the value is known at compile-time, the return type is comptime_int
+ instead of u1.
+
@cDefine(comptime name: []u8, value)
@@ -4834,21 +4847,6 @@ test "main" { Creates a symbol in the output object file.
{#header_close#} - {#header_open|@tagName#} -@tagName(value: var) []const u8
- - Converts an enum value or union value to a slice of bytes representing the name. -
- {#header_close#} - {#header_open|@TagType#} -@TagType(T: type) type
- - For an enum, returns the integer type that is used to store the enumeration value. -
-- For a union, returns the enum type that is used to store the tag value. -
- {#header_close#} {#header_open|@errorName#}@errorName(err: error) []u8
@@ -4883,6 +4881,12 @@ test "main" {
{#see_also|Compile Variables#} {#header_close#} + + {#header_open|@field#} +@field(lhs: var, comptime field_name: []const u8) (field)
+ Preforms field access equivalent to lhs.->field_name-<.
@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8,
field_ptr: *T) *ParentType
@@ -4890,6 +4894,23 @@ test "main" {
Given a pointer to a field, returns the base pointer of a struct.
{#header_close#}
+
+ {#header_open|@floatCast#}
+ @floatCast(comptime DestType: type, value: var) DestType
+ + Convert from one float type to another. This cast is safe, but may cause the + numeric value to lose precision. +
+ {#header_close#} + + {#header_open|@floatToInt#} +@floatToInt(comptime DestType: type, float: var) DestType
+ + Converts the integer part of a floating point number to the destination type. + To convert the other way, use {#link|@intToFloat#}. This cast is always safe. +
+ {#header_close#} + {#header_open|@frameAddress#}@frameAddress()
@@ -4944,12 +4965,30 @@ fn add(a: i32, b: i32) i32 { return a + b; }
{#see_also|@noInlineCall#} {#header_close#} + + {#header_open|@intCast#} +@intCast(comptime DestType: type, int: var) DestType
+ + Converts an integer to another integer while keeping the same numerical value. + Attempting to convert a number which is out of range of the destination type results in + {#link|Undefined Behavior#}. +
+ {#header_close#} + + {#header_open|@intToFloat#} +@intToFloat(comptime DestType: type, int: var) DestType
+ + Converts an integer to the closest floating point representation. To convert the other way, use {#link|@floatToInt#}. This cast is always safe. +
+ {#header_close#} + {#header_open|@intToPtr#}@intToPtr(comptime DestType: type, int: usize) DestType
Converts an integer to a pointer. To convert the other way, use {#link|@ptrToInt#}.
{#header_close#} + {#header_open|@IntType#}@IntType(comptime is_signed: bool, comptime bit_count: u8) type
@@ -4987,10 +5026,6 @@ fn add(a: i32, b: i32) i32 { return a + b; } It does not include functions, variables, or constants.
{#header_close#} - {#header_open|@field#} -@field(lhs: var, comptime field_name: []const u8) (field)
- Preforms field access equivalent to lhs.->field_name-<.
@memberType(comptime T: type, comptime index: usize) type
Returns the field type of a struct or union.
@@ -5370,6 +5405,21 @@ pub const FloatMode = enum { If no overflow or underflow occurs, returnsfalse.
{#header_close#}
+ {#header_open|@tagName#}
+ @tagName(value: var) []const u8
+ + Converts an enum value or union value to a slice of bytes representing the name. +
+ {#header_close#} + {#header_open|@TagType#} +@TagType(T: type) type
+ + For an enum, returns the integer type that is used to store the enumeration value. +
++ For a union, returns the enum type that is used to store the tag value. +
+ {#header_close#} {#header_open|@truncate#}@truncate(comptime T: type, integer) T
@@ -6665,7 +6715,7 @@ hljs.registerLanguage("zig", function(t) {
a = t.IR + "\\s*\\(",
c = {
keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong resume cancel await async orelse",
- built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall",
+ built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall",
literal: "true false null undefined"
},
n = [e, t.CLCM, t.CBCM, s, r];
diff --git a/src/all_types.hpp b/src/all_types.hpp
index bc2fe07c18..d94dfa0fcb 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -1361,6 +1361,7 @@ enum BuiltinFnId {
BuiltinFnIdFloatCast,
BuiltinFnIdIntToFloat,
BuiltinFnIdFloatToInt,
+ BuiltinFnIdBoolToInt,
BuiltinFnIdIntType,
BuiltinFnIdSetCold,
BuiltinFnIdSetRuntimeSafety,
@@ -2048,6 +2049,7 @@ enum IrInstructionId {
IrInstructionIdFloatCast,
IrInstructionIdIntToFloat,
IrInstructionIdFloatToInt,
+ IrInstructionIdBoolToInt,
IrInstructionIdIntType,
IrInstructionIdBoolNot,
IrInstructionIdMemset,
@@ -2668,6 +2670,12 @@ struct IrInstructionFloatToInt {
IrInstruction *target;
};
+struct IrInstructionBoolToInt {
+ IrInstruction base;
+
+ IrInstruction *target;
+};
+
struct IrInstructionIntType {
IrInstruction base;
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 4108cbbd68..84335d4e06 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -4726,6 +4726,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
+ case IrInstructionIdBoolToInt:
zig_unreachable();
case IrInstructionIdReturn:
@@ -6318,6 +6319,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdFloatCast, "floatCast", 2);
create_builtin_fn(g, BuiltinFnIdIntToFloat, "intToFloat", 2);
create_builtin_fn(g, BuiltinFnIdFloatToInt, "floatToInt", 2);
+ create_builtin_fn(g, BuiltinFnIdBoolToInt, "boolToInt", 1);
create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1);
create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
diff --git a/src/ir.cpp b/src/ir.cpp
index 0b847fc4e4..e6339a72f6 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -476,6 +476,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatToInt *) {
return IrInstructionIdFloatToInt;
}
+static constexpr IrInstructionId ir_instruction_id(IrInstructionBoolToInt *) {
+ return IrInstructionIdBoolToInt;
+}
+
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntType *) {
return IrInstructionIdIntType;
}
@@ -1959,6 +1963,15 @@ static IrInstruction *ir_build_float_to_int(IrBuilder *irb, Scope *scope, AstNod
return &instruction->base;
}
+static IrInstruction *ir_build_bool_to_int(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target) {
+ IrInstructionBoolToInt *instruction = ir_build_instruction