blob: 8f3ecc0ff2411ac6c465688340e840a76d4d7b14 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
David Tolnay3caa50a2020-04-19 21:25:34 -07006use crate::syntax::{
7 mangle, Api, ExternFn, ExternType, Receiver, Signature, Struct, Type, Types, Var,
8};
David Tolnay7db73692019-10-20 14:51:12 -04009use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -070010use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnay33d30292020-03-18 18:02:02 -070012pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070013 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070014 apis: &[Api],
15 types: &Types,
16 opt: Opt,
17 header: bool,
18) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040019 let mut out_file = OutFile::new(namespace.clone(), header);
20 let out = &mut out_file;
21
22 if header {
23 writeln!(out, "#pragma once");
24 }
25
David Tolnay33d30292020-03-18 18:02:02 -070026 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040027 for api in apis {
28 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080029 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040030 }
31 }
32
33 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080034 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040035
David Tolnay7db73692019-10-20 14:51:12 -040036 out.next_section();
37 for name in &namespace {
38 writeln!(out, "namespace {} {{", name);
39 }
40
David Tolnay7db73692019-10-20 14:51:12 -040041 out.next_section();
42 for api in apis {
43 match api {
44 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080045 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
46 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040047 _ => {}
48 }
49 }
50
David Tolnayf94bef12020-04-17 14:46:42 -070051 let mut methods_for_type = HashMap::new();
52 for api in apis {
53 if let Api::RustFunction(efn) = api {
54 if let Some(receiver) = &efn.sig.receiver {
55 methods_for_type
56 .entry(&receiver.ident)
57 .or_insert_with(Vec::new)
58 .push(efn);
59 }
60 }
61 }
Joel Galenson968738f2020-04-15 14:19:33 -070062
David Tolnay7db73692019-10-20 14:51:12 -040063 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070064 match api {
65 Api::Struct(strct) => {
66 out.next_section();
67 write_struct(out, strct);
68 }
David Tolnayc1fe0052020-04-17 15:15:06 -070069 Api::RustType(ety) => {
70 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070071 out.next_section();
72 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 }
David Tolnayc1fe0052020-04-17 15:15:06 -070074 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77 }
78
79 if !header {
80 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070081 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040082 for api in apis {
83 let (efn, write): (_, fn(_, _, _)) = match api {
84 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
85 Api::RustFunction(efn) => (efn, write_rust_function_decl),
86 _ => continue,
87 };
88 out.next_section();
89 write(out, efn, types);
90 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080091 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040092 }
93
94 for api in apis {
95 if let Api::RustFunction(efn) = api {
96 out.next_section();
97 write_rust_function_shim(out, efn, types);
98 }
99 }
100
101 out.next_section();
102 for name in namespace.iter().rev() {
103 writeln!(out, "}} // namespace {}", name);
104 }
105
106 if !header {
107 out.next_section();
108 write_generic_instantiations(out, types);
109 }
110
David Tolnay9c68b1a2020-03-06 11:12:55 -0800111 out.prepend(out.include.to_string());
112
David Tolnay7db73692019-10-20 14:51:12 -0400113 out_file
114}
115
116fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400117 for ty in types {
118 match ty {
119 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700120 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
121 | Some(I64) => out.include.cstdint = true,
122 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800123 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700124 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400125 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800126 Type::RustBox(_) => out.include.type_traits = true,
127 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700128 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400129 _ => {}
130 }
131 }
David Tolnay7db73692019-10-20 14:51:12 -0400132}
133
David Tolnayf51447e2020-03-06 14:14:27 -0800134fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700135 let mut needs_rust_string = false;
136 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700137 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400138 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700139 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700140 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400141 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 match ty {
143 Type::RustBox(_) => {
144 out.include.type_traits = true;
145 needs_rust_box = true;
146 }
147 Type::Str(_) => {
148 out.include.cstdint = true;
149 out.include.string = true;
150 needs_rust_str = true;
151 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700152 Type::Fn(_) => {
153 needs_rust_fn = true;
154 }
David Tolnay4770b472020-04-14 16:32:59 -0700155 Type::Slice(_) | Type::SliceRefU8(_) => {
156 needs_rust_slice = true;
157 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700158 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700159 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700160 needs_rust_isize = true;
161 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 ty if ty == RustString => {
163 out.include.array = true;
164 out.include.cstdint = true;
165 out.include.string = true;
166 needs_rust_string = true;
167 }
168 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400169 }
170 }
171
David Tolnayb7a7cb62020-03-17 21:18:40 -0700172 let mut needs_rust_error = false;
173 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800174 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800175 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700176 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800177 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 match api {
179 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700180 if efn.throws {
181 needs_trycatch = true;
182 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 for arg in &efn.args {
184 if arg.ty == RustString {
185 needs_unsafe_bitcopy = true;
186 break;
187 }
David Tolnay09011c32020-03-06 14:40:28 -0800188 }
189 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700190 Api::RustFunction(efn) if !out.header => {
191 if efn.throws {
192 out.include.exception = true;
193 needs_rust_error = true;
194 }
195 for arg in &efn.args {
196 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
197 needs_manually_drop = true;
198 break;
199 }
200 }
201 if let Some(ret) = &efn.ret {
202 if types.needs_indirect_abi(ret) {
203 needs_maybe_uninit = true;
204 }
David Tolnayf51447e2020-03-06 14:14:27 -0800205 }
206 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800208 }
209 }
210
David Tolnay750755e2020-03-01 13:04:08 -0800211 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700212 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800213
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 if needs_rust_string
215 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700216 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700218 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700220 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 || needs_unsafe_bitcopy
222 || needs_manually_drop
223 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700224 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 {
David Tolnay736cbca2020-03-11 16:49:18 -0700226 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800227 }
228
David Tolnayd1402742020-03-25 22:21:42 -0700229 if needs_rust_string {
230 out.next_section();
231 writeln!(out, "struct unsafe_bitcopy_t;");
232 }
233
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
235 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700236 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700238 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700240 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800242
243 if needs_manually_drop {
244 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700245 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800246 writeln!(out, "template <typename T>");
247 writeln!(out, "union ManuallyDrop {{");
248 writeln!(out, " T value;");
249 writeln!(
250 out,
251 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
252 );
253 writeln!(out, " ~ManuallyDrop() {{}}");
254 writeln!(out, "}};");
255 }
256
David Tolnay09011c32020-03-06 14:40:28 -0800257 if needs_maybe_uninit {
258 out.next_section();
259 writeln!(out, "template <typename T>");
260 writeln!(out, "union MaybeUninit {{");
261 writeln!(out, " T value;");
262 writeln!(out, " MaybeUninit() {{}}");
263 writeln!(out, " ~MaybeUninit() {{}}");
264 writeln!(out, "}};");
265 }
266
David Tolnay3e3e0af2020-03-17 22:42:49 -0700267 out.end_block("namespace cxxbridge02");
268
David Tolnay5d121442020-03-17 22:14:40 -0700269 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700270 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700271 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700272 out.include.type_traits = true;
273 out.include.utility = true;
274 writeln!(out, "class missing {{}};");
275 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700276 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700277 writeln!(out, "template <typename Try, typename Fail>");
278 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700279 writeln!(
280 out,
David Tolnay04722332020-03-18 11:31:54 -0700281 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700282 );
David Tolnay04722332020-03-18 11:31:54 -0700283 writeln!(out, " missing>::value>::type");
284 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700285 writeln!(out, " func();");
286 writeln!(out, "}} catch (const ::std::exception &e) {{");
287 writeln!(out, " fail(e.what());");
288 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700290 }
291
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800292 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400293}
294
David Tolnayb7a7cb62020-03-17 21:18:40 -0700295fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700296 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700297 if needed {
298 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700299 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700300 if !line.trim_start().starts_with("//") {
301 writeln!(out, "{}", line);
302 }
303 }
304 }
305}
306
David Tolnay7db73692019-10-20 14:51:12 -0400307fn write_struct(out: &mut OutFile, strct: &Struct) {
308 for line in strct.doc.to_string().lines() {
309 writeln!(out, "//{}", line);
310 }
311 writeln!(out, "struct {} final {{", strct.ident);
312 for field in &strct.fields {
313 write!(out, " ");
314 write_type_space(out, &field.ty);
315 writeln!(out, "{};", field.ident);
316 }
317 writeln!(out, "}};");
318}
319
320fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
321 writeln!(out, "struct {};", ident);
322}
323
David Tolnay8861bee2020-01-20 18:39:24 -0800324fn write_struct_using(out: &mut OutFile, ident: &Ident) {
325 writeln!(out, "using {} = {};", ident, ident);
326}
327
David Tolnayc1fe0052020-04-17 15:15:06 -0700328fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700329 for line in ety.doc.to_string().lines() {
330 writeln!(out, "//{}", line);
331 }
332 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700333 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700334 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700335 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700336 write!(out, " ");
337 let sig = &method.sig;
David Tolnay891061b2020-04-19 22:42:33 -0700338 let local_name = Symbol::from(&method.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700339 write_rust_function_shim_decl(out, &local_name, sig, None, false);
340 writeln!(out, ";");
341 }
342 writeln!(out, "}};");
343}
344
David Tolnayebef4a22020-03-17 15:33:47 -0700345fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
346 let mut has_cxx_throws = false;
347 for api in apis {
348 if let Api::CxxFunction(efn) = api {
349 if efn.throws {
350 has_cxx_throws = true;
351 break;
352 }
353 }
354 }
355
356 if has_cxx_throws {
357 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700358 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700359 out,
360 "const char *cxxbridge02$exception(const char *, size_t);",
361 );
362 }
363}
364
David Tolnay7db73692019-10-20 14:51:12 -0400365fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700366 if efn.throws {
367 write!(out, "::rust::Str::Repr ");
368 } else {
David Tolnay99642622020-03-25 13:07:35 -0700369 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700370 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700371 let mangled = mangle::extern_fn(&out.namespace, efn);
372 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700373 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700374 if receiver.mutability.is_none() {
375 write!(out, "const ");
376 }
David Tolnaye439c772020-04-20 00:23:55 -0700377 write!(out, "{} *self$", receiver.ident);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700378 }
David Tolnay7db73692019-10-20 14:51:12 -0400379 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700380 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400381 write!(out, ", ");
382 }
David Tolnaya46a2372020-03-06 10:03:48 -0800383 if arg.ty == RustString {
384 write!(out, "const ");
385 }
David Tolnay7db73692019-10-20 14:51:12 -0400386 write_extern_arg(out, arg, types);
387 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700388 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400389 if indirect_return {
390 if !efn.args.is_empty() {
391 write!(out, ", ");
392 }
David Tolnay99642622020-03-25 13:07:35 -0700393 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400394 write!(out, "*return$");
395 }
396 writeln!(out, ") noexcept {{");
397 write!(out, " ");
398 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700399 match &efn.receiver {
400 None => write!(out, "(*{}$)(", efn.ident),
David Tolnaye439c772020-04-20 00:23:55 -0700401 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ident, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700402 }
David Tolnay7db73692019-10-20 14:51:12 -0400403 for (i, arg) in efn.args.iter().enumerate() {
404 if i > 0 {
405 write!(out, ", ");
406 }
407 write_type(out, &arg.ty);
408 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700409 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700410 if let Some(receiver) = &efn.receiver {
411 if receiver.mutability.is_none() {
412 write!(out, " const");
413 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700414 }
415 write!(out, " = ");
416 match &efn.receiver {
417 None => write!(out, "{}", efn.ident),
David Tolnaye439c772020-04-20 00:23:55 -0700418 Some(receiver) => write!(out, "&{}::{}", receiver.ident, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700419 }
420 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400421 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700422 if efn.throws {
423 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700424 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700425 writeln!(out, " [&] {{");
426 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700427 }
David Tolnay7db73692019-10-20 14:51:12 -0400428 if indirect_return {
429 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700430 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400431 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700432 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400433 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700434 }
435 match &efn.ret {
436 Some(Type::Ref(_)) => write!(out, "&"),
437 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700438 Some(Type::SliceRefU8(_)) if !indirect_return => {
439 write!(out, "::rust::Slice<uint8_t>::Repr(")
440 }
David Tolnay99642622020-03-25 13:07:35 -0700441 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400442 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700443 match &efn.receiver {
444 None => write!(out, "{}$(", efn.ident),
David Tolnay26804bd2020-04-19 20:06:51 -0700445 Some(_) => write!(out, "(self$->*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 for (i, arg) in efn.args.iter().enumerate() {
448 if i > 0 {
449 write!(out, ", ");
450 }
451 if let Type::RustBox(_) = &arg.ty {
452 write_type(out, &arg.ty);
453 write!(out, "::from_raw({})", arg.ident);
454 } else if let Type::UniquePtr(_) = &arg.ty {
455 write_type(out, &arg.ty);
456 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800457 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800458 write!(
459 out,
460 "::rust::String(::rust::unsafe_bitcopy, *{})",
461 arg.ident,
462 );
David Tolnay7db73692019-10-20 14:51:12 -0400463 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700464 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800465 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400466 } else {
467 write!(out, "{}", arg.ident);
468 }
469 }
470 write!(out, ")");
471 match &efn.ret {
472 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
473 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700474 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400475 _ => {}
476 }
477 if indirect_return {
478 write!(out, ")");
479 }
480 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700481 if efn.throws {
482 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700483 writeln!(out, " throw$.ptr = nullptr;");
484 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700485 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700486 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700487 writeln!(
488 out,
David Tolnay5d121442020-03-17 22:14:40 -0700489 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700490 );
David Tolnay5d121442020-03-17 22:14:40 -0700491 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700492 writeln!(out, " return throw$;");
493 }
David Tolnay7db73692019-10-20 14:51:12 -0400494 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700495 for arg in &efn.args {
496 if let Type::Fn(f) = &arg.ty {
497 let var = &arg.ident;
498 write_function_pointer_trampoline(out, efn, var, f, types);
499 }
500 }
501}
502
503fn write_function_pointer_trampoline(
504 out: &mut OutFile,
505 efn: &ExternFn,
506 var: &Ident,
507 f: &Signature,
508 types: &Types,
509) {
510 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700511 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700512 let indirect_call = true;
513 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
514
515 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700516 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700517 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400518}
519
520fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700521 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700522 let indirect_call = false;
523 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
524}
525
526fn write_rust_function_decl_impl(
527 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700528 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700529 sig: &Signature,
530 types: &Types,
531 indirect_call: bool,
532) {
533 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700534 write!(out, "::rust::Str::Repr ");
535 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700536 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700537 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700538 write!(out, "{}(", link_name);
539 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700540 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700541 if receiver.mutability.is_none() {
542 write!(out, "const ");
543 }
David Tolnaye439c772020-04-20 00:23:55 -0700544 write!(out, "{} &self$", receiver.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700545 needs_comma = true;
546 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700547 for arg in &sig.args {
548 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400549 write!(out, ", ");
550 }
551 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400553 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 if indirect_return(sig, types) {
555 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400556 write!(out, ", ");
557 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400559 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700560 needs_comma = true;
561 }
562 if indirect_call {
563 if needs_comma {
564 write!(out, ", ");
565 }
566 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400567 }
568 writeln!(out, ") noexcept;");
569}
570
571fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400572 for line in efn.doc.to_string().lines() {
573 writeln!(out, "//{}", line);
574 }
David Tolnay891061b2020-04-19 22:42:33 -0700575 let local_name = Symbol::from(&efn.ident);
David Tolnay3caa50a2020-04-19 21:25:34 -0700576 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700577 let indirect_call = false;
578 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
579}
580
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700581fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700582 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700583 local_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700584 sig: &Signature,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700585 receiver: Option<&Receiver>,
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 indirect_call: bool,
587) {
588 write_return_type(out, &sig.ret);
David Tolnaye439c772020-04-20 00:23:55 -0700589 if let Some(receiver) = receiver {
590 write!(out, "{}::", receiver.ident);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700591 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 write!(out, "{}(", local_name);
593 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400594 if i > 0 {
595 write!(out, ", ");
596 }
597 write_type_space(out, &arg.ty);
598 write!(out, "{}", arg.ident);
599 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 if indirect_call {
601 if !sig.args.is_empty() {
602 write!(out, ", ");
603 }
604 write!(out, "void *extern$");
605 }
David Tolnay1e548172020-03-16 13:37:09 -0700606 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700607 if let Some(receiver) = &sig.receiver {
608 if receiver.mutability.is_none() {
609 write!(out, " const");
610 }
611 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700612 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700613 write!(out, " noexcept");
614 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700615}
616
617fn write_rust_function_shim_impl(
618 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700619 local_name: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700620 sig: &Signature,
621 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700622 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700623 indirect_call: bool,
624) {
625 if out.header && sig.receiver.is_some() {
626 // We've already defined this inside the struct.
627 return;
628 }
629 write_rust_function_shim_decl(out, local_name, sig, sig.receiver.as_ref(), indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400630 if out.header {
631 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700632 return;
David Tolnay7db73692019-10-20 14:51:12 -0400633 }
David Tolnay439cde22020-04-20 00:46:25 -0700634 writeln!(out, " {{");
635 for arg in &sig.args {
636 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
637 out.include.utility = true;
638 write!(out, " ::rust::ManuallyDrop<");
639 write_type(out, &arg.ty);
640 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
641 }
642 }
643 write!(out, " ");
644 let indirect_return = indirect_return(sig, types);
645 if indirect_return {
646 write!(out, "::rust::MaybeUninit<");
647 write_type(out, sig.ret.as_ref().unwrap());
648 writeln!(out, "> return$;");
649 write!(out, " ");
650 } else if let Some(ret) = &sig.ret {
651 write!(out, "return ");
652 match ret {
653 Type::RustBox(_) => {
654 write_type(out, ret);
655 write!(out, "::from_raw(");
656 }
657 Type::UniquePtr(_) => {
658 write_type(out, ret);
659 write!(out, "(");
660 }
661 Type::Ref(_) => write!(out, "*"),
662 _ => {}
663 }
664 }
665 if sig.throws {
666 write!(out, "::rust::Str::Repr error$ = ");
667 }
668 write!(out, "{}(", invoke);
669 if sig.receiver.is_some() {
670 write!(out, "*this");
671 }
672 for (i, arg) in sig.args.iter().enumerate() {
673 if i > 0 || sig.receiver.is_some() {
674 write!(out, ", ");
675 }
676 match &arg.ty {
677 Type::Str(_) => write!(out, "::rust::Str::Repr("),
678 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
679 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
680 _ => {}
681 }
682 write!(out, "{}", arg.ident);
683 match &arg.ty {
684 Type::RustBox(_) => write!(out, ".into_raw()"),
685 Type::UniquePtr(_) => write!(out, ".release()"),
686 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
687 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
688 _ => {}
689 }
690 }
691 if indirect_return {
692 if !sig.args.is_empty() {
693 write!(out, ", ");
694 }
695 write!(out, "&return$.value");
696 }
697 if indirect_call {
698 if !sig.args.is_empty() || indirect_return {
699 write!(out, ", ");
700 }
701 write!(out, "extern$");
702 }
703 write!(out, ")");
704 if let Some(ret) = &sig.ret {
705 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
706 write!(out, ")");
707 }
708 }
709 writeln!(out, ";");
710 if sig.throws {
711 writeln!(out, " if (error$.ptr) {{");
712 writeln!(out, " throw ::rust::Error(error$);");
713 writeln!(out, " }}");
714 }
715 if indirect_return {
716 out.include.utility = true;
717 writeln!(out, " return ::std::move(return$.value);");
718 }
719 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400720}
721
722fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
723 match ty {
724 None => write!(out, "void "),
725 Some(ty) => write_type_space(out, ty),
726 }
727}
728
David Tolnay75dca2e2020-03-25 20:17:52 -0700729fn indirect_return(sig: &Signature, types: &Types) -> bool {
730 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700731 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700732 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700733}
734
David Tolnay99642622020-03-25 13:07:35 -0700735fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
736 match ty {
737 Type::RustBox(ty) | Type::UniquePtr(ty) => {
738 write_type_space(out, &ty.inner);
739 write!(out, "*");
740 }
741 Type::Ref(ty) => {
742 if ty.mutability.is_none() {
743 write!(out, "const ");
744 }
745 write_type(out, &ty.inner);
746 write!(out, " *");
747 }
748 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700749 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700750 _ => write_type(out, ty),
751 }
752}
753
754fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
755 write_indirect_return_type(out, ty);
756 match ty {
757 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700758 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700759 _ => write_space_after_type(out, ty),
760 }
761}
762
763fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400764 match ty {
765 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
766 write_type_space(out, &ty.inner);
767 write!(out, "*");
768 }
David Tolnay4a441222020-01-25 16:24:27 -0800769 Some(Type::Ref(ty)) => {
770 if ty.mutability.is_none() {
771 write!(out, "const ");
772 }
773 write_type(out, &ty.inner);
774 write!(out, " *");
775 }
David Tolnay750755e2020-03-01 13:04:08 -0800776 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700777 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400778 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
779 _ => write_return_type(out, ty),
780 }
781}
782
783fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
784 match &arg.ty {
785 Type::RustBox(ty) | Type::UniquePtr(ty) => {
786 write_type_space(out, &ty.inner);
787 write!(out, "*");
788 }
David Tolnay750755e2020-03-01 13:04:08 -0800789 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700790 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400791 _ => write_type_space(out, &arg.ty),
792 }
793 if types.needs_indirect_abi(&arg.ty) {
794 write!(out, "*");
795 }
796 write!(out, "{}", arg.ident);
797}
798
799fn write_type(out: &mut OutFile, ty: &Type) {
800 match ty {
801 Type::Ident(ident) => match Atom::from(ident) {
802 Some(Bool) => write!(out, "bool"),
803 Some(U8) => write!(out, "uint8_t"),
804 Some(U16) => write!(out, "uint16_t"),
805 Some(U32) => write!(out, "uint32_t"),
806 Some(U64) => write!(out, "uint64_t"),
807 Some(Usize) => write!(out, "size_t"),
808 Some(I8) => write!(out, "int8_t"),
809 Some(I16) => write!(out, "int16_t"),
810 Some(I32) => write!(out, "int32_t"),
811 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700812 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700813 Some(F32) => write!(out, "float"),
814 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800815 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800816 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400817 None => write!(out, "{}", ident),
818 },
819 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800820 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400821 write_type(out, &ty.inner);
822 write!(out, ">");
823 }
824 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800825 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400826 write_type(out, &ptr.inner);
827 write!(out, ">");
828 }
829 Type::Ref(r) => {
830 if r.mutability.is_none() {
831 write!(out, "const ");
832 }
833 write_type(out, &r.inner);
834 write!(out, " &");
835 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700836 Type::Slice(_) => {
837 // For now, only U8 slices are supported, which are covered separately below
838 unreachable!()
839 }
David Tolnay7db73692019-10-20 14:51:12 -0400840 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800841 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400842 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700843 Type::SliceRefU8(_) => {
844 write!(out, "::rust::Slice<uint8_t>");
845 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700846 Type::Fn(f) => {
847 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
848 match &f.ret {
849 Some(ret) => write_type(out, ret),
850 None => write!(out, "void"),
851 }
852 write!(out, "(");
853 for (i, arg) in f.args.iter().enumerate() {
854 if i > 0 {
855 write!(out, ", ");
856 }
857 write_type(out, &arg.ty);
858 }
859 write!(out, ")>");
860 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700861 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400862 }
863}
864
865fn write_type_space(out: &mut OutFile, ty: &Type) {
866 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700867 write_space_after_type(out, ty);
868}
869
870fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400871 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700872 Type::Ident(_)
873 | Type::RustBox(_)
874 | Type::UniquePtr(_)
875 | Type::Str(_)
876 | Type::SliceRefU8(_)
877 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400878 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700879 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400880 }
881}
882
883fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
884 fn allow_unique_ptr(ident: &Ident) -> bool {
885 Atom::from(ident).is_none()
886 }
887
888 out.begin_block("extern \"C\"");
889 for ty in types {
890 if let Type::RustBox(ty) = ty {
891 if let Type::Ident(inner) = &ty.inner {
892 out.next_section();
893 write_rust_box_extern(out, inner);
894 }
895 } else if let Type::UniquePtr(ptr) = ty {
896 if let Type::Ident(inner) = &ptr.inner {
897 if allow_unique_ptr(inner) {
898 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700899 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400900 }
901 }
902 }
903 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800904 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400905
David Tolnay750755e2020-03-01 13:04:08 -0800906 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700907 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400908 for ty in types {
909 if let Type::RustBox(ty) = ty {
910 if let Type::Ident(inner) = &ty.inner {
911 write_rust_box_impl(out, inner);
912 }
913 }
914 }
David Tolnay8c730492020-03-13 01:29:06 -0700915 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800916 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400917}
918
919fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
920 let mut inner = String::new();
921 for name in &out.namespace {
922 inner += name;
923 inner += "::";
924 }
925 inner += &ident.to_string();
926 let instance = inner.replace("::", "$");
927
David Tolnay8c730492020-03-13 01:29:06 -0700928 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
929 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400930 writeln!(
931 out,
David Tolnay8c730492020-03-13 01:29:06 -0700932 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400933 instance, inner,
934 );
935 writeln!(
936 out,
David Tolnay8c730492020-03-13 01:29:06 -0700937 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400938 instance, inner,
939 );
David Tolnay8c730492020-03-13 01:29:06 -0700940 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400941}
942
943fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
944 let mut inner = String::new();
945 for name in &out.namespace {
946 inner += name;
947 inner += "::";
948 }
949 inner += &ident.to_string();
950 let instance = inner.replace("::", "$");
951
952 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800953 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700954 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400955 writeln!(out, "}}");
956
957 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800958 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700959 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400960 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400961}
962
David Tolnay53838912020-04-09 20:56:44 -0700963fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700964 out.include.utility = true;
965
David Tolnay7db73692019-10-20 14:51:12 -0400966 let mut inner = String::new();
967 for name in &out.namespace {
968 inner += name;
969 inner += "::";
970 }
971 inner += &ident.to_string();
972 let instance = inner.replace("::", "$");
973
David Tolnay8c730492020-03-13 01:29:06 -0700974 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
975 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400976 writeln!(
977 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800978 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400979 inner,
980 );
981 writeln!(
982 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800983 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400984 inner,
985 );
986 writeln!(
987 out,
David Tolnay8c730492020-03-13 01:29:06 -0700988 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400989 instance, inner,
990 );
David Tolnay7e219b82020-03-01 13:14:51 -0800991 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400992 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700993 if types.structs.contains_key(ident) {
994 writeln!(
995 out,
996 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
997 instance, inner, inner,
998 );
999 writeln!(
1000 out,
1001 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1002 inner, inner,
1003 );
1004 writeln!(out, "}}");
1005 }
David Tolnay7db73692019-10-20 14:51:12 -04001006 writeln!(
1007 out,
David Tolnay8c730492020-03-13 01:29:06 -07001008 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001009 instance, inner, inner,
1010 );
David Tolnay7e219b82020-03-01 13:14:51 -08001011 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001012 writeln!(out, "}}");
1013 writeln!(
1014 out,
David Tolnay8c730492020-03-13 01:29:06 -07001015 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001016 inner, instance, inner,
1017 );
1018 writeln!(out, " return ptr.get();");
1019 writeln!(out, "}}");
1020 writeln!(
1021 out,
David Tolnay8c730492020-03-13 01:29:06 -07001022 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001023 inner, instance, inner,
1024 );
1025 writeln!(out, " return ptr.release();");
1026 writeln!(out, "}}");
1027 writeln!(
1028 out,
David Tolnay8c730492020-03-13 01:29:06 -07001029 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001030 instance, inner,
1031 );
1032 writeln!(out, " ptr->~unique_ptr();");
1033 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001034 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001035}