blob: 72962f2c3fba76ad6e11d9eb860171c47e7b9a19 [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) {
David Tolnay30430f12020-03-19 20:49:00 -070094 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
95 | Some(I64) => out.include.cstdint = true,
96 Some(Usize) => out.include.cstddef = true,
David Tolnay9c68b1a2020-03-06 11:12:55 -080097 Some(CxxString) => out.include.string = true,
David Tolnay30430f12020-03-19 20:49:00 -070098 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay7db73692019-10-20 14:51:12 -040099 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800100 Type::RustBox(_) => out.include.type_traits = true,
101 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay7db73692019-10-20 14:51:12 -0400102 _ => {}
103 }
104 }
David Tolnay7db73692019-10-20 14:51:12 -0400105}
106
David Tolnayf51447e2020-03-06 14:14:27 -0800107fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700108 let mut needs_rust_string = false;
109 let mut needs_rust_str = false;
David Tolnay7db73692019-10-20 14:51:12 -0400110 let mut needs_rust_box = false;
111 for ty in types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700112 match ty {
113 Type::RustBox(_) => {
114 out.include.type_traits = true;
115 needs_rust_box = true;
116 }
117 Type::Str(_) => {
118 out.include.cstdint = true;
119 out.include.string = true;
120 needs_rust_str = true;
121 }
122 ty if ty == RustString => {
123 out.include.array = true;
124 out.include.cstdint = true;
125 out.include.string = true;
126 needs_rust_string = true;
127 }
128 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400129 }
130 }
131
David Tolnayb7a7cb62020-03-17 21:18:40 -0700132 let mut needs_rust_error = false;
133 let mut needs_unsafe_bitcopy = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800134 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800135 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700136 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800137 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700138 match api {
139 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700140 if efn.throws {
141 needs_trycatch = true;
142 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700143 for arg in &efn.args {
144 if arg.ty == RustString {
145 needs_unsafe_bitcopy = true;
146 break;
147 }
David Tolnay09011c32020-03-06 14:40:28 -0800148 }
149 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700150 Api::RustFunction(efn) if !out.header => {
151 if efn.throws {
152 out.include.exception = true;
153 needs_rust_error = true;
154 }
155 for arg in &efn.args {
156 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
157 needs_manually_drop = true;
158 break;
159 }
160 }
161 if let Some(ret) = &efn.ret {
162 if types.needs_indirect_abi(ret) {
163 needs_maybe_uninit = true;
164 }
David Tolnayf51447e2020-03-06 14:14:27 -0800165 }
166 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700167 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800168 }
169 }
170
David Tolnay750755e2020-03-01 13:04:08 -0800171 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700172 out.begin_block("inline namespace cxxbridge02");
David Tolnayf51447e2020-03-06 14:14:27 -0800173
David Tolnayb7a7cb62020-03-17 21:18:40 -0700174 if needs_rust_string
175 || needs_rust_str
176 || needs_rust_box
177 || needs_rust_error
178 || needs_unsafe_bitcopy
179 || needs_manually_drop
180 || needs_maybe_uninit
David Tolnay5d121442020-03-17 22:14:40 -0700181 || needs_trycatch
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 {
David Tolnay736cbca2020-03-11 16:49:18 -0700183 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800184 }
185
David Tolnayb7a7cb62020-03-17 21:18:40 -0700186 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
187 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
188 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
189 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
190 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800191
192 if needs_manually_drop {
193 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700194 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800195 writeln!(out, "template <typename T>");
196 writeln!(out, "union ManuallyDrop {{");
197 writeln!(out, " T value;");
198 writeln!(
199 out,
200 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
201 );
202 writeln!(out, " ~ManuallyDrop() {{}}");
203 writeln!(out, "}};");
204 }
205
David Tolnay09011c32020-03-06 14:40:28 -0800206 if needs_maybe_uninit {
207 out.next_section();
208 writeln!(out, "template <typename T>");
209 writeln!(out, "union MaybeUninit {{");
210 writeln!(out, " T value;");
211 writeln!(out, " MaybeUninit() {{}}");
212 writeln!(out, " ~MaybeUninit() {{}}");
213 writeln!(out, "}};");
214 }
215
David Tolnay3e3e0af2020-03-17 22:42:49 -0700216 out.end_block("namespace cxxbridge02");
217
David Tolnay5d121442020-03-17 22:14:40 -0700218 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700219 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700220 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700221 out.include.type_traits = true;
222 out.include.utility = true;
223 writeln!(out, "class missing {{}};");
224 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700225 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700226 writeln!(out, "template <typename Try, typename Fail>");
227 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700228 writeln!(
229 out,
David Tolnay04722332020-03-18 11:31:54 -0700230 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700231 );
David Tolnay04722332020-03-18 11:31:54 -0700232 writeln!(out, " missing>::value>::type");
233 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700234 writeln!(out, " func();");
235 writeln!(out, "}} catch (const ::std::exception &e) {{");
236 writeln!(out, " fail(e.what());");
237 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700238 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700239 }
240
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800241 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400242}
243
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
245 if needed {
246 out.next_section();
247 for line in include::get(section).lines() {
248 if !line.trim_start().starts_with("//") {
249 writeln!(out, "{}", line);
250 }
251 }
252 }
253}
254
David Tolnay7db73692019-10-20 14:51:12 -0400255fn write_struct(out: &mut OutFile, strct: &Struct) {
256 for line in strct.doc.to_string().lines() {
257 writeln!(out, "//{}", line);
258 }
259 writeln!(out, "struct {} final {{", strct.ident);
260 for field in &strct.fields {
261 write!(out, " ");
262 write_type_space(out, &field.ty);
263 writeln!(out, "{};", field.ident);
264 }
265 writeln!(out, "}};");
266}
267
268fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
269 writeln!(out, "struct {};", ident);
270}
271
David Tolnay8861bee2020-01-20 18:39:24 -0800272fn write_struct_using(out: &mut OutFile, ident: &Ident) {
273 writeln!(out, "using {} = {};", ident, ident);
274}
275
David Tolnayebef4a22020-03-17 15:33:47 -0700276fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
277 let mut has_cxx_throws = false;
278 for api in apis {
279 if let Api::CxxFunction(efn) = api {
280 if efn.throws {
281 has_cxx_throws = true;
282 break;
283 }
284 }
285 }
286
287 if has_cxx_throws {
288 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700289 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700290 out,
291 "const char *cxxbridge02$exception(const char *, size_t);",
292 );
293 }
294}
295
David Tolnay7db73692019-10-20 14:51:12 -0400296fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700297 if efn.throws {
298 write!(out, "::rust::Str::Repr ");
299 } else {
David Tolnay99642622020-03-25 13:07:35 -0700300 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700301 }
David Tolnay7db73692019-10-20 14:51:12 -0400302 for name in out.namespace.clone() {
303 write!(out, "{}$", name);
304 }
David Tolnay8c730492020-03-13 01:29:06 -0700305 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400306 for (i, arg) in efn.args.iter().enumerate() {
307 if i > 0 {
308 write!(out, ", ");
309 }
David Tolnaya46a2372020-03-06 10:03:48 -0800310 if arg.ty == RustString {
311 write!(out, "const ");
312 }
David Tolnay7db73692019-10-20 14:51:12 -0400313 write_extern_arg(out, arg, types);
314 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700315 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400316 if indirect_return {
317 if !efn.args.is_empty() {
318 write!(out, ", ");
319 }
David Tolnay99642622020-03-25 13:07:35 -0700320 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400321 write!(out, "*return$");
322 }
323 writeln!(out, ") noexcept {{");
324 write!(out, " ");
325 write_return_type(out, &efn.ret);
326 write!(out, "(*{}$)(", efn.ident);
327 for (i, arg) in efn.args.iter().enumerate() {
328 if i > 0 {
329 write!(out, ", ");
330 }
331 write_type(out, &arg.ty);
332 }
333 writeln!(out, ") = {};", efn.ident);
334 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700335 if efn.throws {
336 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700337 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700338 writeln!(out, " [&] {{");
339 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700340 }
David Tolnay7db73692019-10-20 14:51:12 -0400341 if indirect_return {
342 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700343 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400344 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700345 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400346 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700347 }
348 match &efn.ret {
349 Some(Type::Ref(_)) => write!(out, "&"),
350 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
351 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400352 }
353 write!(out, "{}$(", efn.ident);
354 for (i, arg) in efn.args.iter().enumerate() {
355 if i > 0 {
356 write!(out, ", ");
357 }
358 if let Type::RustBox(_) = &arg.ty {
359 write_type(out, &arg.ty);
360 write!(out, "::from_raw({})", arg.ident);
361 } else if let Type::UniquePtr(_) = &arg.ty {
362 write_type(out, &arg.ty);
363 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800364 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800365 write!(
366 out,
367 "::rust::String(::rust::unsafe_bitcopy, *{})",
368 arg.ident,
369 );
David Tolnay7db73692019-10-20 14:51:12 -0400370 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700371 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800372 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400373 } else {
374 write!(out, "{}", arg.ident);
375 }
376 }
377 write!(out, ")");
378 match &efn.ret {
379 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
380 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay99642622020-03-25 13:07:35 -0700381 Some(Type::Str(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400382 _ => {}
383 }
384 if indirect_return {
385 write!(out, ")");
386 }
387 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700388 if efn.throws {
389 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700390 writeln!(out, " throw$.ptr = nullptr;");
391 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700392 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700393 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700394 writeln!(
395 out,
David Tolnay5d121442020-03-17 22:14:40 -0700396 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700397 );
David Tolnay5d121442020-03-17 22:14:40 -0700398 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700399 writeln!(out, " return throw$;");
400 }
David Tolnay7db73692019-10-20 14:51:12 -0400401 writeln!(out, "}}");
402}
403
404fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700405 if efn.throws {
406 write!(out, "::rust::Str::Repr ");
407 } else {
David Tolnay99642622020-03-25 13:07:35 -0700408 write_extern_return_type_space(out, &efn.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700409 }
David Tolnay7db73692019-10-20 14:51:12 -0400410 for name in out.namespace.clone() {
411 write!(out, "{}$", name);
412 }
David Tolnay8c730492020-03-13 01:29:06 -0700413 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400414 for (i, arg) in efn.args.iter().enumerate() {
415 if i > 0 {
416 write!(out, ", ");
417 }
418 write_extern_arg(out, arg, types);
419 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700420 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400421 if !efn.args.is_empty() {
422 write!(out, ", ");
423 }
424 write_return_type(out, &efn.ret);
425 write!(out, "*return$");
426 }
427 writeln!(out, ") noexcept;");
428}
429
430fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400431 for line in efn.doc.to_string().lines() {
432 writeln!(out, "//{}", line);
433 }
434 write_return_type(out, &efn.ret);
435 write!(out, "{}(", efn.ident);
436 for (i, arg) in efn.args.iter().enumerate() {
437 if i > 0 {
438 write!(out, ", ");
439 }
440 write_type_space(out, &arg.ty);
441 write!(out, "{}", arg.ident);
442 }
David Tolnay1e548172020-03-16 13:37:09 -0700443 write!(out, ")");
444 if !efn.throws {
445 write!(out, " noexcept");
446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 if out.header {
448 writeln!(out, ";");
449 } else {
450 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800451 for arg in &efn.args {
452 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700453 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800454 write!(out, " ::rust::ManuallyDrop<");
455 write_type(out, &arg.ty);
456 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
457 }
458 }
David Tolnay7db73692019-10-20 14:51:12 -0400459 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700460 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400461 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800462 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400463 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800464 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400465 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800466 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400467 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800468 match ret {
469 Type::RustBox(_) => {
470 write_type(out, ret);
471 write!(out, "::from_raw(");
472 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800473 Type::UniquePtr(_) => {
474 write_type(out, ret);
475 write!(out, "(");
476 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800477 Type::Ref(_) => write!(out, "*"),
478 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800479 }
David Tolnay7db73692019-10-20 14:51:12 -0400480 }
David Tolnay1e548172020-03-16 13:37:09 -0700481 if efn.throws {
482 write!(out, "::rust::Str::Repr error$ = ");
483 }
David Tolnay7db73692019-10-20 14:51:12 -0400484 for name in out.namespace.clone() {
485 write!(out, "{}$", name);
486 }
David Tolnay8c730492020-03-13 01:29:06 -0700487 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400488 for (i, arg) in efn.args.iter().enumerate() {
489 if i > 0 {
490 write!(out, ", ");
491 }
David Tolnaybaae4432020-03-01 20:20:10 -0800492 match &arg.ty {
493 Type::Str(_) => write!(out, "::rust::Str::Repr("),
494 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
495 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400496 }
497 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800498 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800499 Type::RustBox(_) => write!(out, ".into_raw()"),
500 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800501 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800502 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800503 _ => {}
504 }
David Tolnay7db73692019-10-20 14:51:12 -0400505 }
506 if indirect_return {
507 if !efn.args.is_empty() {
508 write!(out, ", ");
509 }
David Tolnay09011c32020-03-06 14:40:28 -0800510 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400511 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800512 write!(out, ")");
513 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800514 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800515 write!(out, ")");
516 }
517 }
518 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700519 if efn.throws {
520 writeln!(out, " if (error$.ptr) {{");
521 writeln!(out, " throw ::rust::Error(error$);");
522 writeln!(out, " }}");
523 }
David Tolnay7db73692019-10-20 14:51:12 -0400524 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700525 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800526 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400527 }
528 writeln!(out, "}}");
529 }
530}
531
532fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
533 match ty {
534 None => write!(out, "void "),
535 Some(ty) => write_type_space(out, ty),
536 }
537}
538
David Tolnay277e3cc2020-03-17 00:11:01 -0700539fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
540 efn.ret
541 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700542 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700543}
544
David Tolnay99642622020-03-25 13:07:35 -0700545fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
546 match ty {
547 Type::RustBox(ty) | Type::UniquePtr(ty) => {
548 write_type_space(out, &ty.inner);
549 write!(out, "*");
550 }
551 Type::Ref(ty) => {
552 if ty.mutability.is_none() {
553 write!(out, "const ");
554 }
555 write_type(out, &ty.inner);
556 write!(out, " *");
557 }
558 Type::Str(_) => write!(out, "::rust::Str::Repr"),
559 _ => write_type(out, ty),
560 }
561}
562
563fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
564 write_indirect_return_type(out, ty);
565 match ty {
566 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
567 Type::Str(_) => write!(out, " "),
568 _ => write_space_after_type(out, ty),
569 }
570}
571
572fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400573 match ty {
574 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
575 write_type_space(out, &ty.inner);
576 write!(out, "*");
577 }
David Tolnay4a441222020-01-25 16:24:27 -0800578 Some(Type::Ref(ty)) => {
579 if ty.mutability.is_none() {
580 write!(out, "const ");
581 }
582 write_type(out, &ty.inner);
583 write!(out, " *");
584 }
David Tolnay750755e2020-03-01 13:04:08 -0800585 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400586 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
587 _ => write_return_type(out, ty),
588 }
589}
590
591fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
592 match &arg.ty {
593 Type::RustBox(ty) | Type::UniquePtr(ty) => {
594 write_type_space(out, &ty.inner);
595 write!(out, "*");
596 }
David Tolnay750755e2020-03-01 13:04:08 -0800597 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400598 _ => write_type_space(out, &arg.ty),
599 }
600 if types.needs_indirect_abi(&arg.ty) {
601 write!(out, "*");
602 }
603 write!(out, "{}", arg.ident);
604}
605
606fn write_type(out: &mut OutFile, ty: &Type) {
607 match ty {
608 Type::Ident(ident) => match Atom::from(ident) {
609 Some(Bool) => write!(out, "bool"),
610 Some(U8) => write!(out, "uint8_t"),
611 Some(U16) => write!(out, "uint16_t"),
612 Some(U32) => write!(out, "uint32_t"),
613 Some(U64) => write!(out, "uint64_t"),
614 Some(Usize) => write!(out, "size_t"),
615 Some(I8) => write!(out, "int8_t"),
616 Some(I16) => write!(out, "int16_t"),
617 Some(I32) => write!(out, "int32_t"),
618 Some(I64) => write!(out, "int64_t"),
619 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700620 Some(F32) => write!(out, "float"),
621 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800622 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800623 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400624 None => write!(out, "{}", ident),
625 },
626 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800627 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400628 write_type(out, &ty.inner);
629 write!(out, ">");
630 }
631 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800632 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400633 write_type(out, &ptr.inner);
634 write!(out, ">");
635 }
636 Type::Ref(r) => {
637 if r.mutability.is_none() {
638 write!(out, "const ");
639 }
640 write_type(out, &r.inner);
641 write!(out, " &");
642 }
643 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800644 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400645 }
David Tolnay417305a2020-03-18 13:54:00 -0700646 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700647 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400648 }
649}
650
651fn write_type_space(out: &mut OutFile, ty: &Type) {
652 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700653 write_space_after_type(out, ty);
654}
655
656fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400657 match ty {
658 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
659 Type::Ref(_) => {}
David Tolnay417305a2020-03-18 13:54:00 -0700660 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700661 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400662 }
663}
664
665fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
666 fn allow_unique_ptr(ident: &Ident) -> bool {
667 Atom::from(ident).is_none()
668 }
669
670 out.begin_block("extern \"C\"");
671 for ty in types {
672 if let Type::RustBox(ty) = ty {
673 if let Type::Ident(inner) = &ty.inner {
674 out.next_section();
675 write_rust_box_extern(out, inner);
676 }
677 } else if let Type::UniquePtr(ptr) = ty {
678 if let Type::Ident(inner) = &ptr.inner {
679 if allow_unique_ptr(inner) {
680 out.next_section();
681 write_unique_ptr(out, inner);
682 }
683 }
684 }
685 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800686 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400687
David Tolnay750755e2020-03-01 13:04:08 -0800688 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700689 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400690 for ty in types {
691 if let Type::RustBox(ty) = ty {
692 if let Type::Ident(inner) = &ty.inner {
693 write_rust_box_impl(out, inner);
694 }
695 }
696 }
David Tolnay8c730492020-03-13 01:29:06 -0700697 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800698 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400699}
700
701fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
702 let mut inner = String::new();
703 for name in &out.namespace {
704 inner += name;
705 inner += "::";
706 }
707 inner += &ident.to_string();
708 let instance = inner.replace("::", "$");
709
David Tolnay8c730492020-03-13 01:29:06 -0700710 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
711 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400712 writeln!(
713 out,
David Tolnay8c730492020-03-13 01:29:06 -0700714 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400715 instance, inner,
716 );
717 writeln!(
718 out,
David Tolnay8c730492020-03-13 01:29:06 -0700719 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400720 instance, inner,
721 );
David Tolnay8c730492020-03-13 01:29:06 -0700722 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400723}
724
725fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
726 let mut inner = String::new();
727 for name in &out.namespace {
728 inner += name;
729 inner += "::";
730 }
731 inner += &ident.to_string();
732 let instance = inner.replace("::", "$");
733
734 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800735 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700736 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400737 writeln!(out, "}}");
738
739 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800740 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700741 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400742 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400743}
744
745fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700746 out.include.utility = true;
747
David Tolnay7db73692019-10-20 14:51:12 -0400748 let mut inner = String::new();
749 for name in &out.namespace {
750 inner += name;
751 inner += "::";
752 }
753 inner += &ident.to_string();
754 let instance = inner.replace("::", "$");
755
David Tolnay8c730492020-03-13 01:29:06 -0700756 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
757 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400758 writeln!(
759 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800760 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400761 inner,
762 );
763 writeln!(
764 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800765 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400766 inner,
767 );
768 writeln!(
769 out,
David Tolnay8c730492020-03-13 01:29:06 -0700770 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400771 instance, inner,
772 );
David Tolnay7e219b82020-03-01 13:14:51 -0800773 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400774 writeln!(out, "}}");
775 writeln!(
776 out,
David Tolnay8c730492020-03-13 01:29:06 -0700777 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400778 instance, inner, inner,
779 );
780 writeln!(
781 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800782 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400783 inner, inner,
784 );
785 writeln!(out, "}}");
786 writeln!(
787 out,
David Tolnay8c730492020-03-13 01:29:06 -0700788 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400789 instance, inner, inner,
790 );
David Tolnay7e219b82020-03-01 13:14:51 -0800791 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400792 writeln!(out, "}}");
793 writeln!(
794 out,
David Tolnay8c730492020-03-13 01:29:06 -0700795 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400796 inner, instance, inner,
797 );
798 writeln!(out, " return ptr.get();");
799 writeln!(out, "}}");
800 writeln!(
801 out,
David Tolnay8c730492020-03-13 01:29:06 -0700802 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400803 inner, instance, inner,
804 );
805 writeln!(out, " return ptr.release();");
806 writeln!(out, "}}");
807 writeln!(
808 out,
David Tolnay8c730492020-03-13 01:29:06 -0700809 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400810 instance, inner,
811 );
812 writeln!(out, " ptr->~unique_ptr();");
813 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700814 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400815}