blob: e0396c9de50d3533080eeb6aca10c6e626bebf7e [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 Tolnaybaae4432020-03-01 20:20:10 -0800194 match ret {
195 Type::Ref(_) => write!(out, "&"),
196 Type::Str(_) => write!(out, "::rust::Str::Repr("),
197 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800198 }
David Tolnay7db73692019-10-20 14:51:12 -0400199 }
200 write!(out, "{}$(", efn.ident);
201 for (i, arg) in efn.args.iter().enumerate() {
202 if i > 0 {
203 write!(out, ", ");
204 }
205 if let Type::RustBox(_) = &arg.ty {
206 write_type(out, &arg.ty);
207 write!(out, "::from_raw({})", arg.ident);
208 } else if let Type::UniquePtr(_) = &arg.ty {
209 write_type(out, &arg.ty);
210 write!(out, "({})", arg.ident);
211 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800212 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400213 } else {
214 write!(out, "{}", arg.ident);
215 }
216 }
217 write!(out, ")");
218 match &efn.ret {
219 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
220 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800221 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400222 _ => {}
223 }
224 if indirect_return {
225 write!(out, ")");
226 }
227 writeln!(out, ";");
228 writeln!(out, "}}");
229}
230
231fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
232 write_extern_return_type(out, &efn.ret, types);
233 for name in out.namespace.clone() {
234 write!(out, "{}$", name);
235 }
David Tolnaye43b7372020-01-08 08:46:20 -0800236 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400237 for (i, arg) in efn.args.iter().enumerate() {
238 if i > 0 {
239 write!(out, ", ");
240 }
241 write_extern_arg(out, arg, types);
242 }
243 if efn
244 .ret
245 .as_ref()
246 .map_or(false, |ret| types.needs_indirect_abi(ret))
247 {
248 if !efn.args.is_empty() {
249 write!(out, ", ");
250 }
251 write_return_type(out, &efn.ret);
252 write!(out, "*return$");
253 }
254 writeln!(out, ") noexcept;");
255}
256
257fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
258 let indirect_return = efn
259 .ret
260 .as_ref()
261 .map_or(false, |ret| types.needs_indirect_abi(ret));
262 for line in efn.doc.to_string().lines() {
263 writeln!(out, "//{}", line);
264 }
265 write_return_type(out, &efn.ret);
266 write!(out, "{}(", efn.ident);
267 for (i, arg) in efn.args.iter().enumerate() {
268 if i > 0 {
269 write!(out, ", ");
270 }
271 write_type_space(out, &arg.ty);
272 write!(out, "{}", arg.ident);
273 }
274 write!(out, ") noexcept");
275 if out.header {
276 writeln!(out, ";");
277 } else {
278 writeln!(out, " {{");
279 write!(out, " ");
280 if indirect_return {
281 write!(out, "char return$[sizeof(");
282 write_type(out, efn.ret.as_ref().unwrap());
283 writeln!(out, ")];");
284 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800285 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400286 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800287 if let Type::Ref(_) = ret {
288 write!(out, "*");
289 }
David Tolnay7db73692019-10-20 14:51:12 -0400290 }
291 for name in out.namespace.clone() {
292 write!(out, "{}$", name);
293 }
David Tolnaye43b7372020-01-08 08:46:20 -0800294 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400295 for (i, arg) in efn.args.iter().enumerate() {
296 if i > 0 {
297 write!(out, ", ");
298 }
David Tolnaybaae4432020-03-01 20:20:10 -0800299 match &arg.ty {
300 Type::Str(_) => write!(out, "::rust::Str::Repr("),
301 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
302 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400303 }
304 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800305 match arg.ty {
306 Type::RustBox(_) => write!(out, ".into_raw()"),
307 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800308 Type::Str(_) => write!(out, ")"),
David Tolnay17955e22020-01-20 17:58:24 -0800309 _ => {}
310 }
David Tolnay7db73692019-10-20 14:51:12 -0400311 }
312 if indirect_return {
313 if !efn.args.is_empty() {
314 write!(out, ", ");
315 }
316 write!(out, "reinterpret_cast<");
317 write_return_type(out, &efn.ret);
318 write!(out, "*>(return$)");
319 }
320 writeln!(out, ");");
321 if indirect_return {
322 write!(out, " return ");
323 write_type(out, efn.ret.as_ref().unwrap());
324 write!(out, "(*reinterpret_cast<");
325 write_return_type(out, &efn.ret);
326 writeln!(out, "*>(return$));");
327 }
328 writeln!(out, "}}");
329 }
330}
331
332fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
333 match ty {
334 None => write!(out, "void "),
335 Some(ty) => write_type_space(out, ty),
336 }
337}
338
339fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
340 match ty {
341 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
342 write_type_space(out, &ty.inner);
343 write!(out, "*");
344 }
David Tolnay4a441222020-01-25 16:24:27 -0800345 Some(Type::Ref(ty)) => {
346 if ty.mutability.is_none() {
347 write!(out, "const ");
348 }
349 write_type(out, &ty.inner);
350 write!(out, " *");
351 }
David Tolnay750755e2020-03-01 13:04:08 -0800352 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400353 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
354 _ => write_return_type(out, ty),
355 }
356}
357
358fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
359 match &arg.ty {
360 Type::RustBox(ty) | Type::UniquePtr(ty) => {
361 write_type_space(out, &ty.inner);
362 write!(out, "*");
363 }
David Tolnay750755e2020-03-01 13:04:08 -0800364 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400365 _ => write_type_space(out, &arg.ty),
366 }
367 if types.needs_indirect_abi(&arg.ty) {
368 write!(out, "*");
369 }
370 write!(out, "{}", arg.ident);
371}
372
373fn write_type(out: &mut OutFile, ty: &Type) {
374 match ty {
375 Type::Ident(ident) => match Atom::from(ident) {
376 Some(Bool) => write!(out, "bool"),
377 Some(U8) => write!(out, "uint8_t"),
378 Some(U16) => write!(out, "uint16_t"),
379 Some(U32) => write!(out, "uint32_t"),
380 Some(U64) => write!(out, "uint64_t"),
381 Some(Usize) => write!(out, "size_t"),
382 Some(I8) => write!(out, "int8_t"),
383 Some(I16) => write!(out, "int16_t"),
384 Some(I32) => write!(out, "int32_t"),
385 Some(I64) => write!(out, "int64_t"),
386 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800387 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800388 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400389 None => write!(out, "{}", ident),
390 },
391 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800392 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400393 write_type(out, &ty.inner);
394 write!(out, ">");
395 }
396 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800397 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400398 write_type(out, &ptr.inner);
399 write!(out, ">");
400 }
401 Type::Ref(r) => {
402 if r.mutability.is_none() {
403 write!(out, "const ");
404 }
405 write_type(out, &r.inner);
406 write!(out, " &");
407 }
408 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800409 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400410 }
411 }
412}
413
414fn write_type_space(out: &mut OutFile, ty: &Type) {
415 write_type(out, ty);
416 match ty {
417 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
418 Type::Ref(_) => {}
419 }
420}
421
422fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
423 fn allow_unique_ptr(ident: &Ident) -> bool {
424 Atom::from(ident).is_none()
425 }
426
427 out.begin_block("extern \"C\"");
428 for ty in types {
429 if let Type::RustBox(ty) = ty {
430 if let Type::Ident(inner) = &ty.inner {
431 out.next_section();
432 write_rust_box_extern(out, inner);
433 }
434 } else if let Type::UniquePtr(ptr) = ty {
435 if let Type::Ident(inner) = &ptr.inner {
436 if allow_unique_ptr(inner) {
437 out.next_section();
438 write_unique_ptr(out, inner);
439 }
440 }
441 }
442 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800443 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400444
David Tolnay750755e2020-03-01 13:04:08 -0800445 out.begin_block("namespace rust");
446 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400447 for ty in types {
448 if let Type::RustBox(ty) = ty {
449 if let Type::Ident(inner) = &ty.inner {
450 write_rust_box_impl(out, inner);
451 }
452 }
453 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800454 out.end_block("namespace cxxbridge01");
455 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400456}
457
458fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
459 let mut inner = String::new();
460 for name in &out.namespace {
461 inner += name;
462 inner += "::";
463 }
464 inner += &ident.to_string();
465 let instance = inner.replace("::", "$");
466
David Tolnaye43b7372020-01-08 08:46:20 -0800467 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
468 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400469 writeln!(
470 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800471 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400472 instance, inner,
473 );
474 writeln!(
475 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800476 "void cxxbridge01$box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400477 instance, inner, inner
478 );
479 writeln!(
480 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800481 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400482 instance, inner,
483 );
484 writeln!(
485 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800486 "const {} *cxxbridge01$box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400487 inner, instance, inner,
488 );
489 writeln!(
490 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800491 "{} *cxxbridge01$box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400492 inner, instance, inner,
493 );
David Tolnaye43b7372020-01-08 08:46:20 -0800494 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400495}
496
497fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
498 let mut inner = String::new();
499 for name in &out.namespace {
500 inner += name;
501 inner += "::";
502 }
503 inner += &ident.to_string();
504 let instance = inner.replace("::", "$");
505
506 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800507 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800508 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400509 writeln!(out, "}}");
510
511 writeln!(out, "template <>");
512 writeln!(
513 out,
David Tolnay324437a2020-03-01 13:02:24 -0800514 "void Box<{}>::set_raw({} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400515 inner, inner,
516 );
517 writeln!(
518 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800519 " return cxxbridge01$box${}$set_raw(this, raw);",
David Tolnay7db73692019-10-20 14:51:12 -0400520 instance
521 );
522 writeln!(out, "}}");
523
524 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800525 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800526 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400527 writeln!(out, "}}");
528
529 writeln!(out, "template <>");
530 writeln!(
531 out,
David Tolnay324437a2020-03-01 13:02:24 -0800532 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400533 inner, inner,
534 );
David Tolnay9081beb2020-03-01 19:51:46 -0800535 writeln!(out, " return cxxbridge01$box${}$deref(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400536 writeln!(out, "}}");
537
538 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800539 writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400540 writeln!(
541 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800542 " return cxxbridge01$box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400543 instance
544 );
545 writeln!(out, "}}");
546}
547
548fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
549 let mut inner = String::new();
550 for name in &out.namespace {
551 inner += name;
552 inner += "::";
553 }
554 inner += &ident.to_string();
555 let instance = inner.replace("::", "$");
556
David Tolnaye43b7372020-01-08 08:46:20 -0800557 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
558 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400559 writeln!(
560 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800561 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400562 inner,
563 );
564 writeln!(
565 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800566 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400567 inner,
568 );
569 writeln!(
570 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800571 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400572 instance, inner,
573 );
David Tolnay7e219b82020-03-01 13:14:51 -0800574 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400575 writeln!(out, "}}");
576 writeln!(
577 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800578 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400579 instance, inner, inner,
580 );
581 writeln!(
582 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800583 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400584 inner, inner,
585 );
586 writeln!(out, "}}");
587 writeln!(
588 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800589 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400590 instance, inner, inner,
591 );
David Tolnay7e219b82020-03-01 13:14:51 -0800592 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400593 writeln!(out, "}}");
594 writeln!(
595 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800596 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400597 inner, instance, inner,
598 );
599 writeln!(out, " return ptr.get();");
600 writeln!(out, "}}");
601 writeln!(
602 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800603 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400604 inner, instance, inner,
605 );
606 writeln!(out, " return ptr.release();");
607 writeln!(out, "}}");
608 writeln!(
609 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800610 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400611 instance, inner,
612 );
613 writeln!(out, " ptr->~unique_ptr();");
614 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800615 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400616}