Skip to content

Commit

Permalink
ignore private members + new ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
lassade committed Dec 20, 2023
1 parent 77fe1cd commit ab4d761
Show file tree
Hide file tree
Showing 2 changed files with 1,382 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Transpiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub fn run(self: *Self, value: *const json.Value) anyerror!void {

if (!self.no_glue) {
_ = try self.c_out.write("// auto generated by c2z\n");
try self.c_out.print("#include <new>\n", .{});
try self.c_out.print("#include \"{s}\"\n\n", .{self.header});
}

Expand Down Expand Up @@ -647,6 +648,8 @@ fn visitCXXRecordDecl(self: *Self, value: *const json.Value) !void {

try self.out.print(",\n", .{});
} else if (mem.eql(u8, kind, "CXXMethodDecl")) {
if (!self.public) continue;

const out = self.out;
self.out = functions.writer();
try self.visitCXXMethodDecl(item, name);
Expand All @@ -663,11 +666,15 @@ fn visitCXXRecordDecl(self: *Self, value: *const json.Value) !void {
try self.visitVarDecl(item);
self.out = out;
} else if (mem.eql(u8, kind, "CXXConstructorDecl")) {
if (!self.public) continue;

const out = self.out;
self.out = functions.writer();
try self.visitCXXConstructorDecl(item);
self.out = out;
} else if (mem.eql(u8, kind, "CXXDestructorDecl")) {
if (!self.public) continue;

const dtor = if (self.no_glue)
item.object.get("mangledName").?.string
else
Expand Down Expand Up @@ -827,14 +834,9 @@ fn visitCXXConstructorDecl(self: *Self, value: *const json.Value) !void {
if (!self.no_glue) self.allocator.free(mangled_name);
}

var ctor_comma = false;
try self.out.print("extern fn @\"{s}\"(", .{mangled_name});
if (self.no_glue) {
ctor_comma = true;
try self.out.print("self: *{s}", .{parent});
} else {
try self.c_out.print("extern \"C\" {s} {s}(", .{ self.namespace.full_path.items, mangled_name });
}
var ctor_comma = true;
try self.out.print("extern fn @\"{s}\"(self: *{s}", .{ mangled_name, parent });
try self.c_out.print("extern \"C\" void {s}({s}* self", .{ mangled_name, self.namespace.full_path.items });

var comma = false;
var fn_args = std.ArrayList(u8).init(self.allocator);
Expand Down Expand Up @@ -868,10 +870,8 @@ fn visitCXXConstructorDecl(self: *Self, value: *const json.Value) !void {
defer if (free_arg) self.allocator.free(arg);

if (!self.no_glue) {
if (comma) {
try c_call.appendSlice(", ");
try self.c_buffer.appendSlice(", ");
}
if (comma) try c_call.appendSlice(", ");
if (ctor_comma) try self.c_out.print(", ", .{});
try c_call.appendSlice(arg);

if (mem.indexOf(u8, c_type, "(*)")) |o| {
Expand Down Expand Up @@ -923,11 +923,11 @@ fn visitCXXConstructorDecl(self: *Self, value: *const json.Value) !void {
try self.out.print(" return self;\n", .{});
try self.out.print("}}\n\n", .{});
} else {
try self.out.print(") {s};\npub const init", .{parent});
try self.out.print(") void;\npub const init", .{});
if (self.scope.ctors != 0) try self.out.print("{d}", .{self.scope.ctors + 1}); // avoid name conflict
try self.out.print(" = @\"{s}\";\n\n", .{mangled_name});

try self.c_out.print(") {{ return {s}({s}); }}\n", .{ self.namespace.full_path.items, c_call.items });
try self.c_out.print(") {{ new (self) {s}({s}); }}\n", .{ self.namespace.full_path.items, c_call.items });
}

self.scope.ctors += 1;
Expand Down
Loading

0 comments on commit ab4d761

Please sign in to comment.