From 623d5f442c832ec0ea2a07aba73b8e2eae57191c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 28 Mar 2021 23:12:26 -0700 Subject: stage2: guidance on how to implement switch expressions Here's what I think the ZIR should be. AstGen is not yet implemented to match this, and the main implementation of analyzeSwitch in Sema is not yet implemented to match it either. Here are some example byte size reductions from master branch, with the ZIR memory layout from this commit: ``` switch (foo) { a => 1, b => 2, c => 3, d => 4, } ``` 184 bytes (master) => 40 bytes (this branch) ``` switch (foo) { a, b => 1, c..d, e, f => 2, g => 3, else => 4, } ``` 240 bytes (master) => 80 bytes (this branch) --- src/Module.zig | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index c92df1aaae..3309a10b30 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1532,6 +1532,7 @@ pub const SrcLoc = struct { .node_offset_bin_op, .node_offset_bin_lhs, .node_offset_bin_rhs, + .node_offset_switch_operand, => src_loc.container.decl.container.file_scope, }; } @@ -1663,6 +1664,7 @@ pub const SrcLoc = struct { const token_starts = tree.tokens.items(.start); return token_starts[tok_index]; }, + .node_offset_switch_operand => @panic("TODO"), } } }; @@ -1795,6 +1797,11 @@ pub const LazySrcLoc = union(enum) { /// which points to a binary expression AST node. Next, nagivate to the RHS. /// The Decl is determined contextually. node_offset_bin_rhs: i32, + /// The source location points to the operand of a switch expression, found + /// by taking this AST node index offset from the containing Decl AST node, + /// which points to a switch expression AST node. Next, nagivate to the operand. + /// The Decl is determined contextually. + node_offset_switch_operand: i32, /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope. pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc { @@ -1828,6 +1835,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_bin_op, .node_offset_bin_lhs, .node_offset_bin_rhs, + .node_offset_switch_operand, => .{ .container = .{ .decl = scope.srcDecl().? }, .lazy = lazy, @@ -1867,6 +1875,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_bin_op, .node_offset_bin_lhs, .node_offset_bin_rhs, + .node_offset_switch_operand, => .{ .container = .{ .decl = decl }, .lazy = lazy, -- cgit v1.2.3