blob: ccd6c2fb8863d5b25933ee728a8c053f57d8118c [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 {
David Tolnay313b10e2020-04-25 16:30:51 -0700189 let bitcopy = match arg.ty {
190 Type::RustVec(_) => true,
191 _ => arg.ty == RustString,
192 };
193 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700194 needs_unsafe_bitcopy = true;
195 break;
196 }
David Tolnay09011c32020-03-06 14:40:28 -0800197 }
198 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199 Api::RustFunction(efn) if !out.header => {
200 if efn.throws {
201 out.include.exception = true;
202 needs_rust_error = true;
203 }
204 for arg in &efn.args {
205 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
206 needs_manually_drop = true;
207 break;
208 }
209 }
210 if let Some(ret) = &efn.ret {
211 if types.needs_indirect_abi(ret) {
212 needs_maybe_uninit = true;
213 }
David Tolnayf51447e2020-03-06 14:14:27 -0800214 }
215 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700216 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800217 }
218 }
219
David Tolnay750755e2020-03-01 13:04:08 -0800220 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700221 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800222
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 if needs_rust_string
224 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700225 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700227 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700228 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700229 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700230 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700231 || needs_unsafe_bitcopy
232 || needs_manually_drop
233 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700234 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 {
David Tolnay736cbca2020-03-11 16:49:18 -0700236 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800237 }
238
David Tolnayd1402742020-03-25 22:21:42 -0700239 if needs_rust_string {
240 out.next_section();
241 writeln!(out, "struct unsafe_bitcopy_t;");
242 }
243
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
245 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700246 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
Myron Ahneba35cf2020-02-05 19:41:51 +0700248 write_header_section(out, needs_rust_vec, "CXXBRIDGE02_RUST_VEC");
David Tolnay75dca2e2020-03-25 20:17:52 -0700249 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700250 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnay7c295462020-04-25 12:45:07 -0700251 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700252 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800253
254 if needs_manually_drop {
255 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700256 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800257 writeln!(out, "template <typename T>");
258 writeln!(out, "union ManuallyDrop {{");
259 writeln!(out, " T value;");
260 writeln!(
261 out,
262 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
263 );
264 writeln!(out, " ~ManuallyDrop() {{}}");
265 writeln!(out, "}};");
266 }
267
David Tolnay09011c32020-03-06 14:40:28 -0800268 if needs_maybe_uninit {
269 out.next_section();
270 writeln!(out, "template <typename T>");
271 writeln!(out, "union MaybeUninit {{");
272 writeln!(out, " T value;");
273 writeln!(out, " MaybeUninit() {{}}");
274 writeln!(out, " ~MaybeUninit() {{}}");
275 writeln!(out, "}};");
276 }
277
David Tolnay3e3e0af2020-03-17 22:42:49 -0700278 out.end_block("namespace cxxbridge02");
279
David Tolnay5d121442020-03-17 22:14:40 -0700280 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700281 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700282 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700283 out.include.type_traits = true;
284 out.include.utility = true;
285 writeln!(out, "class missing {{}};");
286 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700288 writeln!(out, "template <typename Try, typename Fail>");
289 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700290 writeln!(
291 out,
David Tolnay04722332020-03-18 11:31:54 -0700292 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700293 );
David Tolnay04722332020-03-18 11:31:54 -0700294 writeln!(out, " missing>::value>::type");
295 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700296 writeln!(out, " func();");
297 writeln!(out, "}} catch (const ::std::exception &e) {{");
298 writeln!(out, " fail(e.what());");
299 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700300 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700301 }
302
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800303 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400304}
305
David Tolnayb7a7cb62020-03-17 21:18:40 -0700306fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700307 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700308 if needed {
309 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700310 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700311 if !line.trim_start().starts_with("//") {
312 writeln!(out, "{}", line);
313 }
314 }
315 }
316}
317
David Tolnay7db73692019-10-20 14:51:12 -0400318fn write_struct(out: &mut OutFile, strct: &Struct) {
319 for line in strct.doc.to_string().lines() {
320 writeln!(out, "//{}", line);
321 }
322 writeln!(out, "struct {} final {{", strct.ident);
323 for field in &strct.fields {
324 write!(out, " ");
325 write_type_space(out, &field.ty);
326 writeln!(out, "{};", field.ident);
327 }
328 writeln!(out, "}};");
329}
330
331fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
332 writeln!(out, "struct {};", ident);
333}
334
David Tolnay8861bee2020-01-20 18:39:24 -0800335fn write_struct_using(out: &mut OutFile, ident: &Ident) {
336 writeln!(out, "using {} = {};", ident, ident);
337}
338
David Tolnayc1fe0052020-04-17 15:15:06 -0700339fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700340 for line in ety.doc.to_string().lines() {
341 writeln!(out, "//{}", line);
342 }
343 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700344 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700345 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700346 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700347 write!(out, " ");
348 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700349 let local_name = method.ident.to_string();
350 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700351 writeln!(out, ";");
352 }
353 writeln!(out, "}};");
354}
355
David Tolnayebef4a22020-03-17 15:33:47 -0700356fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
357 let mut has_cxx_throws = false;
358 for api in apis {
359 if let Api::CxxFunction(efn) = api {
360 if efn.throws {
361 has_cxx_throws = true;
362 break;
363 }
364 }
365 }
366
367 if has_cxx_throws {
368 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700369 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700370 out,
371 "const char *cxxbridge02$exception(const char *, size_t);",
372 );
373 }
374}
375
David Tolnay7db73692019-10-20 14:51:12 -0400376fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700377 if efn.throws {
378 write!(out, "::rust::Str::Repr ");
379 } else {
David Tolnay99642622020-03-25 13:07:35 -0700380 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700381 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700382 let mangled = mangle::extern_fn(&out.namespace, efn);
383 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700384 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700385 if receiver.mutability.is_none() {
386 write!(out, "const ");
387 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700388 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700389 }
David Tolnay7db73692019-10-20 14:51:12 -0400390 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700391 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400392 write!(out, ", ");
393 }
David Tolnaya46a2372020-03-06 10:03:48 -0800394 if arg.ty == RustString {
395 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700396 } else if let Type::RustVec(_) = arg.ty {
397 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800398 }
David Tolnay7db73692019-10-20 14:51:12 -0400399 write_extern_arg(out, arg, types);
400 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700401 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400402 if indirect_return {
403 if !efn.args.is_empty() {
404 write!(out, ", ");
405 }
David Tolnay99642622020-03-25 13:07:35 -0700406 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400407 write!(out, "*return$");
408 }
409 writeln!(out, ") noexcept {{");
410 write!(out, " ");
411 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700412 match &efn.receiver {
413 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700414 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 for (i, arg) in efn.args.iter().enumerate() {
417 if i > 0 {
418 write!(out, ", ");
419 }
420 write_type(out, &arg.ty);
421 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700422 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700423 if let Some(receiver) = &efn.receiver {
424 if receiver.mutability.is_none() {
425 write!(out, " const");
426 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700427 }
428 write!(out, " = ");
429 match &efn.receiver {
430 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700431 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700432 }
433 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400434 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700435 if efn.throws {
436 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700437 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700438 writeln!(out, " [&] {{");
439 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700440 }
David Tolnay7db73692019-10-20 14:51:12 -0400441 if indirect_return {
442 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700443 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400444 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700445 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400446 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700447 }
448 match &efn.ret {
449 Some(Type::Ref(_)) => write!(out, "&"),
450 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700451 Some(Type::SliceRefU8(_)) if !indirect_return => {
452 write!(out, "::rust::Slice<uint8_t>::Repr(")
453 }
David Tolnay99642622020-03-25 13:07:35 -0700454 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400455 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700456 match &efn.receiver {
457 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700458 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700459 }
David Tolnay7db73692019-10-20 14:51:12 -0400460 for (i, arg) in efn.args.iter().enumerate() {
461 if i > 0 {
462 write!(out, ", ");
463 }
464 if let Type::RustBox(_) = &arg.ty {
465 write_type(out, &arg.ty);
466 write!(out, "::from_raw({})", arg.ident);
467 } else if let Type::UniquePtr(_) = &arg.ty {
468 write_type(out, &arg.ty);
469 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800470 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800471 write!(
472 out,
473 "::rust::String(::rust::unsafe_bitcopy, *{})",
474 arg.ident,
475 );
David Tolnay313b10e2020-04-25 16:30:51 -0700476 } else if let Type::RustVec(_) = arg.ty {
477 write_type(out, &arg.ty);
478 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400479 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700480 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800481 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400482 } else {
483 write!(out, "{}", arg.ident);
484 }
485 }
486 write!(out, ")");
487 match &efn.ret {
488 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
489 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700490 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700491 out,
492 " /* Use RVO to convert to r-value and move construct */"
493 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700494 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400495 _ => {}
496 }
497 if indirect_return {
498 write!(out, ")");
499 }
500 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700501 if efn.throws {
502 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700503 writeln!(out, " throw$.ptr = nullptr;");
504 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700505 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700506 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700507 writeln!(
508 out,
David Tolnay5d121442020-03-17 22:14:40 -0700509 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700510 );
David Tolnay5d121442020-03-17 22:14:40 -0700511 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700512 writeln!(out, " return throw$;");
513 }
David Tolnay7db73692019-10-20 14:51:12 -0400514 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700515 for arg in &efn.args {
516 if let Type::Fn(f) = &arg.ty {
517 let var = &arg.ident;
518 write_function_pointer_trampoline(out, efn, var, f, types);
519 }
520 }
521}
522
523fn write_function_pointer_trampoline(
524 out: &mut OutFile,
525 efn: &ExternFn,
526 var: &Ident,
527 f: &Signature,
528 types: &Types,
529) {
530 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700531 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700532 let indirect_call = true;
533 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
534
535 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700536 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700537 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400538}
539
540fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700541 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700542 let indirect_call = false;
543 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
544}
545
546fn write_rust_function_decl_impl(
547 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700548 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700549 sig: &Signature,
550 types: &Types,
551 indirect_call: bool,
552) {
553 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700554 write!(out, "::rust::Str::Repr ");
555 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700556 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700557 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 write!(out, "{}(", link_name);
559 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700560 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700561 if receiver.mutability.is_none() {
562 write!(out, "const ");
563 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700564 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700565 needs_comma = true;
566 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700567 for arg in &sig.args {
568 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400569 write!(out, ", ");
570 }
571 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700572 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400573 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700574 if indirect_return(sig, types) {
575 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400576 write!(out, ", ");
577 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700578 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400579 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 needs_comma = true;
581 }
582 if indirect_call {
583 if needs_comma {
584 write!(out, ", ");
585 }
586 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400587 }
588 writeln!(out, ") noexcept;");
589}
590
591fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400592 for line in efn.doc.to_string().lines() {
593 writeln!(out, "//{}", line);
594 }
David Tolnaya73853b2020-04-20 01:19:56 -0700595 let local_name = match &efn.sig.receiver {
596 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700597 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700598 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700599 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 let indirect_call = false;
601 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
602}
603
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700604fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700605 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700606 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700607 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700608 indirect_call: bool,
609) {
610 write_return_type(out, &sig.ret);
611 write!(out, "{}(", local_name);
612 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400613 if i > 0 {
614 write!(out, ", ");
615 }
616 write_type_space(out, &arg.ty);
617 write!(out, "{}", arg.ident);
618 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700619 if indirect_call {
620 if !sig.args.is_empty() {
621 write!(out, ", ");
622 }
623 write!(out, "void *extern$");
624 }
David Tolnay1e548172020-03-16 13:37:09 -0700625 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700626 if let Some(receiver) = &sig.receiver {
627 if receiver.mutability.is_none() {
628 write!(out, " const");
629 }
630 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700631 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700632 write!(out, " noexcept");
633 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700634}
635
636fn write_rust_function_shim_impl(
637 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700638 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700639 sig: &Signature,
640 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700641 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700642 indirect_call: bool,
643) {
644 if out.header && sig.receiver.is_some() {
645 // We've already defined this inside the struct.
646 return;
647 }
David Tolnaya73853b2020-04-20 01:19:56 -0700648 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400649 if out.header {
650 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700651 return;
David Tolnay7db73692019-10-20 14:51:12 -0400652 }
David Tolnay439cde22020-04-20 00:46:25 -0700653 writeln!(out, " {{");
654 for arg in &sig.args {
655 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
656 out.include.utility = true;
657 write!(out, " ::rust::ManuallyDrop<");
658 write_type(out, &arg.ty);
659 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
660 }
661 }
662 write!(out, " ");
663 let indirect_return = indirect_return(sig, types);
664 if indirect_return {
665 write!(out, "::rust::MaybeUninit<");
666 write_type(out, sig.ret.as_ref().unwrap());
667 writeln!(out, "> return$;");
668 write!(out, " ");
669 } else if let Some(ret) = &sig.ret {
670 write!(out, "return ");
671 match ret {
672 Type::RustBox(_) => {
673 write_type(out, ret);
674 write!(out, "::from_raw(");
675 }
676 Type::UniquePtr(_) => {
677 write_type(out, ret);
678 write!(out, "(");
679 }
680 Type::Ref(_) => write!(out, "*"),
681 _ => {}
682 }
683 }
684 if sig.throws {
685 write!(out, "::rust::Str::Repr error$ = ");
686 }
687 write!(out, "{}(", invoke);
688 if sig.receiver.is_some() {
689 write!(out, "*this");
690 }
691 for (i, arg) in sig.args.iter().enumerate() {
692 if i > 0 || sig.receiver.is_some() {
693 write!(out, ", ");
694 }
695 match &arg.ty {
696 Type::Str(_) => write!(out, "::rust::Str::Repr("),
697 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
698 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
699 _ => {}
700 }
701 write!(out, "{}", arg.ident);
702 match &arg.ty {
703 Type::RustBox(_) => write!(out, ".into_raw()"),
704 Type::UniquePtr(_) => write!(out, ".release()"),
705 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
706 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
707 _ => {}
708 }
709 }
710 if indirect_return {
711 if !sig.args.is_empty() {
712 write!(out, ", ");
713 }
714 write!(out, "&return$.value");
715 }
716 if indirect_call {
717 if !sig.args.is_empty() || indirect_return {
718 write!(out, ", ");
719 }
720 write!(out, "extern$");
721 }
722 write!(out, ")");
723 if let Some(ret) = &sig.ret {
724 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
725 write!(out, ")");
726 }
727 }
728 writeln!(out, ";");
729 if sig.throws {
730 writeln!(out, " if (error$.ptr) {{");
731 writeln!(out, " throw ::rust::Error(error$);");
732 writeln!(out, " }}");
733 }
734 if indirect_return {
735 out.include.utility = true;
736 writeln!(out, " return ::std::move(return$.value);");
737 }
738 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400739}
740
741fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
742 match ty {
743 None => write!(out, "void "),
744 Some(ty) => write_type_space(out, ty),
745 }
746}
747
David Tolnay75dca2e2020-03-25 20:17:52 -0700748fn indirect_return(sig: &Signature, types: &Types) -> bool {
749 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700750 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700751 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700752}
753
David Tolnay99642622020-03-25 13:07:35 -0700754fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
755 match ty {
756 Type::RustBox(ty) | Type::UniquePtr(ty) => {
757 write_type_space(out, &ty.inner);
758 write!(out, "*");
759 }
760 Type::Ref(ty) => {
761 if ty.mutability.is_none() {
762 write!(out, "const ");
763 }
764 write_type(out, &ty.inner);
765 write!(out, " *");
766 }
767 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700768 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700769 _ => write_type(out, ty),
770 }
771}
772
773fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
774 write_indirect_return_type(out, ty);
775 match ty {
776 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700777 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700778 _ => write_space_after_type(out, ty),
779 }
780}
781
782fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400783 match ty {
784 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
785 write_type_space(out, &ty.inner);
786 write!(out, "*");
787 }
David Tolnay4a441222020-01-25 16:24:27 -0800788 Some(Type::Ref(ty)) => {
789 if ty.mutability.is_none() {
790 write!(out, "const ");
791 }
792 write_type(out, &ty.inner);
793 write!(out, " *");
794 }
David Tolnay750755e2020-03-01 13:04:08 -0800795 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700796 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400797 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
798 _ => write_return_type(out, ty),
799 }
800}
801
802fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
803 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700804 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400805 write_type_space(out, &ty.inner);
806 write!(out, "*");
807 }
David Tolnay750755e2020-03-01 13:04:08 -0800808 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700809 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400810 _ => write_type_space(out, &arg.ty),
811 }
812 if types.needs_indirect_abi(&arg.ty) {
813 write!(out, "*");
814 }
815 write!(out, "{}", arg.ident);
816}
817
818fn write_type(out: &mut OutFile, ty: &Type) {
819 match ty {
820 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay029f1d62020-04-25 10:19:25 -0700821 Some(Bool) => write!(out, "bool"),
822 Some(U8) => write!(out, "uint8_t"),
823 Some(U16) => write!(out, "uint16_t"),
824 Some(U32) => write!(out, "uint32_t"),
825 Some(U64) => write!(out, "uint64_t"),
826 Some(Usize) => write!(out, "size_t"),
827 Some(I8) => write!(out, "int8_t"),
828 Some(I16) => write!(out, "int16_t"),
829 Some(I32) => write!(out, "int32_t"),
830 Some(I64) => write!(out, "int64_t"),
831 Some(Isize) => write!(out, "::rust::isize"),
832 Some(F32) => write!(out, "float"),
833 Some(F64) => write!(out, "double"),
834 Some(CxxString) => write!(out, "::std::string"),
835 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400836 None => write!(out, "{}", ident),
837 },
838 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800839 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400840 write_type(out, &ty.inner);
841 write!(out, ">");
842 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700843 Type::RustVec(ty) => {
844 write!(out, "::rust::Vec<");
845 write_type(out, &ty.inner);
846 write!(out, ">");
847 }
David Tolnay7db73692019-10-20 14:51:12 -0400848 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800849 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400850 write_type(out, &ptr.inner);
851 write!(out, ">");
852 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700853 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700854 write!(out, "::std::vector<");
855 write_type(out, &ty.inner);
856 write!(out, ">");
857 }
David Tolnay7db73692019-10-20 14:51:12 -0400858 Type::Ref(r) => {
859 if r.mutability.is_none() {
860 write!(out, "const ");
861 }
862 write_type(out, &r.inner);
863 write!(out, " &");
864 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700865 Type::Slice(_) => {
866 // For now, only U8 slices are supported, which are covered separately below
867 unreachable!()
868 }
David Tolnay7db73692019-10-20 14:51:12 -0400869 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800870 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400871 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700872 Type::SliceRefU8(_) => {
873 write!(out, "::rust::Slice<uint8_t>");
874 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700875 Type::Fn(f) => {
876 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
877 match &f.ret {
878 Some(ret) => write_type(out, ret),
879 None => write!(out, "void"),
880 }
881 write!(out, "(");
882 for (i, arg) in f.args.iter().enumerate() {
883 if i > 0 {
884 write!(out, ", ");
885 }
886 write_type(out, &arg.ty);
887 }
888 write!(out, ")>");
889 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700890 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400891 }
892}
893
894fn write_type_space(out: &mut OutFile, ty: &Type) {
895 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700896 write_space_after_type(out, ty);
897}
898
899fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400900 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700901 Type::Ident(_)
902 | Type::RustBox(_)
903 | Type::UniquePtr(_)
904 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700905 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700906 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700907 | Type::SliceRefU8(_)
908 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400909 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700910 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400911 }
912}
913
David Tolnaycd08c442020-04-25 10:16:33 -0700914// Only called for legal referent types of unique_ptr and element types of
915// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700916fn to_typename(namespace: &Namespace, ty: &Type) -> String {
917 match ty {
918 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700919 let mut path = String::new();
920 for name in namespace {
921 path += name;
922 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700923 }
David Tolnaycd08c442020-04-25 10:16:33 -0700924 path += &ident.to_string();
925 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700926 }
927 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700928 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700929 }
930}
931
David Tolnayacdf20a2020-04-25 12:40:53 -0700932// Only called for legal referent types of unique_ptr and element types of
933// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700934fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
935 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700936 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700937 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700938 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700939 }
940}
941
David Tolnay7db73692019-10-20 14:51:12 -0400942fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
943 fn allow_unique_ptr(ident: &Ident) -> bool {
944 Atom::from(ident).is_none()
945 }
946
947 out.begin_block("extern \"C\"");
948 for ty in types {
949 if let Type::RustBox(ty) = ty {
950 if let Type::Ident(inner) = &ty.inner {
951 out.next_section();
952 write_rust_box_extern(out, inner);
953 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700954 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700955 if let Type::Ident(inner) = &ty.inner {
956 if Atom::from(inner).is_none() {
957 out.next_section();
958 write_rust_vec_extern(out, inner);
959 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700960 }
David Tolnay7db73692019-10-20 14:51:12 -0400961 } else if let Type::UniquePtr(ptr) = ty {
962 if let Type::Ident(inner) = &ptr.inner {
963 if allow_unique_ptr(inner) {
964 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700965 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700966 }
967 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700968 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700969 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -0700970 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +0700971 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -0700972 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400973 }
974 }
975 }
976 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800977 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400978
David Tolnay750755e2020-03-01 13:04:08 -0800979 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700980 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400981 for ty in types {
982 if let Type::RustBox(ty) = ty {
983 if let Type::Ident(inner) = &ty.inner {
984 write_rust_box_impl(out, inner);
985 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700986 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700987 if let Type::Ident(inner) = &ty.inner {
988 if Atom::from(inner).is_none() {
989 write_rust_vec_impl(out, inner);
990 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 }
David Tolnay7db73692019-10-20 14:51:12 -0400992 }
993 }
David Tolnay8c730492020-03-13 01:29:06 -0700994 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800995 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400996}
997
998fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
999 let mut inner = String::new();
1000 for name in &out.namespace {
1001 inner += name;
1002 inner += "::";
1003 }
1004 inner += &ident.to_string();
1005 let instance = inner.replace("::", "$");
1006
David Tolnay8c730492020-03-13 01:29:06 -07001007 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
1008 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001009 writeln!(
1010 out,
David Tolnay8c730492020-03-13 01:29:06 -07001011 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001012 instance, inner,
1013 );
1014 writeln!(
1015 out,
David Tolnay8c730492020-03-13 01:29:06 -07001016 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001017 instance, inner,
1018 );
David Tolnay8c730492020-03-13 01:29:06 -07001019 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001020}
1021
David Tolnay6787be62020-04-25 11:01:02 -07001022fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1023 let element = Type::Ident(element.clone());
1024 let inner = to_typename(&out.namespace, &element);
1025 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001026
1027 writeln!(out, "#ifndef CXXBRIDGE02_RUST_VEC_{}", instance);
1028 writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance);
1029 writeln!(
1030 out,
1031 "void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
1032 instance, inner,
1033 );
1034 writeln!(
1035 out,
Myron Ahneba35cf2020-02-05 19:41:51 +07001036 "size_t cxxbridge02$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
1037 instance, inner,
1038 );
David Tolnay219c0792020-04-24 20:31:37 -07001039 writeln!(
1040 out,
1041 "const {} *cxxbridge02$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
1042 inner, instance,
1043 );
David Tolnay503d0192020-04-24 22:18:56 -07001044 writeln!(
1045 out,
1046 "size_t cxxbridge02$rust_vec${}$stride() noexcept;",
1047 instance,
1048 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001049 writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance);
1050}
1051
David Tolnay7db73692019-10-20 14:51:12 -04001052fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1053 let mut inner = String::new();
1054 for name in &out.namespace {
1055 inner += name;
1056 inner += "::";
1057 }
1058 inner += &ident.to_string();
1059 let instance = inner.replace("::", "$");
1060
1061 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001062 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001063 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001064 writeln!(out, "}}");
1065
1066 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001067 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001068 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001069 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001070}
1071
David Tolnay6787be62020-04-25 11:01:02 -07001072fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1073 let element = Type::Ident(element.clone());
1074 let inner = to_typename(&out.namespace, &element);
1075 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001076
Myron Ahneba35cf2020-02-05 19:41:51 +07001077 writeln!(out, "template <>");
1078 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1079 writeln!(
1080 out,
1081 " return cxxbridge02$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001082 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 );
1084 writeln!(out, "}}");
1085
1086 writeln!(out, "template <>");
1087 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
1088 writeln!(out, " return cxxbridge02$rust_vec${}$len(this);", instance);
1089 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001090
1091 writeln!(out, "template <>");
1092 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1093 writeln!(
1094 out,
1095 " return cxxbridge02$rust_vec${}$data(this);",
1096 instance,
1097 );
1098 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001099
1100 writeln!(out, "template <>");
1101 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
1102 writeln!(out, " return cxxbridge02$rust_vec${}$stride();", instance);
1103 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001104}
1105
David Tolnay63da4d32020-04-25 09:41:12 -07001106fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1107 let ty = Type::Ident(ident.clone());
1108 let instance = to_mangled(&out.namespace, &ty);
1109
1110 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1111 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1112
1113 write_unique_ptr_common(out, &ty, types);
1114
1115 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1116}
1117
1118// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1119fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001120 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001121 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001122 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001123
David Tolnay63da4d32020-04-25 09:41:12 -07001124 let can_construct_from_value = match ty {
1125 Type::Ident(ident) => types.structs.contains_key(ident),
1126 _ => false,
1127 };
1128
David Tolnay7db73692019-10-20 14:51:12 -04001129 writeln!(
1130 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001131 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001132 inner,
1133 );
1134 writeln!(
1135 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001136 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001137 inner,
1138 );
1139 writeln!(
1140 out,
David Tolnay8c730492020-03-13 01:29:06 -07001141 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001142 instance, inner,
1143 );
David Tolnay7e219b82020-03-01 13:14:51 -08001144 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001145 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001146 if can_construct_from_value {
1147 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001148 out,
1149 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1150 instance, inner, inner,
1151 );
David Tolnay63da4d32020-04-25 09:41:12 -07001152 writeln!(
1153 out,
1154 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1155 inner, inner,
1156 );
1157 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001158 }
David Tolnay7db73692019-10-20 14:51:12 -04001159 writeln!(
1160 out,
David Tolnay8c730492020-03-13 01:29:06 -07001161 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001162 instance, inner, inner,
1163 );
David Tolnay7e219b82020-03-01 13:14:51 -08001164 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001165 writeln!(out, "}}");
1166 writeln!(
1167 out,
David Tolnay8c730492020-03-13 01:29:06 -07001168 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001169 inner, instance, inner,
1170 );
1171 writeln!(out, " return ptr.get();");
1172 writeln!(out, "}}");
1173 writeln!(
1174 out,
David Tolnay8c730492020-03-13 01:29:06 -07001175 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001176 inner, instance, inner,
1177 );
1178 writeln!(out, " return ptr.release();");
1179 writeln!(out, "}}");
1180 writeln!(
1181 out,
David Tolnay8c730492020-03-13 01:29:06 -07001182 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001183 instance, inner,
1184 );
1185 writeln!(out, " ptr->~unique_ptr();");
1186 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001187}
Myron Ahneba35cf2020-02-05 19:41:51 +07001188
David Tolnay92105da2020-04-25 11:15:31 -07001189fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001190 let element = Type::Ident(element.clone());
1191 let inner = to_typename(&out.namespace, &element);
1192 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001193
David Tolnay63da4d32020-04-25 09:41:12 -07001194 writeln!(out, "#ifndef CXXBRIDGE02_VECTOR_{}", instance);
1195 writeln!(out, "#define CXXBRIDGE02_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001196 writeln!(
1197 out,
David Tolnaya83247c2020-04-24 14:36:10 -07001198 "size_t cxxbridge02$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001199 instance, inner,
1200 );
1201 writeln!(out, " return s.size();");
1202 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001203 writeln!(
1204 out,
David Tolnaydd3f6342020-04-24 14:38:55 -07001205 "const {} &cxxbridge02$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001206 inner, instance, inner,
1207 );
David Tolnaydd3f6342020-04-24 14:38:55 -07001208 writeln!(out, " return s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001209 writeln!(out, "}}");
David Tolnaycc75ad22020-04-24 14:45:16 -07001210 writeln!(
1211 out,
1212 "void cxxbridge02$std$vector${}$push_back(::std::vector<{}> &s, const {} &item) noexcept {{",
1213 instance, inner, inner
1214 );
1215 writeln!(out, " s.push_back(item);");
1216 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001217
1218 write_unique_ptr_common(out, vector_ty, types);
1219
1220 writeln!(out, "#endif // CXXBRIDGE02_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001221}