Skip to content

Commit

Permalink
remove integer and float casting syntax
Browse files Browse the repository at this point in the history
 * add `@intCast`
 * add `@floatCast`
 * add `@floatToInt`
 * add `@intToFloat`

See #1061
  • Loading branch information
andrewrk committed Jun 17, 2018
1 parent 06a26f0 commit 7912061
Show file tree
Hide file tree
Showing 85 changed files with 799 additions and 413 deletions.
12 changes: 6 additions & 6 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ var some_integers: [100]i32 = undefined;

test "modify an array" {
for (some_integers) |*item, i| {
item.* = i32(i);
item.* = @intCast(i32, i);
}
assert(some_integers[10] == 10);
assert(some_integers[99] == 99);
Expand Down Expand Up @@ -1397,8 +1397,8 @@ var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (initial_value) |*pt, i| {
pt.* = Point{
.x = i32(i),
.y = i32(i) * 2,
.x = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
};
}
break :init initial_value;
Expand Down Expand Up @@ -2410,7 +2410,7 @@ test "for basics" {
var sum2: i32 = 0;
for (items) |value, i| {
assert(@typeOf(i) == usize);
sum2 += i32(i);
sum2 += @intCast(i32, i);
}
assert(sum2 == 10);
}
Expand Down Expand Up @@ -5730,7 +5730,7 @@ comptime {
{#code_begin|test_err|attempt to cast negative value to unsigned integer#}
comptime {
const value: i32 = -1;
const unsigned = u32(value);
const unsigned = @intCast(u32, value);
}
{#code_end#}
<p>At runtime crashes with the message <code>attempt to cast negative value to unsigned integer</code> and a stack trace.</p>
Expand All @@ -5744,7 +5744,7 @@ comptime {
{#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#}
comptime {
const spartan_count: u16 = 300;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
}
{#code_end#}
<p>At runtime crashes with the message <code>integer cast truncated bits</code> and a stack trace.</p>
Expand Down
36 changes: 36 additions & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,10 @@ enum BuiltinFnId {
BuiltinFnIdMod,
BuiltinFnIdSqrt,
BuiltinFnIdTruncate,
BuiltinFnIdIntCast,
BuiltinFnIdFloatCast,
BuiltinFnIdIntToFloat,
BuiltinFnIdFloatToInt,
BuiltinFnIdIntType,
BuiltinFnIdSetCold,
BuiltinFnIdSetRuntimeSafety,
Expand Down Expand Up @@ -2040,6 +2044,10 @@ enum IrInstructionId {
IrInstructionIdCmpxchg,
IrInstructionIdFence,
IrInstructionIdTruncate,
IrInstructionIdIntCast,
IrInstructionIdFloatCast,
IrInstructionIdIntToFloat,
IrInstructionIdFloatToInt,
IrInstructionIdIntType,
IrInstructionIdBoolNot,
IrInstructionIdMemset,
Expand Down Expand Up @@ -2632,6 +2640,34 @@ struct IrInstructionTruncate {
IrInstruction *target;
};

struct IrInstructionIntCast {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionFloatCast {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionIntToFloat {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionFloatToInt {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionIntType {
IrInstruction base;

Expand Down
8 changes: 8 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdPromiseResultType:
case IrInstructionIdAwaitBookkeeping:
case IrInstructionIdAddImplicitReturnType:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
zig_unreachable();

case IrInstructionIdReturn:
Expand Down Expand Up @@ -6310,6 +6314,10 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6);
create_builtin_fn(g, BuiltinFnIdFence, "fence", 1);
create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2);
create_builtin_fn(g, BuiltinFnIdIntCast, "intCast", 2);
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, BuiltinFnIdCompileErr, "compileError", 1);
create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
Expand Down

0 comments on commit 7912061

Please sign in to comment.