From 9238b706a86a14753123d44f6736ac1f1d4ff7be Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Sat, 31 Oct 2020 22:15:05 -0700 Subject: [PATCH 1/2] Test for namespace order sensitivity. --- tests/ffi/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ffi/lib.rs b/tests/ffi/lib.rs index fee5bcb11..c79aff3d1 100644 --- a/tests/ffi/lib.rs +++ b/tests/ffi/lib.rs @@ -101,6 +101,16 @@ pub mod ffi { z: usize, } + #[namespace = "first"] + struct First { + second: Box, + } + + #[namespace = "second"] + struct Second { + i: i32, + } + extern "C" { include!("tests/ffi/tests.h"); From f9213628a58e891f27032244ab0cdd0b3a3d269d Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Sat, 31 Oct 2020 22:25:42 -0700 Subject: [PATCH 2/2] Fix namespace forward declarations. This fixes the case where a lexicographically early namespace has a reference to a type in a lexicographically later namespace. --- gen/src/write.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/gen/src/write.rs b/gen/src/write.rs index f25922239..dfdade22e 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -32,6 +32,7 @@ pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) - let apis_by_namespace = NamespaceEntries::new(apis); + gen_namespace_forward_declarations(&apis_by_namespace, out); gen_namespace_contents(&apis_by_namespace, types, opt, header, out); if !header { @@ -44,13 +45,7 @@ pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) - out_file } -fn gen_namespace_contents( - ns_entries: &NamespaceEntries, - types: &Types, - opt: &Opt, - header: bool, - out: &mut OutFile, -) { +fn gen_namespace_forward_declarations(ns_entries: &NamespaceEntries, out: &mut OutFile) { let apis = ns_entries.entries(); out.next_section(); @@ -63,6 +58,24 @@ fn gen_namespace_contents( } } + out.next_section(); + + for (child_ns, child_ns_entries) in ns_entries.children() { + writeln!(out, "namespace {} {{", child_ns); + gen_namespace_forward_declarations(&child_ns_entries, out); + writeln!(out, "}} // namespace {}", child_ns); + } +} + +fn gen_namespace_contents( + ns_entries: &NamespaceEntries, + types: &Types, + opt: &Opt, + header: bool, + out: &mut OutFile, +) { + let apis = ns_entries.entries(); + let mut methods_for_type = HashMap::new(); for api in apis.iter() { if let Api::RustFunction(efn) = api {