blob: a17f8f73c89cd305738aa84083bf372efea271e4 [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 Tolnayd1402742020-03-25 22:21:42 -0700186 if needs_rust_string {
187 out.next_section();
188 writeln!(out, "struct unsafe_bitcopy_t;");
189 }
190
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 write_header_section(out, needs_rust_string, "CXXBRIDGE02_RUST_STRING");
192 write_header_section(out, needs_rust_str, "CXXBRIDGE02_RUST_STR");
193 write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
194 write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
195 write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");
David Tolnayf51447e2020-03-06 14:14:27 -0800196
197 if needs_manually_drop {
198 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700199 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800200 writeln!(out, "template <typename T>");
201 writeln!(out, "union ManuallyDrop {{");
202 writeln!(out, " T value;");
203 writeln!(
204 out,
205 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
206 );
207 writeln!(out, " ~ManuallyDrop() {{}}");
208 writeln!(out, "}};");
209 }
210
David Tolnay09011c32020-03-06 14:40:28 -0800211 if needs_maybe_uninit {
212 out.next_section();
213 writeln!(out, "template <typename T>");
214 writeln!(out, "union MaybeUninit {{");
215 writeln!(out, " T value;");
216 writeln!(out, " MaybeUninit() {{}}");
217 writeln!(out, " ~MaybeUninit() {{}}");
218 writeln!(out, "}};");
219 }
220
David Tolnay3e3e0af2020-03-17 22:42:49 -0700221 out.end_block("namespace cxxbridge02");
222
David Tolnay5d121442020-03-17 22:14:40 -0700223 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700224 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700225 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700226 out.include.type_traits = true;
227 out.include.utility = true;
228 writeln!(out, "class missing {{}};");
229 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700230 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700231 writeln!(out, "template <typename Try, typename Fail>");
232 writeln!(out, "static typename std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700233 writeln!(
234 out,
David Tolnay04722332020-03-18 11:31:54 -0700235 " std::is_same<decltype(trycatch(std::declval<Try>(), std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700236 );
David Tolnay04722332020-03-18 11:31:54 -0700237 writeln!(out, " missing>::value>::type");
238 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700239 writeln!(out, " func();");
240 writeln!(out, "}} catch (const ::std::exception &e) {{");
241 writeln!(out, " fail(e.what());");
242 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700243 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700244 }
245
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800246 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400247}
248
David Tolnayb7a7cb62020-03-17 21:18:40 -0700249fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
250 if needed {
251 out.next_section();
252 for line in include::get(section).lines() {
253 if !line.trim_start().starts_with("//") {
254 writeln!(out, "{}", line);
255 }
256 }
257 }
258}
259
David Tolnay7db73692019-10-20 14:51:12 -0400260fn write_struct(out: &mut OutFile, strct: &Struct) {
261 for line in strct.doc.to_string().lines() {
262 writeln!(out, "//{}", line);
263 }
264 writeln!(out, "struct {} final {{", strct.ident);
265 for field in &strct.fields {
266 write!(out, " ");
267 write_type_space(out, &field.ty);
268 writeln!(out, "{};", field.ident);
269 }
270 writeln!(out, "}};");
271}
272
273fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
274 writeln!(out, "struct {};", ident);
275}
276
David Tolnay8861bee2020-01-20 18:39:24 -0800277fn write_struct_using(out: &mut OutFile, ident: &Ident) {
278 writeln!(out, "using {} = {};", ident, ident);
279}
280
David Tolnayebef4a22020-03-17 15:33:47 -0700281fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
282 let mut has_cxx_throws = false;
283 for api in apis {
284 if let Api::CxxFunction(efn) = api {
285 if efn.throws {
286 has_cxx_throws = true;
287 break;
288 }
289 }
290 }
291
292 if has_cxx_throws {
293 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700294 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700295 out,
296 "const char *cxxbridge02$exception(const char *, size_t);",
297 );
298 }
299}
300
David Tolnay7db73692019-10-20 14:51:12 -0400301fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnayebef4a22020-03-17 15:33:47 -0700302 if efn.throws {
303 write!(out, "::rust::Str::Repr ");
304 } else {
David Tolnay99642622020-03-25 13:07:35 -0700305 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700306 }
David Tolnay7db73692019-10-20 14:51:12 -0400307 for name in out.namespace.clone() {
308 write!(out, "{}$", name);
309 }
David Tolnay8c730492020-03-13 01:29:06 -0700310 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400311 for (i, arg) in efn.args.iter().enumerate() {
312 if i > 0 {
313 write!(out, ", ");
314 }
David Tolnaya46a2372020-03-06 10:03:48 -0800315 if arg.ty == RustString {
316 write!(out, "const ");
317 }
David Tolnay7db73692019-10-20 14:51:12 -0400318 write_extern_arg(out, arg, types);
319 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700320 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400321 if indirect_return {
322 if !efn.args.is_empty() {
323 write!(out, ", ");
324 }
David Tolnay99642622020-03-25 13:07:35 -0700325 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400326 write!(out, "*return$");
327 }
328 writeln!(out, ") noexcept {{");
329 write!(out, " ");
330 write_return_type(out, &efn.ret);
331 write!(out, "(*{}$)(", efn.ident);
332 for (i, arg) in efn.args.iter().enumerate() {
333 if i > 0 {
334 write!(out, ", ");
335 }
336 write_type(out, &arg.ty);
337 }
338 writeln!(out, ") = {};", efn.ident);
339 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700340 if efn.throws {
341 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700342 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700343 writeln!(out, " [&] {{");
344 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700345 }
David Tolnay7db73692019-10-20 14:51:12 -0400346 if indirect_return {
347 write!(out, "new (return$) ");
David Tolnay99642622020-03-25 13:07:35 -0700348 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400349 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700350 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400351 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700352 }
353 match &efn.ret {
354 Some(Type::Ref(_)) => write!(out, "&"),
355 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
356 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400357 }
358 write!(out, "{}$(", efn.ident);
359 for (i, arg) in efn.args.iter().enumerate() {
360 if i > 0 {
361 write!(out, ", ");
362 }
363 if let Type::RustBox(_) = &arg.ty {
364 write_type(out, &arg.ty);
365 write!(out, "::from_raw({})", arg.ident);
366 } else if let Type::UniquePtr(_) = &arg.ty {
367 write_type(out, &arg.ty);
368 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800369 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800370 write!(
371 out,
372 "::rust::String(::rust::unsafe_bitcopy, *{})",
373 arg.ident,
374 );
David Tolnay7db73692019-10-20 14:51:12 -0400375 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700376 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800377 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400378 } else {
379 write!(out, "{}", arg.ident);
380 }
381 }
382 write!(out, ")");
383 match &efn.ret {
384 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
385 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay99642622020-03-25 13:07:35 -0700386 Some(Type::Str(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400387 _ => {}
388 }
389 if indirect_return {
390 write!(out, ")");
391 }
392 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700393 if efn.throws {
394 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700395 writeln!(out, " throw$.ptr = nullptr;");
396 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700397 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700398 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700399 writeln!(
400 out,
David Tolnay5d121442020-03-17 22:14:40 -0700401 " throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700402 );
David Tolnay5d121442020-03-17 22:14:40 -0700403 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700404 writeln!(out, " return throw$;");
405 }
David Tolnay7db73692019-10-20 14:51:12 -0400406 writeln!(out, "}}");
407}
408
409fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay1e548172020-03-16 13:37:09 -0700410 if efn.throws {
411 write!(out, "::rust::Str::Repr ");
412 } else {
David Tolnay99642622020-03-25 13:07:35 -0700413 write_extern_return_type_space(out, &efn.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700414 }
David Tolnay7db73692019-10-20 14:51:12 -0400415 for name in out.namespace.clone() {
416 write!(out, "{}$", name);
417 }
David Tolnay8c730492020-03-13 01:29:06 -0700418 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400419 for (i, arg) in efn.args.iter().enumerate() {
420 if i > 0 {
421 write!(out, ", ");
422 }
423 write_extern_arg(out, arg, types);
424 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700425 if indirect_return(efn, types) {
David Tolnay7db73692019-10-20 14:51:12 -0400426 if !efn.args.is_empty() {
427 write!(out, ", ");
428 }
429 write_return_type(out, &efn.ret);
430 write!(out, "*return$");
431 }
432 writeln!(out, ") noexcept;");
433}
434
435fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400436 for line in efn.doc.to_string().lines() {
437 writeln!(out, "//{}", line);
438 }
439 write_return_type(out, &efn.ret);
440 write!(out, "{}(", efn.ident);
441 for (i, arg) in efn.args.iter().enumerate() {
442 if i > 0 {
443 write!(out, ", ");
444 }
445 write_type_space(out, &arg.ty);
446 write!(out, "{}", arg.ident);
447 }
David Tolnay1e548172020-03-16 13:37:09 -0700448 write!(out, ")");
449 if !efn.throws {
450 write!(out, " noexcept");
451 }
David Tolnay7db73692019-10-20 14:51:12 -0400452 if out.header {
453 writeln!(out, ";");
454 } else {
455 writeln!(out, " {{");
David Tolnayf51447e2020-03-06 14:14:27 -0800456 for arg in &efn.args {
457 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700458 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800459 write!(out, " ::rust::ManuallyDrop<");
460 write_type(out, &arg.ty);
461 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
462 }
463 }
David Tolnay7db73692019-10-20 14:51:12 -0400464 write!(out, " ");
David Tolnay277e3cc2020-03-17 00:11:01 -0700465 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400466 if indirect_return {
David Tolnay09011c32020-03-06 14:40:28 -0800467 write!(out, "::rust::MaybeUninit<");
David Tolnay7db73692019-10-20 14:51:12 -0400468 write_type(out, efn.ret.as_ref().unwrap());
David Tolnay09011c32020-03-06 14:40:28 -0800469 writeln!(out, "> return$;");
David Tolnay7db73692019-10-20 14:51:12 -0400470 write!(out, " ");
David Tolnay4a441222020-01-25 16:24:27 -0800471 } else if let Some(ret) = &efn.ret {
David Tolnay7db73692019-10-20 14:51:12 -0400472 write!(out, "return ");
David Tolnay5cd8d612020-03-06 15:56:30 -0800473 match ret {
474 Type::RustBox(_) => {
475 write_type(out, ret);
476 write!(out, "::from_raw(");
477 }
David Tolnay4b3a66e2020-03-06 16:14:00 -0800478 Type::UniquePtr(_) => {
479 write_type(out, ret);
480 write!(out, "(");
481 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800482 Type::Ref(_) => write!(out, "*"),
483 _ => {}
David Tolnay4a441222020-01-25 16:24:27 -0800484 }
David Tolnay7db73692019-10-20 14:51:12 -0400485 }
David Tolnay1e548172020-03-16 13:37:09 -0700486 if efn.throws {
487 write!(out, "::rust::Str::Repr error$ = ");
488 }
David Tolnay7db73692019-10-20 14:51:12 -0400489 for name in out.namespace.clone() {
490 write!(out, "{}$", name);
491 }
David Tolnay8c730492020-03-13 01:29:06 -0700492 write!(out, "cxxbridge02${}(", efn.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400493 for (i, arg) in efn.args.iter().enumerate() {
494 if i > 0 {
495 write!(out, ", ");
496 }
David Tolnaybaae4432020-03-01 20:20:10 -0800497 match &arg.ty {
498 Type::Str(_) => write!(out, "::rust::Str::Repr("),
499 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
500 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400501 }
502 write!(out, "{}", arg.ident);
David Tolnayf51447e2020-03-06 14:14:27 -0800503 match &arg.ty {
David Tolnay17955e22020-01-20 17:58:24 -0800504 Type::RustBox(_) => write!(out, ".into_raw()"),
505 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnaybaae4432020-03-01 20:20:10 -0800506 Type::Str(_) => write!(out, ")"),
David Tolnayf51447e2020-03-06 14:14:27 -0800507 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay17955e22020-01-20 17:58:24 -0800508 _ => {}
509 }
David Tolnay7db73692019-10-20 14:51:12 -0400510 }
511 if indirect_return {
512 if !efn.args.is_empty() {
513 write!(out, ", ");
514 }
David Tolnay09011c32020-03-06 14:40:28 -0800515 write!(out, "&return$.value");
David Tolnay7db73692019-10-20 14:51:12 -0400516 }
David Tolnay5cd8d612020-03-06 15:56:30 -0800517 write!(out, ")");
518 if let Some(ret) = &efn.ret {
David Tolnay4b3a66e2020-03-06 16:14:00 -0800519 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
David Tolnay5cd8d612020-03-06 15:56:30 -0800520 write!(out, ")");
521 }
522 }
523 writeln!(out, ";");
David Tolnay1e548172020-03-16 13:37:09 -0700524 if efn.throws {
525 writeln!(out, " if (error$.ptr) {{");
526 writeln!(out, " throw ::rust::Error(error$);");
527 writeln!(out, " }}");
528 }
David Tolnay7db73692019-10-20 14:51:12 -0400529 if indirect_return {
David Tolnay4791f1c2020-03-17 21:53:16 -0700530 out.include.utility = true;
David Tolnay09011c32020-03-06 14:40:28 -0800531 writeln!(out, " return ::std::move(return$.value);");
David Tolnay7db73692019-10-20 14:51:12 -0400532 }
533 writeln!(out, "}}");
534 }
535}
536
537fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
538 match ty {
539 None => write!(out, "void "),
540 Some(ty) => write_type_space(out, ty),
541 }
542}
543
David Tolnay277e3cc2020-03-17 00:11:01 -0700544fn indirect_return(efn: &ExternFn, types: &Types) -> bool {
545 efn.ret
546 .as_ref()
David Tolnay1e548172020-03-16 13:37:09 -0700547 .map_or(false, |ret| efn.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700548}
549
David Tolnay99642622020-03-25 13:07:35 -0700550fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
551 match ty {
552 Type::RustBox(ty) | Type::UniquePtr(ty) => {
553 write_type_space(out, &ty.inner);
554 write!(out, "*");
555 }
556 Type::Ref(ty) => {
557 if ty.mutability.is_none() {
558 write!(out, "const ");
559 }
560 write_type(out, &ty.inner);
561 write!(out, " *");
562 }
563 Type::Str(_) => write!(out, "::rust::Str::Repr"),
564 _ => write_type(out, ty),
565 }
566}
567
568fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
569 write_indirect_return_type(out, ty);
570 match ty {
571 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
572 Type::Str(_) => write!(out, " "),
573 _ => write_space_after_type(out, ty),
574 }
575}
576
577fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400578 match ty {
579 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
580 write_type_space(out, &ty.inner);
581 write!(out, "*");
582 }
David Tolnay4a441222020-01-25 16:24:27 -0800583 Some(Type::Ref(ty)) => {
584 if ty.mutability.is_none() {
585 write!(out, "const ");
586 }
587 write_type(out, &ty.inner);
588 write!(out, " *");
589 }
David Tolnay750755e2020-03-01 13:04:08 -0800590 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400591 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
592 _ => write_return_type(out, ty),
593 }
594}
595
596fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
597 match &arg.ty {
598 Type::RustBox(ty) | Type::UniquePtr(ty) => {
599 write_type_space(out, &ty.inner);
600 write!(out, "*");
601 }
David Tolnay750755e2020-03-01 13:04:08 -0800602 Type::Str(_) => write!(out, "::rust::Str::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400603 _ => write_type_space(out, &arg.ty),
604 }
605 if types.needs_indirect_abi(&arg.ty) {
606 write!(out, "*");
607 }
608 write!(out, "{}", arg.ident);
609}
610
611fn write_type(out: &mut OutFile, ty: &Type) {
612 match ty {
613 Type::Ident(ident) => match Atom::from(ident) {
614 Some(Bool) => write!(out, "bool"),
615 Some(U8) => write!(out, "uint8_t"),
616 Some(U16) => write!(out, "uint16_t"),
617 Some(U32) => write!(out, "uint32_t"),
618 Some(U64) => write!(out, "uint64_t"),
619 Some(Usize) => write!(out, "size_t"),
620 Some(I8) => write!(out, "int8_t"),
621 Some(I16) => write!(out, "int16_t"),
622 Some(I32) => write!(out, "int32_t"),
623 Some(I64) => write!(out, "int64_t"),
624 Some(Isize) => write!(out, "ssize_t"),
David Tolnay3383ae72020-03-13 01:12:26 -0700625 Some(F32) => write!(out, "float"),
626 Some(F64) => write!(out, "double"),
David Tolnay7e219b82020-03-01 13:14:51 -0800627 Some(CxxString) => write!(out, "::std::string"),
David Tolnay750755e2020-03-01 13:04:08 -0800628 Some(RustString) => write!(out, "::rust::String"),
David Tolnay7db73692019-10-20 14:51:12 -0400629 None => write!(out, "{}", ident),
630 },
631 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800632 write!(out, "::rust::Box<");
David Tolnay7db73692019-10-20 14:51:12 -0400633 write_type(out, &ty.inner);
634 write!(out, ">");
635 }
636 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800637 write!(out, "::std::unique_ptr<");
David Tolnay7db73692019-10-20 14:51:12 -0400638 write_type(out, &ptr.inner);
639 write!(out, ">");
640 }
641 Type::Ref(r) => {
642 if r.mutability.is_none() {
643 write!(out, "const ");
644 }
645 write_type(out, &r.inner);
646 write!(out, " &");
647 }
648 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800649 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400650 }
David Tolnay417305a2020-03-18 13:54:00 -0700651 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700652 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400653 }
654}
655
656fn write_type_space(out: &mut OutFile, ty: &Type) {
657 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700658 write_space_after_type(out, ty);
659}
660
661fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400662 match ty {
663 Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
664 Type::Ref(_) => {}
David Tolnay417305a2020-03-18 13:54:00 -0700665 Type::Fn(_) => unimplemented!(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700666 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400667 }
668}
669
670fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
671 fn allow_unique_ptr(ident: &Ident) -> bool {
672 Atom::from(ident).is_none()
673 }
674
675 out.begin_block("extern \"C\"");
676 for ty in types {
677 if let Type::RustBox(ty) = ty {
678 if let Type::Ident(inner) = &ty.inner {
679 out.next_section();
680 write_rust_box_extern(out, inner);
681 }
682 } else if let Type::UniquePtr(ptr) = ty {
683 if let Type::Ident(inner) = &ptr.inner {
684 if allow_unique_ptr(inner) {
685 out.next_section();
686 write_unique_ptr(out, inner);
687 }
688 }
689 }
690 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800691 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400692
David Tolnay750755e2020-03-01 13:04:08 -0800693 out.begin_block("namespace rust");
David Tolnay8c730492020-03-13 01:29:06 -0700694 out.begin_block("inline namespace cxxbridge02");
David Tolnay7db73692019-10-20 14:51:12 -0400695 for ty in types {
696 if let Type::RustBox(ty) = ty {
697 if let Type::Ident(inner) = &ty.inner {
698 write_rust_box_impl(out, inner);
699 }
700 }
701 }
David Tolnay8c730492020-03-13 01:29:06 -0700702 out.end_block("namespace cxxbridge02");
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800703 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400704}
705
706fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
707 let mut inner = String::new();
708 for name in &out.namespace {
709 inner += name;
710 inner += "::";
711 }
712 inner += &ident.to_string();
713 let instance = inner.replace("::", "$");
714
David Tolnay8c730492020-03-13 01:29:06 -0700715 writeln!(out, "#ifndef CXXBRIDGE02_RUST_BOX_{}", instance);
716 writeln!(out, "#define CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400717 writeln!(
718 out,
David Tolnay8c730492020-03-13 01:29:06 -0700719 "void cxxbridge02$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400720 instance, inner,
721 );
722 writeln!(
723 out,
David Tolnay8c730492020-03-13 01:29:06 -0700724 "void cxxbridge02$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -0400725 instance, inner,
726 );
David Tolnay8c730492020-03-13 01:29:06 -0700727 writeln!(out, "#endif // CXXBRIDGE02_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400728}
729
730fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
731 let mut inner = String::new();
732 for name in &out.namespace {
733 inner += name;
734 inner += "::";
735 }
736 inner += &ident.to_string();
737 let instance = inner.replace("::", "$");
738
739 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800740 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700741 writeln!(out, " return cxxbridge02$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400742 writeln!(out, "}}");
743
744 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -0800745 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8c730492020-03-13 01:29:06 -0700746 writeln!(out, " return cxxbridge02$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400747 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400748}
749
750fn write_unique_ptr(out: &mut OutFile, ident: &Ident) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700751 out.include.utility = true;
752
David Tolnay7db73692019-10-20 14:51:12 -0400753 let mut inner = String::new();
754 for name in &out.namespace {
755 inner += name;
756 inner += "::";
757 }
758 inner += &ident.to_string();
759 let instance = inner.replace("::", "$");
760
David Tolnay8c730492020-03-13 01:29:06 -0700761 writeln!(out, "#ifndef CXXBRIDGE02_UNIQUE_PTR_{}", instance);
762 writeln!(out, "#define CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400763 writeln!(
764 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800765 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400766 inner,
767 );
768 writeln!(
769 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800770 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -0400771 inner,
772 );
773 writeln!(
774 out,
David Tolnay8c730492020-03-13 01:29:06 -0700775 "void cxxbridge02$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400776 instance, inner,
777 );
David Tolnay7e219b82020-03-01 13:14:51 -0800778 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400779 writeln!(out, "}}");
780 writeln!(
781 out,
David Tolnay8c730492020-03-13 01:29:06 -0700782 "void cxxbridge02$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400783 instance, inner, inner,
784 );
785 writeln!(
786 out,
David Tolnay7e219b82020-03-01 13:14:51 -0800787 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
David Tolnay7db73692019-10-20 14:51:12 -0400788 inner, inner,
789 );
790 writeln!(out, "}}");
791 writeln!(
792 out,
David Tolnay8c730492020-03-13 01:29:06 -0700793 "void cxxbridge02$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400794 instance, inner, inner,
795 );
David Tolnay7e219b82020-03-01 13:14:51 -0800796 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -0400797 writeln!(out, "}}");
798 writeln!(
799 out,
David Tolnay8c730492020-03-13 01:29:06 -0700800 "const {} *cxxbridge02$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400801 inner, instance, inner,
802 );
803 writeln!(out, " return ptr.get();");
804 writeln!(out, "}}");
805 writeln!(
806 out,
David Tolnay8c730492020-03-13 01:29:06 -0700807 "{} *cxxbridge02$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400808 inner, instance, inner,
809 );
810 writeln!(out, " return ptr.release();");
811 writeln!(out, "}}");
812 writeln!(
813 out,
David Tolnay8c730492020-03-13 01:29:06 -0700814 "void cxxbridge02$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -0400815 instance, inner,
816 );
817 writeln!(out, " ptr->~unique_ptr();");
818 writeln!(out, "}}");
David Tolnay8c730492020-03-13 01:29:06 -0700819 writeln!(out, "#endif // CXXBRIDGE02_UNIQUE_PTR_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -0400820}