blob: 9d6ef6e450bad29502a206d2327fe8868c03224c [file] [log] [blame]
David Tolnay754e21c2020-03-29 20:58:46 -07001use crate::gen::namespace::Namespace;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay75dca2e2020-03-25 20:17:52 -07005use crate::syntax::{Api, ExternFn, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04006use proc_macro2::Ident;
7
David Tolnay33d30292020-03-18 18:02:02 -07008pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -07009 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070010 apis: &[Api],
11 types: &Types,
12 opt: Opt,
13 header: bool,
14) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040015 let mut out_file = OutFile::new(namespace.clone(), header);
16 let out = &mut out_file;
17
18 if header {
19 writeln!(out, "#pragma once");
20 }
21
David Tolnay33d30292020-03-18 18:02:02 -070022 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023 for api in apis {
24 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080025 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040026 }
27 }
28
29 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080030 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay7db73692019-10-20 14:51:12 -040032 out.next_section();
33 for name in &namespace {
34 writeln!(out, "namespace {} {{", name);
35 }
36
David Tolnay7db73692019-10-20 14:51:12 -040037 out.next_section();
38 for api in apis {
39 match api {
40 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080041 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
42 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040043 _ => {}
44 }
45 }
46
47 for api in apis {
48 if let Api::Struct(strct) = api {
49 out.next_section();
50 write_struct(out, strct);
51 }
52 }
53
54 if !header {
55 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070056 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
58 let (efn, write): (_, fn(_, _, _)) = match api {
59 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
60 Api::RustFunction(efn) => (efn, write_rust_function_decl),
61 _ => continue,
62 };
63 out.next_section();
64 write(out, efn, types);
65 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080066 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68
69 for api in apis {
70 if let Api::RustFunction(efn) = api {
71 out.next_section();
72 write_rust_function_shim(out, efn, types);
73 }
74 }
75
76 out.next_section();
77 for name in namespace.iter().rev() {
78 writeln!(out, "}} // namespace {}", name);
79 }
80
81 if !header {
82 out.next_section();
83 write_generic_instantiations(out, types);
84 }
85
David Tolnay9c68b1a2020-03-06 11:12:55 -080086 out.prepend(out.include.to_string());
87
David Tolnay7db73692019-10-20 14:51:12 -040088 out_file
89}
90
91fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040092 for ty in types {
93 match ty {
94 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -070095 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
96 | Some(I64) => out.include.cstdint = true,
97 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -080098 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -070099 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400100 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800101 Type::RustBox(_) => out.include.type_traits = true,
102 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -0400103 _ => {}
104 }
105 }
David Tolnay7db73692019-10-20 14:51:12 -0400106}
107
David Tolnayf51447e2020-03-06 14:14:27 -0800108fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700109 let mut needs_rust_string = false;
110 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400111 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700112 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700113 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400114 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700115 match ty {
116 Type::RustBox(_) => {
117 out.include.type_traits = true;
118 needs_rust_box = true;
119 }
120 Type::Str(_) => {
121 out.include.cstdint = true;
122 out.include.string = true;
123 needs_rust_str = true;
124 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700125 Type::Fn(_) => {
126 needs_rust_fn = true;
127 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700128 ty if ty == Isize => {
129 needs_rust_isize = true;
130 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700131 ty if ty == RustString => {
132 out.include.array = true;
133 out.include.cstdint = true;
134 out.include.string = true;
135 needs_rust_string = true;
136 }
137 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400138 }
139 }
140
David Tolnayb7a7cb62020-03-17 21:18:40 -0700141 let mut needs_rust_error = false;
142 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800143 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800144 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700145 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800146 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700147 match api {
148 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700149 if efn.throws {
150 needs_trycatch = true;
151 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700152 for arg in &efn.args {
153 if arg.ty == RustString {
154 needs_unsafe_bitcopy = true;
155 break;
156 }
David Tolnay09011c32020-03-06 14:40:28 -0800157 }
158 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700159 Api::RustFunction(efn) if !out.header => {
160 if efn.throws {
161 out.include.exception = true;
162 needs_rust_error = true;
163 }
164 for arg in &efn.args {
165 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
166 needs_manually_drop = true;
167 break;
168 }
169 }
170 if let Some(ret) = &efn.ret {
171 if types.needs_indirect_abi(ret) {
172 needs_maybe_uninit = true;
173 }
David Tolnayf51447e2020-03-06 14:14:27 -0800174 }
175 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700176 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800177 }
178 }
179
David Tolnay750755e2020-03-01 13:04:08 -0800180 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700181 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800182
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 if needs_rust_string
184 || needs_rust_str
185 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700186 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700188 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 || needs_unsafe_bitcopy
190 || needs_manually_drop
191 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700192 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 {
David Tolnay736cbca2020-03-11 16:49:18 -0700194 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800195 }
196
David Tolnayd1402742020-03-25 22:21:42 -0700197 if needs_rust_string {
198 out.next_section();
199 writeln!(out, "struct unsafe_bitcopy_t;");
200 }
201
David Tolnayb7a7cb62020-03-17 21:18:40 -0700202 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
203 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
204 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700205 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700207 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700208 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800209
210 if needs_manually_drop {
211 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700212 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800213 writeln!(out, "template <typename T>");
214 writeln!(out, "union ManuallyDrop {{");
215 writeln!(out, " T value;");
216 writeln!(
217 out,
218 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
219 );
220 writeln!(out, " ~ManuallyDrop() {{}}");
221 writeln!(out, "}};");
222 }
223
David Tolnay09011c32020-03-06 14:40:28 -0800224 if needs_maybe_uninit {
225 out.next_section();
226 writeln!(out, "template <typename T>");
227 writeln!(out, "union MaybeUninit {{");
228 writeln!(out, " T value;");
229 writeln!(out, " MaybeUninit() {{}}");
230 writeln!(out, " ~MaybeUninit() {{}}");
231 writeln!(out, "}};");
232 }
233
David Tolnay3e3e0af2020-03-17 22:42:49 -0700234 out.end_block("namespace cxxbridge02");
235
David Tolnay5d121442020-03-17 22:14:40 -0700236 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700237 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700238 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700239 out.include.type_traits = true;
240 out.include.utility = true;
241 writeln!(out, "class missing {{}};");
242 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700243 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700244 writeln!(out, "template <typename Try, typename Fail>");
245 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700246 writeln!(
247 out,
David Tolnay04722332020-03-18 11:31:54 -0700248 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700249 );
David Tolnay04722332020-03-18 11:31:54 -0700250 writeln!(out, " missing>::value>::type");
251 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700252 writeln!(out, " func();");
253 writeln!(out, "}} catch (const ::std::exception &e) {{");
254 writeln!(out, " fail(e.what());");
255 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700256 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700257 }
258
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800259 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400260}
261
David Tolnayb7a7cb62020-03-17 21:18:40 -0700262fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
263 if needed {
264 out.next_section();
265 for line in include::get(section).lines() {
266 if !line.trim_start().starts_with("//") {
267 writeln!(out, "{}", line);
268 }
269 }
270 }
271}
272
David Tolnay7db73692019-10-20 14:51:12 -0400273fn write_struct(out: &mut OutFile, strct: &Struct) {
274 for line in strct.doc.to_string().lines() {
275 writeln!(out, "//{}", line);
276 }
277 writeln!(out, "struct {} final {{", strct.ident);
278 for field in &strct.fields {
279 write!(out, " ");
280 write_type_space(out, &field.ty);
281 writeln!(out, "{};", field.ident);
282 }
283 writeln!(out, "}};");
284}
285
286fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
287 writeln!(out, "struct {};", ident);
288}
289
David Tolnay8861bee2020-01-20 18:39:24 -0800290fn write_struct_using(out: &mut OutFile, ident: &Ident) {
291 writeln!(out, "using {} = {};", ident, ident);
292}
293
David Tolnayebef4a22020-03-17 15:33:47 -0700294fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
295 let mut has_cxx_throws = false;
296 for api in apis {
297 if let Api::CxxFunction(efn) = api {
298 if efn.throws {
299 has_cxx_throws = true;
300 break;
301 }
302 }
303 }
304
305 if has_cxx_throws {
306 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700307 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700308 out,
309 "const char *cxxbridge02$exception(const char *, size_t);",
310 );
311 }
312}
313
David Tolnay7db73692019-10-20 14:51:12 -0400314fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700315 if efn.throws {
316 write!(out, "::rust::Str::Repr ");
317 } else {
David Tolnay99642622020-03-25 13:07:35 -0700318 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700319 }
David Tolnayd815de02020-03-29 21:29:17 -0700320 write!(out, "{}cxxbridge02${}(", out.namespace, efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400321 for (i, arg) in efn.args.iter().enumerate() {
322 if i > 0 {
323 write!(out, ", ");
324 }
David Tolnaya46a2372020-03-06 10:03:48 -0800325 if arg.ty == RustString {
326 write!(out, "const ");
327 }
David Tolnay7db73692019-10-20 14:51:12 -0400328 write_extern_arg(out, arg, types);
329 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700330 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400331 if indirect_return {
332 if !efn.args.is_empty() {
333 write!(out, ", ");
334 }
David Tolnay99642622020-03-25 13:07:35 -0700335 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400336 write!(out, "*return$");
337 }
338 writeln!(out, ") noexcept {{");
339 write!(out, " ");
340 write_return_type(out, &efn.ret);
341 write!(out, "(*{}$)(", efn.ident);
342 for (i, arg) in efn.args.iter().enumerate() {
343 if i > 0 {
344 write!(out, ", ");
345 }
346 write_type(out, &arg.ty);
347 }
348 writeln!(out, ") = {};", efn.ident);
349 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700350 if efn.throws {
351 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700352 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700353 writeln!(out, " [&] {{");
354 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700355 }
David Tolnay7db73692019-10-20 14:51:12 -0400356 if indirect_return {
357 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700358 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400359 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700360 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400361 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700362 }
363 match &efn.ret {
364 Some(Type::Ref(_)) => write!(out, "&"),
365 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
366 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400367 }
368 write!(out, "{}$(", efn.ident);
369 for (i, arg) in efn.args.iter().enumerate() {
370 if i > 0 {
371 write!(out, ", ");
372 }
373 if let Type::RustBox(_) = &arg.ty {
374 write_type(out, &arg.ty);
375 write!(out, "::from_raw({})", arg.ident);
376 } else if let Type::UniquePtr(_) = &arg.ty {
377 write_type(out, &arg.ty);
378 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800379 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800380 write!(
381 out,
382 "::rust::String(::rust::unsafe_bitcopy, *{})",
383 arg.ident,
384 );
David Tolnay7db73692019-10-20 14:51:12 -0400385 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700386 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800387 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400388 } else {
389 write!(out, "{}", arg.ident);
390 }
391 }
392 write!(out, ")");
393 match &efn.ret {
394 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
395 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay99642622020-03-25 13:07:35 -0700396 Some(Type::Str(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400397 _ => {}
398 }
399 if indirect_return {
400 write!(out, ")");
401 }
402 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700403 if efn.throws {
404 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700405 writeln!(out, " throw$.ptr = nullptr;");
406 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700407 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700408 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700409 writeln!(
410 out,
David Tolnay5d121442020-03-17 22:14:40 -0700411 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700412 );
David Tolnay5d121442020-03-17 22:14:40 -0700413 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700414 writeln!(out, " return throw$;");
415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700417 for arg in &efn.args {
418 if let Type::Fn(f) = &arg.ty {
419 let var = &arg.ident;
420 write_function_pointer_trampoline(out, efn, var, f, types);
421 }
422 }
423}
424
425fn write_function_pointer_trampoline(
426 out: &mut OutFile,
427 efn: &ExternFn,
428 var: &Ident,
429 f: &Signature,
430 types: &Types,
431) {
432 out.next_section();
433 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
434 let indirect_call = true;
435 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
436
437 out.next_section();
438 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
439 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400440}
441
442fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700443 let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident);
444 let indirect_call = false;
445 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
446}
447
448fn write_rust_function_decl_impl(
449 out: &mut OutFile,
450 link_name: &str,
451 sig: &Signature,
452 types: &Types,
453 indirect_call: bool,
454) {
455 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700456 write!(out, "::rust::Str::Repr ");
457 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700458 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700459 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700460 write!(out, "{}(", link_name);
461 let mut needs_comma = false;
462 for arg in &sig.args {
463 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400464 write!(out, ", ");
465 }
466 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700467 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400468 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700469 if indirect_return(sig, types) {
470 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400471 write!(out, ", ");
472 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700473 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400474 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700475 needs_comma = true;
476 }
477 if indirect_call {
478 if needs_comma {
479 write!(out, ", ");
480 }
481 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400482 }
483 writeln!(out, ") noexcept;");
484}
485
486fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400487 for line in efn.doc.to_string().lines() {
488 writeln!(out, "//{}", line);
489 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700490 let local_name = efn.ident.to_string();
491 let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident);
492 let indirect_call = false;
493 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
494}
495
496fn write_rust_function_shim_impl(
497 out: &mut OutFile,
498 local_name: &str,
499 sig: &Signature,
500 types: &Types,
501 invoke: &str,
502 indirect_call: bool,
503) {
504 write_return_type(out, &sig.ret);
505 write!(out, "{}(", local_name);
506 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400507 if i > 0 {
508 write!(out, ", ");
509 }
510 write_type_space(out, &arg.ty);
511 write!(out, "{}", arg.ident);
512 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700513 if indirect_call {
514 if !sig.args.is_empty() {
515 write!(out, ", ");
516 }
517 write!(out, "void *extern$");
518 }
David Tolnay1e548172020-03-16 13:37:09 -0700519 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700520 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700521 write!(out, " noexcept");
522 }
David Tolnay7db73692019-10-20 14:51:12 -0400523 if out.header {
524 writeln!(out, ";");
525 } else {
526 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700527 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800528 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700529 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800530 write!(out, " ::rust::ManuallyDrop<");
531 write_type(out, &arg.ty);
532 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
533 }
534 }
David Tolnay7db73692019-10-20 14:51:12 -0400535 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700536 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400537 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800538 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700539 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800540 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400541 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700542 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400543 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800544 match ret {
545 Type::RustBox(_) => {
546 write_type(out, ret);
547 write!(out, "::from_raw(");
548 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800549 Type::UniquePtr(_) => {
550 write_type(out, ret);
551 write!(out, "(");
552 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800553 Type::Ref(_) => write!(out, "*"),
554 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800555 }
David Tolnay7db73692019-10-20 14:51:12 -0400556 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700557 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700558 write!(out, "::rust::Str::Repr error$ = ");
559 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 write!(out, "{}(", invoke);
561 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400562 if i > 0 {
563 write!(out, ", ");
564 }
David Tolnaybaae4432020-03-01 20:20:10 -0800565 match &arg.ty {
566 Type::Str(_) => write!(out, "::rust::Str::Repr("),
567 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
568 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400569 }
570 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800571 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800572 Type::RustBox(_) => write!(out, ".into_raw()"),
573 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800574 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800575 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800576 _ => {}
577 }
David Tolnay7db73692019-10-20 14:51:12 -0400578 }
579 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400581 write!(out, ", ");
582 }
David Tolnay09011c32020-03-06 14:40:28 -0800583 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400584 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 if indirect_call {
586 if !sig.args.is_empty() || indirect_return {
587 write!(out, ", ");
588 }
589 write!(out, "extern$");
590 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800591 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800593 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800594 write!(out, ")");
595 }
596 }
597 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700599 writeln!(out, " if (error$.ptr) {{");
600 writeln!(out, " throw ::rust::Error(error$);");
601 writeln!(out, " }}");
602 }
David Tolnay7db73692019-10-20 14:51:12 -0400603 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700604 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800605 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400606 }
607 writeln!(out, "}}");
608 }
609}
610
611fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
612 match ty {
613 None => write!(out, "void "),
614 Some(ty) => write_type_space(out, ty),
615 }
616}
617
David Tolnay75dca2e2020-03-25 20:17:52 -0700618fn indirect_return(sig: &Signature, types: &Types) -> bool {
619 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700620 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700621 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700622}
623
David Tolnay99642622020-03-25 13:07:35 -0700624fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
625 match ty {
626 Type::RustBox(ty) | Type::UniquePtr(ty) => {
627 write_type_space(out, &ty.inner);
628 write!(out, "*");
629 }
630 Type::Ref(ty) => {
631 if ty.mutability.is_none() {
632 write!(out, "const ");
633 }
634 write_type(out, &ty.inner);
635 write!(out, " *");
636 }
637 Type::Str(_) => write!(out, "::rust::Str::Repr"),
638 _ => write_type(out, ty),
639 }
640}
641
642fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
643 write_indirect_return_type(out, ty);
644 match ty {
645 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
646 Type::Str(_) => write!(out, " "),
647 _ => write_space_after_type(out, ty),
648 }
649}
650
651fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400652 match ty {
653 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
654 write_type_space(out, &ty.inner);
655 write!(out, "*");
656 }
David Tolnay4a441222020-01-25 16:24:27 -0800657 Some(Type::Ref(ty)) => {
658 if ty.mutability.is_none() {
659 write!(out, "const ");
660 }
661 write_type(out, &ty.inner);
662 write!(out, " *");
663 }
David Tolnay750755e2020-03-01 13:04:08 -0800664 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400665 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
666 _ => write_return_type(out, ty),
667 }
668}
669
670fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
671 match &arg.ty {
672 Type::RustBox(ty) | Type::UniquePtr(ty) => {
673 write_type_space(out, &ty.inner);
674 write!(out, "*");
675 }
David Tolnay750755e2020-03-01 13:04:08 -0800676 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400677 _ => write_type_space(out, &arg.ty),
678 }
679 if types.needs_indirect_abi(&arg.ty) {
680 write!(out, "*");
681 }
682 write!(out, "{}", arg.ident);
683}
684
685fn write_type(out: &mut OutFile, ty: &Type) {
686 match ty {
687 Type::Ident(ident) => match Atom::from(ident) {
688 Some(Bool) => write!(out, "bool"),
689 Some(U8) => write!(out, "uint8_t"),
690 Some(U16) => write!(out, "uint16_t"),
691 Some(U32) => write!(out, "uint32_t"),
692 Some(U64) => write!(out, "uint64_t"),
693 Some(Usize) => write!(out, "size_t"),
694 Some(I8) => write!(out, "int8_t"),
695 Some(I16) => write!(out, "int16_t"),
696 Some(I32) => write!(out, "int32_t"),
697 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700698 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700699 Some(F32) => write!(out, "float"),
700 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800701 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800702 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400703 None => write!(out, "{}", ident),
704 },
705 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800706 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400707 write_type(out, &ty.inner);
708 write!(out, ">");
709 }
710 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800711 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400712 write_type(out, &ptr.inner);
713 write!(out, ">");
714 }
715 Type::Ref(r) => {
716 if r.mutability.is_none() {
717 write!(out, "const ");
718 }
719 write_type(out, &r.inner);
720 write!(out, " &");
721 }
722 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800723 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400724 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700725 Type::Fn(f) => {
726 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
727 match &f.ret {
728 Some(ret) => write_type(out, ret),
729 None => write!(out, "void"),
730 }
731 write!(out, "(");
732 for (i, arg) in f.args.iter().enumerate() {
733 if i > 0 {
734 write!(out, ", ");
735 }
736 write_type(out, &arg.ty);
737 }
738 write!(out, ")>");
739 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700740 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400741 }
742}
743
744fn write_type_space(out: &mut OutFile, ty: &Type) {
745 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700746 write_space_after_type(out, ty);
747}
748
749fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400750 match ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700751 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::Fn(_) => {
752 write!(out, " ")
753 }
David Tolnay7db73692019-10-20 14:51:12 -0400754 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700755 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400756 }
757}
758
759fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
760 fn allow_unique_ptr(ident: &Ident) -> bool {
761 Atom::from(ident).is_none()
762 }
763
764 out.begin_block("extern \"C\"");
765 for ty in types {
766 if let Type::RustBox(ty) = ty {
767 if let Type::Ident(inner) = &ty.inner {
768 out.next_section();
769 write_rust_box_extern(out, inner);
770 }
771 } else if let Type::UniquePtr(ptr) = ty {
772 if let Type::Ident(inner) = &ptr.inner {
773 if allow_unique_ptr(inner) {
774 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700775 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400776 }
777 }
778 }
779 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800780 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400781
David Tolnay750755e2020-03-01 13:04:08 -0800782 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700783 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400784 for ty in types {
785 if let Type::RustBox(ty) = ty {
786 if let Type::Ident(inner) = &ty.inner {
787 write_rust_box_impl(out, inner);
788 }
789 }
790 }
David Tolnay8c730492020-03-13 01:29:06 -0700791 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800792 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400793}
794
795fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
796 let mut inner = String::new();
797 for name in &out.namespace {
798 inner += name;
799 inner += "::";
800 }
801 inner += &ident.to_string();
802 let instance = inner.replace("::", "$");
803
David Tolnay8c730492020-03-13 01:29:06 -0700804 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
805 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400806 writeln!(
807 out,
David Tolnay8c730492020-03-13 01:29:06 -0700808 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400809 instance, inner,
810 );
811 writeln!(
812 out,
David Tolnay8c730492020-03-13 01:29:06 -0700813 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400814 instance, inner,
815 );
David Tolnay8c730492020-03-13 01:29:06 -0700816 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400817}
818
819fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
820 let mut inner = String::new();
821 for name in &out.namespace {
822 inner += name;
823 inner += "::";
824 }
825 inner += &ident.to_string();
826 let instance = inner.replace("::", "$");
827
828 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800829 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700830 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400831 writeln!(out, "}}");
832
833 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800834 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700835 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400836 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400837}
838
David Tolnay53838912020-04-09 20:56:44 -0700839fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700840 out.include.utility = true;
841
David Tolnay7db73692019-10-20 14:51:12 -0400842 let mut inner = String::new();
843 for name in &out.namespace {
844 inner += name;
845 inner += "::";
846 }
847 inner += &ident.to_string();
848 let instance = inner.replace("::", "$");
849
David Tolnay8c730492020-03-13 01:29:06 -0700850 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
851 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400852 writeln!(
853 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800854 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400855 inner,
856 );
857 writeln!(
858 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800859 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400860 inner,
861 );
862 writeln!(
863 out,
David Tolnay8c730492020-03-13 01:29:06 -0700864 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400865 instance, inner,
866 );
David Tolnay7e219b82020-03-01 13:14:51 -0800867 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400868 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700869 if types.structs.contains_key(ident) {
870 writeln!(
871 out,
872 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
873 instance, inner, inner,
874 );
875 writeln!(
876 out,
877 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
878 inner, inner,
879 );
880 writeln!(out, "}}");
881 }
David Tolnay7db73692019-10-20 14:51:12 -0400882 writeln!(
883 out,
David Tolnay8c730492020-03-13 01:29:06 -0700884 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400885 instance, inner, inner,
886 );
David Tolnay7e219b82020-03-01 13:14:51 -0800887 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400888 writeln!(out, "}}");
889 writeln!(
890 out,
David Tolnay8c730492020-03-13 01:29:06 -0700891 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400892 inner, instance, inner,
893 );
894 writeln!(out, " return ptr.get();");
895 writeln!(out, "}}");
896 writeln!(
897 out,
David Tolnay8c730492020-03-13 01:29:06 -0700898 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400899 inner, instance, inner,
900 );
901 writeln!(out, " return ptr.release();");
902 writeln!(out, "}}");
903 writeln!(
904 out,
David Tolnay8c730492020-03-13 01:29:06 -0700905 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400906 instance, inner,
907 );
908 writeln!(out, " ptr->~unique_ptr();");
909 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700910 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400911}