blob: 8fc698c795b22aee8dd221f84ed0b73c4f39e6ea [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 Tolnaya73853b2020-04-20 01:19:56 -07006use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070011 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
14 opt: Opt,
15 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
20 if header {
21 writeln!(out, "#pragma once");
22 }
23
David Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080027 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
35 for name in &namespace {
36 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070045 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040046 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
65 write_struct(out, strct);
66 }
David Tolnayc1fe0052020-04-17 15:15:06 -070067 Api::RustType(ety) => {
68 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070069 out.next_section();
70 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070071 }
David Tolnayc1fe0052020-04-17 15:15:06 -070072 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040074 }
75 }
76
77 if !header {
78 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070079 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040080 for api in apis {
81 let (efn, write): (_, fn(_, _, _)) = match api {
82 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
83 Api::RustFunction(efn) => (efn, write_rust_function_decl),
84 _ => continue,
85 };
86 out.next_section();
87 write(out, efn, types);
88 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080089 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040090 }
91
92 for api in apis {
93 if let Api::RustFunction(efn) = api {
94 out.next_section();
95 write_rust_function_shim(out, efn, types);
96 }
97 }
98
99 out.next_section();
100 for name in namespace.iter().rev() {
101 writeln!(out, "}} // namespace {}", name);
102 }
103
104 if !header {
105 out.next_section();
106 write_generic_instantiations(out, types);
107 }
108
David Tolnay9c68b1a2020-03-06 11:12:55 -0800109 out.prepend(out.include.to_string());
110
David Tolnay7db73692019-10-20 14:51:12 -0400111 out_file
112}
113
114fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400115 for ty in types {
116 match ty {
117 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700118 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
119 | Some(I64) => out.include.cstdint = true,
120 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800121 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700122 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400123 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800124 Type::RustBox(_) => out.include.type_traits = true,
125 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700126 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700127 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700128 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400129 }
130 }
David Tolnay7db73692019-10-20 14:51:12 -0400131}
132
David Tolnayf51447e2020-03-06 14:14:27 -0800133fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700134 let mut needs_rust_string = false;
135 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700136 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400137 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700138 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700139 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -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 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700147 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700148 out.include.array = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700149 out.include.type_traits = true;
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()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700481 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700482 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 {
David Tolnay4377a9e2020-04-24 15:20:26 -0700795 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(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) {
David Tolnay029f1d62020-04-25 10:19:25 -0700812 Some(Bool) => write!(out, "bool"),
813 Some(U8) => write!(out, "uint8_t"),
814 Some(U16) => write!(out, "uint16_t"),
815 Some(U32) => write!(out, "uint32_t"),
816 Some(U64) => write!(out, "uint64_t"),
817 Some(Usize) => write!(out, "size_t"),
818 Some(I8) => write!(out, "int8_t"),
819 Some(I16) => write!(out, "int16_t"),
820 Some(I32) => write!(out, "int32_t"),
821 Some(I64) => write!(out, "int64_t"),
822 Some(Isize) => write!(out, "::rust::isize"),
823 Some(F32) => write!(out, "float"),
824 Some(F64) => write!(out, "double"),
825 Some(CxxString) => write!(out, "::std::string"),
826 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400827 None => write!(out, "{}", ident),
828 },
829 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800830 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400831 write_type(out, &ty.inner);
832 write!(out, ">");
833 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700834 Type::RustVec(ty) => {
835 write!(out, "::rust::Vec<");
836 write_type(out, &ty.inner);
837 write!(out, ">");
838 }
David Tolnay7db73692019-10-20 14:51:12 -0400839 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800840 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400841 write_type(out, &ptr.inner);
842 write!(out, ">");
843 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700844 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700845 write!(out, "::std::vector<");
846 write_type(out, &ty.inner);
847 write!(out, ">");
848 }
David Tolnay7db73692019-10-20 14:51:12 -0400849 Type::Ref(r) => {
850 if r.mutability.is_none() {
851 write!(out, "const ");
852 }
853 write_type(out, &r.inner);
854 write!(out, " &");
855 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700856 Type::Slice(_) => {
857 // For now, only U8 slices are supported, which are covered separately below
858 unreachable!()
859 }
David Tolnay7db73692019-10-20 14:51:12 -0400860 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800861 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400862 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700863 Type::SliceRefU8(_) => {
864 write!(out, "::rust::Slice<uint8_t>");
865 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700866 Type::Fn(f) => {
867 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
868 match &f.ret {
869 Some(ret) => write_type(out, ret),
870 None => write!(out, "void"),
871 }
872 write!(out, "(");
873 for (i, arg) in f.args.iter().enumerate() {
874 if i > 0 {
875 write!(out, ", ");
876 }
877 write_type(out, &arg.ty);
878 }
879 write!(out, ")>");
880 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700881 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400882 }
883}
884
885fn write_type_space(out: &mut OutFile, ty: &Type) {
886 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700887 write_space_after_type(out, ty);
888}
889
890fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400891 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700892 Type::Ident(_)
893 | Type::RustBox(_)
894 | Type::UniquePtr(_)
895 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700896 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700897 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700898 | Type::SliceRefU8(_)
899 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400900 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700901 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400902 }
903}
904
David Tolnaycd08c442020-04-25 10:16:33 -0700905// Only called for legal referent types of unique_ptr and element types of
906// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700907fn to_typename(namespace: &Namespace, ty: &Type) -> String {
908 match ty {
909 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700910 let mut path = String::new();
911 for name in namespace {
912 path += name;
913 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700914 }
David Tolnaycd08c442020-04-25 10:16:33 -0700915 path += &ident.to_string();
916 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700917 }
918 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700919 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700920 }
921}
922
David Tolnayacdf20a2020-04-25 12:40:53 -0700923// Only called for legal referent types of unique_ptr and element types of
924// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700925fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
926 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700927 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700928 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700929 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700930 }
931}
932
David Tolnay7db73692019-10-20 14:51:12 -0400933fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
934 fn allow_unique_ptr(ident: &Ident) -> bool {
935 Atom::from(ident).is_none()
936 }
937
938 out.begin_block("extern \"C\"");
939 for ty in types {
940 if let Type::RustBox(ty) = ty {
941 if let Type::Ident(inner) = &ty.inner {
942 out.next_section();
943 write_rust_box_extern(out, inner);
944 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700945 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700946 if let Type::Ident(inner) = &ty.inner {
947 if Atom::from(inner).is_none() {
948 out.next_section();
949 write_rust_vec_extern(out, inner);
950 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700951 }
David Tolnay7db73692019-10-20 14:51:12 -0400952 } else if let Type::UniquePtr(ptr) = ty {
953 if let Type::Ident(inner) = &ptr.inner {
954 if allow_unique_ptr(inner) {
955 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700956 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700957 }
958 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700959 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700960 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -0700961 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +0700962 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -0700963 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400964 }
965 }
966 }
967 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800968 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400969
David Tolnay750755e2020-03-01 13:04:08 -0800970 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700971 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400972 for ty in types {
973 if let Type::RustBox(ty) = ty {
974 if let Type::Ident(inner) = &ty.inner {
975 write_rust_box_impl(out, inner);
976 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700977 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700978 if let Type::Ident(inner) = &ty.inner {
979 if Atom::from(inner).is_none() {
980 write_rust_vec_impl(out, inner);
981 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700982 }
David Tolnay7db73692019-10-20 14:51:12 -0400983 }
984 }
David Tolnay8c730492020-03-13 01:29:06 -0700985 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800986 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400987}
988
989fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
990 let mut inner = String::new();
991 for name in &out.namespace {
992 inner += name;
993 inner += "::";
994 }
995 inner += &ident.to_string();
996 let instance = inner.replace("::", "$");
997
David Tolnay8c730492020-03-13 01:29:06 -0700998 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
999 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001000 writeln!(
1001 out,
David Tolnay8c730492020-03-13 01:29:06 -07001002 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001003 instance, inner,
1004 );
1005 writeln!(
1006 out,
David Tolnay8c730492020-03-13 01:29:06 -07001007 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001008 instance, inner,
1009 );
David Tolnay8c730492020-03-13 01:29:06 -07001010 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001011}
1012
David Tolnay6787be62020-04-25 11:01:02 -07001013fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1014 let element = Type::Ident(element.clone());
1015 let inner = to_typename(&out.namespace, &element);
1016 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001017
1018 writeln!(out, "#ifndef CXXBRIDGE02_RUST_VEC_{}", instance);
1019 writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance);
1020 writeln!(
1021 out,
1022 "void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
1023 instance, inner,
1024 );
1025 writeln!(
1026 out,
Myron Ahneba35cf2020-02-05 19:41:51 +07001027 "size_t cxxbridge02$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
1028 instance, inner,
1029 );
David Tolnay219c0792020-04-24 20:31:37 -07001030 writeln!(
1031 out,
1032 "const {} *cxxbridge02$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
1033 inner, instance,
1034 );
David Tolnay503d0192020-04-24 22:18:56 -07001035 writeln!(
1036 out,
1037 "size_t cxxbridge02$rust_vec${}$stride() noexcept;",
1038 instance,
1039 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001040 writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance);
1041}
1042
David Tolnay7db73692019-10-20 14:51:12 -04001043fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1044 let mut inner = String::new();
1045 for name in &out.namespace {
1046 inner += name;
1047 inner += "::";
1048 }
1049 inner += &ident.to_string();
1050 let instance = inner.replace("::", "$");
1051
1052 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001053 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001054 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001055 writeln!(out, "}}");
1056
1057 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001058 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001059 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001060 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001061}
1062
David Tolnay6787be62020-04-25 11:01:02 -07001063fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1064 let element = Type::Ident(element.clone());
1065 let inner = to_typename(&out.namespace, &element);
1066 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001067
Myron Ahneba35cf2020-02-05 19:41:51 +07001068 writeln!(out, "template <>");
1069 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1070 writeln!(
1071 out,
1072 " return cxxbridge02$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001073 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001074 );
1075 writeln!(out, "}}");
1076
1077 writeln!(out, "template <>");
1078 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
1079 writeln!(out, " return cxxbridge02$rust_vec${}$len(this);", instance);
1080 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001081
1082 writeln!(out, "template <>");
1083 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1084 writeln!(
1085 out,
1086 " return cxxbridge02$rust_vec${}$data(this);",
1087 instance,
1088 );
1089 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001090
1091 writeln!(out, "template <>");
1092 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
1093 writeln!(out, " return cxxbridge02$rust_vec${}$stride();", instance);
1094 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001095}
1096
David Tolnay63da4d32020-04-25 09:41:12 -07001097fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1098 let ty = Type::Ident(ident.clone());
1099 let instance = to_mangled(&out.namespace, &ty);
1100
1101 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1102 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1103
1104 write_unique_ptr_common(out, &ty, types);
1105
1106 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1107}
1108
1109// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1110fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001111 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001112 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001113 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001114
David Tolnay63da4d32020-04-25 09:41:12 -07001115 let can_construct_from_value = match ty {
1116 Type::Ident(ident) => types.structs.contains_key(ident),
1117 _ => false,
1118 };
1119
David Tolnay7db73692019-10-20 14:51:12 -04001120 writeln!(
1121 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001122 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001123 inner,
1124 );
1125 writeln!(
1126 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001127 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001128 inner,
1129 );
1130 writeln!(
1131 out,
David Tolnay8c730492020-03-13 01:29:06 -07001132 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001133 instance, inner,
1134 );
David Tolnay7e219b82020-03-01 13:14:51 -08001135 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001136 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001137 if can_construct_from_value {
1138 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001139 out,
1140 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1141 instance, inner, inner,
1142 );
David Tolnay63da4d32020-04-25 09:41:12 -07001143 writeln!(
1144 out,
1145 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1146 inner, inner,
1147 );
1148 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001149 }
David Tolnay7db73692019-10-20 14:51:12 -04001150 writeln!(
1151 out,
David Tolnay8c730492020-03-13 01:29:06 -07001152 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001153 instance, inner, inner,
1154 );
David Tolnay7e219b82020-03-01 13:14:51 -08001155 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001156 writeln!(out, "}}");
1157 writeln!(
1158 out,
David Tolnay8c730492020-03-13 01:29:06 -07001159 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001160 inner, instance, inner,
1161 );
1162 writeln!(out, " return ptr.get();");
1163 writeln!(out, "}}");
1164 writeln!(
1165 out,
David Tolnay8c730492020-03-13 01:29:06 -07001166 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001167 inner, instance, inner,
1168 );
1169 writeln!(out, " return ptr.release();");
1170 writeln!(out, "}}");
1171 writeln!(
1172 out,
David Tolnay8c730492020-03-13 01:29:06 -07001173 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001174 instance, inner,
1175 );
1176 writeln!(out, " ptr->~unique_ptr();");
1177 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001178}
Myron Ahneba35cf2020-02-05 19:41:51 +07001179
David Tolnay92105da2020-04-25 11:15:31 -07001180fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001181 let element = Type::Ident(element.clone());
1182 let inner = to_typename(&out.namespace, &element);
1183 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001184
David Tolnay63da4d32020-04-25 09:41:12 -07001185 writeln!(out, "#ifndef CXXBRIDGE02_VECTOR_{}", instance);
1186 writeln!(out, "#define CXXBRIDGE02_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001187 writeln!(
1188 out,
David Tolnaya83247c2020-04-24 14:36:10 -07001189 "size_t cxxbridge02$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001190 instance, inner,
1191 );
1192 writeln!(out, " return s.size();");
1193 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 writeln!(
1195 out,
David Tolnaydd3f6342020-04-24 14:38:55 -07001196 "const {} &cxxbridge02$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001197 inner, instance, inner,
1198 );
David Tolnaydd3f6342020-04-24 14:38:55 -07001199 writeln!(out, " return s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001200 writeln!(out, "}}");
David Tolnaycc75ad22020-04-24 14:45:16 -07001201 writeln!(
1202 out,
1203 "void cxxbridge02$std$vector${}$push_back(::std::vector<{}> &s, const {} &item) noexcept {{",
1204 instance, inner, inner
1205 );
1206 writeln!(out, " s.push_back(item);");
1207 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001208
1209 write_unique_ptr_common(out, vector_ty, types);
1210
1211 writeln!(out, "#endif // CXXBRIDGE02_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001212}