diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-01 22:46:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-01 22:46:46 -0500 |
| commit | 576320e6d5bf706a0ee03a3e9318c8ef2fd6e76f (patch) | |
| tree | 0405ebde5048781fd6f65d3bf9d444c54d04bd02 /lib/std | |
| parent | 1b64a5f5f0cece3cf0009410ddb13b5dd6f899e1 (diff) | |
| parent | 88c5e2a96e09270a2ec3045639e7cab3712f5291 (diff) | |
| download | zig-576320e6d5bf706a0ee03a3e9318c8ef2fd6e76f.tar.gz zig-576320e6d5bf706a0ee03a3e9318c8ef2fd6e76f.zip | |
Merge pull request #4025 from ziglang/Vexu-stage-2-cimport
Use self hosted translate-c for cImport
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/crypto/chacha20.zig | 2 | ||||
| -rw-r--r-- | lib/std/http/headers.zig | 4 | ||||
| -rw-r--r-- | lib/std/math.zig | 32 | ||||
| -rw-r--r-- | lib/std/mem.zig | 48 | ||||
| -rw-r--r-- | lib/std/rb.zig | 38 | ||||
| -rw-r--r-- | lib/std/zig/ast.zig | 95 | ||||
| -rw-r--r-- | lib/std/zig/render.zig | 2 |
7 files changed, 113 insertions, 108 deletions
diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig index 8a0f677660..d67877b051 100644 --- a/lib/std/crypto/chacha20.zig +++ b/lib/std/crypto/chacha20.zig @@ -224,7 +224,7 @@ test "crypto.chacha20 test vector sunscreen" { // Chacha20 is self-reversing. var plaintext: [114]u8 = undefined; chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce); - testing.expect(mem.compare(u8, input, &plaintext) == mem.Compare.Equal); + testing.expect(mem.order(u8, input, &plaintext) == .eq); } // https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7 diff --git a/lib/std/http/headers.zig b/lib/std/http/headers.zig index 588ee7f796..b1d047aeec 100644 --- a/lib/std/http/headers.zig +++ b/lib/std/http/headers.zig @@ -70,12 +70,12 @@ const HeaderEntry = struct { } // Sort lexicographically on header name - return mem.compare(u8, a.name, b.name) == mem.Compare.LessThan; + return mem.order(u8, a.name, b.name) == .lt; } // Sort lexicographically on header value if (!mem.eql(u8, a.value, b.value)) { - return mem.compare(u8, a.value, b.value) == mem.Compare.LessThan; + return mem.order(u8, a.value, b.value) == .lt; } // Doesn't matter here; need to pick something for sort consistency diff --git a/lib/std/math.zig b/lib/std/math.zig index a12bf5f4e9..051dc341a2 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -926,9 +926,6 @@ test "minInt and maxInt" { } test "max value type" { - // If the type of maxInt(i32) was i32 then this implicit cast to - // u32 would not work. But since the value is a number literal, - // it works fine. const x: u32 = maxInt(i32); testing.expect(x == 2147483647); } @@ -944,7 +941,32 @@ test "math.mulWide" { testing.expect(mulWide(u8, 100, 100) == 10000); } -/// Not to be confused with `std.mem.Compare`. +/// See also `CompareOperator`. +pub const Order = enum { + /// Less than (`<`) + lt, + + /// Equal (`==`) + eq, + + /// Greater than (`>`) + gt, +}; + +/// Given two numbers, this function returns the order they are with respect to each other. +pub fn order(a: var, b: var) Order { + if (a == b) { + return .eq; + } else if (a < b) { + return .lt; + } else if (a > b) { + return .gt; + } else { + unreachable; + } +} + +/// See also `Order`. pub const CompareOperator = enum { /// Less than (`<`) lt, @@ -979,7 +1001,7 @@ pub fn compare(a: var, op: CompareOperator, b: var) bool { }; } -test "math.lt, et al < <= > >= between signed and unsigned" { +test "compare between signed and unsigned" { testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255))); testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1))); testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255))); diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 133e3b8a2d..48f4a68a5e 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -239,12 +239,6 @@ pub const Allocator = struct { } }; -pub const Compare = enum { - LessThan, - Equal, - GreaterThan, -}; - /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// dest.ptr must be <= src.ptr. @@ -297,46 +291,30 @@ test "mem.secureZero" { testing.expectEqualSlices(u8, a[0..], b[0..]); } -pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare { +pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { const n = math.min(lhs.len, rhs.len); var i: usize = 0; while (i < n) : (i += 1) { - if (lhs[i] == rhs[i]) { - continue; - } else if (lhs[i] < rhs[i]) { - return Compare.LessThan; - } else if (lhs[i] > rhs[i]) { - return Compare.GreaterThan; - } else { - unreachable; + switch (math.order(lhs[i], rhs[i])) { + .eq => continue, + .lt => return .lt, + .gt => return .gt, } } - - if (lhs.len == rhs.len) { - return Compare.Equal; - } else if (lhs.len < rhs.len) { - return Compare.LessThan; - } else if (lhs.len > rhs.len) { - return Compare.GreaterThan; - } - unreachable; + return math.order(lhs.len, rhs.len); } -test "mem.compare" { - testing.expect(compare(u8, "abcd", "bee") == Compare.LessThan); - testing.expect(compare(u8, "abc", "abc") == Compare.Equal); - testing.expect(compare(u8, "abc", "abc0") == Compare.LessThan); - testing.expect(compare(u8, "", "") == Compare.Equal); - testing.expect(compare(u8, "", "a") == Compare.LessThan); +test "order" { + testing.expect(order(u8, "abcd", "bee") == .lt); + testing.expect(order(u8, "abc", "abc") == .eq); + testing.expect(order(u8, "abc", "abc0") == .lt); + testing.expect(order(u8, "", "") == .eq); + testing.expect(order(u8, "", "a") == .lt); } /// Returns true if lhs < rhs, false otherwise pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool { - var result = compare(T, lhs, rhs); - if (result == Compare.LessThan) { - return true; - } else - return false; + return order(T, lhs, rhs) == .lt; } test "mem.lessThan" { diff --git a/lib/std/rb.zig b/lib/std/rb.zig index 4180c7459c..c41a269a27 100644 --- a/lib/std/rb.zig +++ b/lib/std/rb.zig @@ -1,7 +1,7 @@ const std = @import("std.zig"); const assert = std.debug.assert; const testing = std.testing; -const mem = std.mem; // For mem.Compare +const Order = std.math.Order; const Color = enum(u1) { Black, @@ -132,7 +132,7 @@ pub const Node = struct { pub const Tree = struct { root: ?*Node, - compareFn: fn (*Node, *Node) mem.Compare, + compareFn: fn (*Node, *Node) Order, /// If you have a need for a version that caches this, please file a bug. pub fn first(tree: *Tree) ?*Node { @@ -389,7 +389,7 @@ pub const Tree = struct { var new = newconst; // I assume this can get optimized out if the caller already knows. - if (tree.compareFn(old, new) != mem.Compare.Equal) return ReplaceError.NotEqual; + if (tree.compareFn(old, new) != .eq) return ReplaceError.NotEqual; if (old.getParent()) |parent| { parent.setChild(new, parent.left == old); @@ -404,7 +404,7 @@ pub const Tree = struct { new.* = old.*; } - pub fn init(tree: *Tree, f: fn (*Node, *Node) mem.Compare) void { + pub fn init(tree: *Tree, f: fn (*Node, *Node) Order) void { tree.root = null; tree.compareFn = f; } @@ -469,19 +469,21 @@ fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node { is_left.* = false; while (maybe_node) |node| { - var res: mem.Compare = tree.compareFn(node, key); - if (res == mem.Compare.Equal) { + const res = tree.compareFn(node, key); + if (res == .eq) { return node; } pparent.* = node; - if (res == mem.Compare.GreaterThan) { - is_left.* = true; - maybe_node = node.left; - } else if (res == mem.Compare.LessThan) { - is_left.* = false; - maybe_node = node.right; - } else { - unreachable; + switch (res) { + .gt => { + is_left.* = true; + maybe_node = node.left; + }, + .lt => { + is_left.* = false; + maybe_node = node.right; + }, + .eq => unreachable, // handled above } } return null; @@ -496,16 +498,16 @@ fn testGetNumber(node: *Node) *testNumber { return @fieldParentPtr(testNumber, "node", node); } -fn testCompare(l: *Node, r: *Node) mem.Compare { +fn testCompare(l: *Node, r: *Node) Order { var left = testGetNumber(l); var right = testGetNumber(r); if (left.value < right.value) { - return mem.Compare.LessThan; + return .lt; } else if (left.value == right.value) { - return mem.Compare.Equal; + return .eq; } else if (left.value > right.value) { - return mem.Compare.GreaterThan; + return .gt; } unreachable; } diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 349ae914c4..adbcef165e 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -13,6 +13,7 @@ pub const Tree = struct { root_node: *Node.Root, arena_allocator: std.heap.ArenaAllocator, errors: ErrorList, + generated: bool = false, pub const TokenList = SegmentedList(Token, 64); pub const ErrorList = SegmentedList(Error, 0); @@ -58,6 +59,8 @@ pub const Tree = struct { .line_start = start_index, .line_end = self.source.len, }; + if (self.generated) + return loc; const token_start = token.start; for (self.source[start_index..]) |c, i| { if (i + start_index == token_start) { @@ -581,7 +584,7 @@ pub const Node = struct { } pub const Root = struct { - base: Node = Node {.id = .Root}, + base: Node = Node{ .id = .Root }, decls: DeclList, eof_token: TokenIndex, @@ -604,7 +607,7 @@ pub const Node = struct { }; pub const VarDecl = struct { - base: Node = Node {.id = .VarDecl}, + base: Node = Node{ .id = .VarDecl }, doc_comments: ?*DocComment, visib_token: ?TokenIndex, thread_local_token: ?TokenIndex, @@ -661,7 +664,7 @@ pub const Node = struct { }; pub const Use = struct { - base: Node = Node {.id = .Use}, + base: Node = Node{ .id = .Use }, doc_comments: ?*DocComment, visib_token: ?TokenIndex, use_token: TokenIndex, @@ -688,7 +691,7 @@ pub const Node = struct { }; pub const ErrorSetDecl = struct { - base: Node = Node {.id = .ErrorSetDecl}, + base: Node = Node{ .id = .ErrorSetDecl }, error_token: TokenIndex, decls: DeclList, rbrace_token: TokenIndex, @@ -714,7 +717,7 @@ pub const Node = struct { }; pub const ContainerDecl = struct { - base: Node = Node {.id = .ContainerDecl}, + base: Node = Node{ .id = .ContainerDecl }, layout_token: ?TokenIndex, kind_token: TokenIndex, init_arg_expr: InitArg, @@ -801,7 +804,7 @@ pub const Node = struct { }; pub const ErrorTag = struct { - base: Node = Node {.id = .ErrorTag}, + base: Node = Node{ .id = .ErrorTag }, doc_comments: ?*DocComment, name_token: TokenIndex, @@ -826,7 +829,7 @@ pub const Node = struct { }; pub const Identifier = struct { - base: Node = Node {.id = .Identifier}, + base: Node = Node{ .id = .Identifier }, token: TokenIndex, pub fn iterate(self: *Identifier, index: usize) ?*Node { @@ -843,7 +846,7 @@ pub const Node = struct { }; pub const FnProto = struct { - base: Node = Node {.id = .FnProto}, + base: Node = Node{ .id = .FnProto }, doc_comments: ?*DocComment, visib_token: ?TokenIndex, fn_token: TokenIndex, @@ -925,7 +928,7 @@ pub const Node = struct { }; pub const AnyFrameType = struct { - base: Node = Node {.id = .AnyFrameType}, + base: Node = Node{ .id = .AnyFrameType }, anyframe_token: TokenIndex, result: ?Result, @@ -956,7 +959,7 @@ pub const Node = struct { }; pub const ParamDecl = struct { - base: Node = Node {.id = .ParamDecl}, + base: Node = Node{ .id = .ParamDecl }, doc_comments: ?*DocComment, comptime_token: ?TokenIndex, noalias_token: ?TokenIndex, @@ -989,7 +992,7 @@ pub const Node = struct { }; pub const Block = struct { - base: Node = Node {.id = .Block}, + base: Node = Node{ .id = .Block }, label: ?TokenIndex, lbrace: TokenIndex, statements: StatementList, @@ -1020,7 +1023,7 @@ pub const Node = struct { }; pub const Defer = struct { - base: Node = Node {.id = .Defer}, + base: Node = Node{ .id = .Defer }, defer_token: TokenIndex, expr: *Node, @@ -1043,7 +1046,7 @@ pub const Node = struct { }; pub const Comptime = struct { - base: Node = Node {.id = .Comptime}, + base: Node = Node{ .id = .Comptime }, doc_comments: ?*DocComment, comptime_token: TokenIndex, expr: *Node, @@ -1067,7 +1070,7 @@ pub const Node = struct { }; pub const Payload = struct { - base: Node = Node {.id = .Payload}, + base: Node = Node{ .id = .Payload }, lpipe: TokenIndex, error_symbol: *Node, rpipe: TokenIndex, @@ -1091,7 +1094,7 @@ pub const Node = struct { }; pub const PointerPayload = struct { - base: Node = Node {.id = .PointerPayload}, + base: Node = Node{ .id = .PointerPayload }, lpipe: TokenIndex, ptr_token: ?TokenIndex, value_symbol: *Node, @@ -1116,7 +1119,7 @@ pub const Node = struct { }; pub const PointerIndexPayload = struct { - base: Node = Node {.id = .PointerIndexPayload}, + base: Node = Node{ .id = .PointerIndexPayload }, lpipe: TokenIndex, ptr_token: ?TokenIndex, value_symbol: *Node, @@ -1147,7 +1150,7 @@ pub const Node = struct { }; pub const Else = struct { - base: Node = Node {.id = .Else}, + base: Node = Node{ .id = .Else }, else_token: TokenIndex, payload: ?*Node, body: *Node, @@ -1176,7 +1179,7 @@ pub const Node = struct { }; pub const Switch = struct { - base: Node = Node {.id = .Switch}, + base: Node = Node{ .id = .Switch }, switch_token: TokenIndex, expr: *Node, @@ -1208,7 +1211,7 @@ pub const Node = struct { }; pub const SwitchCase = struct { - base: Node = Node {.id = .SwitchCase}, + base: Node = Node{ .id = .SwitchCase }, items: ItemList, arrow_token: TokenIndex, payload: ?*Node, @@ -1243,7 +1246,7 @@ pub const Node = struct { }; pub const SwitchElse = struct { - base: Node = Node {.id = .SwitchElse}, + base: Node = Node{ .id = .SwitchElse }, token: TokenIndex, pub fn iterate(self: *SwitchElse, index: usize) ?*Node { @@ -1260,7 +1263,7 @@ pub const Node = struct { }; pub const While = struct { - base: Node = Node {.id = .While}, + base: Node = Node{ .id = .While }, label: ?TokenIndex, inline_token: ?TokenIndex, while_token: TokenIndex, @@ -1319,7 +1322,7 @@ pub const Node = struct { }; pub const For = struct { - base: Node = Node {.id = .For}, + base: Node = Node{ .id = .For }, label: ?TokenIndex, inline_token: ?TokenIndex, for_token: TokenIndex, @@ -1370,7 +1373,7 @@ pub const Node = struct { }; pub const If = struct { - base: Node = Node {.id = .If}, + base: Node = Node{ .id = .If }, if_token: TokenIndex, condition: *Node, payload: ?*Node, @@ -1413,7 +1416,7 @@ pub const Node = struct { }; pub const InfixOp = struct { - base: Node = Node {.id = .InfixOp}, + base: Node = Node{ .id = .InfixOp }, op_token: TokenIndex, lhs: *Node, op: Op, @@ -1646,7 +1649,7 @@ pub const Node = struct { }; pub const FieldInitializer = struct { - base: Node = Node {.id = .FieldInitializer}, + base: Node = Node{ .id = .FieldInitializer }, period_token: TokenIndex, name_token: TokenIndex, expr: *Node, @@ -1670,7 +1673,7 @@ pub const Node = struct { }; pub const SuffixOp = struct { - base: Node = Node {.id = .SuffixOp}, + base: Node = Node{ .id = .SuffixOp }, lhs: Lhs, op: Op, rtoken: TokenIndex, @@ -1771,7 +1774,7 @@ pub const Node = struct { }; pub const GroupedExpression = struct { - base: Node = Node {.id = .GroupedExpression}, + base: Node = Node{ .id = .GroupedExpression }, lparen: TokenIndex, expr: *Node, rparen: TokenIndex, @@ -1795,7 +1798,7 @@ pub const Node = struct { }; pub const ControlFlowExpression = struct { - base: Node = Node {.id = .ControlFlowExpression}, + base: Node = Node{ .id = .ControlFlowExpression }, ltoken: TokenIndex, kind: Kind, rhs: ?*Node, @@ -1861,7 +1864,7 @@ pub const Node = struct { }; pub const Suspend = struct { - base: Node = Node {.id = .Suspend}, + base: Node = Node{ .id = .Suspend }, suspend_token: TokenIndex, body: ?*Node, @@ -1890,7 +1893,7 @@ pub const Node = struct { }; pub const IntegerLiteral = struct { - base: Node = Node {.id = .IntegerLiteral}, + base: Node = Node{ .id = .IntegerLiteral }, token: TokenIndex, pub fn iterate(self: *IntegerLiteral, index: usize) ?*Node { @@ -1907,7 +1910,7 @@ pub const Node = struct { }; pub const EnumLiteral = struct { - base: Node = Node {.id = .EnumLiteral}, + base: Node = Node{ .id = .EnumLiteral }, dot: TokenIndex, name: TokenIndex, @@ -1925,7 +1928,7 @@ pub const Node = struct { }; pub const FloatLiteral = struct { - base: Node = Node {.id = .FloatLiteral}, + base: Node = Node{ .id = .FloatLiteral }, token: TokenIndex, pub fn iterate(self: *FloatLiteral, index: usize) ?*Node { @@ -1942,7 +1945,7 @@ pub const Node = struct { }; pub const BuiltinCall = struct { - base: Node = Node {.id = .BuiltinCall}, + base: Node = Node{ .id = .BuiltinCall }, builtin_token: TokenIndex, params: ParamList, rparen_token: TokenIndex, @@ -1968,7 +1971,7 @@ pub const Node = struct { }; pub const StringLiteral = struct { - base: Node = Node {.id = .StringLiteral}, + base: Node = Node{ .id = .StringLiteral }, token: TokenIndex, pub fn iterate(self: *StringLiteral, index: usize) ?*Node { @@ -1985,7 +1988,7 @@ pub const Node = struct { }; pub const MultilineStringLiteral = struct { - base: Node = Node {.id = .MultilineStringLiteral}, + base: Node = Node{ .id = .MultilineStringLiteral }, lines: LineList, pub const LineList = SegmentedList(TokenIndex, 4); @@ -2004,7 +2007,7 @@ pub const Node = struct { }; pub const CharLiteral = struct { - base: Node = Node {.id = .CharLiteral}, + base: Node = Node{ .id = .CharLiteral }, token: TokenIndex, pub fn iterate(self: *CharLiteral, index: usize) ?*Node { @@ -2021,7 +2024,7 @@ pub const Node = struct { }; pub const BoolLiteral = struct { - base: Node = Node {.id = .BoolLiteral}, + base: Node = Node{ .id = .BoolLiteral }, token: TokenIndex, pub fn iterate(self: *BoolLiteral, index: usize) ?*Node { @@ -2038,7 +2041,7 @@ pub const Node = struct { }; pub const NullLiteral = struct { - base: Node = Node {.id = .NullLiteral}, + base: Node = Node{ .id = .NullLiteral }, token: TokenIndex, pub fn iterate(self: *NullLiteral, index: usize) ?*Node { @@ -2055,7 +2058,7 @@ pub const Node = struct { }; pub const UndefinedLiteral = struct { - base: Node = Node {.id = .UndefinedLiteral}, + base: Node = Node{ .id = .UndefinedLiteral }, token: TokenIndex, pub fn iterate(self: *UndefinedLiteral, index: usize) ?*Node { @@ -2072,7 +2075,7 @@ pub const Node = struct { }; pub const AsmOutput = struct { - base: Node = Node {.id = .AsmOutput}, + base: Node = Node{ .id = .AsmOutput }, lbracket: TokenIndex, symbolic_name: *Node, constraint: *Node, @@ -2117,7 +2120,7 @@ pub const Node = struct { }; pub const AsmInput = struct { - base: Node = Node {.id = .AsmInput}, + base: Node = Node{ .id = .AsmInput }, lbracket: TokenIndex, symbolic_name: *Node, constraint: *Node, @@ -2149,7 +2152,7 @@ pub const Node = struct { }; pub const Asm = struct { - base: Node = Node {.id = .Asm}, + base: Node = Node{ .id = .Asm }, asm_token: TokenIndex, volatile_token: ?TokenIndex, template: *Node, @@ -2184,7 +2187,7 @@ pub const Node = struct { }; pub const Unreachable = struct { - base: Node = Node {.id = .Unreachable}, + base: Node = Node{ .id = .Unreachable }, token: TokenIndex, pub fn iterate(self: *Unreachable, index: usize) ?*Node { @@ -2201,7 +2204,7 @@ pub const Node = struct { }; pub const ErrorType = struct { - base: Node = Node {.id = .ErrorType}, + base: Node = Node{ .id = .ErrorType }, token: TokenIndex, pub fn iterate(self: *ErrorType, index: usize) ?*Node { @@ -2235,7 +2238,7 @@ pub const Node = struct { }; pub const DocComment = struct { - base: Node = Node {.id = .DocComment}, + base: Node = Node{ .id = .DocComment }, lines: LineList, pub const LineList = SegmentedList(TokenIndex, 4); @@ -2254,7 +2257,7 @@ pub const Node = struct { }; pub const TestDecl = struct { - base: Node = Node {.id = .TestDecl}, + base: Node = Node{ .id = .TestDecl }, doc_comments: ?*DocComment, test_token: TokenIndex, name: *Node, diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index d65d9bfc57..6e1ee4a601 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -643,7 +643,7 @@ fn renderExpression( }, .ArrayAccess => |index_expr| { - const lbracket = tree.prevToken(index_expr.firstToken()); + const lbracket = tree.nextToken(suffix_op.lhs.node.lastToken()); const rbracket = tree.nextToken(index_expr.lastToken()); try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs.node, Space.None); |
