blob: 217a591047011b0af3265bae8f6c74a29d63aff6 [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 Tolnay7db73692019-10-20 14:51:12 -0400113 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700114 match ty {
115 Type::RustBox(_) => {
116 out.include.type_traits = true;
117 needs_rust_box = true;
118 }
119 Type::Str(_) => {
120 out.include.cstdint = true;
121 out.include.string = true;
122 needs_rust_str = true;
123 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700124 Type::Fn(_) => {
125 needs_rust_fn = true;
126 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700127 ty if ty == RustString => {
128 out.include.array = true;
129 out.include.cstdint = true;
130 out.include.string = true;
131 needs_rust_string = true;
132 }
133 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400134 }
135 }
136
David Tolnayb7a7cb62020-03-17 21:18:40 -0700137 let mut needs_rust_error = false;
138 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800139 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800140 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700141 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800142 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700143 match api {
144 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700145 if efn.throws {
146 needs_trycatch = true;
147 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700148 for arg in &efn.args {
149 if arg.ty == RustString {
150 needs_unsafe_bitcopy = true;
151 break;
152 }
David Tolnay09011c32020-03-06 14:40:28 -0800153 }
154 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700155 Api::RustFunction(efn) if !out.header => {
156 if efn.throws {
157 out.include.exception = true;
158 needs_rust_error = true;
159 }
160 for arg in &efn.args {
161 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
162 needs_manually_drop = true;
163 break;
164 }
165 }
166 if let Some(ret) = &efn.ret {
167 if types.needs_indirect_abi(ret) {
168 needs_maybe_uninit = true;
169 }
David Tolnayf51447e2020-03-06 14:14:27 -0800170 }
171 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700172 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800173 }
174 }
175
David Tolnay750755e2020-03-01 13:04:08 -0800176 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700177 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800178
David Tolnayb7a7cb62020-03-17 21:18:40 -0700179 if needs_rust_string
180 || needs_rust_str
181 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700182 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 || needs_rust_error
184 || needs_unsafe_bitcopy
185 || needs_manually_drop
186 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700187 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 {
David Tolnay736cbca2020-03-11 16:49:18 -0700189 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800190 }
191
David Tolnayd1402742020-03-25 22:21:42 -0700192 if needs_rust_string {
193 out.next_section();
194 writeln!(out, "struct unsafe_bitcopy_t;");
195 }
196
David Tolnayb7a7cb62020-03-17 21:18:40 -0700197 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
198 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
199 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700200 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700201 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
202 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800203
204 if needs_manually_drop {
205 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700206 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800207 writeln!(out, "template <typename T>");
208 writeln!(out, "union ManuallyDrop {{");
209 writeln!(out, " T value;");
210 writeln!(
211 out,
212 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
213 );
214 writeln!(out, " ~ManuallyDrop() {{}}");
215 writeln!(out, "}};");
216 }
217
David Tolnay09011c32020-03-06 14:40:28 -0800218 if needs_maybe_uninit {
219 out.next_section();
220 writeln!(out, "template <typename T>");
221 writeln!(out, "union MaybeUninit {{");
222 writeln!(out, " T value;");
223 writeln!(out, " MaybeUninit() {{}}");
224 writeln!(out, " ~MaybeUninit() {{}}");
225 writeln!(out, "}};");
226 }
227
David Tolnay3e3e0af2020-03-17 22:42:49 -0700228 out.end_block("namespace cxxbridge02");
229
David Tolnay5d121442020-03-17 22:14:40 -0700230 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700231 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700232 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700233 out.include.type_traits = true;
234 out.include.utility = true;
235 writeln!(out, "class missing {{}};");
236 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700237 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700238 writeln!(out, "template <typename Try, typename Fail>");
239 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700240 writeln!(
241 out,
David Tolnay04722332020-03-18 11:31:54 -0700242 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700243 );
David Tolnay04722332020-03-18 11:31:54 -0700244 writeln!(out, " missing>::value>::type");
245 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700246 writeln!(out, " func();");
247 writeln!(out, "}} catch (const ::std::exception &e) {{");
248 writeln!(out, " fail(e.what());");
249 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700250 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700251 }
252
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800253 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400254}
255
David Tolnayb7a7cb62020-03-17 21:18:40 -0700256fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
257 if needed {
258 out.next_section();
259 for line in include::get(section).lines() {
260 if !line.trim_start().starts_with("//") {
261 writeln!(out, "{}", line);
262 }
263 }
264 }
265}
266
David Tolnay7db73692019-10-20 14:51:12 -0400267fn write_struct(out: &mut OutFile, strct: &Struct) {
268 for line in strct.doc.to_string().lines() {
269 writeln!(out, "//{}", line);
270 }
271 writeln!(out, "struct {} final {{", strct.ident);
272 for field in &strct.fields {
273 write!(out, " ");
274 write_type_space(out, &field.ty);
275 writeln!(out, "{};", field.ident);
276 }
277 writeln!(out, "}};");
278}
279
280fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
281 writeln!(out, "struct {};", ident);
282}
283
David Tolnay8861bee2020-01-20 18:39:24 -0800284fn write_struct_using(out: &mut OutFile, ident: &Ident) {
285 writeln!(out, "using {} = {};", ident, ident);
286}
287
David Tolnayebef4a22020-03-17 15:33:47 -0700288fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
289 let mut has_cxx_throws = false;
290 for api in apis {
291 if let Api::CxxFunction(efn) = api {
292 if efn.throws {
293 has_cxx_throws = true;
294 break;
295 }
296 }
297 }
298
299 if has_cxx_throws {
300 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700301 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700302 out,
303 "const char *cxxbridge02$exception(const char *, size_t);",
304 );
305 }
306}
307
David Tolnay7db73692019-10-20 14:51:12 -0400308fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700309 if efn.throws {
310 write!(out, "::rust::Str::Repr ");
311 } else {
David Tolnay99642622020-03-25 13:07:35 -0700312 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700313 }
David Tolnayd815de02020-03-29 21:29:17 -0700314 write!(out, "{}cxxbridge02${}(", out.namespace, efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400315 for (i, arg) in efn.args.iter().enumerate() {
316 if i > 0 {
317 write!(out, ", ");
318 }
David Tolnaya46a2372020-03-06 10:03:48 -0800319 if arg.ty == RustString {
320 write!(out, "const ");
321 }
David Tolnay7db73692019-10-20 14:51:12 -0400322 write_extern_arg(out, arg, types);
323 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700324 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400325 if indirect_return {
326 if !efn.args.is_empty() {
327 write!(out, ", ");
328 }
David Tolnay99642622020-03-25 13:07:35 -0700329 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400330 write!(out, "*return$");
331 }
332 writeln!(out, ") noexcept {{");
333 write!(out, " ");
334 write_return_type(out, &efn.ret);
335 write!(out, "(*{}$)(", efn.ident);
336 for (i, arg) in efn.args.iter().enumerate() {
337 if i > 0 {
338 write!(out, ", ");
339 }
340 write_type(out, &arg.ty);
341 }
342 writeln!(out, ") = {};", efn.ident);
343 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700344 if efn.throws {
345 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700346 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700347 writeln!(out, " [&] {{");
348 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700349 }
David Tolnay7db73692019-10-20 14:51:12 -0400350 if indirect_return {
351 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700352 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400353 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700354 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400355 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700356 }
357 match &efn.ret {
358 Some(Type::Ref(_)) => write!(out, "&"),
359 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
360 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400361 }
362 write!(out, "{}$(", efn.ident);
363 for (i, arg) in efn.args.iter().enumerate() {
364 if i > 0 {
365 write!(out, ", ");
366 }
367 if let Type::RustBox(_) = &arg.ty {
368 write_type(out, &arg.ty);
369 write!(out, "::from_raw({})", arg.ident);
370 } else if let Type::UniquePtr(_) = &arg.ty {
371 write_type(out, &arg.ty);
372 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800373 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800374 write!(
375 out,
376 "::rust::String(::rust::unsafe_bitcopy, *{})",
377 arg.ident,
378 );
David Tolnay7db73692019-10-20 14:51:12 -0400379 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700380 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800381 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400382 } else {
383 write!(out, "{}", arg.ident);
384 }
385 }
386 write!(out, ")");
387 match &efn.ret {
388 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
389 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay99642622020-03-25 13:07:35 -0700390 Some(Type::Str(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400391 _ => {}
392 }
393 if indirect_return {
394 write!(out, ")");
395 }
396 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700397 if efn.throws {
398 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700399 writeln!(out, " throw$.ptr = nullptr;");
400 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700401 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700402 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700403 writeln!(
404 out,
David Tolnay5d121442020-03-17 22:14:40 -0700405 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700406 );
David Tolnay5d121442020-03-17 22:14:40 -0700407 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700408 writeln!(out, " return throw$;");
409 }
David Tolnay7db73692019-10-20 14:51:12 -0400410 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700411 for arg in &efn.args {
412 if let Type::Fn(f) = &arg.ty {
413 let var = &arg.ident;
414 write_function_pointer_trampoline(out, efn, var, f, types);
415 }
416 }
417}
418
419fn write_function_pointer_trampoline(
420 out: &mut OutFile,
421 efn: &ExternFn,
422 var: &Ident,
423 f: &Signature,
424 types: &Types,
425) {
426 out.next_section();
427 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
428 let indirect_call = true;
429 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
430
431 out.next_section();
432 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
433 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400434}
435
436fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700437 let link_name = format!("{}cxxbridge02${}", out.namespace, efn.ident);
438 let indirect_call = false;
439 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
440}
441
442fn write_rust_function_decl_impl(
443 out: &mut OutFile,
444 link_name: &str,
445 sig: &Signature,
446 types: &Types,
447 indirect_call: bool,
448) {
449 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700450 write!(out, "::rust::Str::Repr ");
451 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700452 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700453 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700454 write!(out, "{}(", link_name);
455 let mut needs_comma = false;
456 for arg in &sig.args {
457 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400458 write!(out, ", ");
459 }
460 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700461 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400462 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700463 if indirect_return(sig, types) {
464 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, ", ");
466 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700467 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400468 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700469 needs_comma = true;
470 }
471 if indirect_call {
472 if needs_comma {
473 write!(out, ", ");
474 }
475 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400476 }
477 writeln!(out, ") noexcept;");
478}
479
480fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400481 for line in efn.doc.to_string().lines() {
482 writeln!(out, "//{}", line);
483 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700484 let local_name = efn.ident.to_string();
485 let invoke = format!("{}cxxbridge02${}", out.namespace, efn.ident);
486 let indirect_call = false;
487 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
488}
489
490fn write_rust_function_shim_impl(
491 out: &mut OutFile,
492 local_name: &str,
493 sig: &Signature,
494 types: &Types,
495 invoke: &str,
496 indirect_call: bool,
497) {
498 write_return_type(out, &sig.ret);
499 write!(out, "{}(", local_name);
500 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400501 if i > 0 {
502 write!(out, ", ");
503 }
504 write_type_space(out, &arg.ty);
505 write!(out, "{}", arg.ident);
506 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700507 if indirect_call {
508 if !sig.args.is_empty() {
509 write!(out, ", ");
510 }
511 write!(out, "void *extern$");
512 }
David Tolnay1e548172020-03-16 13:37:09 -0700513 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700514 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700515 write!(out, " noexcept");
516 }
David Tolnay7db73692019-10-20 14:51:12 -0400517 if out.header {
518 writeln!(out, ";");
519 } else {
520 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700521 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800522 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700523 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800524 write!(out, " ::rust::ManuallyDrop<");
525 write_type(out, &arg.ty);
526 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
527 }
528 }
David Tolnay7db73692019-10-20 14:51:12 -0400529 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700530 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400531 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800532 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700533 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800534 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400535 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700536 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400537 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800538 match ret {
539 Type::RustBox(_) => {
540 write_type(out, ret);
541 write!(out, "::from_raw(");
542 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800543 Type::UniquePtr(_) => {
544 write_type(out, ret);
545 write!(out, "(");
546 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800547 Type::Ref(_) => write!(out, "*"),
548 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800549 }
David Tolnay7db73692019-10-20 14:51:12 -0400550 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700551 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700552 write!(out, "::rust::Str::Repr error$ = ");
553 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 write!(out, "{}(", invoke);
555 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400556 if i > 0 {
557 write!(out, ", ");
558 }
David Tolnaybaae4432020-03-01 20:20:10 -0800559 match &arg.ty {
560 Type::Str(_) => write!(out, "::rust::Str::Repr("),
561 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
562 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400563 }
564 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800565 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800566 Type::RustBox(_) => write!(out, ".into_raw()"),
567 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800568 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800569 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800570 _ => {}
571 }
David Tolnay7db73692019-10-20 14:51:12 -0400572 }
573 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700574 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400575 write!(out, ", ");
576 }
David Tolnay09011c32020-03-06 14:40:28 -0800577 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400578 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700579 if indirect_call {
580 if !sig.args.is_empty() || indirect_return {
581 write!(out, ", ");
582 }
583 write!(out, "extern$");
584 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800585 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800587 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800588 write!(out, ")");
589 }
590 }
591 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700593 writeln!(out, " if (error$.ptr) {{");
594 writeln!(out, " throw ::rust::Error(error$);");
595 writeln!(out, " }}");
596 }
David Tolnay7db73692019-10-20 14:51:12 -0400597 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700598 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800599 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400600 }
601 writeln!(out, "}}");
602 }
603}
604
605fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
606 match ty {
607 None => write!(out, "void "),
608 Some(ty) => write_type_space(out, ty),
609 }
610}
611
David Tolnay75dca2e2020-03-25 20:17:52 -0700612fn indirect_return(sig: &Signature, types: &Types) -> bool {
613 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700614 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700616}
617
David Tolnay99642622020-03-25 13:07:35 -0700618fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
619 match ty {
620 Type::RustBox(ty) | Type::UniquePtr(ty) => {
621 write_type_space(out, &ty.inner);
622 write!(out, "*");
623 }
624 Type::Ref(ty) => {
625 if ty.mutability.is_none() {
626 write!(out, "const ");
627 }
628 write_type(out, &ty.inner);
629 write!(out, " *");
630 }
631 Type::Str(_) => write!(out, "::rust::Str::Repr"),
632 _ => write_type(out, ty),
633 }
634}
635
636fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
637 write_indirect_return_type(out, ty);
638 match ty {
639 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
640 Type::Str(_) => write!(out, " "),
641 _ => write_space_after_type(out, ty),
642 }
643}
644
645fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400646 match ty {
647 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
648 write_type_space(out, &ty.inner);
649 write!(out, "*");
650 }
David Tolnay4a441222020-01-25 16:24:27 -0800651 Some(Type::Ref(ty)) => {
652 if ty.mutability.is_none() {
653 write!(out, "const ");
654 }
655 write_type(out, &ty.inner);
656 write!(out, " *");
657 }
David Tolnay750755e2020-03-01 13:04:08 -0800658 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400659 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
660 _ => write_return_type(out, ty),
661 }
662}
663
664fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
665 match &arg.ty {
666 Type::RustBox(ty) | Type::UniquePtr(ty) => {
667 write_type_space(out, &ty.inner);
668 write!(out, "*");
669 }
David Tolnay750755e2020-03-01 13:04:08 -0800670 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400671 _ => write_type_space(out, &arg.ty),
672 }
673 if types.needs_indirect_abi(&arg.ty) {
674 write!(out, "*");
675 }
676 write!(out, "{}", arg.ident);
677}
678
679fn write_type(out: &mut OutFile, ty: &Type) {
680 match ty {
681 Type::Ident(ident) => match Atom::from(ident) {
682 Some(Bool) => write!(out, "bool"),
683 Some(U8) => write!(out, "uint8_t"),
684 Some(U16) => write!(out, "uint16_t"),
685 Some(U32) => write!(out, "uint32_t"),
686 Some(U64) => write!(out, "uint64_t"),
687 Some(Usize) => write!(out, "size_t"),
688 Some(I8) => write!(out, "int8_t"),
689 Some(I16) => write!(out, "int16_t"),
690 Some(I32) => write!(out, "int32_t"),
691 Some(I64) => write!(out, "int64_t"),
692 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700693 Some(F32) => write!(out, "float"),
694 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800695 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800696 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400697 None => write!(out, "{}", ident),
698 },
699 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800700 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400701 write_type(out, &ty.inner);
702 write!(out, ">");
703 }
704 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800705 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400706 write_type(out, &ptr.inner);
707 write!(out, ">");
708 }
709 Type::Ref(r) => {
710 if r.mutability.is_none() {
711 write!(out, "const ");
712 }
713 write_type(out, &r.inner);
714 write!(out, " &");
715 }
716 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800717 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400718 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 Type::Fn(f) => {
720 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
721 match &f.ret {
722 Some(ret) => write_type(out, ret),
723 None => write!(out, "void"),
724 }
725 write!(out, "(");
726 for (i, arg) in f.args.iter().enumerate() {
727 if i > 0 {
728 write!(out, ", ");
729 }
730 write_type(out, &arg.ty);
731 }
732 write!(out, ")>");
733 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700734 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400735 }
736}
737
738fn write_type_space(out: &mut OutFile, ty: &Type) {
739 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700740 write_space_after_type(out, ty);
741}
742
743fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400744 match ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700745 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::Fn(_) => {
746 write!(out, " ")
747 }
David Tolnay7db73692019-10-20 14:51:12 -0400748 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700749 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400750 }
751}
752
753fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
754 fn allow_unique_ptr(ident: &Ident) -> bool {
755 Atom::from(ident).is_none()
756 }
757
758 out.begin_block("extern \"C\"");
759 for ty in types {
760 if let Type::RustBox(ty) = ty {
761 if let Type::Ident(inner) = &ty.inner {
762 out.next_section();
763 write_rust_box_extern(out, inner);
764 }
765 } else if let Type::UniquePtr(ptr) = ty {
766 if let Type::Ident(inner) = &ptr.inner {
767 if allow_unique_ptr(inner) {
768 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700769 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400770 }
771 }
772 }
773 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800774 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400775
David Tolnay750755e2020-03-01 13:04:08 -0800776 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700777 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400778 for ty in types {
779 if let Type::RustBox(ty) = ty {
780 if let Type::Ident(inner) = &ty.inner {
781 write_rust_box_impl(out, inner);
782 }
783 }
784 }
David Tolnay8c730492020-03-13 01:29:06 -0700785 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800786 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400787}
788
789fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
790 let mut inner = String::new();
791 for name in &out.namespace {
792 inner += name;
793 inner += "::";
794 }
795 inner += &ident.to_string();
796 let instance = inner.replace("::", "$");
797
David Tolnay8c730492020-03-13 01:29:06 -0700798 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
799 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400800 writeln!(
801 out,
David Tolnay8c730492020-03-13 01:29:06 -0700802 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400803 instance, inner,
804 );
805 writeln!(
806 out,
David Tolnay8c730492020-03-13 01:29:06 -0700807 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400808 instance, inner,
809 );
David Tolnay8c730492020-03-13 01:29:06 -0700810 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400811}
812
813fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
814 let mut inner = String::new();
815 for name in &out.namespace {
816 inner += name;
817 inner += "::";
818 }
819 inner += &ident.to_string();
820 let instance = inner.replace("::", "$");
821
822 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800823 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700824 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400825 writeln!(out, "}}");
826
827 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800828 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700829 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400830 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400831}
832
David Tolnay53838912020-04-09 20:56:44 -0700833fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700834 out.include.utility = true;
835
David Tolnay7db73692019-10-20 14:51:12 -0400836 let mut inner = String::new();
837 for name in &out.namespace {
838 inner += name;
839 inner += "::";
840 }
841 inner += &ident.to_string();
842 let instance = inner.replace("::", "$");
843
David Tolnay8c730492020-03-13 01:29:06 -0700844 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
845 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400846 writeln!(
847 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800848 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400849 inner,
850 );
851 writeln!(
852 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800853 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400854 inner,
855 );
856 writeln!(
857 out,
David Tolnay8c730492020-03-13 01:29:06 -0700858 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400859 instance, inner,
860 );
David Tolnay7e219b82020-03-01 13:14:51 -0800861 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400862 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700863 if types.structs.contains_key(ident) {
864 writeln!(
865 out,
866 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
867 instance, inner, inner,
868 );
869 writeln!(
870 out,
871 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
872 inner, inner,
873 );
874 writeln!(out, "}}");
875 }
David Tolnay7db73692019-10-20 14:51:12 -0400876 writeln!(
877 out,
David Tolnay8c730492020-03-13 01:29:06 -0700878 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400879 instance, inner, inner,
880 );
David Tolnay7e219b82020-03-01 13:14:51 -0800881 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400882 writeln!(out, "}}");
883 writeln!(
884 out,
David Tolnay8c730492020-03-13 01:29:06 -0700885 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400886 inner, instance, inner,
887 );
888 writeln!(out, " return ptr.get();");
889 writeln!(out, "}}");
890 writeln!(
891 out,
David Tolnay8c730492020-03-13 01:29:06 -0700892 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400893 inner, instance, inner,
894 );
895 writeln!(out, " return ptr.release();");
896 writeln!(out, "}}");
897 writeln!(
898 out,
David Tolnay8c730492020-03-13 01:29:06 -0700899 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400900 instance, inner,
901 );
902 writeln!(out, " ptr->~unique_ptr();");
903 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700904 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400905}