blob: 99ea418ac999aa718abe1318aca30d3a534329f5 [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;
82 let mut has_unique_ptr = false;
83 let mut has_string = false;
84
85 for ty in types {
86 match ty {
87 Type::Ident(ident) => match Atom::from(ident) {
88 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
89 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => has_int = true,
90 Some(CxxString) => has_string = true,
91 Some(Bool) | Some(RustString) | None => {}
92 },
93 Type::UniquePtr(_) => has_unique_ptr = true,
94 _ => {}
95 }
96 }
97
98 if has_int {
99 writeln!(out, "#include <cstdint>");
100 }
101 if has_unique_ptr {
102 writeln!(out, "#include <memory>");
103 }
104 if has_string {
105 writeln!(out, "#include <string>");
106 }
107}
108
109fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
110 let mut needs_rust_box = false;
111 for ty in types {
112 if let Type::RustBox(_) = ty {
113 needs_rust_box = true;
114 break;
115 }
116 }
117
David Tolnay750755e2020-03-01 13:04:08 -0800118 out.begin_block("namespace rust");
119 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400120 if needs_rust_box {
121 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnaye43b7372020-01-08 08:46:20 -0800122 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400123 if !line.trim_start().starts_with("//") {
124 writeln!(out, "{}", line);
125 }
126 }
127 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800128 out.end_block("namespace cxxbridge01");
129 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400130}
131
132fn write_struct(out: &mut OutFile, strct: &Struct) {
133 for line in strct.doc.to_string().lines() {
134 writeln!(out, "//{}", line);
135 }
136 writeln!(out, "struct {} final {{", strct.ident);
137 for field in &strct.fields {
138 write!(out, " ");
139 write_type_space(out, &field.ty);
140 writeln!(out, "{};", field.ident);
141 }
142 writeln!(out, "}};");
143}
144
145fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
146 writeln!(out, "struct {};", ident);
147}
148
David Tolnay8861bee2020-01-20 18:39:24 -0800149fn write_struct_using(out: &mut OutFile, ident: &Ident) {
150 writeln!(out, "using {} = {};", ident, ident);
151}
152
David Tolnay7db73692019-10-20 14:51:12 -0400153fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
154 let indirect_return = efn
155 .ret
156 .as_ref()
157 .map_or(false, |ret| types.needs_indirect_abi(ret));
158 write_extern_return_type(out, &efn.ret, types);
159 for name in out.namespace.clone() {
160 write!(out, "{}$", name);
161 }
David Tolnaye43b7372020-01-08 08:46:20 -0800162 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400163 for (i, arg) in efn.args.iter().enumerate() {
164 if i > 0 {
165 write!(out, ", ");
166 }
167 write_extern_arg(out, arg, types);
168 }
169 if indirect_return {
170 if !efn.args.is_empty() {
171 write!(out, ", ");
172 }
173 write_return_type(out, &efn.ret);
174 write!(out, "*return$");
175 }
176 writeln!(out, ") noexcept {{");
177 write!(out, " ");
178 write_return_type(out, &efn.ret);
179 write!(out, "(*{}$)(", efn.ident);
180 for (i, arg) in efn.args.iter().enumerate() {
181 if i > 0 {
182 write!(out, ", ");
183 }
184 write_type(out, &arg.ty);
185 }
186 writeln!(out, ") = {};", efn.ident);
187 write!(out, " ");
188 if indirect_return {
189 write!(out, "new (return$) ");
190 write_type(out, efn.ret.as_ref().unwrap());
191 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800192 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400193 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800194 if let Type::Ref(_) = ret {
195 write!(out, "&");
196 }
David Tolnay7db73692019-10-20 14:51:12 -0400197 }
198 write!(out, "{}$(", efn.ident);
199 for (i, arg) in efn.args.iter().enumerate() {
200 if i > 0 {
201 write!(out, ", ");
202 }
203 if let Type::RustBox(_) = &arg.ty {
204 write_type(out, &arg.ty);
205 write!(out, "::from_raw({})", arg.ident);
206 } else if let Type::UniquePtr(_) = &arg.ty {
207 write_type(out, &arg.ty);
208 write!(out, "({})", arg.ident);
209 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800210 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400211 } else {
212 write!(out, "{}", arg.ident);
213 }
214 }
215 write!(out, ")");
216 match &efn.ret {
217 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
218 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
219 _ => {}
220 }
221 if indirect_return {
222 write!(out, ")");
223 }
224 writeln!(out, ";");
225 writeln!(out, "}}");
226}
227
228fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
229 write_extern_return_type(out, &efn.ret, types);
230 for name in out.namespace.clone() {
231 write!(out, "{}$", name);
232 }
David Tolnaye43b7372020-01-08 08:46:20 -0800233 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400234 for (i, arg) in efn.args.iter().enumerate() {
235 if i > 0 {
236 write!(out, ", ");
237 }
238 write_extern_arg(out, arg, types);
239 }
240 if efn
241 .ret
242 .as_ref()
243 .map_or(false, |ret| types.needs_indirect_abi(ret))
244 {
245 if !efn.args.is_empty() {
246 write!(out, ", ");
247 }
248 write_return_type(out, &efn.ret);
249 write!(out, "*return$");
250 }
251 writeln!(out, ") noexcept;");
252}
253
254fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
255 let indirect_return = efn
256 .ret
257 .as_ref()
258 .map_or(false, |ret| types.needs_indirect_abi(ret));
259 for line in efn.doc.to_string().lines() {
260 writeln!(out, "//{}", line);
261 }
262 write_return_type(out, &efn.ret);
263 write!(out, "{}(", efn.ident);
264 for (i, arg) in efn.args.iter().enumerate() {
265 if i > 0 {
266 write!(out, ", ");
267 }
268 write_type_space(out, &arg.ty);
269 write!(out, "{}", arg.ident);
270 }
271 write!(out, ") noexcept");
272 if out.header {
273 writeln!(out, ";");
274 } else {
275 writeln!(out, " {{");
276 write!(out, " ");
277 if indirect_return {
278 write!(out, "char return$[sizeof(");
279 write_type(out, efn.ret.as_ref().unwrap());
280 writeln!(out, ")];");
281 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800282 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400283 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800284 if let Type::Ref(_) = ret {
285 write!(out, "*");
286 }
David Tolnay7db73692019-10-20 14:51:12 -0400287 }
288 for name in out.namespace.clone() {
289 write!(out, "{}$", name);
290 }
David Tolnaye43b7372020-01-08 08:46:20 -0800291 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400292 for (i, arg) in efn.args.iter().enumerate() {
293 if i > 0 {
294 write!(out, ", ");
295 }
296 if types.needs_indirect_abi(&arg.ty) {
297 write!(out, "&");
298 }
299 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800300 match arg.ty {
301 Type::RustBox(_) => write!(out, ".into_raw()"),
302 Type::UniquePtr(_) => write!(out, ".release()"),
303 _ => {}
304 }
David Tolnay7db73692019-10-20 14:51:12 -0400305 }
306 if indirect_return {
307 if !efn.args.is_empty() {
308 write!(out, ", ");
309 }
310 write!(out, "reinterpret_cast<");
311 write_return_type(out, &efn.ret);
312 write!(out, "*>(return$)");
313 }
314 writeln!(out, ");");
315 if indirect_return {
316 write!(out, " return ");
317 write_type(out, efn.ret.as_ref().unwrap());
318 write!(out, "(*reinterpret_cast<");
319 write_return_type(out, &efn.ret);
320 writeln!(out, "*>(return$));");
321 }
322 writeln!(out, "}}");
323 }
324}
325
326fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
327 match ty {
328 None => write!(out, "void "),
329 Some(ty) => write_type_space(out, ty),
330 }
331}
332
333fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
334 match ty {
335 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
336 write_type_space(out, &ty.inner);
337 write!(out, "*");
338 }
David Tolnay4a441222020-01-25 16:24:27 -0800339 Some(Type::Ref(ty)) => {
340 if ty.mutability.is_none() {
341 write!(out, "const ");
342 }
343 write_type(out, &ty.inner);
344 write!(out, " *");
345 }
David Tolnay750755e2020-03-01 13:04:08 -0800346 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400347 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
348 _ => write_return_type(out, ty),
349 }
350}
351
352fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
353 match &arg.ty {
354 Type::RustBox(ty) | Type::UniquePtr(ty) => {
355 write_type_space(out, &ty.inner);
356 write!(out, "*");
357 }
David Tolnay750755e2020-03-01 13:04:08 -0800358 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400359 _ => write_type_space(out, &arg.ty),
360 }
361 if types.needs_indirect_abi(&arg.ty) {
362 write!(out, "*");
363 }
364 write!(out, "{}", arg.ident);
365}
366
367fn write_type(out: &mut OutFile, ty: &Type) {
368 match ty {
369 Type::Ident(ident) => match Atom::from(ident) {
370 Some(Bool) => write!(out, "bool"),
371 Some(U8) => write!(out, "uint8_t"),
372 Some(U16) => write!(out, "uint16_t"),
373 Some(U32) => write!(out, "uint32_t"),
374 Some(U64) => write!(out, "uint64_t"),
375 Some(Usize) => write!(out, "size_t"),
376 Some(I8) => write!(out, "int8_t"),
377 Some(I16) => write!(out, "int16_t"),
378 Some(I32) => write!(out, "int32_t"),
379 Some(I64) => write!(out, "int64_t"),
380 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800381 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800382 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400383 None => write!(out, "{}", ident),
384 },
385 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800386 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400387 write_type(out, &ty.inner);
388 write!(out, ">");
389 }
390 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800391 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400392 write_type(out, &ptr.inner);
393 write!(out, ">");
394 }
395 Type::Ref(r) => {
396 if r.mutability.is_none() {
397 write!(out, "const ");
398 }
399 write_type(out, &r.inner);
400 write!(out, " &");
401 }
402 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800403 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400404 }
405 }
406}
407
408fn write_type_space(out: &mut OutFile, ty: &Type) {
409 write_type(out, ty);
410 match ty {
411 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
412 Type::Ref(_) => {}
413 }
414}
415
416fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
417 fn allow_unique_ptr(ident: &Ident) -> bool {
418 Atom::from(ident).is_none()
419 }
420
421 out.begin_block("extern \"C\"");
422 for ty in types {
423 if let Type::RustBox(ty) = ty {
424 if let Type::Ident(inner) = &ty.inner {
425 out.next_section();
426 write_rust_box_extern(out, inner);
427 }
428 } else if let Type::UniquePtr(ptr) = ty {
429 if let Type::Ident(inner) = &ptr.inner {
430 if allow_unique_ptr(inner) {
431 out.next_section();
432 write_unique_ptr(out, inner);
433 }
434 }
435 }
436 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800437 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400438
David Tolnay750755e2020-03-01 13:04:08 -0800439 out.begin_block("namespace rust");
440 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400441 for ty in types {
442 if let Type::RustBox(ty) = ty {
443 if let Type::Ident(inner) = &ty.inner {
444 write_rust_box_impl(out, inner);
445 }
446 }
447 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800448 out.end_block("namespace cxxbridge01");
449 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400450}
451
452fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
453 let mut inner = String::new();
454 for name in &out.namespace {
455 inner += name;
456 inner += "::";
457 }
458 inner += &ident.to_string();
459 let instance = inner.replace("::", "$");
460
David Tolnaye43b7372020-01-08 08:46:20 -0800461 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
462 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400463 writeln!(
464 out,
David Tolnay750755e2020-03-01 13:04:08 -0800465 "void cxxbridge01$rust_box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400466 instance, inner,
467 );
468 writeln!(
469 out,
David Tolnay750755e2020-03-01 13:04:08 -0800470 "void cxxbridge01$rust_box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400471 instance, inner, inner
472 );
473 writeln!(
474 out,
David Tolnay750755e2020-03-01 13:04:08 -0800475 "void cxxbridge01$rust_box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400476 instance, inner,
477 );
478 writeln!(
479 out,
David Tolnay750755e2020-03-01 13:04:08 -0800480 "const {} *cxxbridge01$rust_box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400481 inner, instance, inner,
482 );
483 writeln!(
484 out,
David Tolnay750755e2020-03-01 13:04:08 -0800485 "{} *cxxbridge01$rust_box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400486 inner, instance, inner,
487 );
David Tolnaye43b7372020-01-08 08:46:20 -0800488 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400489}
490
491fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
492 let mut inner = String::new();
493 for name in &out.namespace {
494 inner += name;
495 inner += "::";
496 }
497 inner += &ident.to_string();
498 let instance = inner.replace("::", "$");
499
500 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800501 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400502 writeln!(
503 out,
David Tolnaye43b7372020-01-08 08:46:20 -0800504 " return cxxbridge01$rust_box${}$uninit(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400505 instance
506 );
507 writeln!(out, "}}");
508
509 writeln!(out, "template <>");
510 writeln!(
511 out,
David Tolnay324437a2020-03-01 13:02:24 -0800512 "void Box<{}>::set_raw({} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400513 inner, inner,
514 );
515 writeln!(
516 out,
David Tolnaye43b7372020-01-08 08:46:20 -0800517 " return cxxbridge01$rust_box${}$set_raw(this, raw);",
David Tolnay7db73692019-10-20 14:51:12 -0400518 instance
519 );
520 writeln!(out, "}}");
521
522 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800523 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400524 writeln!(
525 out,
David Tolnaye43b7372020-01-08 08:46:20 -0800526 " return cxxbridge01$rust_box${}$drop(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400527 instance
528 );
529 writeln!(out, "}}");
530
531 writeln!(out, "template <>");
532 writeln!(
533 out,
David Tolnay324437a2020-03-01 13:02:24 -0800534 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400535 inner, inner,
536 );
537 writeln!(
538 out,
David Tolnaye43b7372020-01-08 08:46:20 -0800539 " return cxxbridge01$rust_box${}$deref(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400540 instance
541 );
542 writeln!(out, "}}");
543
544 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800545 writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400546 writeln!(
547 out,
David Tolnaye43b7372020-01-08 08:46:20 -0800548 " return cxxbridge01$rust_box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400549 instance
550 );
551 writeln!(out, "}}");
552}
553
554fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
555 let mut inner = String::new();
556 for name in &out.namespace {
557 inner += name;
558 inner += "::";
559 }
560 inner += &ident.to_string();
561 let instance = inner.replace("::", "$");
562
David Tolnaye43b7372020-01-08 08:46:20 -0800563 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
564 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400565 writeln!(
566 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800567 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400568 inner,
569 );
570 writeln!(
571 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800572 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400573 inner,
574 );
575 writeln!(
576 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800577 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400578 instance, inner,
579 );
David Tolnay7e219b82020-03-01 13:14:51 -0800580 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400581 writeln!(out, "}}");
582 writeln!(
583 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800584 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400585 instance, inner, inner,
586 );
587 writeln!(
588 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800589 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400590 inner, inner,
591 );
592 writeln!(out, "}}");
593 writeln!(
594 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800595 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400596 instance, inner, inner,
597 );
David Tolnay7e219b82020-03-01 13:14:51 -0800598 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400599 writeln!(out, "}}");
600 writeln!(
601 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800602 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400603 inner, instance, inner,
604 );
605 writeln!(out, " return ptr.get();");
606 writeln!(out, "}}");
607 writeln!(
608 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800609 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400610 inner, instance, inner,
611 );
612 writeln!(out, " return ptr.release();");
613 writeln!(out, "}}");
614 writeln!(
615 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800616 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400617 instance, inner,
618 );
619 writeln!(out, " ptr->~unique_ptr();");
620 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800621 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400622}