blob: 8b554d84d2cbaa6f7d2b1111f3f83f53cd2434d3 [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;
Joel Galensonc03402a2020-04-23 17:31:09 -07006use crate::syntax::{mangle, Api, Enum, 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 }
Joel Galensonc03402a2020-04-23 17:31:09 -070067 Api::Enum(enm) => {
68 out.next_section();
69 write_enum(out, enm);
70 }
David Tolnayc1fe0052020-04-17 15:15:06 -070071 Api::RustType(ety) => {
72 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070073 out.next_section();
74 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070075 }
David Tolnayc1fe0052020-04-17 15:15:06 -070076 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070077 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79 }
80
81 if !header {
82 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070083 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040084 for api in apis {
85 let (efn, write): (_, fn(_, _, _)) = match api {
86 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
87 Api::RustFunction(efn) => (efn, write_rust_function_decl),
88 _ => continue,
89 };
90 out.next_section();
91 write(out, efn, types);
92 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080093 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95
96 for api in apis {
97 if let Api::RustFunction(efn) = api {
98 out.next_section();
99 write_rust_function_shim(out, efn, types);
100 }
101 }
102
103 out.next_section();
104 for name in namespace.iter().rev() {
105 writeln!(out, "}} // namespace {}", name);
106 }
107
108 if !header {
109 out.next_section();
110 write_generic_instantiations(out, types);
111 }
112
David Tolnay9c68b1a2020-03-06 11:12:55 -0800113 out.prepend(out.include.to_string());
114
David Tolnay7db73692019-10-20 14:51:12 -0400115 out_file
116}
117
118fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400119 for ty in types {
120 match ty {
121 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700122 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
123 | Some(I64) => out.include.cstdint = true,
124 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800125 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700126 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400127 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800128 Type::RustBox(_) => out.include.type_traits = true,
129 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700130 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700131 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700132 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400133 }
134 }
David Tolnay7db73692019-10-20 14:51:12 -0400135}
136
David Tolnayf51447e2020-03-06 14:14:27 -0800137fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700138 let mut needs_rust_string = false;
139 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700140 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400141 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700142 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700143 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700144 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400145 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700146 match ty {
147 Type::RustBox(_) => {
148 out.include.type_traits = true;
149 needs_rust_box = true;
150 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700151 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700152 out.include.array = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700153 out.include.type_traits = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700154 needs_rust_vec = true;
155 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700156 Type::Str(_) => {
157 out.include.cstdint = true;
158 out.include.string = true;
159 needs_rust_str = true;
160 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700161 Type::Fn(_) => {
162 needs_rust_fn = true;
163 }
David Tolnay4770b472020-04-14 16:32:59 -0700164 Type::Slice(_) | Type::SliceRefU8(_) => {
165 needs_rust_slice = true;
166 }
David Tolnay7c295462020-04-25 12:45:07 -0700167 ty if ty == Isize => {
168 out.include.base_tsd = true;
169 needs_rust_isize = true;
170 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700171 ty if ty == RustString => {
172 out.include.array = true;
173 out.include.cstdint = true;
174 out.include.string = true;
175 needs_rust_string = true;
176 }
177 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400178 }
179 }
180
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 let mut needs_rust_error = false;
182 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800183 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800184 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700185 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800186 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700187 match api {
188 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700189 if efn.throws {
190 needs_trycatch = true;
191 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700192 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700193 let bitcopy = match arg.ty {
194 Type::RustVec(_) => true,
195 _ => arg.ty == RustString,
196 };
197 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700198 needs_unsafe_bitcopy = true;
199 break;
200 }
David Tolnay09011c32020-03-06 14:40:28 -0800201 }
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 Api::RustFunction(efn) if !out.header => {
204 if efn.throws {
205 out.include.exception = true;
206 needs_rust_error = true;
207 }
208 for arg in &efn.args {
209 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
210 needs_manually_drop = true;
211 break;
212 }
213 }
214 if let Some(ret) = &efn.ret {
215 if types.needs_indirect_abi(ret) {
216 needs_maybe_uninit = true;
217 }
David Tolnayf51447e2020-03-06 14:14:27 -0800218 }
219 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700220 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800221 }
222 }
223
David Tolnay750755e2020-03-01 13:04:08 -0800224 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -0700225 out.begin_block("inline namespace cxxbridge03");
David Tolnayf51447e2020-03-06 14:14:27 -0800226
David Tolnayb7a7cb62020-03-17 21:18:40 -0700227 if needs_rust_string
228 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700229 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700231 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700232 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700233 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700234 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 || needs_unsafe_bitcopy
236 || needs_manually_drop
237 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700238 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 {
David Tolnay736cbca2020-03-11 16:49:18 -0700240 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800241 }
242
David Tolnayd1402742020-03-25 22:21:42 -0700243 if needs_rust_string {
244 out.next_section();
245 writeln!(out, "struct unsafe_bitcopy_t;");
246 }
247
David Tolnay69601622020-04-29 18:48:36 -0700248 write_header_section(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
249 write_header_section(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
250 write_header_section(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
251 write_header_section(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
252 write_header_section(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
253 write_header_section(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
254 write_header_section(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
255 write_header_section(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
256 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800257
258 if needs_manually_drop {
259 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700260 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800261 writeln!(out, "template <typename T>");
262 writeln!(out, "union ManuallyDrop {{");
263 writeln!(out, " T value;");
264 writeln!(
265 out,
266 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
267 );
268 writeln!(out, " ~ManuallyDrop() {{}}");
269 writeln!(out, "}};");
270 }
271
David Tolnay09011c32020-03-06 14:40:28 -0800272 if needs_maybe_uninit {
273 out.next_section();
274 writeln!(out, "template <typename T>");
275 writeln!(out, "union MaybeUninit {{");
276 writeln!(out, " T value;");
277 writeln!(out, " MaybeUninit() {{}}");
278 writeln!(out, " ~MaybeUninit() {{}}");
279 writeln!(out, "}};");
280 }
281
David Tolnay69601622020-04-29 18:48:36 -0700282 out.end_block("namespace cxxbridge03");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700283
David Tolnay5d121442020-03-17 22:14:40 -0700284 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700285 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700286 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700287 out.include.type_traits = true;
288 out.include.utility = true;
289 writeln!(out, "class missing {{}};");
290 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700291 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700292 writeln!(out, "template <typename Try, typename Fail>");
293 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700294 writeln!(
295 out,
David Tolnay04722332020-03-18 11:31:54 -0700296 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700297 );
David Tolnay04722332020-03-18 11:31:54 -0700298 writeln!(out, " missing>::value>::type");
299 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700300 writeln!(out, " func();");
301 writeln!(out, "}} catch (const ::std::exception &e) {{");
302 writeln!(out, " fail(e.what());");
303 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700304 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700305 }
306
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800307 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400308}
309
David Tolnayb7a7cb62020-03-17 21:18:40 -0700310fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700311 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700312 if needed {
313 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700314 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700315 if !line.trim_start().starts_with("//") {
316 writeln!(out, "{}", line);
317 }
318 }
319 }
320}
321
David Tolnay7db73692019-10-20 14:51:12 -0400322fn write_struct(out: &mut OutFile, strct: &Struct) {
323 for line in strct.doc.to_string().lines() {
324 writeln!(out, "//{}", line);
325 }
326 writeln!(out, "struct {} final {{", strct.ident);
327 for field in &strct.fields {
328 write!(out, " ");
329 write_type_space(out, &field.ty);
330 writeln!(out, "{};", field.ident);
331 }
332 writeln!(out, "}};");
333}
334
335fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
336 writeln!(out, "struct {};", ident);
337}
338
David Tolnay8861bee2020-01-20 18:39:24 -0800339fn write_struct_using(out: &mut OutFile, ident: &Ident) {
340 writeln!(out, "using {} = {};", ident, ident);
341}
342
David Tolnayc1fe0052020-04-17 15:15:06 -0700343fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700344 for line in ety.doc.to_string().lines() {
345 writeln!(out, "//{}", line);
346 }
347 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700348 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700349 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700350 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700351 write!(out, " ");
352 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700353 let local_name = method.ident.to_string();
354 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700355 writeln!(out, ";");
356 }
357 writeln!(out, "}};");
358}
359
Joel Galensonc03402a2020-04-23 17:31:09 -0700360fn write_enum(out: &mut OutFile, enm: &Enum) {
361 for line in enm.doc.to_string().lines() {
362 writeln!(out, "//{}", line);
363 }
364 writeln!(out, "enum class {} : uint32_t {{", enm.ident);
365 for variant in &enm.variants {
366 write!(out, " ");
367 write!(out, "{}", variant.ident);
368 if let Some(discriminant) = &variant.discriminant {
369 write!(out, " = {}", discriminant);
370 }
371 writeln!(out, ",");
372 }
373 writeln!(out, "}};");
374}
375
David Tolnayebef4a22020-03-17 15:33:47 -0700376fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
377 let mut has_cxx_throws = false;
378 for api in apis {
379 if let Api::CxxFunction(efn) = api {
380 if efn.throws {
381 has_cxx_throws = true;
382 break;
383 }
384 }
385 }
386
387 if has_cxx_throws {
388 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700389 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700390 out,
David Tolnay69601622020-04-29 18:48:36 -0700391 "const char *cxxbridge03$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700392 );
393 }
394}
395
David Tolnay7db73692019-10-20 14:51:12 -0400396fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700397 if efn.throws {
398 write!(out, "::rust::Str::Repr ");
399 } else {
David Tolnay99642622020-03-25 13:07:35 -0700400 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700401 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700402 let mangled = mangle::extern_fn(&out.namespace, efn);
403 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700404 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700405 if receiver.mutability.is_none() {
406 write!(out, "const ");
407 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700408 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700409 }
David Tolnay7db73692019-10-20 14:51:12 -0400410 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700411 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400412 write!(out, ", ");
413 }
David Tolnaya46a2372020-03-06 10:03:48 -0800414 if arg.ty == RustString {
415 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700416 } else if let Type::RustVec(_) = arg.ty {
417 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800418 }
David Tolnay7db73692019-10-20 14:51:12 -0400419 write_extern_arg(out, arg, types);
420 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700421 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400422 if indirect_return {
423 if !efn.args.is_empty() {
424 write!(out, ", ");
425 }
David Tolnay99642622020-03-25 13:07:35 -0700426 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400427 write!(out, "*return$");
428 }
429 writeln!(out, ") noexcept {{");
430 write!(out, " ");
431 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700432 match &efn.receiver {
433 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700434 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700435 }
David Tolnay7db73692019-10-20 14:51:12 -0400436 for (i, arg) in efn.args.iter().enumerate() {
437 if i > 0 {
438 write!(out, ", ");
439 }
440 write_type(out, &arg.ty);
441 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700442 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700443 if let Some(receiver) = &efn.receiver {
444 if receiver.mutability.is_none() {
445 write!(out, " const");
446 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700447 }
448 write!(out, " = ");
449 match &efn.receiver {
450 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700451 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700452 }
453 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400454 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700455 if efn.throws {
456 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700457 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700458 writeln!(out, " [&] {{");
459 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700460 }
David Tolnay7db73692019-10-20 14:51:12 -0400461 if indirect_return {
462 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700463 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400464 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700465 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400466 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700467 }
468 match &efn.ret {
469 Some(Type::Ref(_)) => write!(out, "&"),
470 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700471 Some(Type::SliceRefU8(_)) if !indirect_return => {
472 write!(out, "::rust::Slice<uint8_t>::Repr(")
473 }
David Tolnay99642622020-03-25 13:07:35 -0700474 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400475 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700476 match &efn.receiver {
477 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700478 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700479 }
David Tolnay7db73692019-10-20 14:51:12 -0400480 for (i, arg) in efn.args.iter().enumerate() {
481 if i > 0 {
482 write!(out, ", ");
483 }
484 if let Type::RustBox(_) = &arg.ty {
485 write_type(out, &arg.ty);
486 write!(out, "::from_raw({})", arg.ident);
487 } else if let Type::UniquePtr(_) = &arg.ty {
488 write_type(out, &arg.ty);
489 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800490 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800491 write!(
492 out,
493 "::rust::String(::rust::unsafe_bitcopy, *{})",
494 arg.ident,
495 );
David Tolnay313b10e2020-04-25 16:30:51 -0700496 } else if let Type::RustVec(_) = arg.ty {
497 write_type(out, &arg.ty);
498 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400499 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700500 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800501 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400502 } else {
503 write!(out, "{}", arg.ident);
504 }
505 }
506 write!(out, ")");
507 match &efn.ret {
508 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
509 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay4377a9e2020-04-24 15:20:26 -0700510 Some(Type::CxxVector(_)) => write!(
Myron Ahneba35cf2020-02-05 19:41:51 +0700511 out,
512 " /* Use RVO to convert to r-value and move construct */"
513 ),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700514 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400515 _ => {}
516 }
517 if indirect_return {
518 write!(out, ")");
519 }
520 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700521 if efn.throws {
522 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700523 writeln!(out, " throw$.ptr = nullptr;");
524 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700525 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700526 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700527 writeln!(
528 out,
David Tolnay69601622020-04-29 18:48:36 -0700529 " throw$.ptr = cxxbridge03$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700530 );
David Tolnay5d121442020-03-17 22:14:40 -0700531 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700532 writeln!(out, " return throw$;");
533 }
David Tolnay7db73692019-10-20 14:51:12 -0400534 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700535 for arg in &efn.args {
536 if let Type::Fn(f) = &arg.ty {
537 let var = &arg.ident;
538 write_function_pointer_trampoline(out, efn, var, f, types);
539 }
540 }
541}
542
543fn write_function_pointer_trampoline(
544 out: &mut OutFile,
545 efn: &ExternFn,
546 var: &Ident,
547 f: &Signature,
548 types: &Types,
549) {
550 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700551 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 let indirect_call = true;
553 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
554
555 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700556 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700557 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400558}
559
560fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700561 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700562 let indirect_call = false;
563 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
564}
565
566fn write_rust_function_decl_impl(
567 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700568 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700569 sig: &Signature,
570 types: &Types,
571 indirect_call: bool,
572) {
573 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700574 write!(out, "::rust::Str::Repr ");
575 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700576 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700577 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700578 write!(out, "{}(", link_name);
579 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700580 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700581 if receiver.mutability.is_none() {
582 write!(out, "const ");
583 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700584 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700585 needs_comma = true;
586 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 for arg in &sig.args {
588 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400589 write!(out, ", ");
590 }
591 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700592 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400593 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700594 if indirect_return(sig, types) {
595 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400596 write!(out, ", ");
597 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700598 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400599 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700600 needs_comma = true;
601 }
602 if indirect_call {
603 if needs_comma {
604 write!(out, ", ");
605 }
606 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400607 }
608 writeln!(out, ") noexcept;");
609}
610
611fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400612 for line in efn.doc.to_string().lines() {
613 writeln!(out, "//{}", line);
614 }
David Tolnaya73853b2020-04-20 01:19:56 -0700615 let local_name = match &efn.sig.receiver {
616 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700617 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700618 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700619 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700620 let indirect_call = false;
621 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
622}
623
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700624fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700625 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700626 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700627 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700628 indirect_call: bool,
629) {
630 write_return_type(out, &sig.ret);
631 write!(out, "{}(", local_name);
632 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400633 if i > 0 {
634 write!(out, ", ");
635 }
636 write_type_space(out, &arg.ty);
637 write!(out, "{}", arg.ident);
638 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700639 if indirect_call {
640 if !sig.args.is_empty() {
641 write!(out, ", ");
642 }
643 write!(out, "void *extern$");
644 }
David Tolnay1e548172020-03-16 13:37:09 -0700645 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700646 if let Some(receiver) = &sig.receiver {
647 if receiver.mutability.is_none() {
648 write!(out, " const");
649 }
650 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700651 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700652 write!(out, " noexcept");
653 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700654}
655
656fn write_rust_function_shim_impl(
657 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700658 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700659 sig: &Signature,
660 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700661 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700662 indirect_call: bool,
663) {
664 if out.header && sig.receiver.is_some() {
665 // We've already defined this inside the struct.
666 return;
667 }
David Tolnaya73853b2020-04-20 01:19:56 -0700668 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400669 if out.header {
670 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700671 return;
David Tolnay7db73692019-10-20 14:51:12 -0400672 }
David Tolnay439cde22020-04-20 00:46:25 -0700673 writeln!(out, " {{");
674 for arg in &sig.args {
675 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
676 out.include.utility = true;
677 write!(out, " ::rust::ManuallyDrop<");
678 write_type(out, &arg.ty);
679 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
680 }
681 }
682 write!(out, " ");
683 let indirect_return = indirect_return(sig, types);
684 if indirect_return {
685 write!(out, "::rust::MaybeUninit<");
686 write_type(out, sig.ret.as_ref().unwrap());
687 writeln!(out, "> return$;");
688 write!(out, " ");
689 } else if let Some(ret) = &sig.ret {
690 write!(out, "return ");
691 match ret {
692 Type::RustBox(_) => {
693 write_type(out, ret);
694 write!(out, "::from_raw(");
695 }
696 Type::UniquePtr(_) => {
697 write_type(out, ret);
698 write!(out, "(");
699 }
700 Type::Ref(_) => write!(out, "*"),
701 _ => {}
702 }
703 }
704 if sig.throws {
705 write!(out, "::rust::Str::Repr error$ = ");
706 }
707 write!(out, "{}(", invoke);
708 if sig.receiver.is_some() {
709 write!(out, "*this");
710 }
711 for (i, arg) in sig.args.iter().enumerate() {
712 if i > 0 || sig.receiver.is_some() {
713 write!(out, ", ");
714 }
715 match &arg.ty {
716 Type::Str(_) => write!(out, "::rust::Str::Repr("),
717 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
718 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
719 _ => {}
720 }
721 write!(out, "{}", arg.ident);
722 match &arg.ty {
723 Type::RustBox(_) => write!(out, ".into_raw()"),
724 Type::UniquePtr(_) => write!(out, ".release()"),
725 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
726 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
727 _ => {}
728 }
729 }
730 if indirect_return {
731 if !sig.args.is_empty() {
732 write!(out, ", ");
733 }
734 write!(out, "&return$.value");
735 }
736 if indirect_call {
737 if !sig.args.is_empty() || indirect_return {
738 write!(out, ", ");
739 }
740 write!(out, "extern$");
741 }
742 write!(out, ")");
743 if let Some(ret) = &sig.ret {
744 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
745 write!(out, ")");
746 }
747 }
748 writeln!(out, ";");
749 if sig.throws {
750 writeln!(out, " if (error$.ptr) {{");
751 writeln!(out, " throw ::rust::Error(error$);");
752 writeln!(out, " }}");
753 }
754 if indirect_return {
755 out.include.utility = true;
756 writeln!(out, " return ::std::move(return$.value);");
757 }
758 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400759}
760
761fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
762 match ty {
763 None => write!(out, "void "),
764 Some(ty) => write_type_space(out, ty),
765 }
766}
767
David Tolnay75dca2e2020-03-25 20:17:52 -0700768fn indirect_return(sig: &Signature, types: &Types) -> bool {
769 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700770 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700771 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700772}
773
David Tolnay99642622020-03-25 13:07:35 -0700774fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
775 match ty {
776 Type::RustBox(ty) | Type::UniquePtr(ty) => {
777 write_type_space(out, &ty.inner);
778 write!(out, "*");
779 }
780 Type::Ref(ty) => {
781 if ty.mutability.is_none() {
782 write!(out, "const ");
783 }
784 write_type(out, &ty.inner);
785 write!(out, " *");
786 }
787 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700788 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700789 _ => write_type(out, ty),
790 }
791}
792
793fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
794 write_indirect_return_type(out, ty);
795 match ty {
796 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700797 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700798 _ => write_space_after_type(out, ty),
799 }
800}
801
802fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400803 match ty {
804 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
805 write_type_space(out, &ty.inner);
806 write!(out, "*");
807 }
David Tolnay4a441222020-01-25 16:24:27 -0800808 Some(Type::Ref(ty)) => {
809 if ty.mutability.is_none() {
810 write!(out, "const ");
811 }
812 write_type(out, &ty.inner);
813 write!(out, " *");
814 }
David Tolnay750755e2020-03-01 13:04:08 -0800815 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700816 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400817 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
818 _ => write_return_type(out, ty),
819 }
820}
821
822fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
823 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700824 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnay7db73692019-10-20 14:51:12 -0400825 write_type_space(out, &ty.inner);
826 write!(out, "*");
827 }
David Tolnay750755e2020-03-01 13:04:08 -0800828 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700829 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400830 _ => write_type_space(out, &arg.ty),
831 }
832 if types.needs_indirect_abi(&arg.ty) {
833 write!(out, "*");
834 }
835 write!(out, "{}", arg.ident);
836}
837
838fn write_type(out: &mut OutFile, ty: &Type) {
839 match ty {
840 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay029f1d62020-04-25 10:19:25 -0700841 Some(Bool) => write!(out, "bool"),
842 Some(U8) => write!(out, "uint8_t"),
843 Some(U16) => write!(out, "uint16_t"),
844 Some(U32) => write!(out, "uint32_t"),
845 Some(U64) => write!(out, "uint64_t"),
846 Some(Usize) => write!(out, "size_t"),
847 Some(I8) => write!(out, "int8_t"),
848 Some(I16) => write!(out, "int16_t"),
849 Some(I32) => write!(out, "int32_t"),
850 Some(I64) => write!(out, "int64_t"),
851 Some(Isize) => write!(out, "::rust::isize"),
852 Some(F32) => write!(out, "float"),
853 Some(F64) => write!(out, "double"),
854 Some(CxxString) => write!(out, "::std::string"),
855 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400856 None => write!(out, "{}", ident),
857 },
858 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800859 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400860 write_type(out, &ty.inner);
861 write!(out, ">");
862 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700863 Type::RustVec(ty) => {
864 write!(out, "::rust::Vec<");
865 write_type(out, &ty.inner);
866 write!(out, ">");
867 }
David Tolnay7db73692019-10-20 14:51:12 -0400868 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800869 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400870 write_type(out, &ptr.inner);
871 write!(out, ">");
872 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700873 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700874 write!(out, "::std::vector<");
875 write_type(out, &ty.inner);
876 write!(out, ">");
877 }
David Tolnay7db73692019-10-20 14:51:12 -0400878 Type::Ref(r) => {
879 if r.mutability.is_none() {
880 write!(out, "const ");
881 }
882 write_type(out, &r.inner);
883 write!(out, " &");
884 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700885 Type::Slice(_) => {
886 // For now, only U8 slices are supported, which are covered separately below
887 unreachable!()
888 }
David Tolnay7db73692019-10-20 14:51:12 -0400889 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800890 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400891 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700892 Type::SliceRefU8(_) => {
893 write!(out, "::rust::Slice<uint8_t>");
894 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700895 Type::Fn(f) => {
896 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
897 match &f.ret {
898 Some(ret) => write_type(out, ret),
899 None => write!(out, "void"),
900 }
901 write!(out, "(");
902 for (i, arg) in f.args.iter().enumerate() {
903 if i > 0 {
904 write!(out, ", ");
905 }
906 write_type(out, &arg.ty);
907 }
908 write!(out, ")>");
909 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700910 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400911 }
912}
913
914fn write_type_space(out: &mut OutFile, ty: &Type) {
915 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700916 write_space_after_type(out, ty);
917}
918
919fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400920 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700921 Type::Ident(_)
922 | Type::RustBox(_)
923 | Type::UniquePtr(_)
924 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700925 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700926 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700927 | Type::SliceRefU8(_)
928 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400929 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700930 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400931 }
932}
933
David Tolnaycd08c442020-04-25 10:16:33 -0700934// Only called for legal referent types of unique_ptr and element types of
935// std::vector and Vec.
David Tolnay2eca4a02020-04-24 19:50:51 -0700936fn to_typename(namespace: &Namespace, ty: &Type) -> String {
937 match ty {
938 Type::Ident(ident) => {
David Tolnaycd08c442020-04-25 10:16:33 -0700939 let mut path = String::new();
940 for name in namespace {
David Tolnay63f92e82020-04-30 20:40:20 -0700941 path += &name.to_string();
David Tolnaycd08c442020-04-25 10:16:33 -0700942 path += "::";
David Tolnay2eca4a02020-04-24 19:50:51 -0700943 }
David Tolnaycd08c442020-04-25 10:16:33 -0700944 path += &ident.to_string();
945 path
David Tolnay2eca4a02020-04-24 19:50:51 -0700946 }
947 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(namespace, &ptr.inner)),
David Tolnaycd08c442020-04-25 10:16:33 -0700948 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700949 }
950}
951
David Tolnayacdf20a2020-04-25 12:40:53 -0700952// Only called for legal referent types of unique_ptr and element types of
953// std::vector and Vec.
David Tolnaybae50ef2020-04-25 12:38:41 -0700954fn to_mangled(namespace: &Namespace, ty: &Type) -> String {
955 match ty {
David Tolnayacdf20a2020-04-25 12:40:53 -0700956 Type::Ident(_) => to_typename(namespace, ty).replace("::", "$"),
David Tolnaybae50ef2020-04-25 12:38:41 -0700957 Type::CxxVector(ptr) => format!("std$vector${}", to_mangled(namespace, &ptr.inner)),
David Tolnayacdf20a2020-04-25 12:40:53 -0700958 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700959 }
960}
961
David Tolnay7db73692019-10-20 14:51:12 -0400962fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
963 fn allow_unique_ptr(ident: &Ident) -> bool {
964 Atom::from(ident).is_none()
965 }
966
967 out.begin_block("extern \"C\"");
968 for ty in types {
969 if let Type::RustBox(ty) = ty {
970 if let Type::Ident(inner) = &ty.inner {
971 out.next_section();
972 write_rust_box_extern(out, inner);
973 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700974 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700975 if let Type::Ident(inner) = &ty.inner {
976 if Atom::from(inner).is_none() {
977 out.next_section();
978 write_rust_vec_extern(out, inner);
979 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700980 }
David Tolnay7db73692019-10-20 14:51:12 -0400981 } else if let Type::UniquePtr(ptr) = ty {
982 if let Type::Ident(inner) = &ptr.inner {
983 if allow_unique_ptr(inner) {
984 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -0700985 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700986 }
987 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700988 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +0700989 if let Type::Ident(inner) = &ptr.inner {
David Tolnay63da4d32020-04-25 09:41:12 -0700990 if Atom::from(inner).is_none() {
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -0700992 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400993 }
994 }
995 }
996 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800997 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400998
David Tolnay750755e2020-03-01 13:04:08 -0800999 out.begin_block("namespace rust");
David Tolnay69601622020-04-29 18:48:36 -07001000 out.begin_block("inline namespace cxxbridge03");
David Tolnay7db73692019-10-20 14:51:12 -04001001 for ty in types {
1002 if let Type::RustBox(ty) = ty {
1003 if let Type::Ident(inner) = &ty.inner {
1004 write_rust_box_impl(out, inner);
1005 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001006 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001007 if let Type::Ident(inner) = &ty.inner {
1008 if Atom::from(inner).is_none() {
1009 write_rust_vec_impl(out, inner);
1010 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001011 }
David Tolnay7db73692019-10-20 14:51:12 -04001012 }
1013 }
David Tolnay69601622020-04-29 18:48:36 -07001014 out.end_block("namespace cxxbridge03");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001015 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001016}
1017
1018fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
1019 let mut inner = String::new();
1020 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001021 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001022 inner += "::";
1023 }
1024 inner += &ident.to_string();
1025 let instance = inner.replace("::", "$");
1026
David Tolnay69601622020-04-29 18:48:36 -07001027 writeln!(out, "#ifndef CXXBRIDGE03_RUST_BOX_{}", instance);
1028 writeln!(out, "#define CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001029 writeln!(
1030 out,
David Tolnay69601622020-04-29 18:48:36 -07001031 "void cxxbridge03$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001032 instance, inner,
1033 );
1034 writeln!(
1035 out,
David Tolnay69601622020-04-29 18:48:36 -07001036 "void cxxbridge03$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001037 instance, inner,
1038 );
David Tolnay69601622020-04-29 18:48:36 -07001039 writeln!(out, "#endif // CXXBRIDGE03_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001040}
1041
David Tolnay6787be62020-04-25 11:01:02 -07001042fn write_rust_vec_extern(out: &mut OutFile, element: &Ident) {
1043 let element = Type::Ident(element.clone());
1044 let inner = to_typename(&out.namespace, &element);
1045 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001046
David Tolnay69601622020-04-29 18:48:36 -07001047 writeln!(out, "#ifndef CXXBRIDGE03_RUST_VEC_{}", instance);
1048 writeln!(out, "#define CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001049 writeln!(
1050 out,
David Tolnay69601622020-04-29 18:48:36 -07001051 "void cxxbridge03$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001052 instance, inner,
1053 );
1054 writeln!(
1055 out,
David Tolnay69601622020-04-29 18:48:36 -07001056 "void cxxbridge03$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001057 instance, inner,
1058 );
1059 writeln!(
1060 out,
David Tolnay69601622020-04-29 18:48:36 -07001061 "size_t cxxbridge03$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001062 instance, inner,
1063 );
David Tolnay219c0792020-04-24 20:31:37 -07001064 writeln!(
1065 out,
David Tolnay69601622020-04-29 18:48:36 -07001066 "const {} *cxxbridge03$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001067 inner, instance,
1068 );
David Tolnay503d0192020-04-24 22:18:56 -07001069 writeln!(
1070 out,
David Tolnay69601622020-04-29 18:48:36 -07001071 "size_t cxxbridge03$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001072 instance,
1073 );
David Tolnay69601622020-04-29 18:48:36 -07001074 writeln!(out, "#endif // CXXBRIDGE03_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001075}
1076
David Tolnay7db73692019-10-20 14:51:12 -04001077fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
1078 let mut inner = String::new();
1079 for name in &out.namespace {
David Tolnay63f92e82020-04-30 20:40:20 -07001080 inner += &name.to_string();
David Tolnay7db73692019-10-20 14:51:12 -04001081 inner += "::";
1082 }
1083 inner += &ident.to_string();
1084 let instance = inner.replace("::", "$");
1085
1086 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001087 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001088 writeln!(out, " cxxbridge03$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001089 writeln!(out, "}}");
1090
1091 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001092 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001093 writeln!(out, " cxxbridge03$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001094 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001095}
1096
David Tolnay6787be62020-04-25 11:01:02 -07001097fn write_rust_vec_impl(out: &mut OutFile, element: &Ident) {
1098 let element = Type::Ident(element.clone());
1099 let inner = to_typename(&out.namespace, &element);
1100 let instance = to_mangled(&out.namespace, &element);
David Tolnay4791f1c2020-03-17 21:53:16 -07001101
Myron Ahneba35cf2020-02-05 19:41:51 +07001102 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001103 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001104 writeln!(out, " cxxbridge03$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001105 writeln!(out, "}}");
1106
1107 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001108 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1109 writeln!(
1110 out,
David Tolnay69601622020-04-29 18:48:36 -07001111 " return cxxbridge03$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001112 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001113 );
1114 writeln!(out, "}}");
1115
1116 writeln!(out, "template <>");
1117 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001118 writeln!(out, " return cxxbridge03$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001119 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001120
1121 writeln!(out, "template <>");
1122 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1123 writeln!(
1124 out,
David Tolnay69601622020-04-29 18:48:36 -07001125 " return cxxbridge03$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001126 instance,
1127 );
1128 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001129
1130 writeln!(out, "template <>");
1131 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay69601622020-04-29 18:48:36 -07001132 writeln!(out, " return cxxbridge03$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001133 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001134}
1135
David Tolnay63da4d32020-04-25 09:41:12 -07001136fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
1137 let ty = Type::Ident(ident.clone());
1138 let instance = to_mangled(&out.namespace, &ty);
1139
David Tolnay69601622020-04-29 18:48:36 -07001140 writeln!(out, "#ifndef CXXBRIDGE03_UNIQUE_PTR_{}", instance);
1141 writeln!(out, "#define CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001142
1143 write_unique_ptr_common(out, &ty, types);
1144
David Tolnay69601622020-04-29 18:48:36 -07001145 writeln!(out, "#endif // CXXBRIDGE03_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001146}
1147
1148// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1149fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
Myron Ahneba35cf2020-02-05 19:41:51 +07001150 out.include.utility = true;
David Tolnay4c4b5502020-04-24 18:41:36 -07001151 let inner = to_typename(&out.namespace, ty);
David Tolnayf12e9832020-04-24 18:46:44 -07001152 let instance = to_mangled(&out.namespace, ty);
David Tolnay7db73692019-10-20 14:51:12 -04001153
David Tolnay63da4d32020-04-25 09:41:12 -07001154 let can_construct_from_value = match ty {
1155 Type::Ident(ident) => types.structs.contains_key(ident),
1156 _ => false,
1157 };
1158
David Tolnay7db73692019-10-20 14:51:12 -04001159 writeln!(
1160 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001161 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001162 inner,
1163 );
1164 writeln!(
1165 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001166 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001167 inner,
1168 );
1169 writeln!(
1170 out,
David Tolnay69601622020-04-29 18:48:36 -07001171 "void cxxbridge03$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001172 instance, inner,
1173 );
David Tolnay7e219b82020-03-01 13:14:51 -08001174 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001175 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001176 if can_construct_from_value {
1177 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001178 out,
David Tolnay69601622020-04-29 18:48:36 -07001179 "void cxxbridge03$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001180 instance, inner, inner,
1181 );
David Tolnay63da4d32020-04-25 09:41:12 -07001182 writeln!(
1183 out,
1184 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1185 inner, inner,
1186 );
1187 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001188 }
David Tolnay7db73692019-10-20 14:51:12 -04001189 writeln!(
1190 out,
David Tolnay69601622020-04-29 18:48:36 -07001191 "void cxxbridge03$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001192 instance, inner, inner,
1193 );
David Tolnay7e219b82020-03-01 13:14:51 -08001194 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001195 writeln!(out, "}}");
1196 writeln!(
1197 out,
David Tolnay69601622020-04-29 18:48:36 -07001198 "const {} *cxxbridge03$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001199 inner, instance, inner,
1200 );
1201 writeln!(out, " return ptr.get();");
1202 writeln!(out, "}}");
1203 writeln!(
1204 out,
David Tolnay69601622020-04-29 18:48:36 -07001205 "{} *cxxbridge03$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001206 inner, instance, inner,
1207 );
1208 writeln!(out, " return ptr.release();");
1209 writeln!(out, "}}");
1210 writeln!(
1211 out,
David Tolnay69601622020-04-29 18:48:36 -07001212 "void cxxbridge03$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001213 instance, inner,
1214 );
1215 writeln!(out, " ptr->~unique_ptr();");
1216 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001217}
Myron Ahneba35cf2020-02-05 19:41:51 +07001218
David Tolnay92105da2020-04-25 11:15:31 -07001219fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &Ident, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001220 let element = Type::Ident(element.clone());
1221 let inner = to_typename(&out.namespace, &element);
1222 let instance = to_mangled(&out.namespace, &element);
Myron Ahneba35cf2020-02-05 19:41:51 +07001223
David Tolnay69601622020-04-29 18:48:36 -07001224 writeln!(out, "#ifndef CXXBRIDGE03_VECTOR_{}", instance);
1225 writeln!(out, "#define CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001226 writeln!(
1227 out,
David Tolnay69601622020-04-29 18:48:36 -07001228 "size_t cxxbridge03$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001229 instance, inner,
1230 );
1231 writeln!(out, " return s.size();");
1232 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001233 writeln!(
1234 out,
David Tolnay69601622020-04-29 18:48:36 -07001235 "const {} &cxxbridge03$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001236 inner, instance, inner,
1237 );
David Tolnaydd3f6342020-04-24 14:38:55 -07001238 writeln!(out, " return s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001239 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001240
1241 write_unique_ptr_common(out, vector_ty, types);
1242
David Tolnay69601622020-04-29 18:48:36 -07001243 writeln!(out, "#endif // CXXBRIDGE03_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001244}