blob: f930d5208fec96b910eb3cdc030df50775daba55 [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, *};
David Tolnay75dca2e2020-03-25 20:17:52 -07005use crate::syntax::{Api, ExternFn, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04006use proc_macro2::Ident;
7
David Tolnay33d30292020-03-18 18:02:02 -07008pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -07009 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070010 apis: &[Api],
11 types: &Types,
12 opt: Opt,
13 header: bool,
14) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040015 let mut out_file = OutFile::new(namespace.clone(), header);
16 let out = &mut out_file;
17
18 if header {
19 writeln!(out, "#pragma once");
20 }
21
David Tolnay33d30292020-03-18 18:02:02 -070022 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023 for api in apis {
24 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080025 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040026 }
27 }
28
29 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080030 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay7db73692019-10-20 14:51:12 -040032 out.next_section();
33 for name in &namespace {
34 writeln!(out, "namespace {} {{", name);
35 }
36
David Tolnay7db73692019-10-20 14:51:12 -040037 out.next_section();
38 for api in apis {
39 match api {
40 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080041 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
42 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040043 _ => {}
44 }
45 }
46
47 for api in apis {
48 if let Api::Struct(strct) = api {
49 out.next_section();
50 write_struct(out, strct);
51 }
52 }
53
54 if !header {
55 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070056 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
58 let (efn, write): (_, fn(_, _, _)) = match api {
59 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
60 Api::RustFunction(efn) => (efn, write_rust_function_decl),
61 _ => continue,
62 };
63 out.next_section();
64 write(out, efn, types);
65 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080066 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68
69 for api in apis {
70 if let Api::RustFunction(efn) = api {
71 out.next_section();
72 write_rust_function_shim(out, efn, types);
73 }
74 }
75
76 out.next_section();
77 for name in namespace.iter().rev() {
78 writeln!(out, "}} // namespace {}", name);
79 }
80
81 if !header {
82 out.next_section();
83 write_generic_instantiations(out, types);
84 }
85
David Tolnay9c68b1a2020-03-06 11:12:55 -080086 out.prepend(out.include.to_string());
87
David Tolnay7db73692019-10-20 14:51:12 -040088 out_file
89}
90
91fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040092 for ty in types {
93 match ty {
94 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -070095 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
96 | Some(I64) => out.include.cstdint = true,
97 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -080098 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -070099 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400100 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800101 Type::RustBox(_) => out.include.type_traits = true,
102 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -0400103 _ => {}
104 }
105 }
David Tolnay7db73692019-10-20 14:51:12 -0400106}
107
David Tolnayf51447e2020-03-06 14:14:27 -0800108fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700109 let mut needs_rust_string = false;
110 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400111 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700112 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700113 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400114 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700115 match ty {
116 Type::RustBox(_) => {
117 out.include.type_traits = true;
118 needs_rust_box = true;
119 }
120 Type::Str(_) => {
121 out.include.cstdint = true;
122 out.include.string = true;
123 needs_rust_str = true;
124 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700125 Type::Fn(_) => {
126 needs_rust_fn = true;
127 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700128 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700129 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700130 needs_rust_isize = true;
131 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700132 ty if ty == RustString => {
133 out.include.array = true;
134 out.include.cstdint = true;
135 out.include.string = true;
136 needs_rust_string = true;
137 }
138 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400139 }
140 }
141
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 let mut needs_rust_error = false;
143 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800144 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800145 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700146 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800147 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700148 match api {
149 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700150 if efn.throws {
151 needs_trycatch = true;
152 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 for arg in &efn.args {
154 if arg.ty == RustString {
155 needs_unsafe_bitcopy = true;
156 break;
157 }
David Tolnay09011c32020-03-06 14:40:28 -0800158 }
159 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700160 Api::RustFunction(efn) if !out.header => {
161 if efn.throws {
162 out.include.exception = true;
163 needs_rust_error = true;
164 }
165 for arg in &efn.args {
166 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
167 needs_manually_drop = true;
168 break;
169 }
170 }
171 if let Some(ret) = &efn.ret {
172 if types.needs_indirect_abi(ret) {
173 needs_maybe_uninit = true;
174 }
David Tolnayf51447e2020-03-06 14:14:27 -0800175 }
176 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800178 }
179 }
180
David Tolnay750755e2020-03-01 13:04:08 -0800181 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700182 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800183
David Tolnayb7a7cb62020-03-17 21:18:40 -0700184 if needs_rust_string
185 || needs_rust_str
186 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700187 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700189 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700190 || needs_unsafe_bitcopy
191 || needs_manually_drop
192 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700193 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700194 {
David Tolnay736cbca2020-03-11 16:49:18 -0700195 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800196 }
197
David Tolnayd1402742020-03-25 22:21:42 -0700198 if needs_rust_string {
199 out.next_section();
200 writeln!(out, "struct unsafe_bitcopy_t;");
201 }
202
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
204 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
205 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700206 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700208 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800210
211 if needs_manually_drop {
212 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700213 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800214 writeln!(out, "template <typename T>");
215 writeln!(out, "union ManuallyDrop {{");
216 writeln!(out, " T value;");
217 writeln!(
218 out,
219 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
220 );
221 writeln!(out, " ~ManuallyDrop() {{}}");
222 writeln!(out, "}};");
223 }
224
David Tolnay09011c32020-03-06 14:40:28 -0800225 if needs_maybe_uninit {
226 out.next_section();
227 writeln!(out, "template <typename T>");
228 writeln!(out, "union MaybeUninit {{");
229 writeln!(out, " T value;");
230 writeln!(out, " MaybeUninit() {{}}");
231 writeln!(out, " ~MaybeUninit() {{}}");
232 writeln!(out, "}};");
233 }
234
David Tolnay3e3e0af2020-03-17 22:42:49 -0700235 out.end_block("namespace cxxbridge02");
236
David Tolnay5d121442020-03-17 22:14:40 -0700237 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700238 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700239 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700240 out.include.type_traits = true;
241 out.include.utility = true;
242 writeln!(out, "class missing {{}};");
243 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700244 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700245 writeln!(out, "template <typename Try, typename Fail>");
246 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700247 writeln!(
248 out,
David Tolnay04722332020-03-18 11:31:54 -0700249 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700250 );
David Tolnay04722332020-03-18 11:31:54 -0700251 writeln!(out, " missing>::value>::type");
252 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700253 writeln!(out, " func();");
254 writeln!(out, "}} catch (const ::std::exception &e) {{");
255 writeln!(out, " fail(e.what());");
256 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700257 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700258 }
259
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800260 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400261}
262
David Tolnayb7a7cb62020-03-17 21:18:40 -0700263fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700264 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700265 if needed {
266 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700267 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700268 if !line.trim_start().starts_with("//") {
269 writeln!(out, "{}", line);
270 }
271 }
272 }
273}
274
David Tolnay7db73692019-10-20 14:51:12 -0400275fn write_struct(out: &mut OutFile, strct: &Struct) {
276 for line in strct.doc.to_string().lines() {
277 writeln!(out, "//{}", line);
278 }
279 writeln!(out, "struct {} final {{", strct.ident);
280 for field in &strct.fields {
281 write!(out, " ");
282 write_type_space(out, &field.ty);
283 writeln!(out, "{};", field.ident);
284 }
285 writeln!(out, "}};");
286}
287
288fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
289 writeln!(out, "struct {};", ident);
290}
291
David Tolnay8861bee2020-01-20 18:39:24 -0800292fn write_struct_using(out: &mut OutFile, ident: &Ident) {
293 writeln!(out, "using {} = {};", ident, ident);
294}
295
David Tolnayebef4a22020-03-17 15:33:47 -0700296fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
297 let mut has_cxx_throws = false;
298 for api in apis {
299 if let Api::CxxFunction(efn) = api {
300 if efn.throws {
301 has_cxx_throws = true;
302 break;
303 }
304 }
305 }
306
307 if has_cxx_throws {
308 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700309 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700310 out,
311 "const char *cxxbridge02$exception(const char *, size_t);",
312 );
313 }
314}
315
David Tolnay7db73692019-10-20 14:51:12 -0400316fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700317 if efn.throws {
318 write!(out, "::rust::Str::Repr ");
319 } else {
David Tolnay99642622020-03-25 13:07:35 -0700320 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700321 }
David Tolnayd815de02020-03-29 21:29:17 -0700322 write!(out, "{}cxxbridge02${}(", out.namespace, efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400323 for (i, arg) in efn.args.iter().enumerate() {
324 if i > 0 {
325 write!(out, ", ");
326 }
David Tolnaya46a2372020-03-06 10:03:48 -0800327 if arg.ty == RustString {
328 write!(out, "const ");
329 }
David Tolnay7db73692019-10-20 14:51:12 -0400330 write_extern_arg(out, arg, types);
331 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700332 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400333 if indirect_return {
334 if !efn.args.is_empty() {
335 write!(out, ", ");
336 }
David Tolnay99642622020-03-25 13:07:35 -0700337 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400338 write!(out, "*return$");
339 }
340 writeln!(out, ") noexcept {{");
341 write!(out, " ");
342 write_return_type(out, &efn.ret);
343 write!(out, "(*{}$)(", efn.ident);
344 for (i, arg) in efn.args.iter().enumerate() {
345 if i > 0 {
346 write!(out, ", ");
347 }
348 write_type(out, &arg.ty);
349 }
350 writeln!(out, ") = {};", efn.ident);
351 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700352 if efn.throws {
353 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700354 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700355 writeln!(out, " [&] {{");
356 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700357 }
David Tolnay7db73692019-10-20 14:51:12 -0400358 if indirect_return {
359 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700360 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400361 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700362 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400363 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700364 }
365 match &efn.ret {
366 Some(Type::Ref(_)) => write!(out, "&"),
367 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700368 Some(Type::SliceRefU8(_)) if !indirect_return => {
369 write!(out, "::rust::Slice<uint8_t>::Repr(")
370 }
David Tolnay99642622020-03-25 13:07:35 -0700371 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400372 }
373 write!(out, "{}$(", efn.ident);
374 for (i, arg) in efn.args.iter().enumerate() {
375 if i > 0 {
376 write!(out, ", ");
377 }
378 if let Type::RustBox(_) = &arg.ty {
379 write_type(out, &arg.ty);
380 write!(out, "::from_raw({})", arg.ident);
381 } else if let Type::UniquePtr(_) = &arg.ty {
382 write_type(out, &arg.ty);
383 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800384 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800385 write!(
386 out,
387 "::rust::String(::rust::unsafe_bitcopy, *{})",
388 arg.ident,
389 );
David Tolnay7db73692019-10-20 14:51:12 -0400390 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700391 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800392 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400393 } else {
394 write!(out, "{}", arg.ident);
395 }
396 }
397 write!(out, ")");
398 match &efn.ret {
399 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
400 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700401 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400402 _ => {}
403 }
404 if indirect_return {
405 write!(out, ")");
406 }
407 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700408 if efn.throws {
409 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700410 writeln!(out, " throw$.ptr = nullptr;");
411 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700412 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700413 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700414 writeln!(
415 out,
David Tolnay5d121442020-03-17 22:14:40 -0700416 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700417 );
David Tolnay5d121442020-03-17 22:14:40 -0700418 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700419 writeln!(out, " return throw$;");
420 }
David Tolnay7db73692019-10-20 14:51:12 -0400421 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700422 for arg in &efn.args {
423 if let Type::Fn(f) = &arg.ty {
424 let var = &arg.ident;
425 write_function_pointer_trampoline(out, efn, var, f, types);
426 }
427 }
428}
429
430fn write_function_pointer_trampoline(
431 out: &mut OutFile,
432 efn: &ExternFn,
433 var: &Ident,
434 f: &Signature,
435 types: &Types,
436) {
437 out.next_section();
438 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
439 let indirect_call = true;
440 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
441
442 out.next_section();
443 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
444 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400445}
446
447fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700448 let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident);
449 let indirect_call = false;
450 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
451}
452
453fn write_rust_function_decl_impl(
454 out: &mut OutFile,
455 link_name: &str,
456 sig: &Signature,
457 types: &Types,
458 indirect_call: bool,
459) {
460 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700461 write!(out, "::rust::Str::Repr ");
462 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700463 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700464 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700465 write!(out, "{}(", link_name);
466 let mut needs_comma = false;
467 for arg in &sig.args {
468 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400469 write!(out, ", ");
470 }
471 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700472 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400473 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700474 if indirect_return(sig, types) {
475 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400476 write!(out, ", ");
477 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700478 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400479 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700480 needs_comma = true;
481 }
482 if indirect_call {
483 if needs_comma {
484 write!(out, ", ");
485 }
486 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400487 }
488 writeln!(out, ") noexcept;");
489}
490
491fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400492 for line in efn.doc.to_string().lines() {
493 writeln!(out, "//{}", line);
494 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700495 let local_name = efn.ident.to_string();
496 let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident);
497 let indirect_call = false;
498 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
499}
500
501fn write_rust_function_shim_impl(
502 out: &mut OutFile,
503 local_name: &str,
504 sig: &Signature,
505 types: &Types,
506 invoke: &str,
507 indirect_call: bool,
508) {
509 write_return_type(out, &sig.ret);
510 write!(out, "{}(", local_name);
511 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400512 if i > 0 {
513 write!(out, ", ");
514 }
515 write_type_space(out, &arg.ty);
516 write!(out, "{}", arg.ident);
517 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700518 if indirect_call {
519 if !sig.args.is_empty() {
520 write!(out, ", ");
521 }
522 write!(out, "void *extern$");
523 }
David Tolnay1e548172020-03-16 13:37:09 -0700524 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700525 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700526 write!(out, " noexcept");
527 }
David Tolnay7db73692019-10-20 14:51:12 -0400528 if out.header {
529 writeln!(out, ";");
530 } else {
531 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700532 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800533 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700534 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800535 write!(out, " ::rust::ManuallyDrop<");
536 write_type(out, &arg.ty);
537 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
538 }
539 }
David Tolnay7db73692019-10-20 14:51:12 -0400540 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700541 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400542 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800543 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700544 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800545 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400546 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700547 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400548 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800549 match ret {
550 Type::RustBox(_) => {
551 write_type(out, ret);
552 write!(out, "::from_raw(");
553 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800554 Type::UniquePtr(_) => {
555 write_type(out, ret);
556 write!(out, "(");
557 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800558 Type::Ref(_) => write!(out, "*"),
559 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800560 }
David Tolnay7db73692019-10-20 14:51:12 -0400561 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700562 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700563 write!(out, "::rust::Str::Repr error$ = ");
564 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 write!(out, "{}(", invoke);
566 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400567 if i > 0 {
568 write!(out, ", ");
569 }
David Tolnaybaae4432020-03-01 20:20:10 -0800570 match &arg.ty {
571 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700572 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800573 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
574 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400575 }
576 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800577 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800578 Type::RustBox(_) => write!(out, ".into_raw()"),
579 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700580 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800581 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800582 _ => {}
583 }
David Tolnay7db73692019-10-20 14:51:12 -0400584 }
585 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400587 write!(out, ", ");
588 }
David Tolnay09011c32020-03-06 14:40:28 -0800589 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400590 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700591 if indirect_call {
592 if !sig.args.is_empty() || indirect_return {
593 write!(out, ", ");
594 }
595 write!(out, "extern$");
596 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800597 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800599 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800600 write!(out, ")");
601 }
602 }
603 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700605 writeln!(out, " if (error$.ptr) {{");
606 writeln!(out, " throw ::rust::Error(error$);");
607 writeln!(out, " }}");
608 }
David Tolnay7db73692019-10-20 14:51:12 -0400609 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700610 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800611 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400612 }
613 writeln!(out, "}}");
614 }
615}
616
617fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
618 match ty {
619 None => write!(out, "void "),
620 Some(ty) => write_type_space(out, ty),
621 }
622}
623
David Tolnay75dca2e2020-03-25 20:17:52 -0700624fn indirect_return(sig: &Signature, types: &Types) -> bool {
625 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700626 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700628}
629
David Tolnay99642622020-03-25 13:07:35 -0700630fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
631 match ty {
632 Type::RustBox(ty) | Type::UniquePtr(ty) => {
633 write_type_space(out, &ty.inner);
634 write!(out, "*");
635 }
636 Type::Ref(ty) => {
637 if ty.mutability.is_none() {
638 write!(out, "const ");
639 }
640 write_type(out, &ty.inner);
641 write!(out, " *");
642 }
643 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700644 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700645 _ => write_type(out, ty),
646 }
647}
648
649fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
650 write_indirect_return_type(out, ty);
651 match ty {
652 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700653 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700654 _ => write_space_after_type(out, ty),
655 }
656}
657
658fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400659 match ty {
660 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
661 write_type_space(out, &ty.inner);
662 write!(out, "*");
663 }
David Tolnay4a441222020-01-25 16:24:27 -0800664 Some(Type::Ref(ty)) => {
665 if ty.mutability.is_none() {
666 write!(out, "const ");
667 }
668 write_type(out, &ty.inner);
669 write!(out, " *");
670 }
David Tolnay750755e2020-03-01 13:04:08 -0800671 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700672 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400673 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
674 _ => write_return_type(out, ty),
675 }
676}
677
678fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
679 match &arg.ty {
680 Type::RustBox(ty) | Type::UniquePtr(ty) => {
681 write_type_space(out, &ty.inner);
682 write!(out, "*");
683 }
David Tolnay750755e2020-03-01 13:04:08 -0800684 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700685 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400686 _ => write_type_space(out, &arg.ty),
687 }
688 if types.needs_indirect_abi(&arg.ty) {
689 write!(out, "*");
690 }
691 write!(out, "{}", arg.ident);
692}
693
694fn write_type(out: &mut OutFile, ty: &Type) {
695 match ty {
696 Type::Ident(ident) => match Atom::from(ident) {
697 Some(Bool) => write!(out, "bool"),
698 Some(U8) => write!(out, "uint8_t"),
699 Some(U16) => write!(out, "uint16_t"),
700 Some(U32) => write!(out, "uint32_t"),
701 Some(U64) => write!(out, "uint64_t"),
702 Some(Usize) => write!(out, "size_t"),
703 Some(I8) => write!(out, "int8_t"),
704 Some(I16) => write!(out, "int16_t"),
705 Some(I32) => write!(out, "int32_t"),
706 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700707 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700708 Some(F32) => write!(out, "float"),
709 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800710 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800711 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400712 None => write!(out, "{}", ident),
713 },
714 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800715 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400716 write_type(out, &ty.inner);
717 write!(out, ">");
718 }
719 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800720 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400721 write_type(out, &ptr.inner);
722 write!(out, ">");
723 }
724 Type::Ref(r) => {
725 if r.mutability.is_none() {
726 write!(out, "const ");
727 }
728 write_type(out, &r.inner);
729 write!(out, " &");
730 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700731 Type::Slice(_) => {
732 // For now, only U8 slices are supported, which are covered separately below
733 unreachable!()
734 }
David Tolnay7db73692019-10-20 14:51:12 -0400735 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800736 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400737 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700738 Type::SliceRefU8(_) => {
739 write!(out, "::rust::Slice<uint8_t>");
740 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700741 Type::Fn(f) => {
742 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
743 match &f.ret {
744 Some(ret) => write_type(out, ret),
745 None => write!(out, "void"),
746 }
747 write!(out, "(");
748 for (i, arg) in f.args.iter().enumerate() {
749 if i > 0 {
750 write!(out, ", ");
751 }
752 write_type(out, &arg.ty);
753 }
754 write!(out, ")>");
755 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700756 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400757 }
758}
759
760fn write_type_space(out: &mut OutFile, ty: &Type) {
761 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700762 write_space_after_type(out, ty);
763}
764
765fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400766 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700767 Type::Ident(_)
768 | Type::RustBox(_)
769 | Type::UniquePtr(_)
770 | Type::Str(_)
771 | Type::SliceRefU8(_)
772 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400773 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700774 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400775 }
776}
777
778fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
779 fn allow_unique_ptr(ident: &Ident) -> bool {
780 Atom::from(ident).is_none()
781 }
782
783 out.begin_block("extern \"C\"");
784 for ty in types {
785 if let Type::RustBox(ty) = ty {
786 if let Type::Ident(inner) = &ty.inner {
787 out.next_section();
788 write_rust_box_extern(out, inner);
789 }
790 } else if let Type::UniquePtr(ptr) = ty {
791 if let Type::Ident(inner) = &ptr.inner {
792 if allow_unique_ptr(inner) {
793 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700794 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400795 }
796 }
797 }
798 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800799 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400800
David Tolnay750755e2020-03-01 13:04:08 -0800801 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700802 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400803 for ty in types {
804 if let Type::RustBox(ty) = ty {
805 if let Type::Ident(inner) = &ty.inner {
806 write_rust_box_impl(out, inner);
807 }
808 }
809 }
David Tolnay8c730492020-03-13 01:29:06 -0700810 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800811 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400812}
813
814fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
815 let mut inner = String::new();
816 for name in &out.namespace {
817 inner += name;
818 inner += "::";
819 }
820 inner += &ident.to_string();
821 let instance = inner.replace("::", "$");
822
David Tolnay8c730492020-03-13 01:29:06 -0700823 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
824 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400825 writeln!(
826 out,
David Tolnay8c730492020-03-13 01:29:06 -0700827 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400828 instance, inner,
829 );
830 writeln!(
831 out,
David Tolnay8c730492020-03-13 01:29:06 -0700832 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400833 instance, inner,
834 );
David Tolnay8c730492020-03-13 01:29:06 -0700835 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400836}
837
838fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
839 let mut inner = String::new();
840 for name in &out.namespace {
841 inner += name;
842 inner += "::";
843 }
844 inner += &ident.to_string();
845 let instance = inner.replace("::", "$");
846
847 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800848 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700849 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400850 writeln!(out, "}}");
851
852 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800853 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700854 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400855 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400856}
857
David Tolnay53838912020-04-09 20:56:44 -0700858fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700859 out.include.utility = true;
860
David Tolnay7db73692019-10-20 14:51:12 -0400861 let mut inner = String::new();
862 for name in &out.namespace {
863 inner += name;
864 inner += "::";
865 }
866 inner += &ident.to_string();
867 let instance = inner.replace("::", "$");
868
David Tolnay8c730492020-03-13 01:29:06 -0700869 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
870 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400871 writeln!(
872 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800873 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400874 inner,
875 );
876 writeln!(
877 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800878 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400879 inner,
880 );
881 writeln!(
882 out,
David Tolnay8c730492020-03-13 01:29:06 -0700883 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400884 instance, inner,
885 );
David Tolnay7e219b82020-03-01 13:14:51 -0800886 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400887 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700888 if types.structs.contains_key(ident) {
889 writeln!(
890 out,
891 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
892 instance, inner, inner,
893 );
894 writeln!(
895 out,
896 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
897 inner, inner,
898 );
899 writeln!(out, "}}");
900 }
David Tolnay7db73692019-10-20 14:51:12 -0400901 writeln!(
902 out,
David Tolnay8c730492020-03-13 01:29:06 -0700903 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400904 instance, inner, inner,
905 );
David Tolnay7e219b82020-03-01 13:14:51 -0800906 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400907 writeln!(out, "}}");
908 writeln!(
909 out,
David Tolnay8c730492020-03-13 01:29:06 -0700910 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400911 inner, instance, inner,
912 );
913 writeln!(out, " return ptr.get();");
914 writeln!(out, "}}");
915 writeln!(
916 out,
David Tolnay8c730492020-03-13 01:29:06 -0700917 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400918 inner, instance, inner,
919 );
920 writeln!(out, " return ptr.release();");
921 writeln!(out, "}}");
922 writeln!(
923 out,
David Tolnay8c730492020-03-13 01:29:06 -0700924 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400925 instance, inner,
926 );
927 writeln!(out, " ptr->~unique_ptr();");
928 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700929 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400930}