blob: 33674b40ac2c680a2ea69e3202dbdfd10bbe20ec [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 Tolnayb7a7cb62020-03-17 21:18:40 -0700100 let mut needs_rust_string = false;
101 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400102 let mut needs_rust_box = false;
103 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700104 match ty {
105 Type::RustBox(_) => {
106 out.include.type_traits = true;
107 needs_rust_box = true;
108 }
109 Type::Str(_) => {
110 out.include.cstdint = true;
111 out.include.string = true;
112 needs_rust_str = true;
113 }
114 ty if ty == RustString => {
115 out.include.array = true;
116 out.include.cstdint = true;
117 out.include.string = true;
118 needs_rust_string = true;
119 }
120 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400121 }
122 }
123
David Tolnayb7a7cb62020-03-17 21:18:40 -0700124 let mut needs_rust_error = false;
125 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800126 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800127 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700128 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800129 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700130 match api {
131 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700132 if efn.throws {
133 needs_trycatch = true;
134 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700135 for arg in &efn.args {
136 if arg.ty == RustString {
137 needs_unsafe_bitcopy = true;
138 break;
139 }
David Tolnay09011c32020-03-06 14:40:28 -0800140 }
141 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 Api::RustFunction(efn) if !out.header => {
143 if efn.throws {
144 out.include.exception = true;
145 needs_rust_error = true;
146 }
147 for arg in &efn.args {
148 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
149 needs_manually_drop = true;
150 break;
151 }
152 }
153 if let Some(ret) = &efn.ret {
154 if types.needs_indirect_abi(ret) {
155 needs_maybe_uninit = true;
156 }
David Tolnayf51447e2020-03-06 14:14:27 -0800157 }
158 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700159 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800160 }
161 }
162
David Tolnay750755e2020-03-01 13:04:08 -0800163 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700164 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800165
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 if needs_rust_string
167 || needs_rust_str
168 || needs_rust_box
169 || needs_rust_error
170 || needs_unsafe_bitcopy
171 || needs_manually_drop
172 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700173 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700174 {
David Tolnay736cbca2020-03-11 16:49:18 -0700175 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800176 }
177
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
179 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
180 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
181 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
182 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800183
184 if needs_manually_drop {
185 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700186 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800187 writeln!(out, "template <typename T>");
188 writeln!(out, "union ManuallyDrop {{");
189 writeln!(out, " T value;");
190 writeln!(
191 out,
192 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
193 );
194 writeln!(out, " ~ManuallyDrop() {{}}");
195 writeln!(out, "}};");
196 }
197
David Tolnay09011c32020-03-06 14:40:28 -0800198 if needs_maybe_uninit {
199 out.next_section();
200 writeln!(out, "template <typename T>");
201 writeln!(out, "union MaybeUninit {{");
202 writeln!(out, " T value;");
203 writeln!(out, " MaybeUninit() {{}}");
204 writeln!(out, " ~MaybeUninit() {{}}");
205 writeln!(out, "}};");
206 }
207
David Tolnay5d121442020-03-17 22:14:40 -0700208 if needs_trycatch {
209 out.next_section();
210 out.include.exception = true;
211 writeln!(out, "template <typename Try, typename Fail>");
212 writeln!(
213 out,
214 "static void trycatch(Try &&func, Fail &&fail) noexcept try {{",
215 );
216 writeln!(out, " func();");
217 writeln!(out, "}} catch (const ::std::exception &e) {{");
218 writeln!(out, " fail(e.what());");
219 writeln!(out, "}}");
220 }
221
David Tolnay8c730492020-03-13 01:29:06 -0700222 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800223 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400224}
225
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
227 if needed {
228 out.next_section();
229 for line in include::get(section).lines() {
230 if !line.trim_start().starts_with("//") {
231 writeln!(out, "{}", line);
232 }
233 }
234 }
235}
236
David Tolnay7db73692019-10-20 14:51:12 -0400237fn write_struct(out: &mut OutFile, strct: &Struct) {
238 for line in strct.doc.to_string().lines() {
239 writeln!(out, "//{}", line);
240 }
241 writeln!(out, "struct {} final {{", strct.ident);
242 for field in &strct.fields {
243 write!(out, " ");
244 write_type_space(out, &field.ty);
245 writeln!(out, "{};", field.ident);
246 }
247 writeln!(out, "}};");
248}
249
250fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
251 writeln!(out, "struct {};", ident);
252}
253
David Tolnay8861bee2020-01-20 18:39:24 -0800254fn write_struct_using(out: &mut OutFile, ident: &Ident) {
255 writeln!(out, "using {} = {};", ident, ident);
256}
257
David Tolnayebef4a22020-03-17 15:33:47 -0700258fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
259 let mut has_cxx_throws = false;
260 for api in apis {
261 if let Api::CxxFunction(efn) = api {
262 if efn.throws {
263 has_cxx_throws = true;
264 break;
265 }
266 }
267 }
268
269 if has_cxx_throws {
270 out.next_section();
271 write!(
272 out,
273 "const char *cxxbridge02$exception(const char *, size_t);",
274 );
275 }
276}
277
David Tolnay7db73692019-10-20 14:51:12 -0400278fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700279 if efn.throws {
280 write!(out, "::rust::Str::Repr ");
281 } else {
282 write_extern_return_type(out, &efn.ret, types);
283 }
David Tolnay7db73692019-10-20 14:51:12 -0400284 for name in out.namespace.clone() {
285 write!(out, "{}$", name);
286 }
David Tolnay8c730492020-03-13 01:29:06 -0700287 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400288 for (i, arg) in efn.args.iter().enumerate() {
289 if i > 0 {
290 write!(out, ", ");
291 }
David Tolnaya46a2372020-03-06 10:03:48 -0800292 if arg.ty == RustString {
293 write!(out, "const ");
294 }
David Tolnay7db73692019-10-20 14:51:12 -0400295 write_extern_arg(out, arg, types);
296 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700297 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400298 if indirect_return {
299 if !efn.args.is_empty() {
300 write!(out, ", ");
301 }
302 write_return_type(out, &efn.ret);
303 write!(out, "*return$");
304 }
305 writeln!(out, ") noexcept {{");
306 write!(out, " ");
307 write_return_type(out, &efn.ret);
308 write!(out, "(*{}$)(", efn.ident);
309 for (i, arg) in efn.args.iter().enumerate() {
310 if i > 0 {
311 write!(out, ", ");
312 }
313 write_type(out, &arg.ty);
314 }
315 writeln!(out, ") = {};", efn.ident);
316 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700317 if efn.throws {
318 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay5d121442020-03-17 22:14:40 -0700319 writeln!(out, " ::rust::trycatch(");
320 writeln!(out, " [&] {{");
321 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700322 }
David Tolnay7db73692019-10-20 14:51:12 -0400323 if indirect_return {
324 write!(out, "new (return$) ");
325 write_type(out, efn.ret.as_ref().unwrap());
326 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 Tolnaybaae4432020-03-01 20:20:10 -0800329 match ret {
330 Type::Ref(_) => write!(out, "&"),
331 Type::Str(_) => write!(out, "::rust::Str::Repr("),
332 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800333 }
David Tolnay7db73692019-10-20 14:51:12 -0400334 }
335 write!(out, "{}$(", efn.ident);
336 for (i, arg) in efn.args.iter().enumerate() {
337 if i > 0 {
338 write!(out, ", ");
339 }
340 if let Type::RustBox(_) = &arg.ty {
341 write_type(out, &arg.ty);
342 write!(out, "::from_raw({})", arg.ident);
343 } else if let Type::UniquePtr(_) = &arg.ty {
344 write_type(out, &arg.ty);
345 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800346 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800347 write!(
348 out,
349 "::rust::String(::rust::unsafe_bitcopy, *{})",
350 arg.ident,
351 );
David Tolnay7db73692019-10-20 14:51:12 -0400352 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700353 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800354 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400355 } else {
356 write!(out, "{}", arg.ident);
357 }
358 }
359 write!(out, ")");
360 match &efn.ret {
361 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
362 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800363 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400364 _ => {}
365 }
366 if indirect_return {
367 write!(out, ")");
368 }
369 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700370 if efn.throws {
371 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700372 writeln!(out, " throw$.ptr = nullptr;");
373 writeln!(out, " }},");
374 writeln!(out, " [&](const char *catch$) {{");
375 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700376 writeln!(
377 out,
David Tolnay5d121442020-03-17 22:14:40 -0700378 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700379 );
David Tolnay5d121442020-03-17 22:14:40 -0700380 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700381 writeln!(out, " return throw$;");
382 }
David Tolnay7db73692019-10-20 14:51:12 -0400383 writeln!(out, "}}");
384}
385
386fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700387 if efn.throws {
388 write!(out, "::rust::Str::Repr ");
389 } else {
390 write_extern_return_type(out, &efn.ret, types);
391 }
David Tolnay7db73692019-10-20 14:51:12 -0400392 for name in out.namespace.clone() {
393 write!(out, "{}$", name);
394 }
David Tolnay8c730492020-03-13 01:29:06 -0700395 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400396 for (i, arg) in efn.args.iter().enumerate() {
397 if i > 0 {
398 write!(out, ", ");
399 }
400 write_extern_arg(out, arg, types);
401 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700402 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400403 if !efn.args.is_empty() {
404 write!(out, ", ");
405 }
406 write_return_type(out, &efn.ret);
407 write!(out, "*return$");
408 }
409 writeln!(out, ") noexcept;");
410}
411
412fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400413 for line in efn.doc.to_string().lines() {
414 writeln!(out, "//{}", line);
415 }
416 write_return_type(out, &efn.ret);
417 write!(out, "{}(", efn.ident);
418 for (i, arg) in efn.args.iter().enumerate() {
419 if i > 0 {
420 write!(out, ", ");
421 }
422 write_type_space(out, &arg.ty);
423 write!(out, "{}", arg.ident);
424 }
David Tolnay1e548172020-03-16 13:37:09 -0700425 write!(out, ")");
426 if !efn.throws {
427 write!(out, " noexcept");
428 }
David Tolnay7db73692019-10-20 14:51:12 -0400429 if out.header {
430 writeln!(out, ";");
431 } else {
432 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800433 for arg in &efn.args {
434 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700435 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800436 write!(out, " ::rust::ManuallyDrop<");
437 write_type(out, &arg.ty);
438 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
439 }
440 }
David Tolnay7db73692019-10-20 14:51:12 -0400441 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700442 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400443 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800444 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400445 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800446 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400447 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800448 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400449 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800450 match ret {
451 Type::RustBox(_) => {
452 write_type(out, ret);
453 write!(out, "::from_raw(");
454 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800455 Type::UniquePtr(_) => {
456 write_type(out, ret);
457 write!(out, "(");
458 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800459 Type::Ref(_) => write!(out, "*"),
460 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800461 }
David Tolnay7db73692019-10-20 14:51:12 -0400462 }
David Tolnay1e548172020-03-16 13:37:09 -0700463 if efn.throws {
464 write!(out, "::rust::Str::Repr error$ = ");
465 }
David Tolnay7db73692019-10-20 14:51:12 -0400466 for name in out.namespace.clone() {
467 write!(out, "{}$", name);
468 }
David Tolnay8c730492020-03-13 01:29:06 -0700469 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400470 for (i, arg) in efn.args.iter().enumerate() {
471 if i > 0 {
472 write!(out, ", ");
473 }
David Tolnaybaae4432020-03-01 20:20:10 -0800474 match &arg.ty {
475 Type::Str(_) => write!(out, "::rust::Str::Repr("),
476 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
477 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400478 }
479 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800480 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800481 Type::RustBox(_) => write!(out, ".into_raw()"),
482 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800483 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800484 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800485 _ => {}
486 }
David Tolnay7db73692019-10-20 14:51:12 -0400487 }
488 if indirect_return {
489 if !efn.args.is_empty() {
490 write!(out, ", ");
491 }
David Tolnay09011c32020-03-06 14:40:28 -0800492 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400493 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800494 write!(out, ")");
495 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800496 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800497 write!(out, ")");
498 }
499 }
500 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700501 if efn.throws {
502 writeln!(out, " if (error$.ptr) {{");
503 writeln!(out, " throw ::rust::Error(error$);");
504 writeln!(out, " }}");
505 }
David Tolnay7db73692019-10-20 14:51:12 -0400506 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700507 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800508 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400509 }
510 writeln!(out, "}}");
511 }
512}
513
514fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
515 match ty {
516 None => write!(out, "void "),
517 Some(ty) => write_type_space(out, ty),
518 }
519}
520
David Tolnay277e3cc2020-03-17 00:11:01 -0700521fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
522 efn.ret
523 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700524 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700525}
526
David Tolnay7db73692019-10-20 14:51:12 -0400527fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
528 match ty {
529 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
530 write_type_space(out, &ty.inner);
531 write!(out, "*");
532 }
David Tolnay4a441222020-01-25 16:24:27 -0800533 Some(Type::Ref(ty)) => {
534 if ty.mutability.is_none() {
535 write!(out, "const ");
536 }
537 write_type(out, &ty.inner);
538 write!(out, " *");
539 }
David Tolnay750755e2020-03-01 13:04:08 -0800540 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400541 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
542 _ => write_return_type(out, ty),
543 }
544}
545
546fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
547 match &arg.ty {
548 Type::RustBox(ty) | Type::UniquePtr(ty) => {
549 write_type_space(out, &ty.inner);
550 write!(out, "*");
551 }
David Tolnay750755e2020-03-01 13:04:08 -0800552 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400553 _ => write_type_space(out, &arg.ty),
554 }
555 if types.needs_indirect_abi(&arg.ty) {
556 write!(out, "*");
557 }
558 write!(out, "{}", arg.ident);
559}
560
561fn write_type(out: &mut OutFile, ty: &Type) {
562 match ty {
563 Type::Ident(ident) => match Atom::from(ident) {
564 Some(Bool) => write!(out, "bool"),
565 Some(U8) => write!(out, "uint8_t"),
566 Some(U16) => write!(out, "uint16_t"),
567 Some(U32) => write!(out, "uint32_t"),
568 Some(U64) => write!(out, "uint64_t"),
569 Some(Usize) => write!(out, "size_t"),
570 Some(I8) => write!(out, "int8_t"),
571 Some(I16) => write!(out, "int16_t"),
572 Some(I32) => write!(out, "int32_t"),
573 Some(I64) => write!(out, "int64_t"),
574 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700575 Some(F32) => write!(out, "float"),
576 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800577 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800578 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400579 None => write!(out, "{}", ident),
580 },
581 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800582 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400583 write_type(out, &ty.inner);
584 write!(out, ">");
585 }
586 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800587 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400588 write_type(out, &ptr.inner);
589 write!(out, ">");
590 }
591 Type::Ref(r) => {
592 if r.mutability.is_none() {
593 write!(out, "const ");
594 }
595 write_type(out, &r.inner);
596 write!(out, " &");
597 }
598 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800599 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400600 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700601 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400602 }
603}
604
605fn write_type_space(out: &mut OutFile, ty: &Type) {
606 write_type(out, ty);
607 match ty {
608 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
609 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700610 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400611 }
612}
613
614fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
615 fn allow_unique_ptr(ident: &Ident) -> bool {
616 Atom::from(ident).is_none()
617 }
618
619 out.begin_block("extern \"C\"");
620 for ty in types {
621 if let Type::RustBox(ty) = ty {
622 if let Type::Ident(inner) = &ty.inner {
623 out.next_section();
624 write_rust_box_extern(out, inner);
625 }
626 } else if let Type::UniquePtr(ptr) = ty {
627 if let Type::Ident(inner) = &ptr.inner {
628 if allow_unique_ptr(inner) {
629 out.next_section();
630 write_unique_ptr(out, inner);
631 }
632 }
633 }
634 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800635 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400636
David Tolnay750755e2020-03-01 13:04:08 -0800637 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700638 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400639 for ty in types {
640 if let Type::RustBox(ty) = ty {
641 if let Type::Ident(inner) = &ty.inner {
642 write_rust_box_impl(out, inner);
643 }
644 }
645 }
David Tolnay8c730492020-03-13 01:29:06 -0700646 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800647 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400648}
649
650fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
651 let mut inner = String::new();
652 for name in &out.namespace {
653 inner += name;
654 inner += "::";
655 }
656 inner += &ident.to_string();
657 let instance = inner.replace("::", "$");
658
David Tolnay8c730492020-03-13 01:29:06 -0700659 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
660 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400661 writeln!(
662 out,
David Tolnay8c730492020-03-13 01:29:06 -0700663 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400664 instance, inner,
665 );
666 writeln!(
667 out,
David Tolnay8c730492020-03-13 01:29:06 -0700668 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400669 instance, inner,
670 );
David Tolnay8c730492020-03-13 01:29:06 -0700671 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400672}
673
674fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
675 let mut inner = String::new();
676 for name in &out.namespace {
677 inner += name;
678 inner += "::";
679 }
680 inner += &ident.to_string();
681 let instance = inner.replace("::", "$");
682
683 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800684 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700685 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400686 writeln!(out, "}}");
687
688 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800689 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700690 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400691 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400692}
693
694fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700695 out.include.utility = true;
696
David Tolnay7db73692019-10-20 14:51:12 -0400697 let mut inner = String::new();
698 for name in &out.namespace {
699 inner += name;
700 inner += "::";
701 }
702 inner += &ident.to_string();
703 let instance = inner.replace("::", "$");
704
David Tolnay8c730492020-03-13 01:29:06 -0700705 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
706 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400707 writeln!(
708 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800709 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400710 inner,
711 );
712 writeln!(
713 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800714 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400715 inner,
716 );
717 writeln!(
718 out,
David Tolnay8c730492020-03-13 01:29:06 -0700719 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400720 instance, inner,
721 );
David Tolnay7e219b82020-03-01 13:14:51 -0800722 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400723 writeln!(out, "}}");
724 writeln!(
725 out,
David Tolnay8c730492020-03-13 01:29:06 -0700726 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400727 instance, inner, inner,
728 );
729 writeln!(
730 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800731 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400732 inner, inner,
733 );
734 writeln!(out, "}}");
735 writeln!(
736 out,
David Tolnay8c730492020-03-13 01:29:06 -0700737 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400738 instance, inner, inner,
739 );
David Tolnay7e219b82020-03-01 13:14:51 -0800740 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400741 writeln!(out, "}}");
742 writeln!(
743 out,
David Tolnay8c730492020-03-13 01:29:06 -0700744 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400745 inner, instance, inner,
746 );
747 writeln!(out, " return ptr.get();");
748 writeln!(out, "}}");
749 writeln!(
750 out,
David Tolnay8c730492020-03-13 01:29:06 -0700751 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400752 inner, instance, inner,
753 );
754 writeln!(out, " return ptr.release();");
755 writeln!(out, "}}");
756 writeln!(
757 out,
David Tolnay8c730492020-03-13 01:29:06 -0700758 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400759 instance, inner,
760 );
761 writeln!(out, " ptr->~unique_ptr();");
762 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700763 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400764}