blob: 16322c1c5dec55623de05425b238c587631f77df [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();
David Tolnay4791f1c2020-03-17 21:53:16 -0700181 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800182 writeln!(out, "template <typename T>");
183 writeln!(out, "union ManuallyDrop {{");
184 writeln!(out, " T value;");
185 writeln!(
186 out,
187 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
188 );
189 writeln!(out, " ~ManuallyDrop() {{}}");
190 writeln!(out, "}};");
191 }
192
David Tolnay09011c32020-03-06 14:40:28 -0800193 if needs_maybe_uninit {
194 out.next_section();
195 writeln!(out, "template <typename T>");
196 writeln!(out, "union MaybeUninit {{");
197 writeln!(out, " T value;");
198 writeln!(out, " MaybeUninit() {{}}");
199 writeln!(out, " ~MaybeUninit() {{}}");
200 writeln!(out, "}};");
201 }
202
David Tolnay8c730492020-03-13 01:29:06 -0700203 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800204 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400205}
206
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
208 if needed {
209 out.next_section();
210 for line in include::get(section).lines() {
211 if !line.trim_start().starts_with("//") {
212 writeln!(out, "{}", line);
213 }
214 }
215 }
216}
217
David Tolnay7db73692019-10-20 14:51:12 -0400218fn write_struct(out: &mut OutFile, strct: &Struct) {
219 for line in strct.doc.to_string().lines() {
220 writeln!(out, "//{}", line);
221 }
222 writeln!(out, "struct {} final {{", strct.ident);
223 for field in &strct.fields {
224 write!(out, " ");
225 write_type_space(out, &field.ty);
226 writeln!(out, "{};", field.ident);
227 }
228 writeln!(out, "}};");
229}
230
231fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
232 writeln!(out, "struct {};", ident);
233}
234
David Tolnay8861bee2020-01-20 18:39:24 -0800235fn write_struct_using(out: &mut OutFile, ident: &Ident) {
236 writeln!(out, "using {} = {};", ident, ident);
237}
238
David Tolnayebef4a22020-03-17 15:33:47 -0700239fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
240 let mut has_cxx_throws = false;
241 for api in apis {
242 if let Api::CxxFunction(efn) = api {
243 if efn.throws {
244 has_cxx_throws = true;
245 break;
246 }
247 }
248 }
249
250 if has_cxx_throws {
251 out.next_section();
252 write!(
253 out,
254 "const char *cxxbridge02$exception(const char *, size_t);",
255 );
256 }
257}
258
David Tolnay7db73692019-10-20 14:51:12 -0400259fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700260 if efn.throws {
261 write!(out, "::rust::Str::Repr ");
262 } else {
263 write_extern_return_type(out, &efn.ret, types);
264 }
David Tolnay7db73692019-10-20 14:51:12 -0400265 for name in out.namespace.clone() {
266 write!(out, "{}$", name);
267 }
David Tolnay8c730492020-03-13 01:29:06 -0700268 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400269 for (i, arg) in efn.args.iter().enumerate() {
270 if i > 0 {
271 write!(out, ", ");
272 }
David Tolnaya46a2372020-03-06 10:03:48 -0800273 if arg.ty == RustString {
274 write!(out, "const ");
275 }
David Tolnay7db73692019-10-20 14:51:12 -0400276 write_extern_arg(out, arg, types);
277 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700278 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400279 if indirect_return {
280 if !efn.args.is_empty() {
281 write!(out, ", ");
282 }
283 write_return_type(out, &efn.ret);
284 write!(out, "*return$");
285 }
286 writeln!(out, ") noexcept {{");
287 write!(out, " ");
288 write_return_type(out, &efn.ret);
289 write!(out, "(*{}$)(", efn.ident);
290 for (i, arg) in efn.args.iter().enumerate() {
291 if i > 0 {
292 write!(out, ", ");
293 }
294 write_type(out, &arg.ty);
295 }
296 writeln!(out, ") = {};", efn.ident);
297 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700298 if efn.throws {
299 writeln!(out, "::rust::Str::Repr throw$;");
300 writeln!(out, " try {{");
301 write!(out, " ");
302 }
David Tolnay7db73692019-10-20 14:51:12 -0400303 if indirect_return {
304 write!(out, "new (return$) ");
305 write_type(out, efn.ret.as_ref().unwrap());
306 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800307 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400308 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800309 match ret {
310 Type::Ref(_) => write!(out, "&"),
311 Type::Str(_) => write!(out, "::rust::Str::Repr("),
312 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800313 }
David Tolnay7db73692019-10-20 14:51:12 -0400314 }
315 write!(out, "{}$(", efn.ident);
316 for (i, arg) in efn.args.iter().enumerate() {
317 if i > 0 {
318 write!(out, ", ");
319 }
320 if let Type::RustBox(_) = &arg.ty {
321 write_type(out, &arg.ty);
322 write!(out, "::from_raw({})", arg.ident);
323 } else if let Type::UniquePtr(_) = &arg.ty {
324 write_type(out, &arg.ty);
325 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800326 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800327 write!(
328 out,
329 "::rust::String(::rust::unsafe_bitcopy, *{})",
330 arg.ident,
331 );
David Tolnay7db73692019-10-20 14:51:12 -0400332 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700333 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800334 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400335 } else {
336 write!(out, "{}", arg.ident);
337 }
338 }
339 write!(out, ")");
340 match &efn.ret {
341 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
342 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800343 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400344 _ => {}
345 }
346 if indirect_return {
347 write!(out, ")");
348 }
349 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700350 if efn.throws {
351 out.include.cstring = true;
David Tolnay4791f1c2020-03-17 21:53:16 -0700352 out.include.exception = true;
David Tolnayebef4a22020-03-17 15:33:47 -0700353 writeln!(out, " throw$.ptr = nullptr;");
354 writeln!(out, " }} catch (const ::std::exception &catch$) {{");
355 writeln!(out, " const char *return$ = catch$.what();");
356 writeln!(out, " throw$.len = ::std::strlen(return$);");
357 writeln!(
358 out,
359 " throw$.ptr = cxxbridge02$exception(return$, throw$.len);",
360 );
361 writeln!(out, " }}");
362 writeln!(out, " return throw$;");
363 }
David Tolnay7db73692019-10-20 14:51:12 -0400364 writeln!(out, "}}");
365}
366
367fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700368 if efn.throws {
369 write!(out, "::rust::Str::Repr ");
370 } else {
371 write_extern_return_type(out, &efn.ret, types);
372 }
David Tolnay7db73692019-10-20 14:51:12 -0400373 for name in out.namespace.clone() {
374 write!(out, "{}$", name);
375 }
David Tolnay8c730492020-03-13 01:29:06 -0700376 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400377 for (i, arg) in efn.args.iter().enumerate() {
378 if i > 0 {
379 write!(out, ", ");
380 }
381 write_extern_arg(out, arg, types);
382 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700383 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400384 if !efn.args.is_empty() {
385 write!(out, ", ");
386 }
387 write_return_type(out, &efn.ret);
388 write!(out, "*return$");
389 }
390 writeln!(out, ") noexcept;");
391}
392
393fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400394 for line in efn.doc.to_string().lines() {
395 writeln!(out, "//{}", line);
396 }
397 write_return_type(out, &efn.ret);
398 write!(out, "{}(", efn.ident);
399 for (i, arg) in efn.args.iter().enumerate() {
400 if i > 0 {
401 write!(out, ", ");
402 }
403 write_type_space(out, &arg.ty);
404 write!(out, "{}", arg.ident);
405 }
David Tolnay1e548172020-03-16 13:37:09 -0700406 write!(out, ")");
407 if !efn.throws {
408 write!(out, " noexcept");
409 }
David Tolnay7db73692019-10-20 14:51:12 -0400410 if out.header {
411 writeln!(out, ";");
412 } else {
413 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800414 for arg in &efn.args {
415 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700416 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800417 write!(out, " ::rust::ManuallyDrop<");
418 write_type(out, &arg.ty);
419 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
420 }
421 }
David Tolnay7db73692019-10-20 14:51:12 -0400422 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700423 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400424 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800425 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400426 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800427 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400428 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800429 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400430 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800431 match ret {
432 Type::RustBox(_) => {
433 write_type(out, ret);
434 write!(out, "::from_raw(");
435 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800436 Type::UniquePtr(_) => {
437 write_type(out, ret);
438 write!(out, "(");
439 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800440 Type::Ref(_) => write!(out, "*"),
441 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800442 }
David Tolnay7db73692019-10-20 14:51:12 -0400443 }
David Tolnay1e548172020-03-16 13:37:09 -0700444 if efn.throws {
445 write!(out, "::rust::Str::Repr error$ = ");
446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 for name in out.namespace.clone() {
448 write!(out, "{}$", name);
449 }
David Tolnay8c730492020-03-13 01:29:06 -0700450 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400451 for (i, arg) in efn.args.iter().enumerate() {
452 if i > 0 {
453 write!(out, ", ");
454 }
David Tolnaybaae4432020-03-01 20:20:10 -0800455 match &arg.ty {
456 Type::Str(_) => write!(out, "::rust::Str::Repr("),
457 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
458 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400459 }
460 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800461 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800462 Type::RustBox(_) => write!(out, ".into_raw()"),
463 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800464 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800465 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800466 _ => {}
467 }
David Tolnay7db73692019-10-20 14:51:12 -0400468 }
469 if indirect_return {
470 if !efn.args.is_empty() {
471 write!(out, ", ");
472 }
David Tolnay09011c32020-03-06 14:40:28 -0800473 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400474 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800475 write!(out, ")");
476 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800477 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800478 write!(out, ")");
479 }
480 }
481 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700482 if efn.throws {
483 writeln!(out, " if (error$.ptr) {{");
484 writeln!(out, " throw ::rust::Error(error$);");
485 writeln!(out, " }}");
486 }
David Tolnay7db73692019-10-20 14:51:12 -0400487 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700488 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800489 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400490 }
491 writeln!(out, "}}");
492 }
493}
494
495fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
496 match ty {
497 None => write!(out, "void "),
498 Some(ty) => write_type_space(out, ty),
499 }
500}
501
David Tolnay277e3cc2020-03-17 00:11:01 -0700502fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
503 efn.ret
504 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700505 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700506}
507
David Tolnay7db73692019-10-20 14:51:12 -0400508fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
509 match ty {
510 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
511 write_type_space(out, &ty.inner);
512 write!(out, "*");
513 }
David Tolnay4a441222020-01-25 16:24:27 -0800514 Some(Type::Ref(ty)) => {
515 if ty.mutability.is_none() {
516 write!(out, "const ");
517 }
518 write_type(out, &ty.inner);
519 write!(out, " *");
520 }
David Tolnay750755e2020-03-01 13:04:08 -0800521 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400522 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
523 _ => write_return_type(out, ty),
524 }
525}
526
527fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
528 match &arg.ty {
529 Type::RustBox(ty) | Type::UniquePtr(ty) => {
530 write_type_space(out, &ty.inner);
531 write!(out, "*");
532 }
David Tolnay750755e2020-03-01 13:04:08 -0800533 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400534 _ => write_type_space(out, &arg.ty),
535 }
536 if types.needs_indirect_abi(&arg.ty) {
537 write!(out, "*");
538 }
539 write!(out, "{}", arg.ident);
540}
541
542fn write_type(out: &mut OutFile, ty: &Type) {
543 match ty {
544 Type::Ident(ident) => match Atom::from(ident) {
545 Some(Bool) => write!(out, "bool"),
546 Some(U8) => write!(out, "uint8_t"),
547 Some(U16) => write!(out, "uint16_t"),
548 Some(U32) => write!(out, "uint32_t"),
549 Some(U64) => write!(out, "uint64_t"),
550 Some(Usize) => write!(out, "size_t"),
551 Some(I8) => write!(out, "int8_t"),
552 Some(I16) => write!(out, "int16_t"),
553 Some(I32) => write!(out, "int32_t"),
554 Some(I64) => write!(out, "int64_t"),
555 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700556 Some(F32) => write!(out, "float"),
557 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800558 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800559 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400560 None => write!(out, "{}", ident),
561 },
562 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800563 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400564 write_type(out, &ty.inner);
565 write!(out, ">");
566 }
567 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800568 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400569 write_type(out, &ptr.inner);
570 write!(out, ">");
571 }
572 Type::Ref(r) => {
573 if r.mutability.is_none() {
574 write!(out, "const ");
575 }
576 write_type(out, &r.inner);
577 write!(out, " &");
578 }
579 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800580 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400581 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700582 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400583 }
584}
585
586fn write_type_space(out: &mut OutFile, ty: &Type) {
587 write_type(out, ty);
588 match ty {
589 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
590 Type::Ref(_) => {}
David Tolnay2fb14e92020-03-15 23:11:38 -0700591 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400592 }
593}
594
595fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
596 fn allow_unique_ptr(ident: &Ident) -> bool {
597 Atom::from(ident).is_none()
598 }
599
600 out.begin_block("extern \"C\"");
601 for ty in types {
602 if let Type::RustBox(ty) = ty {
603 if let Type::Ident(inner) = &ty.inner {
604 out.next_section();
605 write_rust_box_extern(out, inner);
606 }
607 } else if let Type::UniquePtr(ptr) = ty {
608 if let Type::Ident(inner) = &ptr.inner {
609 if allow_unique_ptr(inner) {
610 out.next_section();
611 write_unique_ptr(out, inner);
612 }
613 }
614 }
615 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800616 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400617
David Tolnay750755e2020-03-01 13:04:08 -0800618 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700619 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400620 for ty in types {
621 if let Type::RustBox(ty) = ty {
622 if let Type::Ident(inner) = &ty.inner {
623 write_rust_box_impl(out, inner);
624 }
625 }
626 }
David Tolnay8c730492020-03-13 01:29:06 -0700627 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800628 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400629}
630
631fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
632 let mut inner = String::new();
633 for name in &out.namespace {
634 inner += name;
635 inner += "::";
636 }
637 inner += &ident.to_string();
638 let instance = inner.replace("::", "$");
639
David Tolnay8c730492020-03-13 01:29:06 -0700640 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
641 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400642 writeln!(
643 out,
David Tolnay8c730492020-03-13 01:29:06 -0700644 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400645 instance, inner,
646 );
647 writeln!(
648 out,
David Tolnay8c730492020-03-13 01:29:06 -0700649 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400650 instance, inner,
651 );
David Tolnay8c730492020-03-13 01:29:06 -0700652 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400653}
654
655fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
656 let mut inner = String::new();
657 for name in &out.namespace {
658 inner += name;
659 inner += "::";
660 }
661 inner += &ident.to_string();
662 let instance = inner.replace("::", "$");
663
664 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800665 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700666 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400667 writeln!(out, "}}");
668
669 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800670 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700671 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400672 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400673}
674
675fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700676 out.include.utility = true;
677
David Tolnay7db73692019-10-20 14:51:12 -0400678 let mut inner = String::new();
679 for name in &out.namespace {
680 inner += name;
681 inner += "::";
682 }
683 inner += &ident.to_string();
684 let instance = inner.replace("::", "$");
685
David Tolnay8c730492020-03-13 01:29:06 -0700686 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
687 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400688 writeln!(
689 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800690 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400691 inner,
692 );
693 writeln!(
694 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800695 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400696 inner,
697 );
698 writeln!(
699 out,
David Tolnay8c730492020-03-13 01:29:06 -0700700 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400701 instance, inner,
702 );
David Tolnay7e219b82020-03-01 13:14:51 -0800703 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400704 writeln!(out, "}}");
705 writeln!(
706 out,
David Tolnay8c730492020-03-13 01:29:06 -0700707 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400708 instance, inner, inner,
709 );
710 writeln!(
711 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800712 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400713 inner, inner,
714 );
715 writeln!(out, "}}");
716 writeln!(
717 out,
David Tolnay8c730492020-03-13 01:29:06 -0700718 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400719 instance, inner, inner,
720 );
David Tolnay7e219b82020-03-01 13:14:51 -0800721 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400722 writeln!(out, "}}");
723 writeln!(
724 out,
David Tolnay8c730492020-03-13 01:29:06 -0700725 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400726 inner, instance, inner,
727 );
728 writeln!(out, " return ptr.get();");
729 writeln!(out, "}}");
730 writeln!(
731 out,
David Tolnay8c730492020-03-13 01:29:06 -0700732 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400733 inner, instance, inner,
734 );
735 writeln!(out, " return ptr.release();");
736 writeln!(out, "}}");
737 writeln!(
738 out,
David Tolnay8c730492020-03-13 01:29:06 -0700739 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400740 instance, inner,
741 );
742 writeln!(out, " ptr->~unique_ptr();");
743 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700744 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400745}