blob: a75d4c0e862289d735826a513a7963400a6bca1c [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::include;
2use crate::gen::out::OutFile;
3use crate::syntax::atom::Atom::{self, *};
4use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
5use proc_macro2::Ident;
6
7pub(super) fn gen(namespace: Vec<String>, apis: &[Api], types: &Types, header: bool) -> OutFile {
8 let mut out_file = OutFile::new(namespace.clone(), header);
9 let out = &mut out_file;
10
11 if header {
12 writeln!(out, "#pragma once");
13 }
14
15 for api in apis {
16 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080017 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040018 }
19 }
20
21 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080022 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay7db73692019-10-20 14:51:12 -040024 out.next_section();
25 for name in &namespace {
26 writeln!(out, "namespace {} {{", name);
27 }
28
David Tolnay7db73692019-10-20 14:51:12 -040029 out.next_section();
30 for api in apis {
31 match api {
32 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080033 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
34 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040035 _ => {}
36 }
37 }
38
39 for api in apis {
40 if let Api::Struct(strct) = api {
41 out.next_section();
42 write_struct(out, strct);
43 }
44 }
45
46 if !header {
47 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070048 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040049 for api in apis {
50 let (efn, write): (_, fn(_, _, _)) = match api {
51 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
52 Api::RustFunction(efn) => (efn, write_rust_function_decl),
53 _ => continue,
54 };
55 out.next_section();
56 write(out, efn, types);
57 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080058 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040059 }
60
61 for api in apis {
62 if let Api::RustFunction(efn) = api {
63 out.next_section();
64 write_rust_function_shim(out, efn, types);
65 }
66 }
67
68 out.next_section();
69 for name in namespace.iter().rev() {
70 writeln!(out, "}} // namespace {}", name);
71 }
72
73 if !header {
74 out.next_section();
75 write_generic_instantiations(out, types);
76 }
77
David Tolnay9c68b1a2020-03-06 11:12:55 -080078 out.prepend(out.include.to_string());
79
David Tolnay7db73692019-10-20 14:51:12 -040080 out_file
81}
82
83fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040084 for ty in types {
85 match ty {
86 Type::Ident(ident) => match Atom::from(ident) {
87 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay9c68b1a2020-03-06 11:12:55 -080088 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => out.include.cstdint = true,
89 Some(CxxString) => out.include.string = true,
David Tolnay3383ae72020-03-13 01:12:26 -070090 Some(Bool) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -040091 },
David Tolnay9c68b1a2020-03-06 11:12:55 -080092 Type::RustBox(_) => out.include.type_traits = true,
93 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -040094 _ => {}
95 }
96 }
David Tolnay7db73692019-10-20 14:51:12 -040097}
98
David Tolnayf51447e2020-03-06 14:14:27 -080099fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700100 let mut needs_rust_string = false;
101 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400102 let mut needs_rust_box = false;
103 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700104 match ty {
105 Type::RustBox(_) => {
106 out.include.type_traits = true;
107 needs_rust_box = true;
108 }
109 Type::Str(_) => {
110 out.include.cstdint = true;
111 out.include.string = true;
112 needs_rust_str = true;
113 }
114 ty if ty == RustString => {
115 out.include.array = true;
116 out.include.cstdint = true;
117 out.include.string = true;
118 needs_rust_string = true;
119 }
120 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400121 }
122 }
123
David Tolnayb7a7cb62020-03-17 21:18:40 -0700124 let mut needs_rust_error = false;
125 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800126 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800127 let mut needs_maybe_uninit = false;
128 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700129 match api {
130 Api::CxxFunction(efn) if !out.header => {
131 for arg in &efn.args {
132 if arg.ty == RustString {
133 needs_unsafe_bitcopy = true;
134 break;
135 }
David Tolnay09011c32020-03-06 14:40:28 -0800136 }
137 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700138 Api::RustFunction(efn) if !out.header => {
139 if efn.throws {
140 out.include.exception = true;
141 needs_rust_error = true;
142 }
143 for arg in &efn.args {
144 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
145 needs_manually_drop = true;
146 break;
147 }
148 }
149 if let Some(ret) = &efn.ret {
150 if types.needs_indirect_abi(ret) {
151 needs_maybe_uninit = true;
152 }
David Tolnayf51447e2020-03-06 14:14:27 -0800153 }
154 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700155 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800156 }
157 }
158
David Tolnay750755e2020-03-01 13:04:08 -0800159 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700160 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800161
David Tolnayb7a7cb62020-03-17 21:18:40 -0700162 if needs_rust_string
163 || needs_rust_str
164 || needs_rust_box
165 || needs_rust_error
166 || needs_unsafe_bitcopy
167 || needs_manually_drop
168 || needs_maybe_uninit
169 {
David Tolnay736cbca2020-03-11 16:49:18 -0700170 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800171 }
172
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
174 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
175 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
176 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
177 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800178
179 if needs_manually_drop {
180 out.next_section();
181 writeln!(out, "template <typename T>");
182 writeln!(out, "union ManuallyDrop {{");
183 writeln!(out, " T value;");
184 writeln!(
185 out,
186 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
187 );
188 writeln!(out, " ~ManuallyDrop() {{}}");
189 writeln!(out, "}};");
190 }
191
David Tolnay09011c32020-03-06 14:40:28 -0800192 if needs_maybe_uninit {
193 out.next_section();
194 writeln!(out, "template <typename T>");
195 writeln!(out, "union MaybeUninit {{");
196 writeln!(out, " T value;");
197 writeln!(out, " MaybeUninit() {{}}");
198 writeln!(out, " ~MaybeUninit() {{}}");
199 writeln!(out, "}};");
200 }
201
David Tolnay8c730492020-03-13 01:29:06 -0700202 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800203 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400204}
205
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
207 if needed {
208 out.next_section();
209 for line in include::get(section).lines() {
210 if !line.trim_start().starts_with("//") {
211 writeln!(out, "{}", line);
212 }
213 }
214 }
215}
216
David Tolnay7db73692019-10-20 14:51:12 -0400217fn write_struct(out: &mut OutFile, strct: &Struct) {
218 for line in strct.doc.to_string().lines() {
219 writeln!(out, "//{}", line);
220 }
221 writeln!(out, "struct {} final {{", strct.ident);
222 for field in &strct.fields {
223 write!(out, " ");
224 write_type_space(out, &field.ty);
225 writeln!(out, "{};", field.ident);
226 }
227 writeln!(out, "}};");
228}
229
230fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
231 writeln!(out, "struct {};", ident);
232}
233
David Tolnay8861bee2020-01-20 18:39:24 -0800234fn write_struct_using(out: &mut OutFile, ident: &Ident) {
235 writeln!(out, "using {} = {};", ident, ident);
236}
237
David Tolnayebef4a22020-03-17 15:33:47 -0700238fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
239 let mut has_cxx_throws = false;
240 for api in apis {
241 if let Api::CxxFunction(efn) = api {
242 if efn.throws {
243 has_cxx_throws = true;
244 break;
245 }
246 }
247 }
248
249 if has_cxx_throws {
250 out.next_section();
251 write!(
252 out,
253 "const char *cxxbridge02$exception(const char *, size_t);",
254 );
255 }
256}
257
David Tolnay7db73692019-10-20 14:51:12 -0400258fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700259 if efn.throws {
260 write!(out, "::rust::Str::Repr ");
261 } else {
262 write_extern_return_type(out, &efn.ret, types);
263 }
David Tolnay7db73692019-10-20 14:51:12 -0400264 for name in out.namespace.clone() {
265 write!(out, "{}$", name);
266 }
David Tolnay8c730492020-03-13 01:29:06 -0700267 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400268 for (i, arg) in efn.args.iter().enumerate() {
269 if i > 0 {
270 write!(out, ", ");
271 }
David Tolnaya46a2372020-03-06 10:03:48 -0800272 if arg.ty == RustString {
273 write!(out, "const ");
274 }
David Tolnay7db73692019-10-20 14:51:12 -0400275 write_extern_arg(out, arg, types);
276 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700277 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400278 if indirect_return {
279 if !efn.args.is_empty() {
280 write!(out, ", ");
281 }
282 write_return_type(out, &efn.ret);
283 write!(out, "*return$");
284 }
285 writeln!(out, ") noexcept {{");
286 write!(out, " ");
287 write_return_type(out, &efn.ret);
288 write!(out, "(*{}$)(", efn.ident);
289 for (i, arg) in efn.args.iter().enumerate() {
290 if i > 0 {
291 write!(out, ", ");
292 }
293 write_type(out, &arg.ty);
294 }
295 writeln!(out, ") = {};", efn.ident);
296 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700297 if efn.throws {
298 writeln!(out, "::rust::Str::Repr throw$;");
299 writeln!(out, " try {{");
300 write!(out, " ");
301 }
David Tolnay7db73692019-10-20 14:51:12 -0400302 if indirect_return {
303 write!(out, "new (return$) ");
304 write_type(out, efn.ret.as_ref().unwrap());
305 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800306 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400307 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800308 match ret {
309 Type::Ref(_) => write!(out, "&"),
310 Type::Str(_) => write!(out, "::rust::Str::Repr("),
311 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800312 }
David Tolnay7db73692019-10-20 14:51:12 -0400313 }
314 write!(out, "{}$(", efn.ident);
315 for (i, arg) in efn.args.iter().enumerate() {
316 if i > 0 {
317 write!(out, ", ");
318 }
319 if let Type::RustBox(_) = &arg.ty {
320 write_type(out, &arg.ty);
321 write!(out, "::from_raw({})", arg.ident);
322 } else if let Type::UniquePtr(_) = &arg.ty {
323 write_type(out, &arg.ty);
324 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800325 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800326 write!(
327 out,
328 "::rust::String(::rust::unsafe_bitcopy, *{})",
329 arg.ident,
330 );
David Tolnay7db73692019-10-20 14:51:12 -0400331 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay7e219b82020-03-01 13:14:51 -0800332 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400333 } else {
334 write!(out, "{}", arg.ident);
335 }
336 }
337 write!(out, ")");
338 match &efn.ret {
339 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
340 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800341 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400342 _ => {}
343 }
344 if indirect_return {
345 write!(out, ")");
346 }
347 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700348 if efn.throws {
349 out.include.cstring = true;
350 writeln!(out, " throw$.ptr = nullptr;");
351 writeln!(out, " }} catch (const ::std::exception &catch$) {{");
352 writeln!(out, " const char *return$ = catch$.what();");
353 writeln!(out, " throw$.len = ::std::strlen(return$);");
354 writeln!(
355 out,
356 " throw$.ptr = cxxbridge02$exception(return$, throw$.len);",
357 );
358 writeln!(out, " }}");
359 writeln!(out, " return throw$;");
360 }
David Tolnay7db73692019-10-20 14:51:12 -0400361 writeln!(out, "}}");
362}
363
364fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700365 if efn.throws {
366 write!(out, "::rust::Str::Repr ");
367 } else {
368 write_extern_return_type(out, &efn.ret, types);
369 }
David Tolnay7db73692019-10-20 14:51:12 -0400370 for name in out.namespace.clone() {
371 write!(out, "{}$", name);
372 }
David Tolnay8c730492020-03-13 01:29:06 -0700373 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400374 for (i, arg) in efn.args.iter().enumerate() {
375 if i > 0 {
376 write!(out, ", ");
377 }
378 write_extern_arg(out, arg, types);
379 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700380 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400381 if !efn.args.is_empty() {
382 write!(out, ", ");
383 }
384 write_return_type(out, &efn.ret);
385 write!(out, "*return$");
386 }
387 writeln!(out, ") noexcept;");
388}
389
390fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400391 for line in efn.doc.to_string().lines() {
392 writeln!(out, "//{}", line);
393 }
394 write_return_type(out, &efn.ret);
395 write!(out, "{}(", efn.ident);
396 for (i, arg) in efn.args.iter().enumerate() {
397 if i > 0 {
398 write!(out, ", ");
399 }
400 write_type_space(out, &arg.ty);
401 write!(out, "{}", arg.ident);
402 }
David Tolnay1e548172020-03-16 13:37:09 -0700403 write!(out, ")");
404 if !efn.throws {
405 write!(out, " noexcept");
406 }
David Tolnay7db73692019-10-20 14:51:12 -0400407 if out.header {
408 writeln!(out, ";");
409 } else {
410 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800411 for arg in &efn.args {
412 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
413 write!(out, " ::rust::ManuallyDrop<");
414 write_type(out, &arg.ty);
415 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
416 }
417 }
David Tolnay7db73692019-10-20 14:51:12 -0400418 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700419 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400420 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800421 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400422 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800423 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400424 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800425 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400426 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800427 match ret {
428 Type::RustBox(_) => {
429 write_type(out, ret);
430 write!(out, "::from_raw(");
431 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800432 Type::UniquePtr(_) => {
433 write_type(out, ret);
434 write!(out, "(");
435 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800436 Type::Ref(_) => write!(out, "*"),
437 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800438 }
David Tolnay7db73692019-10-20 14:51:12 -0400439 }
David Tolnay1e548172020-03-16 13:37:09 -0700440 if efn.throws {
441 write!(out, "::rust::Str::Repr error$ = ");
442 }
David Tolnay7db73692019-10-20 14:51:12 -0400443 for name in out.namespace.clone() {
444 write!(out, "{}$", name);
445 }
David Tolnay8c730492020-03-13 01:29:06 -0700446 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400447 for (i, arg) in efn.args.iter().enumerate() {
448 if i > 0 {
449 write!(out, ", ");
450 }
David Tolnaybaae4432020-03-01 20:20:10 -0800451 match &arg.ty {
452 Type::Str(_) => write!(out, "::rust::Str::Repr("),
453 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
454 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400455 }
456 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800457 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800458 Type::RustBox(_) => write!(out, ".into_raw()"),
459 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800460 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800461 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800462 _ => {}
463 }
David Tolnay7db73692019-10-20 14:51:12 -0400464 }
465 if indirect_return {
466 if !efn.args.is_empty() {
467 write!(out, ", ");
468 }
David Tolnay09011c32020-03-06 14:40:28 -0800469 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400470 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800471 write!(out, ")");
472 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800473 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800474 write!(out, ")");
475 }
476 }
477 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700478 if efn.throws {
479 writeln!(out, " if (error$.ptr) {{");
480 writeln!(out, " throw ::rust::Error(error$);");
481 writeln!(out, " }}");
482 }
David Tolnay7db73692019-10-20 14:51:12 -0400483 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800484 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400485 }
486 writeln!(out, "}}");
487 }
488}
489
490fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
491 match ty {
492 None => write!(out, "void "),
493 Some(ty) => write_type_space(out, ty),
494 }
495}
496
David Tolnay277e3cc2020-03-17 00:11:01 -0700497fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
498 efn.ret
499 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700500 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700501}
502
David Tolnay7db73692019-10-20 14:51:12 -0400503fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
504 match ty {
505 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
506 write_type_space(out, &ty.inner);
507 write!(out, "*");
508 }
David Tolnay4a441222020-01-25 16:24:27 -0800509 Some(Type::Ref(ty)) => {
510 if ty.mutability.is_none() {
511 write!(out, "const ");
512 }
513 write_type(out, &ty.inner);
514 write!(out, " *");
515 }
David Tolnay750755e2020-03-01 13:04:08 -0800516 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400517 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
518 _ => write_return_type(out, ty),
519 }
520}
521
522fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
523 match &arg.ty {
524 Type::RustBox(ty) | Type::UniquePtr(ty) => {
525 write_type_space(out, &ty.inner);
526 write!(out, "*");
527 }
David Tolnay750755e2020-03-01 13:04:08 -0800528 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400529 _ => write_type_space(out, &arg.ty),
530 }
531 if types.needs_indirect_abi(&arg.ty) {
532 write!(out, "*");
533 }
534 write!(out, "{}", arg.ident);
535}
536
537fn write_type(out: &mut OutFile, ty: &Type) {
538 match ty {
539 Type::Ident(ident) => match Atom::from(ident) {
540 Some(Bool) => write!(out, "bool"),
541 Some(U8) => write!(out, "uint8_t"),
542 Some(U16) => write!(out, "uint16_t"),
543 Some(U32) => write!(out, "uint32_t"),
544 Some(U64) => write!(out, "uint64_t"),
545 Some(Usize) => write!(out, "size_t"),
546 Some(I8) => write!(out, "int8_t"),
547 Some(I16) => write!(out, "int16_t"),
548 Some(I32) => write!(out, "int32_t"),
549 Some(I64) => write!(out, "int64_t"),
550 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700551 Some(F32) => write!(out, "float"),
552 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800553 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800554 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400555 None => write!(out, "{}", ident),
556 },
557 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800558 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400559 write_type(out, &ty.inner);
560 write!(out, ">");
561 }
562 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800563 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400564 write_type(out, &ptr.inner);
565 write!(out, ">");
566 }
567 Type::Ref(r) => {
568 if r.mutability.is_none() {
569 write!(out, "const ");
570 }
571 write_type(out, &r.inner);
572 write!(out, " &");
573 }
574 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800575 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400576 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700577 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400578 }
579}
580
581fn write_type_space(out: &mut OutFile, ty: &Type) {
582 write_type(out, ty);
583 match ty {
584 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
585 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700586 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400587 }
588}
589
590fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
591 fn allow_unique_ptr(ident: &Ident) -> bool {
592 Atom::from(ident).is_none()
593 }
594
595 out.begin_block("extern \"C\"");
596 for ty in types {
597 if let Type::RustBox(ty) = ty {
598 if let Type::Ident(inner) = &ty.inner {
599 out.next_section();
600 write_rust_box_extern(out, inner);
601 }
602 } else if let Type::UniquePtr(ptr) = ty {
603 if let Type::Ident(inner) = &ptr.inner {
604 if allow_unique_ptr(inner) {
605 out.next_section();
606 write_unique_ptr(out, inner);
607 }
608 }
609 }
610 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800611 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400612
David Tolnay750755e2020-03-01 13:04:08 -0800613 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700614 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400615 for ty in types {
616 if let Type::RustBox(ty) = ty {
617 if let Type::Ident(inner) = &ty.inner {
618 write_rust_box_impl(out, inner);
619 }
620 }
621 }
David Tolnay8c730492020-03-13 01:29:06 -0700622 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800623 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400624}
625
626fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
627 let mut inner = String::new();
628 for name in &out.namespace {
629 inner += name;
630 inner += "::";
631 }
632 inner += &ident.to_string();
633 let instance = inner.replace("::", "$");
634
David Tolnay8c730492020-03-13 01:29:06 -0700635 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
636 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400637 writeln!(
638 out,
David Tolnay8c730492020-03-13 01:29:06 -0700639 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400640 instance, inner,
641 );
642 writeln!(
643 out,
David Tolnay8c730492020-03-13 01:29:06 -0700644 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400645 instance, inner,
646 );
David Tolnay8c730492020-03-13 01:29:06 -0700647 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400648}
649
650fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
651 let mut inner = String::new();
652 for name in &out.namespace {
653 inner += name;
654 inner += "::";
655 }
656 inner += &ident.to_string();
657 let instance = inner.replace("::", "$");
658
659 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800660 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700661 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400662 writeln!(out, "}}");
663
664 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800665 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700666 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400667 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400668}
669
670fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
671 let mut inner = String::new();
672 for name in &out.namespace {
673 inner += name;
674 inner += "::";
675 }
676 inner += &ident.to_string();
677 let instance = inner.replace("::", "$");
678
David Tolnay8c730492020-03-13 01:29:06 -0700679 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
680 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400681 writeln!(
682 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800683 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400684 inner,
685 );
686 writeln!(
687 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800688 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400689 inner,
690 );
691 writeln!(
692 out,
David Tolnay8c730492020-03-13 01:29:06 -0700693 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400694 instance, inner,
695 );
David Tolnay7e219b82020-03-01 13:14:51 -0800696 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400697 writeln!(out, "}}");
698 writeln!(
699 out,
David Tolnay8c730492020-03-13 01:29:06 -0700700 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400701 instance, inner, inner,
702 );
703 writeln!(
704 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800705 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400706 inner, inner,
707 );
708 writeln!(out, "}}");
709 writeln!(
710 out,
David Tolnay8c730492020-03-13 01:29:06 -0700711 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400712 instance, inner, inner,
713 );
David Tolnay7e219b82020-03-01 13:14:51 -0800714 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400715 writeln!(out, "}}");
716 writeln!(
717 out,
David Tolnay8c730492020-03-13 01:29:06 -0700718 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400719 inner, instance, inner,
720 );
721 writeln!(out, " return ptr.get();");
722 writeln!(out, "}}");
723 writeln!(
724 out,
David Tolnay8c730492020-03-13 01:29:06 -0700725 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400726 inner, instance, inner,
727 );
728 writeln!(out, " return ptr.release();");
729 writeln!(out, "}}");
730 writeln!(
731 out,
David Tolnay8c730492020-03-13 01:29:06 -0700732 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400733 instance, inner,
734 );
735 writeln!(out, " ptr->~unique_ptr();");
736 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700737 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400738}