Skip to content

Commit e9a03cc

Browse files
committedJul 16, 2018
all integer sizes are available as primitives
* fix wrong implicit cast for `@IntType` bit_count parameter. * fix incorrect docs for `@IntType` bit_count parameter. closes #1242 closes #745 closes #1240
·
0.15.10.3.0
1 parent 363f4fa commit e9a03cc

File tree

17 files changed

+74
-104
lines changed

17 files changed

+74
-104
lines changed
 

‎doc/langref.html.in‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,11 +2310,11 @@ test "while loop continue expression" {
23102310
}
23112311

23122312
test "while loop continue expression, more complicated" {
2313-
var i1: usize = 1;
2314-
var j1: usize = 1;
2315-
while (i1 * j1 < 2000) : ({ i1 *= 2; j1 *= 3; }) {
2316-
const my_ij1 = i1 * j1;
2317-
assert(my_ij1 < 2000);
2313+
var i: usize = 1;
2314+
var j: usize = 1;
2315+
while (i * j < 2000) : ({ i *= 2; j *= 3; }) {
2316+
const my_ij = i * j;
2317+
assert(my_ij < 2000);
23182318
}
23192319
}
23202320
{#code_end#}
@@ -5424,7 +5424,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
54245424
{#header_close#}
54255425

54265426
{#header_open|@IntType#}
5427-
<pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u8) type</code></pre>
5427+
<pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u32) type</code></pre>
54285428
<p>
54295429
This function returns an integer type with the given signness and bit count.
54305430
</p>

‎src/all_types.hpp‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,6 @@ struct CodeGen {
15871587

15881588
struct {
15891589
TypeTableEntry *entry_bool;
1590-
TypeTableEntry *entry_int[2][12]; // [signed,unsigned][2,3,4,5,6,7,8,16,29,32,64,128]
15911590
TypeTableEntry *entry_c_int[CIntTypeCount];
15921591
TypeTableEntry *entry_c_longdouble;
15931592
TypeTableEntry *entry_c_void;
@@ -1596,12 +1595,9 @@ struct CodeGen {
15961595
TypeTableEntry *entry_u32;
15971596
TypeTableEntry *entry_u29;
15981597
TypeTableEntry *entry_u64;
1599-
TypeTableEntry *entry_u128;
16001598
TypeTableEntry *entry_i8;
1601-
TypeTableEntry *entry_i16;
16021599
TypeTableEntry *entry_i32;
16031600
TypeTableEntry *entry_i64;
1604-
TypeTableEntry *entry_i128;
16051601
TypeTableEntry *entry_isize;
16061602
TypeTableEntry *entry_usize;
16071603
TypeTableEntry *entry_f16;

‎src/analyze.cpp‎

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,9 +3227,8 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
32273227
}
32283228

32293229
{
3230-
auto entry = g->primitive_type_table.maybe_get(tld->name);
3231-
if (entry) {
3232-
TypeTableEntry *type = entry->value;
3230+
TypeTableEntry *type = get_primitive_type(g, tld->name);
3231+
if (type != nullptr) {
32333232
add_node_error(g, tld->source_node,
32343233
buf_sprintf("declaration shadows type '%s'", buf_ptr(&type->name)));
32353234
}
@@ -3474,9 +3473,8 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent
34743473
add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
34753474
variable_entry->value->type = g->builtin_types.entry_invalid;
34763475
} else {
3477-
auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
3478-
if (primitive_table_entry) {
3479-
TypeTableEntry *type = primitive_table_entry->value;
3476+
TypeTableEntry *type = get_primitive_type(g, name);
3477+
if (type != nullptr) {
34803478
add_node_error(g, source_node,
34813479
buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
34823480
variable_entry->value->type = g->builtin_types.entry_invalid;
@@ -4307,43 +4305,7 @@ void semantic_analyze(CodeGen *g) {
43074305
}
43084306
}
43094307

4310-
TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
4311-
size_t index;
4312-
if (size_in_bits == 2) {
4313-
index = 0;
4314-
} else if (size_in_bits == 3) {
4315-
index = 1;
4316-
} else if (size_in_bits == 4) {
4317-
index = 2;
4318-
} else if (size_in_bits == 5) {
4319-
index = 3;
4320-
} else if (size_in_bits == 6) {
4321-
index = 4;
4322-
} else if (size_in_bits == 7) {
4323-
index = 5;
4324-
} else if (size_in_bits == 8) {
4325-
index = 6;
4326-
} else if (size_in_bits == 16) {
4327-
index = 7;
4328-
} else if (size_in_bits == 29) {
4329-
index = 8;
4330-
} else if (size_in_bits == 32) {
4331-
index = 9;
4332-
} else if (size_in_bits == 64) {
4333-
index = 10;
4334-
} else if (size_in_bits == 128) {
4335-
index = 11;
4336-
} else {
4337-
return nullptr;
4338-
}
4339-
return &g->builtin_types.entry_int[is_signed ? 0 : 1][index];
4340-
}
4341-
43424308
TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
4343-
TypeTableEntry **common_entry = get_int_type_ptr(g, is_signed, size_in_bits);
4344-
if (common_entry)
4345-
return *common_entry;
4346-
43474309
TypeId type_id = {};
43484310
type_id.id = TypeTableEntryIdInt;
43494311
type_id.data.integer.is_signed = is_signed;
@@ -4953,6 +4915,8 @@ bool fn_eval_cacheable(Scope *scope, TypeTableEntry *return_type) {
49534915
while (scope) {
49544916
if (scope->id == ScopeIdVarDecl) {
49554917
ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
4918+
if (type_is_invalid(var_scope->var->value->type))
4919+
return false;
49564920
if (can_mutate_comptime_var_state(var_scope->var->value))
49574921
return false;
49584922
} else if (scope->id == ScopeIdFnDef) {
@@ -6310,3 +6274,28 @@ bool type_can_fail(TypeTableEntry *type_entry) {
63106274
bool fn_type_can_fail(FnTypeId *fn_type_id) {
63116275
return type_can_fail(fn_type_id->return_type) || fn_type_id->cc == CallingConventionAsync;
63126276
}
6277+
6278+
TypeTableEntry *get_primitive_type(CodeGen *g, Buf *name) {
6279+
if (buf_len(name) >= 2) {
6280+
uint8_t first_c = buf_ptr(name)[0];
6281+
if (first_c == 'i' || first_c == 'u') {
6282+
for (size_t i = 1; i < buf_len(name); i += 1) {
6283+
uint8_t c = buf_ptr(name)[i];
6284+
if (c < '0' || c > '9') {
6285+
goto not_integer;
6286+
}
6287+
}
6288+
bool is_signed = (first_c == 'i');
6289+
uint32_t bit_count = atoi(buf_ptr(name) + 1);
Code has comments. Press enter to view.
6290+
return get_int_type(g, is_signed, bit_count);
6291+
}
6292+
}
6293+
6294+
not_integer:
6295+
6296+
auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
6297+
if (primitive_table_entry != nullptr) {
6298+
return primitive_table_entry->value;
6299+
}
6300+
return nullptr;
6301+
}

‎src/analyze.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
1919
bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count);
2020
uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
2121
uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry);
22-
TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits);
2322
TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
2423
TypeTableEntry **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
2524
TypeTableEntry *get_c_int_type(CodeGen *g, CIntType c_int_type);
@@ -204,4 +203,6 @@ bool type_can_fail(TypeTableEntry *type_entry);
204203
bool fn_eval_cacheable(Scope *scope, TypeTableEntry *return_type);
205204
AstNode *type_decl_node(TypeTableEntry *type_entry);
206205

206+
TypeTableEntry *get_primitive_type(CodeGen *g, Buf *name);
207+
207208
#endif

‎src/codegen.cpp‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6161,16 +6161,6 @@ static void define_builtin_types(CodeGen *g) {
61616161
g->builtin_types.entry_arg_tuple = entry;
61626162
}
61636163

6164-
for (size_t int_size_i = 0; int_size_i < array_length(int_sizes_in_bits); int_size_i += 1) {
6165-
uint8_t size_in_bits = int_sizes_in_bits[int_size_i];
6166-
for (size_t is_sign_i = 0; is_sign_i < array_length(is_signed_list); is_sign_i += 1) {
6167-
bool is_signed = is_signed_list[is_sign_i];
6168-
TypeTableEntry *entry = make_int_type(g, is_signed, size_in_bits);
6169-
g->primitive_type_table.put(&entry->name, entry);
6170-
get_int_type_ptr(g, is_signed, size_in_bits)[0] = entry;
6171-
}
6172-
}
6173-
61746164
for (size_t i = 0; i < array_length(c_int_type_infos); i += 1) {
61756165
const CIntTypeInfo *info = &c_int_type_infos[i];
61766166
uint32_t size_in_bits = target_c_type_size_in_bits(&g->zig_target, info->id);
@@ -6286,12 +6276,9 @@ static void define_builtin_types(CodeGen *g) {
62866276
g->builtin_types.entry_u29 = get_int_type(g, false, 29);
62876277
g->builtin_types.entry_u32 = get_int_type(g, false, 32);
62886278
g->builtin_types.entry_u64 = get_int_type(g, false, 64);
6289-
g->builtin_types.entry_u128 = get_int_type(g, false, 128);
62906279
g->builtin_types.entry_i8 = get_int_type(g, true, 8);
6291-
g->builtin_types.entry_i16 = get_int_type(g, true, 16);
62926280
g->builtin_types.entry_i32 = get_int_type(g, true, 32);
62936281
g->builtin_types.entry_i64 = get_int_type(g, true, 64);
6294-
g->builtin_types.entry_i128 = get_int_type(g, true, 128);
62956282

62966283
{
62976284
g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void");

‎src/ir.cpp‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,9 +3217,8 @@ static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Sco
32173217
add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
32183218
variable_entry->value->type = codegen->builtin_types.entry_invalid;
32193219
} else {
3220-
auto primitive_table_entry = codegen->primitive_type_table.maybe_get(name);
3221-
if (primitive_table_entry) {
3222-
TypeTableEntry *type = primitive_table_entry->value;
3220+
TypeTableEntry *type = get_primitive_type(codegen, name);
3221+
if (type != nullptr) {
32233222
add_node_error(codegen, node,
32243223
buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
32253224
variable_entry->value->type = codegen->builtin_types.entry_invalid;
@@ -3661,9 +3660,9 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
36613660
return &const_instruction->base;
36623661
}
36633662

3664-
auto primitive_table_entry = irb->codegen->primitive_type_table.maybe_get(variable_name);
3665-
if (primitive_table_entry) {
3666-
IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value);
3663+
TypeTableEntry *primitive_type = get_primitive_type(irb->codegen, variable_name);
3664+
if (primitive_type != nullptr) {
3665+
IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_type);
36673666
if (lval == LValPtr) {
36683667
return ir_build_ref(irb, scope, node, value, false, false);
36693668
} else {
@@ -10691,11 +10690,11 @@ static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out
1069110690
return true;
1069210691
}
1069310692

10694-
static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out) {
10693+
static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *int_type, uint64_t *out) {
1069510694
if (type_is_invalid(value->value.type))
1069610695
return false;
1069710696

10698-
IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_usize);
10697+
IrInstruction *casted_value = ir_implicit_cast(ira, value, int_type);
1069910698
if (type_is_invalid(casted_value->value.type))
1070010699
return false;
1070110700

@@ -10707,6 +10706,10 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out
1070710706
return true;
1070810707
}
1070910708

10709+
static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out) {
10710+
return ir_resolve_unsigned(ira, value, ira->codegen->builtin_types.entry_usize, out);
10711+
}
10712+
1071010713
static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {
1071110714
if (type_is_invalid(value->value.type))
1071210715
return false;
@@ -18025,7 +18028,7 @@ static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruc
1802518028

1802618029
IrInstruction *bit_count_value = instruction->bit_count->other;
1802718030
uint64_t bit_count;
18028-
if (!ir_resolve_usize(ira, bit_count_value, &bit_count))
18031+
if (!ir_resolve_unsigned(ira, bit_count_value, ira->codegen->builtin_types.entry_u32, &bit_count))
1802918032
return ira->codegen->builtin_types.entry_invalid;
1803018033

1803118034
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);

‎src/translate_c.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ static AstNode *get_global(Context *c, Buf *name) {
427427
if (entry)
428428
return entry->value;
429429
}
430-
if (c->codegen->primitive_type_table.maybe_get(name) != nullptr) {
430+
if (get_primitive_type(c->codegen, name) != nullptr) {
431431
return trans_create_node_symbol(c, name);
432432
}
433433
return nullptr;

‎std/buffer.zig‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const Allocator = mem.Allocator;
55
const assert = debug.assert;
66
const ArrayList = std.ArrayList;
77

8-
const fmt = std.fmt;
9-
108
/// A buffer that allocates memory and maintains a null byte at the end.
119
pub const Buffer = struct {
1210
list: ArrayList(u8),

‎std/crypto/sha1.zig‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const endian = @import("../endian.zig");
44
const debug = @import("../debug/index.zig");
55
const builtin = @import("builtin");
66

7-
pub const u160 = @IntType(false, 160);
8-
97
const RoundParam = struct {
108
a: usize,
119
b: usize,

‎std/json.zig‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const std = @import("index.zig");
66
const debug = std.debug;
77
const mem = std.mem;
88

9-
const u1 = @IntType(false, 1);
10-
const u256 = @IntType(false, 256);
11-
129
// A single token slice into the parent string.
1310
//
1411
// Use `token.slice()` on the input at the current position to get the current slice.

‎std/math/big/int.zig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,6 @@ pub const Int = struct {
996996
// They will still run on larger than this and should pass, but the multi-limb code-paths
997997
// may be untested in some cases.
998998

999-
const u256 = @IntType(false, 256);
1000999
const al = debug.global_allocator;
10011000

10021001
test "big.int comptime_int set" {

‎std/math/exp2.zig‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ fn exp2_32(x: f32) f32 {
7575
}
7676

7777
var uf = x + redux;
78-
var i0 = @bitCast(u32, uf);
79-
i0 += tblsiz / 2;
78+
var i_0 = @bitCast(u32, uf);
79+
i_0 += tblsiz / 2;
8080

81-
const k = i0 / tblsiz;
81+
const k = i_0 / tblsiz;
8282
// NOTE: musl relies on undefined overflow shift behaviour. Appears that this produces the
8383
// intended result but should confirm how GCC/Clang handle this to ensure.
8484
const uk = @bitCast(f64, u64(0x3FF + k) << 52);
85-
i0 &= tblsiz - 1;
85+
i_0 &= tblsiz - 1;
8686
uf -= redux;
8787

8888
const z: f64 = x - uf;
89-
var r: f64 = exp2ft[i0];
89+
var r: f64 = exp2ft[i_0];
9090
const t: f64 = r * z;
9191
r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
9292
return @floatCast(f32, r * uk);
@@ -401,18 +401,18 @@ fn exp2_64(x: f64) f64 {
401401
// reduce x
402402
var uf = x + redux;
403403
// NOTE: musl performs an implicit 64-bit to 32-bit u32 truncation here
404-
var i0 = @truncate(u32, @bitCast(u64, uf));
405-
i0 += tblsiz / 2;
404+
var i_0 = @truncate(u32, @bitCast(u64, uf));
405+
i_0 += tblsiz / 2;
406406

407-
const k: u32 = i0 / tblsiz * tblsiz;
407+
const k: u32 = i_0 / tblsiz * tblsiz;
408408
const ik = @bitCast(i32, k / tblsiz);
409-
i0 %= tblsiz;
409+
i_0 %= tblsiz;
410410
uf -= redux;
411411

412-
// r = exp2(y) = exp2t[i0] * p(z - eps[i])
412+
// r = exp2(y) = exp2t[i_0] * p(z - eps[i])
413413
var z = x - uf;
414-
const t = exp2dt[2 * i0];
415-
z -= exp2dt[2 * i0 + 1];
414+
const t = exp2dt[2 * i_0];
415+
z -= exp2dt[2 * i_0 + 1];
416416
const r = t + t * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * P5))));
417417

418418
return math.scalbn(r, ik);

‎std/math/index.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ test "math.rotl" {
354354

355355
pub fn Log2Int(comptime T: type) type {
356356
// comptime ceil log2
357-
comptime var count: usize = 0;
357+
comptime var count = 0;
358358
comptime var s = T.bit_count - 1;
359359
inline while (s != 0) : (s >>= 1) {
360360
count += 1;

‎std/os/time.zig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub fn sleep(seconds: usize, nanoseconds: usize) void {
2525
}
2626
}
2727

28-
const u63 = @IntType(false, 63);
2928
pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
3029
var req = posix.timespec{
3130
.tv_sec = seconds,

‎test/cases/misc.zig‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ test "floating point primitive bit counts" {
5858
assert(f64.bit_count == 64);
5959
}
6060

61-
const u1 = @IntType(false, 1);
62-
const u63 = @IntType(false, 63);
63-
const i1 = @IntType(true, 1);
64-
const i63 = @IntType(true, 63);
65-
6661
test "@minValue and @maxValue" {
6762
assert(@maxValue(u1) == 1);
6863
assert(@maxValue(u8) == 255);

‎test/cases/struct.zig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ fn getC(data: *const BitField1) u2 {
240240
return data.c;
241241
}
242242

243-
const u24 = @IntType(false, 24);
244243
const Foo24Bits = packed struct {
245244
field: u24,
246245
};

‎test/compile_errors.zig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const tests = @import("tests.zig");
22

33
pub fn addCases(cases: *tests.CompileErrorContext) void {
4+
cases.add(
5+
"optional pointer to void in extern struct",
6+
\\comptime {
7+
\\ _ = @IntType(false, @maxValue(u32) + 1);
8+
\\}
9+
,
10+
".tmp_source.zig:2:40: error: integer value 4294967296 cannot be implicitly casted to type 'u32'",
11+
);
12+
413
cases.add(
514
"optional pointer to void in extern struct",
615
\\const Foo = extern struct {

0 commit comments

Comments
 (0)
Please sign in to comment.