diff options
| author | Veikka Tuominen <git@vexu.eu> | 2021-06-15 07:59:46 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-15 07:59:46 +0300 |
| commit | 97946e2a41a6a44ed376b57b32a19ce9d85f0bc2 (patch) | |
| tree | 5c7de4068bbb50e11ebe414bfc61540bc85b79ce /src | |
| parent | ac42503266f27af2a4528af3ff0f1ef2cfbfe766 (diff) | |
| parent | 6dbdac4b605fb56b4180b6c66154275c22954a8d (diff) | |
| download | zig-97946e2a41a6a44ed376b57b32a19ce9d85f0bc2.tar.gz zig-97946e2a41a6a44ed376b57b32a19ce9d85f0bc2.zip | |
Merge pull request #9091 from Vexu/translate-c
Translate-c: move utility functions to a separate namespace
Diffstat (limited to 'src')
| -rw-r--r-- | src/translate_c.zig | 58 | ||||
| -rw-r--r-- | src/translate_c/ast.zig | 87 |
2 files changed, 79 insertions, 66 deletions
diff --git a/src/translate_c.zig b/src/translate_c.zig index 2863377aad..4b07618391 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -12,7 +12,6 @@ const meta = std.meta; const ast = @import("translate_c/ast.zig"); const Node = ast.Node; const Tag = Node.Tag; -const c_builtins = std.c.builtins; const CallingConvention = std.builtin.CallingConvention; @@ -863,7 +862,7 @@ fn buildFlexibleArrayFn( defer block_scope.deinit(); const intermediate_type_name = try block_scope.makeMangledName(c, "Intermediate"); - const intermediate_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type }); + const intermediate_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type }); const intermediate_type_decl = try Tag.var_simple.create(c.arena, .{ .name = intermediate_type_name, .init = intermediate_type, @@ -872,7 +871,7 @@ fn buildFlexibleArrayFn( const intermediate_type_ident = try Tag.identifier.create(c.arena, intermediate_type_name); const return_type_name = try block_scope.makeMangledName(c, "ReturnType"); - const return_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type }); + const return_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type }); const return_type_decl = try Tag.var_simple.create(c.arena, .{ .name = return_type_name, .init = return_type, @@ -1290,9 +1289,19 @@ fn transStmt( return maybeSuppressResult(c, scope, result_used, shuffle_vec_node); }, // When adding new cases here, see comment for maybeBlockify() - else => { - return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}); - }, + .GCCAsmStmtClass, + .GotoStmtClass, + .IndirectGotoStmtClass, + .AttributedStmtClass, + .AddrLabelExprClass, + .AtomicExprClass, + .BlockExprClass, + .UserDefinedLiteralClass, + .BuiltinBitCastExprClass, + .DesignatedInitExprClass, + .LabelStmtClass, + => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}), + else => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "unsupported stmt class {s}", .{@tagName(sc)}), } } @@ -1374,7 +1383,7 @@ fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorE for (init_list) |*init, i| { const index_expr = try transExprCoercing(c, scope, expr.getExpr(@intCast(c_uint, i + 2)), .used); - const converted_index = try Tag.std_meta_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len }); + const converted_index = try Tag.helpers_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len }); init.* = converted_index; } @@ -1820,13 +1829,10 @@ fn transDeclStmtOne( .Function => { try visitFnDecl(c, @ptrCast(*const clang.FunctionDecl, decl)); }, - else => |kind| return fail( - c, - error.UnsupportedTranslation, - decl.getLocation(), - "TODO implement translation of DeclStmt kind {s}", - .{@tagName(kind)}, - ), + else => { + const decl_name = try c.str(decl.getDeclKindName()); + try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name}); + }, } } @@ -1902,7 +1908,7 @@ fn transImplicitCastExpr( const ne = try Tag.not_equal.create(c.arena, .{ .lhs = ptr_to_int, .rhs = Tag.zero_literal.init() }); return maybeSuppressResult(c, scope, result_used, ne); }, - .IntegralToBoolean => { + .IntegralToBoolean, .FloatingToBoolean => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); // The expression is already a boolean one, return it as-is @@ -1924,14 +1930,14 @@ fn transImplicitCastExpr( c, error.UnsupportedTranslation, @ptrCast(*const clang.Stmt, expr).getBeginLoc(), - "TODO implement translation of CastKind {s}", + "unsupported CastKind {s}", .{@tagName(kind)}, ), } } fn isBuiltinDefined(name: []const u8) bool { - inline for (meta.declarations(c_builtins)) |decl| { + inline for (meta.declarations(std.zig.c_builtins)) |decl| { if (std.mem.eql(u8, name, decl.name)) return true; } return false; @@ -2358,8 +2364,10 @@ fn transCCast( return Tag.float_to_int.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (!cIsFloating(src_type) and cIsFloating(dst_type)) { + var rhs = expr; + if (qualTypeIsBoolean(src_type)) rhs = try Tag.bool_to_int.create(c.arena, expr); // @intToFloat(dest_type, val) - return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); + return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = rhs }); } if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) { // @boolToInt returns either a comptime_int or a u1 @@ -2370,7 +2378,7 @@ fn transCCast( } if (cIsEnum(dst_type)) { // import("std").meta.cast(dest_type, val) - return Tag.std_meta_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); + return Tag.helpers_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsEnum(src_type) and !cIsEnum(dst_type)) { // @enumToInt(val) @@ -4547,6 +4555,10 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan .rhs = try transQualType(c, scope, element_qt, source_loc), }); }, + .ExtInt, .ExtVector => { + const type_name = c.str(ty.getTypeClassName()); + return fail(c, error.UnsupportedType, source_loc, "TODO implement translation of type: '{s}'", .{type_name}); + }, else => { const type_name = c.str(ty.getTypeClassName()); return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name}); @@ -4982,7 +4994,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void { break :blk br.data.val; } else expr; - const return_type = if (typeof_arg.castTag(.std_meta_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some| + const return_type = if (typeof_arg.castTag(.helpers_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some| some.data.lhs else if (typeof_arg.castTag(.std_mem_zeroes)) |some| some.data @@ -5095,7 +5107,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { if (guaranteed_to_fit) { return Tag.as.create(c.arena, .{ .lhs = type_node, .rhs = literal_node }); } else { - return Tag.std_meta_promoteIntLiteral.create(c.arena, .{ + return Tag.helpers_promoteIntLiteral.create(c.arena, .{ .type = type_node, .value = literal_node, .radix = try Tag.enum_literal.create(c.arena, radix), @@ -5578,7 +5590,7 @@ fn parseCCastExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { return parseCPostfixExpr(c, m, scope, type_name); } const node_to_cast = try parseCCastExpr(c, m, scope); - return Tag.std_meta_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast }); + return Tag.helpers_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast }); } }, else => {}, @@ -5925,7 +5937,7 @@ fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { break :blk inner; } else try parseCUnaryExpr(c, m, scope); - return Tag.std_meta_sizeof.create(c.arena, operand); + return Tag.helpers_sizeof.create(c.arena, operand); }, .Keyword_alignof => { // TODO this won't work if using <stdalign.h>'s diff --git a/src/translate_c/ast.zig b/src/translate_c/ast.zig index 8ee8b670c3..59e84c6ba0 100644 --- a/src/translate_c/ast.zig +++ b/src/translate_c/ast.zig @@ -74,7 +74,7 @@ pub const Node = extern union { tuple, container_init, container_init_dot, - std_meta_cast, + helpers_cast, /// _ = operand; discard, @@ -124,8 +124,8 @@ pub const Node = extern union { std_math_Log2Int, /// @intCast(lhs, rhs) int_cast, - /// @import("std").meta.promoteIntLiteral(value, type, radix) - std_meta_promoteIntLiteral, + /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix) + helpers_promoteIntLiteral, /// @import("std").meta.alignment(value) std_meta_alignment, /// @rem(lhs, rhs) @@ -193,12 +193,12 @@ pub const Node = extern union { array_type, null_sentinel_array_type, - /// @import("std").meta.sizeof(operand) - std_meta_sizeof, - /// @import("std").meta.FlexibleArrayType(lhs, rhs) - std_meta_flexible_array_type, - /// @import("std").meta.shuffleVectorIndex(lhs, rhs) - std_meta_shuffle_vector_index, + /// @import("std").zig.c_translation.sizeof(operand) + helpers_sizeof, + /// @import("std").zig.c_translation.FlexibleArrayType(lhs, rhs) + helpers_flexible_array_type, + /// @import("std").zig.c_translation.shuffleVectorIndex(lhs, rhs) + helpers_shuffle_vector_index, /// @import("std").meta.Vector(lhs, rhs) std_meta_vector, /// @import("std").mem.zeroes(operand) @@ -272,7 +272,7 @@ pub const Node = extern union { .if_not_break, .switch_else, .block_single, - .std_meta_sizeof, + .helpers_sizeof, .std_meta_alignment, .bool_to_int, .sizeof, @@ -332,13 +332,13 @@ pub const Node = extern union { .align_cast, .array_access, .std_mem_zeroinit, - .std_meta_flexible_array_type, - .std_meta_shuffle_vector_index, + .helpers_flexible_array_type, + .helpers_shuffle_vector_index, .std_meta_vector, .ptr_cast, .div_exact, .offset_of, - .std_meta_cast, + .helpers_cast, => Payload.BinOp, .integer_literal, @@ -362,7 +362,7 @@ pub const Node = extern union { .tuple => Payload.TupleInit, .container_init => Payload.ContainerInit, .container_init_dot => Payload.ContainerInitDot, - .std_meta_promoteIntLiteral => Payload.PromoteIntLiteral, + .helpers_promoteIntLiteral => Payload.PromoteIntLiteral, .block => Payload.Block, .c_pointer, .single_pointer => Payload.Pointer, .array_type, .null_sentinel_array_type => Payload.Array, @@ -868,7 +868,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { // pub usingnamespace @import("std").c.builtins; _ = try c.addToken(.keyword_pub, "pub"); const usingnamespace_token = try c.addToken(.keyword_usingnamespace, "usingnamespace"); - const import_node = try renderStdImport(c, "c", "builtins"); + const import_node = try renderStdImport(c, &.{ "zig", "c_builtins" }); _ = try c.addToken(.semicolon, ";"); return c.addNode(.{ @@ -882,52 +882,52 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { }, .std_math_Log2Int => { const payload = node.castTag(.std_math_Log2Int).?.data; - const import_node = try renderStdImport(c, "math", "Log2Int"); + const import_node = try renderStdImport(c, &.{ "math", "Log2Int" }); return renderCall(c, import_node, &.{payload}); }, - .std_meta_cast => { - const payload = node.castTag(.std_meta_cast).?.data; - const import_node = try renderStdImport(c, "meta", "cast"); + .helpers_cast => { + const payload = node.castTag(.helpers_cast).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "cast" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_promoteIntLiteral => { - const payload = node.castTag(.std_meta_promoteIntLiteral).?.data; - const import_node = try renderStdImport(c, "meta", "promoteIntLiteral"); + .helpers_promoteIntLiteral => { + const payload = node.castTag(.helpers_promoteIntLiteral).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" }); return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix }); }, .std_meta_alignment => { const payload = node.castTag(.std_meta_alignment).?.data; - const import_node = try renderStdImport(c, "meta", "alignment"); + const import_node = try renderStdImport(c, &.{ "meta", "alignment" }); return renderCall(c, import_node, &.{payload}); }, - .std_meta_sizeof => { - const payload = node.castTag(.std_meta_sizeof).?.data; - const import_node = try renderStdImport(c, "meta", "sizeof"); + .helpers_sizeof => { + const payload = node.castTag(.helpers_sizeof).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "sizeof" }); return renderCall(c, import_node, &.{payload}); }, .std_mem_zeroes => { const payload = node.castTag(.std_mem_zeroes).?.data; - const import_node = try renderStdImport(c, "mem", "zeroes"); + const import_node = try renderStdImport(c, &.{ "mem", "zeroes" }); return renderCall(c, import_node, &.{payload}); }, .std_mem_zeroinit => { const payload = node.castTag(.std_mem_zeroinit).?.data; - const import_node = try renderStdImport(c, "mem", "zeroInit"); + const import_node = try renderStdImport(c, &.{ "mem", "zeroInit" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_flexible_array_type => { - const payload = node.castTag(.std_meta_flexible_array_type).?.data; - const import_node = try renderStdImport(c, "meta", "FlexibleArrayType"); + .helpers_flexible_array_type => { + const payload = node.castTag(.helpers_flexible_array_type).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "FlexibleArrayType" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_shuffle_vector_index => { - const payload = node.castTag(.std_meta_shuffle_vector_index).?.data; - const import_node = try renderStdImport(c, "meta", "shuffleVectorIndex"); + .helpers_shuffle_vector_index => { + const payload = node.castTag(.helpers_shuffle_vector_index).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, .std_meta_vector => { const payload = node.castTag(.std_meta_vector).?.data; - const import_node = try renderStdImport(c, "meta", "Vector"); + const import_node = try renderStdImport(c, &.{ "meta", "Vector" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, .call => { @@ -2269,13 +2269,13 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex { .alignof, .typeof, .typeinfo, - .std_meta_sizeof, .std_meta_alignment, - .std_meta_cast, - .std_meta_promoteIntLiteral, .std_meta_vector, - .std_meta_shuffle_vector_index, - .std_meta_flexible_array_type, + .helpers_sizeof, + .helpers_cast, + .helpers_promoteIntLiteral, + .helpers_shuffle_vector_index, + .helpers_flexible_array_type, .std_mem_zeroinit, .integer_literal, .float_literal, @@ -2442,7 +2442,7 @@ fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: Toke }); } -fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeIndex { +fn renderStdImport(c: *Context, parts: []const []const u8) !NodeIndex { const import_tok = try c.addToken(.builtin, "@import"); _ = try c.addToken(.l_paren, "("); const std_tok = try c.addToken(.string_literal, "\"std\""); @@ -2463,8 +2463,9 @@ fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeInde }); var access_chain = import_node; - access_chain = try renderFieldAccess(c, access_chain, first); - access_chain = try renderFieldAccess(c, access_chain, second); + for (parts) |part| { + access_chain = try renderFieldAccess(c, access_chain, part); + } return access_chain; } |
