blob: 41b5b3743e557a6a282813b78bb6aaeee85a9056 [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 Galenson3d4f6122020-04-07 15:54:05 -07005use crate::syntax::{Api, ExternFn, Receiver, 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);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700330 if let Some(base) = &efn.receiver {
331 write!(out, "{} *__receiver$", base.ident);
332 }
David Tolnay7db73692019-10-20 14:51:12 -0400333 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700334 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400335 write!(out, ", ");
336 }
David Tolnaya46a2372020-03-06 10:03:48 -0800337 if arg.ty == RustString {
338 write!(out, "const ");
339 }
David Tolnay7db73692019-10-20 14:51:12 -0400340 write_extern_arg(out, arg, types);
341 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700342 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400343 if indirect_return {
344 if !efn.args.is_empty() {
345 write!(out, ", ");
346 }
David Tolnay99642622020-03-25 13:07:35 -0700347 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400348 write!(out, "*return$");
349 }
350 writeln!(out, ") noexcept {{");
351 write!(out, " ");
352 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700353 match &efn.receiver {
354 None => write!(out, "(*{}$)(", efn.ident),
355 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
356 }
David Tolnay7db73692019-10-20 14:51:12 -0400357 for (i, arg) in efn.args.iter().enumerate() {
358 if i > 0 {
359 write!(out, ", ");
360 }
361 write_type(out, &arg.ty);
362 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700363 write!(out, ")");
364 match &efn.receiver {
365 Some(Receiver { mutability: None, ident: _ }) => write!(out, " const"),
366 _ => {},
367 }
368 write!(out, " = ");
369 match &efn.receiver {
370 None => write!(out, "{}", efn.ident),
371 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
372 }
373 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400374 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700375 if efn.throws {
376 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700377 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700378 writeln!(out, " [&] {{");
379 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700380 }
David Tolnay7db73692019-10-20 14:51:12 -0400381 if indirect_return {
382 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700383 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400384 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700385 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400386 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700387 }
388 match &efn.ret {
389 Some(Type::Ref(_)) => write!(out, "&"),
390 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700391 Some(Type::SliceRefU8(_)) if !indirect_return => {
392 write!(out, "::rust::Slice<uint8_t>::Repr(")
393 }
David Tolnay99642622020-03-25 13:07:35 -0700394 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400395 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700396 match &efn.receiver {
397 None => write!(out, "{}$(", efn.ident),
398 Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident),
399 }
David Tolnay7db73692019-10-20 14:51:12 -0400400 for (i, arg) in efn.args.iter().enumerate() {
401 if i > 0 {
402 write!(out, ", ");
403 }
404 if let Type::RustBox(_) = &arg.ty {
405 write_type(out, &arg.ty);
406 write!(out, "::from_raw({})", arg.ident);
407 } else if let Type::UniquePtr(_) = &arg.ty {
408 write_type(out, &arg.ty);
409 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800410 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800411 write!(
412 out,
413 "::rust::String(::rust::unsafe_bitcopy, *{})",
414 arg.ident,
415 );
David Tolnay7db73692019-10-20 14:51:12 -0400416 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700417 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800418 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400419 } else {
420 write!(out, "{}", arg.ident);
421 }
422 }
423 write!(out, ")");
424 match &efn.ret {
425 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
426 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700427 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400428 _ => {}
429 }
430 if indirect_return {
431 write!(out, ")");
432 }
433 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700434 if efn.throws {
435 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700436 writeln!(out, " throw$.ptr = nullptr;");
437 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700438 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700439 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700440 writeln!(
441 out,
David Tolnay5d121442020-03-17 22:14:40 -0700442 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700443 );
David Tolnay5d121442020-03-17 22:14:40 -0700444 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700445 writeln!(out, " return throw$;");
446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700448 for arg in &efn.args {
449 if let Type::Fn(f) = &arg.ty {
450 let var = &arg.ident;
451 write_function_pointer_trampoline(out, efn, var, f, types);
452 }
453 }
454}
455
456fn write_function_pointer_trampoline(
457 out: &mut OutFile,
458 efn: &ExternFn,
459 var: &Ident,
460 f: &Signature,
461 types: &Types,
462) {
463 out.next_section();
464 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
465 let indirect_call = true;
466 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
467
468 out.next_section();
469 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
470 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400471}
472
473fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700474 let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident);
475 let indirect_call = false;
476 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
477}
478
479fn write_rust_function_decl_impl(
480 out: &mut OutFile,
481 link_name: &str,
482 sig: &Signature,
483 types: &Types,
484 indirect_call: bool,
485) {
486 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700487 write!(out, "::rust::Str::Repr ");
488 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700489 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700490 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700491 write!(out, "{}(", link_name);
492 let mut needs_comma = false;
493 for arg in &sig.args {
494 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400495 write!(out, ", ");
496 }
497 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700498 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400499 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700500 if indirect_return(sig, types) {
501 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400502 write!(out, ", ");
503 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700504 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400505 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700506 needs_comma = true;
507 }
508 if indirect_call {
509 if needs_comma {
510 write!(out, ", ");
511 }
512 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400513 }
514 writeln!(out, ") noexcept;");
515}
516
517fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400518 for line in efn.doc.to_string().lines() {
519 writeln!(out, "//{}", line);
520 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700521 let local_name = efn.ident.to_string();
522 let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident);
523 let indirect_call = false;
524 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
525}
526
527fn write_rust_function_shim_impl(
528 out: &mut OutFile,
529 local_name: &str,
530 sig: &Signature,
531 types: &Types,
532 invoke: &str,
533 indirect_call: bool,
534) {
535 write_return_type(out, &sig.ret);
536 write!(out, "{}(", local_name);
537 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400538 if i > 0 {
539 write!(out, ", ");
540 }
541 write_type_space(out, &arg.ty);
542 write!(out, "{}", arg.ident);
543 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700544 if indirect_call {
545 if !sig.args.is_empty() {
546 write!(out, ", ");
547 }
548 write!(out, "void *extern$");
549 }
David Tolnay1e548172020-03-16 13:37:09 -0700550 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700551 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700552 write!(out, " noexcept");
553 }
David Tolnay7db73692019-10-20 14:51:12 -0400554 if out.header {
555 writeln!(out, ";");
556 } else {
557 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800559 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700560 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800561 write!(out, " ::rust::ManuallyDrop<");
562 write_type(out, &arg.ty);
563 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
564 }
565 }
David Tolnay7db73692019-10-20 14:51:12 -0400566 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700567 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400568 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800569 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700570 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800571 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400572 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700573 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400574 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800575 match ret {
576 Type::RustBox(_) => {
577 write_type(out, ret);
578 write!(out, "::from_raw(");
579 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800580 Type::UniquePtr(_) => {
581 write_type(out, ret);
582 write!(out, "(");
583 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800584 Type::Ref(_) => write!(out, "*"),
585 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800586 }
David Tolnay7db73692019-10-20 14:51:12 -0400587 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700588 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700589 write!(out, "::rust::Str::Repr error$ = ");
590 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700591 write!(out, "{}(", invoke);
592 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400593 if i > 0 {
594 write!(out, ", ");
595 }
David Tolnaybaae4432020-03-01 20:20:10 -0800596 match &arg.ty {
597 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700598 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800599 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
600 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400601 }
602 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800603 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800604 Type::RustBox(_) => write!(out, ".into_raw()"),
605 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700606 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800607 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800608 _ => {}
609 }
David Tolnay7db73692019-10-20 14:51:12 -0400610 }
611 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400613 write!(out, ", ");
614 }
David Tolnay09011c32020-03-06 14:40:28 -0800615 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400616 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700617 if indirect_call {
618 if !sig.args.is_empty() || indirect_return {
619 write!(out, ", ");
620 }
621 write!(out, "extern$");
622 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800623 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700624 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800625 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800626 write!(out, ")");
627 }
628 }
629 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700630 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700631 writeln!(out, " if (error$.ptr) {{");
632 writeln!(out, " throw ::rust::Error(error$);");
633 writeln!(out, " }}");
634 }
David Tolnay7db73692019-10-20 14:51:12 -0400635 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700636 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800637 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400638 }
639 writeln!(out, "}}");
640 }
641}
642
643fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
644 match ty {
645 None => write!(out, "void "),
646 Some(ty) => write_type_space(out, ty),
647 }
648}
649
David Tolnay75dca2e2020-03-25 20:17:52 -0700650fn indirect_return(sig: &Signature, types: &Types) -> bool {
651 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700652 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700654}
655
David Tolnay99642622020-03-25 13:07:35 -0700656fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
657 match ty {
658 Type::RustBox(ty) | Type::UniquePtr(ty) => {
659 write_type_space(out, &ty.inner);
660 write!(out, "*");
661 }
662 Type::Ref(ty) => {
663 if ty.mutability.is_none() {
664 write!(out, "const ");
665 }
666 write_type(out, &ty.inner);
667 write!(out, " *");
668 }
669 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700670 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700671 _ => write_type(out, ty),
672 }
673}
674
675fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
676 write_indirect_return_type(out, ty);
677 match ty {
678 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700679 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700680 _ => write_space_after_type(out, ty),
681 }
682}
683
684fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400685 match ty {
686 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
687 write_type_space(out, &ty.inner);
688 write!(out, "*");
689 }
David Tolnay4a441222020-01-25 16:24:27 -0800690 Some(Type::Ref(ty)) => {
691 if ty.mutability.is_none() {
692 write!(out, "const ");
693 }
694 write_type(out, &ty.inner);
695 write!(out, " *");
696 }
David Tolnay750755e2020-03-01 13:04:08 -0800697 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700698 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400699 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
700 _ => write_return_type(out, ty),
701 }
702}
703
704fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
705 match &arg.ty {
706 Type::RustBox(ty) | Type::UniquePtr(ty) => {
707 write_type_space(out, &ty.inner);
708 write!(out, "*");
709 }
David Tolnay750755e2020-03-01 13:04:08 -0800710 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700711 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400712 _ => write_type_space(out, &arg.ty),
713 }
714 if types.needs_indirect_abi(&arg.ty) {
715 write!(out, "*");
716 }
717 write!(out, "{}", arg.ident);
718}
719
720fn write_type(out: &mut OutFile, ty: &Type) {
721 match ty {
722 Type::Ident(ident) => match Atom::from(ident) {
723 Some(Bool) => write!(out, "bool"),
724 Some(U8) => write!(out, "uint8_t"),
725 Some(U16) => write!(out, "uint16_t"),
726 Some(U32) => write!(out, "uint32_t"),
727 Some(U64) => write!(out, "uint64_t"),
728 Some(Usize) => write!(out, "size_t"),
729 Some(I8) => write!(out, "int8_t"),
730 Some(I16) => write!(out, "int16_t"),
731 Some(I32) => write!(out, "int32_t"),
732 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700733 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700734 Some(F32) => write!(out, "float"),
735 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800736 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800737 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400738 None => write!(out, "{}", ident),
739 },
740 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800741 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400742 write_type(out, &ty.inner);
743 write!(out, ">");
744 }
745 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800746 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400747 write_type(out, &ptr.inner);
748 write!(out, ">");
749 }
750 Type::Ref(r) => {
751 if r.mutability.is_none() {
752 write!(out, "const ");
753 }
754 write_type(out, &r.inner);
755 write!(out, " &");
756 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700757 Type::Slice(_) => {
758 // For now, only U8 slices are supported, which are covered separately below
759 unreachable!()
760 }
David Tolnay7db73692019-10-20 14:51:12 -0400761 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800762 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400763 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700764 Type::SliceRefU8(_) => {
765 write!(out, "::rust::Slice<uint8_t>");
766 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700767 Type::Fn(f) => {
768 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
769 match &f.ret {
770 Some(ret) => write_type(out, ret),
771 None => write!(out, "void"),
772 }
773 write!(out, "(");
774 for (i, arg) in f.args.iter().enumerate() {
775 if i > 0 {
776 write!(out, ", ");
777 }
778 write_type(out, &arg.ty);
779 }
780 write!(out, ")>");
781 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700782 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400783 }
784}
785
786fn write_type_space(out: &mut OutFile, ty: &Type) {
787 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700788 write_space_after_type(out, ty);
789}
790
791fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400792 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700793 Type::Ident(_)
794 | Type::RustBox(_)
795 | Type::UniquePtr(_)
796 | Type::Str(_)
797 | Type::SliceRefU8(_)
798 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400799 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700800 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400801 }
802}
803
804fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
805 fn allow_unique_ptr(ident: &Ident) -> bool {
806 Atom::from(ident).is_none()
807 }
808
809 out.begin_block("extern \"C\"");
810 for ty in types {
811 if let Type::RustBox(ty) = ty {
812 if let Type::Ident(inner) = &ty.inner {
813 out.next_section();
814 write_rust_box_extern(out, inner);
815 }
816 } else if let Type::UniquePtr(ptr) = ty {
817 if let Type::Ident(inner) = &ptr.inner {
818 if allow_unique_ptr(inner) {
819 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700820 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400821 }
822 }
823 }
824 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800825 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400826
David Tolnay750755e2020-03-01 13:04:08 -0800827 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700828 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400829 for ty in types {
830 if let Type::RustBox(ty) = ty {
831 if let Type::Ident(inner) = &ty.inner {
832 write_rust_box_impl(out, inner);
833 }
834 }
835 }
David Tolnay8c730492020-03-13 01:29:06 -0700836 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800837 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400838}
839
840fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
841 let mut inner = String::new();
842 for name in &out.namespace {
843 inner += name;
844 inner += "::";
845 }
846 inner += &ident.to_string();
847 let instance = inner.replace("::", "$");
848
David Tolnay8c730492020-03-13 01:29:06 -0700849 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
850 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400851 writeln!(
852 out,
David Tolnay8c730492020-03-13 01:29:06 -0700853 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400854 instance, inner,
855 );
856 writeln!(
857 out,
David Tolnay8c730492020-03-13 01:29:06 -0700858 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400859 instance, inner,
860 );
David Tolnay8c730492020-03-13 01:29:06 -0700861 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400862}
863
864fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
865 let mut inner = String::new();
866 for name in &out.namespace {
867 inner += name;
868 inner += "::";
869 }
870 inner += &ident.to_string();
871 let instance = inner.replace("::", "$");
872
873 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800874 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700875 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400876 writeln!(out, "}}");
877
878 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800879 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700880 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400881 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400882}
883
David Tolnay53838912020-04-09 20:56:44 -0700884fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700885 out.include.utility = true;
886
David Tolnay7db73692019-10-20 14:51:12 -0400887 let mut inner = String::new();
888 for name in &out.namespace {
889 inner += name;
890 inner += "::";
891 }
892 inner += &ident.to_string();
893 let instance = inner.replace("::", "$");
894
David Tolnay8c730492020-03-13 01:29:06 -0700895 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
896 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400897 writeln!(
898 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800899 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400900 inner,
901 );
902 writeln!(
903 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800904 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400905 inner,
906 );
907 writeln!(
908 out,
David Tolnay8c730492020-03-13 01:29:06 -0700909 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400910 instance, inner,
911 );
David Tolnay7e219b82020-03-01 13:14:51 -0800912 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400913 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700914 if types.structs.contains_key(ident) {
915 writeln!(
916 out,
917 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
918 instance, inner, inner,
919 );
920 writeln!(
921 out,
922 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
923 inner, inner,
924 );
925 writeln!(out, "}}");
926 }
David Tolnay7db73692019-10-20 14:51:12 -0400927 writeln!(
928 out,
David Tolnay8c730492020-03-13 01:29:06 -0700929 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400930 instance, inner, inner,
931 );
David Tolnay7e219b82020-03-01 13:14:51 -0800932 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400933 writeln!(out, "}}");
934 writeln!(
935 out,
David Tolnay8c730492020-03-13 01:29:06 -0700936 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400937 inner, instance, inner,
938 );
939 writeln!(out, " return ptr.get();");
940 writeln!(out, "}}");
941 writeln!(
942 out,
David Tolnay8c730492020-03-13 01:29:06 -0700943 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400944 inner, instance, inner,
945 );
946 writeln!(out, " return ptr.release();");
947 writeln!(out, "}}");
948 writeln!(
949 out,
David Tolnay8c730492020-03-13 01:29:06 -0700950 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400951 instance, inner,
952 );
953 writeln!(out, " ptr->~unique_ptr();");
954 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700955 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400956}