blob: 187bf5eeb66456b0249e6e2eeeb22b6e5c1e529d [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;
108 'outer: for api in apis {
109 if let Api::RustFunction(efn) = api {
110 for arg in &efn.args {
111 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
112 needs_manually_drop = true;
113 break 'outer;
114 }
115 }
116 }
117 }
118
David Tolnay750755e2020-03-01 13:04:08 -0800119 out.begin_block("namespace rust");
120 out.begin_block("inline namespace cxxbridge01");
David Tolnayf51447e2020-03-06 14:14:27 -0800121
122 if needs_rust_box || needs_manually_drop {
David Tolnay7db73692019-10-20 14:51:12 -0400123 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800124 }
125
126 if needs_rust_box {
127 out.next_section();
David Tolnaye43b7372020-01-08 08:46:20 -0800128 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400129 if !line.trim_start().starts_with("//") {
130 writeln!(out, "{}", line);
131 }
132 }
133 }
David Tolnayf51447e2020-03-06 14:14:27 -0800134
135 if needs_manually_drop {
136 out.next_section();
137 writeln!(out, "template <typename T>");
138 writeln!(out, "union ManuallyDrop {{");
139 writeln!(out, " T value;");
140 writeln!(
141 out,
142 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
143 );
144 writeln!(out, " ~ManuallyDrop() {{}}");
145 writeln!(out, "}};");
146 }
147
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800148 out.end_block("namespace cxxbridge01");
149 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400150}
151
152fn write_struct(out: &mut OutFile, strct: &Struct) {
153 for line in strct.doc.to_string().lines() {
154 writeln!(out, "//{}", line);
155 }
156 writeln!(out, "struct {} final {{", strct.ident);
157 for field in &strct.fields {
158 write!(out, " ");
159 write_type_space(out, &field.ty);
160 writeln!(out, "{};", field.ident);
161 }
162 writeln!(out, "}};");
163}
164
165fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
166 writeln!(out, "struct {};", ident);
167}
168
David Tolnay8861bee2020-01-20 18:39:24 -0800169fn write_struct_using(out: &mut OutFile, ident: &Ident) {
170 writeln!(out, "using {} = {};", ident, ident);
171}
172
David Tolnay7db73692019-10-20 14:51:12 -0400173fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
174 let indirect_return = efn
175 .ret
176 .as_ref()
177 .map_or(false, |ret| types.needs_indirect_abi(ret));
178 write_extern_return_type(out, &efn.ret, types);
179 for name in out.namespace.clone() {
180 write!(out, "{}$", name);
181 }
David Tolnaye43b7372020-01-08 08:46:20 -0800182 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400183 for (i, arg) in efn.args.iter().enumerate() {
184 if i > 0 {
185 write!(out, ", ");
186 }
David Tolnaya46a2372020-03-06 10:03:48 -0800187 if arg.ty == RustString {
188 write!(out, "const ");
189 }
David Tolnay7db73692019-10-20 14:51:12 -0400190 write_extern_arg(out, arg, types);
191 }
192 if indirect_return {
193 if !efn.args.is_empty() {
194 write!(out, ", ");
195 }
196 write_return_type(out, &efn.ret);
197 write!(out, "*return$");
198 }
199 writeln!(out, ") noexcept {{");
200 write!(out, " ");
201 write_return_type(out, &efn.ret);
202 write!(out, "(*{}$)(", efn.ident);
203 for (i, arg) in efn.args.iter().enumerate() {
204 if i > 0 {
205 write!(out, ", ");
206 }
207 write_type(out, &arg.ty);
208 }
209 writeln!(out, ") = {};", efn.ident);
210 write!(out, " ");
211 if indirect_return {
212 write!(out, "new (return$) ");
213 write_type(out, efn.ret.as_ref().unwrap());
214 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800215 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400216 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800217 match ret {
218 Type::Ref(_) => write!(out, "&"),
219 Type::Str(_) => write!(out, "::rust::Str::Repr("),
220 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800221 }
David Tolnay7db73692019-10-20 14:51:12 -0400222 }
223 write!(out, "{}$(", efn.ident);
224 for (i, arg) in efn.args.iter().enumerate() {
225 if i > 0 {
226 write!(out, ", ");
227 }
228 if let Type::RustBox(_) = &arg.ty {
229 write_type(out, &arg.ty);
230 write!(out, "::from_raw({})", arg.ident);
231 } else if let Type::UniquePtr(_) = &arg.ty {
232 write_type(out, &arg.ty);
233 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800234 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800235 write!(
236 out,
237 "::rust::String(::rust::unsafe_bitcopy, *{})",
238 arg.ident,
239 );
David Tolnay7db73692019-10-20 14:51:12 -0400240 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800241 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400242 } else {
243 write!(out, "{}", arg.ident);
244 }
245 }
246 write!(out, ")");
247 match &efn.ret {
248 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
249 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800250 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400251 _ => {}
252 }
253 if indirect_return {
254 write!(out, ")");
255 }
256 writeln!(out, ";");
257 writeln!(out, "}}");
258}
259
260fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
261 write_extern_return_type(out, &efn.ret, types);
262 for name in out.namespace.clone() {
263 write!(out, "{}$", name);
264 }
David Tolnaye43b7372020-01-08 08:46:20 -0800265 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400266 for (i, arg) in efn.args.iter().enumerate() {
267 if i > 0 {
268 write!(out, ", ");
269 }
270 write_extern_arg(out, arg, types);
271 }
272 if efn
273 .ret
274 .as_ref()
275 .map_or(false, |ret| types.needs_indirect_abi(ret))
276 {
277 if !efn.args.is_empty() {
278 write!(out, ", ");
279 }
280 write_return_type(out, &efn.ret);
281 write!(out, "*return$");
282 }
283 writeln!(out, ") noexcept;");
284}
285
286fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
287 let indirect_return = efn
288 .ret
289 .as_ref()
290 .map_or(false, |ret| types.needs_indirect_abi(ret));
291 for line in efn.doc.to_string().lines() {
292 writeln!(out, "//{}", line);
293 }
294 write_return_type(out, &efn.ret);
295 write!(out, "{}(", efn.ident);
296 for (i, arg) in efn.args.iter().enumerate() {
297 if i > 0 {
298 write!(out, ", ");
299 }
300 write_type_space(out, &arg.ty);
301 write!(out, "{}", arg.ident);
302 }
303 write!(out, ") noexcept");
304 if out.header {
305 writeln!(out, ";");
306 } else {
307 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800308 for arg in &efn.args {
309 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
310 write!(out, " ::rust::ManuallyDrop<");
311 write_type(out, &arg.ty);
312 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
313 }
314 }
David Tolnay7db73692019-10-20 14:51:12 -0400315 write!(out, " ");
316 if indirect_return {
317 write!(out, "char return$[sizeof(");
318 write_type(out, efn.ret.as_ref().unwrap());
319 writeln!(out, ")];");
320 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800321 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400322 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800323 if let Type::Ref(_) = ret {
324 write!(out, "*");
325 }
David Tolnay7db73692019-10-20 14:51:12 -0400326 }
327 for name in out.namespace.clone() {
328 write!(out, "{}$", name);
329 }
David Tolnaye43b7372020-01-08 08:46:20 -0800330 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400331 for (i, arg) in efn.args.iter().enumerate() {
332 if i > 0 {
333 write!(out, ", ");
334 }
David Tolnaybaae4432020-03-01 20:20:10 -0800335 match &arg.ty {
336 Type::Str(_) => write!(out, "::rust::Str::Repr("),
337 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
338 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400339 }
340 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800341 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800342 Type::RustBox(_) => write!(out, ".into_raw()"),
343 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800344 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800345 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800346 _ => {}
347 }
David Tolnay7db73692019-10-20 14:51:12 -0400348 }
349 if indirect_return {
350 if !efn.args.is_empty() {
351 write!(out, ", ");
352 }
353 write!(out, "reinterpret_cast<");
354 write_return_type(out, &efn.ret);
355 write!(out, "*>(return$)");
356 }
357 writeln!(out, ");");
358 if indirect_return {
359 write!(out, " return ");
360 write_type(out, efn.ret.as_ref().unwrap());
361 write!(out, "(*reinterpret_cast<");
362 write_return_type(out, &efn.ret);
363 writeln!(out, "*>(return$));");
364 }
365 writeln!(out, "}}");
366 }
367}
368
369fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
370 match ty {
371 None => write!(out, "void "),
372 Some(ty) => write_type_space(out, ty),
373 }
374}
375
376fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
377 match ty {
378 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
379 write_type_space(out, &ty.inner);
380 write!(out, "*");
381 }
David Tolnay4a441222020-01-25 16:24:27 -0800382 Some(Type::Ref(ty)) => {
383 if ty.mutability.is_none() {
384 write!(out, "const ");
385 }
386 write_type(out, &ty.inner);
387 write!(out, " *");
388 }
David Tolnay750755e2020-03-01 13:04:08 -0800389 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400390 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
391 _ => write_return_type(out, ty),
392 }
393}
394
395fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
396 match &arg.ty {
397 Type::RustBox(ty) | Type::UniquePtr(ty) => {
398 write_type_space(out, &ty.inner);
399 write!(out, "*");
400 }
David Tolnay750755e2020-03-01 13:04:08 -0800401 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400402 _ => write_type_space(out, &arg.ty),
403 }
404 if types.needs_indirect_abi(&arg.ty) {
405 write!(out, "*");
406 }
407 write!(out, "{}", arg.ident);
408}
409
410fn write_type(out: &mut OutFile, ty: &Type) {
411 match ty {
412 Type::Ident(ident) => match Atom::from(ident) {
413 Some(Bool) => write!(out, "bool"),
414 Some(U8) => write!(out, "uint8_t"),
415 Some(U16) => write!(out, "uint16_t"),
416 Some(U32) => write!(out, "uint32_t"),
417 Some(U64) => write!(out, "uint64_t"),
418 Some(Usize) => write!(out, "size_t"),
419 Some(I8) => write!(out, "int8_t"),
420 Some(I16) => write!(out, "int16_t"),
421 Some(I32) => write!(out, "int32_t"),
422 Some(I64) => write!(out, "int64_t"),
423 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800424 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800425 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400426 None => write!(out, "{}", ident),
427 },
428 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800429 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400430 write_type(out, &ty.inner);
431 write!(out, ">");
432 }
433 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800434 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400435 write_type(out, &ptr.inner);
436 write!(out, ">");
437 }
438 Type::Ref(r) => {
439 if r.mutability.is_none() {
440 write!(out, "const ");
441 }
442 write_type(out, &r.inner);
443 write!(out, " &");
444 }
445 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800446 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400447 }
448 }
449}
450
451fn write_type_space(out: &mut OutFile, ty: &Type) {
452 write_type(out, ty);
453 match ty {
454 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
455 Type::Ref(_) => {}
456 }
457}
458
459fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
460 fn allow_unique_ptr(ident: &Ident) -> bool {
461 Atom::from(ident).is_none()
462 }
463
464 out.begin_block("extern \"C\"");
465 for ty in types {
466 if let Type::RustBox(ty) = ty {
467 if let Type::Ident(inner) = &ty.inner {
468 out.next_section();
469 write_rust_box_extern(out, inner);
470 }
471 } else if let Type::UniquePtr(ptr) = ty {
472 if let Type::Ident(inner) = &ptr.inner {
473 if allow_unique_ptr(inner) {
474 out.next_section();
475 write_unique_ptr(out, inner);
476 }
477 }
478 }
479 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800480 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400481
David Tolnay750755e2020-03-01 13:04:08 -0800482 out.begin_block("namespace rust");
483 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400484 for ty in types {
485 if let Type::RustBox(ty) = ty {
486 if let Type::Ident(inner) = &ty.inner {
487 write_rust_box_impl(out, inner);
488 }
489 }
490 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800491 out.end_block("namespace cxxbridge01");
492 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400493}
494
495fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
496 let mut inner = String::new();
497 for name in &out.namespace {
498 inner += name;
499 inner += "::";
500 }
501 inner += &ident.to_string();
502 let instance = inner.replace("::", "$");
503
David Tolnaye43b7372020-01-08 08:46:20 -0800504 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
505 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400506 writeln!(
507 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800508 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400509 instance, inner,
510 );
511 writeln!(
512 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800513 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400514 instance, inner,
515 );
David Tolnaye43b7372020-01-08 08:46:20 -0800516 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400517}
518
519fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
520 let mut inner = String::new();
521 for name in &out.namespace {
522 inner += name;
523 inner += "::";
524 }
525 inner += &ident.to_string();
526 let instance = inner.replace("::", "$");
527
528 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800529 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800530 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400531 writeln!(out, "}}");
532
533 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800534 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800535 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400536 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400537}
538
539fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
540 let mut inner = String::new();
541 for name in &out.namespace {
542 inner += name;
543 inner += "::";
544 }
545 inner += &ident.to_string();
546 let instance = inner.replace("::", "$");
547
David Tolnaye43b7372020-01-08 08:46:20 -0800548 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
549 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400550 writeln!(
551 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800552 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400553 inner,
554 );
555 writeln!(
556 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800557 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400558 inner,
559 );
560 writeln!(
561 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800562 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400563 instance, inner,
564 );
David Tolnay7e219b82020-03-01 13:14:51 -0800565 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400566 writeln!(out, "}}");
567 writeln!(
568 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800569 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400570 instance, inner, inner,
571 );
572 writeln!(
573 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800574 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400575 inner, inner,
576 );
577 writeln!(out, "}}");
578 writeln!(
579 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800580 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400581 instance, inner, inner,
582 );
David Tolnay7e219b82020-03-01 13:14:51 -0800583 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400584 writeln!(out, "}}");
585 writeln!(
586 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800587 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400588 inner, instance, inner,
589 );
590 writeln!(out, " return ptr.get();");
591 writeln!(out, "}}");
592 writeln!(
593 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800594 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400595 inner, instance, inner,
596 );
597 writeln!(out, " return ptr.release();");
598 writeln!(out, "}}");
599 writeln!(
600 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800601 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400602 instance, inner,
603 );
604 writeln!(out, " ptr->~unique_ptr();");
605 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800606 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400607}