blob: 322ca08716930c58e466db56a4bc27b227650049 [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 Tolnay4770b472020-04-14 16:32:59 -0700103 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400104 _ => {}
105 }
106 }
David Tolnay7db73692019-10-20 14:51:12 -0400107}
108
David Tolnayf51447e2020-03-06 14:14:27 -0800109fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700110 let mut needs_rust_string = false;
111 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700112 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400113 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700114 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700115 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400116 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700117 match ty {
118 Type::RustBox(_) => {
119 out.include.type_traits = true;
120 needs_rust_box = true;
121 }
122 Type::Str(_) => {
123 out.include.cstdint = true;
124 out.include.string = true;
125 needs_rust_str = true;
126 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700127 Type::Fn(_) => {
128 needs_rust_fn = true;
129 }
David Tolnay4770b472020-04-14 16:32:59 -0700130 Type::Slice(_) | Type::SliceRefU8(_) => {
131 needs_rust_slice = true;
132 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700133 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700134 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700135 needs_rust_isize = true;
136 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700137 ty if ty == RustString => {
138 out.include.array = true;
139 out.include.cstdint = true;
140 out.include.string = true;
141 needs_rust_string = true;
142 }
143 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400144 }
145 }
146
David Tolnayb7a7cb62020-03-17 21:18:40 -0700147 let mut needs_rust_error = false;
148 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800149 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800150 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700151 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800152 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 match api {
154 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700155 if efn.throws {
156 needs_trycatch = true;
157 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700158 for arg in &efn.args {
159 if arg.ty == RustString {
160 needs_unsafe_bitcopy = true;
161 break;
162 }
David Tolnay09011c32020-03-06 14:40:28 -0800163 }
164 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700165 Api::RustFunction(efn) if !out.header => {
166 if efn.throws {
167 out.include.exception = true;
168 needs_rust_error = true;
169 }
170 for arg in &efn.args {
171 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
172 needs_manually_drop = true;
173 break;
174 }
175 }
176 if let Some(ret) = &efn.ret {
177 if types.needs_indirect_abi(ret) {
178 needs_maybe_uninit = true;
179 }
David Tolnayf51447e2020-03-06 14:14:27 -0800180 }
181 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800183 }
184 }
185
David Tolnay750755e2020-03-01 13:04:08 -0800186 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700187 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800188
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 if needs_rust_string
190 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700191 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700192 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700193 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700194 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700195 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700196 || needs_unsafe_bitcopy
197 || needs_manually_drop
198 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700199 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700200 {
David Tolnay736cbca2020-03-11 16:49:18 -0700201 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800202 }
203
David Tolnayd1402742020-03-25 22:21:42 -0700204 if needs_rust_string {
205 out.next_section();
206 writeln!(out, "struct unsafe_bitcopy_t;");
207 }
208
David Tolnayb7a7cb62020-03-17 21:18:40 -0700209 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
210 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700211 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700213 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700215 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700216 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800217
218 if needs_manually_drop {
219 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700220 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800221 writeln!(out, "template <typename T>");
222 writeln!(out, "union ManuallyDrop {{");
223 writeln!(out, " T value;");
224 writeln!(
225 out,
226 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
227 );
228 writeln!(out, " ~ManuallyDrop() {{}}");
229 writeln!(out, "}};");
230 }
231
David Tolnay09011c32020-03-06 14:40:28 -0800232 if needs_maybe_uninit {
233 out.next_section();
234 writeln!(out, "template <typename T>");
235 writeln!(out, "union MaybeUninit {{");
236 writeln!(out, " T value;");
237 writeln!(out, " MaybeUninit() {{}}");
238 writeln!(out, " ~MaybeUninit() {{}}");
239 writeln!(out, "}};");
240 }
241
David Tolnay3e3e0af2020-03-17 22:42:49 -0700242 out.end_block("namespace cxxbridge02");
243
David Tolnay5d121442020-03-17 22:14:40 -0700244 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700245 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700246 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700247 out.include.type_traits = true;
248 out.include.utility = true;
249 writeln!(out, "class missing {{}};");
250 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700251 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700252 writeln!(out, "template <typename Try, typename Fail>");
253 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700254 writeln!(
255 out,
David Tolnay04722332020-03-18 11:31:54 -0700256 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700257 );
David Tolnay04722332020-03-18 11:31:54 -0700258 writeln!(out, " missing>::value>::type");
259 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700260 writeln!(out, " func();");
261 writeln!(out, "}} catch (const ::std::exception &e) {{");
262 writeln!(out, " fail(e.what());");
263 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700264 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700265 }
266
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800267 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400268}
269
David Tolnayb7a7cb62020-03-17 21:18:40 -0700270fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700271 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700272 if needed {
273 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700274 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700275 if !line.trim_start().starts_with("//") {
276 writeln!(out, "{}", line);
277 }
278 }
279 }
280}
281
David Tolnay7db73692019-10-20 14:51:12 -0400282fn write_struct(out: &mut OutFile, strct: &Struct) {
283 for line in strct.doc.to_string().lines() {
284 writeln!(out, "//{}", line);
285 }
286 writeln!(out, "struct {} final {{", strct.ident);
287 for field in &strct.fields {
288 write!(out, " ");
289 write_type_space(out, &field.ty);
290 writeln!(out, "{};", field.ident);
291 }
292 writeln!(out, "}};");
293}
294
295fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
296 writeln!(out, "struct {};", ident);
297}
298
David Tolnay8861bee2020-01-20 18:39:24 -0800299fn write_struct_using(out: &mut OutFile, ident: &Ident) {
300 writeln!(out, "using {} = {};", ident, ident);
301}
302
David Tolnayebef4a22020-03-17 15:33:47 -0700303fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
304 let mut has_cxx_throws = false;
305 for api in apis {
306 if let Api::CxxFunction(efn) = api {
307 if efn.throws {
308 has_cxx_throws = true;
309 break;
310 }
311 }
312 }
313
314 if has_cxx_throws {
315 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700316 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700317 out,
318 "const char *cxxbridge02$exception(const char *, size_t);",
319 );
320 }
321}
322
David Tolnay7db73692019-10-20 14:51:12 -0400323fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700324 if efn.throws {
325 write!(out, "::rust::Str::Repr ");
326 } else {
David Tolnay99642622020-03-25 13:07:35 -0700327 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700328 }
David Tolnayd815de02020-03-29 21:29:17 -0700329 write!(out, "{}cxxbridge02${}(", out.namespace, efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400330 for (i, arg) in efn.args.iter().enumerate() {
331 if i > 0 {
332 write!(out, ", ");
333 }
David Tolnaya46a2372020-03-06 10:03:48 -0800334 if arg.ty == RustString {
335 write!(out, "const ");
336 }
David Tolnay7db73692019-10-20 14:51:12 -0400337 write_extern_arg(out, arg, types);
338 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700339 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400340 if indirect_return {
341 if !efn.args.is_empty() {
342 write!(out, ", ");
343 }
David Tolnay99642622020-03-25 13:07:35 -0700344 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400345 write!(out, "*return$");
346 }
347 writeln!(out, ") noexcept {{");
348 write!(out, " ");
349 write_return_type(out, &efn.ret);
350 write!(out, "(*{}$)(", efn.ident);
351 for (i, arg) in efn.args.iter().enumerate() {
352 if i > 0 {
353 write!(out, ", ");
354 }
355 write_type(out, &arg.ty);
356 }
357 writeln!(out, ") = {};", efn.ident);
358 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700359 if efn.throws {
360 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700361 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700362 writeln!(out, " [&] {{");
363 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700364 }
David Tolnay7db73692019-10-20 14:51:12 -0400365 if indirect_return {
366 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700367 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400368 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700369 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400370 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700371 }
372 match &efn.ret {
373 Some(Type::Ref(_)) => write!(out, "&"),
374 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700375 Some(Type::SliceRefU8(_)) if !indirect_return => {
376 write!(out, "::rust::Slice<uint8_t>::Repr(")
377 }
David Tolnay99642622020-03-25 13:07:35 -0700378 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400379 }
380 write!(out, "{}$(", efn.ident);
381 for (i, arg) in efn.args.iter().enumerate() {
382 if i > 0 {
383 write!(out, ", ");
384 }
385 if let Type::RustBox(_) = &arg.ty {
386 write_type(out, &arg.ty);
387 write!(out, "::from_raw({})", arg.ident);
388 } else if let Type::UniquePtr(_) = &arg.ty {
389 write_type(out, &arg.ty);
390 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800391 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800392 write!(
393 out,
394 "::rust::String(::rust::unsafe_bitcopy, *{})",
395 arg.ident,
396 );
David Tolnay7db73692019-10-20 14:51:12 -0400397 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700398 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800399 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400400 } else {
401 write!(out, "{}", arg.ident);
402 }
403 }
404 write!(out, ")");
405 match &efn.ret {
406 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
407 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700408 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400409 _ => {}
410 }
411 if indirect_return {
412 write!(out, ")");
413 }
414 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700415 if efn.throws {
416 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700417 writeln!(out, " throw$.ptr = nullptr;");
418 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700419 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700420 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700421 writeln!(
422 out,
David Tolnay5d121442020-03-17 22:14:40 -0700423 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700424 );
David Tolnay5d121442020-03-17 22:14:40 -0700425 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700426 writeln!(out, " return throw$;");
427 }
David Tolnay7db73692019-10-20 14:51:12 -0400428 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700429 for arg in &efn.args {
430 if let Type::Fn(f) = &arg.ty {
431 let var = &arg.ident;
432 write_function_pointer_trampoline(out, efn, var, f, types);
433 }
434 }
435}
436
437fn write_function_pointer_trampoline(
438 out: &mut OutFile,
439 efn: &ExternFn,
440 var: &Ident,
441 f: &Signature,
442 types: &Types,
443) {
444 out.next_section();
445 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
446 let indirect_call = true;
447 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
448
449 out.next_section();
450 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
451 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400452}
453
454fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700455 let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident);
456 let indirect_call = false;
457 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
458}
459
460fn write_rust_function_decl_impl(
461 out: &mut OutFile,
462 link_name: &str,
463 sig: &Signature,
464 types: &Types,
465 indirect_call: bool,
466) {
467 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700468 write!(out, "::rust::Str::Repr ");
469 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700470 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700471 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700472 write!(out, "{}(", link_name);
473 let mut needs_comma = false;
474 for arg in &sig.args {
475 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400476 write!(out, ", ");
477 }
478 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700479 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400480 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700481 if indirect_return(sig, types) {
482 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400483 write!(out, ", ");
484 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700485 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400486 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700487 needs_comma = true;
488 }
489 if indirect_call {
490 if needs_comma {
491 write!(out, ", ");
492 }
493 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400494 }
495 writeln!(out, ") noexcept;");
496}
497
498fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400499 for line in efn.doc.to_string().lines() {
500 writeln!(out, "//{}", line);
501 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700502 let local_name = efn.ident.to_string();
503 let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident);
504 let indirect_call = false;
505 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
506}
507
508fn write_rust_function_shim_impl(
509 out: &mut OutFile,
510 local_name: &str,
511 sig: &Signature,
512 types: &Types,
513 invoke: &str,
514 indirect_call: bool,
515) {
516 write_return_type(out, &sig.ret);
517 write!(out, "{}(", local_name);
518 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400519 if i > 0 {
520 write!(out, ", ");
521 }
522 write_type_space(out, &arg.ty);
523 write!(out, "{}", arg.ident);
524 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700525 if indirect_call {
526 if !sig.args.is_empty() {
527 write!(out, ", ");
528 }
529 write!(out, "void *extern$");
530 }
David Tolnay1e548172020-03-16 13:37:09 -0700531 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700532 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700533 write!(out, " noexcept");
534 }
David Tolnay7db73692019-10-20 14:51:12 -0400535 if out.header {
536 writeln!(out, ";");
537 } else {
538 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700539 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800540 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700541 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800542 write!(out, " ::rust::ManuallyDrop<");
543 write_type(out, &arg.ty);
544 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
545 }
546 }
David Tolnay7db73692019-10-20 14:51:12 -0400547 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400549 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800550 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700551 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800552 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400553 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400555 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800556 match ret {
557 Type::RustBox(_) => {
558 write_type(out, ret);
559 write!(out, "::from_raw(");
560 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800561 Type::UniquePtr(_) => {
562 write_type(out, ret);
563 write!(out, "(");
564 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800565 Type::Ref(_) => write!(out, "*"),
566 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800567 }
David Tolnay7db73692019-10-20 14:51:12 -0400568 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700570 write!(out, "::rust::Str::Repr error$ = ");
571 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700572 write!(out, "{}(", invoke);
573 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400574 if i > 0 {
575 write!(out, ", ");
576 }
David Tolnaybaae4432020-03-01 20:20:10 -0800577 match &arg.ty {
578 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700579 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800580 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
581 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400582 }
583 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800584 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800585 Type::RustBox(_) => write!(out, ".into_raw()"),
586 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700587 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800588 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800589 _ => {}
590 }
David Tolnay7db73692019-10-20 14:51:12 -0400591 }
592 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700593 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400594 write!(out, ", ");
595 }
David Tolnay09011c32020-03-06 14:40:28 -0800596 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400597 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 if indirect_call {
599 if !sig.args.is_empty() || indirect_return {
600 write!(out, ", ");
601 }
602 write!(out, "extern$");
603 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800604 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800606 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800607 write!(out, ")");
608 }
609 }
610 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700612 writeln!(out, " if (error$.ptr) {{");
613 writeln!(out, " throw ::rust::Error(error$);");
614 writeln!(out, " }}");
615 }
David Tolnay7db73692019-10-20 14:51:12 -0400616 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700617 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800618 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400619 }
620 writeln!(out, "}}");
621 }
622}
623
624fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
625 match ty {
626 None => write!(out, "void "),
627 Some(ty) => write_type_space(out, ty),
628 }
629}
630
David Tolnay75dca2e2020-03-25 20:17:52 -0700631fn indirect_return(sig: &Signature, types: &Types) -> bool {
632 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700633 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700634 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700635}
636
David Tolnay99642622020-03-25 13:07:35 -0700637fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
638 match ty {
639 Type::RustBox(ty) | Type::UniquePtr(ty) => {
640 write_type_space(out, &ty.inner);
641 write!(out, "*");
642 }
643 Type::Ref(ty) => {
644 if ty.mutability.is_none() {
645 write!(out, "const ");
646 }
647 write_type(out, &ty.inner);
648 write!(out, " *");
649 }
650 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700651 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700652 _ => write_type(out, ty),
653 }
654}
655
656fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
657 write_indirect_return_type(out, ty);
658 match ty {
659 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700660 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700661 _ => write_space_after_type(out, ty),
662 }
663}
664
665fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400666 match ty {
667 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
668 write_type_space(out, &ty.inner);
669 write!(out, "*");
670 }
David Tolnay4a441222020-01-25 16:24:27 -0800671 Some(Type::Ref(ty)) => {
672 if ty.mutability.is_none() {
673 write!(out, "const ");
674 }
675 write_type(out, &ty.inner);
676 write!(out, " *");
677 }
David Tolnay750755e2020-03-01 13:04:08 -0800678 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700679 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400680 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
681 _ => write_return_type(out, ty),
682 }
683}
684
685fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
686 match &arg.ty {
687 Type::RustBox(ty) | Type::UniquePtr(ty) => {
688 write_type_space(out, &ty.inner);
689 write!(out, "*");
690 }
David Tolnay750755e2020-03-01 13:04:08 -0800691 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700692 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400693 _ => write_type_space(out, &arg.ty),
694 }
695 if types.needs_indirect_abi(&arg.ty) {
696 write!(out, "*");
697 }
698 write!(out, "{}", arg.ident);
699}
700
701fn write_type(out: &mut OutFile, ty: &Type) {
702 match ty {
703 Type::Ident(ident) => match Atom::from(ident) {
704 Some(Bool) => write!(out, "bool"),
705 Some(U8) => write!(out, "uint8_t"),
706 Some(U16) => write!(out, "uint16_t"),
707 Some(U32) => write!(out, "uint32_t"),
708 Some(U64) => write!(out, "uint64_t"),
709 Some(Usize) => write!(out, "size_t"),
710 Some(I8) => write!(out, "int8_t"),
711 Some(I16) => write!(out, "int16_t"),
712 Some(I32) => write!(out, "int32_t"),
713 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700714 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700715 Some(F32) => write!(out, "float"),
716 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800717 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800718 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400719 None => write!(out, "{}", ident),
720 },
721 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800722 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400723 write_type(out, &ty.inner);
724 write!(out, ">");
725 }
726 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800727 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400728 write_type(out, &ptr.inner);
729 write!(out, ">");
730 }
731 Type::Ref(r) => {
732 if r.mutability.is_none() {
733 write!(out, "const ");
734 }
735 write_type(out, &r.inner);
736 write!(out, " &");
737 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700738 Type::Slice(_) => {
739 // For now, only U8 slices are supported, which are covered separately below
740 unreachable!()
741 }
David Tolnay7db73692019-10-20 14:51:12 -0400742 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800743 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400744 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700745 Type::SliceRefU8(_) => {
746 write!(out, "::rust::Slice<uint8_t>");
747 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700748 Type::Fn(f) => {
749 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
750 match &f.ret {
751 Some(ret) => write_type(out, ret),
752 None => write!(out, "void"),
753 }
754 write!(out, "(");
755 for (i, arg) in f.args.iter().enumerate() {
756 if i > 0 {
757 write!(out, ", ");
758 }
759 write_type(out, &arg.ty);
760 }
761 write!(out, ")>");
762 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700763 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400764 }
765}
766
767fn write_type_space(out: &mut OutFile, ty: &Type) {
768 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700769 write_space_after_type(out, ty);
770}
771
772fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400773 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700774 Type::Ident(_)
775 | Type::RustBox(_)
776 | Type::UniquePtr(_)
777 | Type::Str(_)
778 | Type::SliceRefU8(_)
779 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400780 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700781 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400782 }
783}
784
785fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
786 fn allow_unique_ptr(ident: &Ident) -> bool {
787 Atom::from(ident).is_none()
788 }
789
790 out.begin_block("extern \"C\"");
791 for ty in types {
792 if let Type::RustBox(ty) = ty {
793 if let Type::Ident(inner) = &ty.inner {
794 out.next_section();
795 write_rust_box_extern(out, inner);
796 }
797 } else if let Type::UniquePtr(ptr) = ty {
798 if let Type::Ident(inner) = &ptr.inner {
799 if allow_unique_ptr(inner) {
800 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700801 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400802 }
803 }
804 }
805 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800806 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400807
David Tolnay750755e2020-03-01 13:04:08 -0800808 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700809 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400810 for ty in types {
811 if let Type::RustBox(ty) = ty {
812 if let Type::Ident(inner) = &ty.inner {
813 write_rust_box_impl(out, inner);
814 }
815 }
816 }
David Tolnay8c730492020-03-13 01:29:06 -0700817 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800818 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400819}
820
821fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
822 let mut inner = String::new();
823 for name in &out.namespace {
824 inner += name;
825 inner += "::";
826 }
827 inner += &ident.to_string();
828 let instance = inner.replace("::", "$");
829
David Tolnay8c730492020-03-13 01:29:06 -0700830 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
831 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400832 writeln!(
833 out,
David Tolnay8c730492020-03-13 01:29:06 -0700834 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400835 instance, inner,
836 );
837 writeln!(
838 out,
David Tolnay8c730492020-03-13 01:29:06 -0700839 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400840 instance, inner,
841 );
David Tolnay8c730492020-03-13 01:29:06 -0700842 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400843}
844
845fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
846 let mut inner = String::new();
847 for name in &out.namespace {
848 inner += name;
849 inner += "::";
850 }
851 inner += &ident.to_string();
852 let instance = inner.replace("::", "$");
853
854 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800855 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700856 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400857 writeln!(out, "}}");
858
859 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800860 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700861 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400862 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400863}
864
David Tolnay53838912020-04-09 20:56:44 -0700865fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700866 out.include.utility = true;
867
David Tolnay7db73692019-10-20 14:51:12 -0400868 let mut inner = String::new();
869 for name in &out.namespace {
870 inner += name;
871 inner += "::";
872 }
873 inner += &ident.to_string();
874 let instance = inner.replace("::", "$");
875
David Tolnay8c730492020-03-13 01:29:06 -0700876 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
877 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400878 writeln!(
879 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800880 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400881 inner,
882 );
883 writeln!(
884 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800885 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400886 inner,
887 );
888 writeln!(
889 out,
David Tolnay8c730492020-03-13 01:29:06 -0700890 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400891 instance, inner,
892 );
David Tolnay7e219b82020-03-01 13:14:51 -0800893 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400894 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700895 if types.structs.contains_key(ident) {
896 writeln!(
897 out,
898 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
899 instance, inner, inner,
900 );
901 writeln!(
902 out,
903 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
904 inner, inner,
905 );
906 writeln!(out, "}}");
907 }
David Tolnay7db73692019-10-20 14:51:12 -0400908 writeln!(
909 out,
David Tolnay8c730492020-03-13 01:29:06 -0700910 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400911 instance, inner, inner,
912 );
David Tolnay7e219b82020-03-01 13:14:51 -0800913 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400914 writeln!(out, "}}");
915 writeln!(
916 out,
David Tolnay8c730492020-03-13 01:29:06 -0700917 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400918 inner, instance, inner,
919 );
920 writeln!(out, " return ptr.get();");
921 writeln!(out, "}}");
922 writeln!(
923 out,
David Tolnay8c730492020-03-13 01:29:06 -0700924 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400925 inner, instance, inner,
926 );
927 writeln!(out, " return ptr.release();");
928 writeln!(out, "}}");
929 writeln!(
930 out,
David Tolnay8c730492020-03-13 01:29:06 -0700931 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400932 instance, inner,
933 );
934 writeln!(out, " ptr->~unique_ptr();");
935 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700936 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400937}