blob: 601f7efca6fb26d290649e6f6606f9d7088f7e0c [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 Tolnay5cd8d612020-03-06 15:56:30 -0800339 match ret {
340 Type::RustBox(_) => {
341 write_type(out, ret);
342 write!(out, "::from_raw(");
343 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800344 Type::UniquePtr(_) => {
345 write_type(out, ret);
346 write!(out, "(");
347 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800348 Type::Ref(_) => write!(out, "*"),
349 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800350 }
David Tolnay7db73692019-10-20 14:51:12 -0400351 }
352 for name in out.namespace.clone() {
353 write!(out, "{}$", name);
354 }
David Tolnaye43b7372020-01-08 08:46:20 -0800355 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400356 for (i, arg) in efn.args.iter().enumerate() {
357 if i > 0 {
358 write!(out, ", ");
359 }
David Tolnaybaae4432020-03-01 20:20:10 -0800360 match &arg.ty {
361 Type::Str(_) => write!(out, "::rust::Str::Repr("),
362 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
363 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400364 }
365 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800366 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800367 Type::RustBox(_) => write!(out, ".into_raw()"),
368 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800369 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800370 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800371 _ => {}
372 }
David Tolnay7db73692019-10-20 14:51:12 -0400373 }
374 if indirect_return {
375 if !efn.args.is_empty() {
376 write!(out, ", ");
377 }
David Tolnay09011c32020-03-06 14:40:28 -0800378 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400379 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800380 write!(out, ")");
381 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800382 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800383 write!(out, ")");
384 }
385 }
386 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400387 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800388 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400389 }
390 writeln!(out, "}}");
391 }
392}
393
394fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
395 match ty {
396 None => write!(out, "void "),
397 Some(ty) => write_type_space(out, ty),
398 }
399}
400
401fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
402 match ty {
403 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
404 write_type_space(out, &ty.inner);
405 write!(out, "*");
406 }
David Tolnay4a441222020-01-25 16:24:27 -0800407 Some(Type::Ref(ty)) => {
408 if ty.mutability.is_none() {
409 write!(out, "const ");
410 }
411 write_type(out, &ty.inner);
412 write!(out, " *");
413 }
David Tolnay750755e2020-03-01 13:04:08 -0800414 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400415 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
416 _ => write_return_type(out, ty),
417 }
418}
419
420fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
421 match &arg.ty {
422 Type::RustBox(ty) | Type::UniquePtr(ty) => {
423 write_type_space(out, &ty.inner);
424 write!(out, "*");
425 }
David Tolnay750755e2020-03-01 13:04:08 -0800426 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400427 _ => write_type_space(out, &arg.ty),
428 }
429 if types.needs_indirect_abi(&arg.ty) {
430 write!(out, "*");
431 }
432 write!(out, "{}", arg.ident);
433}
434
435fn write_type(out: &mut OutFile, ty: &Type) {
436 match ty {
437 Type::Ident(ident) => match Atom::from(ident) {
438 Some(Bool) => write!(out, "bool"),
439 Some(U8) => write!(out, "uint8_t"),
440 Some(U16) => write!(out, "uint16_t"),
441 Some(U32) => write!(out, "uint32_t"),
442 Some(U64) => write!(out, "uint64_t"),
443 Some(Usize) => write!(out, "size_t"),
444 Some(I8) => write!(out, "int8_t"),
445 Some(I16) => write!(out, "int16_t"),
446 Some(I32) => write!(out, "int32_t"),
447 Some(I64) => write!(out, "int64_t"),
448 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800449 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800450 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400451 None => write!(out, "{}", ident),
452 },
453 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800454 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400455 write_type(out, &ty.inner);
456 write!(out, ">");
457 }
458 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800459 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400460 write_type(out, &ptr.inner);
461 write!(out, ">");
462 }
463 Type::Ref(r) => {
464 if r.mutability.is_none() {
465 write!(out, "const ");
466 }
467 write_type(out, &r.inner);
468 write!(out, " &");
469 }
470 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800471 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400472 }
473 }
474}
475
476fn write_type_space(out: &mut OutFile, ty: &Type) {
477 write_type(out, ty);
478 match ty {
479 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
480 Type::Ref(_) => {}
481 }
482}
483
484fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
485 fn allow_unique_ptr(ident: &Ident) -> bool {
486 Atom::from(ident).is_none()
487 }
488
489 out.begin_block("extern \"C\"");
490 for ty in types {
491 if let Type::RustBox(ty) = ty {
492 if let Type::Ident(inner) = &ty.inner {
493 out.next_section();
494 write_rust_box_extern(out, inner);
495 }
496 } else if let Type::UniquePtr(ptr) = ty {
497 if let Type::Ident(inner) = &ptr.inner {
498 if allow_unique_ptr(inner) {
499 out.next_section();
500 write_unique_ptr(out, inner);
501 }
502 }
503 }
504 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800505 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400506
David Tolnay750755e2020-03-01 13:04:08 -0800507 out.begin_block("namespace rust");
508 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400509 for ty in types {
510 if let Type::RustBox(ty) = ty {
511 if let Type::Ident(inner) = &ty.inner {
512 write_rust_box_impl(out, inner);
513 }
514 }
515 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800516 out.end_block("namespace cxxbridge01");
517 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400518}
519
520fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
521 let mut inner = String::new();
522 for name in &out.namespace {
523 inner += name;
524 inner += "::";
525 }
526 inner += &ident.to_string();
527 let instance = inner.replace("::", "$");
528
David Tolnaye43b7372020-01-08 08:46:20 -0800529 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
530 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400531 writeln!(
532 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800533 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400534 instance, inner,
535 );
536 writeln!(
537 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800538 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400539 instance, inner,
540 );
David Tolnaye43b7372020-01-08 08:46:20 -0800541 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400542}
543
544fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
545 let mut inner = String::new();
546 for name in &out.namespace {
547 inner += name;
548 inner += "::";
549 }
550 inner += &ident.to_string();
551 let instance = inner.replace("::", "$");
552
553 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800554 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800555 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400556 writeln!(out, "}}");
557
558 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800559 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800560 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400561 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400562}
563
564fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
565 let mut inner = String::new();
566 for name in &out.namespace {
567 inner += name;
568 inner += "::";
569 }
570 inner += &ident.to_string();
571 let instance = inner.replace("::", "$");
572
David Tolnaye43b7372020-01-08 08:46:20 -0800573 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
574 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400575 writeln!(
576 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800577 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400578 inner,
579 );
580 writeln!(
581 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800582 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400583 inner,
584 );
585 writeln!(
586 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800587 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400588 instance, inner,
589 );
David Tolnay7e219b82020-03-01 13:14:51 -0800590 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400591 writeln!(out, "}}");
592 writeln!(
593 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800594 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400595 instance, inner, inner,
596 );
597 writeln!(
598 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800599 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400600 inner, inner,
601 );
602 writeln!(out, "}}");
603 writeln!(
604 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800605 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400606 instance, inner, inner,
607 );
David Tolnay7e219b82020-03-01 13:14:51 -0800608 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400609 writeln!(out, "}}");
610 writeln!(
611 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800612 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400613 inner, instance, inner,
614 );
615 writeln!(out, " return ptr.get();");
616 writeln!(out, "}}");
617 writeln!(
618 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800619 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400620 inner, instance, inner,
621 );
622 writeln!(out, " return ptr.release();");
623 writeln!(out, "}}");
624 writeln!(
625 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800626 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400627 instance, inner,
628 );
629 writeln!(out, " ptr->~unique_ptr();");
630 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800631 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400632}