Skip to content

Add C++ symbol visibility annotations for Rust functions and impls #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,17 @@ fn write_opaque_type_layout<'a>(out: &mut OutFile<'a>, ety: &'a ExternType) {
writeln!(out, "}}");
}

fn begin_function_definition(out: &mut OutFile) {
if let Some(annotation) = &out.opt.cxx_impl_annotations {
write!(out, "{} ", annotation);
}
}

fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
out.next_section();
out.set_namespace(&efn.name.namespace);
out.begin_block(Block::ExternC);
if let Some(annotation) = &out.opt.cxx_impl_annotations {
write!(out, "{} ", annotation);
}
begin_function_definition(out);
if efn.throws {
out.builtin.ptr_len = true;
write!(out, "::rust::repr::PtrLen ");
Expand Down Expand Up @@ -946,6 +950,9 @@ fn write_rust_function_shim_impl(
// We've already defined this inside the struct.
return;
}
if !out.header {
begin_function_definition(out);
}
write_rust_function_shim_decl(out, local_name, sig, indirect_call);
if out.header {
writeln!(out, ";");
Expand Down Expand Up @@ -1483,6 +1490,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
let instance = ident.to_symbol();

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"{} *Box<{}>::allocation::alloc() noexcept {{",
Expand All @@ -1492,6 +1500,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"void Box<{}>::allocation::dealloc({} *ptr) noexcept {{",
Expand All @@ -1501,6 +1510,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Pair) {
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
writeln!(out, "}}");
Expand All @@ -1513,16 +1523,19 @@ fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
out.include.cstddef = true;

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
writeln!(out, " cxxbridge1$rust_vec${}$new(this);", instance);
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
writeln!(out, " return cxxbridge1$rust_vec${}$drop(this);", instance);
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"::std::size_t Vec<{}>::size() const noexcept {{",
Expand All @@ -1532,6 +1545,7 @@ fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"::std::size_t Vec<{}>::capacity() const noexcept {{",
Expand All @@ -1545,11 +1559,13 @@ fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
writeln!(out, " return cxxbridge1$rust_vec${}$data(this);", instance);
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{",
Expand All @@ -1563,6 +1579,7 @@ fn write_rust_vec_impl(out: &mut OutFile, element: &RustName) {
writeln!(out, "}}");

writeln!(out, "template <>");
begin_function_definition(out);
writeln!(
out,
"void Vec<{}>::set_len(::std::size_t len) noexcept {{",
Expand Down