blob: 6c43928971116baf5139f1deb3196c883870d990 [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, *};
Myron Ahneba35cf2020-02-05 19:41:51 +07004use crate::syntax::mangled::ToMangled;
David Tolnay08419302020-04-19 20:38:20 -07005use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
Myron Ahneba35cf2020-02-05 19:41:51 +07007use crate::syntax::typename::ToTypename;
David Tolnaya73853b2020-04-20 01:19:56 -07008use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
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 Tolnay7c295462020-04-25 12:45:07 -070047 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040048 }
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
David Tolnay05e11cc2020-04-20 02:13:56 -070056 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070057 .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,
Myron Ahneba35cf2020-02-05 19:41:51 +0700128 Type::Vector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700129 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700130 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
132 }
David Tolnay7db73692019-10-20 14:51:12 -0400133}
134
David Tolnayf51447e2020-03-06 14:14:27 -0800135fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700136 let mut needs_rust_string = false;
137 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700138 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400139 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700140 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700141 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700142 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400143 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700144 match ty {
145 Type::RustBox(_) => {
146 out.include.type_traits = true;
147 needs_rust_box = true;
148 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700149 Type::RustVec(_) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700150 needs_rust_vec = true;
151 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700152 Type::Str(_) => {
153 out.include.cstdint = true;
154 out.include.string = true;
155 needs_rust_str = true;
156 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700157 Type::Fn(_) => {
158 needs_rust_fn = true;
159 }
David Tolnay4770b472020-04-14 16:32:59 -0700160 Type::Slice(_) | Type::SliceRefU8(_) => {
161 needs_rust_slice = true;
162 }
David Tolnay7c295462020-04-25 12:45:07 -0700163 ty if ty == Isize => {
164 out.include.base_tsd = true;
165 needs_rust_isize = true;
166 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700167 ty if ty == RustString => {
168 out.include.array = true;
169 out.include.cstdint = true;
170 out.include.string = true;
171 needs_rust_string = true;
172 }
173 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400174 }
175 }
176
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 let mut needs_rust_error = false;
178 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800179 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800180 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700181 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800182 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 match api {
184 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700185 if efn.throws {
186 needs_trycatch = true;
187 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 for arg in &efn.args {
189 if arg.ty == RustString {
190 needs_unsafe_bitcopy = true;
191 break;
192 }
David Tolnay09011c32020-03-06 14:40:28 -0800193 }
194 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700195 Api::RustFunction(efn) if !out.header => {
196 if efn.throws {
197 out.include.exception = true;
198 needs_rust_error = true;
199 }
200 for arg in &efn.args {
201 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
202 needs_manually_drop = true;
203 break;
204 }
205 }
206 if let Some(ret) = &efn.ret {
207 if types.needs_indirect_abi(ret) {
208 needs_maybe_uninit = true;
209 }
David Tolnayf51447e2020-03-06 14:14:27 -0800210 }
211 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800213 }
214 }
215
David Tolnay750755e2020-03-01 13:04:08 -0800216 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700217 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800218
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 if needs_rust_string
220 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700221 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700222 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700223 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700224 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700225 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700226 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700227 || needs_unsafe_bitcopy
228 || needs_manually_drop
229 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700230 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 {
David Tolnay736cbca2020-03-11 16:49:18 -0700232 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800233 }
234
David Tolnayd1402742020-03-25 22:21:42 -0700235 if needs_rust_string {
236 out.next_section();
237 writeln!(out, "struct unsafe_bitcopy_t;");
238 }
239
David Tolnayb7a7cb62020-03-17 21:18:40 -0700240 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
241 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700242 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
Myron Ahneba35cf2020-02-05 19:41:51 +0700244 write_header_section(out, needs_rust_vec, "CXXBRIDGE02_RUST_VEC");
David Tolnay75dca2e2020-03-25 20:17:52 -0700245 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700246 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnay7c295462020-04-25 12:45:07 -0700247 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700248 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800249
250 if needs_manually_drop {
251 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700252 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800253 writeln!(out, "template <typename T>");
254 writeln!(out, "union ManuallyDrop {{");
255 writeln!(out, " T value;");
256 writeln!(
257 out,
258 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
259 );
260 writeln!(out, " ~ManuallyDrop() {{}}");
261 writeln!(out, "}};");
262 }
263
David Tolnay09011c32020-03-06 14:40:28 -0800264 if needs_maybe_uninit {
265 out.next_section();
266 writeln!(out, "template <typename T>");
267 writeln!(out, "union MaybeUninit {{");
268 writeln!(out, " T value;");
269 writeln!(out, " MaybeUninit() {{}}");
270 writeln!(out, " ~MaybeUninit() {{}}");
271 writeln!(out, "}};");
272 }
273
David Tolnay3e3e0af2020-03-17 22:42:49 -0700274 out.end_block("namespace cxxbridge02");
275
David Tolnay5d121442020-03-17 22:14:40 -0700276 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700277 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700278 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700279 out.include.type_traits = true;
280 out.include.utility = true;
281 writeln!(out, "class missing {{}};");
282 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700283 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700284 writeln!(out, "template <typename Try, typename Fail>");
285 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700286 writeln!(
287 out,
David Tolnay04722332020-03-18 11:31:54 -0700288 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700289 );
David Tolnay04722332020-03-18 11:31:54 -0700290 writeln!(out, " missing>::value>::type");
291 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700292 writeln!(out, " func();");
293 writeln!(out, "}} catch (const ::std::exception &e) {{");
294 writeln!(out, " fail(e.what());");
295 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700296 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700297 }
298
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800299 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400300}
301
David Tolnayb7a7cb62020-03-17 21:18:40 -0700302fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700303 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700304 if needed {
305 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700306 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700307 if !line.trim_start().starts_with("//") {
308 writeln!(out, "{}", line);
309 }
310 }
311 }
312}
313
David Tolnay7db73692019-10-20 14:51:12 -0400314fn write_struct(out: &mut OutFile, strct: &Struct) {
315 for line in strct.doc.to_string().lines() {
316 writeln!(out, "//{}", line);
317 }
318 writeln!(out, "struct {} final {{", strct.ident);
319 for field in &strct.fields {
320 write!(out, " ");
321 write_type_space(out, &field.ty);
322 writeln!(out, "{};", field.ident);
323 }
324 writeln!(out, "}};");
325}
326
327fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
328 writeln!(out, "struct {};", ident);
329}
330
David Tolnay8861bee2020-01-20 18:39:24 -0800331fn write_struct_using(out: &mut OutFile, ident: &Ident) {
332 writeln!(out, "using {} = {};", ident, ident);
333}
334
David Tolnayc1fe0052020-04-17 15:15:06 -0700335fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700336 for line in ety.doc.to_string().lines() {
337 writeln!(out, "//{}", line);
338 }
339 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700340 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700341 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700342 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700343 write!(out, " ");
344 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700345 let local_name = method.ident.to_string();
346 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 writeln!(out, ";");
348 }
349 writeln!(out, "}};");
350}
351
David Tolnayebef4a22020-03-17 15:33:47 -0700352fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
353 let mut has_cxx_throws = false;
354 for api in apis {
355 if let Api::CxxFunction(efn) = api {
356 if efn.throws {
357 has_cxx_throws = true;
358 break;
359 }
360 }
361 }
362
363 if has_cxx_throws {
364 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700365 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700366 out,
367 "const char *cxxbridge02$exception(const char *, size_t);",
368 );
369 }
370}
371
David Tolnay7db73692019-10-20 14:51:12 -0400372fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700373 if efn.throws {
374 write!(out, "::rust::Str::Repr ");
375 } else {
David Tolnay99642622020-03-25 13:07:35 -0700376 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700377 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700378 let mangled = mangle::extern_fn(&out.namespace, efn);
379 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700380 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700381 if receiver.mutability.is_none() {
382 write!(out, "const ");
383 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700384 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700385 }
David Tolnay7db73692019-10-20 14:51:12 -0400386 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700387 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400388 write!(out, ", ");
389 }
David Tolnaya46a2372020-03-06 10:03:48 -0800390 if arg.ty == RustString {
391 write!(out, "const ");
392 }
David Tolnay7db73692019-10-20 14:51:12 -0400393 write_extern_arg(out, arg, types);
394 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700395 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400396 if indirect_return {
397 if !efn.args.is_empty() {
398 write!(out, ", ");
399 }
David Tolnay99642622020-03-25 13:07:35 -0700400 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400401 write!(out, "*return$");
402 }
403 writeln!(out, ") noexcept {{");
404 write!(out, " ");
405 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700406 match &efn.receiver {
407 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700408 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700409 }
David Tolnay7db73692019-10-20 14:51:12 -0400410 for (i, arg) in efn.args.iter().enumerate() {
411 if i > 0 {
412 write!(out, ", ");
413 }
414 write_type(out, &arg.ty);
415 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700416 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700417 if let Some(receiver) = &efn.receiver {
418 if receiver.mutability.is_none() {
419 write!(out, " const");
420 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700421 }
422 write!(out, " = ");
423 match &efn.receiver {
424 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700425 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700426 }
427 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700429 if efn.throws {
430 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700431 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700432 writeln!(out, " [&] {{");
433 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700434 }
David Tolnay7db73692019-10-20 14:51:12 -0400435 if indirect_return {
436 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700437 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400438 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700439 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400440 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700441 }
442 match &efn.ret {
443 Some(Type::Ref(_)) => write!(out, "&"),
444 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700445 Some(Type::SliceRefU8(_)) if !indirect_return => {
446 write!(out, "::rust::Slice<uint8_t>::Repr(")
447 }
David Tolnay99642622020-03-25 13:07:35 -0700448 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400449 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700450 match &efn.receiver {
451 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700452 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700453 }
David Tolnay7db73692019-10-20 14:51:12 -0400454 for (i, arg) in efn.args.iter().enumerate() {
455 if i > 0 {
456 write!(out, ", ");
457 }
458 if let Type::RustBox(_) = &arg.ty {
459 write_type(out, &arg.ty);
460 write!(out, "::from_raw({})", arg.ident);
461 } else if let Type::UniquePtr(_) = &arg.ty {
462 write_type(out, &arg.ty);
463 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800464 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800465 write!(
466 out,
467 "::rust::String(::rust::unsafe_bitcopy, *{})",
468 arg.ident,
469 );
David Tolnay7db73692019-10-20 14:51:12 -0400470 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700471 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800472 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400473 } else {
474 write!(out, "{}", arg.ident);
475 }
476 }
477 write!(out, ")");
478 match &efn.ret {
479 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
480 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Myron Ahneba35cf2020-02-05 19:41:51 +0700481 Some(Type::Vector(_)) => write!(
482 out,
483 " /* Use RVO to convert to r-value and move construct */"
484 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700485 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400486 _ => {}
487 }
488 if indirect_return {
489 write!(out, ")");
490 }
491 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700492 if efn.throws {
493 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700494 writeln!(out, " throw$.ptr = nullptr;");
495 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700496 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700497 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700498 writeln!(
499 out,
David Tolnay5d121442020-03-17 22:14:40 -0700500 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700501 );
David Tolnay5d121442020-03-17 22:14:40 -0700502 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700503 writeln!(out, " return throw$;");
504 }
David Tolnay7db73692019-10-20 14:51:12 -0400505 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700506 for arg in &efn.args {
507 if let Type::Fn(f) = &arg.ty {
508 let var = &arg.ident;
509 write_function_pointer_trampoline(out, efn, var, f, types);
510 }
511 }
512}
513
514fn write_function_pointer_trampoline(
515 out: &mut OutFile,
516 efn: &ExternFn,
517 var: &Ident,
518 f: &Signature,
519 types: &Types,
520) {
521 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700522 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700523 let indirect_call = true;
524 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
525
526 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700527 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700528 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400529}
530
531fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700532 let link_name = mangle::extern_fn(&out.namespace, efn);
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,
David Tolnay891061b2020-04-19 22:42:33 -0700539 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700540 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;
David Tolnaye439c772020-04-20 00:23:55 -0700551 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700552 if receiver.mutability.is_none() {
553 write!(out, "const ");
554 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700555 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700556 needs_comma = true;
557 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 for arg in &sig.args {
559 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400560 write!(out, ", ");
561 }
562 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700563 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400564 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700565 if indirect_return(sig, types) {
566 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400567 write!(out, ", ");
568 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400570 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700571 needs_comma = true;
572 }
573 if indirect_call {
574 if needs_comma {
575 write!(out, ", ");
576 }
577 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400578 }
579 writeln!(out, ") noexcept;");
580}
581
582fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400583 for line in efn.doc.to_string().lines() {
584 writeln!(out, "//{}", line);
585 }
David Tolnaya73853b2020-04-20 01:19:56 -0700586 let local_name = match &efn.sig.receiver {
587 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700588 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700589 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700590 let invoke = mangle::extern_fn(&out.namespace, efn);
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,
David Tolnaya73853b2020-04-20 01:19:56 -0700597 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700599 indirect_call: bool,
600) {
601 write_return_type(out, &sig.ret);
602 write!(out, "{}(", local_name);
603 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400604 if i > 0 {
605 write!(out, ", ");
606 }
607 write_type_space(out, &arg.ty);
608 write!(out, "{}", arg.ident);
609 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700610 if indirect_call {
611 if !sig.args.is_empty() {
612 write!(out, ", ");
613 }
614 write!(out, "void *extern$");
615 }
David Tolnay1e548172020-03-16 13:37:09 -0700616 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700617 if let Some(receiver) = &sig.receiver {
618 if receiver.mutability.is_none() {
619 write!(out, " const");
620 }
621 }
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,
David Tolnaya73853b2020-04-20 01:19:56 -0700629 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700630 sig: &Signature,
631 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700632 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700633 indirect_call: bool,
634) {
635 if out.header && sig.receiver.is_some() {
636 // We've already defined this inside the struct.
637 return;
638 }
David Tolnaya73853b2020-04-20 01:19:56 -0700639 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400640 if out.header {
641 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700642 return;
David Tolnay7db73692019-10-20 14:51:12 -0400643 }
David Tolnay439cde22020-04-20 00:46:25 -0700644 writeln!(out, " {{");
645 for arg in &sig.args {
646 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
647 out.include.utility = true;
648 write!(out, " ::rust::ManuallyDrop<");
649 write_type(out, &arg.ty);
650 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
651 }
652 }
653 write!(out, " ");
654 let indirect_return = indirect_return(sig, types);
655 if indirect_return {
656 write!(out, "::rust::MaybeUninit<");
657 write_type(out, sig.ret.as_ref().unwrap());
658 writeln!(out, "> return$;");
659 write!(out, " ");
660 } else if let Some(ret) = &sig.ret {
661 write!(out, "return ");
662 match ret {
663 Type::RustBox(_) => {
664 write_type(out, ret);
665 write!(out, "::from_raw(");
666 }
667 Type::UniquePtr(_) => {
668 write_type(out, ret);
669 write!(out, "(");
670 }
671 Type::Ref(_) => write!(out, "*"),
672 _ => {}
673 }
674 }
675 if sig.throws {
676 write!(out, "::rust::Str::Repr error$ = ");
677 }
678 write!(out, "{}(", invoke);
679 if sig.receiver.is_some() {
680 write!(out, "*this");
681 }
682 for (i, arg) in sig.args.iter().enumerate() {
683 if i > 0 || sig.receiver.is_some() {
684 write!(out, ", ");
685 }
686 match &arg.ty {
687 Type::Str(_) => write!(out, "::rust::Str::Repr("),
688 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
689 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
690 _ => {}
691 }
692 write!(out, "{}", arg.ident);
693 match &arg.ty {
694 Type::RustBox(_) => write!(out, ".into_raw()"),
695 Type::UniquePtr(_) => write!(out, ".release()"),
696 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
697 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
698 _ => {}
699 }
700 }
701 if indirect_return {
702 if !sig.args.is_empty() {
703 write!(out, ", ");
704 }
705 write!(out, "&return$.value");
706 }
707 if indirect_call {
708 if !sig.args.is_empty() || indirect_return {
709 write!(out, ", ");
710 }
711 write!(out, "extern$");
712 }
713 write!(out, ")");
714 if let Some(ret) = &sig.ret {
715 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
716 write!(out, ")");
717 }
718 }
719 writeln!(out, ";");
720 if sig.throws {
721 writeln!(out, " if (error$.ptr) {{");
722 writeln!(out, " throw ::rust::Error(error$);");
723 writeln!(out, " }}");
724 }
725 if indirect_return {
726 out.include.utility = true;
727 writeln!(out, " return ::std::move(return$.value);");
728 }
729 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400730}
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 {
Myron Ahneba35cf2020-02-05 19:41:51 +0700795 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::Vector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400796 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) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700812 Some(a) => write!(out, "{}", a.to_cxx()),
David Tolnay7db73692019-10-20 14:51:12 -0400813 None => write!(out, "{}", ident),
814 },
815 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800816 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400817 write_type(out, &ty.inner);
818 write!(out, ">");
819 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700820 Type::RustVec(ty) => {
821 write!(out, "::rust::Vec<");
822 write_type(out, &ty.inner);
823 write!(out, ">");
824 }
David Tolnay7db73692019-10-20 14:51:12 -0400825 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800826 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400827 write_type(out, &ptr.inner);
828 write!(out, ">");
829 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700830 Type::Vector(ty) => {
831 write!(out, "::std::vector<");
832 write_type(out, &ty.inner);
833 write!(out, ">");
834 }
David Tolnay7db73692019-10-20 14:51:12 -0400835 Type::Ref(r) => {
836 if r.mutability.is_none() {
837 write!(out, "const ");
838 }
839 write_type(out, &r.inner);
840 write!(out, " &");
841 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700842 Type::Slice(_) => {
843 // For now, only U8 slices are supported, which are covered separately below
844 unreachable!()
845 }
David Tolnay7db73692019-10-20 14:51:12 -0400846 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800847 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400848 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700849 Type::SliceRefU8(_) => {
850 write!(out, "::rust::Slice<uint8_t>");
851 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700852 Type::Fn(f) => {
853 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
854 match &f.ret {
855 Some(ret) => write_type(out, ret),
856 None => write!(out, "void"),
857 }
858 write!(out, "(");
859 for (i, arg) in f.args.iter().enumerate() {
860 if i > 0 {
861 write!(out, ", ");
862 }
863 write_type(out, &arg.ty);
864 }
865 write!(out, ")>");
866 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700867 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400868 }
869}
870
871fn write_type_space(out: &mut OutFile, ty: &Type) {
872 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700873 write_space_after_type(out, ty);
874}
875
876fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400877 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700878 Type::Ident(_)
879 | Type::RustBox(_)
880 | Type::UniquePtr(_)
881 | Type::Str(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700882 | Type::Vector(_)
883 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700884 | Type::SliceRefU8(_)
885 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400886 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700887 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400888 }
889}
890
891fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
892 fn allow_unique_ptr(ident: &Ident) -> bool {
893 Atom::from(ident).is_none()
894 }
895
Myron Ahneba35cf2020-02-05 19:41:51 +0700896 fn allow_vector(ident: &Ident) -> bool {
897 // Note: built-in types such as u8 are already defined in cxx.cc
898 Atom::from(ident).is_none()
899 }
900
David Tolnay7db73692019-10-20 14:51:12 -0400901 out.begin_block("extern \"C\"");
902 for ty in types {
903 if let Type::RustBox(ty) = ty {
904 if let Type::Ident(inner) = &ty.inner {
905 out.next_section();
906 write_rust_box_extern(out, inner);
907 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700908 } else if let Type::RustVec(ty) = ty {
909 if let Type::Ident(_) = &ty.inner {
910 out.next_section();
911 write_rust_vec_extern(out, &ty.inner);
912 }
David Tolnay7db73692019-10-20 14:51:12 -0400913 } else if let Type::UniquePtr(ptr) = ty {
914 if let Type::Ident(inner) = &ptr.inner {
915 if allow_unique_ptr(inner) {
916 out.next_section();
Myron Ahneba35cf2020-02-05 19:41:51 +0700917 write_unique_ptr(out, &ptr.inner, types);
918 }
919 } else if let Type::Vector(ptr1) = &ptr.inner {
920 if let Type::Ident(inner) = &ptr1.inner {
921 if allow_vector(inner) {
922 out.next_section();
923 write_unique_ptr(out, &ptr.inner, types);
924 }
925 }
926 }
927 } else if let Type::Vector(ptr) = ty {
928 if let Type::Ident(inner) = &ptr.inner {
929 if allow_vector(inner) {
930 out.next_section();
931 write_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400932 }
933 }
934 }
935 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800936 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400937
David Tolnay750755e2020-03-01 13:04:08 -0800938 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700939 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400940 for ty in types {
941 if let Type::RustBox(ty) = ty {
942 if let Type::Ident(inner) = &ty.inner {
943 write_rust_box_impl(out, inner);
944 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700945 } else if let Type::RustVec(ty) = ty {
946 if let Type::Ident(_) = &ty.inner {
947 write_rust_vec_impl(out, &ty.inner);
948 }
David Tolnay7db73692019-10-20 14:51:12 -0400949 }
950 }
David Tolnay8c730492020-03-13 01:29:06 -0700951 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800952 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400953}
954
955fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
956 let mut inner = String::new();
957 for name in &out.namespace {
958 inner += name;
959 inner += "::";
960 }
961 inner += &ident.to_string();
962 let instance = inner.replace("::", "$");
963
David Tolnay8c730492020-03-13 01:29:06 -0700964 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
965 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400966 writeln!(
967 out,
David Tolnay8c730492020-03-13 01:29:06 -0700968 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400969 instance, inner,
970 );
971 writeln!(
972 out,
David Tolnay8c730492020-03-13 01:29:06 -0700973 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400974 instance, inner,
975 );
David Tolnay8c730492020-03-13 01:29:06 -0700976 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400977}
978
Myron Ahneba35cf2020-02-05 19:41:51 +0700979fn write_rust_vec_extern(out: &mut OutFile, ty: &Type) {
980 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
981 let inner = ty.to_typename(&namespace);
982 let instance = ty.to_mangled(&namespace);
983
984 writeln!(out, "#ifndef CXXBRIDGE02_RUST_VEC_{}", instance);
985 writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance);
986 writeln!(
987 out,
988 "void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
989 instance, inner,
990 );
991 writeln!(
992 out,
Myron Ahneba35cf2020-02-05 19:41:51 +0700993 "size_t cxxbridge02$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
994 instance, inner,
995 );
996 writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance);
997}
998
David Tolnay7db73692019-10-20 14:51:12 -0400999fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1000 let mut inner = String::new();
1001 for name in &out.namespace {
1002 inner += name;
1003 inner += "::";
1004 }
1005 inner += &ident.to_string();
1006 let instance = inner.replace("::", "$");
1007
1008 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001009 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001010 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001011 writeln!(out, "}}");
1012
1013 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001014 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001015 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001016 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001017}
1018
Myron Ahneba35cf2020-02-05 19:41:51 +07001019fn write_rust_vec_impl(out: &mut OutFile, ty: &Type) {
1020 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
1021 let inner = ty.to_typename(&namespace);
1022 let instance = ty.to_mangled(&namespace);
David Tolnay4791f1c2020-03-17 21:53:16 -07001023
Myron Ahneba35cf2020-02-05 19:41:51 +07001024 writeln!(out, "template <>");
1025 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1026 writeln!(
1027 out,
1028 " return cxxbridge02$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001029 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001030 );
1031 writeln!(out, "}}");
1032
1033 writeln!(out, "template <>");
1034 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
1035 writeln!(out, " return cxxbridge02$rust_vec${}$len(this);", instance);
1036 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001037}
1038
1039fn write_unique_ptr(out: &mut OutFile, ty: &Type, types: &Types) {
1040 out.include.utility = true;
1041 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
1042 let inner = ty.to_typename(&namespace);
1043 let instance = ty.to_mangled(&namespace);
David Tolnay7db73692019-10-20 14:51:12 -04001044
David Tolnay8c730492020-03-13 01:29:06 -07001045 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1046 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001047 writeln!(
1048 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001049 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001050 inner,
1051 );
1052 writeln!(
1053 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001054 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001055 inner,
1056 );
1057 writeln!(
1058 out,
David Tolnay8c730492020-03-13 01:29:06 -07001059 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001060 instance, inner,
1061 );
David Tolnay7e219b82020-03-01 13:14:51 -08001062 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001063 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001064 match ty {
1065 Type::Ident(ident) if types.structs.contains_key(ident) => {
1066 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001067 out,
1068 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1069 instance, inner, inner,
1070 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001071 writeln!(
1072 out,
1073 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1074 inner, inner,
1075 );
1076 writeln!(out, "}}");
1077 }
1078 _ => (),
David Tolnay53838912020-04-09 20:56:44 -07001079 }
David Tolnay7db73692019-10-20 14:51:12 -04001080 writeln!(
1081 out,
David Tolnay8c730492020-03-13 01:29:06 -07001082 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001083 instance, inner, inner,
1084 );
David Tolnay7e219b82020-03-01 13:14:51 -08001085 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001086 writeln!(out, "}}");
1087 writeln!(
1088 out,
David Tolnay8c730492020-03-13 01:29:06 -07001089 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001090 inner, instance, inner,
1091 );
1092 writeln!(out, " return ptr.get();");
1093 writeln!(out, "}}");
1094 writeln!(
1095 out,
David Tolnay8c730492020-03-13 01:29:06 -07001096 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001097 inner, instance, inner,
1098 );
1099 writeln!(out, " return ptr.release();");
1100 writeln!(out, "}}");
1101 writeln!(
1102 out,
David Tolnay8c730492020-03-13 01:29:06 -07001103 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001104 instance, inner,
1105 );
1106 writeln!(out, " ptr->~unique_ptr();");
1107 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001108 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001109}
Myron Ahneba35cf2020-02-05 19:41:51 +07001110
1111fn write_vector(out: &mut OutFile, ident: &Ident) {
1112 let mut inner = String::new();
1113 // Do not apply namespace to built-in type
1114 let is_user_type = Atom::from(ident).is_none();
1115 if is_user_type {
1116 for name in &out.namespace {
1117 inner += name;
1118 inner += "::";
1119 }
1120 }
1121 let mut instance = inner.clone();
1122 if let Some(ti) = Atom::from(ident) {
1123 inner += ti.to_cxx();
1124 } else {
1125 inner += &ident.to_string();
1126 };
1127 instance += &ident.to_string();
1128 let instance = instance.replace("::", "$");
1129
1130 writeln!(out, "#ifndef CXXBRIDGE02_vector_{}", instance);
1131 writeln!(out, "#define CXXBRIDGE02_vector_{}", instance);
1132 writeln!(
1133 out,
David Tolnaya83247c2020-04-24 14:36:10 -07001134 "size_t cxxbridge02$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001135 instance, inner,
1136 );
1137 writeln!(out, " return s.size();");
1138 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001139 writeln!(
1140 out,
David Tolnaydd3f6342020-04-24 14:38:55 -07001141 "const {} &cxxbridge02$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001142 inner, instance, inner,
1143 );
David Tolnaydd3f6342020-04-24 14:38:55 -07001144 writeln!(out, " return s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001145 writeln!(out, "}}");
David Tolnaycc75ad22020-04-24 14:45:16 -07001146 writeln!(
1147 out,
1148 "void cxxbridge02$std$vector${}$push_back(::std::vector<{}> &s, const {} &item) noexcept {{",
1149 instance, inner, inner
1150 );
1151 writeln!(out, " s.push_back(item);");
1152 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001153 writeln!(out, "#endif // CXXBRIDGE02_vector_{}", instance);
1154}