blob: 24c9c09e68b8906bec14414e57f61349e935a413 [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);
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
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
98fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
99 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 Tolnay750755e2020-03-01 13:04:08 -0800107 out.begin_block("namespace rust");
108 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400109 if needs_rust_box {
110 writeln!(out, "// #include \"cxxbridge.h\"");
David Tolnaye43b7372020-01-08 08:46:20 -0800111 for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400112 if !line.trim_start().starts_with("//") {
113 writeln!(out, "{}", line);
114 }
115 }
116 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800117 out.end_block("namespace cxxbridge01");
118 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400119}
120
121fn write_struct(out: &mut OutFile, strct: &Struct) {
122 for line in strct.doc.to_string().lines() {
123 writeln!(out, "//{}", line);
124 }
125 writeln!(out, "struct {} final {{", strct.ident);
126 for field in &strct.fields {
127 write!(out, " ");
128 write_type_space(out, &field.ty);
129 writeln!(out, "{};", field.ident);
130 }
131 writeln!(out, "}};");
132}
133
134fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
135 writeln!(out, "struct {};", ident);
136}
137
David Tolnay8861bee2020-01-20 18:39:24 -0800138fn write_struct_using(out: &mut OutFile, ident: &Ident) {
139 writeln!(out, "using {} = {};", ident, ident);
140}
141
David Tolnay7db73692019-10-20 14:51:12 -0400142fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
143 let indirect_return = efn
144 .ret
145 .as_ref()
146 .map_or(false, |ret| types.needs_indirect_abi(ret));
147 write_extern_return_type(out, &efn.ret, types);
148 for name in out.namespace.clone() {
149 write!(out, "{}$", name);
150 }
David Tolnaye43b7372020-01-08 08:46:20 -0800151 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400152 for (i, arg) in efn.args.iter().enumerate() {
153 if i > 0 {
154 write!(out, ", ");
155 }
David Tolnaya46a2372020-03-06 10:03:48 -0800156 if arg.ty == RustString {
157 write!(out, "const ");
158 }
David Tolnay7db73692019-10-20 14:51:12 -0400159 write_extern_arg(out, arg, types);
160 }
161 if indirect_return {
162 if !efn.args.is_empty() {
163 write!(out, ", ");
164 }
165 write_return_type(out, &efn.ret);
166 write!(out, "*return$");
167 }
168 writeln!(out, ") noexcept {{");
169 write!(out, " ");
170 write_return_type(out, &efn.ret);
171 write!(out, "(*{}$)(", efn.ident);
172 for (i, arg) in efn.args.iter().enumerate() {
173 if i > 0 {
174 write!(out, ", ");
175 }
176 write_type(out, &arg.ty);
177 }
178 writeln!(out, ") = {};", efn.ident);
179 write!(out, " ");
180 if indirect_return {
181 write!(out, "new (return$) ");
182 write_type(out, efn.ret.as_ref().unwrap());
183 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800184 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400185 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800186 match ret {
187 Type::Ref(_) => write!(out, "&"),
188 Type::Str(_) => write!(out, "::rust::Str::Repr("),
189 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800190 }
David Tolnay7db73692019-10-20 14:51:12 -0400191 }
192 write!(out, "{}$(", efn.ident);
193 for (i, arg) in efn.args.iter().enumerate() {
194 if i > 0 {
195 write!(out, ", ");
196 }
197 if let Type::RustBox(_) = &arg.ty {
198 write_type(out, &arg.ty);
199 write!(out, "::from_raw({})", arg.ident);
200 } else if let Type::UniquePtr(_) = &arg.ty {
201 write_type(out, &arg.ty);
202 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800203 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800204 write!(
205 out,
206 "::rust::String(::rust::unsafe_bitcopy, *{})",
207 arg.ident,
208 );
David Tolnay7db73692019-10-20 14:51:12 -0400209 } 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()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800219 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400220 _ => {}
221 }
222 if indirect_return {
223 write!(out, ")");
224 }
225 writeln!(out, ";");
226 writeln!(out, "}}");
227}
228
229fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
230 write_extern_return_type(out, &efn.ret, types);
231 for name in out.namespace.clone() {
232 write!(out, "{}$", name);
233 }
David Tolnaye43b7372020-01-08 08:46:20 -0800234 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400235 for (i, arg) in efn.args.iter().enumerate() {
236 if i > 0 {
237 write!(out, ", ");
238 }
239 write_extern_arg(out, arg, types);
240 }
241 if efn
242 .ret
243 .as_ref()
244 .map_or(false, |ret| types.needs_indirect_abi(ret))
245 {
246 if !efn.args.is_empty() {
247 write!(out, ", ");
248 }
249 write_return_type(out, &efn.ret);
250 write!(out, "*return$");
251 }
252 writeln!(out, ") noexcept;");
253}
254
255fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
256 let indirect_return = efn
257 .ret
258 .as_ref()
259 .map_or(false, |ret| types.needs_indirect_abi(ret));
260 for line in efn.doc.to_string().lines() {
261 writeln!(out, "//{}", line);
262 }
263 write_return_type(out, &efn.ret);
264 write!(out, "{}(", efn.ident);
265 for (i, arg) in efn.args.iter().enumerate() {
266 if i > 0 {
267 write!(out, ", ");
268 }
269 write_type_space(out, &arg.ty);
270 write!(out, "{}", arg.ident);
271 }
272 write!(out, ") noexcept");
273 if out.header {
274 writeln!(out, ";");
275 } else {
276 writeln!(out, " {{");
277 write!(out, " ");
278 if indirect_return {
279 write!(out, "char return$[sizeof(");
280 write_type(out, efn.ret.as_ref().unwrap());
281 writeln!(out, ")];");
282 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800283 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400284 write!(out, "return ");
David Tolnay4a441222020-01-25 16:24:27 -0800285 if let Type::Ref(_) = ret {
286 write!(out, "*");
287 }
David Tolnay7db73692019-10-20 14:51:12 -0400288 }
289 for name in out.namespace.clone() {
290 write!(out, "{}$", name);
291 }
David Tolnaye43b7372020-01-08 08:46:20 -0800292 write!(out, "cxxbridge01${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400293 for (i, arg) in efn.args.iter().enumerate() {
294 if i > 0 {
295 write!(out, ", ");
296 }
David Tolnaybaae4432020-03-01 20:20:10 -0800297 match &arg.ty {
298 Type::Str(_) => write!(out, "::rust::Str::Repr("),
299 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
300 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400301 }
302 write!(out, "{}", arg.ident);
David Tolnay17955e22020-01-20 17:58:24 -0800303 match arg.ty {
304 Type::RustBox(_) => write!(out, ".into_raw()"),
305 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800306 Type::Str(_) => write!(out, ")"),
David Tolnay17955e22020-01-20 17:58:24 -0800307 _ => {}
308 }
David Tolnay7db73692019-10-20 14:51:12 -0400309 }
310 if indirect_return {
311 if !efn.args.is_empty() {
312 write!(out, ", ");
313 }
314 write!(out, "reinterpret_cast<");
315 write_return_type(out, &efn.ret);
316 write!(out, "*>(return$)");
317 }
318 writeln!(out, ");");
319 if indirect_return {
320 write!(out, " return ");
321 write_type(out, efn.ret.as_ref().unwrap());
322 write!(out, "(*reinterpret_cast<");
323 write_return_type(out, &efn.ret);
324 writeln!(out, "*>(return$));");
325 }
326 writeln!(out, "}}");
327 }
328}
329
330fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
331 match ty {
332 None => write!(out, "void "),
333 Some(ty) => write_type_space(out, ty),
334 }
335}
336
337fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
338 match ty {
339 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
340 write_type_space(out, &ty.inner);
341 write!(out, "*");
342 }
David Tolnay4a441222020-01-25 16:24:27 -0800343 Some(Type::Ref(ty)) => {
344 if ty.mutability.is_none() {
345 write!(out, "const ");
346 }
347 write_type(out, &ty.inner);
348 write!(out, " *");
349 }
David Tolnay750755e2020-03-01 13:04:08 -0800350 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400351 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
352 _ => write_return_type(out, ty),
353 }
354}
355
356fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
357 match &arg.ty {
358 Type::RustBox(ty) | Type::UniquePtr(ty) => {
359 write_type_space(out, &ty.inner);
360 write!(out, "*");
361 }
David Tolnay750755e2020-03-01 13:04:08 -0800362 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400363 _ => write_type_space(out, &arg.ty),
364 }
365 if types.needs_indirect_abi(&arg.ty) {
366 write!(out, "*");
367 }
368 write!(out, "{}", arg.ident);
369}
370
371fn write_type(out: &mut OutFile, ty: &Type) {
372 match ty {
373 Type::Ident(ident) => match Atom::from(ident) {
374 Some(Bool) => write!(out, "bool"),
375 Some(U8) => write!(out, "uint8_t"),
376 Some(U16) => write!(out, "uint16_t"),
377 Some(U32) => write!(out, "uint32_t"),
378 Some(U64) => write!(out, "uint64_t"),
379 Some(Usize) => write!(out, "size_t"),
380 Some(I8) => write!(out, "int8_t"),
381 Some(I16) => write!(out, "int16_t"),
382 Some(I32) => write!(out, "int32_t"),
383 Some(I64) => write!(out, "int64_t"),
384 Some(Isize) => write!(out, "ssize_t"),
David Tolnay7e219b82020-03-01 13:14:51 -0800385 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800386 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400387 None => write!(out, "{}", ident),
388 },
389 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800390 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400391 write_type(out, &ty.inner);
392 write!(out, ">");
393 }
394 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800395 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400396 write_type(out, &ptr.inner);
397 write!(out, ">");
398 }
399 Type::Ref(r) => {
400 if r.mutability.is_none() {
401 write!(out, "const ");
402 }
403 write_type(out, &r.inner);
404 write!(out, " &");
405 }
406 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800407 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400408 }
409 }
410}
411
412fn write_type_space(out: &mut OutFile, ty: &Type) {
413 write_type(out, ty);
414 match ty {
415 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
416 Type::Ref(_) => {}
417 }
418}
419
420fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
421 fn allow_unique_ptr(ident: &Ident) -> bool {
422 Atom::from(ident).is_none()
423 }
424
425 out.begin_block("extern \"C\"");
426 for ty in types {
427 if let Type::RustBox(ty) = ty {
428 if let Type::Ident(inner) = &ty.inner {
429 out.next_section();
430 write_rust_box_extern(out, inner);
431 }
432 } else if let Type::UniquePtr(ptr) = ty {
433 if let Type::Ident(inner) = &ptr.inner {
434 if allow_unique_ptr(inner) {
435 out.next_section();
436 write_unique_ptr(out, inner);
437 }
438 }
439 }
440 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800441 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400442
David Tolnay750755e2020-03-01 13:04:08 -0800443 out.begin_block("namespace rust");
444 out.begin_block("inline namespace cxxbridge01");
David Tolnay7db73692019-10-20 14:51:12 -0400445 for ty in types {
446 if let Type::RustBox(ty) = ty {
447 if let Type::Ident(inner) = &ty.inner {
448 write_rust_box_impl(out, inner);
449 }
450 }
451 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800452 out.end_block("namespace cxxbridge01");
453 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400454}
455
456fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
457 let mut inner = String::new();
458 for name in &out.namespace {
459 inner += name;
460 inner += "::";
461 }
462 inner += &ident.to_string();
463 let instance = inner.replace("::", "$");
464
David Tolnaye43b7372020-01-08 08:46:20 -0800465 writeln!(out, "#ifndef CXXBRIDGE01_RUST_BOX_{}", instance);
466 writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400467 writeln!(
468 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800469 "void cxxbridge01$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400470 instance, inner,
471 );
472 writeln!(
473 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800474 "void cxxbridge01$box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400475 instance, inner, inner
476 );
477 writeln!(
478 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800479 "void cxxbridge01$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400480 instance, inner,
481 );
482 writeln!(
483 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800484 "const {} *cxxbridge01$box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400485 inner, instance, inner,
486 );
487 writeln!(
488 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800489 "{} *cxxbridge01$box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400490 inner, instance, inner,
491 );
David Tolnaye43b7372020-01-08 08:46:20 -0800492 writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400493}
494
495fn write_rust_box_impl(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
504 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800505 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay9081beb2020-03-01 19:51:46 -0800506 writeln!(out, " return cxxbridge01$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400507 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 Tolnay9081beb2020-03-01 19:51:46 -0800517 " return cxxbridge01$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 Tolnay9081beb2020-03-01 19:51:46 -0800524 writeln!(out, " return cxxbridge01$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400525 writeln!(out, "}}");
526
527 writeln!(out, "template <>");
528 writeln!(
529 out,
David Tolnay324437a2020-03-01 13:02:24 -0800530 "const {} *Box<{}>::deref() const noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400531 inner, inner,
532 );
David Tolnay9081beb2020-03-01 19:51:46 -0800533 writeln!(out, " return cxxbridge01$box${}$deref(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400534 writeln!(out, "}}");
535
536 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800537 writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400538 writeln!(
539 out,
David Tolnay9081beb2020-03-01 19:51:46 -0800540 " return cxxbridge01$box${}$deref_mut(this);",
David Tolnay7db73692019-10-20 14:51:12 -0400541 instance
542 );
543 writeln!(out, "}}");
544}
545
546fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
547 let mut inner = String::new();
548 for name in &out.namespace {
549 inner += name;
550 inner += "::";
551 }
552 inner += &ident.to_string();
553 let instance = inner.replace("::", "$");
554
David Tolnaye43b7372020-01-08 08:46:20 -0800555 writeln!(out, "#ifndef CXXBRIDGE01_UNIQUE_PTR_{}", instance);
556 writeln!(out, "#define CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400557 writeln!(
558 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800559 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400560 inner,
561 );
562 writeln!(
563 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800564 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400565 inner,
566 );
567 writeln!(
568 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800569 "void cxxbridge01$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400570 instance, inner,
571 );
David Tolnay7e219b82020-03-01 13:14:51 -0800572 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400573 writeln!(out, "}}");
574 writeln!(
575 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800576 "void cxxbridge01$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400577 instance, inner, inner,
578 );
579 writeln!(
580 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800581 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400582 inner, inner,
583 );
584 writeln!(out, "}}");
585 writeln!(
586 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800587 "void cxxbridge01$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400588 instance, inner, inner,
589 );
David Tolnay7e219b82020-03-01 13:14:51 -0800590 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400591 writeln!(out, "}}");
592 writeln!(
593 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800594 "const {} *cxxbridge01$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400595 inner, instance, inner,
596 );
597 writeln!(out, " return ptr.get();");
598 writeln!(out, "}}");
599 writeln!(
600 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800601 "{} *cxxbridge01$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400602 inner, instance, inner,
603 );
604 writeln!(out, " return ptr.release();");
605 writeln!(out, "}}");
606 writeln!(
607 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800608 "void cxxbridge01$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400609 instance, inner,
610 );
611 writeln!(out, " ptr->~unique_ptr();");
612 writeln!(out, "}}");
David Tolnaye43b7372020-01-08 08:46:20 -0800613 writeln!(out, "#endif // CXXBRIDGE01_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400614}