blob: 68f0315b05e953820e270137dd3231aca57886af [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 }
David Tolnaya46a2372020-03-06 10:03:48 -0800172 if arg.ty == RustString {
173 write!(out, "const ");
174 }
David Tolnay7db73692019-10-20 14:51:12 -0400175 write_extern_arg(out, arg, types);
176 }
177 if indirect_return {
178 if !efn.args.is_empty() {
179 write!(out, ", ");
180 }
181 write_return_type(out, &efn.ret);
182 write!(out, "*return$");
183 }
184 writeln!(out, ") noexcept {{");
185 write!(out, " ");
186 write_return_type(out, &efn.ret);
187 write!(out, "(*{}$)(", efn.ident);
188 for (i, arg) in efn.args.iter().enumerate() {
189 if i > 0 {
190 write!(out, ", ");
191 }
192 write_type(out, &arg.ty);
193 }
194 writeln!(out, ") = {};", efn.ident);
195 write!(out, " ");
196 if indirect_return {
197 write!(out, "new (return$) ");
198 write_type(out, efn.ret.as_ref().unwrap());
199 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800200 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400201 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800202 match ret {
203 Type::Ref(_) => write!(out, "&"),
204 Type::Str(_) => write!(out, "::rust::Str::Repr("),
205 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800206 }
David Tolnay7db73692019-10-20 14:51:12 -0400207 }
208 write!(out, "{}$(", efn.ident);
209 for (i, arg) in efn.args.iter().enumerate() {
210 if i > 0 {
211 write!(out, ", ");
212 }
213 if let Type::RustBox(_) = &arg.ty {
214 write_type(out, &arg.ty);
215 write!(out, "::from_raw({})", arg.ident);
216 } else if let Type::UniquePtr(_) = &arg.ty {
217 write_type(out, &arg.ty);
218 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800219 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800220 write!(
221 out,
222 "::rust::String(::rust::unsafe_bitcopy, *{})",
223 arg.ident,
224 );
David Tolnay7db73692019-10-20 14:51:12 -0400225 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800226 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400227 } else {
228 write!(out, "{}", arg.ident);
229 }
230 }
231 write!(out, ")");
232 match &efn.ret {
233 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
234 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800235 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400236 _ => {}
237 }
238 if indirect_return {
239 write!(out, ")");
240 }
241 writeln!(out, ";");
242 writeln!(out, "}}");
243}
244
245fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
246 write_extern_return_type(out, &efn.ret, types);
247 for name in out.namespace.clone() {
248 write!(out, "{}$", name);
249 }
David Tolnaye43b7372020-01-08 08:46:20 -0800250 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400251 for (i, arg) in efn.args.iter().enumerate() {
252 if i > 0 {
253 write!(out, ", ");
254 }
255 write_extern_arg(out, arg, types);
256 }
257 if efn
258 .ret
259 .as_ref()
260 .map_or(false, |ret| types.needs_indirect_abi(ret))
261 {
262 if !efn.args.is_empty() {
263 write!(out, ", ");
264 }
265 write_return_type(out, &efn.ret);
266 write!(out, "*return$");
267 }
268 writeln!(out, ") noexcept;");
269}
270
271fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
272 let indirect_return = efn
273 .ret
274 .as_ref()
275 .map_or(false, |ret| types.needs_indirect_abi(ret));
276 for line in efn.doc.to_string().lines() {
277 writeln!(out, "//{}", line);
278 }
279 write_return_type(out, &efn.ret);
280 write!(out, "{}(", efn.ident);
281 for (i, arg) in efn.args.iter().enumerate() {
282 if i > 0 {
283 write!(out, ", ");
284 }
285 write_type_space(out, &arg.ty);
286 write!(out, "{}", arg.ident);
287 }
288 write!(out, ") noexcept");
289 if out.header {
290 writeln!(out, ";");
291 } else {
292 writeln!(out, " {{");
293 write!(out, " ");
294 if indirect_return {
295 write!(out, "char return$[sizeof(");
296 write_type(out, efn.ret.as_ref().unwrap());
297 writeln!(out, ")];");
298 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800299 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400300 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800301 if let Type::Ref(_) = ret {
302 write!(out, "*");
303 }
David Tolnay7db73692019-10-20 14:51:12 -0400304 }
305 for name in out.namespace.clone() {
306 write!(out, "{}$", name);
307 }
David Tolnaye43b7372020-01-08 08:46:20 -0800308 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400309 for (i, arg) in efn.args.iter().enumerate() {
310 if i > 0 {
311 write!(out, ", ");
312 }
David Tolnaybaae4432020-03-01 20:20:10 -0800313 match &arg.ty {
314 Type::Str(_) => write!(out, "::rust::Str::Repr("),
315 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
316 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400317 }
318 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800319 match arg.ty {
320 Type::RustBox(_) => write!(out, ".into_raw()"),
321 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800322 Type::Str(_) => write!(out, ")"),
David Tolnay17955e22020-01-20 17:58:24 -0800323 _ => {}
324 }
David Tolnay7db73692019-10-20 14:51:12 -0400325 }
326 if indirect_return {
327 if !efn.args.is_empty() {
328 write!(out, ", ");
329 }
330 write!(out, "reinterpret_cast<");
331 write_return_type(out, &efn.ret);
332 write!(out, "*>(return$)");
333 }
334 writeln!(out, ");");
335 if indirect_return {
336 write!(out, " return ");
337 write_type(out, efn.ret.as_ref().unwrap());
338 write!(out, "(*reinterpret_cast<");
339 write_return_type(out, &efn.ret);
340 writeln!(out, "*>(return$));");
341 }
342 writeln!(out, "}}");
343 }
344}
345
346fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
347 match ty {
348 None => write!(out, "void "),
349 Some(ty) => write_type_space(out, ty),
350 }
351}
352
353fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
354 match ty {
355 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
356 write_type_space(out, &ty.inner);
357 write!(out, "*");
358 }
David Tolnay4a441222020-01-25 16:24:27 -0800359 Some(Type::Ref(ty)) => {
360 if ty.mutability.is_none() {
361 write!(out, "const ");
362 }
363 write_type(out, &ty.inner);
364 write!(out, " *");
365 }
David Tolnay750755e2020-03-01 13:04:08 -0800366 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400367 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
368 _ => write_return_type(out, ty),
369 }
370}
371
372fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
373 match &arg.ty {
374 Type::RustBox(ty) | Type::UniquePtr(ty) => {
375 write_type_space(out, &ty.inner);
376 write!(out, "*");
377 }
David Tolnay750755e2020-03-01 13:04:08 -0800378 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400379 _ => write_type_space(out, &arg.ty),
380 }
381 if types.needs_indirect_abi(&arg.ty) {
382 write!(out, "*");
383 }
384 write!(out, "{}", arg.ident);
385}
386
387fn write_type(out: &mut OutFile, ty: &Type) {
388 match ty {
389 Type::Ident(ident) => match Atom::from(ident) {
390 Some(Bool) => write!(out, "bool"),
391 Some(U8) => write!(out, "uint8_t"),
392 Some(U16) => write!(out, "uint16_t"),
393 Some(U32) => write!(out, "uint32_t"),
394 Some(U64) => write!(out, "uint64_t"),
395 Some(Usize) => write!(out, "size_t"),
396 Some(I8) => write!(out, "int8_t"),
397 Some(I16) => write!(out, "int16_t"),
398 Some(I32) => write!(out, "int32_t"),
399 Some(I64) => write!(out, "int64_t"),
400 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800401 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800402 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400403 None => write!(out, "{}", ident),
404 },
405 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800406 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400407 write_type(out, &ty.inner);
408 write!(out, ">");
409 }
410 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800411 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400412 write_type(out, &ptr.inner);
413 write!(out, ">");
414 }
415 Type::Ref(r) => {
416 if r.mutability.is_none() {
417 write!(out, "const ");
418 }
419 write_type(out, &r.inner);
420 write!(out, " &");
421 }
422 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800423 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400424 }
425 }
426}
427
428fn write_type_space(out: &mut OutFile, ty: &Type) {
429 write_type(out, ty);
430 match ty {
431 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
432 Type::Ref(_) => {}
433 }
434}
435
436fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
437 fn allow_unique_ptr(ident: &Ident) -> bool {
438 Atom::from(ident).is_none()
439 }
440
441 out.begin_block("extern \"C\"");
442 for ty in types {
443 if let Type::RustBox(ty) = ty {
444 if let Type::Ident(inner) = &ty.inner {
445 out.next_section();
446 write_rust_box_extern(out, inner);
447 }
448 } else if let Type::UniquePtr(ptr) = ty {
449 if let Type::Ident(inner) = &ptr.inner {
450 if allow_unique_ptr(inner) {
451 out.next_section();
452 write_unique_ptr(out, inner);
453 }
454 }
455 }
456 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800457 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400458
David Tolnay750755e2020-03-01 13:04:08 -0800459 out.begin_block("namespace rust");
460 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400461 for ty in types {
462 if let Type::RustBox(ty) = ty {
463 if let Type::Ident(inner) = &ty.inner {
464 write_rust_box_impl(out, inner);
465 }
466 }
467 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800468 out.end_block("namespace cxxbridge01");
469 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400470}
471
472fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
473 let mut inner = String::new();
474 for name in &out.namespace {
475 inner += name;
476 inner += "::";
477 }
478 inner += &ident.to_string();
479 let instance = inner.replace("::", "$");
480
David Tolnaye43b7372020-01-08 08:46:20 -0800481 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
482 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400483 writeln!(
484 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800485 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400486 instance, inner,
487 );
488 writeln!(
489 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800490 "void cxxbridge01$box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400491 instance, inner, inner
492 );
493 writeln!(
494 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800495 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400496 instance, inner,
497 );
498 writeln!(
499 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800500 "const {} *cxxbridge01$box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400501 inner, instance, inner,
502 );
503 writeln!(
504 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800505 "{} *cxxbridge01$box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400506 inner, instance, inner,
507 );
David Tolnaye43b7372020-01-08 08:46:20 -0800508 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400509}
510
511fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
512 let mut inner = String::new();
513 for name in &out.namespace {
514 inner += name;
515 inner += "::";
516 }
517 inner += &ident.to_string();
518 let instance = inner.replace("::", "$");
519
520 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800521 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800522 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400523 writeln!(out, "}}");
524
525 writeln!(out, "template <>");
526 writeln!(
527 out,
David Tolnay324437a2020-03-01 13:02:24 -0800528 "void Box<{}>::set_raw({} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400529 inner, inner,
530 );
531 writeln!(
532 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800533 " return cxxbridge01$box${}$set_raw(this, raw);",
David Tolnay7db73692019-10-20 14:51:12 -0400534 instance
535 );
536 writeln!(out, "}}");
537
538 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800539 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800540 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400541 writeln!(out, "}}");
542
543 writeln!(out, "template <>");
544 writeln!(
545 out,
David Tolnay324437a2020-03-01 13:02:24 -0800546 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400547 inner, inner,
548 );
David Tolnay9081beb2020-03-01 19:51:46 -0800549 writeln!(out, " return cxxbridge01$box${}$deref(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400550 writeln!(out, "}}");
551
552 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800553 writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400554 writeln!(
555 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800556 " return cxxbridge01$box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400557 instance
558 );
559 writeln!(out, "}}");
560}
561
562fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
563 let mut inner = String::new();
564 for name in &out.namespace {
565 inner += name;
566 inner += "::";
567 }
568 inner += &ident.to_string();
569 let instance = inner.replace("::", "$");
570
David Tolnaye43b7372020-01-08 08:46:20 -0800571 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
572 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400573 writeln!(
574 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800575 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400576 inner,
577 );
578 writeln!(
579 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800580 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400581 inner,
582 );
583 writeln!(
584 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800585 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400586 instance, inner,
587 );
David Tolnay7e219b82020-03-01 13:14:51 -0800588 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400589 writeln!(out, "}}");
590 writeln!(
591 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800592 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400593 instance, inner, inner,
594 );
595 writeln!(
596 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800597 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400598 inner, inner,
599 );
600 writeln!(out, "}}");
601 writeln!(
602 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800603 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400604 instance, inner, inner,
605 );
David Tolnay7e219b82020-03-01 13:14:51 -0800606 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400607 writeln!(out, "}}");
608 writeln!(
609 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800610 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400611 inner, instance, inner,
612 );
613 writeln!(out, " return ptr.get();");
614 writeln!(out, "}}");
615 writeln!(
616 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800617 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400618 inner, instance, inner,
619 );
620 writeln!(out, " return ptr.release();");
621 writeln!(out, "}}");
622 writeln!(
623 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800624 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400625 instance, inner,
626 );
627 writeln!(out, " ptr->~unique_ptr();");
628 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800629 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400630}