@@ -2671,12 +2671,23 @@ pub const SrcLoc = struct {
2671
2671
}
2672
2672
} else unreachable ;
2673
2673
},
2674
- .node_offset_switch_prong_capture = > | node_off | {
2674
+ .node_offset_switch_prong_capture ,
2675
+ .node_offset_switch_prong_tag_capture ,
2676
+ = > | node_off | {
2675
2677
const tree = try src_loc .file_scope .getTree (gpa );
2676
2678
const case_node = src_loc .declRelativeToNodeIndex (node_off );
2677
2679
const case = tree .fullSwitchCase (case_node ).? ;
2678
- const start_tok = case .payload_token .? ;
2679
2680
const token_tags = tree .tokens .items (.tag );
2681
+ const start_tok = switch (src_loc .lazy ) {
2682
+ .node_offset_switch_prong_capture = > case .payload_token .? ,
2683
+ .node_offset_switch_prong_tag_capture = > blk : {
2684
+ var tok = case .payload_token .? ;
2685
+ if (token_tags [tok ] == .asterisk ) tok += 1 ;
2686
+ tok += 2 ; // skip over comma
2687
+ break :blk tok ;
2688
+ },
2689
+ else = > unreachable ,
2690
+ };
2680
2691
const end_tok = switch (token_tags [start_tok ]) {
2681
2692
.asterisk = > start_tok + 1 ,
2682
2693
else = > start_tok ,
@@ -3157,6 +3168,9 @@ pub const LazySrcLoc = union(enum) {
3157
3168
/// The source location points to the capture of a switch_prong.
3158
3169
/// The Decl is determined contextually.
3159
3170
node_offset_switch_prong_capture : i32 ,
3171
+ /// The source location points to the tag capture of a switch_prong.
3172
+ /// The Decl is determined contextually.
3173
+ node_offset_switch_prong_tag_capture : i32 ,
3160
3174
/// The source location points to the align expr of a function type
3161
3175
/// expression, found by taking this AST node index offset from the containing
3162
3176
/// Decl AST node, which points to a function type AST node. Next, navigate to
@@ -3330,6 +3344,7 @@ pub const LazySrcLoc = union(enum) {
3330
3344
.node_offset_switch_special_prong ,
3331
3345
.node_offset_switch_range ,
3332
3346
.node_offset_switch_prong_capture ,
3347
+ .node_offset_switch_prong_tag_capture ,
3333
3348
.node_offset_fn_type_align ,
3334
3349
.node_offset_fn_type_addrspace ,
3335
3350
.node_offset_fn_type_section ,
@@ -6009,11 +6024,26 @@ fn lockAndClearFileCompileError(mod: *Module, file: *File) void {
6009
6024
}
6010
6025
6011
6026
pub const SwitchProngSrc = union (enum ) {
6027
+ /// The item for a scalar prong.
6012
6028
scalar : u32 ,
6029
+ /// A given single item for a multi prong.
6013
6030
multi : Multi ,
6031
+ /// A given range item for a multi prong.
6014
6032
range : Multi ,
6015
- multi_capture : u32 ,
6033
+ /// The item for the special prong.
6016
6034
special ,
6035
+ /// The main capture for a scalar prong.
6036
+ scalar_capture : u32 ,
6037
+ /// The main capture for a multi prong.
6038
+ multi_capture : u32 ,
6039
+ /// The main capture for the special prong.
6040
+ special_capture ,
6041
+ /// The tag capture for a scalar prong.
6042
+ scalar_tag_capture : u32 ,
6043
+ /// The tag capture for a multi prong.
6044
+ multi_tag_capture : u32 ,
6045
+ /// The tag capture for the special prong.
6046
+ special_tag_capture ,
6017
6047
6018
6048
pub const Multi = struct {
6019
6049
prong : u32 ,
@@ -6029,6 +6059,7 @@ pub const SwitchProngSrc = union(enum) {
6029
6059
gpa : Allocator ,
6030
6060
decl : * Decl ,
6031
6061
switch_node_offset : i32 ,
6062
+ /// Ignored if `prong_src` is not `.range`
6032
6063
range_expand : RangeExpand ,
6033
6064
) LazySrcLoc {
6034
6065
@setCold (true );
@@ -6048,7 +6079,7 @@ pub const SwitchProngSrc = union(enum) {
6048
6079
6049
6080
var multi_i : u32 = 0 ;
6050
6081
var scalar_i : u32 = 0 ;
6051
- for (case_nodes ) | case_node | {
6082
+ const case_node = for (case_nodes ) | case_node | {
6052
6083
const case = tree .fullSwitchCase (case_node ).? ;
6053
6084
6054
6085
const is_special = special : {
@@ -6060,60 +6091,85 @@ pub const SwitchProngSrc = union(enum) {
6060
6091
};
6061
6092
6062
6093
if (is_special ) {
6063
- if (prong_src != .special ) continue ;
6064
- return LazySrcLoc . nodeOffset (
6065
- decl . nodeIndexToRelative ( case . ast . values [ 0 ]) ,
6066
- );
6094
+ switch (prong_src ) {
6095
+ .special , .special_capture , .special_tag_capture = > break case_node ,
6096
+ else = > continue ,
6097
+ }
6067
6098
}
6068
6099
6069
6100
const is_multi = case .ast .values .len != 1 or
6070
6101
node_tags [case .ast .values [0 ]] == .switch_range ;
6071
6102
6072
6103
switch (prong_src ) {
6073
- .scalar = > | i | if (! is_multi and i == scalar_i ) return LazySrcLoc .nodeOffset (
6074
- decl .nodeIndexToRelative (case .ast .values [0 ]),
6075
- ),
6076
- .multi_capture = > | i | if (is_multi and i == multi_i ) {
6077
- return LazySrcLoc { .node_offset_switch_prong_capture = decl .nodeIndexToRelative (case_node ) };
6078
- },
6079
- .multi = > | s | if (is_multi and s .prong == multi_i ) {
6080
- var item_i : u32 = 0 ;
6081
- for (case .ast .values ) | item_node | {
6082
- if (node_tags [item_node ] == .switch_range ) continue ;
6083
-
6084
- if (item_i == s .item ) return LazySrcLoc .nodeOffset (
6085
- decl .nodeIndexToRelative (item_node ),
6086
- );
6087
- item_i += 1 ;
6088
- } else unreachable ;
6089
- },
6090
- .range = > | s | if (is_multi and s .prong == multi_i ) {
6091
- var range_i : u32 = 0 ;
6092
- for (case .ast .values ) | range | {
6093
- if (node_tags [range ] != .switch_range ) continue ;
6094
-
6095
- if (range_i == s .item ) switch (range_expand ) {
6096
- .none = > return LazySrcLoc .nodeOffset (
6097
- decl .nodeIndexToRelative (range ),
6098
- ),
6099
- .first = > return LazySrcLoc .nodeOffset (
6100
- decl .nodeIndexToRelative (node_datas [range ].lhs ),
6101
- ),
6102
- .last = > return LazySrcLoc .nodeOffset (
6103
- decl .nodeIndexToRelative (node_datas [range ].rhs ),
6104
- ),
6105
- };
6106
- range_i += 1 ;
6107
- } else unreachable ;
6108
- },
6109
- .special = > {},
6104
+ .scalar ,
6105
+ .scalar_capture ,
6106
+ .scalar_tag_capture ,
6107
+ = > | i | if (! is_multi and i == scalar_i ) break case_node ,
6108
+
6109
+ .multi_capture ,
6110
+ .multi_tag_capture ,
6111
+ = > | i | if (is_multi and i == multi_i ) break case_node ,
6112
+
6113
+ .multi ,
6114
+ .range ,
6115
+ = > | m | if (is_multi and m .prong == multi_i ) break case_node ,
6116
+
6117
+ .special ,
6118
+ .special_capture ,
6119
+ .special_tag_capture ,
6120
+ = > {},
6110
6121
}
6122
+
6111
6123
if (is_multi ) {
6112
6124
multi_i += 1 ;
6113
6125
} else {
6114
6126
scalar_i += 1 ;
6115
6127
}
6116
6128
} else unreachable ;
6129
+
6130
+ const case = tree .fullSwitchCase (case_node ).? ;
6131
+
6132
+ switch (prong_src ) {
6133
+ .scalar , .special = > return LazySrcLoc .nodeOffset (
6134
+ decl .nodeIndexToRelative (case .ast .values [0 ]),
6135
+ ),
6136
+ .multi = > | m | {
6137
+ var item_i : u32 = 0 ;
6138
+ for (case .ast .values ) | item_node | {
6139
+ if (node_tags [item_node ] == .switch_range ) continue ;
6140
+ if (item_i == m .item ) return LazySrcLoc .nodeOffset (
6141
+ decl .nodeIndexToRelative (item_node ),
6142
+ );
6143
+ item_i += 1 ;
6144
+ }
6145
+ unreachable ;
6146
+ },
6147
+ .range = > | m | {
6148
+ var range_i : u32 = 0 ;
6149
+ for (case .ast .values ) | range | {
6150
+ if (node_tags [range ] != .switch_range ) continue ;
6151
+ if (range_i == m .item ) switch (range_expand ) {
6152
+ .none = > return LazySrcLoc .nodeOffset (
6153
+ decl .nodeIndexToRelative (range ),
6154
+ ),
6155
+ .first = > return LazySrcLoc .nodeOffset (
6156
+ decl .nodeIndexToRelative (node_datas [range ].lhs ),
6157
+ ),
6158
+ .last = > return LazySrcLoc .nodeOffset (
6159
+ decl .nodeIndexToRelative (node_datas [range ].rhs ),
6160
+ ),
6161
+ };
6162
+ range_i += 1 ;
6163
+ }
6164
+ unreachable ;
6165
+ },
6166
+ .scalar_capture , .multi_capture , .special_capture = > {
6167
+ return .{ .node_offset_switch_prong_capture = decl .nodeIndexToRelative (case_node ) };
6168
+ },
6169
+ .scalar_tag_capture , .multi_tag_capture , .special_tag_capture = > {
6170
+ return .{ .node_offset_switch_prong_tag_capture = decl .nodeIndexToRelative (case_node ) };
6171
+ },
6172
+ }
6117
6173
}
6118
6174
};
6119
6175
0 commit comments