blob: 9a33fc52f74a1a1b50b24ee157f8a4e97b0e7c26 [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 Tolnay3e3e0af2020-03-17 22:42:49 -0700208 out.end_block("namespace cxxbridge02");
209
David Tolnay5d121442020-03-17 22:14:40 -0700210 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700211 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700212 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700213 out.include.type_traits = true;
214 out.include.utility = true;
215 writeln!(out, "class missing {{}};");
216 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700217 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700218 writeln!(out, "template <typename Try, typename Fail>");
219 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700220 writeln!(
221 out,
David Tolnay04722332020-03-18 11:31:54 -0700222 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700223 );
David Tolnay04722332020-03-18 11:31:54 -0700224 writeln!(out, " missing>::value>::type");
225 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700226 writeln!(out, " func();");
227 writeln!(out, "}} catch (const ::std::exception &e) {{");
228 writeln!(out, " fail(e.what());");
229 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700230 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700231 }
232
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800233 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400234}
235
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
237 if needed {
238 out.next_section();
239 for line in include::get(section).lines() {
240 if !line.trim_start().starts_with("//") {
241 writeln!(out, "{}", line);
242 }
243 }
244 }
245}
246
David Tolnay7db73692019-10-20 14:51:12 -0400247fn write_struct(out: &mut OutFile, strct: &Struct) {
248 for line in strct.doc.to_string().lines() {
249 writeln!(out, "//{}", line);
250 }
251 writeln!(out, "struct {} final {{", strct.ident);
252 for field in &strct.fields {
253 write!(out, " ");
254 write_type_space(out, &field.ty);
255 writeln!(out, "{};", field.ident);
256 }
257 writeln!(out, "}};");
258}
259
260fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
261 writeln!(out, "struct {};", ident);
262}
263
David Tolnay8861bee2020-01-20 18:39:24 -0800264fn write_struct_using(out: &mut OutFile, ident: &Ident) {
265 writeln!(out, "using {} = {};", ident, ident);
266}
267
David Tolnayebef4a22020-03-17 15:33:47 -0700268fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
269 let mut has_cxx_throws = false;
270 for api in apis {
271 if let Api::CxxFunction(efn) = api {
272 if efn.throws {
273 has_cxx_throws = true;
274 break;
275 }
276 }
277 }
278
279 if has_cxx_throws {
280 out.next_section();
281 write!(
282 out,
283 "const char *cxxbridge02$exception(const char *, size_t);",
284 );
285 }
286}
287
David Tolnay7db73692019-10-20 14:51:12 -0400288fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700289 if efn.throws {
290 write!(out, "::rust::Str::Repr ");
291 } else {
292 write_extern_return_type(out, &efn.ret, types);
293 }
David Tolnay7db73692019-10-20 14:51:12 -0400294 for name in out.namespace.clone() {
295 write!(out, "{}$", name);
296 }
David Tolnay8c730492020-03-13 01:29:06 -0700297 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400298 for (i, arg) in efn.args.iter().enumerate() {
299 if i > 0 {
300 write!(out, ", ");
301 }
David Tolnaya46a2372020-03-06 10:03:48 -0800302 if arg.ty == RustString {
303 write!(out, "const ");
304 }
David Tolnay7db73692019-10-20 14:51:12 -0400305 write_extern_arg(out, arg, types);
306 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700307 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400308 if indirect_return {
309 if !efn.args.is_empty() {
310 write!(out, ", ");
311 }
312 write_return_type(out, &efn.ret);
313 write!(out, "*return$");
314 }
315 writeln!(out, ") noexcept {{");
316 write!(out, " ");
317 write_return_type(out, &efn.ret);
318 write!(out, "(*{}$)(", efn.ident);
319 for (i, arg) in efn.args.iter().enumerate() {
320 if i > 0 {
321 write!(out, ", ");
322 }
323 write_type(out, &arg.ty);
324 }
325 writeln!(out, ") = {};", efn.ident);
326 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700327 if efn.throws {
328 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700329 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700330 writeln!(out, " [&] {{");
331 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700332 }
David Tolnay7db73692019-10-20 14:51:12 -0400333 if indirect_return {
334 write!(out, "new (return$) ");
335 write_type(out, efn.ret.as_ref().unwrap());
336 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800337 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400338 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800339 match ret {
340 Type::Ref(_) => write!(out, "&"),
341 Type::Str(_) => write!(out, "::rust::Str::Repr("),
342 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800343 }
David Tolnay7db73692019-10-20 14:51:12 -0400344 }
345 write!(out, "{}$(", efn.ident);
346 for (i, arg) in efn.args.iter().enumerate() {
347 if i > 0 {
348 write!(out, ", ");
349 }
350 if let Type::RustBox(_) = &arg.ty {
351 write_type(out, &arg.ty);
352 write!(out, "::from_raw({})", arg.ident);
353 } else if let Type::UniquePtr(_) = &arg.ty {
354 write_type(out, &arg.ty);
355 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800356 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800357 write!(
358 out,
359 "::rust::String(::rust::unsafe_bitcopy, *{})",
360 arg.ident,
361 );
David Tolnay7db73692019-10-20 14:51:12 -0400362 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700363 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800364 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400365 } else {
366 write!(out, "{}", arg.ident);
367 }
368 }
369 write!(out, ")");
370 match &efn.ret {
371 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
372 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800373 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400374 _ => {}
375 }
376 if indirect_return {
377 write!(out, ")");
378 }
379 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700380 if efn.throws {
381 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700382 writeln!(out, " throw$.ptr = nullptr;");
383 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700384 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700385 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700386 writeln!(
387 out,
David Tolnay5d121442020-03-17 22:14:40 -0700388 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700389 );
David Tolnay5d121442020-03-17 22:14:40 -0700390 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700391 writeln!(out, " return throw$;");
392 }
David Tolnay7db73692019-10-20 14:51:12 -0400393 writeln!(out, "}}");
394}
395
396fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700397 if efn.throws {
398 write!(out, "::rust::Str::Repr ");
399 } else {
400 write_extern_return_type(out, &efn.ret, types);
401 }
David Tolnay7db73692019-10-20 14:51:12 -0400402 for name in out.namespace.clone() {
403 write!(out, "{}$", name);
404 }
David Tolnay8c730492020-03-13 01:29:06 -0700405 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400406 for (i, arg) in efn.args.iter().enumerate() {
407 if i > 0 {
408 write!(out, ", ");
409 }
410 write_extern_arg(out, arg, types);
411 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700412 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400413 if !efn.args.is_empty() {
414 write!(out, ", ");
415 }
416 write_return_type(out, &efn.ret);
417 write!(out, "*return$");
418 }
419 writeln!(out, ") noexcept;");
420}
421
422fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400423 for line in efn.doc.to_string().lines() {
424 writeln!(out, "//{}", line);
425 }
426 write_return_type(out, &efn.ret);
427 write!(out, "{}(", efn.ident);
428 for (i, arg) in efn.args.iter().enumerate() {
429 if i > 0 {
430 write!(out, ", ");
431 }
432 write_type_space(out, &arg.ty);
433 write!(out, "{}", arg.ident);
434 }
David Tolnay1e548172020-03-16 13:37:09 -0700435 write!(out, ")");
436 if !efn.throws {
437 write!(out, " noexcept");
438 }
David Tolnay7db73692019-10-20 14:51:12 -0400439 if out.header {
440 writeln!(out, ";");
441 } else {
442 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800443 for arg in &efn.args {
444 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700445 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800446 write!(out, " ::rust::ManuallyDrop<");
447 write_type(out, &arg.ty);
448 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
449 }
450 }
David Tolnay7db73692019-10-20 14:51:12 -0400451 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700452 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400453 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800454 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400455 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800456 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400457 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800458 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400459 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800460 match ret {
461 Type::RustBox(_) => {
462 write_type(out, ret);
463 write!(out, "::from_raw(");
464 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800465 Type::UniquePtr(_) => {
466 write_type(out, ret);
467 write!(out, "(");
468 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800469 Type::Ref(_) => write!(out, "*"),
470 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800471 }
David Tolnay7db73692019-10-20 14:51:12 -0400472 }
David Tolnay1e548172020-03-16 13:37:09 -0700473 if efn.throws {
474 write!(out, "::rust::Str::Repr error$ = ");
475 }
David Tolnay7db73692019-10-20 14:51:12 -0400476 for name in out.namespace.clone() {
477 write!(out, "{}$", name);
478 }
David Tolnay8c730492020-03-13 01:29:06 -0700479 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400480 for (i, arg) in efn.args.iter().enumerate() {
481 if i > 0 {
482 write!(out, ", ");
483 }
David Tolnaybaae4432020-03-01 20:20:10 -0800484 match &arg.ty {
485 Type::Str(_) => write!(out, "::rust::Str::Repr("),
486 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
487 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400488 }
489 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800490 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800491 Type::RustBox(_) => write!(out, ".into_raw()"),
492 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800493 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800494 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800495 _ => {}
496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 }
498 if indirect_return {
499 if !efn.args.is_empty() {
500 write!(out, ", ");
501 }
David Tolnay09011c32020-03-06 14:40:28 -0800502 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400503 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800504 write!(out, ")");
505 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800506 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800507 write!(out, ")");
508 }
509 }
510 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700511 if efn.throws {
512 writeln!(out, " if (error$.ptr) {{");
513 writeln!(out, " throw ::rust::Error(error$);");
514 writeln!(out, " }}");
515 }
David Tolnay7db73692019-10-20 14:51:12 -0400516 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700517 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800518 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400519 }
520 writeln!(out, "}}");
521 }
522}
523
524fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
525 match ty {
526 None => write!(out, "void "),
527 Some(ty) => write_type_space(out, ty),
528 }
529}
530
David Tolnay277e3cc2020-03-17 00:11:01 -0700531fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
532 efn.ret
533 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700534 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700535}
536
David Tolnay7db73692019-10-20 14:51:12 -0400537fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
538 match ty {
539 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
540 write_type_space(out, &ty.inner);
541 write!(out, "*");
542 }
David Tolnay4a441222020-01-25 16:24:27 -0800543 Some(Type::Ref(ty)) => {
544 if ty.mutability.is_none() {
545 write!(out, "const ");
546 }
547 write_type(out, &ty.inner);
548 write!(out, " *");
549 }
David Tolnay750755e2020-03-01 13:04:08 -0800550 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400551 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
552 _ => write_return_type(out, ty),
553 }
554}
555
556fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
557 match &arg.ty {
558 Type::RustBox(ty) | Type::UniquePtr(ty) => {
559 write_type_space(out, &ty.inner);
560 write!(out, "*");
561 }
David Tolnay750755e2020-03-01 13:04:08 -0800562 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400563 _ => write_type_space(out, &arg.ty),
564 }
565 if types.needs_indirect_abi(&arg.ty) {
566 write!(out, "*");
567 }
568 write!(out, "{}", arg.ident);
569}
570
571fn write_type(out: &mut OutFile, ty: &Type) {
572 match ty {
573 Type::Ident(ident) => match Atom::from(ident) {
574 Some(Bool) => write!(out, "bool"),
575 Some(U8) => write!(out, "uint8_t"),
576 Some(U16) => write!(out, "uint16_t"),
577 Some(U32) => write!(out, "uint32_t"),
578 Some(U64) => write!(out, "uint64_t"),
579 Some(Usize) => write!(out, "size_t"),
580 Some(I8) => write!(out, "int8_t"),
581 Some(I16) => write!(out, "int16_t"),
582 Some(I32) => write!(out, "int32_t"),
583 Some(I64) => write!(out, "int64_t"),
584 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700585 Some(F32) => write!(out, "float"),
586 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800587 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800588 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400589 None => write!(out, "{}", ident),
590 },
591 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800592 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400593 write_type(out, &ty.inner);
594 write!(out, ">");
595 }
596 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800597 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400598 write_type(out, &ptr.inner);
599 write!(out, ">");
600 }
601 Type::Ref(r) => {
602 if r.mutability.is_none() {
603 write!(out, "const ");
604 }
605 write_type(out, &r.inner);
606 write!(out, " &");
607 }
608 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800609 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400610 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700611 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400612 }
613}
614
615fn write_type_space(out: &mut OutFile, ty: &Type) {
616 write_type(out, ty);
617 match ty {
618 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
619 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700620 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400621 }
622}
623
624fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
625 fn allow_unique_ptr(ident: &Ident) -> bool {
626 Atom::from(ident).is_none()
627 }
628
629 out.begin_block("extern \"C\"");
630 for ty in types {
631 if let Type::RustBox(ty) = ty {
632 if let Type::Ident(inner) = &ty.inner {
633 out.next_section();
634 write_rust_box_extern(out, inner);
635 }
636 } else if let Type::UniquePtr(ptr) = ty {
637 if let Type::Ident(inner) = &ptr.inner {
638 if allow_unique_ptr(inner) {
639 out.next_section();
640 write_unique_ptr(out, inner);
641 }
642 }
643 }
644 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800645 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400646
David Tolnay750755e2020-03-01 13:04:08 -0800647 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700648 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400649 for ty in types {
650 if let Type::RustBox(ty) = ty {
651 if let Type::Ident(inner) = &ty.inner {
652 write_rust_box_impl(out, inner);
653 }
654 }
655 }
David Tolnay8c730492020-03-13 01:29:06 -0700656 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800657 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400658}
659
660fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
661 let mut inner = String::new();
662 for name in &out.namespace {
663 inner += name;
664 inner += "::";
665 }
666 inner += &ident.to_string();
667 let instance = inner.replace("::", "$");
668
David Tolnay8c730492020-03-13 01:29:06 -0700669 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
670 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400671 writeln!(
672 out,
David Tolnay8c730492020-03-13 01:29:06 -0700673 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400674 instance, inner,
675 );
676 writeln!(
677 out,
David Tolnay8c730492020-03-13 01:29:06 -0700678 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400679 instance, inner,
680 );
David Tolnay8c730492020-03-13 01:29:06 -0700681 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400682}
683
684fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
685 let mut inner = String::new();
686 for name in &out.namespace {
687 inner += name;
688 inner += "::";
689 }
690 inner += &ident.to_string();
691 let instance = inner.replace("::", "$");
692
693 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800694 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700695 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400696 writeln!(out, "}}");
697
698 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800699 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700700 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400701 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400702}
703
704fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700705 out.include.utility = true;
706
David Tolnay7db73692019-10-20 14:51:12 -0400707 let mut inner = String::new();
708 for name in &out.namespace {
709 inner += name;
710 inner += "::";
711 }
712 inner += &ident.to_string();
713 let instance = inner.replace("::", "$");
714
David Tolnay8c730492020-03-13 01:29:06 -0700715 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
716 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400717 writeln!(
718 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800719 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400720 inner,
721 );
722 writeln!(
723 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800724 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400725 inner,
726 );
727 writeln!(
728 out,
David Tolnay8c730492020-03-13 01:29:06 -0700729 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400730 instance, inner,
731 );
David Tolnay7e219b82020-03-01 13:14:51 -0800732 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400733 writeln!(out, "}}");
734 writeln!(
735 out,
David Tolnay8c730492020-03-13 01:29:06 -0700736 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400737 instance, inner, inner,
738 );
739 writeln!(
740 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800741 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400742 inner, inner,
743 );
744 writeln!(out, "}}");
745 writeln!(
746 out,
David Tolnay8c730492020-03-13 01:29:06 -0700747 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400748 instance, inner, inner,
749 );
David Tolnay7e219b82020-03-01 13:14:51 -0800750 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400751 writeln!(out, "}}");
752 writeln!(
753 out,
David Tolnay8c730492020-03-13 01:29:06 -0700754 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400755 inner, instance, inner,
756 );
757 writeln!(out, " return ptr.get();");
758 writeln!(out, "}}");
759 writeln!(
760 out,
David Tolnay8c730492020-03-13 01:29:06 -0700761 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400762 inner, instance, inner,
763 );
764 writeln!(out, " return ptr.release();");
765 writeln!(out, "}}");
766 writeln!(
767 out,
David Tolnay8c730492020-03-13 01:29:06 -0700768 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400769 instance, inner,
770 );
771 writeln!(out, " ptr->~unique_ptr();");
772 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700773 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400774}