blob: 3398f9a8f885899ded1a7c8d86356e4441b3fa9a [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\"");
David Tolnayebef4a22020-03-17 15:33:47 -070048 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040049 for api in apis {
50 let (efn, write): (_, fn(_, _, _)) = match api {
51 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
52 Api::RustFunction(efn) => (efn, write_rust_function_decl),
53 _ => continue,
54 };
55 out.next_section();
56 write(out, efn, types);
57 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080058 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040059 }
60
61 for api in apis {
62 if let Api::RustFunction(efn) = api {
63 out.next_section();
64 write_rust_function_shim(out, efn, types);
65 }
66 }
67
68 out.next_section();
69 for name in namespace.iter().rev() {
70 writeln!(out, "}} // namespace {}", name);
71 }
72
73 if !header {
74 out.next_section();
75 write_generic_instantiations(out, types);
76 }
77
David Tolnay9c68b1a2020-03-06 11:12:55 -080078 out.prepend(out.include.to_string());
79
David Tolnay7db73692019-10-20 14:51:12 -040080 out_file
81}
82
83fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040084 for ty in types {
85 match ty {
86 Type::Ident(ident) => match Atom::from(ident) {
87 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay9c68b1a2020-03-06 11:12:55 -080088 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => out.include.cstdint = true,
89 Some(CxxString) => out.include.string = true,
David Tolnay3383ae72020-03-13 01:12:26 -070090 Some(Bool) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -040091 },
David Tolnay9c68b1a2020-03-06 11:12:55 -080092 Type::RustBox(_) => out.include.type_traits = true,
93 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -040094 _ => {}
95 }
96 }
David Tolnay7db73692019-10-20 14:51:12 -040097}
98
David Tolnayf51447e2020-03-06 14:14:27 -080099fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400100 let mut needs_rust_box = false;
101 for ty in types {
102 if let Type::RustBox(_) = ty {
103 needs_rust_box = true;
104 break;
105 }
106 }
107
David Tolnayf51447e2020-03-06 14:14:27 -0800108 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800109 let mut needs_maybe_uninit = false;
110 for api in apis {
David Tolnayf51447e2020-03-06 14:14:27 -0800111 if let Api::RustFunction(efn) = api {
112 for arg in &efn.args {
113 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
114 needs_manually_drop = true;
David Tolnay09011c32020-03-06 14:40:28 -0800115 break;
116 }
117 }
118 if let Some(ret) = &efn.ret {
119 if types.needs_indirect_abi(ret) {
120 needs_maybe_uninit = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800121 }
122 }
123 }
124 }
125
David Tolnay750755e2020-03-01 13:04:08 -0800126 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700127 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800128
David Tolnay09011c32020-03-06 14:40:28 -0800129 if needs_rust_box || needs_manually_drop || needs_maybe_uninit {
David Tolnay736cbca2020-03-11 16:49:18 -0700130 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800131 }
132
133 if needs_rust_box {
134 out.next_section();
David Tolnay8c730492020-03-13 01:29:06 -0700135 for line in include::get("CXXBRIDGE02_RUST_BOX").lines() {
David Tolnay7db73692019-10-20 14:51:12 -0400136 if !line.trim_start().starts_with("//") {
137 writeln!(out, "{}", line);
138 }
139 }
140 }
David Tolnayf51447e2020-03-06 14:14:27 -0800141
142 if needs_manually_drop {
143 out.next_section();
144 writeln!(out, "template <typename T>");
145 writeln!(out, "union ManuallyDrop {{");
146 writeln!(out, " T value;");
147 writeln!(
148 out,
149 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
150 );
151 writeln!(out, " ~ManuallyDrop() {{}}");
152 writeln!(out, "}};");
153 }
154
David Tolnay09011c32020-03-06 14:40:28 -0800155 if needs_maybe_uninit {
156 out.next_section();
157 writeln!(out, "template <typename T>");
158 writeln!(out, "union MaybeUninit {{");
159 writeln!(out, " T value;");
160 writeln!(out, " MaybeUninit() {{}}");
161 writeln!(out, " ~MaybeUninit() {{}}");
162 writeln!(out, "}};");
163 }
164
David Tolnay8c730492020-03-13 01:29:06 -0700165 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800166 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400167}
168
169fn write_struct(out: &mut OutFile, strct: &Struct) {
170 for line in strct.doc.to_string().lines() {
171 writeln!(out, "//{}", line);
172 }
173 writeln!(out, "struct {} final {{", strct.ident);
174 for field in &strct.fields {
175 write!(out, " ");
176 write_type_space(out, &field.ty);
177 writeln!(out, "{};", field.ident);
178 }
179 writeln!(out, "}};");
180}
181
182fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
183 writeln!(out, "struct {};", ident);
184}
185
David Tolnay8861bee2020-01-20 18:39:24 -0800186fn write_struct_using(out: &mut OutFile, ident: &Ident) {
187 writeln!(out, "using {} = {};", ident, ident);
188}
189
David Tolnayebef4a22020-03-17 15:33:47 -0700190fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
191 let mut has_cxx_throws = false;
192 for api in apis {
193 if let Api::CxxFunction(efn) = api {
194 if efn.throws {
195 has_cxx_throws = true;
196 break;
197 }
198 }
199 }
200
201 if has_cxx_throws {
202 out.next_section();
203 write!(
204 out,
205 "const char *cxxbridge02$exception(const char *, size_t);",
206 );
207 }
208}
209
David Tolnay7db73692019-10-20 14:51:12 -0400210fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700211 if efn.throws {
212 write!(out, "::rust::Str::Repr ");
213 } else {
214 write_extern_return_type(out, &efn.ret, types);
215 }
David Tolnay7db73692019-10-20 14:51:12 -0400216 for name in out.namespace.clone() {
217 write!(out, "{}$", name);
218 }
David Tolnay8c730492020-03-13 01:29:06 -0700219 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400220 for (i, arg) in efn.args.iter().enumerate() {
221 if i > 0 {
222 write!(out, ", ");
223 }
David Tolnaya46a2372020-03-06 10:03:48 -0800224 if arg.ty == RustString {
225 write!(out, "const ");
226 }
David Tolnay7db73692019-10-20 14:51:12 -0400227 write_extern_arg(out, arg, types);
228 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700229 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400230 if indirect_return {
231 if !efn.args.is_empty() {
232 write!(out, ", ");
233 }
234 write_return_type(out, &efn.ret);
235 write!(out, "*return$");
236 }
237 writeln!(out, ") noexcept {{");
238 write!(out, " ");
239 write_return_type(out, &efn.ret);
240 write!(out, "(*{}$)(", efn.ident);
241 for (i, arg) in efn.args.iter().enumerate() {
242 if i > 0 {
243 write!(out, ", ");
244 }
245 write_type(out, &arg.ty);
246 }
247 writeln!(out, ") = {};", efn.ident);
248 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700249 if efn.throws {
250 writeln!(out, "::rust::Str::Repr throw$;");
251 writeln!(out, " try {{");
252 write!(out, " ");
253 }
David Tolnay7db73692019-10-20 14:51:12 -0400254 if indirect_return {
255 write!(out, "new (return$) ");
256 write_type(out, efn.ret.as_ref().unwrap());
257 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800258 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400259 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800260 match ret {
261 Type::Ref(_) => write!(out, "&"),
262 Type::Str(_) => write!(out, "::rust::Str::Repr("),
263 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800264 }
David Tolnay7db73692019-10-20 14:51:12 -0400265 }
266 write!(out, "{}$(", efn.ident);
267 for (i, arg) in efn.args.iter().enumerate() {
268 if i > 0 {
269 write!(out, ", ");
270 }
271 if let Type::RustBox(_) = &arg.ty {
272 write_type(out, &arg.ty);
273 write!(out, "::from_raw({})", arg.ident);
274 } else if let Type::UniquePtr(_) = &arg.ty {
275 write_type(out, &arg.ty);
276 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800277 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800278 write!(
279 out,
280 "::rust::String(::rust::unsafe_bitcopy, *{})",
281 arg.ident,
282 );
David Tolnay7db73692019-10-20 14:51:12 -0400283 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800284 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400285 } else {
286 write!(out, "{}", arg.ident);
287 }
288 }
289 write!(out, ")");
290 match &efn.ret {
291 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
292 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800293 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400294 _ => {}
295 }
296 if indirect_return {
297 write!(out, ")");
298 }
299 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700300 if efn.throws {
301 out.include.cstring = true;
302 writeln!(out, " throw$.ptr = nullptr;");
303 writeln!(out, " }} catch (const ::std::exception &catch$) {{");
304 writeln!(out, " const char *return$ = catch$.what();");
305 writeln!(out, " throw$.len = ::std::strlen(return$);");
306 writeln!(
307 out,
308 " throw$.ptr = cxxbridge02$exception(return$, throw$.len);",
309 );
310 writeln!(out, " }}");
311 writeln!(out, " return throw$;");
312 }
David Tolnay7db73692019-10-20 14:51:12 -0400313 writeln!(out, "}}");
314}
315
316fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700317 if efn.throws {
318 write!(out, "::rust::Str::Repr ");
319 } else {
320 write_extern_return_type(out, &efn.ret, types);
321 }
David Tolnay7db73692019-10-20 14:51:12 -0400322 for name in out.namespace.clone() {
323 write!(out, "{}$", name);
324 }
David Tolnay8c730492020-03-13 01:29:06 -0700325 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400326 for (i, arg) in efn.args.iter().enumerate() {
327 if i > 0 {
328 write!(out, ", ");
329 }
330 write_extern_arg(out, arg, types);
331 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700332 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400333 if !efn.args.is_empty() {
334 write!(out, ", ");
335 }
336 write_return_type(out, &efn.ret);
337 write!(out, "*return$");
338 }
339 writeln!(out, ") noexcept;");
340}
341
342fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400343 for line in efn.doc.to_string().lines() {
344 writeln!(out, "//{}", line);
345 }
346 write_return_type(out, &efn.ret);
347 write!(out, "{}(", efn.ident);
348 for (i, arg) in efn.args.iter().enumerate() {
349 if i > 0 {
350 write!(out, ", ");
351 }
352 write_type_space(out, &arg.ty);
353 write!(out, "{}", arg.ident);
354 }
David Tolnay1e548172020-03-16 13:37:09 -0700355 write!(out, ")");
356 if !efn.throws {
357 write!(out, " noexcept");
358 }
David Tolnay7db73692019-10-20 14:51:12 -0400359 if out.header {
360 writeln!(out, ";");
361 } else {
362 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800363 for arg in &efn.args {
364 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
365 write!(out, " ::rust::ManuallyDrop<");
366 write_type(out, &arg.ty);
367 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
368 }
369 }
David Tolnay7db73692019-10-20 14:51:12 -0400370 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700371 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400372 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800373 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400374 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800375 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400376 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800377 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400378 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800379 match ret {
380 Type::RustBox(_) => {
381 write_type(out, ret);
382 write!(out, "::from_raw(");
383 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800384 Type::UniquePtr(_) => {
385 write_type(out, ret);
386 write!(out, "(");
387 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800388 Type::Ref(_) => write!(out, "*"),
389 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800390 }
David Tolnay7db73692019-10-20 14:51:12 -0400391 }
David Tolnay1e548172020-03-16 13:37:09 -0700392 if efn.throws {
393 write!(out, "::rust::Str::Repr error$ = ");
394 }
David Tolnay7db73692019-10-20 14:51:12 -0400395 for name in out.namespace.clone() {
396 write!(out, "{}$", name);
397 }
David Tolnay8c730492020-03-13 01:29:06 -0700398 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400399 for (i, arg) in efn.args.iter().enumerate() {
400 if i > 0 {
401 write!(out, ", ");
402 }
David Tolnaybaae4432020-03-01 20:20:10 -0800403 match &arg.ty {
404 Type::Str(_) => write!(out, "::rust::Str::Repr("),
405 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
406 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400407 }
408 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800409 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800410 Type::RustBox(_) => write!(out, ".into_raw()"),
411 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800412 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800413 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800414 _ => {}
415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 }
417 if indirect_return {
418 if !efn.args.is_empty() {
419 write!(out, ", ");
420 }
David Tolnay09011c32020-03-06 14:40:28 -0800421 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400422 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800423 write!(out, ")");
424 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800425 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800426 write!(out, ")");
427 }
428 }
429 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700430 if efn.throws {
431 writeln!(out, " if (error$.ptr) {{");
432 writeln!(out, " throw ::rust::Error(error$);");
433 writeln!(out, " }}");
434 }
David Tolnay7db73692019-10-20 14:51:12 -0400435 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800436 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400437 }
438 writeln!(out, "}}");
439 }
440}
441
442fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
443 match ty {
444 None => write!(out, "void "),
445 Some(ty) => write_type_space(out, ty),
446 }
447}
448
David Tolnay277e3cc2020-03-17 00:11:01 -0700449fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
450 efn.ret
451 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700452 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700453}
454
David Tolnay7db73692019-10-20 14:51:12 -0400455fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
456 match ty {
457 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
458 write_type_space(out, &ty.inner);
459 write!(out, "*");
460 }
David Tolnay4a441222020-01-25 16:24:27 -0800461 Some(Type::Ref(ty)) => {
462 if ty.mutability.is_none() {
463 write!(out, "const ");
464 }
465 write_type(out, &ty.inner);
466 write!(out, " *");
467 }
David Tolnay750755e2020-03-01 13:04:08 -0800468 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400469 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
470 _ => write_return_type(out, ty),
471 }
472}
473
474fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
475 match &arg.ty {
476 Type::RustBox(ty) | Type::UniquePtr(ty) => {
477 write_type_space(out, &ty.inner);
478 write!(out, "*");
479 }
David Tolnay750755e2020-03-01 13:04:08 -0800480 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400481 _ => write_type_space(out, &arg.ty),
482 }
483 if types.needs_indirect_abi(&arg.ty) {
484 write!(out, "*");
485 }
486 write!(out, "{}", arg.ident);
487}
488
489fn write_type(out: &mut OutFile, ty: &Type) {
490 match ty {
491 Type::Ident(ident) => match Atom::from(ident) {
492 Some(Bool) => write!(out, "bool"),
493 Some(U8) => write!(out, "uint8_t"),
494 Some(U16) => write!(out, "uint16_t"),
495 Some(U32) => write!(out, "uint32_t"),
496 Some(U64) => write!(out, "uint64_t"),
497 Some(Usize) => write!(out, "size_t"),
498 Some(I8) => write!(out, "int8_t"),
499 Some(I16) => write!(out, "int16_t"),
500 Some(I32) => write!(out, "int32_t"),
501 Some(I64) => write!(out, "int64_t"),
502 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700503 Some(F32) => write!(out, "float"),
504 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800505 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800506 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400507 None => write!(out, "{}", ident),
508 },
509 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800510 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400511 write_type(out, &ty.inner);
512 write!(out, ">");
513 }
514 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800515 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400516 write_type(out, &ptr.inner);
517 write!(out, ">");
518 }
519 Type::Ref(r) => {
520 if r.mutability.is_none() {
521 write!(out, "const ");
522 }
523 write_type(out, &r.inner);
524 write!(out, " &");
525 }
526 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800527 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400528 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700529 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400530 }
531}
532
533fn write_type_space(out: &mut OutFile, ty: &Type) {
534 write_type(out, ty);
535 match ty {
536 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
537 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700538 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400539 }
540}
541
542fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
543 fn allow_unique_ptr(ident: &Ident) -> bool {
544 Atom::from(ident).is_none()
545 }
546
547 out.begin_block("extern \"C\"");
548 for ty in types {
549 if let Type::RustBox(ty) = ty {
550 if let Type::Ident(inner) = &ty.inner {
551 out.next_section();
552 write_rust_box_extern(out, inner);
553 }
554 } else if let Type::UniquePtr(ptr) = ty {
555 if let Type::Ident(inner) = &ptr.inner {
556 if allow_unique_ptr(inner) {
557 out.next_section();
558 write_unique_ptr(out, inner);
559 }
560 }
561 }
562 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800563 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400564
David Tolnay750755e2020-03-01 13:04:08 -0800565 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700566 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400567 for ty in types {
568 if let Type::RustBox(ty) = ty {
569 if let Type::Ident(inner) = &ty.inner {
570 write_rust_box_impl(out, inner);
571 }
572 }
573 }
David Tolnay8c730492020-03-13 01:29:06 -0700574 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800575 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400576}
577
578fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
579 let mut inner = String::new();
580 for name in &out.namespace {
581 inner += name;
582 inner += "::";
583 }
584 inner += &ident.to_string();
585 let instance = inner.replace("::", "$");
586
David Tolnay8c730492020-03-13 01:29:06 -0700587 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
588 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400589 writeln!(
590 out,
David Tolnay8c730492020-03-13 01:29:06 -0700591 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400592 instance, inner,
593 );
594 writeln!(
595 out,
David Tolnay8c730492020-03-13 01:29:06 -0700596 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400597 instance, inner,
598 );
David Tolnay8c730492020-03-13 01:29:06 -0700599 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400600}
601
602fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
603 let mut inner = String::new();
604 for name in &out.namespace {
605 inner += name;
606 inner += "::";
607 }
608 inner += &ident.to_string();
609 let instance = inner.replace("::", "$");
610
611 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800612 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700613 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400614 writeln!(out, "}}");
615
616 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800617 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700618 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400619 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400620}
621
622fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
623 let mut inner = String::new();
624 for name in &out.namespace {
625 inner += name;
626 inner += "::";
627 }
628 inner += &ident.to_string();
629 let instance = inner.replace("::", "$");
630
David Tolnay8c730492020-03-13 01:29:06 -0700631 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
632 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400633 writeln!(
634 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800635 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400636 inner,
637 );
638 writeln!(
639 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800640 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400641 inner,
642 );
643 writeln!(
644 out,
David Tolnay8c730492020-03-13 01:29:06 -0700645 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400646 instance, inner,
647 );
David Tolnay7e219b82020-03-01 13:14:51 -0800648 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400649 writeln!(out, "}}");
650 writeln!(
651 out,
David Tolnay8c730492020-03-13 01:29:06 -0700652 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400653 instance, inner, inner,
654 );
655 writeln!(
656 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800657 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400658 inner, inner,
659 );
660 writeln!(out, "}}");
661 writeln!(
662 out,
David Tolnay8c730492020-03-13 01:29:06 -0700663 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400664 instance, inner, inner,
665 );
David Tolnay7e219b82020-03-01 13:14:51 -0800666 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400667 writeln!(out, "}}");
668 writeln!(
669 out,
David Tolnay8c730492020-03-13 01:29:06 -0700670 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400671 inner, instance, inner,
672 );
673 writeln!(out, " return ptr.get();");
674 writeln!(out, "}}");
675 writeln!(
676 out,
David Tolnay8c730492020-03-13 01:29:06 -0700677 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400678 inner, instance, inner,
679 );
680 writeln!(out, " return ptr.release();");
681 writeln!(out, "}}");
682 writeln!(
683 out,
David Tolnay8c730492020-03-13 01:29:06 -0700684 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400685 instance, inner,
686 );
687 writeln!(out, " ptr->~unique_ptr();");
688 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700689 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400690}