Skip to content

Commit c3dbba1

Browse files
authored
Merge pull request #1 from ziglang/child-nodes
Use childNodes helper + Add source locations for some translation failures
2 parents aca9fb9 + e728c4e commit c3dbba1

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

src/Translator.zig

+18-34
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ fn warn(c: *Context, scope: *Scope, loc: TokenIndex, comptime format: []const u8
434434
try scope.appendNode(try ZigTag.warning.create(c.arena, value));
435435
}
436436

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+
437445
pub fn translate(
438446
gpa: mem.Allocator,
439447
comp: *aro.Compilation,
@@ -582,37 +590,24 @@ fn transTopLevelDecls(c: *Context) !void {
582590

583591
fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
584592
const node_tags = c.tree.nodes.items(.tag);
585-
const node_data = c.tree.nodes.items(.data);
586593
const node_ty = c.tree.nodes.items(.ty);
587-
const data = node_data[@intFromEnum(decl)];
588594
switch (node_tags[@intFromEnum(decl)]) {
589595
.typedef => {
590596
try transTypeDef(c, scope, decl);
591597
},
592598

593599
.struct_decl_two,
594600
.union_decl_two,
595-
=> {
596-
try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
597-
},
598601
.struct_decl,
599602
.union_decl,
600603
=> {
601604
try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
602605
},
603606

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);
614609
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);
616611
},
617612

618613
.enum_field_decl,
@@ -644,7 +639,7 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
644639
=> {
645640
try transVarDecl(c, decl);
646641
},
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", .{}),
648643
else => unreachable,
649644
}
650645
}
@@ -776,7 +771,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_ty: Type) Error!void {
776771
const field_type = transType(c, scope, field.ty, .preserve_quals, field_loc) catch |err| switch (err) {
777772
error.UnsupportedType => {
778773
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}", .{
780775
container_kind_name,
781776
field_name,
782777
});
@@ -943,7 +938,7 @@ fn transVarDecl(c: *Context, node: NodeIndex) Error!void {
943938
return failDecl(c, data.decl.name, name, "unable to translate variable declaration", .{});
944939
}
945940

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 {
947942
if (c.decl_table.get(@intFromPtr(enum_decl))) |_|
948943
return; // Avoid processing this decl twice
949944
const toplevel = scope.id == .root;
@@ -992,9 +987,9 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const Type.Enum, field_
992987
}
993988
}
994989

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) {
996991
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", .{});
998993
},
999994
else => |e| return e,
1000995
};
@@ -1068,7 +1063,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH
10681063
const decl_name = c.mapper.lookup(enum_decl.name);
10691064
if (c.weak_global_names.contains(decl_name)) trans_scope = &c.global_scope.base;
10701065
}
1071-
try transEnumDecl(c, trans_scope, enum_decl, &.{});
1066+
try transEnumDecl(c, trans_scope, enum_decl, &.{}, source_loc);
10721067
return ZigTag.identifier.create(c.arena, c.decl_table.get(@intFromPtr(enum_decl)).?);
10731068
},
10741069
.pointer => {
@@ -1357,18 +1352,7 @@ fn transStmt(c: *Context, node: NodeIndex) TransError!ZigNode {
13571352
}
13581353

13591354
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);
13721356
for (stmts) |stmt| {
13731357
const result = try transStmt(c, stmt);
13741358
switch (result.tag()) {

0 commit comments

Comments
 (0)