authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-11-07 17:04:49+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-11-15 10:51:26+02:00
logc327489d21a438344453e8c9e64091b9c8b540e4
tree093cd710ce7a1d884af8289851848bf5da9ce4b2
parenta15feeb694ed6dec5a2216c7a23df88d29ba8fe6

aro-translate-c: start work on translating statements


1 files changed, 45 insertions(+), 24 deletions(-)

src/aro_translate_c.zig+45-24
...@@ -326,15 +326,9 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {...@@ -326,15 +326,9 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
326 .is_inline = is_always_inline,326 .is_inline = is_always_inline,
327 .is_extern = !has_body,327 .is_extern = !has_body,
328 .is_export = switch (c.tree.nodes.items(.tag)[@intFromEnum(fn_decl)]) {328 .is_export = switch (c.tree.nodes.items(.tag)[@intFromEnum(fn_decl)]) {
329 .fn_proto,329 .fn_proto, .fn_def => has_body and !is_always_inline,
330 .fn_def => has_body and !is_always_inline,
331330
332 .inline_fn_proto,331 .inline_fn_proto, .inline_fn_def, .inline_static_fn_proto, .inline_static_fn_def, .static_fn_proto, .static_fn_def => false,
333 .inline_fn_def,
334 .inline_static_fn_proto,
335 .inline_static_fn_def,
336 .static_fn_proto,
337 .static_fn_def => false,
338332
339 else => unreachable,333 else => unreachable,
340 },334 },
...@@ -354,7 +348,6 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {...@@ -354,7 +348,6 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
354348
355 // actual function definition with body349 // actual function definition with body
356 const body_stmt = node_data.decl.node;350 const body_stmt = node_data.decl.node;
357 _ = body_stmt;
358 var block_scope = try Scope.Block.init(c, &c.global_scope.base, false);351 var block_scope = try Scope.Block.init(c, &c.global_scope.base, false);
359 block_scope.return_type = fn_ty.data.func.return_type;352 block_scope.return_type = fn_ty.data.func.return_type;
360 defer block_scope.deinit();353 defer block_scope.deinit();
...@@ -390,19 +383,18 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {...@@ -390,19 +383,18 @@ fn transFnDecl(c: *Context, fn_decl: NodeIndex) Error!void {
390 param_id += 1;383 param_id += 1;
391 }384 }
392385
393 // const casted_body = @as(*const clang.CompoundStmt, @ptrCast(body_stmt));386 transCompoundStmtInline(c, body_stmt, &block_scope) catch |err| switch (err) {
394 // transCompoundStmtInline(c, casted_body, &block_scope) catch |err| switch (err) {387 error.OutOfMemory => |e| return e,
395 // error.OutOfMemory => |e| return e,388 error.UnsupportedTranslation,
396 // error.UnsupportedTranslation,389 error.UnsupportedType,
397 // error.UnsupportedType,390 => {
398 // => {391 proto_payload.data.is_extern = true;
399 // proto_payload.data.is_extern = true;392 proto_payload.data.is_export = false;
400 // proto_payload.data.is_export = false;393 proto_payload.data.is_inline = false;
401 // proto_payload.data.is_inline = false;394 try warn(c, &c.global_scope.base, fn_decl_loc, "unable to translate function, demoted to extern", .{});
402 // 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);
403 // return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base));396 },
404 // },397 };
405 // };
406398
407 proto_payload.data.body = try block_scope.complete(c);399 proto_payload.data.body = try block_scope.complete(c);
408 return addTopLevelDecl(c, fn_name, proto_node);400 return addTopLevelDecl(c, fn_name, proto_node);
...@@ -629,8 +621,37 @@ fn transFnType(...@@ -629,8 +621,37 @@ fn transFnType(
629 return ZigNode.initPayload(&payload.base);621 return ZigNode.initPayload(&payload.base);
630}622}
631623
632fn transStmt(c: *Context, node: NodeIndex) TransError!void {624fn transStmt(c: *Context, node: NodeIndex) TransError!ZigNode {
633 _ = try c.transExpr(node, .unused);625 return transExpr(c, node, .unused);
626}
627
628fn 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
650fn 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);
634}655}
635656
636fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!ZigNode {657fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!ZigNode {