blob: 7a57111da14268781890b955286a18b54d63bc26 [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, *};
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, ")");
411 match &efn.receiver {
David Tolnay46a54e72020-04-17 14:48:21 -0700412 Some(Receiver {
413 mutability: None,
414 ident: _,
415 }) => write!(out, " const"),
416 _ => {}
Joel Galenson3d4f6122020-04-07 15:54:05 -0700417 }
418 write!(out, " = ");
419 match &efn.receiver {
420 None => write!(out, "{}", efn.ident),
421 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
422 }
423 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400424 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700425 if efn.throws {
426 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700427 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700428 writeln!(out, " [&] {{");
429 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700430 }
David Tolnay7db73692019-10-20 14:51:12 -0400431 if indirect_return {
432 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700433 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400434 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700435 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400436 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700437 }
438 match &efn.ret {
439 Some(Type::Ref(_)) => write!(out, "&"),
440 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700441 Some(Type::SliceRefU8(_)) if !indirect_return => {
442 write!(out, "::rust::Slice<uint8_t>::Repr(")
443 }
David Tolnay99642622020-03-25 13:07:35 -0700444 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400445 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 match &efn.receiver {
447 None => write!(out, "{}$(", efn.ident),
David Tolnay26804bd2020-04-19 20:06:51 -0700448 Some(_) => write!(out, "(self$->*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700449 }
David Tolnay7db73692019-10-20 14:51:12 -0400450 for (i, arg) in efn.args.iter().enumerate() {
451 if i > 0 {
452 write!(out, ", ");
453 }
454 if let Type::RustBox(_) = &arg.ty {
455 write_type(out, &arg.ty);
456 write!(out, "::from_raw({})", arg.ident);
457 } else if let Type::UniquePtr(_) = &arg.ty {
458 write_type(out, &arg.ty);
459 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800460 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800461 write!(
462 out,
463 "::rust::String(::rust::unsafe_bitcopy, *{})",
464 arg.ident,
465 );
David Tolnay7db73692019-10-20 14:51:12 -0400466 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700467 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800468 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400469 } else {
470 write!(out, "{}", arg.ident);
471 }
472 }
473 write!(out, ")");
474 match &efn.ret {
475 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
476 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700477 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400478 _ => {}
479 }
480 if indirect_return {
481 write!(out, ")");
482 }
483 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700484 if efn.throws {
485 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700486 writeln!(out, " throw$.ptr = nullptr;");
487 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700488 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700489 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700490 writeln!(
491 out,
David Tolnay5d121442020-03-17 22:14:40 -0700492 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700493 );
David Tolnay5d121442020-03-17 22:14:40 -0700494 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700495 writeln!(out, " return throw$;");
496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700498 for arg in &efn.args {
499 if let Type::Fn(f) = &arg.ty {
500 let var = &arg.ident;
501 write_function_pointer_trampoline(out, efn, var, f, types);
502 }
503 }
504}
505
506fn write_function_pointer_trampoline(
507 out: &mut OutFile,
508 efn: &ExternFn,
509 var: &Ident,
510 f: &Signature,
511 types: &Types,
512) {
513 out.next_section();
514 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
515 let indirect_call = true;
516 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
517
518 out.next_section();
519 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
520 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400521}
522
523fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700524 let receiver_type = match &efn.receiver {
525 Some(base) => base.ident.to_string(),
526 None => "_".to_string(),
527 };
David Tolnay46a54e72020-04-17 14:48:21 -0700528 let link_name = format!(
529 "{}cxxbridge02${}${}",
530 out.namespace, receiver_type, efn.ident
531 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700532 let indirect_call = false;
533 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
534}
535
536fn write_rust_function_decl_impl(
537 out: &mut OutFile,
538 link_name: &str,
539 sig: &Signature,
540 types: &Types,
541 indirect_call: bool,
542) {
543 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700544 write!(out, "::rust::Str::Repr ");
545 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700546 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700547 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 write!(out, "{}(", link_name);
549 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700550 if let Some(base) = &sig.receiver {
David Tolnay26804bd2020-04-19 20:06:51 -0700551 write!(out, "{} &self$", base.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700552 needs_comma = true;
553 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 for arg in &sig.args {
555 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400556 write!(out, ", ");
557 }
558 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700559 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400560 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700561 if indirect_return(sig, types) {
562 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400563 write!(out, ", ");
564 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400566 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700567 needs_comma = true;
568 }
569 if indirect_call {
570 if needs_comma {
571 write!(out, ", ");
572 }
573 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400574 }
575 writeln!(out, ") noexcept;");
576}
577
578fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400579 for line in efn.doc.to_string().lines() {
580 writeln!(out, "//{}", line);
581 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 let local_name = efn.ident.to_string();
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700583 let receiver_type = match &efn.receiver {
584 Some(base) => base.ident.to_string(),
585 None => "_".to_string(),
586 };
David Tolnay46a54e72020-04-17 14:48:21 -0700587 let invoke = format!(
588 "{}cxxbridge02${}${}",
589 out.namespace, receiver_type, efn.ident
590 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700591 let indirect_call = false;
592 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
593}
594
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700595fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700596 out: &mut OutFile,
597 local_name: &str,
598 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700599 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 indirect_call: bool,
601) {
602 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700603 if let Some(base) = receiver {
604 write!(out, "{}::", base.ident);
605 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700606 write!(out, "{}(", local_name);
607 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400608 if i > 0 {
609 write!(out, ", ");
610 }
611 write_type_space(out, &arg.ty);
612 write!(out, "{}", arg.ident);
613 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700614 if indirect_call {
615 if !sig.args.is_empty() {
616 write!(out, ", ");
617 }
618 write!(out, "void *extern$");
619 }
David Tolnay1e548172020-03-16 13:37:09 -0700620 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700621 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700622 write!(out, " noexcept");
623 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700624}
625
626fn write_rust_function_shim_impl(
627 out: &mut OutFile,
628 local_name: &str,
629 sig: &Signature,
630 types: &Types,
631 invoke: &str,
632 indirect_call: bool,
633) {
634 if out.header && sig.receiver.is_some() {
635 // We've already defined this inside the struct.
636 return;
637 }
638 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400639 if out.header {
640 writeln!(out, ";");
641 } else {
642 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700643 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800644 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700645 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800646 write!(out, " ::rust::ManuallyDrop<");
647 write_type(out, &arg.ty);
648 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
649 }
650 }
David Tolnay7db73692019-10-20 14:51:12 -0400651 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400653 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800654 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700655 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800656 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400657 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700658 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400659 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800660 match ret {
661 Type::RustBox(_) => {
662 write_type(out, ret);
663 write!(out, "::from_raw(");
664 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800665 Type::UniquePtr(_) => {
666 write_type(out, ret);
667 write!(out, "(");
668 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800669 Type::Ref(_) => write!(out, "*"),
670 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800671 }
David Tolnay7db73692019-10-20 14:51:12 -0400672 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700673 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700674 write!(out, "::rust::Str::Repr error$ = ");
675 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 write!(out, "{}(", invoke);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700677 if let Some(_) = &sig.receiver {
678 write!(out, "*this");
679 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700680 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700681 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400682 write!(out, ", ");
683 }
David Tolnaybaae4432020-03-01 20:20:10 -0800684 match &arg.ty {
685 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700686 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800687 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
688 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400689 }
690 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800691 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800692 Type::RustBox(_) => write!(out, ".into_raw()"),
693 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700694 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800695 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800696 _ => {}
697 }
David Tolnay7db73692019-10-20 14:51:12 -0400698 }
699 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700700 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400701 write!(out, ", ");
702 }
David Tolnay09011c32020-03-06 14:40:28 -0800703 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400704 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700705 if indirect_call {
706 if !sig.args.is_empty() || indirect_return {
707 write!(out, ", ");
708 }
709 write!(out, "extern$");
710 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800711 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700712 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800713 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800714 write!(out, ")");
715 }
716 }
717 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700718 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700719 writeln!(out, " if (error$.ptr) {{");
720 writeln!(out, " throw ::rust::Error(error$);");
721 writeln!(out, " }}");
722 }
David Tolnay7db73692019-10-20 14:51:12 -0400723 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700724 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800725 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400726 }
727 writeln!(out, "}}");
728 }
729}
730
731fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
732 match ty {
733 None => write!(out, "void "),
734 Some(ty) => write_type_space(out, ty),
735 }
736}
737
David Tolnay75dca2e2020-03-25 20:17:52 -0700738fn indirect_return(sig: &Signature, types: &Types) -> bool {
739 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700740 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700741 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700742}
743
David Tolnay99642622020-03-25 13:07:35 -0700744fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
745 match ty {
746 Type::RustBox(ty) | Type::UniquePtr(ty) => {
747 write_type_space(out, &ty.inner);
748 write!(out, "*");
749 }
750 Type::Ref(ty) => {
751 if ty.mutability.is_none() {
752 write!(out, "const ");
753 }
754 write_type(out, &ty.inner);
755 write!(out, " *");
756 }
757 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700758 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700759 _ => write_type(out, ty),
760 }
761}
762
763fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
764 write_indirect_return_type(out, ty);
765 match ty {
766 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700767 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700768 _ => write_space_after_type(out, ty),
769 }
770}
771
772fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400773 match ty {
774 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
775 write_type_space(out, &ty.inner);
776 write!(out, "*");
777 }
David Tolnay4a441222020-01-25 16:24:27 -0800778 Some(Type::Ref(ty)) => {
779 if ty.mutability.is_none() {
780 write!(out, "const ");
781 }
782 write_type(out, &ty.inner);
783 write!(out, " *");
784 }
David Tolnay750755e2020-03-01 13:04:08 -0800785 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700786 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400787 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
788 _ => write_return_type(out, ty),
789 }
790}
791
792fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
793 match &arg.ty {
794 Type::RustBox(ty) | Type::UniquePtr(ty) => {
795 write_type_space(out, &ty.inner);
796 write!(out, "*");
797 }
David Tolnay750755e2020-03-01 13:04:08 -0800798 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700799 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400800 _ => write_type_space(out, &arg.ty),
801 }
802 if types.needs_indirect_abi(&arg.ty) {
803 write!(out, "*");
804 }
805 write!(out, "{}", arg.ident);
806}
807
808fn write_type(out: &mut OutFile, ty: &Type) {
809 match ty {
810 Type::Ident(ident) => match Atom::from(ident) {
811 Some(Bool) => write!(out, "bool"),
812 Some(U8) => write!(out, "uint8_t"),
813 Some(U16) => write!(out, "uint16_t"),
814 Some(U32) => write!(out, "uint32_t"),
815 Some(U64) => write!(out, "uint64_t"),
816 Some(Usize) => write!(out, "size_t"),
817 Some(I8) => write!(out, "int8_t"),
818 Some(I16) => write!(out, "int16_t"),
819 Some(I32) => write!(out, "int32_t"),
820 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700821 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700822 Some(F32) => write!(out, "float"),
823 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800824 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800825 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400826 None => write!(out, "{}", ident),
827 },
828 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800829 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400830 write_type(out, &ty.inner);
831 write!(out, ">");
832 }
833 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800834 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400835 write_type(out, &ptr.inner);
836 write!(out, ">");
837 }
838 Type::Ref(r) => {
839 if r.mutability.is_none() {
840 write!(out, "const ");
841 }
842 write_type(out, &r.inner);
843 write!(out, " &");
844 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700845 Type::Slice(_) => {
846 // For now, only U8 slices are supported, which are covered separately below
847 unreachable!()
848 }
David Tolnay7db73692019-10-20 14:51:12 -0400849 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800850 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400851 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700852 Type::SliceRefU8(_) => {
853 write!(out, "::rust::Slice<uint8_t>");
854 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700855 Type::Fn(f) => {
856 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
857 match &f.ret {
858 Some(ret) => write_type(out, ret),
859 None => write!(out, "void"),
860 }
861 write!(out, "(");
862 for (i, arg) in f.args.iter().enumerate() {
863 if i > 0 {
864 write!(out, ", ");
865 }
866 write_type(out, &arg.ty);
867 }
868 write!(out, ")>");
869 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700870 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400871 }
872}
873
874fn write_type_space(out: &mut OutFile, ty: &Type) {
875 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700876 write_space_after_type(out, ty);
877}
878
879fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400880 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700881 Type::Ident(_)
882 | Type::RustBox(_)
883 | Type::UniquePtr(_)
884 | Type::Str(_)
885 | Type::SliceRefU8(_)
886 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400887 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700888 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400889 }
890}
891
892fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
893 fn allow_unique_ptr(ident: &Ident) -> bool {
894 Atom::from(ident).is_none()
895 }
896
897 out.begin_block("extern \"C\"");
898 for ty in types {
899 if let Type::RustBox(ty) = ty {
900 if let Type::Ident(inner) = &ty.inner {
901 out.next_section();
902 write_rust_box_extern(out, inner);
903 }
904 } else if let Type::UniquePtr(ptr) = ty {
905 if let Type::Ident(inner) = &ptr.inner {
906 if allow_unique_ptr(inner) {
907 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700908 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400909 }
910 }
911 }
912 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800913 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400914
David Tolnay750755e2020-03-01 13:04:08 -0800915 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700916 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400917 for ty in types {
918 if let Type::RustBox(ty) = ty {
919 if let Type::Ident(inner) = &ty.inner {
920 write_rust_box_impl(out, inner);
921 }
922 }
923 }
David Tolnay8c730492020-03-13 01:29:06 -0700924 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800925 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400926}
927
928fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
929 let mut inner = String::new();
930 for name in &out.namespace {
931 inner += name;
932 inner += "::";
933 }
934 inner += &ident.to_string();
935 let instance = inner.replace("::", "$");
936
David Tolnay8c730492020-03-13 01:29:06 -0700937 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
938 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400939 writeln!(
940 out,
David Tolnay8c730492020-03-13 01:29:06 -0700941 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400942 instance, inner,
943 );
944 writeln!(
945 out,
David Tolnay8c730492020-03-13 01:29:06 -0700946 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400947 instance, inner,
948 );
David Tolnay8c730492020-03-13 01:29:06 -0700949 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400950}
951
952fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
953 let mut inner = String::new();
954 for name in &out.namespace {
955 inner += name;
956 inner += "::";
957 }
958 inner += &ident.to_string();
959 let instance = inner.replace("::", "$");
960
961 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800962 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700963 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400964 writeln!(out, "}}");
965
966 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800967 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700968 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400969 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400970}
971
David Tolnay53838912020-04-09 20:56:44 -0700972fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700973 out.include.utility = true;
974
David Tolnay7db73692019-10-20 14:51:12 -0400975 let mut inner = String::new();
976 for name in &out.namespace {
977 inner += name;
978 inner += "::";
979 }
980 inner += &ident.to_string();
981 let instance = inner.replace("::", "$");
982
David Tolnay8c730492020-03-13 01:29:06 -0700983 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
984 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400985 writeln!(
986 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800987 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400988 inner,
989 );
990 writeln!(
991 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800992 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400993 inner,
994 );
995 writeln!(
996 out,
David Tolnay8c730492020-03-13 01:29:06 -0700997 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400998 instance, inner,
999 );
David Tolnay7e219b82020-03-01 13:14:51 -08001000 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001001 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001002 if types.structs.contains_key(ident) {
1003 writeln!(
1004 out,
1005 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1006 instance, inner, inner,
1007 );
1008 writeln!(
1009 out,
1010 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1011 inner, inner,
1012 );
1013 writeln!(out, "}}");
1014 }
David Tolnay7db73692019-10-20 14:51:12 -04001015 writeln!(
1016 out,
David Tolnay8c730492020-03-13 01:29:06 -07001017 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001018 instance, inner, inner,
1019 );
David Tolnay7e219b82020-03-01 13:14:51 -08001020 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001021 writeln!(out, "}}");
1022 writeln!(
1023 out,
David Tolnay8c730492020-03-13 01:29:06 -07001024 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001025 inner, instance, inner,
1026 );
1027 writeln!(out, " return ptr.get();");
1028 writeln!(out, "}}");
1029 writeln!(
1030 out,
David Tolnay8c730492020-03-13 01:29:06 -07001031 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001032 inner, instance, inner,
1033 );
1034 writeln!(out, " return ptr.release();");
1035 writeln!(out, "}}");
1036 writeln!(
1037 out,
David Tolnay8c730492020-03-13 01:29:06 -07001038 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001039 instance, inner,
1040 );
1041 writeln!(out, " ptr->~unique_ptr();");
1042 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001043 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001044}