blob: f0bcaf96b5eaedda6cfe2fae339e2706e5d26716 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::include;
2use crate::gen::out::OutFile;
3use crate::syntax::atom::Atom::{self, *};
4use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
5use proc_macro2::Ident;
6
7pub(super) fn gen(namespace: Vec<String>, apis: &[Api], types: &Types, header: bool) -> OutFile {
8 let mut out_file = OutFile::new(namespace.clone(), header);
9 let out = &mut out_file;
10
11 if header {
12 writeln!(out, "#pragma once");
13 }
14
15 for api in apis {
16 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080017 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040018 }
19 }
20
21 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080022 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7db73692019-10-20 14:51:12 -040024 out.next_section();
25 for name in &namespace {
26 writeln!(out, "namespace {} {{", name);
27 }
28
David Tolnay7db73692019-10-20 14:51:12 -040029 out.next_section();
30 for api in apis {
31 match api {
32 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080033 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
34 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040035 _ => {}
36 }
37 }
38
39 for api in apis {
40 if let Api::Struct(strct) = api {
41 out.next_section();
42 write_struct(out, strct);
43 }
44 }
45
46 if !header {
47 out.begin_block("extern \"C\"");
48 for api in apis {
49 let (efn, write): (_, fn(_, _, _)) = match api {
50 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
51 Api::RustFunction(efn) => (efn, write_rust_function_decl),
52 _ => continue,
53 };
54 out.next_section();
55 write(out, efn, types);
56 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080057 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040058 }
59
60 for api in apis {
61 if let Api::RustFunction(efn) = api {
62 out.next_section();
63 write_rust_function_shim(out, efn, types);
64 }
65 }
66
67 out.next_section();
68 for name in namespace.iter().rev() {
69 writeln!(out, "}} // namespace {}", name);
70 }
71
72 if !header {
73 out.next_section();
74 write_generic_instantiations(out, types);
75 }
76
David Tolnay9c68b1a2020-03-06 11:12:55 -080077 out.prepend(out.include.to_string());
78
David Tolnay7db73692019-10-20 14:51:12 -040079 out_file
80}
81
82fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040083 for ty in types {
84 match ty {
85 Type::Ident(ident) => match Atom::from(ident) {
86 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay9c68b1a2020-03-06 11:12:55 -080087 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => out.include.cstdint = true,
88 Some(CxxString) => out.include.string = true,
David Tolnay7db73692019-10-20 14:51:12 -040089 Some(Bool) | Some(RustString) | None => {}
90 },
David Tolnay9c68b1a2020-03-06 11:12:55 -080091 Type::RustBox(_) => out.include.type_traits = true,
92 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -040093 _ => {}
94 }
95 }
David Tolnay7db73692019-10-20 14:51:12 -040096}
97
David Tolnayf51447e2020-03-06 14:14:27 -080098fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040099 let mut needs_rust_box = false;
100 for ty in types {
101 if let Type::RustBox(_) = ty {
102 needs_rust_box = true;
103 break;
104 }
105 }
106
David Tolnayf51447e2020-03-06 14:14:27 -0800107 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800108 let mut needs_maybe_uninit = false;
109 for api in apis {
David Tolnayf51447e2020-03-06 14:14:27 -0800110 if let Api::RustFunction(efn) = api {
111 for arg in &efn.args {
112 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
113 needs_manually_drop = true;
David Tolnay09011c32020-03-06 14:40:28 -0800114 break;
115 }
116 }
117 if let Some(ret) = &efn.ret {
118 if types.needs_indirect_abi(ret) {
119 needs_maybe_uninit = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800120 }
121 }
122 }
123 }
124
David Tolnay750755e2020-03-01 13:04:08 -0800125 out.begin_block("namespace rust");
126 out.begin_block("inline namespace cxxbridge01");
David Tolnayf51447e2020-03-06 14:14:27 -0800127
David Tolnay09011c32020-03-06 14:40:28 -0800128 if needs_rust_box || needs_manually_drop || needs_maybe_uninit {
David Tolnay7db73692019-10-20 14:51:12 -0400129 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800130 }
131
132 if needs_rust_box {
133 out.next_section();
David Tolnaye43b7372020-01-08 08:46:20 -0800134 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400135 if !line.trim_start().starts_with("//") {
136 writeln!(out, "{}", line);
137 }
138 }
139 }
David Tolnayf51447e2020-03-06 14:14:27 -0800140
141 if needs_manually_drop {
142 out.next_section();
143 writeln!(out, "template <typename T>");
144 writeln!(out, "union ManuallyDrop {{");
145 writeln!(out, " T value;");
146 writeln!(
147 out,
148 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
149 );
150 writeln!(out, " ~ManuallyDrop() {{}}");
151 writeln!(out, "}};");
152 }
153
David Tolnay09011c32020-03-06 14:40:28 -0800154 if needs_maybe_uninit {
155 out.next_section();
156 writeln!(out, "template <typename T>");
157 writeln!(out, "union MaybeUninit {{");
158 writeln!(out, " T value;");
159 writeln!(out, " MaybeUninit() {{}}");
160 writeln!(out, " ~MaybeUninit() {{}}");
161 writeln!(out, "}};");
162 }
163
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800164 out.end_block("namespace cxxbridge01");
165 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400166}
167
168fn write_struct(out: &mut OutFile, strct: &Struct) {
169 for line in strct.doc.to_string().lines() {
170 writeln!(out, "//{}", line);
171 }
172 writeln!(out, "struct {} final {{", strct.ident);
173 for field in &strct.fields {
174 write!(out, " ");
175 write_type_space(out, &field.ty);
176 writeln!(out, "{};", field.ident);
177 }
178 writeln!(out, "}};");
179}
180
181fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
182 writeln!(out, "struct {};", ident);
183}
184
David Tolnay8861bee2020-01-20 18:39:24 -0800185fn write_struct_using(out: &mut OutFile, ident: &Ident) {
186 writeln!(out, "using {} = {};", ident, ident);
187}
188
David Tolnay7db73692019-10-20 14:51:12 -0400189fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
190 let indirect_return = efn
191 .ret
192 .as_ref()
193 .map_or(false, |ret| types.needs_indirect_abi(ret));
194 write_extern_return_type(out, &efn.ret, types);
195 for name in out.namespace.clone() {
196 write!(out, "{}$", name);
197 }
David Tolnaye43b7372020-01-08 08:46:20 -0800198 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400199 for (i, arg) in efn.args.iter().enumerate() {
200 if i > 0 {
201 write!(out, ", ");
202 }
David Tolnaya46a2372020-03-06 10:03:48 -0800203 if arg.ty == RustString {
204 write!(out, "const ");
205 }
David Tolnay7db73692019-10-20 14:51:12 -0400206 write_extern_arg(out, arg, types);
207 }
208 if indirect_return {
209 if !efn.args.is_empty() {
210 write!(out, ", ");
211 }
212 write_return_type(out, &efn.ret);
213 write!(out, "*return$");
214 }
215 writeln!(out, ") noexcept {{");
216 write!(out, " ");
217 write_return_type(out, &efn.ret);
218 write!(out, "(*{}$)(", efn.ident);
219 for (i, arg) in efn.args.iter().enumerate() {
220 if i > 0 {
221 write!(out, ", ");
222 }
223 write_type(out, &arg.ty);
224 }
225 writeln!(out, ") = {};", efn.ident);
226 write!(out, " ");
227 if indirect_return {
228 write!(out, "new (return$) ");
229 write_type(out, efn.ret.as_ref().unwrap());
230 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800231 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400232 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800233 match ret {
234 Type::Ref(_) => write!(out, "&"),
235 Type::Str(_) => write!(out, "::rust::Str::Repr("),
236 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800237 }
David Tolnay7db73692019-10-20 14:51:12 -0400238 }
239 write!(out, "{}$(", efn.ident);
240 for (i, arg) in efn.args.iter().enumerate() {
241 if i > 0 {
242 write!(out, ", ");
243 }
244 if let Type::RustBox(_) = &arg.ty {
245 write_type(out, &arg.ty);
246 write!(out, "::from_raw({})", arg.ident);
247 } else if let Type::UniquePtr(_) = &arg.ty {
248 write_type(out, &arg.ty);
249 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800250 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800251 write!(
252 out,
253 "::rust::String(::rust::unsafe_bitcopy, *{})",
254 arg.ident,
255 );
David Tolnay7db73692019-10-20 14:51:12 -0400256 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800257 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400258 } else {
259 write!(out, "{}", arg.ident);
260 }
261 }
262 write!(out, ")");
263 match &efn.ret {
264 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
265 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800266 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400267 _ => {}
268 }
269 if indirect_return {
270 write!(out, ")");
271 }
272 writeln!(out, ";");
273 writeln!(out, "}}");
274}
275
276fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
277 write_extern_return_type(out, &efn.ret, types);
278 for name in out.namespace.clone() {
279 write!(out, "{}$", name);
280 }
David Tolnaye43b7372020-01-08 08:46:20 -0800281 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400282 for (i, arg) in efn.args.iter().enumerate() {
283 if i > 0 {
284 write!(out, ", ");
285 }
286 write_extern_arg(out, arg, types);
287 }
288 if efn
289 .ret
290 .as_ref()
291 .map_or(false, |ret| types.needs_indirect_abi(ret))
292 {
293 if !efn.args.is_empty() {
294 write!(out, ", ");
295 }
296 write_return_type(out, &efn.ret);
297 write!(out, "*return$");
298 }
299 writeln!(out, ") noexcept;");
300}
301
302fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
303 let indirect_return = efn
304 .ret
305 .as_ref()
306 .map_or(false, |ret| types.needs_indirect_abi(ret));
307 for line in efn.doc.to_string().lines() {
308 writeln!(out, "//{}", line);
309 }
310 write_return_type(out, &efn.ret);
311 write!(out, "{}(", efn.ident);
312 for (i, arg) in efn.args.iter().enumerate() {
313 if i > 0 {
314 write!(out, ", ");
315 }
316 write_type_space(out, &arg.ty);
317 write!(out, "{}", arg.ident);
318 }
319 write!(out, ") noexcept");
320 if out.header {
321 writeln!(out, ";");
322 } else {
323 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800324 for arg in &efn.args {
325 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
326 write!(out, " ::rust::ManuallyDrop<");
327 write_type(out, &arg.ty);
328 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
329 }
330 }
David Tolnay7db73692019-10-20 14:51:12 -0400331 write!(out, " ");
332 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800333 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400334 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800335 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400336 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800337 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400338 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800339 if let Type::Ref(_) = ret {
340 write!(out, "*");
341 }
David Tolnay7db73692019-10-20 14:51:12 -0400342 }
343 for name in out.namespace.clone() {
344 write!(out, "{}$", name);
345 }
David Tolnaye43b7372020-01-08 08:46:20 -0800346 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400347 for (i, arg) in efn.args.iter().enumerate() {
348 if i > 0 {
349 write!(out, ", ");
350 }
David Tolnaybaae4432020-03-01 20:20:10 -0800351 match &arg.ty {
352 Type::Str(_) => write!(out, "::rust::Str::Repr("),
353 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
354 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400355 }
356 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800357 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800358 Type::RustBox(_) => write!(out, ".into_raw()"),
359 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800360 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800361 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800362 _ => {}
363 }
David Tolnay7db73692019-10-20 14:51:12 -0400364 }
365 if indirect_return {
366 if !efn.args.is_empty() {
367 write!(out, ", ");
368 }
David Tolnay09011c32020-03-06 14:40:28 -0800369 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400370 }
371 writeln!(out, ");");
372 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800373 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400374 }
375 writeln!(out, "}}");
376 }
377}
378
379fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
380 match ty {
381 None => write!(out, "void "),
382 Some(ty) => write_type_space(out, ty),
383 }
384}
385
386fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
387 match ty {
388 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
389 write_type_space(out, &ty.inner);
390 write!(out, "*");
391 }
David Tolnay4a441222020-01-25 16:24:27 -0800392 Some(Type::Ref(ty)) => {
393 if ty.mutability.is_none() {
394 write!(out, "const ");
395 }
396 write_type(out, &ty.inner);
397 write!(out, " *");
398 }
David Tolnay750755e2020-03-01 13:04:08 -0800399 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400400 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
401 _ => write_return_type(out, ty),
402 }
403}
404
405fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
406 match &arg.ty {
407 Type::RustBox(ty) | Type::UniquePtr(ty) => {
408 write_type_space(out, &ty.inner);
409 write!(out, "*");
410 }
David Tolnay750755e2020-03-01 13:04:08 -0800411 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400412 _ => write_type_space(out, &arg.ty),
413 }
414 if types.needs_indirect_abi(&arg.ty) {
415 write!(out, "*");
416 }
417 write!(out, "{}", arg.ident);
418}
419
420fn write_type(out: &mut OutFile, ty: &Type) {
421 match ty {
422 Type::Ident(ident) => match Atom::from(ident) {
423 Some(Bool) => write!(out, "bool"),
424 Some(U8) => write!(out, "uint8_t"),
425 Some(U16) => write!(out, "uint16_t"),
426 Some(U32) => write!(out, "uint32_t"),
427 Some(U64) => write!(out, "uint64_t"),
428 Some(Usize) => write!(out, "size_t"),
429 Some(I8) => write!(out, "int8_t"),
430 Some(I16) => write!(out, "int16_t"),
431 Some(I32) => write!(out, "int32_t"),
432 Some(I64) => write!(out, "int64_t"),
433 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800434 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800435 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400436 None => write!(out, "{}", ident),
437 },
438 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800439 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400440 write_type(out, &ty.inner);
441 write!(out, ">");
442 }
443 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800444 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400445 write_type(out, &ptr.inner);
446 write!(out, ">");
447 }
448 Type::Ref(r) => {
449 if r.mutability.is_none() {
450 write!(out, "const ");
451 }
452 write_type(out, &r.inner);
453 write!(out, " &");
454 }
455 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800456 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400457 }
458 }
459}
460
461fn write_type_space(out: &mut OutFile, ty: &Type) {
462 write_type(out, ty);
463 match ty {
464 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
465 Type::Ref(_) => {}
466 }
467}
468
469fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
470 fn allow_unique_ptr(ident: &Ident) -> bool {
471 Atom::from(ident).is_none()
472 }
473
474 out.begin_block("extern \"C\"");
475 for ty in types {
476 if let Type::RustBox(ty) = ty {
477 if let Type::Ident(inner) = &ty.inner {
478 out.next_section();
479 write_rust_box_extern(out, inner);
480 }
481 } else if let Type::UniquePtr(ptr) = ty {
482 if let Type::Ident(inner) = &ptr.inner {
483 if allow_unique_ptr(inner) {
484 out.next_section();
485 write_unique_ptr(out, inner);
486 }
487 }
488 }
489 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800490 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400491
David Tolnay750755e2020-03-01 13:04:08 -0800492 out.begin_block("namespace rust");
493 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400494 for ty in types {
495 if let Type::RustBox(ty) = ty {
496 if let Type::Ident(inner) = &ty.inner {
497 write_rust_box_impl(out, inner);
498 }
499 }
500 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800501 out.end_block("namespace cxxbridge01");
502 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400503}
504
505fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
506 let mut inner = String::new();
507 for name in &out.namespace {
508 inner += name;
509 inner += "::";
510 }
511 inner += &ident.to_string();
512 let instance = inner.replace("::", "$");
513
David Tolnaye43b7372020-01-08 08:46:20 -0800514 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
515 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400516 writeln!(
517 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800518 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400519 instance, inner,
520 );
521 writeln!(
522 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800523 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400524 instance, inner,
525 );
David Tolnaye43b7372020-01-08 08:46:20 -0800526 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400527}
528
529fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
530 let mut inner = String::new();
531 for name in &out.namespace {
532 inner += name;
533 inner += "::";
534 }
535 inner += &ident.to_string();
536 let instance = inner.replace("::", "$");
537
538 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800539 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800540 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400541 writeln!(out, "}}");
542
543 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800544 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800545 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400546 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400547}
548
549fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
550 let mut inner = String::new();
551 for name in &out.namespace {
552 inner += name;
553 inner += "::";
554 }
555 inner += &ident.to_string();
556 let instance = inner.replace("::", "$");
557
David Tolnaye43b7372020-01-08 08:46:20 -0800558 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
559 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400560 writeln!(
561 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800562 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400563 inner,
564 );
565 writeln!(
566 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800567 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400568 inner,
569 );
570 writeln!(
571 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800572 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400573 instance, inner,
574 );
David Tolnay7e219b82020-03-01 13:14:51 -0800575 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400576 writeln!(out, "}}");
577 writeln!(
578 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800579 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400580 instance, inner, inner,
581 );
582 writeln!(
583 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800584 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400585 inner, inner,
586 );
587 writeln!(out, "}}");
588 writeln!(
589 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800590 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400591 instance, inner, inner,
592 );
David Tolnay7e219b82020-03-01 13:14:51 -0800593 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400594 writeln!(out, "}}");
595 writeln!(
596 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800597 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400598 inner, instance, inner,
599 );
600 writeln!(out, " return ptr.get();");
601 writeln!(out, "}}");
602 writeln!(
603 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800604 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400605 inner, instance, inner,
606 );
607 writeln!(out, " return ptr.release();");
608 writeln!(out, "}}");
609 writeln!(
610 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800611 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400612 instance, inner,
613 );
614 writeln!(out, " ptr->~unique_ptr();");
615 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800616 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400617}