blob: f0c90774fad8e89ad8d7a7e0521001bc6439d1fa [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07002use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04003use crate::syntax::atom::Atom::{self, *};
4use crate::syntax::{Api, ExternFn, Struct, Type, Types, Var};
5use proc_macro2::Ident;
6
David Tolnay33d30292020-03-18 18:02:02 -07007pub(super) fn gen(
8 namespace: Vec<String>,
9 apis: &[Api],
10 types: &Types,
11 opt: Opt,
12 header: bool,
13) -> OutFile {
David Tolnay7db73692019-10-20 14:51:12 -040014 let mut out_file = OutFile::new(namespace.clone(), header);
15 let out = &mut out_file;
16
17 if header {
18 writeln!(out, "#pragma once");
19 }
20
David Tolnay33d30292020-03-18 18:02:02 -070021 out.include.extend(opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040022 for api in apis {
23 if let Api::Include(include) = api {
David Tolnay9c68b1a2020-03-06 11:12:55 -080024 out.include.insert(include.value());
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
28 write_includes(out, types);
David Tolnayf51447e2020-03-06 14:14:27 -080029 write_include_cxxbridge(out, apis, types);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
32 for name in &namespace {
33 writeln!(out, "namespace {} {{", name);
34 }
35
David Tolnay7db73692019-10-20 14:51:12 -040036 out.next_section();
37 for api in apis {
38 match api {
39 Api::Struct(strct) => write_struct_decl(out, &strct.ident),
David Tolnay8861bee2020-01-20 18:39:24 -080040 Api::CxxType(ety) => write_struct_using(out, &ety.ident),
41 Api::RustType(ety) => write_struct_decl(out, &ety.ident),
David Tolnay7db73692019-10-20 14:51:12 -040042 _ => {}
43 }
44 }
45
46 for api in apis {
47 if let Api::Struct(strct) = api {
48 out.next_section();
49 write_struct(out, strct);
50 }
51 }
52
53 if !header {
54 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -070055 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040056 for api in apis {
57 let (efn, write): (_, fn(_, _, _)) = match api {
58 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
59 Api::RustFunction(efn) => (efn, write_rust_function_decl),
60 _ => continue,
61 };
62 out.next_section();
63 write(out, efn, types);
64 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -080065 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -040066 }
67
68 for api in apis {
69 if let Api::RustFunction(efn) = api {
70 out.next_section();
71 write_rust_function_shim(out, efn, types);
72 }
73 }
74
75 out.next_section();
76 for name in namespace.iter().rev() {
77 writeln!(out, "}} // namespace {}", name);
78 }
79
80 if !header {
81 out.next_section();
82 write_generic_instantiations(out, types);
83 }
84
David Tolnay9c68b1a2020-03-06 11:12:55 -080085 out.prepend(out.include.to_string());
86
David Tolnay7db73692019-10-20 14:51:12 -040087 out_file
88}
89
90fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -040091 for ty in types {
92 match ty {
93 Type::Ident(ident) => match Atom::from(ident) {
94 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay9c68b1a2020-03-06 11:12:55 -080095 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) => out.include.cstdint = true,
96 Some(CxxString) => out.include.string = true,
David Tolnay3383ae72020-03-13 01:12:26 -070097 Some(Bool) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -040098 },
David Tolnay9c68b1a2020-03-06 11:12:55 -080099 Type::RustBox(_) => out.include.type_traits = true,
100 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -0400101 _ => {}
102 }
103 }
David Tolnay7db73692019-10-20 14:51:12 -0400104}
105
David Tolnayf51447e2020-03-06 14:14:27 -0800106fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700107 let mut needs_rust_string = false;
108 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400109 let mut needs_rust_box = false;
110 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700111 match ty {
112 Type::RustBox(_) => {
113 out.include.type_traits = true;
114 needs_rust_box = true;
115 }
116 Type::Str(_) => {
117 out.include.cstdint = true;
118 out.include.string = true;
119 needs_rust_str = true;
120 }
121 ty if ty == RustString => {
122 out.include.array = true;
123 out.include.cstdint = true;
124 out.include.string = true;
125 needs_rust_string = true;
126 }
127 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400128 }
129 }
130
David Tolnayb7a7cb62020-03-17 21:18:40 -0700131 let mut needs_rust_error = false;
132 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800133 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800134 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700135 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800136 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700137 match api {
138 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700139 if efn.throws {
140 needs_trycatch = true;
141 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700142 for arg in &efn.args {
143 if arg.ty == RustString {
144 needs_unsafe_bitcopy = true;
145 break;
146 }
David Tolnay09011c32020-03-06 14:40:28 -0800147 }
148 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700149 Api::RustFunction(efn) if !out.header => {
150 if efn.throws {
151 out.include.exception = true;
152 needs_rust_error = true;
153 }
154 for arg in &efn.args {
155 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
156 needs_manually_drop = true;
157 break;
158 }
159 }
160 if let Some(ret) = &efn.ret {
161 if types.needs_indirect_abi(ret) {
162 needs_maybe_uninit = true;
163 }
David Tolnayf51447e2020-03-06 14:14:27 -0800164 }
165 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800167 }
168 }
169
David Tolnay750755e2020-03-01 13:04:08 -0800170 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700171 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800172
David Tolnayb7a7cb62020-03-17 21:18:40 -0700173 if needs_rust_string
174 || needs_rust_str
175 || needs_rust_box
176 || needs_rust_error
177 || needs_unsafe_bitcopy
178 || needs_manually_drop
179 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700180 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700181 {
David Tolnay736cbca2020-03-11 16:49:18 -0700182 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800183 }
184
David Tolnayb7a7cb62020-03-17 21:18:40 -0700185 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
186 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
187 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
188 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
189 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800190
191 if needs_manually_drop {
192 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700193 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800194 writeln!(out, "template <typename T>");
195 writeln!(out, "union ManuallyDrop {{");
196 writeln!(out, " T value;");
197 writeln!(
198 out,
199 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
200 );
201 writeln!(out, " ~ManuallyDrop() {{}}");
202 writeln!(out, "}};");
203 }
204
David Tolnay09011c32020-03-06 14:40:28 -0800205 if needs_maybe_uninit {
206 out.next_section();
207 writeln!(out, "template <typename T>");
208 writeln!(out, "union MaybeUninit {{");
209 writeln!(out, " T value;");
210 writeln!(out, " MaybeUninit() {{}}");
211 writeln!(out, " ~MaybeUninit() {{}}");
212 writeln!(out, "}};");
213 }
214
David Tolnay3e3e0af2020-03-17 22:42:49 -0700215 out.end_block("namespace cxxbridge02");
216
David Tolnay5d121442020-03-17 22:14:40 -0700217 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700218 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700219 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700220 out.include.type_traits = true;
221 out.include.utility = true;
222 writeln!(out, "class missing {{}};");
223 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700224 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700225 writeln!(out, "template <typename Try, typename Fail>");
226 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700227 writeln!(
228 out,
David Tolnay04722332020-03-18 11:31:54 -0700229 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700230 );
David Tolnay04722332020-03-18 11:31:54 -0700231 writeln!(out, " missing>::value>::type");
232 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700233 writeln!(out, " func();");
234 writeln!(out, "}} catch (const ::std::exception &e) {{");
235 writeln!(out, " fail(e.what());");
236 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700237 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700238 }
239
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800240 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400241}
242
David Tolnayb7a7cb62020-03-17 21:18:40 -0700243fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
244 if needed {
245 out.next_section();
246 for line in include::get(section).lines() {
247 if !line.trim_start().starts_with("//") {
248 writeln!(out, "{}", line);
249 }
250 }
251 }
252}
253
David Tolnay7db73692019-10-20 14:51:12 -0400254fn write_struct(out: &mut OutFile, strct: &Struct) {
255 for line in strct.doc.to_string().lines() {
256 writeln!(out, "//{}", line);
257 }
258 writeln!(out, "struct {} final {{", strct.ident);
259 for field in &strct.fields {
260 write!(out, " ");
261 write_type_space(out, &field.ty);
262 writeln!(out, "{};", field.ident);
263 }
264 writeln!(out, "}};");
265}
266
267fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
268 writeln!(out, "struct {};", ident);
269}
270
David Tolnay8861bee2020-01-20 18:39:24 -0800271fn write_struct_using(out: &mut OutFile, ident: &Ident) {
272 writeln!(out, "using {} = {};", ident, ident);
273}
274
David Tolnayebef4a22020-03-17 15:33:47 -0700275fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
276 let mut has_cxx_throws = false;
277 for api in apis {
278 if let Api::CxxFunction(efn) = api {
279 if efn.throws {
280 has_cxx_throws = true;
281 break;
282 }
283 }
284 }
285
286 if has_cxx_throws {
287 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700288 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700289 out,
290 "const char *cxxbridge02$exception(const char *, size_t);",
291 );
292 }
293}
294
David Tolnay7db73692019-10-20 14:51:12 -0400295fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700296 if efn.throws {
297 write!(out, "::rust::Str::Repr ");
298 } else {
299 write_extern_return_type(out, &efn.ret, types);
300 }
David Tolnay7db73692019-10-20 14:51:12 -0400301 for name in out.namespace.clone() {
302 write!(out, "{}$", name);
303 }
David Tolnay8c730492020-03-13 01:29:06 -0700304 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400305 for (i, arg) in efn.args.iter().enumerate() {
306 if i > 0 {
307 write!(out, ", ");
308 }
David Tolnaya46a2372020-03-06 10:03:48 -0800309 if arg.ty == RustString {
310 write!(out, "const ");
311 }
David Tolnay7db73692019-10-20 14:51:12 -0400312 write_extern_arg(out, arg, types);
313 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700314 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400315 if indirect_return {
316 if !efn.args.is_empty() {
317 write!(out, ", ");
318 }
319 write_return_type(out, &efn.ret);
320 write!(out, "*return$");
321 }
322 writeln!(out, ") noexcept {{");
323 write!(out, " ");
324 write_return_type(out, &efn.ret);
325 write!(out, "(*{}$)(", efn.ident);
326 for (i, arg) in efn.args.iter().enumerate() {
327 if i > 0 {
328 write!(out, ", ");
329 }
330 write_type(out, &arg.ty);
331 }
332 writeln!(out, ") = {};", efn.ident);
333 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700334 if efn.throws {
335 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700336 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700337 writeln!(out, " [&] {{");
338 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700339 }
David Tolnay7db73692019-10-20 14:51:12 -0400340 if indirect_return {
341 write!(out, "new (return$) ");
342 write_type(out, efn.ret.as_ref().unwrap());
343 write!(out, "(");
David Tolnay4a441222020-01-25 16:24:27 -0800344 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400345 write!(out, "return ");
David Tolnaybaae4432020-03-01 20:20:10 -0800346 match ret {
347 Type::Ref(_) => write!(out, "&"),
348 Type::Str(_) => write!(out, "::rust::Str::Repr("),
349 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800350 }
David Tolnay7db73692019-10-20 14:51:12 -0400351 }
352 write!(out, "{}$(", efn.ident);
353 for (i, arg) in efn.args.iter().enumerate() {
354 if i > 0 {
355 write!(out, ", ");
356 }
357 if let Type::RustBox(_) = &arg.ty {
358 write_type(out, &arg.ty);
359 write!(out, "::from_raw({})", arg.ident);
360 } else if let Type::UniquePtr(_) = &arg.ty {
361 write_type(out, &arg.ty);
362 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800363 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800364 write!(
365 out,
366 "::rust::String(::rust::unsafe_bitcopy, *{})",
367 arg.ident,
368 );
David Tolnay7db73692019-10-20 14:51:12 -0400369 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700370 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800371 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400372 } else {
373 write!(out, "{}", arg.ident);
374 }
375 }
376 write!(out, ")");
377 match &efn.ret {
378 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
379 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800380 Some(Type::Str(_)) => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400381 _ => {}
382 }
383 if indirect_return {
384 write!(out, ")");
385 }
386 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700387 if efn.throws {
388 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700389 writeln!(out, " throw$.ptr = nullptr;");
390 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700391 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700392 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700393 writeln!(
394 out,
David Tolnay5d121442020-03-17 22:14:40 -0700395 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700396 );
David Tolnay5d121442020-03-17 22:14:40 -0700397 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700398 writeln!(out, " return throw$;");
399 }
David Tolnay7db73692019-10-20 14:51:12 -0400400 writeln!(out, "}}");
401}
402
403fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700404 if efn.throws {
405 write!(out, "::rust::Str::Repr ");
406 } else {
407 write_extern_return_type(out, &efn.ret, types);
408 }
David Tolnay7db73692019-10-20 14:51:12 -0400409 for name in out.namespace.clone() {
410 write!(out, "{}$", name);
411 }
David Tolnay8c730492020-03-13 01:29:06 -0700412 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400413 for (i, arg) in efn.args.iter().enumerate() {
414 if i > 0 {
415 write!(out, ", ");
416 }
417 write_extern_arg(out, arg, types);
418 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700419 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400420 if !efn.args.is_empty() {
421 write!(out, ", ");
422 }
423 write_return_type(out, &efn.ret);
424 write!(out, "*return$");
425 }
426 writeln!(out, ") noexcept;");
427}
428
429fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400430 for line in efn.doc.to_string().lines() {
431 writeln!(out, "//{}", line);
432 }
433 write_return_type(out, &efn.ret);
434 write!(out, "{}(", efn.ident);
435 for (i, arg) in efn.args.iter().enumerate() {
436 if i > 0 {
437 write!(out, ", ");
438 }
439 write_type_space(out, &arg.ty);
440 write!(out, "{}", arg.ident);
441 }
David Tolnay1e548172020-03-16 13:37:09 -0700442 write!(out, ")");
443 if !efn.throws {
444 write!(out, " noexcept");
445 }
David Tolnay7db73692019-10-20 14:51:12 -0400446 if out.header {
447 writeln!(out, ";");
448 } else {
449 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800450 for arg in &efn.args {
451 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700452 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800453 write!(out, " ::rust::ManuallyDrop<");
454 write_type(out, &arg.ty);
455 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
456 }
457 }
David Tolnay7db73692019-10-20 14:51:12 -0400458 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700459 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400460 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800461 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400462 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800463 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400464 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800465 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400466 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800467 match ret {
468 Type::RustBox(_) => {
469 write_type(out, ret);
470 write!(out, "::from_raw(");
471 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800472 Type::UniquePtr(_) => {
473 write_type(out, ret);
474 write!(out, "(");
475 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800476 Type::Ref(_) => write!(out, "*"),
477 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800478 }
David Tolnay7db73692019-10-20 14:51:12 -0400479 }
David Tolnay1e548172020-03-16 13:37:09 -0700480 if efn.throws {
481 write!(out, "::rust::Str::Repr error$ = ");
482 }
David Tolnay7db73692019-10-20 14:51:12 -0400483 for name in out.namespace.clone() {
484 write!(out, "{}$", name);
485 }
David Tolnay8c730492020-03-13 01:29:06 -0700486 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400487 for (i, arg) in efn.args.iter().enumerate() {
488 if i > 0 {
489 write!(out, ", ");
490 }
David Tolnaybaae4432020-03-01 20:20:10 -0800491 match &arg.ty {
492 Type::Str(_) => write!(out, "::rust::Str::Repr("),
493 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
494 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400495 }
496 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800497 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800498 Type::RustBox(_) => write!(out, ".into_raw()"),
499 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800500 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800501 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800502 _ => {}
503 }
David Tolnay7db73692019-10-20 14:51:12 -0400504 }
505 if indirect_return {
506 if !efn.args.is_empty() {
507 write!(out, ", ");
508 }
David Tolnay09011c32020-03-06 14:40:28 -0800509 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400510 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800511 write!(out, ")");
512 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800513 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800514 write!(out, ")");
515 }
516 }
517 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700518 if efn.throws {
519 writeln!(out, " if (error$.ptr) {{");
520 writeln!(out, " throw ::rust::Error(error$);");
521 writeln!(out, " }}");
522 }
David Tolnay7db73692019-10-20 14:51:12 -0400523 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700524 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800525 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400526 }
527 writeln!(out, "}}");
528 }
529}
530
531fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
532 match ty {
533 None => write!(out, "void "),
534 Some(ty) => write_type_space(out, ty),
535 }
536}
537
David Tolnay277e3cc2020-03-17 00:11:01 -0700538fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
539 efn.ret
540 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700541 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700542}
543
David Tolnay7db73692019-10-20 14:51:12 -0400544fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
545 match ty {
546 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
547 write_type_space(out, &ty.inner);
548 write!(out, "*");
549 }
David Tolnay4a441222020-01-25 16:24:27 -0800550 Some(Type::Ref(ty)) => {
551 if ty.mutability.is_none() {
552 write!(out, "const ");
553 }
554 write_type(out, &ty.inner);
555 write!(out, " *");
556 }
David Tolnay750755e2020-03-01 13:04:08 -0800557 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400558 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
559 _ => write_return_type(out, ty),
560 }
561}
562
563fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
564 match &arg.ty {
565 Type::RustBox(ty) | Type::UniquePtr(ty) => {
566 write_type_space(out, &ty.inner);
567 write!(out, "*");
568 }
David Tolnay750755e2020-03-01 13:04:08 -0800569 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400570 _ => write_type_space(out, &arg.ty),
571 }
572 if types.needs_indirect_abi(&arg.ty) {
573 write!(out, "*");
574 }
575 write!(out, "{}", arg.ident);
576}
577
578fn write_type(out: &mut OutFile, ty: &Type) {
579 match ty {
580 Type::Ident(ident) => match Atom::from(ident) {
581 Some(Bool) => write!(out, "bool"),
582 Some(U8) => write!(out, "uint8_t"),
583 Some(U16) => write!(out, "uint16_t"),
584 Some(U32) => write!(out, "uint32_t"),
585 Some(U64) => write!(out, "uint64_t"),
586 Some(Usize) => write!(out, "size_t"),
587 Some(I8) => write!(out, "int8_t"),
588 Some(I16) => write!(out, "int16_t"),
589 Some(I32) => write!(out, "int32_t"),
590 Some(I64) => write!(out, "int64_t"),
591 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700592 Some(F32) => write!(out, "float"),
593 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800594 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800595 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400596 None => write!(out, "{}", ident),
597 },
598 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800599 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400600 write_type(out, &ty.inner);
601 write!(out, ">");
602 }
603 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800604 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400605 write_type(out, &ptr.inner);
606 write!(out, ">");
607 }
608 Type::Ref(r) => {
609 if r.mutability.is_none() {
610 write!(out, "const ");
611 }
612 write_type(out, &r.inner);
613 write!(out, " &");
614 }
615 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800616 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400617 }
David Tolnay417305a2020-03-18 13:54:00 -0700618 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700619 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400620 }
621}
622
623fn write_type_space(out: &mut OutFile, ty: &Type) {
624 write_type(out, ty);
625 match ty {
626 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
627 Type::Ref(_) => {}
David Tolnay417305a2020-03-18 13:54:00 -0700628 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700629 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400630 }
631}
632
633fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
634 fn allow_unique_ptr(ident: &Ident) -> bool {
635 Atom::from(ident).is_none()
636 }
637
638 out.begin_block("extern \"C\"");
639 for ty in types {
640 if let Type::RustBox(ty) = ty {
641 if let Type::Ident(inner) = &ty.inner {
642 out.next_section();
643 write_rust_box_extern(out, inner);
644 }
645 } else if let Type::UniquePtr(ptr) = ty {
646 if let Type::Ident(inner) = &ptr.inner {
647 if allow_unique_ptr(inner) {
648 out.next_section();
649 write_unique_ptr(out, inner);
650 }
651 }
652 }
653 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800654 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400655
David Tolnay750755e2020-03-01 13:04:08 -0800656 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700657 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400658 for ty in types {
659 if let Type::RustBox(ty) = ty {
660 if let Type::Ident(inner) = &ty.inner {
661 write_rust_box_impl(out, inner);
662 }
663 }
664 }
David Tolnay8c730492020-03-13 01:29:06 -0700665 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800666 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400667}
668
669fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
670 let mut inner = String::new();
671 for name in &out.namespace {
672 inner += name;
673 inner += "::";
674 }
675 inner += &ident.to_string();
676 let instance = inner.replace("::", "$");
677
David Tolnay8c730492020-03-13 01:29:06 -0700678 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
679 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400680 writeln!(
681 out,
David Tolnay8c730492020-03-13 01:29:06 -0700682 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400683 instance, inner,
684 );
685 writeln!(
686 out,
David Tolnay8c730492020-03-13 01:29:06 -0700687 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400688 instance, inner,
689 );
David Tolnay8c730492020-03-13 01:29:06 -0700690 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400691}
692
693fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
694 let mut inner = String::new();
695 for name in &out.namespace {
696 inner += name;
697 inner += "::";
698 }
699 inner += &ident.to_string();
700 let instance = inner.replace("::", "$");
701
702 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800703 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700704 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400705 writeln!(out, "}}");
706
707 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800708 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700709 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400710 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400711}
712
713fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700714 out.include.utility = true;
715
David Tolnay7db73692019-10-20 14:51:12 -0400716 let mut inner = String::new();
717 for name in &out.namespace {
718 inner += name;
719 inner += "::";
720 }
721 inner += &ident.to_string();
722 let instance = inner.replace("::", "$");
723
David Tolnay8c730492020-03-13 01:29:06 -0700724 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
725 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400726 writeln!(
727 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800728 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400729 inner,
730 );
731 writeln!(
732 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800733 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400734 inner,
735 );
736 writeln!(
737 out,
David Tolnay8c730492020-03-13 01:29:06 -0700738 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400739 instance, inner,
740 );
David Tolnay7e219b82020-03-01 13:14:51 -0800741 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400742 writeln!(out, "}}");
743 writeln!(
744 out,
David Tolnay8c730492020-03-13 01:29:06 -0700745 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400746 instance, inner, inner,
747 );
748 writeln!(
749 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800750 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400751 inner, inner,
752 );
753 writeln!(out, "}}");
754 writeln!(
755 out,
David Tolnay8c730492020-03-13 01:29:06 -0700756 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400757 instance, inner, inner,
758 );
David Tolnay7e219b82020-03-01 13:14:51 -0800759 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400760 writeln!(out, "}}");
761 writeln!(
762 out,
David Tolnay8c730492020-03-13 01:29:06 -0700763 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400764 inner, instance, inner,
765 );
766 writeln!(out, " return ptr.get();");
767 writeln!(out, "}}");
768 writeln!(
769 out,
David Tolnay8c730492020-03-13 01:29:06 -0700770 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400771 inner, instance, inner,
772 );
773 writeln!(out, " return ptr.release();");
774 writeln!(out, "}}");
775 writeln!(
776 out,
David Tolnay8c730492020-03-13 01:29:06 -0700777 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400778 instance, inner,
779 );
780 writeln!(out, " ptr->~unique_ptr();");
781 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700782 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400783}