blob: a77c65850363fc08d1a77612730ba1a8c850a69e [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 Tolnay46a54e72020-04-17 14:48:21 -070066 Api::RustType(ety) => match methods_for_type.get(&ety.ident) {
67 Some(methods) => {
68 out.next_section();
69 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070070 }
David Tolnay46a54e72020-04-17 14:48:21 -070071 _ => {}
72 },
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040074 }
75 }
76
77 if !header {
78 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070079 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040080 for api in apis {
81 let (efn, write): (_, fn(_, _, _)) = match api {
82 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
83 Api::RustFunction(efn) => (efn, write_rust_function_decl),
84 _ => continue,
85 };
86 out.next_section();
87 write(out, efn, types);
88 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080089 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040090 }
91
92 for api in apis {
93 if let Api::RustFunction(efn) = api {
94 out.next_section();
95 write_rust_function_shim(out, efn, types);
96 }
97 }
98
99 out.next_section();
100 for name in namespace.iter().rev() {
101 writeln!(out, "}} // namespace {}", name);
102 }
103
104 if !header {
105 out.next_section();
106 write_generic_instantiations(out, types);
107 }
108
David Tolnay9c68b1a2020-03-06 11:12:55 -0800109 out.prepend(out.include.to_string());
110
David Tolnay7db73692019-10-20 14:51:12 -0400111 out_file
112}
113
114fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400115 for ty in types {
116 match ty {
117 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700118 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
119 | Some(I64) => out.include.cstdint = true,
120 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800121 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700122 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400123 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800124 Type::RustBox(_) => out.include.type_traits = true,
125 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700126 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400127 _ => {}
128 }
129 }
David Tolnay7db73692019-10-20 14:51:12 -0400130}
131
David Tolnayf51447e2020-03-06 14:14:27 -0800132fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700133 let mut needs_rust_string = false;
134 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700135 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400136 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700137 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700138 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400139 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700140 match ty {
141 Type::RustBox(_) => {
142 out.include.type_traits = true;
143 needs_rust_box = true;
144 }
145 Type::Str(_) => {
146 out.include.cstdint = true;
147 out.include.string = true;
148 needs_rust_str = true;
149 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700150 Type::Fn(_) => {
151 needs_rust_fn = true;
152 }
David Tolnay4770b472020-04-14 16:32:59 -0700153 Type::Slice(_) | Type::SliceRefU8(_) => {
154 needs_rust_slice = true;
155 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700156 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700157 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700158 needs_rust_isize = true;
159 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700160 ty if ty == RustString => {
161 out.include.array = true;
162 out.include.cstdint = true;
163 out.include.string = true;
164 needs_rust_string = true;
165 }
166 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400167 }
168 }
169
David Tolnayb7a7cb62020-03-17 21:18:40 -0700170 let mut needs_rust_error = false;
171 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800172 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800173 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700174 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800175 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700176 match api {
177 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700178 if efn.throws {
179 needs_trycatch = true;
180 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 for arg in &efn.args {
182 if arg.ty == RustString {
183 needs_unsafe_bitcopy = true;
184 break;
185 }
David Tolnay09011c32020-03-06 14:40:28 -0800186 }
187 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 Api::RustFunction(efn) if !out.header => {
189 if efn.throws {
190 out.include.exception = true;
191 needs_rust_error = true;
192 }
193 for arg in &efn.args {
194 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
195 needs_manually_drop = true;
196 break;
197 }
198 }
199 if let Some(ret) = &efn.ret {
200 if types.needs_indirect_abi(ret) {
201 needs_maybe_uninit = true;
202 }
David Tolnayf51447e2020-03-06 14:14:27 -0800203 }
204 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700205 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800206 }
207 }
208
David Tolnay750755e2020-03-01 13:04:08 -0800209 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700210 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800211
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 if needs_rust_string
213 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700214 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700216 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700218 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 || needs_unsafe_bitcopy
220 || needs_manually_drop
221 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700222 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 {
David Tolnay736cbca2020-03-11 16:49:18 -0700224 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800225 }
226
David Tolnayd1402742020-03-25 22:21:42 -0700227 if needs_rust_string {
228 out.next_section();
229 writeln!(out, "struct unsafe_bitcopy_t;");
230 }
231
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
233 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700234 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700236 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700238 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800240
241 if needs_manually_drop {
242 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700243 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800244 writeln!(out, "template <typename T>");
245 writeln!(out, "union ManuallyDrop {{");
246 writeln!(out, " T value;");
247 writeln!(
248 out,
249 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
250 );
251 writeln!(out, " ~ManuallyDrop() {{}}");
252 writeln!(out, "}};");
253 }
254
David Tolnay09011c32020-03-06 14:40:28 -0800255 if needs_maybe_uninit {
256 out.next_section();
257 writeln!(out, "template <typename T>");
258 writeln!(out, "union MaybeUninit {{");
259 writeln!(out, " T value;");
260 writeln!(out, " MaybeUninit() {{}}");
261 writeln!(out, " ~MaybeUninit() {{}}");
262 writeln!(out, "}};");
263 }
264
David Tolnay3e3e0af2020-03-17 22:42:49 -0700265 out.end_block("namespace cxxbridge02");
266
David Tolnay5d121442020-03-17 22:14:40 -0700267 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700268 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700269 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700270 out.include.type_traits = true;
271 out.include.utility = true;
272 writeln!(out, "class missing {{}};");
273 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700274 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700275 writeln!(out, "template <typename Try, typename Fail>");
276 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700277 writeln!(
278 out,
David Tolnay04722332020-03-18 11:31:54 -0700279 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700280 );
David Tolnay04722332020-03-18 11:31:54 -0700281 writeln!(out, " missing>::value>::type");
282 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700283 writeln!(out, " func();");
284 writeln!(out, "}} catch (const ::std::exception &e) {{");
285 writeln!(out, " fail(e.what());");
286 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700288 }
289
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800290 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400291}
292
David Tolnayb7a7cb62020-03-17 21:18:40 -0700293fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700294 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700295 if needed {
296 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700297 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700298 if !line.trim_start().starts_with("//") {
299 writeln!(out, "{}", line);
300 }
301 }
302 }
303}
304
David Tolnay7db73692019-10-20 14:51:12 -0400305fn write_struct(out: &mut OutFile, strct: &Struct) {
306 for line in strct.doc.to_string().lines() {
307 writeln!(out, "//{}", line);
308 }
309 writeln!(out, "struct {} final {{", strct.ident);
310 for field in &strct.fields {
311 write!(out, " ");
312 write_type_space(out, &field.ty);
313 writeln!(out, "{};", field.ident);
314 }
315 writeln!(out, "}};");
316}
317
318fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
319 writeln!(out, "struct {};", ident);
320}
321
David Tolnay8861bee2020-01-20 18:39:24 -0800322fn write_struct_using(out: &mut OutFile, ident: &Ident) {
323 writeln!(out, "using {} = {};", ident, ident);
324}
325
Joel Galenson968738f2020-04-15 14:19:33 -0700326fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &Vec<&ExternFn>) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700327 for line in ety.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700331 writeln!(out, " {}() = delete;", ety.ident);
332 writeln!(out, " {}(const {}&) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700333 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700334 write!(out, " ");
335 let sig = &method.sig;
336 let local_name = method.ident.to_string();
337 write_rust_function_shim_decl(out, &local_name, sig, None, false);
338 writeln!(out, ";");
339 }
340 writeln!(out, "}};");
341}
342
David Tolnayebef4a22020-03-17 15:33:47 -0700343fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
344 let mut has_cxx_throws = false;
345 for api in apis {
346 if let Api::CxxFunction(efn) = api {
347 if efn.throws {
348 has_cxx_throws = true;
349 break;
350 }
351 }
352 }
353
354 if has_cxx_throws {
355 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700356 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700357 out,
358 "const char *cxxbridge02$exception(const char *, size_t);",
359 );
360 }
361}
362
David Tolnay7db73692019-10-20 14:51:12 -0400363fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700364 if efn.throws {
365 write!(out, "::rust::Str::Repr ");
366 } else {
David Tolnay99642622020-03-25 13:07:35 -0700367 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700368 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700369 let receiver_type = match &efn.receiver {
370 Some(base) => base.ident.to_string(),
371 None => "_".to_string(),
372 };
David Tolnay46a54e72020-04-17 14:48:21 -0700373 write!(
374 out,
375 "{}cxxbridge02${}${}(",
376 out.namespace, receiver_type, efn.ident
377 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700378 if let Some(base) = &efn.receiver {
379 write!(out, "{} *__receiver$", base.ident);
380 }
David Tolnay7db73692019-10-20 14:51:12 -0400381 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700382 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400383 write!(out, ", ");
384 }
David Tolnaya46a2372020-03-06 10:03:48 -0800385 if arg.ty == RustString {
386 write!(out, "const ");
387 }
David Tolnay7db73692019-10-20 14:51:12 -0400388 write_extern_arg(out, arg, types);
389 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700390 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400391 if indirect_return {
392 if !efn.args.is_empty() {
393 write!(out, ", ");
394 }
David Tolnay99642622020-03-25 13:07:35 -0700395 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400396 write!(out, "*return$");
397 }
398 writeln!(out, ") noexcept {{");
399 write!(out, " ");
400 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700401 match &efn.receiver {
402 None => write!(out, "(*{}$)(", efn.ident),
403 Some(base) => write!(out, "({}::*{}$)(", base.ident, efn.ident),
404 }
David Tolnay7db73692019-10-20 14:51:12 -0400405 for (i, arg) in efn.args.iter().enumerate() {
406 if i > 0 {
407 write!(out, ", ");
408 }
409 write_type(out, &arg.ty);
410 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700411 write!(out, ")");
412 match &efn.receiver {
David Tolnay46a54e72020-04-17 14:48:21 -0700413 Some(Receiver {
414 mutability: None,
415 ident: _,
416 }) => write!(out, " const"),
417 _ => {}
Joel Galenson3d4f6122020-04-07 15:54:05 -0700418 }
419 write!(out, " = ");
420 match &efn.receiver {
421 None => write!(out, "{}", efn.ident),
422 Some(base) => write!(out, "&{}::{}", base.ident, efn.ident),
423 }
424 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400425 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700426 if efn.throws {
427 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700428 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700429 writeln!(out, " [&] {{");
430 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700431 }
David Tolnay7db73692019-10-20 14:51:12 -0400432 if indirect_return {
433 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700434 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400435 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700436 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400437 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700438 }
439 match &efn.ret {
440 Some(Type::Ref(_)) => write!(out, "&"),
441 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700442 Some(Type::SliceRefU8(_)) if !indirect_return => {
443 write!(out, "::rust::Slice<uint8_t>::Repr(")
444 }
David Tolnay99642622020-03-25 13:07:35 -0700445 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400446 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 match &efn.receiver {
448 None => write!(out, "{}$(", efn.ident),
449 Some(_) => write!(out, "(__receiver$->*{}$)(", efn.ident),
450 }
David Tolnay7db73692019-10-20 14:51:12 -0400451 for (i, arg) in efn.args.iter().enumerate() {
452 if i > 0 {
453 write!(out, ", ");
454 }
455 if let Type::RustBox(_) = &arg.ty {
456 write_type(out, &arg.ty);
457 write!(out, "::from_raw({})", arg.ident);
458 } else if let Type::UniquePtr(_) = &arg.ty {
459 write_type(out, &arg.ty);
460 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800461 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800462 write!(
463 out,
464 "::rust::String(::rust::unsafe_bitcopy, *{})",
465 arg.ident,
466 );
David Tolnay7db73692019-10-20 14:51:12 -0400467 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700468 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800469 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400470 } else {
471 write!(out, "{}", arg.ident);
472 }
473 }
474 write!(out, ")");
475 match &efn.ret {
476 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
477 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700478 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400479 _ => {}
480 }
481 if indirect_return {
482 write!(out, ")");
483 }
484 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700485 if efn.throws {
486 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700487 writeln!(out, " throw$.ptr = nullptr;");
488 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700489 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700490 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700491 writeln!(
492 out,
David Tolnay5d121442020-03-17 22:14:40 -0700493 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700494 );
David Tolnay5d121442020-03-17 22:14:40 -0700495 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700496 writeln!(out, " return throw$;");
497 }
David Tolnay7db73692019-10-20 14:51:12 -0400498 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700499 for arg in &efn.args {
500 if let Type::Fn(f) = &arg.ty {
501 let var = &arg.ident;
502 write_function_pointer_trampoline(out, efn, var, f, types);
503 }
504 }
505}
506
507fn write_function_pointer_trampoline(
508 out: &mut OutFile,
509 efn: &ExternFn,
510 var: &Ident,
511 f: &Signature,
512 types: &Types,
513) {
514 out.next_section();
515 let r_trampoline = format!("{}cxxbridge02${}${}$1", out.namespace, efn.ident, var);
516 let indirect_call = true;
517 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
518
519 out.next_section();
520 let c_trampoline = format!("{}cxxbridge02${}${}$0", out.namespace, efn.ident, var);
521 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400522}
523
524fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700525 let receiver_type = match &efn.receiver {
526 Some(base) => base.ident.to_string(),
527 None => "_".to_string(),
528 };
David Tolnay46a54e72020-04-17 14:48:21 -0700529 let link_name = format!(
530 "{}cxxbridge02${}${}",
531 out.namespace, receiver_type, efn.ident
532 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700533 let indirect_call = false;
534 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
535}
536
537fn write_rust_function_decl_impl(
538 out: &mut OutFile,
539 link_name: &str,
540 sig: &Signature,
541 types: &Types,
542 indirect_call: bool,
543) {
544 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700545 write!(out, "::rust::Str::Repr ");
546 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700547 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700548 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700549 write!(out, "{}(", link_name);
550 let mut needs_comma = false;
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700551 if let Some(base) = &sig.receiver {
552 write!(out, "{} &__receiver$", base.ident);
553 needs_comma = true;
554 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700555 for arg in &sig.args {
556 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400557 write!(out, ", ");
558 }
559 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400561 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700562 if indirect_return(sig, types) {
563 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400564 write!(out, ", ");
565 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700566 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400567 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700568 needs_comma = true;
569 }
570 if indirect_call {
571 if needs_comma {
572 write!(out, ", ");
573 }
574 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400575 }
576 writeln!(out, ") noexcept;");
577}
578
579fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400580 for line in efn.doc.to_string().lines() {
581 writeln!(out, "//{}", line);
582 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700583 let local_name = efn.ident.to_string();
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700584 let receiver_type = match &efn.receiver {
585 Some(base) => base.ident.to_string(),
586 None => "_".to_string(),
587 };
David Tolnay46a54e72020-04-17 14:48:21 -0700588 let invoke = format!(
589 "{}cxxbridge02${}${}",
590 out.namespace, receiver_type, efn.ident
591 );
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 let indirect_call = false;
593 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
594}
595
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700596fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 out: &mut OutFile,
598 local_name: &str,
599 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700600 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700601 indirect_call: bool,
602) {
603 write_return_type(out, &sig.ret);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700604 if let Some(base) = receiver {
605 write!(out, "{}::", base.ident);
606 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 write!(out, "{}(", local_name);
608 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400609 if i > 0 {
610 write!(out, ", ");
611 }
612 write_type_space(out, &arg.ty);
613 write!(out, "{}", arg.ident);
614 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700615 if indirect_call {
616 if !sig.args.is_empty() {
617 write!(out, ", ");
618 }
619 write!(out, "void *extern$");
620 }
David Tolnay1e548172020-03-16 13:37:09 -0700621 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700622 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700623 write!(out, " noexcept");
624 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700625}
626
627fn write_rust_function_shim_impl(
628 out: &mut OutFile,
629 local_name: &str,
630 sig: &Signature,
631 types: &Types,
632 invoke: &str,
633 indirect_call: bool,
634) {
635 if out.header && sig.receiver.is_some() {
636 // We've already defined this inside the struct.
637 return;
638 }
639 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400640 if out.header {
641 writeln!(out, ";");
642 } else {
643 writeln!(out, " {{");
David Tolnay75dca2e2020-03-25 20:17:52 -0700644 for arg in &sig.args {
David Tolnayf51447e2020-03-06 14:14:27 -0800645 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700646 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800647 write!(out, " ::rust::ManuallyDrop<");
648 write_type(out, &arg.ty);
649 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
650 }
651 }
David Tolnay7db73692019-10-20 14:51:12 -0400652 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 let indirect_return = indirect_return(sig, types);
David Tolnay7db73692019-10-20 14:51:12 -0400654 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800655 write!(out, "::rust::MaybeUninit<");
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800657 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400658 write!(out, " ");
David Tolnay75dca2e2020-03-25 20:17:52 -0700659 } else if let Some(ret) = &sig.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400660 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800661 match ret {
662 Type::RustBox(_) => {
663 write_type(out, ret);
664 write!(out, "::from_raw(");
665 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800666 Type::UniquePtr(_) => {
667 write_type(out, ret);
668 write!(out, "(");
669 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800670 Type::Ref(_) => write!(out, "*"),
671 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800672 }
David Tolnay7db73692019-10-20 14:51:12 -0400673 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700674 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700675 write!(out, "::rust::Str::Repr error$ = ");
676 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700677 write!(out, "{}(", invoke);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700678 if let Some(_) = &sig.receiver {
679 write!(out, "*this");
680 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700681 for (i, arg) in sig.args.iter().enumerate() {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700682 if i > 0 || sig.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400683 write!(out, ", ");
684 }
David Tolnaybaae4432020-03-01 20:20:10 -0800685 match &arg.ty {
686 Type::Str(_) => write!(out, "::rust::Str::Repr("),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700687 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaybaae4432020-03-01 20:20:10 -0800688 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
689 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400690 }
691 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800692 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800693 Type::RustBox(_) => write!(out, ".into_raw()"),
694 Type::UniquePtr(_) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700695 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800696 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800697 _ => {}
698 }
David Tolnay7db73692019-10-20 14:51:12 -0400699 }
700 if indirect_return {
David Tolnay75dca2e2020-03-25 20:17:52 -0700701 if !sig.args.is_empty() {
David Tolnay7db73692019-10-20 14:51:12 -0400702 write!(out, ", ");
703 }
David Tolnay09011c32020-03-06 14:40:28 -0800704 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400705 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700706 if indirect_call {
707 if !sig.args.is_empty() || indirect_return {
708 write!(out, ", ");
709 }
710 write!(out, "extern$");
711 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800712 write!(out, ")");
David Tolnay75dca2e2020-03-25 20:17:52 -0700713 if let Some(ret) = &sig.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800714 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800715 write!(out, ")");
716 }
717 }
718 writeln!(out, ";");
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700720 writeln!(out, " if (error$.ptr) {{");
721 writeln!(out, " throw ::rust::Error(error$);");
722 writeln!(out, " }}");
723 }
David Tolnay7db73692019-10-20 14:51:12 -0400724 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700725 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800726 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400727 }
728 writeln!(out, "}}");
729 }
730}
731
732fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
733 match ty {
734 None => write!(out, "void "),
735 Some(ty) => write_type_space(out, ty),
736 }
737}
738
David Tolnay75dca2e2020-03-25 20:17:52 -0700739fn indirect_return(sig: &Signature, types: &Types) -> bool {
740 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700741 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700742 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700743}
744
David Tolnay99642622020-03-25 13:07:35 -0700745fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
746 match ty {
747 Type::RustBox(ty) | Type::UniquePtr(ty) => {
748 write_type_space(out, &ty.inner);
749 write!(out, "*");
750 }
751 Type::Ref(ty) => {
752 if ty.mutability.is_none() {
753 write!(out, "const ");
754 }
755 write_type(out, &ty.inner);
756 write!(out, " *");
757 }
758 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700759 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700760 _ => write_type(out, ty),
761 }
762}
763
764fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
765 write_indirect_return_type(out, ty);
766 match ty {
767 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700768 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700769 _ => write_space_after_type(out, ty),
770 }
771}
772
773fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400774 match ty {
775 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
776 write_type_space(out, &ty.inner);
777 write!(out, "*");
778 }
David Tolnay4a441222020-01-25 16:24:27 -0800779 Some(Type::Ref(ty)) => {
780 if ty.mutability.is_none() {
781 write!(out, "const ");
782 }
783 write_type(out, &ty.inner);
784 write!(out, " *");
785 }
David Tolnay750755e2020-03-01 13:04:08 -0800786 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700787 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400788 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
789 _ => write_return_type(out, ty),
790 }
791}
792
793fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
794 match &arg.ty {
795 Type::RustBox(ty) | Type::UniquePtr(ty) => {
796 write_type_space(out, &ty.inner);
797 write!(out, "*");
798 }
David Tolnay750755e2020-03-01 13:04:08 -0800799 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700800 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400801 _ => write_type_space(out, &arg.ty),
802 }
803 if types.needs_indirect_abi(&arg.ty) {
804 write!(out, "*");
805 }
806 write!(out, "{}", arg.ident);
807}
808
809fn write_type(out: &mut OutFile, ty: &Type) {
810 match ty {
811 Type::Ident(ident) => match Atom::from(ident) {
812 Some(Bool) => write!(out, "bool"),
813 Some(U8) => write!(out, "uint8_t"),
814 Some(U16) => write!(out, "uint16_t"),
815 Some(U32) => write!(out, "uint32_t"),
816 Some(U64) => write!(out, "uint64_t"),
817 Some(Usize) => write!(out, "size_t"),
818 Some(I8) => write!(out, "int8_t"),
819 Some(I16) => write!(out, "int16_t"),
820 Some(I32) => write!(out, "int32_t"),
821 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700822 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700823 Some(F32) => write!(out, "float"),
824 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800825 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800826 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400827 None => write!(out, "{}", ident),
828 },
829 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800830 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400831 write_type(out, &ty.inner);
832 write!(out, ">");
833 }
834 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800835 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400836 write_type(out, &ptr.inner);
837 write!(out, ">");
838 }
839 Type::Ref(r) => {
840 if r.mutability.is_none() {
841 write!(out, "const ");
842 }
843 write_type(out, &r.inner);
844 write!(out, " &");
845 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700846 Type::Slice(_) => {
847 // For now, only U8 slices are supported, which are covered separately below
848 unreachable!()
849 }
David Tolnay7db73692019-10-20 14:51:12 -0400850 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800851 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400852 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700853 Type::SliceRefU8(_) => {
854 write!(out, "::rust::Slice<uint8_t>");
855 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700856 Type::Fn(f) => {
857 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
858 match &f.ret {
859 Some(ret) => write_type(out, ret),
860 None => write!(out, "void"),
861 }
862 write!(out, "(");
863 for (i, arg) in f.args.iter().enumerate() {
864 if i > 0 {
865 write!(out, ", ");
866 }
867 write_type(out, &arg.ty);
868 }
869 write!(out, ")>");
870 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700871 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400872 }
873}
874
875fn write_type_space(out: &mut OutFile, ty: &Type) {
876 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700877 write_space_after_type(out, ty);
878}
879
880fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400881 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700882 Type::Ident(_)
883 | Type::RustBox(_)
884 | Type::UniquePtr(_)
885 | Type::Str(_)
886 | Type::SliceRefU8(_)
887 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400888 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700889 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400890 }
891}
892
893fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
894 fn allow_unique_ptr(ident: &Ident) -> bool {
895 Atom::from(ident).is_none()
896 }
897
898 out.begin_block("extern \"C\"");
899 for ty in types {
900 if let Type::RustBox(ty) = ty {
901 if let Type::Ident(inner) = &ty.inner {
902 out.next_section();
903 write_rust_box_extern(out, inner);
904 }
905 } else if let Type::UniquePtr(ptr) = ty {
906 if let Type::Ident(inner) = &ptr.inner {
907 if allow_unique_ptr(inner) {
908 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700909 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400910 }
911 }
912 }
913 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800914 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400915
David Tolnay750755e2020-03-01 13:04:08 -0800916 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700917 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400918 for ty in types {
919 if let Type::RustBox(ty) = ty {
920 if let Type::Ident(inner) = &ty.inner {
921 write_rust_box_impl(out, inner);
922 }
923 }
924 }
David Tolnay8c730492020-03-13 01:29:06 -0700925 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800926 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400927}
928
929fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
930 let mut inner = String::new();
931 for name in &out.namespace {
932 inner += name;
933 inner += "::";
934 }
935 inner += &ident.to_string();
936 let instance = inner.replace("::", "$");
937
David Tolnay8c730492020-03-13 01:29:06 -0700938 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
939 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400940 writeln!(
941 out,
David Tolnay8c730492020-03-13 01:29:06 -0700942 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400943 instance, inner,
944 );
945 writeln!(
946 out,
David Tolnay8c730492020-03-13 01:29:06 -0700947 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400948 instance, inner,
949 );
David Tolnay8c730492020-03-13 01:29:06 -0700950 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400951}
952
953fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
954 let mut inner = String::new();
955 for name in &out.namespace {
956 inner += name;
957 inner += "::";
958 }
959 inner += &ident.to_string();
960 let instance = inner.replace("::", "$");
961
962 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800963 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700964 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400965 writeln!(out, "}}");
966
967 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800968 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700969 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400970 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400971}
972
David Tolnay53838912020-04-09 20:56:44 -0700973fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700974 out.include.utility = true;
975
David Tolnay7db73692019-10-20 14:51:12 -0400976 let mut inner = String::new();
977 for name in &out.namespace {
978 inner += name;
979 inner += "::";
980 }
981 inner += &ident.to_string();
982 let instance = inner.replace("::", "$");
983
David Tolnay8c730492020-03-13 01:29:06 -0700984 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
985 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400986 writeln!(
987 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800988 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400989 inner,
990 );
991 writeln!(
992 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800993 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400994 inner,
995 );
996 writeln!(
997 out,
David Tolnay8c730492020-03-13 01:29:06 -0700998 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400999 instance, inner,
1000 );
David Tolnay7e219b82020-03-01 13:14:51 -08001001 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001002 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001003 if types.structs.contains_key(ident) {
1004 writeln!(
1005 out,
1006 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1007 instance, inner, inner,
1008 );
1009 writeln!(
1010 out,
1011 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1012 inner, inner,
1013 );
1014 writeln!(out, "}}");
1015 }
David Tolnay7db73692019-10-20 14:51:12 -04001016 writeln!(
1017 out,
David Tolnay8c730492020-03-13 01:29:06 -07001018 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001019 instance, inner, inner,
1020 );
David Tolnay7e219b82020-03-01 13:14:51 -08001021 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001022 writeln!(out, "}}");
1023 writeln!(
1024 out,
David Tolnay8c730492020-03-13 01:29:06 -07001025 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001026 inner, instance, inner,
1027 );
1028 writeln!(out, " return ptr.get();");
1029 writeln!(out, "}}");
1030 writeln!(
1031 out,
David Tolnay8c730492020-03-13 01:29:06 -07001032 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001033 inner, instance, inner,
1034 );
1035 writeln!(out, " return ptr.release();");
1036 writeln!(out, "}}");
1037 writeln!(
1038 out,
David Tolnay8c730492020-03-13 01:29:06 -07001039 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001040 instance, inner,
1041 );
1042 writeln!(out, " ptr->~unique_ptr();");
1043 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001044 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001045}