blob: 59e12edba1ff4033d5dd78f9d19dee4e2900c26a [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;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -07005use crate::syntax::{Api, ExternFn, ExternType, Receiver, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04006use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -07007use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04008
David Tolnay33d30292020-03-18 18:02:02 -07009pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070010 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070011 apis: &[Api],
12 types: &Types,
13 opt: Opt,
14 header: bool,
15) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040016 let mut out_file = OutFile::new(namespace.clone(), header);
17 let out = &mut out_file;
18
19 if header {
20 writeln!(out, "#pragma once");
21 }
22
David Tolnay33d30292020-03-18 18:02:02 -070023 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040024 for api in apis {
25 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080026 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040027 }
28 }
29
30 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080031 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040032
David Tolnay7db73692019-10-20 14:51:12 -040033 out.next_section();
34 for name in &namespace {
35 writeln!(out, "namespace {} {{", name);
36 }
37
David Tolnay7db73692019-10-20 14:51:12 -040038 out.next_section();
39 for api in apis {
40 match api {
41 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080042 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
43 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040044 _ => {}
45 }
46 }
47
David Tolnayf94bef12020-04-17 14:46:42 -070048 let mut methods_for_type = HashMap::new();
49 for api in apis {
50 if let Api::RustFunction(efn) = api {
51 if let Some(receiver) = &efn.sig.receiver {
52 methods_for_type
53 .entry(&receiver.ident)
54 .or_insert_with(Vec::new)
55 .push(efn);
56 }
57 }
58 }
Joel Galenson968738f2020-04-15 14:19:33 -070059
David Tolnay7db73692019-10-20 14:51:12 -040060 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070061 match api {
62 Api::Struct(strct) => {
63 out.next_section();
64 write_struct(out, strct);
65 }
David Tolnayc1fe0052020-04-17 15:15:06 -070066 Api::RustType(ety) => {
67 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070068 out.next_section();
69 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070070 }
David Tolnayc1fe0052020-04-17 15:15:06 -070071 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070072 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040073 }
74 }
75
76 if !header {
77 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070078 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040079 for api in apis {
80 let (efn, write): (_, fn(_, _, _)) = match api {
81 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
82 Api::RustFunction(efn) => (efn, write_rust_function_decl),
83 _ => continue,
84 };
85 out.next_section();
86 write(out, efn, types);
87 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080088 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90
91 for api in apis {
92 if let Api::RustFunction(efn) = api {
93 out.next_section();
94 write_rust_function_shim(out, efn, types);
95 }
96 }
97
98 out.next_section();
99 for name in namespace.iter().rev() {
100 writeln!(out, "}} // namespace {}", name);
101 }
102
103 if !header {
104 out.next_section();
105 write_generic_instantiations(out, types);
106 }
107
David Tolnay9c68b1a2020-03-06 11:12:55 -0800108 out.prepend(out.include.to_string());
109
David Tolnay7db73692019-10-20 14:51:12 -0400110 out_file
111}
112
113fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400114 for ty in types {
115 match ty {
116 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700117 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
118 | Some(I64) => out.include.cstdint = true,
119 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800120 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700121 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400122 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800123 Type::RustBox(_) => out.include.type_traits = true,
124 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700125 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400126 _ => {}
127 }
128 }
David Tolnay7db73692019-10-20 14:51:12 -0400129}
130
David Tolnayf51447e2020-03-06 14:14:27 -0800131fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700132 let mut needs_rust_string = false;
133 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700134 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400135 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700136 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700137 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400138 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700139 match ty {
140 Type::RustBox(_) => {
141 out.include.type_traits = true;
142 needs_rust_box = true;
143 }
144 Type::Str(_) => {
145 out.include.cstdint = true;
146 out.include.string = true;
147 needs_rust_str = true;
148 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700149 Type::Fn(_) => {
150 needs_rust_fn = true;
151 }
David Tolnay4770b472020-04-14 16:32:59 -0700152 Type::Slice(_) | Type::SliceRefU8(_) => {
153 needs_rust_slice = true;
154 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700155 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700156 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700157 needs_rust_isize = true;
158 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700159 ty if ty == RustString => {
160 out.include.array = true;
161 out.include.cstdint = true;
162 out.include.string = true;
163 needs_rust_string = true;
164 }
165 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400166 }
167 }
168
David Tolnayb7a7cb62020-03-17 21:18:40 -0700169 let mut needs_rust_error = false;
170 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800171 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800172 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700173 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800174 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 match api {
176 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700177 if efn.throws {
178 needs_trycatch = true;
179 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700180 for arg in &efn.args {
181 if arg.ty == RustString {
182 needs_unsafe_bitcopy = true;
183 break;
184 }
David Tolnay09011c32020-03-06 14:40:28 -0800185 }
186 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 Api::RustFunction(efn) if !out.header => {
188 if efn.throws {
189 out.include.exception = true;
190 needs_rust_error = true;
191 }
192 for arg in &efn.args {
193 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
194 needs_manually_drop = true;
195 break;
196 }
197 }
198 if let Some(ret) = &efn.ret {
199 if types.needs_indirect_abi(ret) {
200 needs_maybe_uninit = true;
201 }
David Tolnayf51447e2020-03-06 14:14:27 -0800202 }
203 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700204 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800205 }
206 }
207
David Tolnay750755e2020-03-01 13:04:08 -0800208 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700209 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800210
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211 if needs_rust_string
212 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700213 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700215 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700216 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700217 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 || needs_unsafe_bitcopy
219 || needs_manually_drop
220 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700221 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700222 {
David Tolnay736cbca2020-03-11 16:49:18 -0700223 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800224 }
225
David Tolnayd1402742020-03-25 22:21:42 -0700226 if needs_rust_string {
227 out.next_section();
228 writeln!(out, "struct unsafe_bitcopy_t;");
229 }
230
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
232 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700233 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700235 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700236 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700237 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700238 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800239
240 if needs_manually_drop {
241 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700242 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800243 writeln!(out, "template <typename T>");
244 writeln!(out, "union ManuallyDrop {{");
245 writeln!(out, " T value;");
246 writeln!(
247 out,
248 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
249 );
250 writeln!(out, " ~ManuallyDrop() {{}}");
251 writeln!(out, "}};");
252 }
253
David Tolnay09011c32020-03-06 14:40:28 -0800254 if needs_maybe_uninit {
255 out.next_section();
256 writeln!(out, "template <typename T>");
257 writeln!(out, "union MaybeUninit {{");
258 writeln!(out, " T value;");
259 writeln!(out, " MaybeUninit() {{}}");
260 writeln!(out, " ~MaybeUninit() {{}}");
261 writeln!(out, "}};");
262 }
263
David Tolnay3e3e0af2020-03-17 22:42:49 -0700264 out.end_block("namespace cxxbridge02");
265
David Tolnay5d121442020-03-17 22:14:40 -0700266 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700267 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700268 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700269 out.include.type_traits = true;
270 out.include.utility = true;
271 writeln!(out, "class missing {{}};");
272 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700273 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700274 writeln!(out, "template <typename Try, typename Fail>");
275 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700276 writeln!(
277 out,
David Tolnay04722332020-03-18 11:31:54 -0700278 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700279 );
David Tolnay04722332020-03-18 11:31:54 -0700280 writeln!(out, " missing>::value>::type");
281 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700282 writeln!(out, " func();");
283 writeln!(out, "}} catch (const ::std::exception &e) {{");
284 writeln!(out, " fail(e.what());");
285 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700286 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700287 }
288
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800289 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400290}
291
David Tolnayb7a7cb62020-03-17 21:18:40 -0700292fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700293 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700294 if needed {
295 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700296 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700297 if !line.trim_start().starts_with("//") {
298 writeln!(out, "{}", line);
299 }
300 }
301 }
302}
303
David Tolnay7db73692019-10-20 14:51:12 -0400304fn write_struct(out: &mut OutFile, strct: &Struct) {
305 for line in strct.doc.to_string().lines() {
306 writeln!(out, "//{}", line);
307 }
308 writeln!(out, "struct {} final {{", strct.ident);
309 for field in &strct.fields {
310 write!(out, " ");
311 write_type_space(out, &field.ty);
312 writeln!(out, "{};", field.ident);
313 }
314 writeln!(out, "}};");
315}
316
317fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
318 writeln!(out, "struct {};", ident);
319}
320
David Tolnay8861bee2020-01-20 18:39:24 -0800321fn write_struct_using(out: &mut OutFile, ident: &Ident) {
322 writeln!(out, "using {} = {};", ident, ident);
323}
324
David Tolnayc1fe0052020-04-17 15:15:06 -0700325fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700326 for line in ety.doc.to_string().lines() {
327 writeln!(out, "//{}", line);
328 }
329 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700330 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700331 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700332 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700333 write!(out, " ");
334 let sig = &method.sig;
335 let local_name = method.ident.to_string();
336 write_rust_function_shim_decl(out, &local_name, sig, None, false);
337 writeln!(out, ";");
338 }
339 writeln!(out, "}};");
340}
341
David Tolnayebef4a22020-03-17 15:33:47 -0700342fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
343 let mut has_cxx_throws = false;
344 for api in apis {
345 if let Api::CxxFunction(efn) = api {
346 if efn.throws {
347 has_cxx_throws = true;
348 break;
349 }
350 }
351 }
352
353 if has_cxx_throws {
354 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700355 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700356 out,
357 "const char *cxxbridge02$exception(const char *, size_t);",
358 );
359 }
360}
361
David Tolnay7db73692019-10-20 14:51:12 -0400362fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700363 if efn.throws {
364 write!(out, "::rust::Str::Repr ");
365 } else {
David Tolnay99642622020-03-25 13:07:35 -0700366 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700367 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700368 let receiver_type = match &efn.receiver {
369 Some(base) => base.ident.to_string(),
370 None => "_".to_string(),
371 };
David Tolnay46a54e72020-04-17 14:48:21 -0700372 write!(
373 out,
374 "{}cxxbridge02${}${}(",
375 out.namespace, receiver_type, efn.ident
376 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700377 if let Some(base) = &efn.receiver {
David Tolnay26804bd2020-04-19 20:06:51 -0700378 write!(out, "{} *self$", base.ident);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700379 }
David Tolnay7db73692019-10-20 14:51:12 -0400380 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700381 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400382 write!(out, ", ");
383 }
David Tolnaya46a2372020-03-06 10:03:48 -0800384 if arg.ty == RustString {
385 write!(out, "const ");
386 }
David Tolnay7db73692019-10-20 14:51:12 -0400387 write_extern_arg(out, arg, types);
388 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700389 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400390 if indirect_return {
391 if !efn.args.is_empty() {
392 write!(out, ", ");
393 }
David Tolnay99642622020-03-25 13:07:35 -0700394 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400395 write!(out, "*return$");
396 }
397 writeln!(out, ") noexcept {{");
398 write!(out, " ");
399 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700400 match &efn.receiver {
401 None => write!(out, "(*{}$)(", efn.ident),
402 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
403 }
David Tolnay7db73692019-10-20 14:51:12 -0400404 for (i, arg) in efn.args.iter().enumerate() {
405 if i > 0 {
406 write!(out, ", ");
407 }
408 write_type(out, &arg.ty);
409 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700410 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700411 if let Some(receiver) = &efn.receiver {
412 if receiver.mutability.is_none() {
413 write!(out, " const");
414 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700415 }
416 write!(out, " = ");
417 match &efn.receiver {
418 None => write!(out, "{}", efn.ident),
419 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
420 }
421 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400422 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700423 if efn.throws {
424 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700425 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700426 writeln!(out, " [&] {{");
427 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700428 }
David Tolnay7db73692019-10-20 14:51:12 -0400429 if indirect_return {
430 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700431 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400432 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700433 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400434 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700435 }
436 match &efn.ret {
437 Some(Type::Ref(_)) => write!(out, "&"),
438 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700439 Some(Type::SliceRefU8(_)) if !indirect_return => {
440 write!(out, "::rust::Slice<uint8_t>::Repr(")
441 }
David Tolnay99642622020-03-25 13:07:35 -0700442 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400443 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700444 match &efn.receiver {
445 None => write!(out, "{}$(", efn.ident),
David Tolnay26804bd2020-04-19 20:06:51 -0700446 Some(_) => write!(out, "(self$->*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 }
David Tolnay7db73692019-10-20 14:51:12 -0400448 for (i, arg) in efn.args.iter().enumerate() {
449 if i > 0 {
450 write!(out, ", ");
451 }
452 if let Type::RustBox(_) = &arg.ty {
453 write_type(out, &arg.ty);
454 write!(out, "::from_raw({})", arg.ident);
455 } else if let Type::UniquePtr(_) = &arg.ty {
456 write_type(out, &arg.ty);
457 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800458 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800459 write!(
460 out,
461 "::rust::String(::rust::unsafe_bitcopy, *{})",
462 arg.ident,
463 );
David Tolnay7db73692019-10-20 14:51:12 -0400464 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700465 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800466 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400467 } else {
468 write!(out, "{}", arg.ident);
469 }
470 }
471 write!(out, ")");
472 match &efn.ret {
473 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
474 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700475 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400476 _ => {}
477 }
478 if indirect_return {
479 write!(out, ")");
480 }
481 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700482 if efn.throws {
483 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700484 writeln!(out, " throw$.ptr = nullptr;");
485 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700486 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700487 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700488 writeln!(
489 out,
David Tolnay5d121442020-03-17 22:14:40 -0700490 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700491 );
David Tolnay5d121442020-03-17 22:14:40 -0700492 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700493 writeln!(out, " return throw$;");
494 }
David Tolnay7db73692019-10-20 14:51:12 -0400495 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700496 for arg in &efn.args {
497 if let Type::Fn(f) = &arg.ty {
498 let var = &arg.ident;
499 write_function_pointer_trampoline(out, efn, var, f, types);
500 }
501 }
502}
503
504fn write_function_pointer_trampoline(
505 out: &mut OutFile,
506 efn: &ExternFn,
507 var: &Ident,
508 f: &Signature,
509 types: &Types,
510) {
511 out.next_section();
512 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
513 let indirect_call = true;
514 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
515
516 out.next_section();
517 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
518 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400519}
520
521fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700522 let receiver_type = match &efn.receiver {
523 Some(base) => base.ident.to_string(),
524 None => "_".to_string(),
525 };
David Tolnay46a54e72020-04-17 14:48:21 -0700526 let link_name = format!(
527 "{}cxxbridge02${}${}",
528 out.namespace, receiver_type, efn.ident
529 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700530 let indirect_call = false;
531 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
532}
533
534fn write_rust_function_decl_impl(
535 out: &mut OutFile,
536 link_name: &str,
537 sig: &Signature,
538 types: &Types,
539 indirect_call: bool,
540) {
541 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700542 write!(out, "::rust::Str::Repr ");
543 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700544 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700545 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700546 write!(out, "{}(", link_name);
547 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700548 if let Some(base) = &sig.receiver {
David Tolnay26804bd2020-04-19 20:06:51 -0700549 write!(out, "{} &self$", base.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700550 needs_comma = true;
551 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 for arg in &sig.args {
553 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400554 write!(out, ", ");
555 }
556 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700557 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400558 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700559 if indirect_return(sig, types) {
560 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400561 write!(out, ", ");
562 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700563 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400564 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 needs_comma = true;
566 }
567 if indirect_call {
568 if needs_comma {
569 write!(out, ", ");
570 }
571 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400572 }
573 writeln!(out, ") noexcept;");
574}
575
576fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400577 for line in efn.doc.to_string().lines() {
578 writeln!(out, "//{}", line);
579 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 let local_name = efn.ident.to_string();
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700581 let receiver_type = match &efn.receiver {
582 Some(base) => base.ident.to_string(),
583 None => "_".to_string(),
584 };
David Tolnay46a54e72020-04-17 14:48:21 -0700585 let invoke = format!(
586 "{}cxxbridge02${}${}",
587 out.namespace, receiver_type, efn.ident
588 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700589 let indirect_call = false;
590 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
591}
592
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700593fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 out: &mut OutFile,
595 local_name: &str,
596 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700597 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 indirect_call: bool,
599) {
600 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700601 if let Some(base) = receiver {
602 write!(out, "{}::", base.ident);
603 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700604 write!(out, "{}(", local_name);
605 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400606 if i > 0 {
607 write!(out, ", ");
608 }
609 write_type_space(out, &arg.ty);
610 write!(out, "{}", arg.ident);
611 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 if indirect_call {
613 if !sig.args.is_empty() {
614 write!(out, ", ");
615 }
616 write!(out, "void *extern$");
617 }
David Tolnay1e548172020-03-16 13:37:09 -0700618 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700620 write!(out, " noexcept");
621 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700622}
623
624fn write_rust_function_shim_impl(
625 out: &mut OutFile,
626 local_name: &str,
627 sig: &Signature,
628 types: &Types,
629 invoke: &str,
630 indirect_call: bool,
631) {
632 if out.header && sig.receiver.is_some() {
633 // We've already defined this inside the struct.
634 return;
635 }
636 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400637 if out.header {
638 writeln!(out, ";");
639 } else {
640 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700641 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800642 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700643 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800644 write!(out, " ::rust::ManuallyDrop<");
645 write_type(out, &arg.ty);
646 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
647 }
648 }
David Tolnay7db73692019-10-20 14:51:12 -0400649 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400651 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800652 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800654 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400655 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800658 match ret {
659 Type::RustBox(_) => {
660 write_type(out, ret);
661 write!(out, "::from_raw(");
662 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800663 Type::UniquePtr(_) => {
664 write_type(out, ret);
665 write!(out, "(");
666 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800667 Type::Ref(_) => write!(out, "*"),
668 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800669 }
David Tolnay7db73692019-10-20 14:51:12 -0400670 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700671 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700672 write!(out, "::rust::Str::Repr error$ = ");
673 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700674 write!(out, "{}(", invoke);
David Tolnay9b5cfe12020-04-19 21:11:50 -0700675 if sig.receiver.is_some() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700676 write!(out, "*this");
677 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700678 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700679 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400680 write!(out, ", ");
681 }
David Tolnaybaae4432020-03-01 20:20:10 -0800682 match &arg.ty {
683 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700684 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800685 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
686 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400687 }
688 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800689 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800690 Type::RustBox(_) => write!(out, ".into_raw()"),
691 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700692 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800693 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800694 _ => {}
695 }
David Tolnay7db73692019-10-20 14:51:12 -0400696 }
697 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700698 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400699 write!(out, ", ");
700 }
David Tolnay09011c32020-03-06 14:40:28 -0800701 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400702 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700703 if indirect_call {
704 if !sig.args.is_empty() || indirect_return {
705 write!(out, ", ");
706 }
707 write!(out, "extern$");
708 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800709 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700710 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800711 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800712 write!(out, ")");
713 }
714 }
715 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700716 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700717 writeln!(out, " if (error$.ptr) {{");
718 writeln!(out, " throw ::rust::Error(error$);");
719 writeln!(out, " }}");
720 }
David Tolnay7db73692019-10-20 14:51:12 -0400721 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700722 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800723 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400724 }
725 writeln!(out, "}}");
726 }
727}
728
729fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
730 match ty {
731 None => write!(out, "void "),
732 Some(ty) => write_type_space(out, ty),
733 }
734}
735
David Tolnay75dca2e2020-03-25 20:17:52 -0700736fn indirect_return(sig: &Signature, types: &Types) -> bool {
737 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700738 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700739 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700740}
741
David Tolnay99642622020-03-25 13:07:35 -0700742fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
743 match ty {
744 Type::RustBox(ty) | Type::UniquePtr(ty) => {
745 write_type_space(out, &ty.inner);
746 write!(out, "*");
747 }
748 Type::Ref(ty) => {
749 if ty.mutability.is_none() {
750 write!(out, "const ");
751 }
752 write_type(out, &ty.inner);
753 write!(out, " *");
754 }
755 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700756 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700757 _ => write_type(out, ty),
758 }
759}
760
761fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
762 write_indirect_return_type(out, ty);
763 match ty {
764 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700765 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700766 _ => write_space_after_type(out, ty),
767 }
768}
769
770fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400771 match ty {
772 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
773 write_type_space(out, &ty.inner);
774 write!(out, "*");
775 }
David Tolnay4a441222020-01-25 16:24:27 -0800776 Some(Type::Ref(ty)) => {
777 if ty.mutability.is_none() {
778 write!(out, "const ");
779 }
780 write_type(out, &ty.inner);
781 write!(out, " *");
782 }
David Tolnay750755e2020-03-01 13:04:08 -0800783 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700784 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400785 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
786 _ => write_return_type(out, ty),
787 }
788}
789
790fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
791 match &arg.ty {
792 Type::RustBox(ty) | Type::UniquePtr(ty) => {
793 write_type_space(out, &ty.inner);
794 write!(out, "*");
795 }
David Tolnay750755e2020-03-01 13:04:08 -0800796 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700797 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400798 _ => write_type_space(out, &arg.ty),
799 }
800 if types.needs_indirect_abi(&arg.ty) {
801 write!(out, "*");
802 }
803 write!(out, "{}", arg.ident);
804}
805
806fn write_type(out: &mut OutFile, ty: &Type) {
807 match ty {
808 Type::Ident(ident) => match Atom::from(ident) {
809 Some(Bool) => write!(out, "bool"),
810 Some(U8) => write!(out, "uint8_t"),
811 Some(U16) => write!(out, "uint16_t"),
812 Some(U32) => write!(out, "uint32_t"),
813 Some(U64) => write!(out, "uint64_t"),
814 Some(Usize) => write!(out, "size_t"),
815 Some(I8) => write!(out, "int8_t"),
816 Some(I16) => write!(out, "int16_t"),
817 Some(I32) => write!(out, "int32_t"),
818 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700819 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700820 Some(F32) => write!(out, "float"),
821 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800822 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800823 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400824 None => write!(out, "{}", ident),
825 },
826 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800827 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400828 write_type(out, &ty.inner);
829 write!(out, ">");
830 }
831 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800832 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400833 write_type(out, &ptr.inner);
834 write!(out, ">");
835 }
836 Type::Ref(r) => {
837 if r.mutability.is_none() {
838 write!(out, "const ");
839 }
840 write_type(out, &r.inner);
841 write!(out, " &");
842 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700843 Type::Slice(_) => {
844 // For now, only U8 slices are supported, which are covered separately below
845 unreachable!()
846 }
David Tolnay7db73692019-10-20 14:51:12 -0400847 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800848 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400849 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700850 Type::SliceRefU8(_) => {
851 write!(out, "::rust::Slice<uint8_t>");
852 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 Type::Fn(f) => {
854 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
855 match &f.ret {
856 Some(ret) => write_type(out, ret),
857 None => write!(out, "void"),
858 }
859 write!(out, "(");
860 for (i, arg) in f.args.iter().enumerate() {
861 if i > 0 {
862 write!(out, ", ");
863 }
864 write_type(out, &arg.ty);
865 }
866 write!(out, ")>");
867 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700868 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400869 }
870}
871
872fn write_type_space(out: &mut OutFile, ty: &Type) {
873 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700874 write_space_after_type(out, ty);
875}
876
877fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400878 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700879 Type::Ident(_)
880 | Type::RustBox(_)
881 | Type::UniquePtr(_)
882 | Type::Str(_)
883 | Type::SliceRefU8(_)
884 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400885 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700886 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400887 }
888}
889
890fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
891 fn allow_unique_ptr(ident: &Ident) -> bool {
892 Atom::from(ident).is_none()
893 }
894
895 out.begin_block("extern \"C\"");
896 for ty in types {
897 if let Type::RustBox(ty) = ty {
898 if let Type::Ident(inner) = &ty.inner {
899 out.next_section();
900 write_rust_box_extern(out, inner);
901 }
902 } else if let Type::UniquePtr(ptr) = ty {
903 if let Type::Ident(inner) = &ptr.inner {
904 if allow_unique_ptr(inner) {
905 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700906 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400907 }
908 }
909 }
910 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800911 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400912
David Tolnay750755e2020-03-01 13:04:08 -0800913 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700914 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400915 for ty in types {
916 if let Type::RustBox(ty) = ty {
917 if let Type::Ident(inner) = &ty.inner {
918 write_rust_box_impl(out, inner);
919 }
920 }
921 }
David Tolnay8c730492020-03-13 01:29:06 -0700922 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800923 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400924}
925
926fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
927 let mut inner = String::new();
928 for name in &out.namespace {
929 inner += name;
930 inner += "::";
931 }
932 inner += &ident.to_string();
933 let instance = inner.replace("::", "$");
934
David Tolnay8c730492020-03-13 01:29:06 -0700935 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
936 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400937 writeln!(
938 out,
David Tolnay8c730492020-03-13 01:29:06 -0700939 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400940 instance, inner,
941 );
942 writeln!(
943 out,
David Tolnay8c730492020-03-13 01:29:06 -0700944 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400945 instance, inner,
946 );
David Tolnay8c730492020-03-13 01:29:06 -0700947 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400948}
949
950fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
951 let mut inner = String::new();
952 for name in &out.namespace {
953 inner += name;
954 inner += "::";
955 }
956 inner += &ident.to_string();
957 let instance = inner.replace("::", "$");
958
959 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800960 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700961 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400962 writeln!(out, "}}");
963
964 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800965 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700966 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400967 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400968}
969
David Tolnay53838912020-04-09 20:56:44 -0700970fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700971 out.include.utility = true;
972
David Tolnay7db73692019-10-20 14:51:12 -0400973 let mut inner = String::new();
974 for name in &out.namespace {
975 inner += name;
976 inner += "::";
977 }
978 inner += &ident.to_string();
979 let instance = inner.replace("::", "$");
980
David Tolnay8c730492020-03-13 01:29:06 -0700981 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
982 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400983 writeln!(
984 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800985 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400986 inner,
987 );
988 writeln!(
989 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800990 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400991 inner,
992 );
993 writeln!(
994 out,
David Tolnay8c730492020-03-13 01:29:06 -0700995 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400996 instance, inner,
997 );
David Tolnay7e219b82020-03-01 13:14:51 -0800998 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400999 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001000 if types.structs.contains_key(ident) {
1001 writeln!(
1002 out,
1003 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1004 instance, inner, inner,
1005 );
1006 writeln!(
1007 out,
1008 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1009 inner, inner,
1010 );
1011 writeln!(out, "}}");
1012 }
David Tolnay7db73692019-10-20 14:51:12 -04001013 writeln!(
1014 out,
David Tolnay8c730492020-03-13 01:29:06 -07001015 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001016 instance, inner, inner,
1017 );
David Tolnay7e219b82020-03-01 13:14:51 -08001018 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001019 writeln!(out, "}}");
1020 writeln!(
1021 out,
David Tolnay8c730492020-03-13 01:29:06 -07001022 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001023 inner, instance, inner,
1024 );
1025 writeln!(out, " return ptr.get();");
1026 writeln!(out, "}}");
1027 writeln!(
1028 out,
David Tolnay8c730492020-03-13 01:29:06 -07001029 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001030 inner, instance, inner,
1031 );
1032 writeln!(out, " return ptr.release();");
1033 writeln!(out, "}}");
1034 writeln!(
1035 out,
David Tolnay8c730492020-03-13 01:29:06 -07001036 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001037 instance, inner,
1038 );
1039 writeln!(out, " ptr->~unique_ptr();");
1040 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001041 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001042}