@@ -434,6 +434,14 @@ fn warn(c: *Context, scope: *Scope, loc: TokenIndex, comptime format: []const u8
434
434
try scope .appendNode (try ZigTag .warning .create (c .arena , value ));
435
435
}
436
436
437
+ fn tokenIndex (c : * Context , node : NodeIndex ) ? TokenIndex {
438
+ const token_index = c .tree .nodes .items (.loc )[@intFromEnum (node )];
439
+ return switch (token_index ) {
440
+ .none = > null ,
441
+ else = > @intFromEnum (token_index ),
442
+ };
443
+ }
444
+
437
445
pub fn translate (
438
446
gpa : mem.Allocator ,
439
447
comp : * aro.Compilation ,
@@ -582,37 +590,24 @@ fn transTopLevelDecls(c: *Context) !void {
582
590
583
591
fn transDecl (c : * Context , scope : * Scope , decl : NodeIndex ) ! void {
584
592
const node_tags = c .tree .nodes .items (.tag );
585
- const node_data = c .tree .nodes .items (.data );
586
593
const node_ty = c .tree .nodes .items (.ty );
587
- const data = node_data [@intFromEnum (decl )];
588
594
switch (node_tags [@intFromEnum (decl )]) {
589
595
.typedef = > {
590
596
try transTypeDef (c , scope , decl );
591
597
},
592
598
593
599
.struct_decl_two ,
594
600
.union_decl_two ,
595
- = > {
596
- try transRecordDecl (c , scope , node_ty [@intFromEnum (decl )]);
597
- },
598
601
.struct_decl ,
599
602
.union_decl ,
600
603
= > {
601
604
try transRecordDecl (c , scope , node_ty [@intFromEnum (decl )]);
602
605
},
603
606
604
- .enum_decl_two = > {
605
- var fields = [2 ]NodeIndex { data .bin .lhs , data .bin .rhs };
606
- var field_count : u8 = 0 ;
607
- if (fields [0 ] != .none ) field_count += 1 ;
608
- if (fields [1 ] != .none ) field_count += 1 ;
609
- const enum_decl = node_ty [@intFromEnum (decl )].canonicalize (.standard ).data .@"enum" ;
610
- try transEnumDecl (c , scope , enum_decl , fields [0.. field_count ]);
611
- },
612
- .enum_decl = > {
613
- const fields = c .tree .data [data .range .start .. data .range .end ];
607
+ .enum_decl_two , .enum_decl = > {
608
+ const fields = c .tree .childNodes (decl );
614
609
const enum_decl = node_ty [@intFromEnum (decl )].canonicalize (.standard ).data .@"enum" ;
615
- try transEnumDecl (c , scope , enum_decl , fields );
610
+ try transEnumDecl (c , scope , enum_decl , fields , c . tokenIndex ( decl ) orelse 0 );
616
611
},
617
612
618
613
.enum_field_decl ,
@@ -644,7 +639,7 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
644
639
= > {
645
640
try transVarDecl (c , decl );
646
641
},
647
- .static_assert = > try warn (c , & c .global_scope .base , 0 , "ignoring _Static_assert declaration" , .{}),
642
+ .static_assert = > try warn (c , & c .global_scope .base , c . tokenIndex ( decl ) orelse 0 , "ignoring _Static_assert declaration" , .{}),
648
643
else = > unreachable ,
649
644
}
650
645
}
@@ -776,7 +771,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_ty: Type) Error!void {
776
771
const field_type = transType (c , scope , field .ty , .preserve_quals , field_loc ) catch | err | switch (err ) {
777
772
error .UnsupportedType = > {
778
773
try c .opaque_demotes .put (c .gpa , @intFromPtr (record_decl ), {});
779
- try warn (c , scope , 0 , "{s} demoted to opaque type - unable to translate type of field {s}" , .{
774
+ try warn (c , scope , field . name_tok , "{s} demoted to opaque type - unable to translate type of field {s}" , .{
780
775
container_kind_name ,
781
776
field_name ,
782
777
});
@@ -943,7 +938,7 @@ fn transVarDecl(c: *Context, node: NodeIndex) Error!void {
943
938
return failDecl (c , data .decl .name , name , "unable to translate variable declaration" , .{});
944
939
}
945
940
946
- fn transEnumDecl (c : * Context , scope : * Scope , enum_decl : * const Type.Enum , field_nodes : []const NodeIndex ) Error ! void {
941
+ fn transEnumDecl (c : * Context , scope : * Scope , enum_decl : * const Type.Enum , field_nodes : []const NodeIndex , source_loc : ? TokenIndex ) Error ! void {
947
942
if (c .decl_table .get (@intFromPtr (enum_decl ))) | _ |
948
943
return ; // Avoid processing this decl twice
949
944
const toplevel = scope .id == .root ;
@@ -992,9 +987,9 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const Type.Enum, field_
992
987
}
993
988
}
994
989
995
- break :blk transType (c , scope , enum_decl .tag_ty , .standard , 0 ) catch | err | switch (err ) {
990
+ break :blk transType (c , scope , enum_decl .tag_ty , .standard , source_loc orelse 0 ) catch | err | switch (err ) {
996
991
error .UnsupportedType = > {
997
- return failDecl (c , 0 , name , "unable to translate enum integer type" , .{});
992
+ return failDecl (c , source_loc orelse 0 , name , "unable to translate enum integer type" , .{});
998
993
},
999
994
else = > | e | return e ,
1000
995
};
@@ -1068,7 +1063,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH
1068
1063
const decl_name = c .mapper .lookup (enum_decl .name );
1069
1064
if (c .weak_global_names .contains (decl_name )) trans_scope = & c .global_scope .base ;
1070
1065
}
1071
- try transEnumDecl (c , trans_scope , enum_decl , &.{});
1066
+ try transEnumDecl (c , trans_scope , enum_decl , &.{}, source_loc );
1072
1067
return ZigTag .identifier .create (c .arena , c .decl_table .get (@intFromPtr (enum_decl )).? );
1073
1068
},
1074
1069
.pointer = > {
@@ -1357,18 +1352,7 @@ fn transStmt(c: *Context, node: NodeIndex) TransError!ZigNode {
1357
1352
}
1358
1353
1359
1354
fn transCompoundStmtInline (c : * Context , compound : NodeIndex , block : * Scope.Block ) TransError ! void {
1360
- const data = c .tree .nodes .items (.data )[@intFromEnum (compound )];
1361
- var buf : [2 ]NodeIndex = undefined ;
1362
- // TODO move these helpers to Aro
1363
- const stmts = switch (c .tree .nodes .items (.tag )[@intFromEnum (compound )]) {
1364
- .compound_stmt_two = > blk : {
1365
- if (data .bin .lhs != .none ) buf [0 ] = data .bin .lhs ;
1366
- if (data .bin .rhs != .none ) buf [1 ] = data .bin .rhs ;
1367
- break :blk buf [0 .. @as (u32 , @intFromBool (data .bin .lhs != .none )) + @intFromBool (data .bin .rhs != .none )];
1368
- },
1369
- .compound_stmt = > c .tree .data [data .range .start .. data .range .end ],
1370
- else = > unreachable ,
1371
- };
1355
+ const stmts = c .tree .childNodes (compound );
1372
1356
for (stmts ) | stmt | {
1373
1357
const result = try transStmt (c , stmt );
1374
1358
switch (result .tag ()) {
0 commit comments