blob: 21fbc8ce32bc3ce3b47419b9f2c9efdb562d8758 [file] [log] [blame]
David Tolnay754e21c2020-03-29 20:58:46 -07001use crate::gen::namespace::Namespace;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
Joel Galensonc1c4e7a2020-04-15 10:21:00 -07005use crate::syntax::{Api, ExternFn, ExternType, Receiver, Signature, Struct, Type, Types, Var};
Joel Galenson968738f2020-04-15 14:19:33 -07006use itertools::Itertools;
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
8
David Tolnay33d30292020-03-18 18:02:02 -07009pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070010 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070011 apis: &[Api],
12 types: &Types,
13 opt: Opt,
14 header: bool,
15) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040016 let mut out_file = OutFile::new(namespace.clone(), header);
17 let out = &mut out_file;
18
19 if header {
20 writeln!(out, "#pragma once");
21 }
22
David Tolnay33d30292020-03-18 18:02:02 -070023 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040024 for api in apis {
25 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080026 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040027 }
28 }
29
30 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080031 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040032
David Tolnay7db73692019-10-20 14:51:12 -040033 out.next_section();
34 for name in &namespace {
35 writeln!(out, "namespace {} {{", name);
36 }
37
David Tolnay7db73692019-10-20 14:51:12 -040038 out.next_section();
39 for api in apis {
40 match api {
41 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080042 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
43 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040044 _ => {}
45 }
46 }
47
Joel Galenson968738f2020-04-15 14:19:33 -070048 let methods_for_type = apis.iter().filter_map(|api| match api {
49 Api::RustFunction(efn) => match &efn.sig.receiver {
50 Some(rcvr) => Some((&rcvr.ident, efn)),
51 _ => None,
52 },
53 _ => None,
54 }).into_group_map();
55
David Tolnay7db73692019-10-20 14:51:12 -040056 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070057 match api {
58 Api::Struct(strct) => {
59 out.next_section();
60 write_struct(out, strct);
61 }
62 Api::RustType(ety) => {
Joel Galenson968738f2020-04-15 14:19:33 -070063 match methods_for_type.get(&ety.ident) {
64 Some(methods) => {
65 out.next_section();
66 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070067 },
Joel Galenson968738f2020-04-15 14:19:33 -070068 _ => {}
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070069 }
70 }
71 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040072 }
73 }
74
75 if !header {
76 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070077 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040078 for api in apis {
79 let (efn, write): (_, fn(_, _, _)) = match api {
80 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
81 Api::RustFunction(efn) => (efn, write_rust_function_decl),
82 _ => continue,
83 };
84 out.next_section();
85 write(out, efn, types);
86 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080087 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040088 }
89
90 for api in apis {
91 if let Api::RustFunction(efn) = api {
92 out.next_section();
93 write_rust_function_shim(out, efn, types);
94 }
95 }
96
97 out.next_section();
98 for name in namespace.iter().rev() {
99 writeln!(out, "}} // namespace {}", name);
100 }
101
102 if !header {
103 out.next_section();
104 write_generic_instantiations(out, types);
105 }
106
David Tolnay9c68b1a2020-03-06 11:12:55 -0800107 out.prepend(out.include.to_string());
108
David Tolnay7db73692019-10-20 14:51:12 -0400109 out_file
110}
111
112fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400113 for ty in types {
114 match ty {
115 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700116 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
117 | Some(I64) => out.include.cstdint = true,
118 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800119 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700120 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400121 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800122 Type::RustBox(_) => out.include.type_traits = true,
123 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700124 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400125 _ => {}
126 }
127 }
David Tolnay7db73692019-10-20 14:51:12 -0400128}
129
David Tolnayf51447e2020-03-06 14:14:27 -0800130fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700131 let mut needs_rust_string = false;
132 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700133 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400134 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700135 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700136 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400137 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700138 match ty {
139 Type::RustBox(_) => {
140 out.include.type_traits = true;
141 needs_rust_box = true;
142 }
143 Type::Str(_) => {
144 out.include.cstdint = true;
145 out.include.string = true;
146 needs_rust_str = true;
147 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700148 Type::Fn(_) => {
149 needs_rust_fn = true;
150 }
David Tolnay4770b472020-04-14 16:32:59 -0700151 Type::Slice(_) | Type::SliceRefU8(_) => {
152 needs_rust_slice = true;
153 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700154 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700155 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700156 needs_rust_isize = true;
157 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700158 ty if ty == RustString => {
159 out.include.array = true;
160 out.include.cstdint = true;
161 out.include.string = true;
162 needs_rust_string = true;
163 }
164 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400165 }
166 }
167
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 let mut needs_rust_error = false;
169 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800170 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800171 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700172 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800173 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700174 match api {
175 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700176 if efn.throws {
177 needs_trycatch = true;
178 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700179 for arg in &efn.args {
180 if arg.ty == RustString {
181 needs_unsafe_bitcopy = true;
182 break;
183 }
David Tolnay09011c32020-03-06 14:40:28 -0800184 }
185 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700186 Api::RustFunction(efn) if !out.header => {
187 if efn.throws {
188 out.include.exception = true;
189 needs_rust_error = true;
190 }
191 for arg in &efn.args {
192 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
193 needs_manually_drop = true;
194 break;
195 }
196 }
197 if let Some(ret) = &efn.ret {
198 if types.needs_indirect_abi(ret) {
199 needs_maybe_uninit = true;
200 }
David Tolnayf51447e2020-03-06 14:14:27 -0800201 }
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800204 }
205 }
206
David Tolnay750755e2020-03-01 13:04:08 -0800207 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700208 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800209
David Tolnayb7a7cb62020-03-17 21:18:40 -0700210 if needs_rust_string
211 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700212 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700214 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700216 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 || needs_unsafe_bitcopy
218 || needs_manually_drop
219 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700220 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 {
David Tolnay736cbca2020-03-11 16:49:18 -0700222 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800223 }
224
David Tolnayd1402742020-03-25 22:21:42 -0700225 if needs_rust_string {
226 out.next_section();
227 writeln!(out, "struct unsafe_bitcopy_t;");
228 }
229
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
231 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700232 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700234 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700236 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800238
239 if needs_manually_drop {
240 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700241 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800242 writeln!(out, "template <typename T>");
243 writeln!(out, "union ManuallyDrop {{");
244 writeln!(out, " T value;");
245 writeln!(
246 out,
247 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
248 );
249 writeln!(out, " ~ManuallyDrop() {{}}");
250 writeln!(out, "}};");
251 }
252
David Tolnay09011c32020-03-06 14:40:28 -0800253 if needs_maybe_uninit {
254 out.next_section();
255 writeln!(out, "template <typename T>");
256 writeln!(out, "union MaybeUninit {{");
257 writeln!(out, " T value;");
258 writeln!(out, " MaybeUninit() {{}}");
259 writeln!(out, " ~MaybeUninit() {{}}");
260 writeln!(out, "}};");
261 }
262
David Tolnay3e3e0af2020-03-17 22:42:49 -0700263 out.end_block("namespace cxxbridge02");
264
David Tolnay5d121442020-03-17 22:14:40 -0700265 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700266 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700267 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700268 out.include.type_traits = true;
269 out.include.utility = true;
270 writeln!(out, "class missing {{}};");
271 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700272 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700273 writeln!(out, "template <typename Try, typename Fail>");
274 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700275 writeln!(
276 out,
David Tolnay04722332020-03-18 11:31:54 -0700277 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700278 );
David Tolnay04722332020-03-18 11:31:54 -0700279 writeln!(out, " missing>::value>::type");
280 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700281 writeln!(out, " func();");
282 writeln!(out, "}} catch (const ::std::exception &e) {{");
283 writeln!(out, " fail(e.what());");
284 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700285 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700286 }
287
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800288 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400289}
290
David Tolnayb7a7cb62020-03-17 21:18:40 -0700291fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700292 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700293 if needed {
294 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700295 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700296 if !line.trim_start().starts_with("//") {
297 writeln!(out, "{}", line);
298 }
299 }
300 }
301}
302
David Tolnay7db73692019-10-20 14:51:12 -0400303fn write_struct(out: &mut OutFile, strct: &Struct) {
304 for line in strct.doc.to_string().lines() {
305 writeln!(out, "//{}", line);
306 }
307 writeln!(out, "struct {} final {{", strct.ident);
308 for field in &strct.fields {
309 write!(out, " ");
310 write_type_space(out, &field.ty);
311 writeln!(out, "{};", field.ident);
312 }
313 writeln!(out, "}};");
314}
315
316fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
317 writeln!(out, "struct {};", ident);
318}
319
David Tolnay8861bee2020-01-20 18:39:24 -0800320fn write_struct_using(out: &mut OutFile, ident: &Ident) {
321 writeln!(out, "using {} = {};", ident, ident);
322}
323
Joel Galenson968738f2020-04-15 14:19:33 -0700324fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &Vec<&ExternFn>) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700325 for line in ety.doc.to_string().lines() {
326 writeln!(out, "//{}", line);
327 }
328 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700329 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700330 write!(out, " ");
331 let sig = &method.sig;
332 let local_name = method.ident.to_string();
333 write_rust_function_shim_decl(out, &local_name, sig, None, false);
334 writeln!(out, ";");
335 }
336 writeln!(out, "}};");
337}
338
David Tolnayebef4a22020-03-17 15:33:47 -0700339fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
340 let mut has_cxx_throws = false;
341 for api in apis {
342 if let Api::CxxFunction(efn) = api {
343 if efn.throws {
344 has_cxx_throws = true;
345 break;
346 }
347 }
348 }
349
350 if has_cxx_throws {
351 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700352 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700353 out,
354 "const char *cxxbridge02$exception(const char *, size_t);",
355 );
356 }
357}
358
David Tolnay7db73692019-10-20 14:51:12 -0400359fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700360 if efn.throws {
361 write!(out, "::rust::Str::Repr ");
362 } else {
David Tolnay99642622020-03-25 13:07:35 -0700363 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700364 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700365 let receiver_type = match &efn.receiver {
366 Some(base) => base.ident.to_string(),
367 None => "_".to_string(),
368 };
369 write!(out, "{}cxxbridge02${}${}(", out.namespace, receiver_type, efn.ident);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700370 if let Some(base) = &efn.receiver {
371 write!(out, "{} *__receiver$", base.ident);
372 }
David Tolnay7db73692019-10-20 14:51:12 -0400373 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700374 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400375 write!(out, ", ");
376 }
David Tolnaya46a2372020-03-06 10:03:48 -0800377 if arg.ty == RustString {
378 write!(out, "const ");
379 }
David Tolnay7db73692019-10-20 14:51:12 -0400380 write_extern_arg(out, arg, types);
381 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700382 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400383 if indirect_return {
384 if !efn.args.is_empty() {
385 write!(out, ", ");
386 }
David Tolnay99642622020-03-25 13:07:35 -0700387 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400388 write!(out, "*return$");
389 }
390 writeln!(out, ") noexcept {{");
391 write!(out, " ");
392 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700393 match &efn.receiver {
394 None => write!(out, "(*{}$)(", efn.ident),
395 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
396 }
David Tolnay7db73692019-10-20 14:51:12 -0400397 for (i, arg) in efn.args.iter().enumerate() {
398 if i > 0 {
399 write!(out, ", ");
400 }
401 write_type(out, &arg.ty);
402 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700403 write!(out, ")");
404 match &efn.receiver {
405 Some(Receiver { mutability: None, ident: _ }) => write!(out, " const"),
406 _ => {},
407 }
408 write!(out, " = ");
409 match &efn.receiver {
410 None => write!(out, "{}", efn.ident),
411 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
412 }
413 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400414 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700415 if efn.throws {
416 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700417 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700418 writeln!(out, " [&] {{");
419 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700420 }
David Tolnay7db73692019-10-20 14:51:12 -0400421 if indirect_return {
422 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700423 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400424 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700425 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400426 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700427 }
428 match &efn.ret {
429 Some(Type::Ref(_)) => write!(out, "&"),
430 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700431 Some(Type::SliceRefU8(_)) if !indirect_return => {
432 write!(out, "::rust::Slice<uint8_t>::Repr(")
433 }
David Tolnay99642622020-03-25 13:07:35 -0700434 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400435 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700436 match &efn.receiver {
437 None => write!(out, "{}$(", efn.ident),
438 Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident),
439 }
David Tolnay7db73692019-10-20 14:51:12 -0400440 for (i, arg) in efn.args.iter().enumerate() {
441 if i > 0 {
442 write!(out, ", ");
443 }
444 if let Type::RustBox(_) = &arg.ty {
445 write_type(out, &arg.ty);
446 write!(out, "::from_raw({})", arg.ident);
447 } else if let Type::UniquePtr(_) = &arg.ty {
448 write_type(out, &arg.ty);
449 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800450 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800451 write!(
452 out,
453 "::rust::String(::rust::unsafe_bitcopy, *{})",
454 arg.ident,
455 );
David Tolnay7db73692019-10-20 14:51:12 -0400456 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700457 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800458 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400459 } else {
460 write!(out, "{}", arg.ident);
461 }
462 }
463 write!(out, ")");
464 match &efn.ret {
465 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
466 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700467 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400468 _ => {}
469 }
470 if indirect_return {
471 write!(out, ")");
472 }
473 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700474 if efn.throws {
475 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700476 writeln!(out, " throw$.ptr = nullptr;");
477 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700478 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700479 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700480 writeln!(
481 out,
David Tolnay5d121442020-03-17 22:14:40 -0700482 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700483 );
David Tolnay5d121442020-03-17 22:14:40 -0700484 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700485 writeln!(out, " return throw$;");
486 }
David Tolnay7db73692019-10-20 14:51:12 -0400487 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700488 for arg in &efn.args {
489 if let Type::Fn(f) = &arg.ty {
490 let var = &arg.ident;
491 write_function_pointer_trampoline(out, efn, var, f, types);
492 }
493 }
494}
495
496fn write_function_pointer_trampoline(
497 out: &mut OutFile,
498 efn: &ExternFn,
499 var: &Ident,
500 f: &Signature,
501 types: &Types,
502) {
503 out.next_section();
504 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
505 let indirect_call = true;
506 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
507
508 out.next_section();
509 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
510 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400511}
512
513fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700514 let receiver_type = match &efn.receiver {
515 Some(base) => base.ident.to_string(),
516 None => "_".to_string(),
517 };
518 let link_name = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident);
David Tolnay75dca2e2020-03-25 20:17:52 -0700519 let indirect_call = false;
520 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
521}
522
523fn write_rust_function_decl_impl(
524 out: &mut OutFile,
525 link_name: &str,
526 sig: &Signature,
527 types: &Types,
528 indirect_call: bool,
529) {
530 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700531 write!(out, "::rust::Str::Repr ");
532 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700533 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700534 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 write!(out, "{}(", link_name);
536 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700537 if let Some(base) = &sig.receiver {
538 write!(out, "{} &__receiver$", base.ident);
539 needs_comma = true;
540 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700541 for arg in &sig.args {
542 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400543 write!(out, ", ");
544 }
545 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700546 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400547 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 if indirect_return(sig, types) {
549 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400550 write!(out, ", ");
551 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400553 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 needs_comma = true;
555 }
556 if indirect_call {
557 if needs_comma {
558 write!(out, ", ");
559 }
560 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400561 }
562 writeln!(out, ") noexcept;");
563}
564
565fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400566 for line in efn.doc.to_string().lines() {
567 writeln!(out, "//{}", line);
568 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 let local_name = efn.ident.to_string();
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700570 let receiver_type = match &efn.receiver {
571 Some(base) => base.ident.to_string(),
572 None => "_".to_string(),
573 };
574 let invoke = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident);
David Tolnay75dca2e2020-03-25 20:17:52 -0700575 let indirect_call = false;
576 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
577}
578
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700579fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 out: &mut OutFile,
581 local_name: &str,
582 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700583 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700584 indirect_call: bool,
585) {
586 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700587 if let Some(base) = receiver {
588 write!(out, "{}::", base.ident);
589 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700590 write!(out, "{}(", local_name);
591 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400592 if i > 0 {
593 write!(out, ", ");
594 }
595 write_type_space(out, &arg.ty);
596 write!(out, "{}", arg.ident);
597 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 if indirect_call {
599 if !sig.args.is_empty() {
600 write!(out, ", ");
601 }
602 write!(out, "void *extern$");
603 }
David Tolnay1e548172020-03-16 13:37:09 -0700604 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700606 write!(out, " noexcept");
607 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700608}
609
610fn write_rust_function_shim_impl(
611 out: &mut OutFile,
612 local_name: &str,
613 sig: &Signature,
614 types: &Types,
615 invoke: &str,
616 indirect_call: bool,
617) {
618 if out.header && sig.receiver.is_some() {
619 // We've already defined this inside the struct.
620 return;
621 }
622 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400623 if out.header {
624 writeln!(out, ";");
625 } else {
626 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800628 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700629 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800630 write!(out, " ::rust::ManuallyDrop<");
631 write_type(out, &arg.ty);
632 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
633 }
634 }
David Tolnay7db73692019-10-20 14:51:12 -0400635 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700636 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400637 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800638 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800640 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400641 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700642 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400643 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800644 match ret {
645 Type::RustBox(_) => {
646 write_type(out, ret);
647 write!(out, "::from_raw(");
648 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800649 Type::UniquePtr(_) => {
650 write_type(out, ret);
651 write!(out, "(");
652 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800653 Type::Ref(_) => write!(out, "*"),
654 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800655 }
David Tolnay7db73692019-10-20 14:51:12 -0400656 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700657 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700658 write!(out, "::rust::Str::Repr error$ = ");
659 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 write!(out, "{}(", invoke);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700661 if let Some(_) = &sig.receiver {
662 write!(out, "*this");
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700665 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400666 write!(out, ", ");
667 }
David Tolnaybaae4432020-03-01 20:20:10 -0800668 match &arg.ty {
669 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700670 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800671 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
672 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
674 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800675 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800676 Type::RustBox(_) => write!(out, ".into_raw()"),
677 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700678 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800679 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800680 _ => {}
681 }
David Tolnay7db73692019-10-20 14:51:12 -0400682 }
683 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700684 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400685 write!(out, ", ");
686 }
David Tolnay09011c32020-03-06 14:40:28 -0800687 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400688 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700689 if indirect_call {
690 if !sig.args.is_empty() || indirect_return {
691 write!(out, ", ");
692 }
693 write!(out, "extern$");
694 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800695 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700696 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800697 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800698 write!(out, ")");
699 }
700 }
701 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700702 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700703 writeln!(out, " if (error$.ptr) {{");
704 writeln!(out, " throw ::rust::Error(error$);");
705 writeln!(out, " }}");
706 }
David Tolnay7db73692019-10-20 14:51:12 -0400707 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700708 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800709 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400710 }
711 writeln!(out, "}}");
712 }
713}
714
715fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
716 match ty {
717 None => write!(out, "void "),
718 Some(ty) => write_type_space(out, ty),
719 }
720}
721
David Tolnay75dca2e2020-03-25 20:17:52 -0700722fn indirect_return(sig: &Signature, types: &Types) -> bool {
723 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700724 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700725 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700726}
727
David Tolnay99642622020-03-25 13:07:35 -0700728fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
729 match ty {
730 Type::RustBox(ty) | Type::UniquePtr(ty) => {
731 write_type_space(out, &ty.inner);
732 write!(out, "*");
733 }
734 Type::Ref(ty) => {
735 if ty.mutability.is_none() {
736 write!(out, "const ");
737 }
738 write_type(out, &ty.inner);
739 write!(out, " *");
740 }
741 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700742 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700743 _ => write_type(out, ty),
744 }
745}
746
747fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
748 write_indirect_return_type(out, ty);
749 match ty {
750 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700751 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700752 _ => write_space_after_type(out, ty),
753 }
754}
755
756fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400757 match ty {
758 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
759 write_type_space(out, &ty.inner);
760 write!(out, "*");
761 }
David Tolnay4a441222020-01-25 16:24:27 -0800762 Some(Type::Ref(ty)) => {
763 if ty.mutability.is_none() {
764 write!(out, "const ");
765 }
766 write_type(out, &ty.inner);
767 write!(out, " *");
768 }
David Tolnay750755e2020-03-01 13:04:08 -0800769 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700770 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400771 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
772 _ => write_return_type(out, ty),
773 }
774}
775
776fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
777 match &arg.ty {
778 Type::RustBox(ty) | Type::UniquePtr(ty) => {
779 write_type_space(out, &ty.inner);
780 write!(out, "*");
781 }
David Tolnay750755e2020-03-01 13:04:08 -0800782 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700783 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400784 _ => write_type_space(out, &arg.ty),
785 }
786 if types.needs_indirect_abi(&arg.ty) {
787 write!(out, "*");
788 }
789 write!(out, "{}", arg.ident);
790}
791
792fn write_type(out: &mut OutFile, ty: &Type) {
793 match ty {
794 Type::Ident(ident) => match Atom::from(ident) {
795 Some(Bool) => write!(out, "bool"),
796 Some(U8) => write!(out, "uint8_t"),
797 Some(U16) => write!(out, "uint16_t"),
798 Some(U32) => write!(out, "uint32_t"),
799 Some(U64) => write!(out, "uint64_t"),
800 Some(Usize) => write!(out, "size_t"),
801 Some(I8) => write!(out, "int8_t"),
802 Some(I16) => write!(out, "int16_t"),
803 Some(I32) => write!(out, "int32_t"),
804 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700805 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700806 Some(F32) => write!(out, "float"),
807 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800808 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800809 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400810 None => write!(out, "{}", ident),
811 },
812 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800813 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400814 write_type(out, &ty.inner);
815 write!(out, ">");
816 }
817 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800818 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400819 write_type(out, &ptr.inner);
820 write!(out, ">");
821 }
822 Type::Ref(r) => {
823 if r.mutability.is_none() {
824 write!(out, "const ");
825 }
826 write_type(out, &r.inner);
827 write!(out, " &");
828 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700829 Type::Slice(_) => {
830 // For now, only U8 slices are supported, which are covered separately below
831 unreachable!()
832 }
David Tolnay7db73692019-10-20 14:51:12 -0400833 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800834 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400835 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700836 Type::SliceRefU8(_) => {
837 write!(out, "::rust::Slice<uint8_t>");
838 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700839 Type::Fn(f) => {
840 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
841 match &f.ret {
842 Some(ret) => write_type(out, ret),
843 None => write!(out, "void"),
844 }
845 write!(out, "(");
846 for (i, arg) in f.args.iter().enumerate() {
847 if i > 0 {
848 write!(out, ", ");
849 }
850 write_type(out, &arg.ty);
851 }
852 write!(out, ")>");
853 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700854 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400855 }
856}
857
858fn write_type_space(out: &mut OutFile, ty: &Type) {
859 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700860 write_space_after_type(out, ty);
861}
862
863fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400864 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700865 Type::Ident(_)
866 | Type::RustBox(_)
867 | Type::UniquePtr(_)
868 | Type::Str(_)
869 | Type::SliceRefU8(_)
870 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400871 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700872 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400873 }
874}
875
876fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
877 fn allow_unique_ptr(ident: &Ident) -> bool {
878 Atom::from(ident).is_none()
879 }
880
881 out.begin_block("extern \"C\"");
882 for ty in types {
883 if let Type::RustBox(ty) = ty {
884 if let Type::Ident(inner) = &ty.inner {
885 out.next_section();
886 write_rust_box_extern(out, inner);
887 }
888 } else if let Type::UniquePtr(ptr) = ty {
889 if let Type::Ident(inner) = &ptr.inner {
890 if allow_unique_ptr(inner) {
891 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700892 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400893 }
894 }
895 }
896 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800897 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400898
David Tolnay750755e2020-03-01 13:04:08 -0800899 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700900 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400901 for ty in types {
902 if let Type::RustBox(ty) = ty {
903 if let Type::Ident(inner) = &ty.inner {
904 write_rust_box_impl(out, inner);
905 }
906 }
907 }
David Tolnay8c730492020-03-13 01:29:06 -0700908 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800909 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400910}
911
912fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
913 let mut inner = String::new();
914 for name in &out.namespace {
915 inner += name;
916 inner += "::";
917 }
918 inner += &ident.to_string();
919 let instance = inner.replace("::", "$");
920
David Tolnay8c730492020-03-13 01:29:06 -0700921 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
922 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400923 writeln!(
924 out,
David Tolnay8c730492020-03-13 01:29:06 -0700925 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400926 instance, inner,
927 );
928 writeln!(
929 out,
David Tolnay8c730492020-03-13 01:29:06 -0700930 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400931 instance, inner,
932 );
David Tolnay8c730492020-03-13 01:29:06 -0700933 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400934}
935
936fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
937 let mut inner = String::new();
938 for name in &out.namespace {
939 inner += name;
940 inner += "::";
941 }
942 inner += &ident.to_string();
943 let instance = inner.replace("::", "$");
944
945 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800946 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700947 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400948 writeln!(out, "}}");
949
950 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800951 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700952 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400953 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400954}
955
David Tolnay53838912020-04-09 20:56:44 -0700956fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700957 out.include.utility = true;
958
David Tolnay7db73692019-10-20 14:51:12 -0400959 let mut inner = String::new();
960 for name in &out.namespace {
961 inner += name;
962 inner += "::";
963 }
964 inner += &ident.to_string();
965 let instance = inner.replace("::", "$");
966
David Tolnay8c730492020-03-13 01:29:06 -0700967 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
968 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400969 writeln!(
970 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800971 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400972 inner,
973 );
974 writeln!(
975 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800976 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400977 inner,
978 );
979 writeln!(
980 out,
David Tolnay8c730492020-03-13 01:29:06 -0700981 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400982 instance, inner,
983 );
David Tolnay7e219b82020-03-01 13:14:51 -0800984 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400985 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700986 if types.structs.contains_key(ident) {
987 writeln!(
988 out,
989 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
990 instance, inner, inner,
991 );
992 writeln!(
993 out,
994 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
995 inner, inner,
996 );
997 writeln!(out, "}}");
998 }
David Tolnay7db73692019-10-20 14:51:12 -0400999 writeln!(
1000 out,
David Tolnay8c730492020-03-13 01:29:06 -07001001 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001002 instance, inner, inner,
1003 );
David Tolnay7e219b82020-03-01 13:14:51 -08001004 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001005 writeln!(out, "}}");
1006 writeln!(
1007 out,
David Tolnay8c730492020-03-13 01:29:06 -07001008 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001009 inner, instance, inner,
1010 );
1011 writeln!(out, " return ptr.get();");
1012 writeln!(out, "}}");
1013 writeln!(
1014 out,
David Tolnay8c730492020-03-13 01:29:06 -07001015 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001016 inner, instance, inner,
1017 );
1018 writeln!(out, " return ptr.release();");
1019 writeln!(out, "}}");
1020 writeln!(
1021 out,
David Tolnay8c730492020-03-13 01:29:06 -07001022 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001023 instance, inner,
1024 );
1025 writeln!(out, " ptr->~unique_ptr();");
1026 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001027 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001028}