blob: faf33116fade24f944e2b5c5f26cbdf1b85c4511 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
Myron Ahneba35cf2020-02-05 19:41:51 +07004use crate::syntax::mangled::ToMangled;
David Tolnay08419302020-04-19 20:38:20 -07005use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07006use crate::syntax::symbol::Symbol;
Myron Ahneba35cf2020-02-05 19:41:51 +07007use crate::syntax::typename::ToTypename;
David Tolnaya73853b2020-04-20 01:19:56 -07008use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04009use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -070010use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnay33d30292020-03-18 18:02:02 -070012pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070013 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070014 apis: &[Api],
15 types: &Types,
16 opt: Opt,
17 header: bool,
18) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040019 let mut out_file = OutFile::new(namespace.clone(), header);
20 let out = &mut out_file;
21
22 if header {
23 writeln!(out, "#pragma once");
24 }
25
David Tolnay33d30292020-03-18 18:02:02 -070026 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040027 for api in apis {
28 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080029 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040030 }
31 }
32
33 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080034 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040035
David Tolnay7db73692019-10-20 14:51:12 -040036 out.next_section();
37 for name in &namespace {
38 writeln!(out, "namespace {} {{", name);
39 }
40
David Tolnay7db73692019-10-20 14:51:12 -040041 out.next_section();
42 for api in apis {
43 match api {
44 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080045 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
46 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7c295462020-04-25 12:45:07 -070047 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040048 }
49 }
50
David Tolnayf94bef12020-04-17 14:46:42 -070051 let mut methods_for_type = HashMap::new();
52 for api in apis {
53 if let Api::RustFunction(efn) = api {
54 if let Some(receiver) = &efn.sig.receiver {
55 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070056 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070057 .or_insert_with(Vec::new)
58 .push(efn);
59 }
60 }
61 }
Joel Galenson968738f2020-04-15 14:19:33 -070062
David Tolnay7db73692019-10-20 14:51:12 -040063 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070064 match api {
65 Api::Struct(strct) => {
66 out.next_section();
67 write_struct(out, strct);
68 }
David Tolnayc1fe0052020-04-17 15:15:06 -070069 Api::RustType(ety) => {
70 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070071 out.next_section();
72 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 }
David Tolnayc1fe0052020-04-17 15:15:06 -070074 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77 }
78
79 if !header {
80 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070081 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040082 for api in apis {
83 let (efn, write): (_, fn(_, _, _)) = match api {
84 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
85 Api::RustFunction(efn) => (efn, write_rust_function_decl),
86 _ => continue,
87 };
88 out.next_section();
89 write(out, efn, types);
90 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080091 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040092 }
93
94 for api in apis {
95 if let Api::RustFunction(efn) = api {
96 out.next_section();
97 write_rust_function_shim(out, efn, types);
98 }
99 }
100
101 out.next_section();
102 for name in namespace.iter().rev() {
103 writeln!(out, "}} // namespace {}", name);
104 }
105
106 if !header {
107 out.next_section();
108 write_generic_instantiations(out, types);
109 }
110
David Tolnay9c68b1a2020-03-06 11:12:55 -0800111 out.prepend(out.include.to_string());
112
David Tolnay7db73692019-10-20 14:51:12 -0400113 out_file
114}
115
116fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400117 for ty in types {
118 match ty {
119 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700120 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
121 | Some(I64) => out.include.cstdint = true,
122 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800123 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700124 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400125 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800126 Type::RustBox(_) => out.include.type_traits = true,
127 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700128 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700129 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700130 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
132 }
David Tolnay7db73692019-10-20 14:51:12 -0400133}
134
David Tolnayf51447e2020-03-06 14:14:27 -0800135fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700136 let mut needs_rust_string = false;
137 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700138 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400139 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700140 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700141 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700142 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400143 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700144 match ty {
145 Type::RustBox(_) => {
146 out.include.type_traits = true;
147 needs_rust_box = true;
148 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700149 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700150 out.include.array = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700151 needs_rust_vec = true;
152 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700153 Type::Str(_) => {
154 out.include.cstdint = true;
155 out.include.string = true;
156 needs_rust_str = true;
157 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700158 Type::Fn(_) => {
159 needs_rust_fn = true;
160 }
David Tolnay4770b472020-04-14 16:32:59 -0700161 Type::Slice(_) | Type::SliceRefU8(_) => {
162 needs_rust_slice = true;
163 }
David Tolnay7c295462020-04-25 12:45:07 -0700164 ty if ty == Isize => {
165 out.include.base_tsd = true;
166 needs_rust_isize = true;
167 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 ty if ty == RustString => {
169 out.include.array = true;
170 out.include.cstdint = true;
171 out.include.string = true;
172 needs_rust_string = true;
173 }
174 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400175 }
176 }
177
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 let mut needs_rust_error = false;
179 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800180 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800181 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700182 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800183 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700184 match api {
185 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700186 if efn.throws {
187 needs_trycatch = true;
188 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700189 for arg in &efn.args {
190 if arg.ty == RustString {
191 needs_unsafe_bitcopy = true;
192 break;
193 }
David Tolnay09011c32020-03-06 14:40:28 -0800194 }
195 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700196 Api::RustFunction(efn) if !out.header => {
197 if efn.throws {
198 out.include.exception = true;
199 needs_rust_error = true;
200 }
201 for arg in &efn.args {
202 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
203 needs_manually_drop = true;
204 break;
205 }
206 }
207 if let Some(ret) = &efn.ret {
208 if types.needs_indirect_abi(ret) {
209 needs_maybe_uninit = true;
210 }
David Tolnayf51447e2020-03-06 14:14:27 -0800211 }
212 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800214 }
215 }
216
David Tolnay750755e2020-03-01 13:04:08 -0800217 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700218 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800219
David Tolnayb7a7cb62020-03-17 21:18:40 -0700220 if needs_rust_string
221 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700222 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700224 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700225 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700226 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700227 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 || needs_unsafe_bitcopy
229 || needs_manually_drop
230 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700231 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 {
David Tolnay736cbca2020-03-11 16:49:18 -0700233 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800234 }
235
David Tolnayd1402742020-03-25 22:21:42 -0700236 if needs_rust_string {
237 out.next_section();
238 writeln!(out, "struct unsafe_bitcopy_t;");
239 }
240
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
242 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700243 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
Myron Ahneba35cf2020-02-05 19:41:51 +0700245 write_header_section(out, needs_rust_vec, "CXXBRIDGE02_RUST_VEC");
David Tolnay75dca2e2020-03-25 20:17:52 -0700246 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700247 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnay7c295462020-04-25 12:45:07 -0700248 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800250
251 if needs_manually_drop {
252 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700253 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800254 writeln!(out, "template <typename T>");
255 writeln!(out, "union ManuallyDrop {{");
256 writeln!(out, " T value;");
257 writeln!(
258 out,
259 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
260 );
261 writeln!(out, " ~ManuallyDrop() {{}}");
262 writeln!(out, "}};");
263 }
264
David Tolnay09011c32020-03-06 14:40:28 -0800265 if needs_maybe_uninit {
266 out.next_section();
267 writeln!(out, "template <typename T>");
268 writeln!(out, "union MaybeUninit {{");
269 writeln!(out, " T value;");
270 writeln!(out, " MaybeUninit() {{}}");
271 writeln!(out, " ~MaybeUninit() {{}}");
272 writeln!(out, "}};");
273 }
274
David Tolnay3e3e0af2020-03-17 22:42:49 -0700275 out.end_block("namespace cxxbridge02");
276
David Tolnay5d121442020-03-17 22:14:40 -0700277 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700278 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700279 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700280 out.include.type_traits = true;
281 out.include.utility = true;
282 writeln!(out, "class missing {{}};");
283 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700284 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700285 writeln!(out, "template <typename Try, typename Fail>");
286 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287 writeln!(
288 out,
David Tolnay04722332020-03-18 11:31:54 -0700289 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700290 );
David Tolnay04722332020-03-18 11:31:54 -0700291 writeln!(out, " missing>::value>::type");
292 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700293 writeln!(out, " func();");
294 writeln!(out, "}} catch (const ::std::exception &e) {{");
295 writeln!(out, " fail(e.what());");
296 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700298 }
299
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800300 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400301}
302
David Tolnayb7a7cb62020-03-17 21:18:40 -0700303fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700304 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700305 if needed {
306 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700307 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700308 if !line.trim_start().starts_with("//") {
309 writeln!(out, "{}", line);
310 }
311 }
312 }
313}
314
David Tolnay7db73692019-10-20 14:51:12 -0400315fn write_struct(out: &mut OutFile, strct: &Struct) {
316 for line in strct.doc.to_string().lines() {
317 writeln!(out, "//{}", line);
318 }
319 writeln!(out, "struct {} final {{", strct.ident);
320 for field in &strct.fields {
321 write!(out, " ");
322 write_type_space(out, &field.ty);
323 writeln!(out, "{};", field.ident);
324 }
325 writeln!(out, "}};");
326}
327
328fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
329 writeln!(out, "struct {};", ident);
330}
331
David Tolnay8861bee2020-01-20 18:39:24 -0800332fn write_struct_using(out: &mut OutFile, ident: &Ident) {
333 writeln!(out, "using {} = {};", ident, ident);
334}
335
David Tolnayc1fe0052020-04-17 15:15:06 -0700336fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700337 for line in ety.doc.to_string().lines() {
338 writeln!(out, "//{}", line);
339 }
340 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700341 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700342 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700343 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700344 write!(out, " ");
345 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700346 let local_name = method.ident.to_string();
347 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700348 writeln!(out, ";");
349 }
350 writeln!(out, "}};");
351}
352
David Tolnayebef4a22020-03-17 15:33:47 -0700353fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
354 let mut has_cxx_throws = false;
355 for api in apis {
356 if let Api::CxxFunction(efn) = api {
357 if efn.throws {
358 has_cxx_throws = true;
359 break;
360 }
361 }
362 }
363
364 if has_cxx_throws {
365 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700366 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700367 out,
368 "const char *cxxbridge02$exception(const char *, size_t);",
369 );
370 }
371}
372
David Tolnay7db73692019-10-20 14:51:12 -0400373fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700374 if efn.throws {
375 write!(out, "::rust::Str::Repr ");
376 } else {
David Tolnay99642622020-03-25 13:07:35 -0700377 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700378 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700379 let mangled = mangle::extern_fn(&out.namespace, efn);
380 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700381 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700382 if receiver.mutability.is_none() {
383 write!(out, "const ");
384 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700385 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700386 }
David Tolnay7db73692019-10-20 14:51:12 -0400387 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700388 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400389 write!(out, ", ");
390 }
David Tolnaya46a2372020-03-06 10:03:48 -0800391 if arg.ty == RustString {
392 write!(out, "const ");
393 }
David Tolnay7db73692019-10-20 14:51:12 -0400394 write_extern_arg(out, arg, types);
395 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700396 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400397 if indirect_return {
398 if !efn.args.is_empty() {
399 write!(out, ", ");
400 }
David Tolnay99642622020-03-25 13:07:35 -0700401 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400402 write!(out, "*return$");
403 }
404 writeln!(out, ") noexcept {{");
405 write!(out, " ");
406 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700407 match &efn.receiver {
408 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700409 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700410 }
David Tolnay7db73692019-10-20 14:51:12 -0400411 for (i, arg) in efn.args.iter().enumerate() {
412 if i > 0 {
413 write!(out, ", ");
414 }
415 write_type(out, &arg.ty);
416 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700417 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700418 if let Some(receiver) = &efn.receiver {
419 if receiver.mutability.is_none() {
420 write!(out, " const");
421 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700422 }
423 write!(out, " = ");
424 match &efn.receiver {
425 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700426 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700427 }
428 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400429 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700430 if efn.throws {
431 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700432 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700433 writeln!(out, " [&] {{");
434 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700435 }
David Tolnay7db73692019-10-20 14:51:12 -0400436 if indirect_return {
437 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700438 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400439 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700440 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400441 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700442 }
443 match &efn.ret {
444 Some(Type::Ref(_)) => write!(out, "&"),
445 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700446 Some(Type::SliceRefU8(_)) if !indirect_return => {
447 write!(out, "::rust::Slice<uint8_t>::Repr(")
448 }
David Tolnay99642622020-03-25 13:07:35 -0700449 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400450 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700451 match &efn.receiver {
452 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700453 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700454 }
David Tolnay7db73692019-10-20 14:51:12 -0400455 for (i, arg) in efn.args.iter().enumerate() {
456 if i > 0 {
457 write!(out, ", ");
458 }
459 if let Type::RustBox(_) = &arg.ty {
460 write_type(out, &arg.ty);
461 write!(out, "::from_raw({})", arg.ident);
462 } else if let Type::UniquePtr(_) = &arg.ty {
463 write_type(out, &arg.ty);
464 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800465 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800466 write!(
467 out,
468 "::rust::String(::rust::unsafe_bitcopy, *{})",
469 arg.ident,
470 );
David Tolnay7db73692019-10-20 14:51:12 -0400471 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700472 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800473 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400474 } else {
475 write!(out, "{}", arg.ident);
476 }
477 }
478 write!(out, ")");
479 match &efn.ret {
480 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
481 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700482 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700483 out,
484 " /* Use RVO to convert to r-value and move construct */"
485 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700486 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400487 _ => {}
488 }
489 if indirect_return {
490 write!(out, ")");
491 }
492 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700493 if efn.throws {
494 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700495 writeln!(out, " throw$.ptr = nullptr;");
496 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700497 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700498 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700499 writeln!(
500 out,
David Tolnay5d121442020-03-17 22:14:40 -0700501 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700502 );
David Tolnay5d121442020-03-17 22:14:40 -0700503 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700504 writeln!(out, " return throw$;");
505 }
David Tolnay7db73692019-10-20 14:51:12 -0400506 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700507 for arg in &efn.args {
508 if let Type::Fn(f) = &arg.ty {
509 let var = &arg.ident;
510 write_function_pointer_trampoline(out, efn, var, f, types);
511 }
512 }
513}
514
515fn write_function_pointer_trampoline(
516 out: &mut OutFile,
517 efn: &ExternFn,
518 var: &Ident,
519 f: &Signature,
520 types: &Types,
521) {
522 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700523 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700524 let indirect_call = true;
525 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
526
527 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700528 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700529 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400530}
531
532fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700533 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700534 let indirect_call = false;
535 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
536}
537
538fn write_rust_function_decl_impl(
539 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700540 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700541 sig: &Signature,
542 types: &Types,
543 indirect_call: bool,
544) {
545 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700546 write!(out, "::rust::Str::Repr ");
547 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700548 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700549 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700550 write!(out, "{}(", link_name);
551 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700552 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700553 if receiver.mutability.is_none() {
554 write!(out, "const ");
555 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700556 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700557 needs_comma = true;
558 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700559 for arg in &sig.args {
560 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400561 write!(out, ", ");
562 }
563 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700564 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400565 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700566 if indirect_return(sig, types) {
567 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400568 write!(out, ", ");
569 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700570 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400571 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700572 needs_comma = true;
573 }
574 if indirect_call {
575 if needs_comma {
576 write!(out, ", ");
577 }
578 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400579 }
580 writeln!(out, ") noexcept;");
581}
582
583fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400584 for line in efn.doc.to_string().lines() {
585 writeln!(out, "//{}", line);
586 }
David Tolnaya73853b2020-04-20 01:19:56 -0700587 let local_name = match &efn.sig.receiver {
588 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700589 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700590 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700591 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 let indirect_call = false;
593 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
594}
595
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700596fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700598 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700599 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 indirect_call: bool,
601) {
602 write_return_type(out, &sig.ret);
603 write!(out, "{}(", local_name);
604 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400605 if i > 0 {
606 write!(out, ", ");
607 }
608 write_type_space(out, &arg.ty);
609 write!(out, "{}", arg.ident);
610 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700611 if indirect_call {
612 if !sig.args.is_empty() {
613 write!(out, ", ");
614 }
615 write!(out, "void *extern$");
616 }
David Tolnay1e548172020-03-16 13:37:09 -0700617 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700618 if let Some(receiver) = &sig.receiver {
619 if receiver.mutability.is_none() {
620 write!(out, " const");
621 }
622 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700623 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700624 write!(out, " noexcept");
625 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700626}
627
628fn write_rust_function_shim_impl(
629 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700630 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700631 sig: &Signature,
632 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700633 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700634 indirect_call: bool,
635) {
636 if out.header && sig.receiver.is_some() {
637 // We've already defined this inside the struct.
638 return;
639 }
David Tolnaya73853b2020-04-20 01:19:56 -0700640 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400641 if out.header {
642 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700643 return;
David Tolnay7db73692019-10-20 14:51:12 -0400644 }
David Tolnay439cde22020-04-20 00:46:25 -0700645 writeln!(out, " {{");
646 for arg in &sig.args {
647 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
648 out.include.utility = true;
649 write!(out, " ::rust::ManuallyDrop<");
650 write_type(out, &arg.ty);
651 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
652 }
653 }
654 write!(out, " ");
655 let indirect_return = indirect_return(sig, types);
656 if indirect_return {
657 write!(out, "::rust::MaybeUninit<");
658 write_type(out, sig.ret.as_ref().unwrap());
659 writeln!(out, "> return$;");
660 write!(out, " ");
661 } else if let Some(ret) = &sig.ret {
662 write!(out, "return ");
663 match ret {
664 Type::RustBox(_) => {
665 write_type(out, ret);
666 write!(out, "::from_raw(");
667 }
668 Type::UniquePtr(_) => {
669 write_type(out, ret);
670 write!(out, "(");
671 }
672 Type::Ref(_) => write!(out, "*"),
673 _ => {}
674 }
675 }
676 if sig.throws {
677 write!(out, "::rust::Str::Repr error$ = ");
678 }
679 write!(out, "{}(", invoke);
680 if sig.receiver.is_some() {
681 write!(out, "*this");
682 }
683 for (i, arg) in sig.args.iter().enumerate() {
684 if i > 0 || sig.receiver.is_some() {
685 write!(out, ", ");
686 }
687 match &arg.ty {
688 Type::Str(_) => write!(out, "::rust::Str::Repr("),
689 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
690 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
691 _ => {}
692 }
693 write!(out, "{}", arg.ident);
694 match &arg.ty {
695 Type::RustBox(_) => write!(out, ".into_raw()"),
696 Type::UniquePtr(_) => write!(out, ".release()"),
697 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
698 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
699 _ => {}
700 }
701 }
702 if indirect_return {
703 if !sig.args.is_empty() {
704 write!(out, ", ");
705 }
706 write!(out, "&return$.value");
707 }
708 if indirect_call {
709 if !sig.args.is_empty() || indirect_return {
710 write!(out, ", ");
711 }
712 write!(out, "extern$");
713 }
714 write!(out, ")");
715 if let Some(ret) = &sig.ret {
716 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
717 write!(out, ")");
718 }
719 }
720 writeln!(out, ";");
721 if sig.throws {
722 writeln!(out, " if (error$.ptr) {{");
723 writeln!(out, " throw ::rust::Error(error$);");
724 writeln!(out, " }}");
725 }
726 if indirect_return {
727 out.include.utility = true;
728 writeln!(out, " return ::std::move(return$.value);");
729 }
730 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400731}
732
733fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
734 match ty {
735 None => write!(out, "void "),
736 Some(ty) => write_type_space(out, ty),
737 }
738}
739
David Tolnay75dca2e2020-03-25 20:17:52 -0700740fn indirect_return(sig: &Signature, types: &Types) -> bool {
741 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700742 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700743 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700744}
745
David Tolnay99642622020-03-25 13:07:35 -0700746fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
747 match ty {
748 Type::RustBox(ty) | Type::UniquePtr(ty) => {
749 write_type_space(out, &ty.inner);
750 write!(out, "*");
751 }
752 Type::Ref(ty) => {
753 if ty.mutability.is_none() {
754 write!(out, "const ");
755 }
756 write_type(out, &ty.inner);
757 write!(out, " *");
758 }
759 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700760 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700761 _ => write_type(out, ty),
762 }
763}
764
765fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
766 write_indirect_return_type(out, ty);
767 match ty {
768 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700769 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700770 _ => write_space_after_type(out, ty),
771 }
772}
773
774fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400775 match ty {
776 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
777 write_type_space(out, &ty.inner);
778 write!(out, "*");
779 }
David Tolnay4a441222020-01-25 16:24:27 -0800780 Some(Type::Ref(ty)) => {
781 if ty.mutability.is_none() {
782 write!(out, "const ");
783 }
784 write_type(out, &ty.inner);
785 write!(out, " *");
786 }
David Tolnay750755e2020-03-01 13:04:08 -0800787 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700788 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400789 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
790 _ => write_return_type(out, ty),
791 }
792}
793
794fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
795 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700796 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400797 write_type_space(out, &ty.inner);
798 write!(out, "*");
799 }
David Tolnay750755e2020-03-01 13:04:08 -0800800 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700801 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400802 _ => write_type_space(out, &arg.ty),
803 }
804 if types.needs_indirect_abi(&arg.ty) {
805 write!(out, "*");
806 }
807 write!(out, "{}", arg.ident);
808}
809
810fn write_type(out: &mut OutFile, ty: &Type) {
811 match ty {
812 Type::Ident(ident) => match Atom::from(ident) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700813 Some(a) => write!(out, "{}", a.to_cxx()),
David Tolnay7db73692019-10-20 14:51:12 -0400814 None => write!(out, "{}", ident),
815 },
816 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800817 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400818 write_type(out, &ty.inner);
819 write!(out, ">");
820 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700821 Type::RustVec(ty) => {
822 write!(out, "::rust::Vec<");
823 write_type(out, &ty.inner);
824 write!(out, ">");
825 }
David Tolnay7db73692019-10-20 14:51:12 -0400826 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800827 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400828 write_type(out, &ptr.inner);
829 write!(out, ">");
830 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700831 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700832 write!(out, "::std::vector<");
833 write_type(out, &ty.inner);
834 write!(out, ">");
835 }
David Tolnay7db73692019-10-20 14:51:12 -0400836 Type::Ref(r) => {
837 if r.mutability.is_none() {
838 write!(out, "const ");
839 }
840 write_type(out, &r.inner);
841 write!(out, " &");
842 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700843 Type::Slice(_) => {
844 // For now, only U8 slices are supported, which are covered separately below
845 unreachable!()
846 }
David Tolnay7db73692019-10-20 14:51:12 -0400847 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800848 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400849 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700850 Type::SliceRefU8(_) => {
851 write!(out, "::rust::Slice<uint8_t>");
852 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700853 Type::Fn(f) => {
854 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
855 match &f.ret {
856 Some(ret) => write_type(out, ret),
857 None => write!(out, "void"),
858 }
859 write!(out, "(");
860 for (i, arg) in f.args.iter().enumerate() {
861 if i > 0 {
862 write!(out, ", ");
863 }
864 write_type(out, &arg.ty);
865 }
866 write!(out, ")>");
867 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700868 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400869 }
870}
871
872fn write_type_space(out: &mut OutFile, ty: &Type) {
873 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700874 write_space_after_type(out, ty);
875}
876
877fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400878 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700879 Type::Ident(_)
880 | Type::RustBox(_)
881 | Type::UniquePtr(_)
882 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700883 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700884 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700885 | Type::SliceRefU8(_)
886 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400887 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700888 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400889 }
890}
891
892fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
893 fn allow_unique_ptr(ident: &Ident) -> bool {
894 Atom::from(ident).is_none()
895 }
896
Myron Ahneba35cf2020-02-05 19:41:51 +0700897 fn allow_vector(ident: &Ident) -> bool {
898 // Note: built-in types such as u8 are already defined in cxx.cc
899 Atom::from(ident).is_none()
900 }
901
David Tolnay7db73692019-10-20 14:51:12 -0400902 out.begin_block("extern \"C\"");
903 for ty in types {
904 if let Type::RustBox(ty) = ty {
905 if let Type::Ident(inner) = &ty.inner {
906 out.next_section();
907 write_rust_box_extern(out, inner);
908 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700909 } else if let Type::RustVec(ty) = ty {
910 if let Type::Ident(_) = &ty.inner {
911 out.next_section();
912 write_rust_vec_extern(out, &ty.inner);
913 }
David Tolnay7db73692019-10-20 14:51:12 -0400914 } else if let Type::UniquePtr(ptr) = ty {
915 if let Type::Ident(inner) = &ptr.inner {
916 if allow_unique_ptr(inner) {
917 out.next_section();
Myron Ahneba35cf2020-02-05 19:41:51 +0700918 write_unique_ptr(out, &ptr.inner, types);
919 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700920 } else if let Type::CxxVector(ptr1) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700921 if let Type::Ident(inner) = &ptr1.inner {
922 if allow_vector(inner) {
923 out.next_section();
924 write_unique_ptr(out, &ptr.inner, types);
925 }
926 }
927 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700928 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700929 if let Type::Ident(inner) = &ptr.inner {
930 if allow_vector(inner) {
931 out.next_section();
932 write_vector(out, inner);
David Tolnay7db73692019-10-20 14:51:12 -0400933 }
934 }
935 }
936 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800937 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400938
David Tolnay750755e2020-03-01 13:04:08 -0800939 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700940 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400941 for ty in types {
942 if let Type::RustBox(ty) = ty {
943 if let Type::Ident(inner) = &ty.inner {
944 write_rust_box_impl(out, inner);
945 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700946 } else if let Type::RustVec(ty) = ty {
947 if let Type::Ident(_) = &ty.inner {
948 write_rust_vec_impl(out, &ty.inner);
949 }
David Tolnay7db73692019-10-20 14:51:12 -0400950 }
951 }
David Tolnay8c730492020-03-13 01:29:06 -0700952 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800953 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400954}
955
956fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
957 let mut inner = String::new();
958 for name in &out.namespace {
959 inner += name;
960 inner += "::";
961 }
962 inner += &ident.to_string();
963 let instance = inner.replace("::", "$");
964
David Tolnay8c730492020-03-13 01:29:06 -0700965 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
966 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400967 writeln!(
968 out,
David Tolnay8c730492020-03-13 01:29:06 -0700969 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400970 instance, inner,
971 );
972 writeln!(
973 out,
David Tolnay8c730492020-03-13 01:29:06 -0700974 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400975 instance, inner,
976 );
David Tolnay8c730492020-03-13 01:29:06 -0700977 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400978}
979
Myron Ahneba35cf2020-02-05 19:41:51 +0700980fn write_rust_vec_extern(out: &mut OutFile, ty: &Type) {
981 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
982 let inner = ty.to_typename(&namespace);
983 let instance = ty.to_mangled(&namespace);
984
985 writeln!(out, "#ifndef CXXBRIDGE02_RUST_VEC_{}", instance);
986 writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance);
987 writeln!(
988 out,
989 "void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
990 instance, inner,
991 );
992 writeln!(
993 out,
Myron Ahneba35cf2020-02-05 19:41:51 +0700994 "size_t cxxbridge02$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
995 instance, inner,
996 );
997 writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance);
998}
999
David Tolnay7db73692019-10-20 14:51:12 -04001000fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1001 let mut inner = String::new();
1002 for name in &out.namespace {
1003 inner += name;
1004 inner += "::";
1005 }
1006 inner += &ident.to_string();
1007 let instance = inner.replace("::", "$");
1008
1009 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001010 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001011 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001012 writeln!(out, "}}");
1013
1014 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001015 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -07001016 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001017 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001018}
1019
Myron Ahneba35cf2020-02-05 19:41:51 +07001020fn write_rust_vec_impl(out: &mut OutFile, ty: &Type) {
1021 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
1022 let inner = ty.to_typename(&namespace);
1023 let instance = ty.to_mangled(&namespace);
David Tolnay4791f1c2020-03-17 21:53:16 -07001024
Myron Ahneba35cf2020-02-05 19:41:51 +07001025 writeln!(out, "template <>");
1026 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1027 writeln!(
1028 out,
1029 " return cxxbridge02$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001030 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001031 );
1032 writeln!(out, "}}");
1033
1034 writeln!(out, "template <>");
1035 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
1036 writeln!(out, " return cxxbridge02$rust_vec${}$len(this);", instance);
1037 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001038}
1039
1040fn write_unique_ptr(out: &mut OutFile, ty: &Type, types: &Types) {
1041 out.include.utility = true;
1042 let namespace = out.namespace.iter().cloned().collect::<Vec<String>>();
1043 let inner = ty.to_typename(&namespace);
1044 let instance = ty.to_mangled(&namespace);
David Tolnay7db73692019-10-20 14:51:12 -04001045
David Tolnay8c730492020-03-13 01:29:06 -07001046 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
1047 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001048 writeln!(
1049 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001050 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001051 inner,
1052 );
1053 writeln!(
1054 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001055 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001056 inner,
1057 );
1058 writeln!(
1059 out,
David Tolnay8c730492020-03-13 01:29:06 -07001060 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001061 instance, inner,
1062 );
David Tolnay7e219b82020-03-01 13:14:51 -08001063 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001064 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001065 match ty {
1066 Type::Ident(ident) if types.structs.contains_key(ident) => {
1067 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001068 out,
1069 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
1070 instance, inner, inner,
1071 );
Myron Ahneba35cf2020-02-05 19:41:51 +07001072 writeln!(
1073 out,
1074 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1075 inner, inner,
1076 );
1077 writeln!(out, "}}");
1078 }
1079 _ => (),
David Tolnay53838912020-04-09 20:56:44 -07001080 }
David Tolnay7db73692019-10-20 14:51:12 -04001081 writeln!(
1082 out,
David Tolnay8c730492020-03-13 01:29:06 -07001083 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001084 instance, inner, inner,
1085 );
David Tolnay7e219b82020-03-01 13:14:51 -08001086 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001087 writeln!(out, "}}");
1088 writeln!(
1089 out,
David Tolnay8c730492020-03-13 01:29:06 -07001090 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001091 inner, instance, inner,
1092 );
1093 writeln!(out, " return ptr.get();");
1094 writeln!(out, "}}");
1095 writeln!(
1096 out,
David Tolnay8c730492020-03-13 01:29:06 -07001097 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001098 inner, instance, inner,
1099 );
1100 writeln!(out, " return ptr.release();");
1101 writeln!(out, "}}");
1102 writeln!(
1103 out,
David Tolnay8c730492020-03-13 01:29:06 -07001104 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001105 instance, inner,
1106 );
1107 writeln!(out, " ptr->~unique_ptr();");
1108 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001109 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001110}
Myron Ahneba35cf2020-02-05 19:41:51 +07001111
1112fn write_vector(out: &mut OutFile, ident: &Ident) {
1113 let mut inner = String::new();
1114 // Do not apply namespace to built-in type
1115 let is_user_type = Atom::from(ident).is_none();
1116 if is_user_type {
1117 for name in &out.namespace {
1118 inner += name;
1119 inner += "::";
1120 }
1121 }
1122 let mut instance = inner.clone();
1123 if let Some(ti) = Atom::from(ident) {
1124 inner += ti.to_cxx();
1125 } else {
1126 inner += &ident.to_string();
1127 };
1128 instance += &ident.to_string();
1129 let instance = instance.replace("::", "$");
1130
1131 writeln!(out, "#ifndef CXXBRIDGE02_vector_{}", instance);
1132 writeln!(out, "#define CXXBRIDGE02_vector_{}", instance);
1133 writeln!(
1134 out,
David Tolnaya83247c2020-04-24 14:36:10 -07001135 "size_t cxxbridge02$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001136 instance, inner,
1137 );
1138 writeln!(out, " return s.size();");
1139 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001140 writeln!(
1141 out,
David Tolnaydd3f6342020-04-24 14:38:55 -07001142 "const {} &cxxbridge02$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001143 inner, instance, inner,
1144 );
David Tolnaydd3f6342020-04-24 14:38:55 -07001145 writeln!(out, " return s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001146 writeln!(out, "}}");
David Tolnaycc75ad22020-04-24 14:45:16 -07001147 writeln!(
1148 out,
1149 "void cxxbridge02$std$vector${}$push_back(::std::vector<{}> &s, const {} &item) noexcept {{",
1150 instance, inner, inner
1151 );
1152 writeln!(out, " s.push_back(item);");
1153 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001154 writeln!(out, "#endif // CXXBRIDGE02_vector_{}", instance);
1155}