blob: a1d574835feeb7ee50766b004daecc486cd1ad38 [file] [log] [blame]
David Tolnay754e21c2020-03-29 20:58:46 -07001use crate::gen::namespace::Namespace;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
5use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
6use proc_macro2::Ident;
7
David Tolnay33d30292020-03-18 18:02:02 -07008pub(super) fn gen(
David Tolnay754e21c2020-03-29 20:58:46 -07009 namespace: Namespace,
David Tolnay33d30292020-03-18 18:02:02 -070010 apis: &[Api],
11 types: &Types,
12 opt: Opt,
13 header: bool,
14) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040015 let mut out_file = OutFile::new(namespace.clone(), header);
16 let out = &mut out_file;
17
18 if header {
19 writeln!(out, "#pragma once");
20 }
21
David Tolnay33d30292020-03-18 18:02:02 -070022 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023 for api in apis {
24 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080025 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040026 }
27 }
28
29 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080030 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay7db73692019-10-20 14:51:12 -040032 out.next_section();
33 for name in &namespace {
34 writeln!(out, "namespace {} {{", name);
35 }
36
David Tolnay7db73692019-10-20 14:51:12 -040037 out.next_section();
38 for api in apis {
39 match api {
40 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080041 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
42 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040043 _ => {}
44 }
45 }
46
47 for api in apis {
48 if let Api::Struct(strct) = api {
49 out.next_section();
50 write_struct(out, strct);
51 }
52 }
53
54 if !header {
55 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070056 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040057 for api in apis {
58 let (efn, write): (_, fn(_, _, _)) = match api {
59 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
60 Api::RustFunction(efn) => (efn, write_rust_function_decl),
61 _ => continue,
62 };
63 out.next_section();
64 write(out, efn, types);
65 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080066 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68
69 for api in apis {
70 if let Api::RustFunction(efn) = api {
71 out.next_section();
72 write_rust_function_shim(out, efn, types);
73 }
74 }
75
76 out.next_section();
77 for name in namespace.iter().rev() {
78 writeln!(out, "}} // namespace {}", name);
79 }
80
81 if !header {
82 out.next_section();
83 write_generic_instantiations(out, types);
84 }
85
David Tolnay9c68b1a2020-03-06 11:12:55 -080086 out.prepend(out.include.to_string());
87
David Tolnay7db73692019-10-20 14:51:12 -040088 out_file
89}
90
91fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040092 for ty in types {
93 match ty {
94 Type::Ident(ident) => match Atom::from(ident) {
David Tolnay30430f12020-03-19 20:49:00 -070095 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
96 | Some(I64) => out.include.cstdint = true,
97 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -080098 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -070099 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -0400100 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800101 Type::RustBox(_) => out.include.type_traits = true,
102 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -0400103 _ => {}
104 }
105 }
David Tolnay7db73692019-10-20 14:51:12 -0400106}
107
David Tolnayf51447e2020-03-06 14:14:27 -0800108fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700109 let mut needs_rust_string = false;
110 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400111 let mut needs_rust_box = false;
112 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700113 match ty {
114 Type::RustBox(_) => {
115 out.include.type_traits = true;
116 needs_rust_box = true;
117 }
118 Type::Str(_) => {
119 out.include.cstdint = true;
120 out.include.string = true;
121 needs_rust_str = true;
122 }
123 ty if ty == RustString => {
124 out.include.array = true;
125 out.include.cstdint = true;
126 out.include.string = true;
127 needs_rust_string = true;
128 }
129 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400130 }
131 }
132
David Tolnayb7a7cb62020-03-17 21:18:40 -0700133 let mut needs_rust_error = false;
134 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800135 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800136 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700137 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800138 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700139 match api {
140 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700141 if efn.throws {
142 needs_trycatch = true;
143 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700144 for arg in &efn.args {
145 if arg.ty == RustString {
146 needs_unsafe_bitcopy = true;
147 break;
148 }
David Tolnay09011c32020-03-06 14:40:28 -0800149 }
150 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700151 Api::RustFunction(efn) if !out.header => {
152 if efn.throws {
153 out.include.exception = true;
154 needs_rust_error = true;
155 }
156 for arg in &efn.args {
157 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
158 needs_manually_drop = true;
159 break;
160 }
161 }
162 if let Some(ret) = &efn.ret {
163 if types.needs_indirect_abi(ret) {
164 needs_maybe_uninit = true;
165 }
David Tolnayf51447e2020-03-06 14:14:27 -0800166 }
167 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800169 }
170 }
171
David Tolnay750755e2020-03-01 13:04:08 -0800172 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700173 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800174
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175 if needs_rust_string
176 || needs_rust_str
177 || needs_rust_box
178 || needs_rust_error
179 || needs_unsafe_bitcopy
180 || needs_manually_drop
181 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700182 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700183 {
David Tolnay736cbca2020-03-11 16:49:18 -0700184 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800185 }
186
David Tolnayd1402742020-03-25 22:21:42 -0700187 if needs_rust_string {
188 out.next_section();
189 writeln!(out, "struct unsafe_bitcopy_t;");
190 }
191
David Tolnayb7a7cb62020-03-17 21:18:40 -0700192 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
193 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
194 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
195 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
196 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800197
198 if needs_manually_drop {
199 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700200 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800201 writeln!(out, "template <typename T>");
202 writeln!(out, "union ManuallyDrop {{");
203 writeln!(out, " T value;");
204 writeln!(
205 out,
206 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
207 );
208 writeln!(out, " ~ManuallyDrop() {{}}");
209 writeln!(out, "}};");
210 }
211
David Tolnay09011c32020-03-06 14:40:28 -0800212 if needs_maybe_uninit {
213 out.next_section();
214 writeln!(out, "template <typename T>");
215 writeln!(out, "union MaybeUninit {{");
216 writeln!(out, " T value;");
217 writeln!(out, " MaybeUninit() {{}}");
218 writeln!(out, " ~MaybeUninit() {{}}");
219 writeln!(out, "}};");
220 }
221
David Tolnay3e3e0af2020-03-17 22:42:49 -0700222 out.end_block("namespace cxxbridge02");
223
David Tolnay5d121442020-03-17 22:14:40 -0700224 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700225 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700226 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700227 out.include.type_traits = true;
228 out.include.utility = true;
229 writeln!(out, "class missing {{}};");
230 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700231 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700232 writeln!(out, "template <typename Try, typename Fail>");
233 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700234 writeln!(
235 out,
David Tolnay04722332020-03-18 11:31:54 -0700236 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700237 );
David Tolnay04722332020-03-18 11:31:54 -0700238 writeln!(out, " missing>::value>::type");
239 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700240 writeln!(out, " func();");
241 writeln!(out, "}} catch (const ::std::exception &e) {{");
242 writeln!(out, " fail(e.what());");
243 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700244 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700245 }
246
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800247 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400248}
249
David Tolnayb7a7cb62020-03-17 21:18:40 -0700250fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
251 if needed {
252 out.next_section();
253 for line in include::get(section).lines() {
254 if !line.trim_start().starts_with("//") {
255 writeln!(out, "{}", line);
256 }
257 }
258 }
259}
260
David Tolnay7db73692019-10-20 14:51:12 -0400261fn write_struct(out: &mut OutFile, strct: &Struct) {
262 for line in strct.doc.to_string().lines() {
263 writeln!(out, "//{}", line);
264 }
265 writeln!(out, "struct {} final {{", strct.ident);
266 for field in &strct.fields {
267 write!(out, " ");
268 write_type_space(out, &field.ty);
269 writeln!(out, "{};", field.ident);
270 }
271 writeln!(out, "}};");
272}
273
274fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
275 writeln!(out, "struct {};", ident);
276}
277
David Tolnay8861bee2020-01-20 18:39:24 -0800278fn write_struct_using(out: &mut OutFile, ident: &Ident) {
279 writeln!(out, "using {} = {};", ident, ident);
280}
281
David Tolnayebef4a22020-03-17 15:33:47 -0700282fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
283 let mut has_cxx_throws = false;
284 for api in apis {
285 if let Api::CxxFunction(efn) = api {
286 if efn.throws {
287 has_cxx_throws = true;
288 break;
289 }
290 }
291 }
292
293 if has_cxx_throws {
294 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700295 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700296 out,
297 "const char *cxxbridge02$exception(const char *, size_t);",
298 );
299 }
300}
301
David Tolnay7db73692019-10-20 14:51:12 -0400302fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700303 if efn.throws {
304 write!(out, "::rust::Str::Repr ");
305 } else {
David Tolnay99642622020-03-25 13:07:35 -0700306 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700307 }
David Tolnay7db73692019-10-20 14:51:12 -0400308 for name in out.namespace.clone() {
309 write!(out, "{}$", name);
310 }
David Tolnay8c730492020-03-13 01:29:06 -0700311 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400312 for (i, arg) in efn.args.iter().enumerate() {
313 if i > 0 {
314 write!(out, ", ");
315 }
David Tolnaya46a2372020-03-06 10:03:48 -0800316 if arg.ty == RustString {
317 write!(out, "const ");
318 }
David Tolnay7db73692019-10-20 14:51:12 -0400319 write_extern_arg(out, arg, types);
320 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700321 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400322 if indirect_return {
323 if !efn.args.is_empty() {
324 write!(out, ", ");
325 }
David Tolnay99642622020-03-25 13:07:35 -0700326 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400327 write!(out, "*return$");
328 }
329 writeln!(out, ") noexcept {{");
330 write!(out, " ");
331 write_return_type(out, &efn.ret);
332 write!(out, "(*{}$)(", efn.ident);
333 for (i, arg) in efn.args.iter().enumerate() {
334 if i > 0 {
335 write!(out, ", ");
336 }
337 write_type(out, &arg.ty);
338 }
339 writeln!(out, ") = {};", efn.ident);
340 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700341 if efn.throws {
342 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700343 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700344 writeln!(out, " [&] {{");
345 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700346 }
David Tolnay7db73692019-10-20 14:51:12 -0400347 if indirect_return {
348 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700349 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400350 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700351 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400352 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700353 }
354 match &efn.ret {
355 Some(Type::Ref(_)) => write!(out, "&"),
356 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
357 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400358 }
359 write!(out, "{}$(", efn.ident);
360 for (i, arg) in efn.args.iter().enumerate() {
361 if i > 0 {
362 write!(out, ", ");
363 }
364 if let Type::RustBox(_) = &arg.ty {
365 write_type(out, &arg.ty);
366 write!(out, "::from_raw({})", arg.ident);
367 } else if let Type::UniquePtr(_) = &arg.ty {
368 write_type(out, &arg.ty);
369 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800370 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800371 write!(
372 out,
373 "::rust::String(::rust::unsafe_bitcopy, *{})",
374 arg.ident,
375 );
David Tolnay7db73692019-10-20 14:51:12 -0400376 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700377 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800378 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400379 } else {
380 write!(out, "{}", arg.ident);
381 }
382 }
383 write!(out, ")");
384 match &efn.ret {
385 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
386 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay99642622020-03-25 13:07:35 -0700387 Some(Type::Str(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400388 _ => {}
389 }
390 if indirect_return {
391 write!(out, ")");
392 }
393 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700394 if efn.throws {
395 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700396 writeln!(out, " throw$.ptr = nullptr;");
397 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700398 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700399 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700400 writeln!(
401 out,
David Tolnay5d121442020-03-17 22:14:40 -0700402 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700403 );
David Tolnay5d121442020-03-17 22:14:40 -0700404 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700405 writeln!(out, " return throw$;");
406 }
David Tolnay7db73692019-10-20 14:51:12 -0400407 writeln!(out, "}}");
408}
409
410fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700411 if efn.throws {
412 write!(out, "::rust::Str::Repr ");
413 } else {
David Tolnay99642622020-03-25 13:07:35 -0700414 write_extern_return_type_space(out, &efn.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 for name in out.namespace.clone() {
417 write!(out, "{}$", name);
418 }
David Tolnay8c730492020-03-13 01:29:06 -0700419 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400420 for (i, arg) in efn.args.iter().enumerate() {
421 if i > 0 {
422 write!(out, ", ");
423 }
424 write_extern_arg(out, arg, types);
425 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700426 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400427 if !efn.args.is_empty() {
428 write!(out, ", ");
429 }
430 write_return_type(out, &efn.ret);
431 write!(out, "*return$");
432 }
433 writeln!(out, ") noexcept;");
434}
435
436fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400437 for line in efn.doc.to_string().lines() {
438 writeln!(out, "//{}", line);
439 }
440 write_return_type(out, &efn.ret);
441 write!(out, "{}(", efn.ident);
442 for (i, arg) in efn.args.iter().enumerate() {
443 if i > 0 {
444 write!(out, ", ");
445 }
446 write_type_space(out, &arg.ty);
447 write!(out, "{}", arg.ident);
448 }
David Tolnay1e548172020-03-16 13:37:09 -0700449 write!(out, ")");
450 if !efn.throws {
451 write!(out, " noexcept");
452 }
David Tolnay7db73692019-10-20 14:51:12 -0400453 if out.header {
454 writeln!(out, ";");
455 } else {
456 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800457 for arg in &efn.args {
458 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700459 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800460 write!(out, " ::rust::ManuallyDrop<");
461 write_type(out, &arg.ty);
462 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
463 }
464 }
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700466 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400467 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800468 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400469 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800470 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400471 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800472 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400473 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800474 match ret {
475 Type::RustBox(_) => {
476 write_type(out, ret);
477 write!(out, "::from_raw(");
478 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800479 Type::UniquePtr(_) => {
480 write_type(out, ret);
481 write!(out, "(");
482 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800483 Type::Ref(_) => write!(out, "*"),
484 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800485 }
David Tolnay7db73692019-10-20 14:51:12 -0400486 }
David Tolnay1e548172020-03-16 13:37:09 -0700487 if efn.throws {
488 write!(out, "::rust::Str::Repr error$ = ");
489 }
David Tolnay7db73692019-10-20 14:51:12 -0400490 for name in out.namespace.clone() {
491 write!(out, "{}$", name);
492 }
David Tolnay8c730492020-03-13 01:29:06 -0700493 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400494 for (i, arg) in efn.args.iter().enumerate() {
495 if i > 0 {
496 write!(out, ", ");
497 }
David Tolnaybaae4432020-03-01 20:20:10 -0800498 match &arg.ty {
499 Type::Str(_) => write!(out, "::rust::Str::Repr("),
500 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
501 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400502 }
503 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800504 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800505 Type::RustBox(_) => write!(out, ".into_raw()"),
506 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800507 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800508 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800509 _ => {}
510 }
David Tolnay7db73692019-10-20 14:51:12 -0400511 }
512 if indirect_return {
513 if !efn.args.is_empty() {
514 write!(out, ", ");
515 }
David Tolnay09011c32020-03-06 14:40:28 -0800516 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400517 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800518 write!(out, ")");
519 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800520 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800521 write!(out, ")");
522 }
523 }
524 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700525 if efn.throws {
526 writeln!(out, " if (error$.ptr) {{");
527 writeln!(out, " throw ::rust::Error(error$);");
528 writeln!(out, " }}");
529 }
David Tolnay7db73692019-10-20 14:51:12 -0400530 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700531 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800532 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400533 }
534 writeln!(out, "}}");
535 }
536}
537
538fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
539 match ty {
540 None => write!(out, "void "),
541 Some(ty) => write_type_space(out, ty),
542 }
543}
544
David Tolnay277e3cc2020-03-17 00:11:01 -0700545fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
546 efn.ret
547 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700548 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700549}
550
David Tolnay99642622020-03-25 13:07:35 -0700551fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
552 match ty {
553 Type::RustBox(ty) | Type::UniquePtr(ty) => {
554 write_type_space(out, &ty.inner);
555 write!(out, "*");
556 }
557 Type::Ref(ty) => {
558 if ty.mutability.is_none() {
559 write!(out, "const ");
560 }
561 write_type(out, &ty.inner);
562 write!(out, " *");
563 }
564 Type::Str(_) => write!(out, "::rust::Str::Repr"),
565 _ => write_type(out, ty),
566 }
567}
568
569fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
570 write_indirect_return_type(out, ty);
571 match ty {
572 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
573 Type::Str(_) => write!(out, " "),
574 _ => write_space_after_type(out, ty),
575 }
576}
577
578fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400579 match ty {
580 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
581 write_type_space(out, &ty.inner);
582 write!(out, "*");
583 }
David Tolnay4a441222020-01-25 16:24:27 -0800584 Some(Type::Ref(ty)) => {
585 if ty.mutability.is_none() {
586 write!(out, "const ");
587 }
588 write_type(out, &ty.inner);
589 write!(out, " *");
590 }
David Tolnay750755e2020-03-01 13:04:08 -0800591 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400592 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
593 _ => write_return_type(out, ty),
594 }
595}
596
597fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
598 match &arg.ty {
599 Type::RustBox(ty) | Type::UniquePtr(ty) => {
600 write_type_space(out, &ty.inner);
601 write!(out, "*");
602 }
David Tolnay750755e2020-03-01 13:04:08 -0800603 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400604 _ => write_type_space(out, &arg.ty),
605 }
606 if types.needs_indirect_abi(&arg.ty) {
607 write!(out, "*");
608 }
609 write!(out, "{}", arg.ident);
610}
611
612fn write_type(out: &mut OutFile, ty: &Type) {
613 match ty {
614 Type::Ident(ident) => match Atom::from(ident) {
615 Some(Bool) => write!(out, "bool"),
616 Some(U8) => write!(out, "uint8_t"),
617 Some(U16) => write!(out, "uint16_t"),
618 Some(U32) => write!(out, "uint32_t"),
619 Some(U64) => write!(out, "uint64_t"),
620 Some(Usize) => write!(out, "size_t"),
621 Some(I8) => write!(out, "int8_t"),
622 Some(I16) => write!(out, "int16_t"),
623 Some(I32) => write!(out, "int32_t"),
624 Some(I64) => write!(out, "int64_t"),
625 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700626 Some(F32) => write!(out, "float"),
627 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800628 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800629 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400630 None => write!(out, "{}", ident),
631 },
632 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800633 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400634 write_type(out, &ty.inner);
635 write!(out, ">");
636 }
637 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800638 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400639 write_type(out, &ptr.inner);
640 write!(out, ">");
641 }
642 Type::Ref(r) => {
643 if r.mutability.is_none() {
644 write!(out, "const ");
645 }
646 write_type(out, &r.inner);
647 write!(out, " &");
648 }
649 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800650 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400651 }
David Tolnay417305a2020-03-18 13:54:00 -0700652 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700653 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400654 }
655}
656
657fn write_type_space(out: &mut OutFile, ty: &Type) {
658 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700659 write_space_after_type(out, ty);
660}
661
662fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400663 match ty {
664 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
665 Type::Ref(_) => {}
David Tolnay417305a2020-03-18 13:54:00 -0700666 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700667 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400668 }
669}
670
671fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
672 fn allow_unique_ptr(ident: &Ident) -> bool {
673 Atom::from(ident).is_none()
674 }
675
676 out.begin_block("extern \"C\"");
677 for ty in types {
678 if let Type::RustBox(ty) = ty {
679 if let Type::Ident(inner) = &ty.inner {
680 out.next_section();
681 write_rust_box_extern(out, inner);
682 }
683 } else if let Type::UniquePtr(ptr) = ty {
684 if let Type::Ident(inner) = &ptr.inner {
685 if allow_unique_ptr(inner) {
686 out.next_section();
687 write_unique_ptr(out, inner);
688 }
689 }
690 }
691 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800692 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400693
David Tolnay750755e2020-03-01 13:04:08 -0800694 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700695 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400696 for ty in types {
697 if let Type::RustBox(ty) = ty {
698 if let Type::Ident(inner) = &ty.inner {
699 write_rust_box_impl(out, inner);
700 }
701 }
702 }
David Tolnay8c730492020-03-13 01:29:06 -0700703 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800704 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400705}
706
707fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
708 let mut inner = String::new();
709 for name in &out.namespace {
710 inner += name;
711 inner += "::";
712 }
713 inner += &ident.to_string();
714 let instance = inner.replace("::", "$");
715
David Tolnay8c730492020-03-13 01:29:06 -0700716 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
717 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400718 writeln!(
719 out,
David Tolnay8c730492020-03-13 01:29:06 -0700720 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400721 instance, inner,
722 );
723 writeln!(
724 out,
David Tolnay8c730492020-03-13 01:29:06 -0700725 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400726 instance, inner,
727 );
David Tolnay8c730492020-03-13 01:29:06 -0700728 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400729}
730
731fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
732 let mut inner = String::new();
733 for name in &out.namespace {
734 inner += name;
735 inner += "::";
736 }
737 inner += &ident.to_string();
738 let instance = inner.replace("::", "$");
739
740 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800741 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700742 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400743 writeln!(out, "}}");
744
745 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800746 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700747 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400748 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400749}
750
751fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700752 out.include.utility = true;
753
David Tolnay7db73692019-10-20 14:51:12 -0400754 let mut inner = String::new();
755 for name in &out.namespace {
756 inner += name;
757 inner += "::";
758 }
759 inner += &ident.to_string();
760 let instance = inner.replace("::", "$");
761
David Tolnay8c730492020-03-13 01:29:06 -0700762 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
763 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400764 writeln!(
765 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800766 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400767 inner,
768 );
769 writeln!(
770 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800771 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400772 inner,
773 );
774 writeln!(
775 out,
David Tolnay8c730492020-03-13 01:29:06 -0700776 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400777 instance, inner,
778 );
David Tolnay7e219b82020-03-01 13:14:51 -0800779 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400780 writeln!(out, "}}");
781 writeln!(
782 out,
David Tolnay8c730492020-03-13 01:29:06 -0700783 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400784 instance, inner, inner,
785 );
786 writeln!(
787 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800788 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400789 inner, inner,
790 );
791 writeln!(out, "}}");
792 writeln!(
793 out,
David Tolnay8c730492020-03-13 01:29:06 -0700794 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400795 instance, inner, inner,
796 );
David Tolnay7e219b82020-03-01 13:14:51 -0800797 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400798 writeln!(out, "}}");
799 writeln!(
800 out,
David Tolnay8c730492020-03-13 01:29:06 -0700801 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400802 inner, instance, inner,
803 );
804 writeln!(out, " return ptr.get();");
805 writeln!(out, "}}");
806 writeln!(
807 out,
David Tolnay8c730492020-03-13 01:29:06 -0700808 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400809 inner, instance, inner,
810 );
811 writeln!(out, " return ptr.release();");
812 writeln!(out, "}}");
813 writeln!(
814 out,
David Tolnay8c730492020-03-13 01:29:06 -0700815 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400816 instance, inner,
817 );
818 writeln!(out, " ptr->~unique_ptr();");
819 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700820 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400821}