blob: d57e21e2b374ebbe7136c1a0a7040bb67925d8f5 [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);
David Tolnayf51447e2020-03-06 14:14:27 -080022 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040023
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 Tolnay3383ae72020-03-13 01:12:26 -070089 Some(Bool) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -040090 },
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
David Tolnayf51447e2020-03-06 14:14:27 -080098fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040099 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 Tolnayf51447e2020-03-06 14:14:27 -0800107 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800108 let mut needs_maybe_uninit = false;
109 for api in apis {
David Tolnayf51447e2020-03-06 14:14:27 -0800110 if let Api::RustFunction(efn) = api {
111 for arg in &efn.args {
112 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
113 needs_manually_drop = true;
David Tolnay09011c32020-03-06 14:40:28 -0800114 break;
115 }
116 }
117 if let Some(ret) = &efn.ret {
118 if types.needs_indirect_abi(ret) {
119 needs_maybe_uninit = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800120 }
121 }
122 }
123 }
124
David Tolnay750755e2020-03-01 13:04:08 -0800125 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700126 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800127
David Tolnay09011c32020-03-06 14:40:28 -0800128 if needs_rust_box || needs_manually_drop || needs_maybe_uninit {
David Tolnay736cbca2020-03-11 16:49:18 -0700129 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800130 }
131
132 if needs_rust_box {
133 out.next_section();
David Tolnay8c730492020-03-13 01:29:06 -0700134 for line in include::get("CXXBRIDGE02_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400135 if !line.trim_start().starts_with("//") {
136 writeln!(out, "{}", line);
137 }
138 }
139 }
David Tolnayf51447e2020-03-06 14:14:27 -0800140
141 if needs_manually_drop {
142 out.next_section();
143 writeln!(out, "template <typename T>");
144 writeln!(out, "union ManuallyDrop {{");
145 writeln!(out, " T value;");
146 writeln!(
147 out,
148 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
149 );
150 writeln!(out, " ~ManuallyDrop() {{}}");
151 writeln!(out, "}};");
152 }
153
David Tolnay09011c32020-03-06 14:40:28 -0800154 if needs_maybe_uninit {
155 out.next_section();
156 writeln!(out, "template <typename T>");
157 writeln!(out, "union MaybeUninit {{");
158 writeln!(out, " T value;");
159 writeln!(out, " MaybeUninit() {{}}");
160 writeln!(out, " ~MaybeUninit() {{}}");
161 writeln!(out, "}};");
162 }
163
David Tolnay8c730492020-03-13 01:29:06 -0700164 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800165 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400166}
167
168fn write_struct(out: &mut OutFile, strct: &Struct) {
169 for line in strct.doc.to_string().lines() {
170 writeln!(out, "//{}", line);
171 }
172 writeln!(out, "struct {} final {{", strct.ident);
173 for field in &strct.fields {
174 write!(out, " ");
175 write_type_space(out, &field.ty);
176 writeln!(out, "{};", field.ident);
177 }
178 writeln!(out, "}};");
179}
180
181fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
182 writeln!(out, "struct {};", ident);
183}
184
David Tolnay8861bee2020-01-20 18:39:24 -0800185fn write_struct_using(out: &mut OutFile, ident: &Ident) {
186 writeln!(out, "using {} = {};", ident, ident);
187}
188
David Tolnay7db73692019-10-20 14:51:12 -0400189fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400190 write_extern_return_type(out, &efn.ret, types);
191 for name in out.namespace.clone() {
192 write!(out, "{}$", name);
193 }
David Tolnay8c730492020-03-13 01:29:06 -0700194 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400195 for (i, arg) in efn.args.iter().enumerate() {
196 if i > 0 {
197 write!(out, ", ");
198 }
David Tolnaya46a2372020-03-06 10:03:48 -0800199 if arg.ty == RustString {
200 write!(out, "const ");
201 }
David Tolnay7db73692019-10-20 14:51:12 -0400202 write_extern_arg(out, arg, types);
203 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700204 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400205 if indirect_return {
206 if !efn.args.is_empty() {
207 write!(out, ", ");
208 }
209 write_return_type(out, &efn.ret);
210 write!(out, "*return$");
211 }
212 writeln!(out, ") noexcept {{");
213 write!(out, " ");
214 write_return_type(out, &efn.ret);
215 write!(out, "(*{}$)(", efn.ident);
216 for (i, arg) in efn.args.iter().enumerate() {
217 if i > 0 {
218 write!(out, ", ");
219 }
220 write_type(out, &arg.ty);
221 }
222 writeln!(out, ") = {};", efn.ident);
223 write!(out, " ");
224 if indirect_return {
225 write!(out, "new (return$) ");
226 write_type(out, efn.ret.as_ref().unwrap());
227 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800228 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400229 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800230 match ret {
231 Type::Ref(_) => write!(out, "&"),
232 Type::Str(_) => write!(out, "::rust::Str::Repr("),
233 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800234 }
David Tolnay7db73692019-10-20 14:51:12 -0400235 }
236 write!(out, "{}$(", efn.ident);
237 for (i, arg) in efn.args.iter().enumerate() {
238 if i > 0 {
239 write!(out, ", ");
240 }
241 if let Type::RustBox(_) = &arg.ty {
242 write_type(out, &arg.ty);
243 write!(out, "::from_raw({})", arg.ident);
244 } else if let Type::UniquePtr(_) = &arg.ty {
245 write_type(out, &arg.ty);
246 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800247 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800248 write!(
249 out,
250 "::rust::String(::rust::unsafe_bitcopy, *{})",
251 arg.ident,
252 );
David Tolnay7db73692019-10-20 14:51:12 -0400253 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800254 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400255 } else {
256 write!(out, "{}", arg.ident);
257 }
258 }
259 write!(out, ")");
260 match &efn.ret {
261 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
262 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800263 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400264 _ => {}
265 }
266 if indirect_return {
267 write!(out, ")");
268 }
269 writeln!(out, ";");
270 writeln!(out, "}}");
271}
272
273fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
274 write_extern_return_type(out, &efn.ret, types);
275 for name in out.namespace.clone() {
276 write!(out, "{}$", name);
277 }
David Tolnay8c730492020-03-13 01:29:06 -0700278 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400279 for (i, arg) in efn.args.iter().enumerate() {
280 if i > 0 {
281 write!(out, ", ");
282 }
283 write_extern_arg(out, arg, types);
284 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700285 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400286 if !efn.args.is_empty() {
287 write!(out, ", ");
288 }
289 write_return_type(out, &efn.ret);
290 write!(out, "*return$");
291 }
292 writeln!(out, ") noexcept;");
293}
294
295fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400296 for line in efn.doc.to_string().lines() {
297 writeln!(out, "//{}", line);
298 }
299 write_return_type(out, &efn.ret);
300 write!(out, "{}(", efn.ident);
301 for (i, arg) in efn.args.iter().enumerate() {
302 if i > 0 {
303 write!(out, ", ");
304 }
305 write_type_space(out, &arg.ty);
306 write!(out, "{}", arg.ident);
307 }
308 write!(out, ") noexcept");
309 if out.header {
310 writeln!(out, ";");
311 } else {
312 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800313 for arg in &efn.args {
314 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
315 write!(out, " ::rust::ManuallyDrop<");
316 write_type(out, &arg.ty);
317 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
318 }
319 }
David Tolnay7db73692019-10-20 14:51:12 -0400320 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700321 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400322 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800323 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400324 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800325 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400326 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800327 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400328 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800329 match ret {
330 Type::RustBox(_) => {
331 write_type(out, ret);
332 write!(out, "::from_raw(");
333 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800334 Type::UniquePtr(_) => {
335 write_type(out, ret);
336 write!(out, "(");
337 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800338 Type::Ref(_) => write!(out, "*"),
339 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800340 }
David Tolnay7db73692019-10-20 14:51:12 -0400341 }
342 for name in out.namespace.clone() {
343 write!(out, "{}$", name);
344 }
David Tolnay8c730492020-03-13 01:29:06 -0700345 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400346 for (i, arg) in efn.args.iter().enumerate() {
347 if i > 0 {
348 write!(out, ", ");
349 }
David Tolnaybaae4432020-03-01 20:20:10 -0800350 match &arg.ty {
351 Type::Str(_) => write!(out, "::rust::Str::Repr("),
352 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
353 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400354 }
355 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800356 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800357 Type::RustBox(_) => write!(out, ".into_raw()"),
358 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800359 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800360 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800361 _ => {}
362 }
David Tolnay7db73692019-10-20 14:51:12 -0400363 }
364 if indirect_return {
365 if !efn.args.is_empty() {
366 write!(out, ", ");
367 }
David Tolnay09011c32020-03-06 14:40:28 -0800368 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400369 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800370 write!(out, ")");
371 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800372 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800373 write!(out, ")");
374 }
375 }
376 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400377 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800378 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400379 }
380 writeln!(out, "}}");
381 }
382}
383
384fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
385 match ty {
386 None => write!(out, "void "),
387 Some(ty) => write_type_space(out, ty),
388 }
389}
390
David Tolnay277e3cc2020-03-17 00:11:01 -0700391fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
392 efn.ret
393 .as_ref()
394 .map_or(false, |ret| types.needs_indirect_abi(ret))
395}
396
David Tolnay7db73692019-10-20 14:51:12 -0400397fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
398 match ty {
399 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
400 write_type_space(out, &ty.inner);
401 write!(out, "*");
402 }
David Tolnay4a441222020-01-25 16:24:27 -0800403 Some(Type::Ref(ty)) => {
404 if ty.mutability.is_none() {
405 write!(out, "const ");
406 }
407 write_type(out, &ty.inner);
408 write!(out, " *");
409 }
David Tolnay750755e2020-03-01 13:04:08 -0800410 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400411 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
412 _ => write_return_type(out, ty),
413 }
414}
415
416fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
417 match &arg.ty {
418 Type::RustBox(ty) | Type::UniquePtr(ty) => {
419 write_type_space(out, &ty.inner);
420 write!(out, "*");
421 }
David Tolnay750755e2020-03-01 13:04:08 -0800422 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400423 _ => write_type_space(out, &arg.ty),
424 }
425 if types.needs_indirect_abi(&arg.ty) {
426 write!(out, "*");
427 }
428 write!(out, "{}", arg.ident);
429}
430
431fn write_type(out: &mut OutFile, ty: &Type) {
432 match ty {
433 Type::Ident(ident) => match Atom::from(ident) {
434 Some(Bool) => write!(out, "bool"),
435 Some(U8) => write!(out, "uint8_t"),
436 Some(U16) => write!(out, "uint16_t"),
437 Some(U32) => write!(out, "uint32_t"),
438 Some(U64) => write!(out, "uint64_t"),
439 Some(Usize) => write!(out, "size_t"),
440 Some(I8) => write!(out, "int8_t"),
441 Some(I16) => write!(out, "int16_t"),
442 Some(I32) => write!(out, "int32_t"),
443 Some(I64) => write!(out, "int64_t"),
444 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700445 Some(F32) => write!(out, "float"),
446 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800447 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800448 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400449 None => write!(out, "{}", ident),
450 },
451 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800452 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400453 write_type(out, &ty.inner);
454 write!(out, ">");
455 }
456 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800457 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400458 write_type(out, &ptr.inner);
459 write!(out, ">");
460 }
461 Type::Ref(r) => {
462 if r.mutability.is_none() {
463 write!(out, "const ");
464 }
465 write_type(out, &r.inner);
466 write!(out, " &");
467 }
468 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800469 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400470 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700471 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400472 }
473}
474
475fn write_type_space(out: &mut OutFile, ty: &Type) {
476 write_type(out, ty);
477 match ty {
478 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
479 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700480 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400481 }
482}
483
484fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
485 fn allow_unique_ptr(ident: &Ident) -> bool {
486 Atom::from(ident).is_none()
487 }
488
489 out.begin_block("extern \"C\"");
490 for ty in types {
491 if let Type::RustBox(ty) = ty {
492 if let Type::Ident(inner) = &ty.inner {
493 out.next_section();
494 write_rust_box_extern(out, inner);
495 }
496 } else if let Type::UniquePtr(ptr) = ty {
497 if let Type::Ident(inner) = &ptr.inner {
498 if allow_unique_ptr(inner) {
499 out.next_section();
500 write_unique_ptr(out, inner);
501 }
502 }
503 }
504 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800505 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400506
David Tolnay750755e2020-03-01 13:04:08 -0800507 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700508 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400509 for ty in types {
510 if let Type::RustBox(ty) = ty {
511 if let Type::Ident(inner) = &ty.inner {
512 write_rust_box_impl(out, inner);
513 }
514 }
515 }
David Tolnay8c730492020-03-13 01:29:06 -0700516 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800517 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400518}
519
520fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
521 let mut inner = String::new();
522 for name in &out.namespace {
523 inner += name;
524 inner += "::";
525 }
526 inner += &ident.to_string();
527 let instance = inner.replace("::", "$");
528
David Tolnay8c730492020-03-13 01:29:06 -0700529 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
530 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400531 writeln!(
532 out,
David Tolnay8c730492020-03-13 01:29:06 -0700533 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400534 instance, inner,
535 );
536 writeln!(
537 out,
David Tolnay8c730492020-03-13 01:29:06 -0700538 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400539 instance, inner,
540 );
David Tolnay8c730492020-03-13 01:29:06 -0700541 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400542}
543
544fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
545 let mut inner = String::new();
546 for name in &out.namespace {
547 inner += name;
548 inner += "::";
549 }
550 inner += &ident.to_string();
551 let instance = inner.replace("::", "$");
552
553 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800554 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700555 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400556 writeln!(out, "}}");
557
558 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800559 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700560 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400561 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400562}
563
564fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
565 let mut inner = String::new();
566 for name in &out.namespace {
567 inner += name;
568 inner += "::";
569 }
570 inner += &ident.to_string();
571 let instance = inner.replace("::", "$");
572
David Tolnay8c730492020-03-13 01:29:06 -0700573 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
574 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400575 writeln!(
576 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800577 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400578 inner,
579 );
580 writeln!(
581 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800582 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400583 inner,
584 );
585 writeln!(
586 out,
David Tolnay8c730492020-03-13 01:29:06 -0700587 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400588 instance, inner,
589 );
David Tolnay7e219b82020-03-01 13:14:51 -0800590 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400591 writeln!(out, "}}");
592 writeln!(
593 out,
David Tolnay8c730492020-03-13 01:29:06 -0700594 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400595 instance, inner, inner,
596 );
597 writeln!(
598 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800599 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400600 inner, inner,
601 );
602 writeln!(out, "}}");
603 writeln!(
604 out,
David Tolnay8c730492020-03-13 01:29:06 -0700605 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400606 instance, inner, inner,
607 );
David Tolnay7e219b82020-03-01 13:14:51 -0800608 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400609 writeln!(out, "}}");
610 writeln!(
611 out,
David Tolnay8c730492020-03-13 01:29:06 -0700612 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400613 inner, instance, inner,
614 );
615 writeln!(out, " return ptr.get();");
616 writeln!(out, "}}");
617 writeln!(
618 out,
David Tolnay8c730492020-03-13 01:29:06 -0700619 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400620 inner, instance, inner,
621 );
622 writeln!(out, " return ptr.release();");
623 writeln!(out, "}}");
624 writeln!(
625 out,
David Tolnay8c730492020-03-13 01:29:06 -0700626 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400627 instance, inner,
628 );
629 writeln!(out, " ptr->~unique_ptr();");
630 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700631 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400632}