blob: a8e3fd485fa4c18da3019b52efbc429847d91d38 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
David Tolnay08419302020-04-19 20:38:20 -07004use crate::syntax::namespace::Namespace;
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
David Tolnaya73853b2020-04-20 01:19:56 -07006use crate::syntax::{mangle, Api, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04007use proc_macro2::Ident;
David Tolnayf94bef12020-04-17 14:46:42 -07008use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -04009
David Tolnay33d30292020-03-18 18:02:02 -070010pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -070011 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070012 apis: &[Api],
13 types: &Types,
14 opt: Opt,
15 header: bool,
16) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040017 let mut out_file = OutFile::new(namespace.clone(), header);
18 let out = &mut out_file;
19
20 if header {
21 writeln!(out, "#pragma once");
22 }
23
David Tolnay33d30292020-03-18 18:02:02 -070024 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040025 for api in apis {
26 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080027 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30
31 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080032 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040033
David Tolnay7db73692019-10-20 14:51:12 -040034 out.next_section();
35 for name in &namespace {
36 writeln!(out, "namespace {} {{", name);
37 }
38
David Tolnay7db73692019-10-20 14:51:12 -040039 out.next_section();
40 for api in apis {
41 match api {
42 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080043 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
44 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040045 _ => {}
46 }
47 }
48
David Tolnayf94bef12020-04-17 14:46:42 -070049 let mut methods_for_type = HashMap::new();
50 for api in apis {
51 if let Api::RustFunction(efn) = api {
52 if let Some(receiver) = &efn.sig.receiver {
53 methods_for_type
David Tolnay05e11cc2020-04-20 02:13:56 -070054 .entry(&receiver.ty)
David Tolnayf94bef12020-04-17 14:46:42 -070055 .or_insert_with(Vec::new)
56 .push(efn);
57 }
58 }
59 }
Joel Galenson968738f2020-04-15 14:19:33 -070060
David Tolnay7db73692019-10-20 14:51:12 -040061 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070062 match api {
63 Api::Struct(strct) => {
64 out.next_section();
65 write_struct(out, strct);
66 }
David Tolnayc1fe0052020-04-17 15:15:06 -070067 Api::RustType(ety) => {
68 if let Some(methods) = methods_for_type.get(&ety.ident) {
David Tolnay46a54e72020-04-17 14:48:21 -070069 out.next_section();
70 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070071 }
David Tolnayc1fe0052020-04-17 15:15:06 -070072 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070073 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040074 }
75 }
76
77 if !header {
78 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070079 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040080 for api in apis {
81 let (efn, write): (_, fn(_, _, _)) = match api {
82 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
83 Api::RustFunction(efn) => (efn, write_rust_function_decl),
84 _ => continue,
85 };
86 out.next_section();
87 write(out, efn, types);
88 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080089 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040090 }
91
92 for api in apis {
93 if let Api::RustFunction(efn) = api {
94 out.next_section();
95 write_rust_function_shim(out, efn, types);
96 }
97 }
98
99 out.next_section();
100 for name in namespace.iter().rev() {
101 writeln!(out, "}} // namespace {}", name);
102 }
103
104 if !header {
105 out.next_section();
106 write_generic_instantiations(out, types);
107 }
108
David Tolnay9c68b1a2020-03-06 11:12:55 -0800109 out.prepend(out.include.to_string());
110
David Tolnay7db73692019-10-20 14:51:12 -0400111 out_file
112}
113
114fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400115 for ty in types {
116 match ty {
117 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -0700118 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
119 | Some(I64) => out.include.cstdint = true,
120 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -0800121 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -0700122 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400123 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800124 Type::RustBox(_) => out.include.type_traits = true,
125 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4770b472020-04-14 16:32:59 -0700126 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7db73692019-10-20 14:51:12 -0400127 _ => {}
128 }
129 }
David Tolnay7db73692019-10-20 14:51:12 -0400130}
131
David Tolnayf51447e2020-03-06 14:14:27 -0800132fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700133 let mut needs_rust_string = false;
134 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700135 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400136 let mut needs_rust_box = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700137 let mut needs_rust_fn = false;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700138 let mut needs_rust_isize = false;
David Tolnay7db73692019-10-20 14:51:12 -0400139 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700140 match ty {
141 Type::RustBox(_) => {
142 out.include.type_traits = true;
143 needs_rust_box = true;
144 }
145 Type::Str(_) => {
146 out.include.cstdint = true;
147 out.include.string = true;
148 needs_rust_str = true;
149 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700150 Type::Fn(_) => {
151 needs_rust_fn = true;
152 }
David Tolnay4770b472020-04-14 16:32:59 -0700153 Type::Slice(_) | Type::SliceRefU8(_) => {
154 needs_rust_slice = true;
155 }
David Tolnayb8a6fb22020-04-10 11:17:28 -0700156 ty if ty == Isize => {
David Tolnay59b5ba12020-04-10 11:32:19 -0700157 out.include.base_tsd = true;
David Tolnayb8a6fb22020-04-10 11:17:28 -0700158 needs_rust_isize = true;
159 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700160 ty if ty == RustString => {
161 out.include.array = true;
162 out.include.cstdint = true;
163 out.include.string = true;
164 needs_rust_string = true;
165 }
166 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400167 }
168 }
169
David Tolnayb7a7cb62020-03-17 21:18:40 -0700170 let mut needs_rust_error = false;
171 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800172 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800173 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700174 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800175 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700176 match api {
177 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700178 if efn.throws {
179 needs_trycatch = true;
180 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 for arg in &efn.args {
182 if arg.ty == RustString {
183 needs_unsafe_bitcopy = true;
184 break;
185 }
David Tolnay09011c32020-03-06 14:40:28 -0800186 }
187 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 Api::RustFunction(efn) if !out.header => {
189 if efn.throws {
190 out.include.exception = true;
191 needs_rust_error = true;
192 }
193 for arg in &efn.args {
194 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
195 needs_manually_drop = true;
196 break;
197 }
198 }
199 if let Some(ret) = &efn.ret {
200 if types.needs_indirect_abi(ret) {
201 needs_maybe_uninit = true;
202 }
David Tolnayf51447e2020-03-06 14:14:27 -0800203 }
204 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700205 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800206 }
207 }
208
David Tolnay750755e2020-03-01 13:04:08 -0800209 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700210 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800211
David Tolnayb7a7cb62020-03-17 21:18:40 -0700212 if needs_rust_string
213 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700214 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700215 || needs_rust_box
David Tolnay75dca2e2020-03-25 20:17:52 -0700216 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217 || needs_rust_error
David Tolnayb8a6fb22020-04-10 11:17:28 -0700218 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700219 || needs_unsafe_bitcopy
220 || needs_manually_drop
221 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700222 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 {
David Tolnay736cbca2020-03-11 16:49:18 -0700224 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800225 }
226
David Tolnayd1402742020-03-25 22:21:42 -0700227 if needs_rust_string {
228 out.next_section();
229 writeln!(out, "struct unsafe_bitcopy_t;");
230 }
231
David Tolnayb7a7cb62020-03-17 21:18:40 -0700232 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
233 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
David Tolnay4770b472020-04-14 16:32:59 -0700234 write_header_section(out, needs_rust_slice, "CXXBRIDGE02_RUST_SLICE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
David Tolnay75dca2e2020-03-25 20:17:52 -0700236 write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700237 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
David Tolnayb8a6fb22020-04-10 11:17:28 -0700238 write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800240
241 if needs_manually_drop {
242 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700243 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800244 writeln!(out, "template <typename T>");
245 writeln!(out, "union ManuallyDrop {{");
246 writeln!(out, " T value;");
247 writeln!(
248 out,
249 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
250 );
251 writeln!(out, " ~ManuallyDrop() {{}}");
252 writeln!(out, "}};");
253 }
254
David Tolnay09011c32020-03-06 14:40:28 -0800255 if needs_maybe_uninit {
256 out.next_section();
257 writeln!(out, "template <typename T>");
258 writeln!(out, "union MaybeUninit {{");
259 writeln!(out, " T value;");
260 writeln!(out, " MaybeUninit() {{}}");
261 writeln!(out, " ~MaybeUninit() {{}}");
262 writeln!(out, "}};");
263 }
264
David Tolnay3e3e0af2020-03-17 22:42:49 -0700265 out.end_block("namespace cxxbridge02");
266
David Tolnay5d121442020-03-17 22:14:40 -0700267 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700268 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700269 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700270 out.include.type_traits = true;
271 out.include.utility = true;
272 writeln!(out, "class missing {{}};");
273 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700274 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700275 writeln!(out, "template <typename Try, typename Fail>");
276 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700277 writeln!(
278 out,
David Tolnay04722332020-03-18 11:31:54 -0700279 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700280 );
David Tolnay04722332020-03-18 11:31:54 -0700281 writeln!(out, " missing>::value>::type");
282 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700283 writeln!(out, " func();");
284 writeln!(out, "}} catch (const ::std::exception &e) {{");
285 writeln!(out, " fail(e.what());");
286 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700287 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700288 }
289
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800290 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400291}
292
David Tolnayb7a7cb62020-03-17 21:18:40 -0700293fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
David Tolnay8e086612020-04-10 12:20:46 -0700294 let section = include::get(section);
David Tolnayb7a7cb62020-03-17 21:18:40 -0700295 if needed {
296 out.next_section();
David Tolnay8e086612020-04-10 12:20:46 -0700297 for line in section.lines() {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700298 if !line.trim_start().starts_with("//") {
299 writeln!(out, "{}", line);
300 }
301 }
302 }
303}
304
David Tolnay7db73692019-10-20 14:51:12 -0400305fn write_struct(out: &mut OutFile, strct: &Struct) {
306 for line in strct.doc.to_string().lines() {
307 writeln!(out, "//{}", line);
308 }
309 writeln!(out, "struct {} final {{", strct.ident);
310 for field in &strct.fields {
311 write!(out, " ");
312 write_type_space(out, &field.ty);
313 writeln!(out, "{};", field.ident);
314 }
315 writeln!(out, "}};");
316}
317
318fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
319 writeln!(out, "struct {};", ident);
320}
321
David Tolnay8861bee2020-01-20 18:39:24 -0800322fn write_struct_using(out: &mut OutFile, ident: &Ident) {
323 writeln!(out, "using {} = {};", ident, ident);
324}
325
David Tolnayc1fe0052020-04-17 15:15:06 -0700326fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700327 for line in ety.doc.to_string().lines() {
328 writeln!(out, "//{}", line);
329 }
330 writeln!(out, "struct {} final {{", ety.ident);
Joel Galenson187588e2020-04-17 16:19:54 -0700331 writeln!(out, " {}() = delete;", ety.ident);
David Tolnay44395e32020-04-19 14:52:49 -0700332 writeln!(out, " {}(const {} &) = delete;", ety.ident, ety.ident);
Joel Galenson968738f2020-04-15 14:19:33 -0700333 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700334 write!(out, " ");
335 let sig = &method.sig;
David Tolnaya73853b2020-04-20 01:19:56 -0700336 let local_name = method.ident.to_string();
337 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700338 writeln!(out, ";");
339 }
340 writeln!(out, "}};");
341}
342
David Tolnayebef4a22020-03-17 15:33:47 -0700343fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
344 let mut has_cxx_throws = false;
345 for api in apis {
346 if let Api::CxxFunction(efn) = api {
347 if efn.throws {
348 has_cxx_throws = true;
349 break;
350 }
351 }
352 }
353
354 if has_cxx_throws {
355 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700356 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700357 out,
358 "const char *cxxbridge02$exception(const char *, size_t);",
359 );
360 }
361}
362
David Tolnay7db73692019-10-20 14:51:12 -0400363fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700364 if efn.throws {
365 write!(out, "::rust::Str::Repr ");
366 } else {
David Tolnay99642622020-03-25 13:07:35 -0700367 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700368 }
David Tolnay3caa50a2020-04-19 21:25:34 -0700369 let mangled = mangle::extern_fn(&out.namespace, efn);
370 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700371 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700372 if receiver.mutability.is_none() {
373 write!(out, "const ");
374 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700375 write!(out, "{} &self", receiver.ty);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700376 }
David Tolnay7db73692019-10-20 14:51:12 -0400377 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700378 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400379 write!(out, ", ");
380 }
David Tolnaya46a2372020-03-06 10:03:48 -0800381 if arg.ty == RustString {
382 write!(out, "const ");
383 }
David Tolnay7db73692019-10-20 14:51:12 -0400384 write_extern_arg(out, arg, types);
385 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700386 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400387 if indirect_return {
388 if !efn.args.is_empty() {
389 write!(out, ", ");
390 }
David Tolnay99642622020-03-25 13:07:35 -0700391 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400392 write!(out, "*return$");
393 }
394 writeln!(out, ") noexcept {{");
395 write!(out, " ");
396 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700397 match &efn.receiver {
398 None => write!(out, "(*{}$)(", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700399 Some(receiver) => write!(out, "({}::*{}$)(", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700400 }
David Tolnay7db73692019-10-20 14:51:12 -0400401 for (i, arg) in efn.args.iter().enumerate() {
402 if i > 0 {
403 write!(out, ", ");
404 }
405 write_type(out, &arg.ty);
406 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700407 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700408 if let Some(receiver) = &efn.receiver {
409 if receiver.mutability.is_none() {
410 write!(out, " const");
411 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700412 }
413 write!(out, " = ");
414 match &efn.receiver {
415 None => write!(out, "{}", efn.ident),
David Tolnay05e11cc2020-04-20 02:13:56 -0700416 Some(receiver) => write!(out, "&{}::{}", receiver.ty, efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700417 }
418 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400419 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700420 if efn.throws {
421 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700422 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700423 writeln!(out, " [&] {{");
424 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700425 }
David Tolnay7db73692019-10-20 14:51:12 -0400426 if indirect_return {
427 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700428 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400429 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700430 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400431 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700432 }
433 match &efn.ret {
434 Some(Type::Ref(_)) => write!(out, "&"),
435 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700436 Some(Type::SliceRefU8(_)) if !indirect_return => {
437 write!(out, "::rust::Slice<uint8_t>::Repr(")
438 }
David Tolnay99642622020-03-25 13:07:35 -0700439 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400440 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700441 match &efn.receiver {
442 None => write!(out, "{}$(", efn.ident),
David Tolnay41909e62020-04-20 00:55:15 -0700443 Some(_) => write!(out, "(self.*{}$)(", efn.ident),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700444 }
David Tolnay7db73692019-10-20 14:51:12 -0400445 for (i, arg) in efn.args.iter().enumerate() {
446 if i > 0 {
447 write!(out, ", ");
448 }
449 if let Type::RustBox(_) = &arg.ty {
450 write_type(out, &arg.ty);
451 write!(out, "::from_raw({})", arg.ident);
452 } else if let Type::UniquePtr(_) = &arg.ty {
453 write_type(out, &arg.ty);
454 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800455 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800456 write!(
457 out,
458 "::rust::String(::rust::unsafe_bitcopy, *{})",
459 arg.ident,
460 );
David Tolnay7db73692019-10-20 14:51:12 -0400461 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700462 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800463 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400464 } else {
465 write!(out, "{}", arg.ident);
466 }
467 }
468 write!(out, ")");
469 match &efn.ret {
470 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
471 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700472 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400473 _ => {}
474 }
475 if indirect_return {
476 write!(out, ")");
477 }
478 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700479 if efn.throws {
480 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700481 writeln!(out, " throw$.ptr = nullptr;");
482 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700483 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700484 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700485 writeln!(
486 out,
David Tolnay5d121442020-03-17 22:14:40 -0700487 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700488 );
David Tolnay5d121442020-03-17 22:14:40 -0700489 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700490 writeln!(out, " return throw$;");
491 }
David Tolnay7db73692019-10-20 14:51:12 -0400492 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700493 for arg in &efn.args {
494 if let Type::Fn(f) = &arg.ty {
495 let var = &arg.ident;
496 write_function_pointer_trampoline(out, efn, var, f, types);
497 }
498 }
499}
500
501fn write_function_pointer_trampoline(
502 out: &mut OutFile,
503 efn: &ExternFn,
504 var: &Ident,
505 f: &Signature,
506 types: &Types,
507) {
508 out.next_section();
David Tolnay891061b2020-04-19 22:42:33 -0700509 let r_trampoline = mangle::r_trampoline(&out.namespace, efn, var);
David Tolnay75dca2e2020-03-25 20:17:52 -0700510 let indirect_call = true;
511 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
512
513 out.next_section();
David Tolnaya73853b2020-04-20 01:19:56 -0700514 let c_trampoline = mangle::c_trampoline(&out.namespace, efn, var).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700515 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400516}
517
518fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay3caa50a2020-04-19 21:25:34 -0700519 let link_name = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700520 let indirect_call = false;
521 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
522}
523
524fn write_rust_function_decl_impl(
525 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700526 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700527 sig: &Signature,
528 types: &Types,
529 indirect_call: bool,
530) {
531 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700532 write!(out, "::rust::Str::Repr ");
533 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700534 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700535 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700536 write!(out, "{}(", link_name);
537 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700538 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700539 if receiver.mutability.is_none() {
540 write!(out, "const ");
541 }
David Tolnay05e11cc2020-04-20 02:13:56 -0700542 write!(out, "{} &self", receiver.ty);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700543 needs_comma = true;
544 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700545 for arg in &sig.args {
546 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400547 write!(out, ", ");
548 }
549 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700550 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400551 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700552 if indirect_return(sig, types) {
553 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400554 write!(out, ", ");
555 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700556 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400557 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 needs_comma = true;
559 }
560 if indirect_call {
561 if needs_comma {
562 write!(out, ", ");
563 }
564 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400565 }
566 writeln!(out, ") noexcept;");
567}
568
569fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400570 for line in efn.doc.to_string().lines() {
571 writeln!(out, "//{}", line);
572 }
David Tolnaya73853b2020-04-20 01:19:56 -0700573 let local_name = match &efn.sig.receiver {
574 None => efn.ident.to_string(),
David Tolnay05e11cc2020-04-20 02:13:56 -0700575 Some(receiver) => format!("{}::{}", receiver.ty, efn.ident),
David Tolnaya73853b2020-04-20 01:19:56 -0700576 };
David Tolnay3caa50a2020-04-19 21:25:34 -0700577 let invoke = mangle::extern_fn(&out.namespace, efn);
David Tolnay75dca2e2020-03-25 20:17:52 -0700578 let indirect_call = false;
579 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
580}
581
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700582fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700583 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700584 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700585 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700586 indirect_call: bool,
587) {
588 write_return_type(out, &sig.ret);
589 write!(out, "{}(", local_name);
590 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400591 if i > 0 {
592 write!(out, ", ");
593 }
594 write_type_space(out, &arg.ty);
595 write!(out, "{}", arg.ident);
596 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700597 if indirect_call {
598 if !sig.args.is_empty() {
599 write!(out, ", ");
600 }
601 write!(out, "void *extern$");
602 }
David Tolnay1e548172020-03-16 13:37:09 -0700603 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700604 if let Some(receiver) = &sig.receiver {
605 if receiver.mutability.is_none() {
606 write!(out, " const");
607 }
608 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700609 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700610 write!(out, " noexcept");
611 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700612}
613
614fn write_rust_function_shim_impl(
615 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700616 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700617 sig: &Signature,
618 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700619 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700620 indirect_call: bool,
621) {
622 if out.header && sig.receiver.is_some() {
623 // We've already defined this inside the struct.
624 return;
625 }
David Tolnaya73853b2020-04-20 01:19:56 -0700626 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400627 if out.header {
628 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700629 return;
David Tolnay7db73692019-10-20 14:51:12 -0400630 }
David Tolnay439cde22020-04-20 00:46:25 -0700631 writeln!(out, " {{");
632 for arg in &sig.args {
633 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
634 out.include.utility = true;
635 write!(out, " ::rust::ManuallyDrop<");
636 write_type(out, &arg.ty);
637 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
638 }
639 }
640 write!(out, " ");
641 let indirect_return = indirect_return(sig, types);
642 if indirect_return {
643 write!(out, "::rust::MaybeUninit<");
644 write_type(out, sig.ret.as_ref().unwrap());
645 writeln!(out, "> return$;");
646 write!(out, " ");
647 } else if let Some(ret) = &sig.ret {
648 write!(out, "return ");
649 match ret {
650 Type::RustBox(_) => {
651 write_type(out, ret);
652 write!(out, "::from_raw(");
653 }
654 Type::UniquePtr(_) => {
655 write_type(out, ret);
656 write!(out, "(");
657 }
658 Type::Ref(_) => write!(out, "*"),
659 _ => {}
660 }
661 }
662 if sig.throws {
663 write!(out, "::rust::Str::Repr error$ = ");
664 }
665 write!(out, "{}(", invoke);
666 if sig.receiver.is_some() {
667 write!(out, "*this");
668 }
669 for (i, arg) in sig.args.iter().enumerate() {
670 if i > 0 || sig.receiver.is_some() {
671 write!(out, ", ");
672 }
673 match &arg.ty {
674 Type::Str(_) => write!(out, "::rust::Str::Repr("),
675 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
676 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
677 _ => {}
678 }
679 write!(out, "{}", arg.ident);
680 match &arg.ty {
681 Type::RustBox(_) => write!(out, ".into_raw()"),
682 Type::UniquePtr(_) => write!(out, ".release()"),
683 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
684 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
685 _ => {}
686 }
687 }
688 if indirect_return {
689 if !sig.args.is_empty() {
690 write!(out, ", ");
691 }
692 write!(out, "&return$.value");
693 }
694 if indirect_call {
695 if !sig.args.is_empty() || indirect_return {
696 write!(out, ", ");
697 }
698 write!(out, "extern$");
699 }
700 write!(out, ")");
701 if let Some(ret) = &sig.ret {
702 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
703 write!(out, ")");
704 }
705 }
706 writeln!(out, ";");
707 if sig.throws {
708 writeln!(out, " if (error$.ptr) {{");
709 writeln!(out, " throw ::rust::Error(error$);");
710 writeln!(out, " }}");
711 }
712 if indirect_return {
713 out.include.utility = true;
714 writeln!(out, " return ::std::move(return$.value);");
715 }
716 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400717}
718
719fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
720 match ty {
721 None => write!(out, "void "),
722 Some(ty) => write_type_space(out, ty),
723 }
724}
725
David Tolnay75dca2e2020-03-25 20:17:52 -0700726fn indirect_return(sig: &Signature, types: &Types) -> bool {
727 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700728 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700729 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700730}
731
David Tolnay99642622020-03-25 13:07:35 -0700732fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
733 match ty {
734 Type::RustBox(ty) | Type::UniquePtr(ty) => {
735 write_type_space(out, &ty.inner);
736 write!(out, "*");
737 }
738 Type::Ref(ty) => {
739 if ty.mutability.is_none() {
740 write!(out, "const ");
741 }
742 write_type(out, &ty.inner);
743 write!(out, " *");
744 }
745 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700746 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnay99642622020-03-25 13:07:35 -0700747 _ => write_type(out, ty),
748 }
749}
750
751fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
752 write_indirect_return_type(out, ty);
753 match ty {
754 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700755 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700756 _ => write_space_after_type(out, ty),
757 }
758}
759
760fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400761 match ty {
762 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
763 write_type_space(out, &ty.inner);
764 write!(out, "*");
765 }
David Tolnay4a441222020-01-25 16:24:27 -0800766 Some(Type::Ref(ty)) => {
767 if ty.mutability.is_none() {
768 write!(out, "const ");
769 }
770 write_type(out, &ty.inner);
771 write!(out, " *");
772 }
David Tolnay750755e2020-03-01 13:04:08 -0800773 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700774 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400775 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
776 _ => write_return_type(out, ty),
777 }
778}
779
780fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
781 match &arg.ty {
782 Type::RustBox(ty) | Type::UniquePtr(ty) => {
783 write_type_space(out, &ty.inner);
784 write!(out, "*");
785 }
David Tolnay750755e2020-03-01 13:04:08 -0800786 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700787 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400788 _ => write_type_space(out, &arg.ty),
789 }
790 if types.needs_indirect_abi(&arg.ty) {
791 write!(out, "*");
792 }
793 write!(out, "{}", arg.ident);
794}
795
796fn write_type(out: &mut OutFile, ty: &Type) {
797 match ty {
798 Type::Ident(ident) => match Atom::from(ident) {
799 Some(Bool) => write!(out, "bool"),
800 Some(U8) => write!(out, "uint8_t"),
801 Some(U16) => write!(out, "uint16_t"),
802 Some(U32) => write!(out, "uint32_t"),
803 Some(U64) => write!(out, "uint64_t"),
804 Some(Usize) => write!(out, "size_t"),
805 Some(I8) => write!(out, "int8_t"),
806 Some(I16) => write!(out, "int16_t"),
807 Some(I32) => write!(out, "int32_t"),
808 Some(I64) => write!(out, "int64_t"),
David Tolnayb8a6fb22020-04-10 11:17:28 -0700809 Some(Isize) => write!(out, "::rust::isize"),
David Tolnay3383ae72020-03-13 01:12:26 -0700810 Some(F32) => write!(out, "float"),
811 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800812 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800813 Some(RustString) => write!(out, "::rust::String"),
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 }
821 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800822 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400823 write_type(out, &ptr.inner);
824 write!(out, ">");
825 }
826 Type::Ref(r) => {
827 if r.mutability.is_none() {
828 write!(out, "const ");
829 }
830 write_type(out, &r.inner);
831 write!(out, " &");
832 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700833 Type::Slice(_) => {
834 // For now, only U8 slices are supported, which are covered separately below
835 unreachable!()
836 }
David Tolnay7db73692019-10-20 14:51:12 -0400837 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800838 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400839 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700840 Type::SliceRefU8(_) => {
841 write!(out, "::rust::Slice<uint8_t>");
842 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700843 Type::Fn(f) => {
844 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
845 match &f.ret {
846 Some(ret) => write_type(out, ret),
847 None => write!(out, "void"),
848 }
849 write!(out, "(");
850 for (i, arg) in f.args.iter().enumerate() {
851 if i > 0 {
852 write!(out, ", ");
853 }
854 write_type(out, &arg.ty);
855 }
856 write!(out, ")>");
857 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700858 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400859 }
860}
861
862fn write_type_space(out: &mut OutFile, ty: &Type) {
863 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700864 write_space_after_type(out, ty);
865}
866
867fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400868 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700869 Type::Ident(_)
870 | Type::RustBox(_)
871 | Type::UniquePtr(_)
872 | Type::Str(_)
873 | Type::SliceRefU8(_)
874 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400875 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700876 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400877 }
878}
879
880fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
881 fn allow_unique_ptr(ident: &Ident) -> bool {
882 Atom::from(ident).is_none()
883 }
884
885 out.begin_block("extern \"C\"");
886 for ty in types {
887 if let Type::RustBox(ty) = ty {
888 if let Type::Ident(inner) = &ty.inner {
889 out.next_section();
890 write_rust_box_extern(out, inner);
891 }
892 } else if let Type::UniquePtr(ptr) = ty {
893 if let Type::Ident(inner) = &ptr.inner {
894 if allow_unique_ptr(inner) {
895 out.next_section();
David Tolnay53838912020-04-09 20:56:44 -0700896 write_unique_ptr(out, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400897 }
898 }
899 }
900 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800901 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400902
David Tolnay750755e2020-03-01 13:04:08 -0800903 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700904 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400905 for ty in types {
906 if let Type::RustBox(ty) = ty {
907 if let Type::Ident(inner) = &ty.inner {
908 write_rust_box_impl(out, inner);
909 }
910 }
911 }
David Tolnay8c730492020-03-13 01:29:06 -0700912 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800913 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400914}
915
916fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
917 let mut inner = String::new();
918 for name in &out.namespace {
919 inner += name;
920 inner += "::";
921 }
922 inner += &ident.to_string();
923 let instance = inner.replace("::", "$");
924
David Tolnay8c730492020-03-13 01:29:06 -0700925 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
926 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400927 writeln!(
928 out,
David Tolnay8c730492020-03-13 01:29:06 -0700929 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400930 instance, inner,
931 );
932 writeln!(
933 out,
David Tolnay8c730492020-03-13 01:29:06 -0700934 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400935 instance, inner,
936 );
David Tolnay8c730492020-03-13 01:29:06 -0700937 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400938}
939
940fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
941 let mut inner = String::new();
942 for name in &out.namespace {
943 inner += name;
944 inner += "::";
945 }
946 inner += &ident.to_string();
947 let instance = inner.replace("::", "$");
948
949 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800950 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700951 writeln!(out, " cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400952 writeln!(out, "}}");
953
954 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800955 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay737e02e2020-04-04 21:52:46 -0700956 writeln!(out, " cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400957 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400958}
959
David Tolnay53838912020-04-09 20:56:44 -0700960fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700961 out.include.utility = true;
962
David Tolnay7db73692019-10-20 14:51:12 -0400963 let mut inner = String::new();
964 for name in &out.namespace {
965 inner += name;
966 inner += "::";
967 }
968 inner += &ident.to_string();
969 let instance = inner.replace("::", "$");
970
David Tolnay8c730492020-03-13 01:29:06 -0700971 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
972 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400973 writeln!(
974 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800975 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400976 inner,
977 );
978 writeln!(
979 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800980 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400981 inner,
982 );
983 writeln!(
984 out,
David Tolnay8c730492020-03-13 01:29:06 -0700985 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400986 instance, inner,
987 );
David Tolnay7e219b82020-03-01 13:14:51 -0800988 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400989 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -0700990 if types.structs.contains_key(ident) {
991 writeln!(
992 out,
993 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
994 instance, inner, inner,
995 );
996 writeln!(
997 out,
998 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
999 inner, inner,
1000 );
1001 writeln!(out, "}}");
1002 }
David Tolnay7db73692019-10-20 14:51:12 -04001003 writeln!(
1004 out,
David Tolnay8c730492020-03-13 01:29:06 -07001005 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001006 instance, inner, inner,
1007 );
David Tolnay7e219b82020-03-01 13:14:51 -08001008 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001009 writeln!(out, "}}");
1010 writeln!(
1011 out,
David Tolnay8c730492020-03-13 01:29:06 -07001012 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001013 inner, instance, inner,
1014 );
1015 writeln!(out, " return ptr.get();");
1016 writeln!(out, "}}");
1017 writeln!(
1018 out,
David Tolnay8c730492020-03-13 01:29:06 -07001019 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001020 inner, instance, inner,
1021 );
1022 writeln!(out, " return ptr.release();");
1023 writeln!(out, "}}");
1024 writeln!(
1025 out,
David Tolnay8c730492020-03-13 01:29:06 -07001026 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001027 instance, inner,
1028 );
1029 writeln!(out, " ptr->~unique_ptr();");
1030 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -07001031 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001032}