Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- remove anonymous struct;
- fix union namespace;
- remove comptime global variables
  • Loading branch information
kassane committed Sep 7, 2022
1 parent 1d04557 commit 6fee4b3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
6 changes: 0 additions & 6 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,6 @@ impl Constant {
self.documentation.write(config, out);

let allow_constexpr = config.constant.allow_constexpr && self.value.can_be_constexpr();
let allow_comptime = config.constant.allow_comptime;
match config.language {
Language::Cxx if config.constant.allow_static_const || allow_constexpr => {
if allow_constexpr {
Expand Down Expand Up @@ -809,11 +808,6 @@ impl Constant {
write!(out, " {} # = ", name);
value.write(config, out);
}
Language::Zig if allow_comptime => {
if allow_comptime {
out.write("comptime ");
}
}
Language::Zig => {
out.write(config.style.zig_def());
self.ty.write(config, out);
Expand Down
22 changes: 17 additions & 5 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,12 @@ impl Enum {
Language::C | Language::Cxx => {}
Language::Cython => out.write(config.style.cython_def()),
Language::Zig => {
write!(out, "{}{} = extern ", config.style.zig_def(), self.export_name());
write!(
out,
"{}{} = extern ",
config.style.zig_def(),
self.export_name()
);
}
}

Expand Down Expand Up @@ -939,7 +944,7 @@ impl Enum {

if config.language != Language::Zig {
write!(out, "{} tag;", tag_name);
}else {
} else {
write!(out, "tag: {},", tag_name);
}

Expand Down Expand Up @@ -978,14 +983,21 @@ impl Enum {
// support unnamed structs.
// For the same reason with Cython we can omit per-variant tags (the first
// field) to avoid extra noise, the main `tag` is enough in this case.
if config.language != Language::Cython {
if config.language != Language::Cython && config.language != Language::Zig {
out.write("struct");
out.open_brace();
}
let start_field =
usize::from(inline_tag_field && config.language == Language::Cython);
out.write_vertical_source_list(&body.fields[start_field..], ListType::Cap(";"));
if config.language != Language::Cython {
out.write_vertical_source_list(
&body.fields[start_field..],
ListType::Cap(if config.language != Language::Zig {
";"
} else {
","
}),
);
if config.language != Language::Cython && config.language != Language::Zig {
out.close_brace(true);
}
} else if config.language == Language::Zig {
Expand Down
17 changes: 13 additions & 4 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl Source for Struct {
write!(out, "{} = extern struct", self.export_name());
} else {
out.write("struct");
}
}

if config.language != Language::Cython {
if let Some(align) = self.alignment {
Expand All @@ -464,8 +464,10 @@ impl Source for Struct {
}
}

if config.language != Language::Zig && config.language != Language::C {
write!(out, " {}", self.export_name());
if config.language != Language::C || config.style.generate_tag() {
if config.language != Language::Zig {
write!(out, " {}", self.export_name);
}
}

out.open_brace();
Expand All @@ -476,7 +478,14 @@ impl Source for Struct {
out.new_line();
}

out.write_vertical_source_list(&self.fields, ListType::Cap(if config.language != Language::Zig {";"}else{","}));
out.write_vertical_source_list(
&self.fields,
ListType::Cap(if config.language != Language::Zig {
";"
} else {
","
}),
);
if config.language == Language::Cython && self.fields.is_empty() {
out.write("pass");
}
Expand Down
19 changes: 16 additions & 3 deletions src/bindgen/ir/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ impl Source for Union {
Language::Zig => out.write(config.style.zig_def()),
}

out.write("union");
if config.language == Language::Zig {
write!(out, "{} = extern union", self.export_name());
} else {
out.write("union");
}

// Cython supports `packed` on structs (see comments there), but not on unions.
if config.language != Language::Cython {
Expand All @@ -303,7 +307,9 @@ impl Source for Union {
}

if config.language != Language::C || config.style.generate_tag() {
write!(out, " {}", self.export_name);
if config.language != Language::Zig {
write!(out, " {}", self.export_name);
}
}

out.open_brace();
Expand All @@ -314,7 +320,14 @@ impl Source for Union {
out.new_line();
}

out.write_vertical_source_list(&self.fields, ListType::Cap(";"));
out.write_vertical_source_list(
&self.fields,
ListType::Cap(if config.language != Language::Zig {
";"
} else {
","
}),
);
if config.language == Language::Cython && self.fields.is_empty() {
out.write("pass");
}
Expand Down
20 changes: 20 additions & 0 deletions tests/expectations/enum_self.zig
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
const std = @import("std");

pub const Foo_Bar = extern struct {
something: ?*i32,
};

pub const Bar_Tag = enum {
Min,
Max,
Other,
};

pub const Bar = extern union {
tag: Bar_Tag,
min_tag: Bar_Tag,
min: Foo_Bar,
max_tag: Bar_Tag,
max: Foo_Bar,
};

pub extern fn root(b: Bar) anyopaque;

0 comments on commit 6fee4b3

Please sign in to comment.