blob: b8d17dfd29daa8f877d9c0945e2015b6602510eb [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 Galenson187588e2020-04-17 16:19:54 -0700329 writeln!(out, " {}() = delete;", ety.ident);
330 writeln!(out, " {}(const {}&) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700331 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700332 write!(out, " ");
333 let sig = &method.sig;
334 let local_name = method.ident.to_string();
335 write_rust_function_shim_decl(out, &local_name, sig, None, false);
336 writeln!(out, ";");
337 }
338 writeln!(out, "}};");
339}
340
David Tolnayebef4a22020-03-17 15:33:47 -0700341fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
342 let mut has_cxx_throws = false;
343 for api in apis {
344 if let Api::CxxFunction(efn) = api {
345 if efn.throws {
346 has_cxx_throws = true;
347 break;
348 }
349 }
350 }
351
352 if has_cxx_throws {
353 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700354 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700355 out,
356 "const char *cxxbridge02$exception(const char *, size_t);",
357 );
358 }
359}
360
David Tolnay7db73692019-10-20 14:51:12 -0400361fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700362 if efn.throws {
363 write!(out, "::rust::Str::Repr ");
364 } else {
David Tolnay99642622020-03-25 13:07:35 -0700365 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700366 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700367 let receiver_type = match &efn.receiver {
368 Some(base) => base.ident.to_string(),
369 None => "_".to_string(),
370 };
371 write!(out, "{}cxxbridge02${}${}(", out.namespace, receiver_type, efn.ident);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700372 if let Some(base) = &efn.receiver {
373 write!(out, "{} *__receiver$", base.ident);
374 }
David Tolnay7db73692019-10-20 14:51:12 -0400375 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700376 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400377 write!(out, ", ");
378 }
David Tolnaya46a2372020-03-06 10:03:48 -0800379 if arg.ty == RustString {
380 write!(out, "const ");
381 }
David Tolnay7db73692019-10-20 14:51:12 -0400382 write_extern_arg(out, arg, types);
383 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700384 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400385 if indirect_return {
386 if !efn.args.is_empty() {
387 write!(out, ", ");
388 }
David Tolnay99642622020-03-25 13:07:35 -0700389 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400390 write!(out, "*return$");
391 }
392 writeln!(out, ") noexcept {{");
393 write!(out, " ");
394 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700395 match &efn.receiver {
396 None => write!(out, "(*{}$)(", efn.ident),
397 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
398 }
David Tolnay7db73692019-10-20 14:51:12 -0400399 for (i, arg) in efn.args.iter().enumerate() {
400 if i > 0 {
401 write!(out, ", ");
402 }
403 write_type(out, &arg.ty);
404 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700405 write!(out, ")");
406 match &efn.receiver {
407 Some(Receiver { mutability: None, ident: _ }) => write!(out, " const"),
408 _ => {},
409 }
410 write!(out, " = ");
411 match &efn.receiver {
412 None => write!(out, "{}", efn.ident),
413 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
414 }
415 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400416 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700417 if efn.throws {
418 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700419 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700420 writeln!(out, " [&] {{");
421 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700422 }
David Tolnay7db73692019-10-20 14:51:12 -0400423 if indirect_return {
424 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700425 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400426 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700427 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700429 }
430 match &efn.ret {
431 Some(Type::Ref(_)) => write!(out, "&"),
432 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700433 Some(Type::SliceRefU8(_)) if !indirect_return => {
434 write!(out, "::rust::Slice<uint8_t>::Repr(")
435 }
David Tolnay99642622020-03-25 13:07:35 -0700436 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400437 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700438 match &efn.receiver {
439 None => write!(out, "{}$(", efn.ident),
440 Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident),
441 }
David Tolnay7db73692019-10-20 14:51:12 -0400442 for (i, arg) in efn.args.iter().enumerate() {
443 if i > 0 {
444 write!(out, ", ");
445 }
446 if let Type::RustBox(_) = &arg.ty {
447 write_type(out, &arg.ty);
448 write!(out, "::from_raw({})", arg.ident);
449 } else if let Type::UniquePtr(_) = &arg.ty {
450 write_type(out, &arg.ty);
451 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800452 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800453 write!(
454 out,
455 "::rust::String(::rust::unsafe_bitcopy, *{})",
456 arg.ident,
457 );
David Tolnay7db73692019-10-20 14:51:12 -0400458 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700459 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800460 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400461 } else {
462 write!(out, "{}", arg.ident);
463 }
464 }
465 write!(out, ")");
466 match &efn.ret {
467 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
468 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700469 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400470 _ => {}
471 }
472 if indirect_return {
473 write!(out, ")");
474 }
475 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700476 if efn.throws {
477 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700478 writeln!(out, " throw$.ptr = nullptr;");
479 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700480 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700481 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700482 writeln!(
483 out,
David Tolnay5d121442020-03-17 22:14:40 -0700484 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700485 );
David Tolnay5d121442020-03-17 22:14:40 -0700486 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700487 writeln!(out, " return throw$;");
488 }
David Tolnay7db73692019-10-20 14:51:12 -0400489 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700490 for arg in &efn.args {
491 if let Type::Fn(f) = &arg.ty {
492 let var = &arg.ident;
493 write_function_pointer_trampoline(out, efn, var, f, types);
494 }
495 }
496}
497
498fn write_function_pointer_trampoline(
499 out: &mut OutFile,
500 efn: &ExternFn,
501 var: &Ident,
502 f: &Signature,
503 types: &Types,
504) {
505 out.next_section();
506 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
507 let indirect_call = true;
508 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
509
510 out.next_section();
511 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
512 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400513}
514
515fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700516 let receiver_type = match &efn.receiver {
517 Some(base) => base.ident.to_string(),
518 None => "_".to_string(),
519 };
520 let link_name = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident);
David Tolnay75dca2e2020-03-25 20:17:52 -0700521 let indirect_call = false;
522 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
523}
524
525fn write_rust_function_decl_impl(
526 out: &mut OutFile,
527 link_name: &str,
528 sig: &Signature,
529 types: &Types,
530 indirect_call: bool,
531) {
532 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700533 write!(out, "::rust::Str::Repr ");
534 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700536 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700537 write!(out, "{}(", link_name);
538 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700539 if let Some(base) = &sig.receiver {
540 write!(out, "{} &__receiver$", base.ident);
541 needs_comma = true;
542 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700543 for arg in &sig.args {
544 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400545 write!(out, ", ");
546 }
547 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400549 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700550 if indirect_return(sig, types) {
551 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400552 write!(out, ", ");
553 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400555 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700556 needs_comma = true;
557 }
558 if indirect_call {
559 if needs_comma {
560 write!(out, ", ");
561 }
562 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400563 }
564 writeln!(out, ") noexcept;");
565}
566
567fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400568 for line in efn.doc.to_string().lines() {
569 writeln!(out, "//{}", line);
570 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700571 let local_name = efn.ident.to_string();
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700572 let receiver_type = match &efn.receiver {
573 Some(base) => base.ident.to_string(),
574 None => "_".to_string(),
575 };
576 let invoke = format!("{}cxxbridge02${}${}", out.namespace, receiver_type, efn.ident);
David Tolnay75dca2e2020-03-25 20:17:52 -0700577 let indirect_call = false;
578 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
579}
580
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700581fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 out: &mut OutFile,
583 local_name: &str,
584 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700585 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 indirect_call: bool,
587) {
588 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700589 if let Some(base) = receiver {
590 write!(out, "{}::", base.ident);
591 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 write!(out, "{}(", local_name);
593 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400594 if i > 0 {
595 write!(out, ", ");
596 }
597 write_type_space(out, &arg.ty);
598 write!(out, "{}", arg.ident);
599 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 if indirect_call {
601 if !sig.args.is_empty() {
602 write!(out, ", ");
603 }
604 write!(out, "void *extern$");
605 }
David Tolnay1e548172020-03-16 13:37:09 -0700606 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700608 write!(out, " noexcept");
609 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700610}
611
612fn write_rust_function_shim_impl(
613 out: &mut OutFile,
614 local_name: &str,
615 sig: &Signature,
616 types: &Types,
617 invoke: &str,
618 indirect_call: bool,
619) {
620 if out.header && sig.receiver.is_some() {
621 // We've already defined this inside the struct.
622 return;
623 }
624 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400625 if out.header {
626 writeln!(out, ";");
627 } else {
628 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700629 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800630 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700631 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800632 write!(out, " ::rust::ManuallyDrop<");
633 write_type(out, &arg.ty);
634 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
635 }
636 }
David Tolnay7db73692019-10-20 14:51:12 -0400637 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400639 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800640 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700641 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800642 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400643 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400645 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800646 match ret {
647 Type::RustBox(_) => {
648 write_type(out, ret);
649 write!(out, "::from_raw(");
650 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800651 Type::UniquePtr(_) => {
652 write_type(out, ret);
653 write!(out, "(");
654 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800655 Type::Ref(_) => write!(out, "*"),
656 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800657 }
David Tolnay7db73692019-10-20 14:51:12 -0400658 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700659 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700660 write!(out, "::rust::Str::Repr error$ = ");
661 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700662 write!(out, "{}(", invoke);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700663 if let Some(_) = &sig.receiver {
664 write!(out, "*this");
665 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700666 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700667 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400668 write!(out, ", ");
669 }
David Tolnaybaae4432020-03-01 20:20:10 -0800670 match &arg.ty {
671 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700672 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800673 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
674 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400675 }
676 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800677 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800678 Type::RustBox(_) => write!(out, ".into_raw()"),
679 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700680 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800681 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800682 _ => {}
683 }
David Tolnay7db73692019-10-20 14:51:12 -0400684 }
685 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700686 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400687 write!(out, ", ");
688 }
David Tolnay09011c32020-03-06 14:40:28 -0800689 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400690 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700691 if indirect_call {
692 if !sig.args.is_empty() || indirect_return {
693 write!(out, ", ");
694 }
695 write!(out, "extern$");
696 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800697 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700698 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800699 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800700 write!(out, ")");
701 }
702 }
703 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700704 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700705 writeln!(out, " if (error$.ptr) {{");
706 writeln!(out, " throw ::rust::Error(error$);");
707 writeln!(out, " }}");
708 }
David Tolnay7db73692019-10-20 14:51:12 -0400709 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700710 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800711 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400712 }
713 writeln!(out, "}}");
714 }
715}
716
717fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
718 match ty {
719 None => write!(out, "void "),
720 Some(ty) => write_type_space(out, ty),
721 }
722}
723
David Tolnay75dca2e2020-03-25 20:17:52 -0700724fn indirect_return(sig: &Signature, types: &Types) -> bool {
725 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700726 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700727 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700728}
729
David Tolnay99642622020-03-25 13:07:35 -0700730fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
731 match ty {
732 Type::RustBox(ty) | Type::UniquePtr(ty) => {
733 write_type_space(out, &ty.inner);
734 write!(out, "*");
735 }
736 Type::Ref(ty) => {
737 if ty.mutability.is_none() {
738 write!(out, "const ");
739 }
740 write_type(out, &ty.inner);
741 write!(out, " *");
742 }
743 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700744 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700745 _ => write_type(out, ty),
746 }
747}
748
749fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
750 write_indirect_return_type(out, ty);
751 match ty {
752 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700753 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700754 _ => write_space_after_type(out, ty),
755 }
756}
757
758fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400759 match ty {
760 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
761 write_type_space(out, &ty.inner);
762 write!(out, "*");
763 }
David Tolnay4a441222020-01-25 16:24:27 -0800764 Some(Type::Ref(ty)) => {
765 if ty.mutability.is_none() {
766 write!(out, "const ");
767 }
768 write_type(out, &ty.inner);
769 write!(out, " *");
770 }
David Tolnay750755e2020-03-01 13:04:08 -0800771 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700772 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400773 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
774 _ => write_return_type(out, ty),
775 }
776}
777
778fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
779 match &arg.ty {
780 Type::RustBox(ty) | Type::UniquePtr(ty) => {
781 write_type_space(out, &ty.inner);
782 write!(out, "*");
783 }
David Tolnay750755e2020-03-01 13:04:08 -0800784 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700785 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400786 _ => write_type_space(out, &arg.ty),
787 }
788 if types.needs_indirect_abi(&arg.ty) {
789 write!(out, "*");
790 }
791 write!(out, "{}", arg.ident);
792}
793
794fn write_type(out: &mut OutFile, ty: &Type) {
795 match ty {
796 Type::Ident(ident) => match Atom::from(ident) {
797 Some(Bool) => write!(out, "bool"),
798 Some(U8) => write!(out, "uint8_t"),
799 Some(U16) => write!(out, "uint16_t"),
800 Some(U32) => write!(out, "uint32_t"),
801 Some(U64) => write!(out, "uint64_t"),
802 Some(Usize) => write!(out, "size_t"),
803 Some(I8) => write!(out, "int8_t"),
804 Some(I16) => write!(out, "int16_t"),
805 Some(I32) => write!(out, "int32_t"),
806 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700807 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700808 Some(F32) => write!(out, "float"),
809 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800810 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800811 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400812 None => write!(out, "{}", ident),
813 },
814 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800815 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400816 write_type(out, &ty.inner);
817 write!(out, ">");
818 }
819 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800820 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400821 write_type(out, &ptr.inner);
822 write!(out, ">");
823 }
824 Type::Ref(r) => {
825 if r.mutability.is_none() {
826 write!(out, "const ");
827 }
828 write_type(out, &r.inner);
829 write!(out, " &");
830 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700831 Type::Slice(_) => {
832 // For now, only U8 slices are supported, which are covered separately below
833 unreachable!()
834 }
David Tolnay7db73692019-10-20 14:51:12 -0400835 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800836 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400837 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700838 Type::SliceRefU8(_) => {
839 write!(out, "::rust::Slice<uint8_t>");
840 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700841 Type::Fn(f) => {
842 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
843 match &f.ret {
844 Some(ret) => write_type(out, ret),
845 None => write!(out, "void"),
846 }
847 write!(out, "(");
848 for (i, arg) in f.args.iter().enumerate() {
849 if i > 0 {
850 write!(out, ", ");
851 }
852 write_type(out, &arg.ty);
853 }
854 write!(out, ")>");
855 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700856 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400857 }
858}
859
860fn write_type_space(out: &mut OutFile, ty: &Type) {
861 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700862 write_space_after_type(out, ty);
863}
864
865fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400866 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700867 Type::Ident(_)
868 | Type::RustBox(_)
869 | Type::UniquePtr(_)
870 | Type::Str(_)
871 | Type::SliceRefU8(_)
872 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400873 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700874 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400875 }
876}
877
878fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
879 fn allow_unique_ptr(ident: &Ident) -> bool {
880 Atom::from(ident).is_none()
881 }
882
883 out.begin_block("extern \"C\"");
884 for ty in types {
885 if let Type::RustBox(ty) = ty {
886 if let Type::Ident(inner) = &ty.inner {
887 out.next_section();
888 write_rust_box_extern(out, inner);
889 }
890 } else if let Type::UniquePtr(ptr) = ty {
891 if let Type::Ident(inner) = &ptr.inner {
892 if allow_unique_ptr(inner) {
893 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700894 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400895 }
896 }
897 }
898 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800899 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400900
David Tolnay750755e2020-03-01 13:04:08 -0800901 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700902 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400903 for ty in types {
904 if let Type::RustBox(ty) = ty {
905 if let Type::Ident(inner) = &ty.inner {
906 write_rust_box_impl(out, inner);
907 }
908 }
909 }
David Tolnay8c730492020-03-13 01:29:06 -0700910 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800911 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400912}
913
914fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
915 let mut inner = String::new();
916 for name in &out.namespace {
917 inner += name;
918 inner += "::";
919 }
920 inner += &ident.to_string();
921 let instance = inner.replace("::", "$");
922
David Tolnay8c730492020-03-13 01:29:06 -0700923 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
924 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400925 writeln!(
926 out,
David Tolnay8c730492020-03-13 01:29:06 -0700927 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400928 instance, inner,
929 );
930 writeln!(
931 out,
David Tolnay8c730492020-03-13 01:29:06 -0700932 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400933 instance, inner,
934 );
David Tolnay8c730492020-03-13 01:29:06 -0700935 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400936}
937
938fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
939 let mut inner = String::new();
940 for name in &out.namespace {
941 inner += name;
942 inner += "::";
943 }
944 inner += &ident.to_string();
945 let instance = inner.replace("::", "$");
946
947 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800948 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700949 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400950 writeln!(out, "}}");
951
952 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800953 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700954 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400955 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400956}
957
David Tolnay53838912020-04-09 20:56:44 -0700958fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700959 out.include.utility = true;
960
David Tolnay7db73692019-10-20 14:51:12 -0400961 let mut inner = String::new();
962 for name in &out.namespace {
963 inner += name;
964 inner += "::";
965 }
966 inner += &ident.to_string();
967 let instance = inner.replace("::", "$");
968
David Tolnay8c730492020-03-13 01:29:06 -0700969 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
970 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400971 writeln!(
972 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800973 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400974 inner,
975 );
976 writeln!(
977 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800978 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400979 inner,
980 );
981 writeln!(
982 out,
David Tolnay8c730492020-03-13 01:29:06 -0700983 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400984 instance, inner,
985 );
David Tolnay7e219b82020-03-01 13:14:51 -0800986 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400987 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700988 if types.structs.contains_key(ident) {
989 writeln!(
990 out,
991 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
992 instance, inner, inner,
993 );
994 writeln!(
995 out,
996 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
997 inner, inner,
998 );
999 writeln!(out, "}}");
1000 }
David Tolnay7db73692019-10-20 14:51:12 -04001001 writeln!(
1002 out,
David Tolnay8c730492020-03-13 01:29:06 -07001003 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001004 instance, inner, inner,
1005 );
David Tolnay7e219b82020-03-01 13:14:51 -08001006 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001007 writeln!(out, "}}");
1008 writeln!(
1009 out,
David Tolnay8c730492020-03-13 01:29:06 -07001010 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001011 inner, instance, inner,
1012 );
1013 writeln!(out, " return ptr.get();");
1014 writeln!(out, "}}");
1015 writeln!(
1016 out,
David Tolnay8c730492020-03-13 01:29:06 -07001017 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001018 inner, instance, inner,
1019 );
1020 writeln!(out, " return ptr.release();");
1021 writeln!(out, "}}");
1022 writeln!(
1023 out,
David Tolnay8c730492020-03-13 01:29:06 -07001024 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001025 instance, inner,
1026 );
1027 writeln!(out, " ptr->~unique_ptr();");
1028 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001029 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001030}