blob: c100691d45ec7917b6469c7143b0cb507288e635 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
David Tolnay3caa50a2020-04-19 21:25:34 -07006use crate::syntax::{
7 mangle, Api, ExternFn, ExternType, Receiver, Signature, Struct, Type, Types, Var,
8};
David Tolnay7db73692019-10-20 14:51:12 -04009use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -070010use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnay33d30292020-03-18 18:02:02 -070012pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070013 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070014 apis: &[Api],
15 types: &Types,
16 opt: Opt,
17 header: bool,
18) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040019 let mut out_file = OutFile::new(namespace.clone(), header);
20 let out = &mut out_file;
21
22 if header {
23 writeln!(out, "#pragma once");
24 }
25
David Tolnay33d30292020-03-18 18:02:02 -070026 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040027 for api in apis {
28 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080029 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040030 }
31 }
32
33 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080034 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040035
David Tolnay7db73692019-10-20 14:51:12 -040036 out.next_section();
37 for name in &namespace {
38 writeln!(out, "namespace {} {{", name);
39 }
40
David Tolnay7db73692019-10-20 14:51:12 -040041 out.next_section();
42 for api in apis {
43 match api {
44 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080045 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
46 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040047 _ => {}
48 }
49 }
50
David Tolnayf94bef12020-04-17 14:46:42 -070051 let mut methods_for_type = HashMap::new();
52 for api in apis {
53 if let Api::RustFunction(efn) = api {
54 if let Some(receiver) = &efn.sig.receiver {
55 methods_for_type
56 .entry(&receiver.ident)
57 .or_insert_with(Vec::new)
58 .push(efn);
59 }
60 }
61 }
Joel Galenson968738f2020-04-15 14:19:33 -070062
David Tolnay7db73692019-10-20 14:51:12 -040063 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070064 match api {
65 Api::Struct(strct) => {
66 out.next_section();
67 write_struct(out, strct);
68 }
David Tolnayc1fe0052020-04-17 15:15:06 -070069 Api::RustType(ety) => {
70 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070071 out.next_section();
72 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 }
David Tolnayc1fe0052020-04-17 15:15:06 -070074 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77 }
78
79 if !header {
80 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070081 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040082 for api in apis {
83 let (efn, write): (_, fn(_, _, _)) = match api {
84 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
85 Api::RustFunction(efn) => (efn, write_rust_function_decl),
86 _ => continue,
87 };
88 out.next_section();
89 write(out, efn, types);
90 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080091 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040092 }
93
94 for api in apis {
95 if let Api::RustFunction(efn) = api {
96 out.next_section();
97 write_rust_function_shim(out, efn, types);
98 }
99 }
100
101 out.next_section();
102 for name in namespace.iter().rev() {
103 writeln!(out, "}} // namespace {}", name);
104 }
105
106 if !header {
107 out.next_section();
108 write_generic_instantiations(out, types);
109 }
110
David Tolnay9c68b1a2020-03-06 11:12:55 -0800111 out.prepend(out.include.to_string());
112
David Tolnay7db73692019-10-20 14:51:12 -0400113 out_file
114}
115
116fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400117 for ty in types {
118 match ty {
119 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700120 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
121 | Some(I64) => out.include.cstdint = true,
122 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800123 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700124 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400125 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800126 Type::RustBox(_) => out.include.type_traits = true,
127 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700128 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400129 _ => {}
130 }
131 }
David Tolnay7db73692019-10-20 14:51:12 -0400132}
133
David Tolnayf51447e2020-03-06 14:14:27 -0800134fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700135 let mut needs_rust_string = false;
136 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700137 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400138 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700139 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700140 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400141 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 match ty {
143 Type::RustBox(_) => {
144 out.include.type_traits = true;
145 needs_rust_box = true;
146 }
147 Type::Str(_) => {
148 out.include.cstdint = true;
149 out.include.string = true;
150 needs_rust_str = true;
151 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700152 Type::Fn(_) => {
153 needs_rust_fn = true;
154 }
David Tolnay4770b472020-04-14 16:32:59 -0700155 Type::Slice(_) | Type::SliceRefU8(_) => {
156 needs_rust_slice = true;
157 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700158 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700159 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700160 needs_rust_isize = true;
161 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 ty if ty == RustString => {
163 out.include.array = true;
164 out.include.cstdint = true;
165 out.include.string = true;
166 needs_rust_string = true;
167 }
168 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400169 }
170 }
171
David Tolnayb7a7cb62020-03-17 21:18:40 -0700172 let mut needs_rust_error = false;
173 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800174 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800175 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700176 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800177 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 match api {
179 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700180 if efn.throws {
181 needs_trycatch = true;
182 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 for arg in &efn.args {
184 if arg.ty == RustString {
185 needs_unsafe_bitcopy = true;
186 break;
187 }
David Tolnay09011c32020-03-06 14:40:28 -0800188 }
189 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700190 Api::RustFunction(efn) if !out.header => {
191 if efn.throws {
192 out.include.exception = true;
193 needs_rust_error = true;
194 }
195 for arg in &efn.args {
196 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
197 needs_manually_drop = true;
198 break;
199 }
200 }
201 if let Some(ret) = &efn.ret {
202 if types.needs_indirect_abi(ret) {
203 needs_maybe_uninit = true;
204 }
David Tolnayf51447e2020-03-06 14:14:27 -0800205 }
206 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800208 }
209 }
210
David Tolnay750755e2020-03-01 13:04:08 -0800211 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700212 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800213
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 if needs_rust_string
215 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700216 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700218 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700220 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 || needs_unsafe_bitcopy
222 || needs_manually_drop
223 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700224 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 {
David Tolnay736cbca2020-03-11 16:49:18 -0700226 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228
David Tolnayd1402742020-03-25 22:21:42 -0700229 if needs_rust_string {
230 out.next_section();
231 writeln!(out, "struct unsafe_bitcopy_t;");
232 }
233
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
235 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700236 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700238 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700240 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800242
243 if needs_manually_drop {
244 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700245 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800246 writeln!(out, "template <typename T>");
247 writeln!(out, "union ManuallyDrop {{");
248 writeln!(out, " T value;");
249 writeln!(
250 out,
251 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
252 );
253 writeln!(out, " ~ManuallyDrop() {{}}");
254 writeln!(out, "}};");
255 }
256
David Tolnay09011c32020-03-06 14:40:28 -0800257 if needs_maybe_uninit {
258 out.next_section();
259 writeln!(out, "template <typename T>");
260 writeln!(out, "union MaybeUninit {{");
261 writeln!(out, " T value;");
262 writeln!(out, " MaybeUninit() {{}}");
263 writeln!(out, " ~MaybeUninit() {{}}");
264 writeln!(out, "}};");
265 }
266
David Tolnay3e3e0af2020-03-17 22:42:49 -0700267 out.end_block("namespace cxxbridge02");
268
David Tolnay5d121442020-03-17 22:14:40 -0700269 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700270 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700271 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700272 out.include.type_traits = true;
273 out.include.utility = true;
274 writeln!(out, "class missing {{}};");
275 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700276 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700277 writeln!(out, "template <typename Try, typename Fail>");
278 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700279 writeln!(
280 out,
David Tolnay04722332020-03-18 11:31:54 -0700281 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700282 );
David Tolnay04722332020-03-18 11:31:54 -0700283 writeln!(out, " missing>::value>::type");
284 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700285 writeln!(out, " func();");
286 writeln!(out, "}} catch (const ::std::exception &e) {{");
287 writeln!(out, " fail(e.what());");
288 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700290 }
291
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800292 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400293}
294
David Tolnayb7a7cb62020-03-17 21:18:40 -0700295fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700296 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700297 if needed {
298 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700299 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700300 if !line.trim_start().starts_with("//") {
301 writeln!(out, "{}", line);
302 }
303 }
304 }
305}
306
David Tolnay7db73692019-10-20 14:51:12 -0400307fn write_struct(out: &mut OutFile, strct: &Struct) {
308 for line in strct.doc.to_string().lines() {
309 writeln!(out, "//{}", line);
310 }
311 writeln!(out, "struct {} final {{", strct.ident);
312 for field in &strct.fields {
313 write!(out, " ");
314 write_type_space(out, &field.ty);
315 writeln!(out, "{};", field.ident);
316 }
317 writeln!(out, "}};");
318}
319
320fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
321 writeln!(out, "struct {};", ident);
322}
323
David Tolnay8861bee2020-01-20 18:39:24 -0800324fn write_struct_using(out: &mut OutFile, ident: &Ident) {
325 writeln!(out, "using {} = {};", ident, ident);
326}
327
David Tolnayc1fe0052020-04-17 15:15:06 -0700328fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700329 for line in ety.doc.to_string().lines() {
330 writeln!(out, "//{}", line);
331 }
332 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700333 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700334 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700335 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700336 write!(out, " ");
337 let sig = &method.sig;
David Tolnay891061b2020-04-19 22:42:33 -0700338 let local_name = Symbol::from(&method.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700339 write_rust_function_shim_decl(out, &local_name, sig, None, false);
340 writeln!(out, ";");
341 }
342 writeln!(out, "}};");
343}
344
David Tolnayebef4a22020-03-17 15:33:47 -0700345fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
346 let mut has_cxx_throws = false;
347 for api in apis {
348 if let Api::CxxFunction(efn) = api {
349 if efn.throws {
350 has_cxx_throws = true;
351 break;
352 }
353 }
354 }
355
356 if has_cxx_throws {
357 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700358 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700359 out,
360 "const char *cxxbridge02$exception(const char *, size_t);",
361 );
362 }
363}
364
David Tolnay7db73692019-10-20 14:51:12 -0400365fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700366 if efn.throws {
367 write!(out, "::rust::Str::Repr ");
368 } else {
David Tolnay99642622020-03-25 13:07:35 -0700369 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700370 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700371 let mangled = mangle::extern_fn(&out.namespace, efn);
372 write!(out, "{}(", mangled);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700373 if let Some(base) = &efn.receiver {
David Tolnay26804bd2020-04-19 20:06:51 -0700374 write!(out, "{} *self$", base.ident);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700375 }
David Tolnay7db73692019-10-20 14:51:12 -0400376 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700377 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400378 write!(out, ", ");
379 }
David Tolnaya46a2372020-03-06 10:03:48 -0800380 if arg.ty == RustString {
381 write!(out, "const ");
382 }
David Tolnay7db73692019-10-20 14:51:12 -0400383 write_extern_arg(out, arg, types);
384 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700385 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400386 if indirect_return {
387 if !efn.args.is_empty() {
388 write!(out, ", ");
389 }
David Tolnay99642622020-03-25 13:07:35 -0700390 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400391 write!(out, "*return$");
392 }
393 writeln!(out, ") noexcept {{");
394 write!(out, " ");
395 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700396 match &efn.receiver {
397 None => write!(out, "(*{}$)(", efn.ident),
398 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
399 }
David Tolnay7db73692019-10-20 14:51:12 -0400400 for (i, arg) in efn.args.iter().enumerate() {
401 if i > 0 {
402 write!(out, ", ");
403 }
404 write_type(out, &arg.ty);
405 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700406 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700407 if let Some(receiver) = &efn.receiver {
408 if receiver.mutability.is_none() {
409 write!(out, " const");
410 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700411 }
412 write!(out, " = ");
413 match &efn.receiver {
414 None => write!(out, "{}", efn.ident),
415 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
416 }
417 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400418 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700419 if efn.throws {
420 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700421 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700422 writeln!(out, " [&] {{");
423 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700424 }
David Tolnay7db73692019-10-20 14:51:12 -0400425 if indirect_return {
426 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700427 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700429 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400430 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700431 }
432 match &efn.ret {
433 Some(Type::Ref(_)) => write!(out, "&"),
434 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700435 Some(Type::SliceRefU8(_)) if !indirect_return => {
436 write!(out, "::rust::Slice<uint8_t>::Repr(")
437 }
David Tolnay99642622020-03-25 13:07:35 -0700438 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400439 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700440 match &efn.receiver {
441 None => write!(out, "{}$(", efn.ident),
David Tolnay26804bd2020-04-19 20:06:51 -0700442 Some(_) => write!(out, "(self$->*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700443 }
David Tolnay7db73692019-10-20 14:51:12 -0400444 for (i, arg) in efn.args.iter().enumerate() {
445 if i > 0 {
446 write!(out, ", ");
447 }
448 if let Type::RustBox(_) = &arg.ty {
449 write_type(out, &arg.ty);
450 write!(out, "::from_raw({})", arg.ident);
451 } else if let Type::UniquePtr(_) = &arg.ty {
452 write_type(out, &arg.ty);
453 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800454 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800455 write!(
456 out,
457 "::rust::String(::rust::unsafe_bitcopy, *{})",
458 arg.ident,
459 );
David Tolnay7db73692019-10-20 14:51:12 -0400460 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700461 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800462 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400463 } else {
464 write!(out, "{}", arg.ident);
465 }
466 }
467 write!(out, ")");
468 match &efn.ret {
469 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
470 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700471 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400472 _ => {}
473 }
474 if indirect_return {
475 write!(out, ")");
476 }
477 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700478 if efn.throws {
479 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700480 writeln!(out, " throw$.ptr = nullptr;");
481 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700482 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700483 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700484 writeln!(
485 out,
David Tolnay5d121442020-03-17 22:14:40 -0700486 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700487 );
David Tolnay5d121442020-03-17 22:14:40 -0700488 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700489 writeln!(out, " return throw$;");
490 }
David Tolnay7db73692019-10-20 14:51:12 -0400491 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700492 for arg in &efn.args {
493 if let Type::Fn(f) = &arg.ty {
494 let var = &arg.ident;
495 write_function_pointer_trampoline(out, efn, var, f, types);
496 }
497 }
498}
499
500fn write_function_pointer_trampoline(
501 out: &mut OutFile,
502 efn: &ExternFn,
503 var: &Ident,
504 f: &Signature,
505 types: &Types,
506) {
507 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700508 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700509 let indirect_call = true;
510 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
511
512 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700513 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700514 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400515}
516
517fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700518 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700519 let indirect_call = false;
520 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
521}
522
523fn write_rust_function_decl_impl(
524 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700525 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700526 sig: &Signature,
527 types: &Types,
528 indirect_call: bool,
529) {
530 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700531 write!(out, "::rust::Str::Repr ");
532 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700533 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700534 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 write!(out, "{}(", link_name);
536 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700537 if let Some(base) = &sig.receiver {
David Tolnay26804bd2020-04-19 20:06:51 -0700538 write!(out, "{} &self$", base.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700539 needs_comma = true;
540 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700541 for arg in &sig.args {
542 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400543 write!(out, ", ");
544 }
545 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700546 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400547 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 if indirect_return(sig, types) {
549 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400550 write!(out, ", ");
551 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400553 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 needs_comma = true;
555 }
556 if indirect_call {
557 if needs_comma {
558 write!(out, ", ");
559 }
560 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400561 }
562 writeln!(out, ") noexcept;");
563}
564
565fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400566 for line in efn.doc.to_string().lines() {
567 writeln!(out, "//{}", line);
568 }
David Tolnay891061b2020-04-19 22:42:33 -0700569 let local_name = Symbol::from(&efn.ident);
David Tolnay3caa50a2020-04-19 21:25:34 -0700570 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700571 let indirect_call = false;
572 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
573}
574
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700575fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700576 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700577 local_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700578 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700579 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 indirect_call: bool,
581) {
582 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700583 if let Some(base) = receiver {
584 write!(out, "{}::", base.ident);
585 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 write!(out, "{}(", local_name);
587 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400588 if i > 0 {
589 write!(out, ", ");
590 }
591 write_type_space(out, &arg.ty);
592 write!(out, "{}", arg.ident);
593 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 if indirect_call {
595 if !sig.args.is_empty() {
596 write!(out, ", ");
597 }
598 write!(out, "void *extern$");
599 }
David Tolnay1e548172020-03-16 13:37:09 -0700600 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700602 write!(out, " noexcept");
603 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700604}
605
606fn write_rust_function_shim_impl(
607 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700608 local_name: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700609 sig: &Signature,
610 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700611 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700612 indirect_call: bool,
613) {
614 if out.header && sig.receiver.is_some() {
615 // We've already defined this inside the struct.
616 return;
617 }
618 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400619 if out.header {
620 writeln!(out, ";");
621 } else {
622 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800624 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700625 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800626 write!(out, " ::rust::ManuallyDrop<");
627 write_type(out, &arg.ty);
628 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
629 }
630 }
David Tolnay7db73692019-10-20 14:51:12 -0400631 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700632 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400633 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800634 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700635 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800636 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400637 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700638 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400639 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800640 match ret {
641 Type::RustBox(_) => {
642 write_type(out, ret);
643 write!(out, "::from_raw(");
644 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800645 Type::UniquePtr(_) => {
646 write_type(out, ret);
647 write!(out, "(");
648 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800649 Type::Ref(_) => write!(out, "*"),
650 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800651 }
David Tolnay7db73692019-10-20 14:51:12 -0400652 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700654 write!(out, "::rust::Str::Repr error$ = ");
655 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 write!(out, "{}(", invoke);
David Tolnay9b5cfe12020-04-19 21:11:50 -0700657 if sig.receiver.is_some() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700658 write!(out, "*this");
659 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700660 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700661 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400662 write!(out, ", ");
663 }
David Tolnaybaae4432020-03-01 20:20:10 -0800664 match &arg.ty {
665 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700666 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800667 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
668 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400669 }
670 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800671 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800672 Type::RustBox(_) => write!(out, ".into_raw()"),
673 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700674 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800675 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800676 _ => {}
677 }
David Tolnay7db73692019-10-20 14:51:12 -0400678 }
679 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700680 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400681 write!(out, ", ");
682 }
David Tolnay09011c32020-03-06 14:40:28 -0800683 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400684 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 if indirect_call {
686 if !sig.args.is_empty() || indirect_return {
687 write!(out, ", ");
688 }
689 write!(out, "extern$");
690 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800691 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700692 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800693 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800694 write!(out, ")");
695 }
696 }
697 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700698 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700699 writeln!(out, " if (error$.ptr) {{");
700 writeln!(out, " throw ::rust::Error(error$);");
701 writeln!(out, " }}");
702 }
David Tolnay7db73692019-10-20 14:51:12 -0400703 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700704 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800705 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400706 }
707 writeln!(out, "}}");
708 }
709}
710
711fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
712 match ty {
713 None => write!(out, "void "),
714 Some(ty) => write_type_space(out, ty),
715 }
716}
717
David Tolnay75dca2e2020-03-25 20:17:52 -0700718fn indirect_return(sig: &Signature, types: &Types) -> bool {
719 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700720 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700721 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700722}
723
David Tolnay99642622020-03-25 13:07:35 -0700724fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
725 match ty {
726 Type::RustBox(ty) | Type::UniquePtr(ty) => {
727 write_type_space(out, &ty.inner);
728 write!(out, "*");
729 }
730 Type::Ref(ty) => {
731 if ty.mutability.is_none() {
732 write!(out, "const ");
733 }
734 write_type(out, &ty.inner);
735 write!(out, " *");
736 }
737 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700738 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700739 _ => write_type(out, ty),
740 }
741}
742
743fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
744 write_indirect_return_type(out, ty);
745 match ty {
746 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700747 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700748 _ => write_space_after_type(out, ty),
749 }
750}
751
752fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400753 match ty {
754 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
755 write_type_space(out, &ty.inner);
756 write!(out, "*");
757 }
David Tolnay4a441222020-01-25 16:24:27 -0800758 Some(Type::Ref(ty)) => {
759 if ty.mutability.is_none() {
760 write!(out, "const ");
761 }
762 write_type(out, &ty.inner);
763 write!(out, " *");
764 }
David Tolnay750755e2020-03-01 13:04:08 -0800765 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700766 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400767 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
768 _ => write_return_type(out, ty),
769 }
770}
771
772fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
773 match &arg.ty {
774 Type::RustBox(ty) | Type::UniquePtr(ty) => {
775 write_type_space(out, &ty.inner);
776 write!(out, "*");
777 }
David Tolnay750755e2020-03-01 13:04:08 -0800778 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700779 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400780 _ => write_type_space(out, &arg.ty),
781 }
782 if types.needs_indirect_abi(&arg.ty) {
783 write!(out, "*");
784 }
785 write!(out, "{}", arg.ident);
786}
787
788fn write_type(out: &mut OutFile, ty: &Type) {
789 match ty {
790 Type::Ident(ident) => match Atom::from(ident) {
791 Some(Bool) => write!(out, "bool"),
792 Some(U8) => write!(out, "uint8_t"),
793 Some(U16) => write!(out, "uint16_t"),
794 Some(U32) => write!(out, "uint32_t"),
795 Some(U64) => write!(out, "uint64_t"),
796 Some(Usize) => write!(out, "size_t"),
797 Some(I8) => write!(out, "int8_t"),
798 Some(I16) => write!(out, "int16_t"),
799 Some(I32) => write!(out, "int32_t"),
800 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700801 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700802 Some(F32) => write!(out, "float"),
803 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800804 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800805 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400806 None => write!(out, "{}", ident),
807 },
808 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800809 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400810 write_type(out, &ty.inner);
811 write!(out, ">");
812 }
813 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800814 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400815 write_type(out, &ptr.inner);
816 write!(out, ">");
817 }
818 Type::Ref(r) => {
819 if r.mutability.is_none() {
820 write!(out, "const ");
821 }
822 write_type(out, &r.inner);
823 write!(out, " &");
824 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700825 Type::Slice(_) => {
826 // For now, only U8 slices are supported, which are covered separately below
827 unreachable!()
828 }
David Tolnay7db73692019-10-20 14:51:12 -0400829 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800830 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400831 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700832 Type::SliceRefU8(_) => {
833 write!(out, "::rust::Slice<uint8_t>");
834 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700835 Type::Fn(f) => {
836 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
837 match &f.ret {
838 Some(ret) => write_type(out, ret),
839 None => write!(out, "void"),
840 }
841 write!(out, "(");
842 for (i, arg) in f.args.iter().enumerate() {
843 if i > 0 {
844 write!(out, ", ");
845 }
846 write_type(out, &arg.ty);
847 }
848 write!(out, ")>");
849 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700850 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400851 }
852}
853
854fn write_type_space(out: &mut OutFile, ty: &Type) {
855 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700856 write_space_after_type(out, ty);
857}
858
859fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400860 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700861 Type::Ident(_)
862 | Type::RustBox(_)
863 | Type::UniquePtr(_)
864 | Type::Str(_)
865 | Type::SliceRefU8(_)
866 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400867 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700868 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400869 }
870}
871
872fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
873 fn allow_unique_ptr(ident: &Ident) -> bool {
874 Atom::from(ident).is_none()
875 }
876
877 out.begin_block("extern \"C\"");
878 for ty in types {
879 if let Type::RustBox(ty) = ty {
880 if let Type::Ident(inner) = &ty.inner {
881 out.next_section();
882 write_rust_box_extern(out, inner);
883 }
884 } else if let Type::UniquePtr(ptr) = ty {
885 if let Type::Ident(inner) = &ptr.inner {
886 if allow_unique_ptr(inner) {
887 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700888 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400889 }
890 }
891 }
892 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800893 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400894
David Tolnay750755e2020-03-01 13:04:08 -0800895 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700896 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400897 for ty in types {
898 if let Type::RustBox(ty) = ty {
899 if let Type::Ident(inner) = &ty.inner {
900 write_rust_box_impl(out, inner);
901 }
902 }
903 }
David Tolnay8c730492020-03-13 01:29:06 -0700904 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800905 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400906}
907
908fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
909 let mut inner = String::new();
910 for name in &out.namespace {
911 inner += name;
912 inner += "::";
913 }
914 inner += &ident.to_string();
915 let instance = inner.replace("::", "$");
916
David Tolnay8c730492020-03-13 01:29:06 -0700917 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
918 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400919 writeln!(
920 out,
David Tolnay8c730492020-03-13 01:29:06 -0700921 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400922 instance, inner,
923 );
924 writeln!(
925 out,
David Tolnay8c730492020-03-13 01:29:06 -0700926 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400927 instance, inner,
928 );
David Tolnay8c730492020-03-13 01:29:06 -0700929 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400930}
931
932fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
933 let mut inner = String::new();
934 for name in &out.namespace {
935 inner += name;
936 inner += "::";
937 }
938 inner += &ident.to_string();
939 let instance = inner.replace("::", "$");
940
941 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800942 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700943 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400944 writeln!(out, "}}");
945
946 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800947 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700948 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400949 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400950}
951
David Tolnay53838912020-04-09 20:56:44 -0700952fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700953 out.include.utility = true;
954
David Tolnay7db73692019-10-20 14:51:12 -0400955 let mut inner = String::new();
956 for name in &out.namespace {
957 inner += name;
958 inner += "::";
959 }
960 inner += &ident.to_string();
961 let instance = inner.replace("::", "$");
962
David Tolnay8c730492020-03-13 01:29:06 -0700963 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
964 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400965 writeln!(
966 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800967 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400968 inner,
969 );
970 writeln!(
971 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800972 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400973 inner,
974 );
975 writeln!(
976 out,
David Tolnay8c730492020-03-13 01:29:06 -0700977 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400978 instance, inner,
979 );
David Tolnay7e219b82020-03-01 13:14:51 -0800980 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400981 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700982 if types.structs.contains_key(ident) {
983 writeln!(
984 out,
985 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
986 instance, inner, inner,
987 );
988 writeln!(
989 out,
990 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
991 inner, inner,
992 );
993 writeln!(out, "}}");
994 }
David Tolnay7db73692019-10-20 14:51:12 -0400995 writeln!(
996 out,
David Tolnay8c730492020-03-13 01:29:06 -0700997 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400998 instance, inner, inner,
999 );
David Tolnay7e219b82020-03-01 13:14:51 -08001000 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001001 writeln!(out, "}}");
1002 writeln!(
1003 out,
David Tolnay8c730492020-03-13 01:29:06 -07001004 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001005 inner, instance, inner,
1006 );
1007 writeln!(out, " return ptr.get();");
1008 writeln!(out, "}}");
1009 writeln!(
1010 out,
David Tolnay8c730492020-03-13 01:29:06 -07001011 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001012 inner, instance, inner,
1013 );
1014 writeln!(out, " return ptr.release();");
1015 writeln!(out, "}}");
1016 writeln!(
1017 out,
David Tolnay8c730492020-03-13 01:29:06 -07001018 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001019 instance, inner,
1020 );
1021 writeln!(out, " ptr->~unique_ptr();");
1022 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001023 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001024}