blob: e6fb4e9a9266bdf96272698c888d145717b78b5b [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 {
17 writeln!(out, "#include \"{}\"", include.value().escape_default());
18 }
19 }
20
21 write_includes(out, types);
22 write_include_cxxbridge(out, types);
23
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
77 out_file
78}
79
80fn write_includes(out: &mut OutFile, types: &Types) {
81 let mut has_int = false;
David Tolnayf6292372020-03-01 21:09:11 -080082 let mut has_box = false;
David Tolnay7db73692019-10-20 14:51:12 -040083 let mut has_unique_ptr = false;
84 let mut has_string = false;
85
86 for ty in types {
87 match ty {
88 Type::Ident(ident) => match Atom::from(ident) {
89 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
90 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => has_int = true,
91 Some(CxxString) => has_string = true,
92 Some(Bool) | Some(RustString) | None => {}
93 },
David Tolnayf6292372020-03-01 21:09:11 -080094 Type::RustBox(_) => has_box = true,
David Tolnay7db73692019-10-20 14:51:12 -040095 Type::UniquePtr(_) => has_unique_ptr = true,
96 _ => {}
97 }
98 }
99
100 if has_int {
101 writeln!(out, "#include <cstdint>");
102 }
103 if has_unique_ptr {
104 writeln!(out, "#include <memory>");
105 }
106 if has_string {
107 writeln!(out, "#include <string>");
108 }
David Tolnayf6292372020-03-01 21:09:11 -0800109 if has_box {
110 writeln!(out, "#include <type_traits>");
111 }
David Tolnay7db73692019-10-20 14:51:12 -0400112}
113
114fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
115 let mut needs_rust_box = false;
116 for ty in types {
117 if let Type::RustBox(_) = ty {
118 needs_rust_box = true;
119 break;
120 }
121 }
122
David Tolnay750755e2020-03-01 13:04:08 -0800123 out.begin_block("namespace rust");
124 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400125 if needs_rust_box {
126 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnaye43b7372020-01-08 08:46:20 -0800127 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400128 if !line.trim_start().starts_with("//") {
129 writeln!(out, "{}", line);
130 }
131 }
132 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800133 out.end_block("namespace cxxbridge01");
134 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400135}
136
137fn write_struct(out: &mut OutFile, strct: &Struct) {
138 for line in strct.doc.to_string().lines() {
139 writeln!(out, "//{}", line);
140 }
141 writeln!(out, "struct {} final {{", strct.ident);
142 for field in &strct.fields {
143 write!(out, " ");
144 write_type_space(out, &field.ty);
145 writeln!(out, "{};", field.ident);
146 }
147 writeln!(out, "}};");
148}
149
150fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
151 writeln!(out, "struct {};", ident);
152}
153
David Tolnay8861bee2020-01-20 18:39:24 -0800154fn write_struct_using(out: &mut OutFile, ident: &Ident) {
155 writeln!(out, "using {} = {};", ident, ident);
156}
157
David Tolnay7db73692019-10-20 14:51:12 -0400158fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
159 let indirect_return = efn
160 .ret
161 .as_ref()
162 .map_or(false, |ret| types.needs_indirect_abi(ret));
163 write_extern_return_type(out, &efn.ret, types);
164 for name in out.namespace.clone() {
165 write!(out, "{}$", name);
166 }
David Tolnaye43b7372020-01-08 08:46:20 -0800167 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400168 for (i, arg) in efn.args.iter().enumerate() {
169 if i > 0 {
170 write!(out, ", ");
171 }
172 write_extern_arg(out, arg, types);
173 }
174 if indirect_return {
175 if !efn.args.is_empty() {
176 write!(out, ", ");
177 }
178 write_return_type(out, &efn.ret);
179 write!(out, "*return$");
180 }
181 writeln!(out, ") noexcept {{");
182 write!(out, " ");
183 write_return_type(out, &efn.ret);
184 write!(out, "(*{}$)(", efn.ident);
185 for (i, arg) in efn.args.iter().enumerate() {
186 if i > 0 {
187 write!(out, ", ");
188 }
189 write_type(out, &arg.ty);
190 }
191 writeln!(out, ") = {};", efn.ident);
192 write!(out, " ");
193 if indirect_return {
194 write!(out, "new (return$) ");
195 write_type(out, efn.ret.as_ref().unwrap());
196 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800197 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400198 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800199 match ret {
200 Type::Ref(_) => write!(out, "&"),
201 Type::Str(_) => write!(out, "::rust::Str::Repr("),
202 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800203 }
David Tolnay7db73692019-10-20 14:51:12 -0400204 }
205 write!(out, "{}$(", efn.ident);
206 for (i, arg) in efn.args.iter().enumerate() {
207 if i > 0 {
208 write!(out, ", ");
209 }
210 if let Type::RustBox(_) = &arg.ty {
211 write_type(out, &arg.ty);
212 write!(out, "::from_raw({})", arg.ident);
213 } else if let Type::UniquePtr(_) = &arg.ty {
214 write_type(out, &arg.ty);
215 write!(out, "({})", arg.ident);
216 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800217 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400218 } else {
219 write!(out, "{}", arg.ident);
220 }
221 }
222 write!(out, ")");
223 match &efn.ret {
224 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
225 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800226 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400227 _ => {}
228 }
229 if indirect_return {
230 write!(out, ")");
231 }
232 writeln!(out, ";");
233 writeln!(out, "}}");
234}
235
236fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
237 write_extern_return_type(out, &efn.ret, types);
238 for name in out.namespace.clone() {
239 write!(out, "{}$", name);
240 }
David Tolnaye43b7372020-01-08 08:46:20 -0800241 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400242 for (i, arg) in efn.args.iter().enumerate() {
243 if i > 0 {
244 write!(out, ", ");
245 }
246 write_extern_arg(out, arg, types);
247 }
248 if efn
249 .ret
250 .as_ref()
251 .map_or(false, |ret| types.needs_indirect_abi(ret))
252 {
253 if !efn.args.is_empty() {
254 write!(out, ", ");
255 }
256 write_return_type(out, &efn.ret);
257 write!(out, "*return$");
258 }
259 writeln!(out, ") noexcept;");
260}
261
262fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
263 let indirect_return = efn
264 .ret
265 .as_ref()
266 .map_or(false, |ret| types.needs_indirect_abi(ret));
267 for line in efn.doc.to_string().lines() {
268 writeln!(out, "//{}", line);
269 }
270 write_return_type(out, &efn.ret);
271 write!(out, "{}(", efn.ident);
272 for (i, arg) in efn.args.iter().enumerate() {
273 if i > 0 {
274 write!(out, ", ");
275 }
276 write_type_space(out, &arg.ty);
277 write!(out, "{}", arg.ident);
278 }
279 write!(out, ") noexcept");
280 if out.header {
281 writeln!(out, ";");
282 } else {
283 writeln!(out, " {{");
284 write!(out, " ");
285 if indirect_return {
286 write!(out, "char return$[sizeof(");
287 write_type(out, efn.ret.as_ref().unwrap());
288 writeln!(out, ")];");
289 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800290 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400291 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800292 if let Type::Ref(_) = ret {
293 write!(out, "*");
294 }
David Tolnay7db73692019-10-20 14:51:12 -0400295 }
296 for name in out.namespace.clone() {
297 write!(out, "{}$", name);
298 }
David Tolnaye43b7372020-01-08 08:46:20 -0800299 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400300 for (i, arg) in efn.args.iter().enumerate() {
301 if i > 0 {
302 write!(out, ", ");
303 }
David Tolnaybaae4432020-03-01 20:20:10 -0800304 match &arg.ty {
305 Type::Str(_) => write!(out, "::rust::Str::Repr("),
306 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
307 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400308 }
309 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800310 match arg.ty {
311 Type::RustBox(_) => write!(out, ".into_raw()"),
312 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800313 Type::Str(_) => write!(out, ")"),
David Tolnay17955e22020-01-20 17:58:24 -0800314 _ => {}
315 }
David Tolnay7db73692019-10-20 14:51:12 -0400316 }
317 if indirect_return {
318 if !efn.args.is_empty() {
319 write!(out, ", ");
320 }
321 write!(out, "reinterpret_cast<");
322 write_return_type(out, &efn.ret);
323 write!(out, "*>(return$)");
324 }
325 writeln!(out, ");");
326 if indirect_return {
327 write!(out, " return ");
328 write_type(out, efn.ret.as_ref().unwrap());
329 write!(out, "(*reinterpret_cast<");
330 write_return_type(out, &efn.ret);
331 writeln!(out, "*>(return$));");
332 }
333 writeln!(out, "}}");
334 }
335}
336
337fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
338 match ty {
339 None => write!(out, "void "),
340 Some(ty) => write_type_space(out, ty),
341 }
342}
343
344fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
345 match ty {
346 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
347 write_type_space(out, &ty.inner);
348 write!(out, "*");
349 }
David Tolnay4a441222020-01-25 16:24:27 -0800350 Some(Type::Ref(ty)) => {
351 if ty.mutability.is_none() {
352 write!(out, "const ");
353 }
354 write_type(out, &ty.inner);
355 write!(out, " *");
356 }
David Tolnay750755e2020-03-01 13:04:08 -0800357 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400358 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
359 _ => write_return_type(out, ty),
360 }
361}
362
363fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
364 match &arg.ty {
365 Type::RustBox(ty) | Type::UniquePtr(ty) => {
366 write_type_space(out, &ty.inner);
367 write!(out, "*");
368 }
David Tolnay750755e2020-03-01 13:04:08 -0800369 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400370 _ => write_type_space(out, &arg.ty),
371 }
372 if types.needs_indirect_abi(&arg.ty) {
373 write!(out, "*");
374 }
375 write!(out, "{}", arg.ident);
376}
377
378fn write_type(out: &mut OutFile, ty: &Type) {
379 match ty {
380 Type::Ident(ident) => match Atom::from(ident) {
381 Some(Bool) => write!(out, "bool"),
382 Some(U8) => write!(out, "uint8_t"),
383 Some(U16) => write!(out, "uint16_t"),
384 Some(U32) => write!(out, "uint32_t"),
385 Some(U64) => write!(out, "uint64_t"),
386 Some(Usize) => write!(out, "size_t"),
387 Some(I8) => write!(out, "int8_t"),
388 Some(I16) => write!(out, "int16_t"),
389 Some(I32) => write!(out, "int32_t"),
390 Some(I64) => write!(out, "int64_t"),
391 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800392 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800393 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400394 None => write!(out, "{}", ident),
395 },
396 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800397 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400398 write_type(out, &ty.inner);
399 write!(out, ">");
400 }
401 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800402 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400403 write_type(out, &ptr.inner);
404 write!(out, ">");
405 }
406 Type::Ref(r) => {
407 if r.mutability.is_none() {
408 write!(out, "const ");
409 }
410 write_type(out, &r.inner);
411 write!(out, " &");
412 }
413 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800414 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400415 }
416 }
417}
418
419fn write_type_space(out: &mut OutFile, ty: &Type) {
420 write_type(out, ty);
421 match ty {
422 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
423 Type::Ref(_) => {}
424 }
425}
426
427fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
428 fn allow_unique_ptr(ident: &Ident) -> bool {
429 Atom::from(ident).is_none()
430 }
431
432 out.begin_block("extern \"C\"");
433 for ty in types {
434 if let Type::RustBox(ty) = ty {
435 if let Type::Ident(inner) = &ty.inner {
436 out.next_section();
437 write_rust_box_extern(out, inner);
438 }
439 } else if let Type::UniquePtr(ptr) = ty {
440 if let Type::Ident(inner) = &ptr.inner {
441 if allow_unique_ptr(inner) {
442 out.next_section();
443 write_unique_ptr(out, inner);
444 }
445 }
446 }
447 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800448 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400449
David Tolnay750755e2020-03-01 13:04:08 -0800450 out.begin_block("namespace rust");
451 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400452 for ty in types {
453 if let Type::RustBox(ty) = ty {
454 if let Type::Ident(inner) = &ty.inner {
455 write_rust_box_impl(out, inner);
456 }
457 }
458 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800459 out.end_block("namespace cxxbridge01");
460 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400461}
462
463fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
464 let mut inner = String::new();
465 for name in &out.namespace {
466 inner += name;
467 inner += "::";
468 }
469 inner += &ident.to_string();
470 let instance = inner.replace("::", "$");
471
David Tolnaye43b7372020-01-08 08:46:20 -0800472 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
473 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400474 writeln!(
475 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800476 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400477 instance, inner,
478 );
479 writeln!(
480 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800481 "void cxxbridge01$box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400482 instance, inner, inner
483 );
484 writeln!(
485 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800486 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400487 instance, inner,
488 );
489 writeln!(
490 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800491 "const {} *cxxbridge01$box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400492 inner, instance, inner,
493 );
494 writeln!(
495 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800496 "{} *cxxbridge01$box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400497 inner, instance, inner,
498 );
David Tolnaye43b7372020-01-08 08:46:20 -0800499 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400500}
501
502fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
503 let mut inner = String::new();
504 for name in &out.namespace {
505 inner += name;
506 inner += "::";
507 }
508 inner += &ident.to_string();
509 let instance = inner.replace("::", "$");
510
511 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800512 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800513 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400514 writeln!(out, "}}");
515
516 writeln!(out, "template <>");
517 writeln!(
518 out,
David Tolnay324437a2020-03-01 13:02:24 -0800519 "void Box<{}>::set_raw({} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400520 inner, inner,
521 );
522 writeln!(
523 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800524 " return cxxbridge01$box${}$set_raw(this, raw);",
David Tolnay7db73692019-10-20 14:51:12 -0400525 instance
526 );
527 writeln!(out, "}}");
528
529 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800530 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800531 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400532 writeln!(out, "}}");
533
534 writeln!(out, "template <>");
535 writeln!(
536 out,
David Tolnay324437a2020-03-01 13:02:24 -0800537 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400538 inner, inner,
539 );
David Tolnay9081beb2020-03-01 19:51:46 -0800540 writeln!(out, " return cxxbridge01$box${}$deref(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, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400545 writeln!(
546 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800547 " return cxxbridge01$box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400548 instance
549 );
550 writeln!(out, "}}");
551}
552
553fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
554 let mut inner = String::new();
555 for name in &out.namespace {
556 inner += name;
557 inner += "::";
558 }
559 inner += &ident.to_string();
560 let instance = inner.replace("::", "$");
561
David Tolnaye43b7372020-01-08 08:46:20 -0800562 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
563 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400564 writeln!(
565 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800566 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400567 inner,
568 );
569 writeln!(
570 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800571 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400572 inner,
573 );
574 writeln!(
575 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800576 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400577 instance, inner,
578 );
David Tolnay7e219b82020-03-01 13:14:51 -0800579 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400580 writeln!(out, "}}");
581 writeln!(
582 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800583 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400584 instance, inner, inner,
585 );
586 writeln!(
587 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800588 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400589 inner, inner,
590 );
591 writeln!(out, "}}");
592 writeln!(
593 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800594 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400595 instance, inner, inner,
596 );
David Tolnay7e219b82020-03-01 13:14:51 -0800597 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400598 writeln!(out, "}}");
599 writeln!(
600 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800601 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400602 inner, instance, inner,
603 );
604 writeln!(out, " return ptr.get();");
605 writeln!(out, "}}");
606 writeln!(
607 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800608 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400609 inner, instance, inner,
610 );
611 writeln!(out, " return ptr.release();");
612 writeln!(out, "}}");
613 writeln!(
614 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800615 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400616 instance, inner,
617 );
618 writeln!(out, " ptr->~unique_ptr();");
619 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800620 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400621}