blob: de2d0cdd905796f2d6a3a27cc346c2ef36af36d0 [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 Tolnay3e3e0af2020-03-17 22:42:49 -0700213 writeln!(out, "struct trycatch {{");
214 writeln!(out, " template <typename T> trycatch(T);");
215 writeln!(out, " static char use_default;");
216 writeln!(out, "}};");
217 writeln!(out);
218 writeln!(out, "template <typename Try, typename Fail,");
219 writeln!(
220 out,
221 " typename = decltype(trycatch(std::declval<Try>()).use_default)>",
222 );
David Tolnay5d121442020-03-17 22:14:40 -0700223 writeln!(
224 out,
225 "static void trycatch(Try &&func, Fail &&fail) noexcept try {{",
226 );
227 writeln!(out, " func();");
228 writeln!(out, "}} catch (const ::std::exception &e) {{");
229 writeln!(out, " fail(e.what());");
230 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700231 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700232 }
233
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800234 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400235}
236
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
238 if needed {
239 out.next_section();
240 for line in include::get(section).lines() {
241 if !line.trim_start().starts_with("//") {
242 writeln!(out, "{}", line);
243 }
244 }
245 }
246}
247
David Tolnay7db73692019-10-20 14:51:12 -0400248fn write_struct(out: &mut OutFile, strct: &Struct) {
249 for line in strct.doc.to_string().lines() {
250 writeln!(out, "//{}", line);
251 }
252 writeln!(out, "struct {} final {{", strct.ident);
253 for field in &strct.fields {
254 write!(out, " ");
255 write_type_space(out, &field.ty);
256 writeln!(out, "{};", field.ident);
257 }
258 writeln!(out, "}};");
259}
260
261fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
262 writeln!(out, "struct {};", ident);
263}
264
David Tolnay8861bee2020-01-20 18:39:24 -0800265fn write_struct_using(out: &mut OutFile, ident: &Ident) {
266 writeln!(out, "using {} = {};", ident, ident);
267}
268
David Tolnayebef4a22020-03-17 15:33:47 -0700269fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
270 let mut has_cxx_throws = false;
271 for api in apis {
272 if let Api::CxxFunction(efn) = api {
273 if efn.throws {
274 has_cxx_throws = true;
275 break;
276 }
277 }
278 }
279
280 if has_cxx_throws {
281 out.next_section();
282 write!(
283 out,
284 "const char *cxxbridge02$exception(const char *, size_t);",
285 );
286 }
287}
288
David Tolnay7db73692019-10-20 14:51:12 -0400289fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700290 if efn.throws {
291 write!(out, "::rust::Str::Repr ");
292 } else {
293 write_extern_return_type(out, &efn.ret, types);
294 }
David Tolnay7db73692019-10-20 14:51:12 -0400295 for name in out.namespace.clone() {
296 write!(out, "{}$", name);
297 }
David Tolnay8c730492020-03-13 01:29:06 -0700298 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400299 for (i, arg) in efn.args.iter().enumerate() {
300 if i > 0 {
301 write!(out, ", ");
302 }
David Tolnaya46a2372020-03-06 10:03:48 -0800303 if arg.ty == RustString {
304 write!(out, "const ");
305 }
David Tolnay7db73692019-10-20 14:51:12 -0400306 write_extern_arg(out, arg, types);
307 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700308 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400309 if indirect_return {
310 if !efn.args.is_empty() {
311 write!(out, ", ");
312 }
313 write_return_type(out, &efn.ret);
314 write!(out, "*return$");
315 }
316 writeln!(out, ") noexcept {{");
317 write!(out, " ");
318 write_return_type(out, &efn.ret);
319 write!(out, "(*{}$)(", efn.ident);
320 for (i, arg) in efn.args.iter().enumerate() {
321 if i > 0 {
322 write!(out, ", ");
323 }
324 write_type(out, &arg.ty);
325 }
326 writeln!(out, ") = {};", efn.ident);
327 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700328 if efn.throws {
329 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700330 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700331 writeln!(out, " [&] {{");
332 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700333 }
David Tolnay7db73692019-10-20 14:51:12 -0400334 if indirect_return {
335 write!(out, "new (return$) ");
336 write_type(out, efn.ret.as_ref().unwrap());
337 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800338 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400339 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800340 match ret {
341 Type::Ref(_) => write!(out, "&"),
342 Type::Str(_) => write!(out, "::rust::Str::Repr("),
343 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800344 }
David Tolnay7db73692019-10-20 14:51:12 -0400345 }
346 write!(out, "{}$(", efn.ident);
347 for (i, arg) in efn.args.iter().enumerate() {
348 if i > 0 {
349 write!(out, ", ");
350 }
351 if let Type::RustBox(_) = &arg.ty {
352 write_type(out, &arg.ty);
353 write!(out, "::from_raw({})", arg.ident);
354 } else if let Type::UniquePtr(_) = &arg.ty {
355 write_type(out, &arg.ty);
356 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800357 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800358 write!(
359 out,
360 "::rust::String(::rust::unsafe_bitcopy, *{})",
361 arg.ident,
362 );
David Tolnay7db73692019-10-20 14:51:12 -0400363 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700364 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800365 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400366 } else {
367 write!(out, "{}", arg.ident);
368 }
369 }
370 write!(out, ")");
371 match &efn.ret {
372 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
373 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800374 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400375 _ => {}
376 }
377 if indirect_return {
378 write!(out, ")");
379 }
380 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700381 if efn.throws {
382 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700383 writeln!(out, " throw$.ptr = nullptr;");
384 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700385 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700386 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700387 writeln!(
388 out,
David Tolnay5d121442020-03-17 22:14:40 -0700389 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700390 );
David Tolnay5d121442020-03-17 22:14:40 -0700391 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700392 writeln!(out, " return throw$;");
393 }
David Tolnay7db73692019-10-20 14:51:12 -0400394 writeln!(out, "}}");
395}
396
397fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700398 if efn.throws {
399 write!(out, "::rust::Str::Repr ");
400 } else {
401 write_extern_return_type(out, &efn.ret, types);
402 }
David Tolnay7db73692019-10-20 14:51:12 -0400403 for name in out.namespace.clone() {
404 write!(out, "{}$", name);
405 }
David Tolnay8c730492020-03-13 01:29:06 -0700406 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400407 for (i, arg) in efn.args.iter().enumerate() {
408 if i > 0 {
409 write!(out, ", ");
410 }
411 write_extern_arg(out, arg, types);
412 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700413 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400414 if !efn.args.is_empty() {
415 write!(out, ", ");
416 }
417 write_return_type(out, &efn.ret);
418 write!(out, "*return$");
419 }
420 writeln!(out, ") noexcept;");
421}
422
423fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400424 for line in efn.doc.to_string().lines() {
425 writeln!(out, "//{}", line);
426 }
427 write_return_type(out, &efn.ret);
428 write!(out, "{}(", efn.ident);
429 for (i, arg) in efn.args.iter().enumerate() {
430 if i > 0 {
431 write!(out, ", ");
432 }
433 write_type_space(out, &arg.ty);
434 write!(out, "{}", arg.ident);
435 }
David Tolnay1e548172020-03-16 13:37:09 -0700436 write!(out, ")");
437 if !efn.throws {
438 write!(out, " noexcept");
439 }
David Tolnay7db73692019-10-20 14:51:12 -0400440 if out.header {
441 writeln!(out, ";");
442 } else {
443 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800444 for arg in &efn.args {
445 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700446 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800447 write!(out, " ::rust::ManuallyDrop<");
448 write_type(out, &arg.ty);
449 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
450 }
451 }
David Tolnay7db73692019-10-20 14:51:12 -0400452 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700453 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400454 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800455 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400456 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800457 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400458 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800459 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400460 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800461 match ret {
462 Type::RustBox(_) => {
463 write_type(out, ret);
464 write!(out, "::from_raw(");
465 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800466 Type::UniquePtr(_) => {
467 write_type(out, ret);
468 write!(out, "(");
469 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800470 Type::Ref(_) => write!(out, "*"),
471 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800472 }
David Tolnay7db73692019-10-20 14:51:12 -0400473 }
David Tolnay1e548172020-03-16 13:37:09 -0700474 if efn.throws {
475 write!(out, "::rust::Str::Repr error$ = ");
476 }
David Tolnay7db73692019-10-20 14:51:12 -0400477 for name in out.namespace.clone() {
478 write!(out, "{}$", name);
479 }
David Tolnay8c730492020-03-13 01:29:06 -0700480 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400481 for (i, arg) in efn.args.iter().enumerate() {
482 if i > 0 {
483 write!(out, ", ");
484 }
David Tolnaybaae4432020-03-01 20:20:10 -0800485 match &arg.ty {
486 Type::Str(_) => write!(out, "::rust::Str::Repr("),
487 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
488 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400489 }
490 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800491 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800492 Type::RustBox(_) => write!(out, ".into_raw()"),
493 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800494 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800495 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800496 _ => {}
497 }
David Tolnay7db73692019-10-20 14:51:12 -0400498 }
499 if indirect_return {
500 if !efn.args.is_empty() {
501 write!(out, ", ");
502 }
David Tolnay09011c32020-03-06 14:40:28 -0800503 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400504 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800505 write!(out, ")");
506 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800507 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800508 write!(out, ")");
509 }
510 }
511 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700512 if efn.throws {
513 writeln!(out, " if (error$.ptr) {{");
514 writeln!(out, " throw ::rust::Error(error$);");
515 writeln!(out, " }}");
516 }
David Tolnay7db73692019-10-20 14:51:12 -0400517 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700518 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800519 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400520 }
521 writeln!(out, "}}");
522 }
523}
524
525fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
526 match ty {
527 None => write!(out, "void "),
528 Some(ty) => write_type_space(out, ty),
529 }
530}
531
David Tolnay277e3cc2020-03-17 00:11:01 -0700532fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
533 efn.ret
534 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700535 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700536}
537
David Tolnay7db73692019-10-20 14:51:12 -0400538fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
539 match ty {
540 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
541 write_type_space(out, &ty.inner);
542 write!(out, "*");
543 }
David Tolnay4a441222020-01-25 16:24:27 -0800544 Some(Type::Ref(ty)) => {
545 if ty.mutability.is_none() {
546 write!(out, "const ");
547 }
548 write_type(out, &ty.inner);
549 write!(out, " *");
550 }
David Tolnay750755e2020-03-01 13:04:08 -0800551 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400552 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
553 _ => write_return_type(out, ty),
554 }
555}
556
557fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
558 match &arg.ty {
559 Type::RustBox(ty) | Type::UniquePtr(ty) => {
560 write_type_space(out, &ty.inner);
561 write!(out, "*");
562 }
David Tolnay750755e2020-03-01 13:04:08 -0800563 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400564 _ => write_type_space(out, &arg.ty),
565 }
566 if types.needs_indirect_abi(&arg.ty) {
567 write!(out, "*");
568 }
569 write!(out, "{}", arg.ident);
570}
571
572fn write_type(out: &mut OutFile, ty: &Type) {
573 match ty {
574 Type::Ident(ident) => match Atom::from(ident) {
575 Some(Bool) => write!(out, "bool"),
576 Some(U8) => write!(out, "uint8_t"),
577 Some(U16) => write!(out, "uint16_t"),
578 Some(U32) => write!(out, "uint32_t"),
579 Some(U64) => write!(out, "uint64_t"),
580 Some(Usize) => write!(out, "size_t"),
581 Some(I8) => write!(out, "int8_t"),
582 Some(I16) => write!(out, "int16_t"),
583 Some(I32) => write!(out, "int32_t"),
584 Some(I64) => write!(out, "int64_t"),
585 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700586 Some(F32) => write!(out, "float"),
587 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800588 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800589 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400590 None => write!(out, "{}", ident),
591 },
592 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800593 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400594 write_type(out, &ty.inner);
595 write!(out, ">");
596 }
597 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800598 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400599 write_type(out, &ptr.inner);
600 write!(out, ">");
601 }
602 Type::Ref(r) => {
603 if r.mutability.is_none() {
604 write!(out, "const ");
605 }
606 write_type(out, &r.inner);
607 write!(out, " &");
608 }
609 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800610 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400611 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700612 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400613 }
614}
615
616fn write_type_space(out: &mut OutFile, ty: &Type) {
617 write_type(out, ty);
618 match ty {
619 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
620 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700621 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400622 }
623}
624
625fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
626 fn allow_unique_ptr(ident: &Ident) -> bool {
627 Atom::from(ident).is_none()
628 }
629
630 out.begin_block("extern \"C\"");
631 for ty in types {
632 if let Type::RustBox(ty) = ty {
633 if let Type::Ident(inner) = &ty.inner {
634 out.next_section();
635 write_rust_box_extern(out, inner);
636 }
637 } else if let Type::UniquePtr(ptr) = ty {
638 if let Type::Ident(inner) = &ptr.inner {
639 if allow_unique_ptr(inner) {
640 out.next_section();
641 write_unique_ptr(out, inner);
642 }
643 }
644 }
645 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800646 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400647
David Tolnay750755e2020-03-01 13:04:08 -0800648 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700649 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400650 for ty in types {
651 if let Type::RustBox(ty) = ty {
652 if let Type::Ident(inner) = &ty.inner {
653 write_rust_box_impl(out, inner);
654 }
655 }
656 }
David Tolnay8c730492020-03-13 01:29:06 -0700657 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800658 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400659}
660
661fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
662 let mut inner = String::new();
663 for name in &out.namespace {
664 inner += name;
665 inner += "::";
666 }
667 inner += &ident.to_string();
668 let instance = inner.replace("::", "$");
669
David Tolnay8c730492020-03-13 01:29:06 -0700670 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
671 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400672 writeln!(
673 out,
David Tolnay8c730492020-03-13 01:29:06 -0700674 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400675 instance, inner,
676 );
677 writeln!(
678 out,
David Tolnay8c730492020-03-13 01:29:06 -0700679 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400680 instance, inner,
681 );
David Tolnay8c730492020-03-13 01:29:06 -0700682 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400683}
684
685fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
686 let mut inner = String::new();
687 for name in &out.namespace {
688 inner += name;
689 inner += "::";
690 }
691 inner += &ident.to_string();
692 let instance = inner.replace("::", "$");
693
694 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800695 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700696 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400697 writeln!(out, "}}");
698
699 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800700 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700701 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400702 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400703}
704
705fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700706 out.include.utility = true;
707
David Tolnay7db73692019-10-20 14:51:12 -0400708 let mut inner = String::new();
709 for name in &out.namespace {
710 inner += name;
711 inner += "::";
712 }
713 inner += &ident.to_string();
714 let instance = inner.replace("::", "$");
715
David Tolnay8c730492020-03-13 01:29:06 -0700716 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
717 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400718 writeln!(
719 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800720 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400721 inner,
722 );
723 writeln!(
724 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800725 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400726 inner,
727 );
728 writeln!(
729 out,
David Tolnay8c730492020-03-13 01:29:06 -0700730 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400731 instance, inner,
732 );
David Tolnay7e219b82020-03-01 13:14:51 -0800733 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400734 writeln!(out, "}}");
735 writeln!(
736 out,
David Tolnay8c730492020-03-13 01:29:06 -0700737 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400738 instance, inner, inner,
739 );
740 writeln!(
741 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800742 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400743 inner, inner,
744 );
745 writeln!(out, "}}");
746 writeln!(
747 out,
David Tolnay8c730492020-03-13 01:29:06 -0700748 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400749 instance, inner, inner,
750 );
David Tolnay7e219b82020-03-01 13:14:51 -0800751 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400752 writeln!(out, "}}");
753 writeln!(
754 out,
David Tolnay8c730492020-03-13 01:29:06 -0700755 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400756 inner, instance, inner,
757 );
758 writeln!(out, " return ptr.get();");
759 writeln!(out, "}}");
760 writeln!(
761 out,
David Tolnay8c730492020-03-13 01:29:06 -0700762 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400763 inner, instance, inner,
764 );
765 writeln!(out, " return ptr.release();");
766 writeln!(out, "}}");
767 writeln!(
768 out,
David Tolnay8c730492020-03-13 01:29:06 -0700769 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400770 instance, inner,
771 );
772 writeln!(out, " ptr->~unique_ptr();");
773 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700774 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400775}