Skip to content

Commit

Permalink
upgrade 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
lassade committed Aug 20, 2023
1 parent 753a56e commit d2ec136
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ inspeired by this [article](https://floooh.github.io/2020/08/23/sokol-bindgen.ht
## Todo

- transpile inline or constexpr constructors when the class isn't polymorphic
- use function pointers to handle varidact functions
- (hard) `#include` -> `@import`
- (easy) walk a directory tree

Expand Down
18 changes: 9 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
lib.addIncludePath("./test_cases/include");
lib.addIncludePath(.{ .path = "./test_cases/include" });
if (target.getAbi() == .msvc) {
lib.linkLibC();
} else {
lib.linkLibCpp();
}
//lib.addCSourceFile("./test_cases/include/c002_cpp_structs.cpp", cflags);
lib.addCSourceFile("./test_cases/include/c005_inheritance.cpp", cflags);
lib.addCSourceFile("./test_cases/include/c013_cpp_vector.cpp", cflags);
lib.addCSourceFile("./test_cases/include/c022_cpp_string.cpp", cflags);
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/include/c005_inheritance.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/include/c013_cpp_vector.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/include/c022_cpp_string.cpp" }, .flags = cflags });
// glue
//lib.addCSourceFile("./test_cases/c001_c_structs_glue.cpp", cflags);
lib.addCSourceFile("./test_cases/c005_inheritance_glue.cpp", cflags);
lib.addCSourceFile("./test_cases/c009_enum_flags_glue.cpp", cflags);
lib.addCSourceFile("./test_cases/c011_index_this_glue.cpp", cflags);
lib.addCSourceFile("./test_cases/c013_cpp_vector_glue.cpp", cflags);
lib.addCSourceFile("./test_cases/c022_cpp_string_glue.cpp", cflags);
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/c005_inheritance_glue.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/c009_enum_flags_glue.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/c011_index_this_glue.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/c013_cpp_vector_glue.cpp" }, .flags = cflags });
lib.addCSourceFile(.{ .file = .{ .path = "./test_cases/c022_cpp_string_glue.cpp" }, .flags = cflags });
test_cases.linkLibrary(lib);

const cpp_mod = b.addModule("cpp", .{ .source_file = .{ .path = "src/cpp.zig" } });
Expand Down
15 changes: 11 additions & 4 deletions src/Transpiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const Scope = struct {
ctors: usize = 0,
/// Generate unnamed nodes
fields: usize = 0,
is_polymorphic: bool = false,
};

const NamespaceScope = struct {
Expand Down Expand Up @@ -578,7 +579,7 @@ fn visitCXXRecordDecl(self: *Self, value: *const json.Value) !void {
defer functions.deinit();

const parent_state = self.scope;
self.scope = .{ .tag = .class, .name = name };
self.scope = .{ .tag = .class, .name = name, .is_polymorphic = is_polymorphic };
defer self.scope = parent_state;

const parent_namespace = self.beginNamespace();
Expand Down Expand Up @@ -656,7 +657,7 @@ fn visitCXXRecordDecl(self: *Self, value: *const json.Value) !void {
} else if (mem.eql(u8, kind, "CXXConstructorDecl")) {
const out = self.out;
self.out = functions.writer();
try self.visitCXXConstructorDecl(item, name);
try self.visitCXXConstructorDecl(item);
self.out = out;
} else if (mem.eql(u8, kind, "CXXDestructorDecl")) {
const dtor = if (self.no_glue)
Expand Down Expand Up @@ -754,7 +755,9 @@ fn visitVarDecl(self: *Self, value: *const json.Value) !void {
}
}

fn visitCXXConstructorDecl(self: *Self, value: *const json.Value, parent: []const u8) !void {
fn visitCXXConstructorDecl(self: *Self, value: *const json.Value) !void {
const parent = self.scope.name.?;

if (value.object.get("isInvalid")) |invalid| {
if (invalid.bool) {
log.err("invalid ctor of `{s}`", .{parent});
Expand Down Expand Up @@ -785,7 +788,11 @@ fn visitCXXConstructorDecl(self: *Self, value: *const json.Value, parent: []cons

self.nodes_visited += 1;

// todo: copy code from visitCXXMethodDecl
if (self.scope.is_polymorphic) {
// todo: inlnie function
} else {
// default behaviour
}

// note: if the function has a `= 0` at the end it will have "pure" = true attribute

Expand Down
12 changes: 2 additions & 10 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ pub fn main() !void {
if (target_tuple == null) {
// assing a default target tuple
target_tuple = host_target;

try clang.append("-target");
try clang.append(host_target);
log.info("using host `{s}` as target", .{host_target});
}

var dclang = std.ArrayList(u8).init(allocator);
Expand All @@ -96,10 +92,6 @@ pub fn main() !void {
}
log.info("{s}", .{dclang.items});

const target_path = if (target_tuple != null) try mem.replaceOwned(u8, allocator, target_tuple.?, "-", "/") else ".";
defer if (target_tuple != null) allocator.free(target_path);
try std.fs.cwd().makePath(target_path);

const cwd = try std.fs.cwd().realpathAlloc(allocator, ".");
defer allocator.free(cwd);

Expand Down Expand Up @@ -140,7 +132,7 @@ pub fn main() !void {
// zig output
{
output_path.clearRetainingCapacity();
try output_path.writer().print("{s}/{s}.zig", .{ target_path, file_name });
try output_path.writer().print("{s}.zig", .{file_name});

var file = try std.fs.cwd().createFile(output_path.items, .{});
try file.writeAll(transpiler.buffer.items);
Expand All @@ -162,7 +154,7 @@ pub fn main() !void {

if (!transpiler.no_glue) {
output_path.clearRetainingCapacity();
try output_path.writer().print("{s}/{s}_glue.cpp", .{ target_path, file_name });
try output_path.writer().print("{s}_glue.cpp", .{file_name});

var file = try std.fs.cwd().createFile(output_path.items, .{});
try file.writeAll(transpiler.c_buffer.items);
Expand Down

0 comments on commit d2ec136

Please sign in to comment.