Skip to content

Commit c327489

Browse files
committed
aro-translate-c: start work on translating statements
1 parent a15feeb commit c327489

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

src/aro_translate_c.zig

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,9 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
326326
.is_inline = is_always_inline,
327327
.is_extern = !has_body,
328328
.is_export = switch (c.tree.nodes.items(.tag)[@intFromEnum(fn_decl)]) {
329-
.fn_proto,
330-
.fn_def => has_body and !is_always_inline,
329+
.fn_proto, .fn_def => has_body and !is_always_inline,
331330

332-
.inline_fn_proto,
333-
.inline_fn_def,
334-
.inline_static_fn_proto,
335-
.inline_static_fn_def,
336-
.static_fn_proto,
337-
.static_fn_def => false,
331+
.inline_fn_proto, .inline_fn_def, .inline_static_fn_proto, .inline_static_fn_def, .static_fn_proto, .static_fn_def => false,
338332

339333
else => unreachable,
340334
},
@@ -354,7 +348,6 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
354348

355349
// actual function definition with body
356350
const body_stmt = node_data.decl.node;
357-
_ = body_stmt;
358351
var block_scope = try Scope.Block.init(c, &c.global_scope.base, false);
359352
block_scope.return_type = fn_ty.data.func.return_type;
360353
defer block_scope.deinit();
@@ -390,19 +383,18 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
390383
param_id += 1;
391384
}
392385

393-
// const casted_body = @as(*const clang.CompoundStmt, @ptrCast(body_stmt));
394-
// transCompoundStmtInline(c, casted_body, &block_scope) catch |err| switch (err) {
395-
// error.OutOfMemory => |e| return e,
396-
// error.UnsupportedTranslation,
397-
// error.UnsupportedType,
398-
// => {
399-
// proto_payload.data.is_extern = true;
400-
// proto_payload.data.is_export = false;
401-
// proto_payload.data.is_inline = false;
402-
// try warn(c, &c.global_scope.base, fn_decl_loc, "unable to translate function, demoted to extern", .{});
403-
// return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base));
404-
// },
405-
// };
386+
transCompoundStmtInline(c, body_stmt, &block_scope) catch |err| switch (err) {
387+
error.OutOfMemory => |e| return e,
388+
error.UnsupportedTranslation,
389+
error.UnsupportedType,
390+
=> {
391+
proto_payload.data.is_extern = true;
392+
proto_payload.data.is_export = false;
393+
proto_payload.data.is_inline = false;
394+
try warn(c, &c.global_scope.base, fn_decl_loc, "unable to translate function, demoted to extern", .{});
395+
return addTopLevelDecl(c, fn_name, proto_node);
396+
},
397+
};
406398

407399
proto_payload.data.body = try block_scope.complete(c);
408400
return addTopLevelDecl(c, fn_name, proto_node);
@@ -629,8 +621,37 @@ fn transFnType(
629621
return ZigNode.initPayload(&payload.base);
630622
}
631623

632-
fn transStmt(c: *Context, node: NodeIndex) TransError!void {
633-
_ = try c.transExpr(node, .unused);
624+
fn transStmt(c: *Context, node: NodeIndex) TransError!ZigNode {
625+
return transExpr(c, node, .unused);
626+
}
627+
628+
fn transCompoundStmtInline(c: *Context, compound: NodeIndex, block: *Scope.Block) TransError!void {
629+
const data = c.tree.nodes.items(.data)[@intFromEnum(compound)];
630+
var buf: [2]NodeIndex = undefined;
631+
// TODO move these helpers to Aro
632+
const stmts = switch (c.tree.nodes.items(.tag)[@intFromEnum(compound)]) {
633+
.compound_stmt_two => blk: {
634+
if (data.bin.lhs != .none) buf[0] = data.bin.lhs;
635+
if (data.bin.rhs != .none) buf[1] = data.bin.rhs;
636+
break :blk buf[0 .. @as(u32, @intFromBool(data.bin.lhs != .none)) + @intFromBool(data.bin.rhs != .none)];
637+
},
638+
.compound_stmt => c.tree.data[data.range.start..data.range.end],
639+
else => unreachable,
640+
};
641+
for (stmts) |stmt| {
642+
const result = try transStmt(c, stmt);
643+
switch (result.tag()) {
644+
.declaration, .empty_block => {},
645+
else => try block.statements.append(result),
646+
}
647+
}
648+
}
649+
650+
fn transCompoundStmt(c: *Context, scope: *Scope, compound: NodeIndex) TransError!ZigNode {
651+
var block_scope = try Scope.Block.init(c, scope, false);
652+
defer block_scope.deinit();
653+
try transCompoundStmtInline(c, compound, &block_scope);
654+
return try block_scope.complete(c);
634655
}
635656

636657
fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!ZigNode {

0 commit comments

Comments
 (0)