Skip to content

Commit 576320e

Browse files
authored
Merge pull request #4025 from ziglang/Vexu-stage-2-cimport
Use self hosted translate-c for cImport
2 parents 1b64a5f + 88c5e2a commit 576320e

27 files changed

+1388
-7943
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ set(ZIG_SOURCES
448448
"${CMAKE_SOURCE_DIR}/src/bigfloat.cpp"
449449
"${CMAKE_SOURCE_DIR}/src/bigint.cpp"
450450
"${CMAKE_SOURCE_DIR}/src/buffer.cpp"
451-
"${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"
452451
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
453452
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
454453
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
@@ -465,7 +464,6 @@ set(ZIG_SOURCES
465464
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
466465
"${CMAKE_SOURCE_DIR}/src/target.cpp"
467466
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
468-
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
469467
"${CMAKE_SOURCE_DIR}/src/util.cpp"
470468
"${ZIG_SOURCES_MEM_PROFILE}"
471469
)

lib/std/crypto/chacha20.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ test "crypto.chacha20 test vector sunscreen" {
224224
// Chacha20 is self-reversing.
225225
var plaintext: [114]u8 = undefined;
226226
chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce);
227-
testing.expect(mem.compare(u8, input, &plaintext) == mem.Compare.Equal);
227+
testing.expect(mem.order(u8, input, &plaintext) == .eq);
228228
}
229229

230230
// https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7

lib/std/http/headers.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ const HeaderEntry = struct {
7070
}
7171

7272
// Sort lexicographically on header name
73-
return mem.compare(u8, a.name, b.name) == mem.Compare.LessThan;
73+
return mem.order(u8, a.name, b.name) == .lt;
7474
}
7575

7676
// Sort lexicographically on header value
7777
if (!mem.eql(u8, a.value, b.value)) {
78-
return mem.compare(u8, a.value, b.value) == mem.Compare.LessThan;
78+
return mem.order(u8, a.value, b.value) == .lt;
7979
}
8080

8181
// Doesn't matter here; need to pick something for sort consistency

lib/std/math.zig

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,6 @@ test "minInt and maxInt" {
926926
}
927927

928928
test "max value type" {
929-
// If the type of maxInt(i32) was i32 then this implicit cast to
930-
// u32 would not work. But since the value is a number literal,
931-
// it works fine.
932929
const x: u32 = maxInt(i32);
933930
testing.expect(x == 2147483647);
934931
}
@@ -944,7 +941,32 @@ test "math.mulWide" {
944941
testing.expect(mulWide(u8, 100, 100) == 10000);
945942
}
946943

947-
/// Not to be confused with `std.mem.Compare`.
944+
/// See also `CompareOperator`.
945+
pub const Order = enum {
946+
/// Less than (`<`)
947+
lt,
948+
949+
/// Equal (`==`)
950+
eq,
951+
952+
/// Greater than (`>`)
953+
gt,
954+
};
955+
956+
/// Given two numbers, this function returns the order they are with respect to each other.
957+
pub fn order(a: var, b: var) Order {
958+
if (a == b) {
959+
return .eq;
960+
} else if (a < b) {
961+
return .lt;
962+
} else if (a > b) {
963+
return .gt;
964+
} else {
965+
unreachable;
966+
}
967+
}
968+
969+
/// See also `Order`.
948970
pub const CompareOperator = enum {
949971
/// Less than (`<`)
950972
lt,
@@ -979,7 +1001,7 @@ pub fn compare(a: var, op: CompareOperator, b: var) bool {
9791001
};
9801002
}
9811003

982-
test "math.lt, et al < <= > >= between signed and unsigned" {
1004+
test "compare between signed and unsigned" {
9831005
testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
9841006
testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
9851007
testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));

lib/std/mem.zig

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,6 @@ pub const Allocator = struct {
239239
}
240240
};
241241

242-
pub const Compare = enum {
243-
LessThan,
244-
Equal,
245-
GreaterThan,
246-
};
247-
248242
/// Copy all of source into dest at position 0.
249243
/// dest.len must be >= source.len.
250244
/// dest.ptr must be <= src.ptr.
@@ -297,46 +291,30 @@ test "mem.secureZero" {
297291
testing.expectEqualSlices(u8, a[0..], b[0..]);
298292
}
299293

300-
pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare {
294+
pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
301295
const n = math.min(lhs.len, rhs.len);
302296
var i: usize = 0;
303297
while (i < n) : (i += 1) {
304-
if (lhs[i] == rhs[i]) {
305-
continue;
306-
} else if (lhs[i] < rhs[i]) {
307-
return Compare.LessThan;
308-
} else if (lhs[i] > rhs[i]) {
309-
return Compare.GreaterThan;
310-
} else {
311-
unreachable;
298+
switch (math.order(lhs[i], rhs[i])) {
299+
.eq => continue,
300+
.lt => return .lt,
301+
.gt => return .gt,
312302
}
313303
}
314-
315-
if (lhs.len == rhs.len) {
316-
return Compare.Equal;
317-
} else if (lhs.len < rhs.len) {
318-
return Compare.LessThan;
319-
} else if (lhs.len > rhs.len) {
320-
return Compare.GreaterThan;
321-
}
322-
unreachable;
304+
return math.order(lhs.len, rhs.len);
323305
}
324306

325-
test "mem.compare" {
326-
testing.expect(compare(u8, "abcd", "bee") == Compare.LessThan);
327-
testing.expect(compare(u8, "abc", "abc") == Compare.Equal);
328-
testing.expect(compare(u8, "abc", "abc0") == Compare.LessThan);
329-
testing.expect(compare(u8, "", "") == Compare.Equal);
330-
testing.expect(compare(u8, "", "a") == Compare.LessThan);
307+
test "order" {
308+
testing.expect(order(u8, "abcd", "bee") == .lt);
309+
testing.expect(order(u8, "abc", "abc") == .eq);
310+
testing.expect(order(u8, "abc", "abc0") == .lt);
311+
testing.expect(order(u8, "", "") == .eq);
312+
testing.expect(order(u8, "", "a") == .lt);
331313
}
332314

333315
/// Returns true if lhs < rhs, false otherwise
334316
pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
335-
var result = compare(T, lhs, rhs);
336-
if (result == Compare.LessThan) {
337-
return true;
338-
} else
339-
return false;
317+
return order(T, lhs, rhs) == .lt;
340318
}
341319

342320
test "mem.lessThan" {

lib/std/rb.zig

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std.zig");
22
const assert = std.debug.assert;
33
const testing = std.testing;
4-
const mem = std.mem; // For mem.Compare
4+
const Order = std.math.Order;
55

66
const Color = enum(u1) {
77
Black,
@@ -132,7 +132,7 @@ pub const Node = struct {
132132

133133
pub const Tree = struct {
134134
root: ?*Node,
135-
compareFn: fn (*Node, *Node) mem.Compare,
135+
compareFn: fn (*Node, *Node) Order,
136136

137137
/// If you have a need for a version that caches this, please file a bug.
138138
pub fn first(tree: *Tree) ?*Node {
@@ -389,7 +389,7 @@ pub const Tree = struct {
389389
var new = newconst;
390390

391391
// I assume this can get optimized out if the caller already knows.
392-
if (tree.compareFn(old, new) != mem.Compare.Equal) return ReplaceError.NotEqual;
392+
if (tree.compareFn(old, new) != .eq) return ReplaceError.NotEqual;
393393

394394
if (old.getParent()) |parent| {
395395
parent.setChild(new, parent.left == old);
@@ -404,7 +404,7 @@ pub const Tree = struct {
404404
new.* = old.*;
405405
}
406406

407-
pub fn init(tree: *Tree, f: fn (*Node, *Node) mem.Compare) void {
407+
pub fn init(tree: *Tree, f: fn (*Node, *Node) Order) void {
408408
tree.root = null;
409409
tree.compareFn = f;
410410
}
@@ -469,19 +469,21 @@ fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node {
469469
is_left.* = false;
470470

471471
while (maybe_node) |node| {
472-
var res: mem.Compare = tree.compareFn(node, key);
473-
if (res == mem.Compare.Equal) {
472+
const res = tree.compareFn(node, key);
473+
if (res == .eq) {
474474
return node;
475475
}
476476
pparent.* = node;
477-
if (res == mem.Compare.GreaterThan) {
478-
is_left.* = true;
479-
maybe_node = node.left;
480-
} else if (res == mem.Compare.LessThan) {
481-
is_left.* = false;
482-
maybe_node = node.right;
483-
} else {
484-
unreachable;
477+
switch (res) {
478+
.gt => {
479+
is_left.* = true;
480+
maybe_node = node.left;
481+
},
482+
.lt => {
483+
is_left.* = false;
484+
maybe_node = node.right;
485+
},
486+
.eq => unreachable, // handled above
485487
}
486488
}
487489
return null;
@@ -496,16 +498,16 @@ fn testGetNumber(node: *Node) *testNumber {
496498
return @fieldParentPtr(testNumber, "node", node);
497499
}
498500

499-
fn testCompare(l: *Node, r: *Node) mem.Compare {
501+
fn testCompare(l: *Node, r: *Node) Order {
500502
var left = testGetNumber(l);
501503
var right = testGetNumber(r);
502504

503505
if (left.value < right.value) {
504-
return mem.Compare.LessThan;
506+
return .lt;
505507
} else if (left.value == right.value) {
506-
return mem.Compare.Equal;
508+
return .eq;
507509
} else if (left.value > right.value) {
508-
return mem.Compare.GreaterThan;
510+
return .gt;
509511
}
510512
unreachable;
511513
}

0 commit comments

Comments
 (0)