blob: ccb5a0fa29a8e59d94afdf0f4bc94fc72b438c05 [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) {
David Tolnay1e548172020-03-16 13:37:09 -0700274 if efn.throws {
275 write!(out, "::rust::Str::Repr ");
276 } else {
277 write_extern_return_type(out, &efn.ret, types);
278 }
David Tolnay7db73692019-10-20 14:51:12 -0400279 for name in out.namespace.clone() {
280 write!(out, "{}$", name);
281 }
David Tolnay8c730492020-03-13 01:29:06 -0700282 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400283 for (i, arg) in efn.args.iter().enumerate() {
284 if i > 0 {
285 write!(out, ", ");
286 }
287 write_extern_arg(out, arg, types);
288 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700289 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400290 if !efn.args.is_empty() {
291 write!(out, ", ");
292 }
293 write_return_type(out, &efn.ret);
294 write!(out, "*return$");
295 }
296 writeln!(out, ") noexcept;");
297}
298
299fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400300 for line in efn.doc.to_string().lines() {
301 writeln!(out, "//{}", line);
302 }
303 write_return_type(out, &efn.ret);
304 write!(out, "{}(", efn.ident);
305 for (i, arg) in efn.args.iter().enumerate() {
306 if i > 0 {
307 write!(out, ", ");
308 }
309 write_type_space(out, &arg.ty);
310 write!(out, "{}", arg.ident);
311 }
David Tolnay1e548172020-03-16 13:37:09 -0700312 write!(out, ")");
313 if !efn.throws {
314 write!(out, " noexcept");
315 }
David Tolnay7db73692019-10-20 14:51:12 -0400316 if out.header {
317 writeln!(out, ";");
318 } else {
319 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800320 for arg in &efn.args {
321 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
322 write!(out, " ::rust::ManuallyDrop<");
323 write_type(out, &arg.ty);
324 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
325 }
326 }
David Tolnay7db73692019-10-20 14:51:12 -0400327 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700328 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400329 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800330 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400331 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800332 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400333 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800334 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400335 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800336 match ret {
337 Type::RustBox(_) => {
338 write_type(out, ret);
339 write!(out, "::from_raw(");
340 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800341 Type::UniquePtr(_) => {
342 write_type(out, ret);
343 write!(out, "(");
344 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800345 Type::Ref(_) => write!(out, "*"),
346 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800347 }
David Tolnay7db73692019-10-20 14:51:12 -0400348 }
David Tolnay1e548172020-03-16 13:37:09 -0700349 if efn.throws {
350 write!(out, "::rust::Str::Repr error$ = ");
351 }
David Tolnay7db73692019-10-20 14:51:12 -0400352 for name in out.namespace.clone() {
353 write!(out, "{}$", name);
354 }
David Tolnay8c730492020-03-13 01:29:06 -0700355 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400356 for (i, arg) in efn.args.iter().enumerate() {
357 if i > 0 {
358 write!(out, ", ");
359 }
David Tolnaybaae4432020-03-01 20:20:10 -0800360 match &arg.ty {
361 Type::Str(_) => write!(out, "::rust::Str::Repr("),
362 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
363 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400364 }
365 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800366 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800367 Type::RustBox(_) => write!(out, ".into_raw()"),
368 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800369 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800370 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800371 _ => {}
372 }
David Tolnay7db73692019-10-20 14:51:12 -0400373 }
374 if indirect_return {
375 if !efn.args.is_empty() {
376 write!(out, ", ");
377 }
David Tolnay09011c32020-03-06 14:40:28 -0800378 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400379 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800380 write!(out, ")");
381 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800382 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800383 write!(out, ")");
384 }
385 }
386 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700387 if efn.throws {
388 writeln!(out, " if (error$.ptr) {{");
389 writeln!(out, " throw ::rust::Error(error$);");
390 writeln!(out, " }}");
391 }
David Tolnay7db73692019-10-20 14:51:12 -0400392 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800393 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400394 }
395 writeln!(out, "}}");
396 }
397}
398
399fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
400 match ty {
401 None => write!(out, "void "),
402 Some(ty) => write_type_space(out, ty),
403 }
404}
405
David Tolnay277e3cc2020-03-17 00:11:01 -0700406fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
407 efn.ret
408 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700409 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700410}
411
David Tolnay7db73692019-10-20 14:51:12 -0400412fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
413 match ty {
414 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
415 write_type_space(out, &ty.inner);
416 write!(out, "*");
417 }
David Tolnay4a441222020-01-25 16:24:27 -0800418 Some(Type::Ref(ty)) => {
419 if ty.mutability.is_none() {
420 write!(out, "const ");
421 }
422 write_type(out, &ty.inner);
423 write!(out, " *");
424 }
David Tolnay750755e2020-03-01 13:04:08 -0800425 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400426 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
427 _ => write_return_type(out, ty),
428 }
429}
430
431fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
432 match &arg.ty {
433 Type::RustBox(ty) | Type::UniquePtr(ty) => {
434 write_type_space(out, &ty.inner);
435 write!(out, "*");
436 }
David Tolnay750755e2020-03-01 13:04:08 -0800437 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400438 _ => write_type_space(out, &arg.ty),
439 }
440 if types.needs_indirect_abi(&arg.ty) {
441 write!(out, "*");
442 }
443 write!(out, "{}", arg.ident);
444}
445
446fn write_type(out: &mut OutFile, ty: &Type) {
447 match ty {
448 Type::Ident(ident) => match Atom::from(ident) {
449 Some(Bool) => write!(out, "bool"),
450 Some(U8) => write!(out, "uint8_t"),
451 Some(U16) => write!(out, "uint16_t"),
452 Some(U32) => write!(out, "uint32_t"),
453 Some(U64) => write!(out, "uint64_t"),
454 Some(Usize) => write!(out, "size_t"),
455 Some(I8) => write!(out, "int8_t"),
456 Some(I16) => write!(out, "int16_t"),
457 Some(I32) => write!(out, "int32_t"),
458 Some(I64) => write!(out, "int64_t"),
459 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700460 Some(F32) => write!(out, "float"),
461 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800462 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800463 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400464 None => write!(out, "{}", ident),
465 },
466 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800467 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400468 write_type(out, &ty.inner);
469 write!(out, ">");
470 }
471 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800472 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400473 write_type(out, &ptr.inner);
474 write!(out, ">");
475 }
476 Type::Ref(r) => {
477 if r.mutability.is_none() {
478 write!(out, "const ");
479 }
480 write_type(out, &r.inner);
481 write!(out, " &");
482 }
483 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800484 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400485 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700486 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400487 }
488}
489
490fn write_type_space(out: &mut OutFile, ty: &Type) {
491 write_type(out, ty);
492 match ty {
493 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
494 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700495 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400496 }
497}
498
499fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
500 fn allow_unique_ptr(ident: &Ident) -> bool {
501 Atom::from(ident).is_none()
502 }
503
504 out.begin_block("extern \"C\"");
505 for ty in types {
506 if let Type::RustBox(ty) = ty {
507 if let Type::Ident(inner) = &ty.inner {
508 out.next_section();
509 write_rust_box_extern(out, inner);
510 }
511 } else if let Type::UniquePtr(ptr) = ty {
512 if let Type::Ident(inner) = &ptr.inner {
513 if allow_unique_ptr(inner) {
514 out.next_section();
515 write_unique_ptr(out, inner);
516 }
517 }
518 }
519 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800520 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400521
David Tolnay750755e2020-03-01 13:04:08 -0800522 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700523 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400524 for ty in types {
525 if let Type::RustBox(ty) = ty {
526 if let Type::Ident(inner) = &ty.inner {
527 write_rust_box_impl(out, inner);
528 }
529 }
530 }
David Tolnay8c730492020-03-13 01:29:06 -0700531 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800532 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400533}
534
535fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
536 let mut inner = String::new();
537 for name in &out.namespace {
538 inner += name;
539 inner += "::";
540 }
541 inner += &ident.to_string();
542 let instance = inner.replace("::", "$");
543
David Tolnay8c730492020-03-13 01:29:06 -0700544 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
545 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400546 writeln!(
547 out,
David Tolnay8c730492020-03-13 01:29:06 -0700548 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400549 instance, inner,
550 );
551 writeln!(
552 out,
David Tolnay8c730492020-03-13 01:29:06 -0700553 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400554 instance, inner,
555 );
David Tolnay8c730492020-03-13 01:29:06 -0700556 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400557}
558
559fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
560 let mut inner = String::new();
561 for name in &out.namespace {
562 inner += name;
563 inner += "::";
564 }
565 inner += &ident.to_string();
566 let instance = inner.replace("::", "$");
567
568 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800569 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700570 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400571 writeln!(out, "}}");
572
573 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800574 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700575 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400576 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400577}
578
579fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
580 let mut inner = String::new();
581 for name in &out.namespace {
582 inner += name;
583 inner += "::";
584 }
585 inner += &ident.to_string();
586 let instance = inner.replace("::", "$");
587
David Tolnay8c730492020-03-13 01:29:06 -0700588 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
589 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400590 writeln!(
591 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800592 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400593 inner,
594 );
595 writeln!(
596 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800597 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400598 inner,
599 );
600 writeln!(
601 out,
David Tolnay8c730492020-03-13 01:29:06 -0700602 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400603 instance, inner,
604 );
David Tolnay7e219b82020-03-01 13:14:51 -0800605 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400606 writeln!(out, "}}");
607 writeln!(
608 out,
David Tolnay8c730492020-03-13 01:29:06 -0700609 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400610 instance, inner, inner,
611 );
612 writeln!(
613 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800614 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400615 inner, inner,
616 );
617 writeln!(out, "}}");
618 writeln!(
619 out,
David Tolnay8c730492020-03-13 01:29:06 -0700620 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400621 instance, inner, inner,
622 );
David Tolnay7e219b82020-03-01 13:14:51 -0800623 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400624 writeln!(out, "}}");
625 writeln!(
626 out,
David Tolnay8c730492020-03-13 01:29:06 -0700627 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400628 inner, instance, inner,
629 );
630 writeln!(out, " return ptr.get();");
631 writeln!(out, "}}");
632 writeln!(
633 out,
David Tolnay8c730492020-03-13 01:29:06 -0700634 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400635 inner, instance, inner,
636 );
637 writeln!(out, " return ptr.release();");
638 writeln!(out, "}}");
639 writeln!(
640 out,
David Tolnay8c730492020-03-13 01:29:06 -0700641 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400642 instance, inner,
643 );
644 writeln!(out, " ptr->~unique_ptr();");
645 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700646 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400647}