blob: a06ab623e10f2a1fdd65ad1e7ba81e48a255cb77 [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay7db73692019-10-20 14:51:12 -04002use crate::gen::out::OutFile;
David Tolnay33d30292020-03-18 18:02:02 -07003use crate::gen::{include, Opt};
David Tolnay7db73692019-10-20 14:51:12 -04004use crate::syntax::atom::Atom::{self, *};
David Tolnay891061b2020-04-19 22:42:33 -07005use crate::syntax::symbol::Symbol;
Adrian Taylorc8713432020-10-21 18:20:55 -07006use crate::syntax::{
7 mangle, Api, CppName, Enum, ExternFn, ExternType, ResolvableName, Signature, Struct, Type,
8 Types, Var,
9};
David Tolnay7db73692019-10-20 14:51:12 -040010use proc_macro2::Ident;
Joel Galenson0f654ff2020-05-04 20:04:21 -070011use std::collections::HashMap;
David Tolnay7db73692019-10-20 14:51:12 -040012
David Tolnayb560a0f2020-10-30 21:28:45 -070013pub(super) fn gen<'a>(apis: &[Api], types: &'a Types, opt: &Opt, header: bool) -> OutFile<'a> {
14 let mut out_file = OutFile::new(header, types);
David Tolnay7db73692019-10-20 14:51:12 -040015 let out = &mut out_file;
16
David Tolnay54702b92020-07-31 11:50:09 -070017 if header {
18 writeln!(out.front, "#pragma once");
19 }
20
David Tolnay4aae7c02020-10-28 12:35:42 -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 Tolnay353d98c2020-10-28 15:43:35 -070024 out.include.insert(include);
David Tolnay7db73692019-10-20 14:51:12 -040025 }
26 }
27
David Tolnaya7c2ea12020-10-30 21:32:53 -070028 write_includes(out);
29 write_include_cxxbridge(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -040030
David Tolnay7db73692019-10-20 14:51:12 -040031 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070032
Adrian Taylor565ddf02020-10-29 21:12:36 -070033 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070034
35 gen_namespace_contents(&apis_by_namespace, types, opt, header, out);
36
37 if !header {
38 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070039 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040040 }
41
Adrian Taylorc8713432020-10-21 18:20:55 -070042 write!(out.front, "{}", out.include);
43
44 out_file
45}
46
47fn gen_namespace_contents(
48 ns_entries: &NamespaceEntries,
49 types: &Types,
50 opt: &Opt,
51 header: bool,
52 out: &mut OutFile,
53) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070054 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070055
David Tolnay7db73692019-10-20 14:51:12 -040056 out.next_section();
Adrian Taylor451ec9f2020-10-29 22:51:30 -070057 for api in apis.iter() {
David Tolnay7db73692019-10-20 14:51:12 -040058 match api {
Adrian Taylorc8713432020-10-21 18:20:55 -070059 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
60 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
61 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070062 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040063 }
64 }
65
David Tolnayf94bef12020-04-17 14:46:42 -070066 let mut methods_for_type = HashMap::new();
Adrian Taylor451ec9f2020-10-29 22:51:30 -070067 for api in apis.iter() {
David Tolnayf94bef12020-04-17 14:46:42 -070068 if let Api::RustFunction(efn) = api {
69 if let Some(receiver) = &efn.sig.receiver {
70 methods_for_type
Adrian Taylorc8713432020-10-21 18:20:55 -070071 .entry(&receiver.ty.rust)
David Tolnayf94bef12020-04-17 14:46:42 -070072 .or_insert_with(Vec::new)
73 .push(efn);
74 }
75 }
76 }
Joel Galenson968738f2020-04-15 14:19:33 -070077
David Tolnay7db73692019-10-20 14:51:12 -040078 for api in apis {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070079 match api {
80 Api::Struct(strct) => {
81 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070082 if !types.cxx.contains(&strct.ident.rust) {
David Tolnaya7c2ea12020-10-30 21:32:53 -070083 write_struct(out, strct);
David Tolnaya593d6e2020-08-29 19:48:08 -070084 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070085 }
Joel Galensonc03402a2020-04-23 17:31:09 -070086 Api::Enum(enm) => {
87 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -070088 if types.cxx.contains(&enm.ident.rust) {
Joel Galenson905eb2e2020-05-04 14:58:14 -070089 check_enum(out, enm);
90 } else {
91 write_enum(out, enm);
92 }
Joel Galensonc03402a2020-04-23 17:31:09 -070093 }
David Tolnayc1fe0052020-04-17 15:15:06 -070094 Api::RustType(ety) => {
Adrian Taylorc8713432020-10-21 18:20:55 -070095 if let Some(methods) = methods_for_type.get(&ety.ident.rust) {
David Tolnay46a54e72020-04-17 14:48:21 -070096 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070097 write_struct_with_methods(out, ety, methods);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -070098 }
David Tolnayc1fe0052020-04-17 15:15:06 -070099 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700100 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400101 }
102 }
103
David Tolnayfabca772020-10-03 21:25:41 -0700104 out.next_section();
105 for api in apis {
106 if let Api::TypeAlias(ety) = api {
Adrian Taylorc8713432020-10-21 18:20:55 -0700107 if types.required_trivial.contains_key(&ety.ident.rust) {
108 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700109 }
110 }
111 }
112
David Tolnay7db73692019-10-20 14:51:12 -0400113 if !header {
114 out.begin_block("extern \"C\"");
David Tolnayebef4a22020-03-17 15:33:47 -0700115 write_exception_glue(out, apis);
David Tolnay7db73692019-10-20 14:51:12 -0400116 for api in apis {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700117 let (efn, write): (_, fn(_, _, _)) = match api {
David Tolnay7db73692019-10-20 14:51:12 -0400118 Api::CxxFunction(efn) => (efn, write_cxx_function_shim),
119 Api::RustFunction(efn) => (efn, write_rust_function_decl),
120 _ => continue,
121 };
122 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700123 write(out, efn, &opt.cxx_impl_annotations);
David Tolnay7db73692019-10-20 14:51:12 -0400124 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800125 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -0400126 }
127
128 for api in apis {
129 if let Api::RustFunction(efn) = api {
130 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700131 write_rust_function_shim(out, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400132 }
133 }
134
135 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700136
Adrian Taylor565ddf02020-10-29 21:12:36 -0700137 for (child_ns, child_ns_entries) in ns_entries.children() {
Adrian Taylorc8713432020-10-21 18:20:55 -0700138 writeln!(out, "namespace {} {{", child_ns);
139 gen_namespace_contents(&child_ns_entries, types, opt, header, out);
140 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
David Tolnaya7c2ea12020-10-30 21:32:53 -0700144fn write_includes(out: &mut OutFile) {
145 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400146 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700147 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnay89e386d2020-10-03 19:02:19 -0700148 Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
149 | Some(I64) => out.include.cstdint = true,
150 Some(Usize) => out.include.cstddef = true,
151 Some(CxxString) => out.include.string = true,
David Tolnayf57f7562020-10-04 19:56:26 -0700152 Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700153 },
David Tolnay9c68b1a2020-03-06 11:12:55 -0800154 Type::RustBox(_) => out.include.type_traits = true,
155 Type::UniquePtr(_) => out.include.memory = true,
David Tolnay4377a9e2020-04-24 15:20:26 -0700156 Type::CxxVector(_) => out.include.vector = true,
David Tolnay4770b472020-04-14 16:32:59 -0700157 Type::SliceRefU8(_) => out.include.cstdint = true,
David Tolnay7c295462020-04-25 12:45:07 -0700158 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400159 }
160 }
David Tolnay7db73692019-10-20 14:51:12 -0400161}
162
David Tolnaya7c2ea12020-10-30 21:32:53 -0700163fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api]) {
David Tolnay16ab1462020-09-02 15:10:09 -0700164 let mut needs_panic = false;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700165 let mut needs_rust_string = false;
166 let mut needs_rust_str = false;
David Tolnay4770b472020-04-14 16:32:59 -0700167 let mut needs_rust_slice = false;
David Tolnay7db73692019-10-20 14:51:12 -0400168 let mut needs_rust_box = false;
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 let mut needs_rust_vec = false;
David Tolnay75dca2e2020-03-25 20:17:52 -0700170 let mut needs_rust_fn = false;
David Tolnay7c295462020-04-25 12:45:07 -0700171 let mut needs_rust_isize = false;
David Tolnay99a95e62020-09-02 15:05:53 -0700172 let mut needs_unsafe_bitcopy = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700173 for ty in out.types {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700174 match ty {
175 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700176 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700177 out.include.type_traits = true;
178 needs_rust_box = true;
179 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700180 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700181 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700182 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700183 out.include.type_traits = true;
David Tolnay16ab1462020-09-02 15:10:09 -0700184 needs_panic = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700185 needs_rust_vec = true;
David Tolnay99a95e62020-09-02 15:05:53 -0700186 needs_unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700187 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188 Type::Str(_) => {
189 out.include.cstdint = true;
190 out.include.string = true;
191 needs_rust_str = true;
192 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700193 Type::Fn(_) => {
194 needs_rust_fn = true;
195 }
David Tolnay4770b472020-04-14 16:32:59 -0700196 Type::Slice(_) | Type::SliceRefU8(_) => {
197 needs_rust_slice = true;
198 }
David Tolnay7c295462020-04-25 12:45:07 -0700199 ty if ty == Isize => {
David Tolnayda38b7c2020-09-16 11:50:04 -0400200 out.include.basetsd = true;
David Tolnay7c295462020-04-25 12:45:07 -0700201 needs_rust_isize = true;
202 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700203 ty if ty == RustString => {
204 out.include.array = true;
205 out.include.cstdint = true;
206 out.include.string = true;
207 needs_rust_string = true;
208 }
209 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400210 }
211 }
212
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213 let mut needs_rust_error = false;
David Tolnayf51447e2020-03-06 14:14:27 -0800214 let mut needs_manually_drop = false;
David Tolnay09011c32020-03-06 14:40:28 -0800215 let mut needs_maybe_uninit = false;
David Tolnay5d121442020-03-17 22:14:40 -0700216 let mut needs_trycatch = false;
David Tolnay09011c32020-03-06 14:40:28 -0800217 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700218 match api {
219 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700220 if efn.throws {
221 needs_trycatch = true;
222 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700223 for arg in &efn.args {
David Tolnay313b10e2020-04-25 16:30:51 -0700224 let bitcopy = match arg.ty {
225 Type::RustVec(_) => true,
226 _ => arg.ty == RustString,
227 };
228 if bitcopy {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700229 needs_unsafe_bitcopy = true;
230 break;
231 }
David Tolnay09011c32020-03-06 14:40:28 -0800232 }
233 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700234 Api::RustFunction(efn) if !out.header => {
235 if efn.throws {
236 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700237 out.include.string = true;
238 needs_rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700239 needs_rust_error = true;
Nehliinffef6bc2020-09-16 13:26:37 +0200240 needs_maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700241 }
242 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700243 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700244 needs_manually_drop = true;
245 break;
246 }
247 }
248 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700249 if out.types.needs_indirect_abi(ret) {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700250 needs_maybe_uninit = true;
251 }
David Tolnayf51447e2020-03-06 14:14:27 -0800252 }
253 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700254 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800255 }
256 }
257
David Tolnay750755e2020-03-01 13:04:08 -0800258 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -0700259 out.begin_block("inline namespace cxxbridge05");
David Tolnayf51447e2020-03-06 14:14:27 -0800260
David Tolnay16ab1462020-09-02 15:10:09 -0700261 if needs_panic
262 || needs_rust_string
David Tolnayb7a7cb62020-03-17 21:18:40 -0700263 || needs_rust_str
David Tolnay4770b472020-04-14 16:32:59 -0700264 || needs_rust_slice
David Tolnayb7a7cb62020-03-17 21:18:40 -0700265 || needs_rust_box
Myron Ahneba35cf2020-02-05 19:41:51 +0700266 || needs_rust_vec
David Tolnay75dca2e2020-03-25 20:17:52 -0700267 || needs_rust_fn
David Tolnayb7a7cb62020-03-17 21:18:40 -0700268 || needs_rust_error
David Tolnay7c295462020-04-25 12:45:07 -0700269 || needs_rust_isize
David Tolnayb7a7cb62020-03-17 21:18:40 -0700270 || needs_unsafe_bitcopy
271 || needs_manually_drop
272 || needs_maybe_uninit
273 {
David Tolnay736cbca2020-03-11 16:49:18 -0700274 writeln!(out, "// #include \"rust/cxx.h\"");
David Tolnayf51447e2020-03-06 14:14:27 -0800275 }
276
David Tolnay8f16ae72020-10-08 18:21:13 -0700277 include::write(out, needs_panic, "CXXBRIDGE05_PANIC");
David Tolnay16ab1462020-09-02 15:10:09 -0700278
David Tolnay99a95e62020-09-02 15:05:53 -0700279 if needs_rust_string {
David Tolnayd1402742020-03-25 22:21:42 -0700280 out.next_section();
281 writeln!(out, "struct unsafe_bitcopy_t;");
282 }
283
David Tolnay84ddf9e2020-10-31 15:36:48 -0700284 if needs_rust_error {
285 out.begin_block("namespace");
286 writeln!(out, "template <typename T>");
287 writeln!(out, "class impl;");
288 out.end_block("namespace");
289 }
290
David Tolnay8f16ae72020-10-08 18:21:13 -0700291 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
292 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
293 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
294 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
295 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
296 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
297 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
298 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
299 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800300
301 if needs_manually_drop {
302 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700303 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800304 writeln!(out, "template <typename T>");
305 writeln!(out, "union ManuallyDrop {{");
306 writeln!(out, " T value;");
307 writeln!(
308 out,
309 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
310 );
311 writeln!(out, " ~ManuallyDrop() {{}}");
312 writeln!(out, "}};");
313 }
314
David Tolnay09011c32020-03-06 14:40:28 -0800315 if needs_maybe_uninit {
316 out.next_section();
317 writeln!(out, "template <typename T>");
318 writeln!(out, "union MaybeUninit {{");
319 writeln!(out, " T value;");
320 writeln!(out, " MaybeUninit() {{}}");
321 writeln!(out, " ~MaybeUninit() {{}}");
322 writeln!(out, "}};");
323 }
324
David Tolnayd68dfa82020-10-31 16:01:24 -0700325 out.begin_block("namespace");
326
327 if needs_trycatch || needs_rust_error {
328 out.begin_block("namespace repr");
329 writeln!(out, "struct PtrLen final {{");
David Tolnay54742b72020-10-31 19:43:13 -0700330 writeln!(out, " const void *ptr;");
David Tolnayd68dfa82020-10-31 16:01:24 -0700331 writeln!(out, " size_t len;");
332 writeln!(out, "}};");
333 out.end_block("namespace repr");
334 }
335
David Tolnaya0c9bc72020-10-31 14:37:14 -0700336 if needs_rust_error {
David Tolnay84ddf9e2020-10-31 15:36:48 -0700337 writeln!(out, "template <>");
338 writeln!(out, "class impl<Error> final {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700339 writeln!(out, "public:");
David Tolnayd68dfa82020-10-31 16:01:24 -0700340 writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700341 writeln!(out, " Error error;");
David Tolnay54742b72020-10-31 19:43:13 -0700342 writeln!(out, " error.msg = static_cast<const char *>(repr.ptr);");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700343 writeln!(out, " error.len = repr.len;");
David Tolnaya0c9bc72020-10-31 14:37:14 -0700344 writeln!(out, " return error;");
345 writeln!(out, " }}");
346 writeln!(out, "}};");
347 }
348
David Tolnayd68dfa82020-10-31 16:01:24 -0700349 out.end_block("namespace");
David Tolnay8f16ae72020-10-08 18:21:13 -0700350 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700351
David Tolnay5d121442020-03-17 22:14:40 -0700352 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700353 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700354 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700355 out.include.type_traits = true;
356 out.include.utility = true;
357 writeln!(out, "class missing {{}};");
358 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700359 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700360 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700361 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700362 writeln!(
363 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700364 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700365 );
David Tolnay04722332020-03-18 11:31:54 -0700366 writeln!(out, " missing>::value>::type");
367 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700368 writeln!(out, " func();");
369 writeln!(out, "}} catch (const ::std::exception &e) {{");
370 writeln!(out, " fail(e.what());");
371 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700372 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700373 }
374
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800375 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400376}
377
David Tolnaya7c2ea12020-10-30 21:32:53 -0700378fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700379 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700380 writeln!(out, "#ifndef {}", guard);
381 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400382 for line in strct.doc.to_string().lines() {
383 writeln!(out, "//{}", line);
384 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700385 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400386 for field in &strct.fields {
387 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700388 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400389 writeln!(out, "{};", field.ident);
390 }
391 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700392 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400393}
394
395fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
396 writeln!(out, "struct {};", ident);
397}
398
Adrian Taylorc8713432020-10-21 18:20:55 -0700399fn write_struct_using(out: &mut OutFile, ident: &CppName) {
400 writeln!(
401 out,
402 "using {} = {};",
403 ident.ident,
404 ident.to_fully_qualified()
405 );
David Tolnay8861bee2020-01-20 18:39:24 -0800406}
407
David Tolnaya7c2ea12020-10-30 21:32:53 -0700408fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700409 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700410 writeln!(out, "#ifndef {}", guard);
411 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700412 for line in ety.doc.to_string().lines() {
413 writeln!(out, "//{}", line);
414 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700415 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
416 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
417 writeln!(
418 out,
419 " {}(const {} &) = delete;",
420 ety.ident.cxx.ident, ety.ident.cxx.ident
421 );
Joel Galenson968738f2020-04-15 14:19:33 -0700422 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700423 write!(out, " ");
424 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700425 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700426 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700427 writeln!(out, ";");
428 }
429 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700430 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700431}
432
Joel Galensonc03402a2020-04-23 17:31:09 -0700433fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700434 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700435 writeln!(out, "#ifndef {}", guard);
436 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700437 for line in enm.doc.to_string().lines() {
438 writeln!(out, "//{}", line);
439 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700440 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700441 write_atom(out, enm.repr);
442 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700443 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700444 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700445 }
446 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700447 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700448}
449
Joel Galenson905eb2e2020-05-04 14:58:14 -0700450fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700451 write!(
452 out,
453 "static_assert(sizeof({}) == sizeof(",
454 enm.ident.cxx.ident
455 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700456 write_atom(out, enm.repr);
457 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700458 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700459 write!(out, "static_assert(static_cast<");
460 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700461 writeln!(
462 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700463 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700464 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700465 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700466 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700467}
468
Adrian Taylorc8713432020-10-21 18:20:55 -0700469fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700470 // NOTE: The following two static assertions are just nice-to-have and not
471 // necessary for soundness. That's because triviality is always declared by
472 // the user in the form of an unsafe impl of cxx::ExternType:
473 //
474 // unsafe impl ExternType for MyType {
475 // type Id = cxx::type_id!("...");
476 // type Kind = cxx::kind::Trivial;
477 // }
478 //
479 // Since the user went on the record with their unsafe impl to unsafely
480 // claim they KNOW that the type is trivial, it's fine for that to be on
481 // them if that were wrong.
482 //
483 // There may be a legitimate reason we'll want to remove these assertions
484 // for support of types that the programmer knows are Rust-movable despite
485 // not being recognized as such by the C++ type system due to a move
486 // constructor or destructor.
487
Adrian Taylorc8713432020-10-21 18:20:55 -0700488 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700489 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700490 writeln!(out, "static_assert(");
491 writeln!(
492 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700493 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700494 id,
495 );
496 writeln!(
497 out,
498 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
499 id,
500 );
501 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700502 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700503 writeln!(
504 out,
505 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
506 id,
507 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700508}
509
Adrian Taylorc8713432020-10-21 18:20:55 -0700510fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700511 let mut has_cxx_throws = false;
512 for api in apis {
513 if let Api::CxxFunction(efn) = api {
514 if efn.throws {
515 has_cxx_throws = true;
516 break;
517 }
518 }
519 }
520
521 if has_cxx_throws {
522 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700523 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700524 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700525 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700526 );
527 }
528}
529
David Tolnaya7c2ea12020-10-30 21:32:53 -0700530fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700531 if let Some(annotation) = impl_annotations {
532 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700533 }
David Tolnayebef4a22020-03-17 15:33:47 -0700534 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700535 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700536 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700537 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700538 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700539 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700540 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700541 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700542 if receiver.mutability.is_none() {
543 write!(out, "const ");
544 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700545 write!(
546 out,
547 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700548 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700549 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700550 }
David Tolnay7db73692019-10-20 14:51:12 -0400551 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700552 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400553 write!(out, ", ");
554 }
David Tolnaya46a2372020-03-06 10:03:48 -0800555 if arg.ty == RustString {
556 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700557 } else if let Type::RustVec(_) = arg.ty {
558 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800559 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700560 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400561 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700562 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400563 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700564 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400565 write!(out, ", ");
566 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700567 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400568 write!(out, "*return$");
569 }
570 writeln!(out, ") noexcept {{");
571 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700572 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700573 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700574 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700575 Some(receiver) => write!(
576 out,
577 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700578 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700579 efn.ident.rust
580 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700581 }
David Tolnay7db73692019-10-20 14:51:12 -0400582 for (i, arg) in efn.args.iter().enumerate() {
583 if i > 0 {
584 write!(out, ", ");
585 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700586 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400587 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700588 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700589 if let Some(receiver) = &efn.receiver {
590 if receiver.mutability.is_none() {
591 write!(out, " const");
592 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700593 }
594 write!(out, " = ");
595 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700596 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
597 Some(receiver) => write!(
598 out,
599 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700600 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700601 efn.ident.cxx.ident
602 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700603 }
604 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400605 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700606 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700607 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700608 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700609 writeln!(out, " [&] {{");
610 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700611 }
David Tolnay7db73692019-10-20 14:51:12 -0400612 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700613 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400614 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400616 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700617 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400618 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700619 }
620 match &efn.ret {
621 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnayeb952ba2020-04-14 15:02:24 -0700622 Some(Type::SliceRefU8(_)) if !indirect_return => {
623 write!(out, "::rust::Slice<uint8_t>::Repr(")
624 }
David Tolnay99642622020-03-25 13:07:35 -0700625 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400626 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700627 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700628 None => write!(out, "{}$(", efn.ident.rust),
629 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700630 }
David Tolnay7db73692019-10-20 14:51:12 -0400631 for (i, arg) in efn.args.iter().enumerate() {
632 if i > 0 {
633 write!(out, ", ");
634 }
635 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700636 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400637 write!(out, "::from_raw({})", arg.ident);
638 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700639 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400640 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800641 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800642 write!(
643 out,
644 "::rust::String(::rust::unsafe_bitcopy, *{})",
645 arg.ident,
646 );
David Tolnay313b10e2020-04-25 16:30:51 -0700647 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700648 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700649 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700650 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700651 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800652 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400653 } else {
654 write!(out, "{}", arg.ident);
655 }
656 }
657 write!(out, ")");
658 match &efn.ret {
659 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
660 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay5df1f062020-10-31 12:31:10 -0700661 Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400662 _ => {}
663 }
664 if indirect_return {
665 write!(out, ")");
666 }
667 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700668 if efn.throws {
669 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700670 writeln!(out, " throw$.ptr = nullptr;");
671 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700672 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700673 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700674 writeln!(
675 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700676 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700677 );
David Tolnay5d121442020-03-17 22:14:40 -0700678 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700679 writeln!(out, " return throw$;");
680 }
David Tolnay7db73692019-10-20 14:51:12 -0400681 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700682 for arg in &efn.args {
683 if let Type::Fn(f) = &arg.ty {
684 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700685 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700686 }
687 }
688}
689
690fn write_function_pointer_trampoline(
691 out: &mut OutFile,
692 efn: &ExternFn,
693 var: &Ident,
694 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700695) {
696 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700697 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700698 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700699 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700700
701 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700702 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
703 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400704}
705
David Tolnaya7c2ea12020-10-30 21:32:53 -0700706fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
707 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700708 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700709 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700710}
711
712fn write_rust_function_decl_impl(
713 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700714 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700715 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700716 indirect_call: bool,
717) {
718 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700719 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700720 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700722 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700723 write!(out, "{}(", link_name);
724 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700725 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700726 if receiver.mutability.is_none() {
727 write!(out, "const ");
728 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700729 write!(
730 out,
731 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700732 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700733 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700734 needs_comma = true;
735 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700736 for arg in &sig.args {
737 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400738 write!(out, ", ");
739 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700740 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700741 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400742 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700744 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400745 write!(out, ", ");
746 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700747 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400748 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700749 needs_comma = true;
750 }
751 if indirect_call {
752 if needs_comma {
753 write!(out, ", ");
754 }
755 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400756 }
757 writeln!(out, ") noexcept;");
758}
759
David Tolnaya7c2ea12020-10-30 21:32:53 -0700760fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400761 for line in efn.doc.to_string().lines() {
762 writeln!(out, "//{}", line);
763 }
David Tolnaya73853b2020-04-20 01:19:56 -0700764 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700765 None => efn.ident.cxx.ident.to_string(),
766 Some(receiver) => format!(
767 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700768 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700769 efn.ident.cxx.ident
770 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700771 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700772 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700773 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700774 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700775}
776
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700777fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700778 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700779 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700780 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700781 indirect_call: bool,
782) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700783 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700784 write!(out, "{}(", local_name);
785 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400786 if i > 0 {
787 write!(out, ", ");
788 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700789 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400790 write!(out, "{}", arg.ident);
791 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700792 if indirect_call {
793 if !sig.args.is_empty() {
794 write!(out, ", ");
795 }
796 write!(out, "void *extern$");
797 }
David Tolnay1e548172020-03-16 13:37:09 -0700798 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700799 if let Some(receiver) = &sig.receiver {
800 if receiver.mutability.is_none() {
801 write!(out, " const");
802 }
803 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700804 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700805 write!(out, " noexcept");
806 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700807}
808
809fn write_rust_function_shim_impl(
810 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700811 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700812 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700813 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700814 indirect_call: bool,
815) {
816 if out.header && sig.receiver.is_some() {
817 // We've already defined this inside the struct.
818 return;
819 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700820 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400821 if out.header {
822 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700823 return;
David Tolnay7db73692019-10-20 14:51:12 -0400824 }
David Tolnay439cde22020-04-20 00:46:25 -0700825 writeln!(out, " {{");
826 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700827 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700828 out.include.utility = true;
829 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700830 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700831 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
832 }
833 }
834 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700835 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700836 if indirect_return {
837 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700838 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700839 writeln!(out, "> return$;");
840 write!(out, " ");
841 } else if let Some(ret) = &sig.ret {
842 write!(out, "return ");
843 match ret {
844 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700845 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700846 write!(out, "::from_raw(");
847 }
848 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700849 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700850 write!(out, "(");
851 }
852 Type::Ref(_) => write!(out, "*"),
853 _ => {}
854 }
855 }
856 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700857 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700858 }
859 write!(out, "{}(", invoke);
860 if sig.receiver.is_some() {
861 write!(out, "*this");
862 }
863 for (i, arg) in sig.args.iter().enumerate() {
864 if i > 0 || sig.receiver.is_some() {
865 write!(out, ", ");
866 }
867 match &arg.ty {
David Tolnay439cde22020-04-20 00:46:25 -0700868 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700869 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700870 _ => {}
871 }
872 write!(out, "{}", arg.ident);
873 match &arg.ty {
874 Type::RustBox(_) => write!(out, ".into_raw()"),
875 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay5df1f062020-10-31 12:31:10 -0700876 Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700877 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700878 _ => {}
879 }
880 }
881 if indirect_return {
882 if !sig.args.is_empty() {
883 write!(out, ", ");
884 }
885 write!(out, "&return$.value");
886 }
887 if indirect_call {
888 if !sig.args.is_empty() || indirect_return {
889 write!(out, ", ");
890 }
891 write!(out, "extern$");
892 }
893 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400894 if !indirect_return {
895 if let Some(ret) = &sig.ret {
896 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
897 write!(out, ")");
898 }
David Tolnay439cde22020-04-20 00:46:25 -0700899 }
900 }
901 writeln!(out, ";");
902 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700903 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700904 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700905 writeln!(out, " }}");
906 }
907 if indirect_return {
908 out.include.utility = true;
909 writeln!(out, " return ::std::move(return$.value);");
910 }
911 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400912}
913
David Tolnaya7c2ea12020-10-30 21:32:53 -0700914fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400915 match ty {
916 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700917 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400918 }
919}
920
David Tolnay75dca2e2020-03-25 20:17:52 -0700921fn indirect_return(sig: &Signature, types: &Types) -> bool {
922 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700923 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700924 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700925}
926
David Tolnaya7c2ea12020-10-30 21:32:53 -0700927fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700928 match ty {
929 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700930 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700931 write!(out, "*");
932 }
933 Type::Ref(ty) => {
934 if ty.mutability.is_none() {
935 write!(out, "const ");
936 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700937 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700938 write!(out, " *");
939 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700940 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700941 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700942 }
943}
944
David Tolnaya7c2ea12020-10-30 21:32:53 -0700945fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
946 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700947 match ty {
948 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700949 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700950 _ => write_space_after_type(out, ty),
951 }
952}
953
David Tolnaya7c2ea12020-10-30 21:32:53 -0700954fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400955 match ty {
956 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700957 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400958 write!(out, "*");
959 }
David Tolnay4a441222020-01-25 16:24:27 -0800960 Some(Type::Ref(ty)) => {
961 if ty.mutability.is_none() {
962 write!(out, "const ");
963 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700964 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800965 write!(out, " *");
966 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700967 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700968 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
969 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400970 }
971}
972
David Tolnaya7c2ea12020-10-30 21:32:53 -0700973fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400974 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700975 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700976 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400977 write!(out, "*");
978 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700979 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700980 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -0400981 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700982 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -0400983 write!(out, "*");
984 }
985 write!(out, "{}", arg.ident);
986}
987
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400989 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700990 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700991 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700992 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -0400993 },
994 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800995 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700996 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400997 write!(out, ">");
998 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700999 Type::RustVec(ty) => {
1000 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001001 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001002 write!(out, ">");
1003 }
David Tolnay7db73692019-10-20 14:51:12 -04001004 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -08001005 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001006 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001007 write!(out, ">");
1008 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001009 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +07001010 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001011 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001012 write!(out, ">");
1013 }
David Tolnay7db73692019-10-20 14:51:12 -04001014 Type::Ref(r) => {
1015 if r.mutability.is_none() {
1016 write!(out, "const ");
1017 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001018 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -04001019 write!(out, " &");
1020 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001021 Type::Slice(_) => {
1022 // For now, only U8 slices are supported, which are covered separately below
1023 unreachable!()
1024 }
David Tolnay7db73692019-10-20 14:51:12 -04001025 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001026 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001027 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001028 Type::SliceRefU8(_) => {
1029 write!(out, "::rust::Slice<uint8_t>");
1030 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001031 Type::Fn(f) => {
1032 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1033 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001034 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -07001035 None => write!(out, "void"),
1036 }
1037 write!(out, "(");
1038 for (i, arg) in f.args.iter().enumerate() {
1039 if i > 0 {
1040 write!(out, ", ");
1041 }
David Tolnaya7c2ea12020-10-30 21:32:53 -07001042 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -07001043 }
1044 write!(out, ")>");
1045 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001046 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001047 }
1048}
1049
David Tolnayf6a89f22020-05-10 23:39:27 -07001050fn write_atom(out: &mut OutFile, atom: Atom) {
1051 match atom {
1052 Bool => write!(out, "bool"),
1053 U8 => write!(out, "uint8_t"),
1054 U16 => write!(out, "uint16_t"),
1055 U32 => write!(out, "uint32_t"),
1056 U64 => write!(out, "uint64_t"),
1057 Usize => write!(out, "size_t"),
1058 I8 => write!(out, "int8_t"),
1059 I16 => write!(out, "int16_t"),
1060 I32 => write!(out, "int32_t"),
1061 I64 => write!(out, "int64_t"),
1062 Isize => write!(out, "::rust::isize"),
1063 F32 => write!(out, "float"),
1064 F64 => write!(out, "double"),
1065 CxxString => write!(out, "::std::string"),
1066 RustString => write!(out, "::rust::String"),
1067 }
1068}
1069
David Tolnaya7c2ea12020-10-30 21:32:53 -07001070fn write_type_space(out: &mut OutFile, ty: &Type) {
1071 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -07001072 write_space_after_type(out, ty);
1073}
1074
1075fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001076 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001077 Type::Ident(_)
1078 | Type::RustBox(_)
1079 | Type::UniquePtr(_)
1080 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001081 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001082 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001083 | Type::SliceRefU8(_)
1084 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001085 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001086 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001087 }
1088}
1089
David Tolnaycd08c442020-04-25 10:16:33 -07001090// Only called for legal referent types of unique_ptr and element types of
1091// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001092fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001093 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001094 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1095 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001096 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001097 }
1098}
1099
David Tolnayacdf20a2020-04-25 12:40:53 -07001100// Only called for legal referent types of unique_ptr and element types of
1101// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001102fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001103 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001104 Type::Ident(ident) => ident.to_symbol(types),
1105 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001106 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001107 }
1108}
1109
David Tolnaya7c2ea12020-10-30 21:32:53 -07001110fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -04001111 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001112 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001113 if let Type::RustBox(ty) = ty {
1114 if let Type::Ident(inner) = &ty.inner {
1115 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001116 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001117 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001118 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001119 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001120 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001121 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001122 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001123 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001124 }
David Tolnay7db73692019-10-20 14:51:12 -04001125 } else if let Type::UniquePtr(ptr) = ty {
1126 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001127 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001128 && (!out.types.aliases.contains_key(&inner.rust)
1129 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001130 {
David Tolnay7db73692019-10-20 14:51:12 -04001131 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001132 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001133 }
1134 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001135 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001136 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001137 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001138 && (!out.types.aliases.contains_key(&inner.rust)
1139 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001140 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001141 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001142 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001143 }
1144 }
1145 }
1146 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001147 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001148
David Tolnay750755e2020-03-01 13:04:08 -08001149 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001150 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001151 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001152 if let Type::RustBox(ty) = ty {
1153 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001155 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001156 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001157 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001158 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001159 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001160 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001161 }
David Tolnay7db73692019-10-20 14:51:12 -04001162 }
1163 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001164 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001165 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001166}
1167
Adrian Taylorc8713432020-10-21 18:20:55 -07001168fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1169 let inner = ident.to_fully_qualified();
1170 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001171
David Tolnay8f16ae72020-10-08 18:21:13 -07001172 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1173 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001174 writeln!(
1175 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001176 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001177 instance, inner,
1178 );
1179 writeln!(
1180 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001181 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001182 instance, inner,
1183 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001184 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001185}
1186
David Tolnaya7c2ea12020-10-30 21:32:53 -07001187fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001188 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001189 let inner = to_typename(&element, out.types);
1190 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001191
David Tolnay8f16ae72020-10-08 18:21:13 -07001192 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1193 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001194 writeln!(
1195 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001196 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001197 instance, inner,
1198 );
1199 writeln!(
1200 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001201 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001202 instance, inner,
1203 );
1204 writeln!(
1205 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001206 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001207 instance, inner,
1208 );
David Tolnay219c0792020-04-24 20:31:37 -07001209 writeln!(
1210 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001211 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001212 inner, instance,
1213 );
David Tolnay503d0192020-04-24 22:18:56 -07001214 writeln!(
1215 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001216 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001217 instance,
1218 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001219 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001220}
1221
Adrian Taylorc8713432020-10-21 18:20:55 -07001222fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1223 let inner = ident.to_fully_qualified();
1224 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001225
1226 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001227 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001228 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001229 writeln!(out, "}}");
1230
1231 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001232 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001233 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001234 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001235}
1236
David Tolnaya7c2ea12020-10-30 21:32:53 -07001237fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001238 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001239 let inner = to_typename(&element, out.types);
1240 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001241
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001243 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001244 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001245 writeln!(out, "}}");
1246
1247 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001248 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1249 writeln!(
1250 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001251 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001252 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001253 );
1254 writeln!(out, "}}");
1255
1256 writeln!(out, "template <>");
1257 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001258 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001259 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001260
1261 writeln!(out, "template <>");
1262 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1263 writeln!(
1264 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001265 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001266 instance,
1267 );
1268 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001269
1270 writeln!(out, "template <>");
1271 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001272 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001273 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001274}
1275
David Tolnaya7c2ea12020-10-30 21:32:53 -07001276fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001277 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001278 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001279
David Tolnay8f16ae72020-10-08 18:21:13 -07001280 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1281 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001282
David Tolnaya7c2ea12020-10-30 21:32:53 -07001283 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001284
David Tolnay8f16ae72020-10-08 18:21:13 -07001285 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001286}
1287
1288// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001289fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001290 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001291 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001292 let inner = to_typename(ty, out.types);
1293 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001294
David Tolnay63da4d32020-04-25 09:41:12 -07001295 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001296 // Some aliases are to opaque types; some are to trivial types. We can't
1297 // know at code generation time, so we generate both C++ and Rust side
1298 // bindings for a "new" method anyway. But the Rust code can't be called
1299 // for Opaque types because the 'new' method is not implemented.
1300 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001301 out.types.structs.contains_key(&ident.rust)
1302 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001303 }
David Tolnay63da4d32020-04-25 09:41:12 -07001304 _ => false,
1305 };
1306
David Tolnay7db73692019-10-20 14:51:12 -04001307 writeln!(
1308 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001309 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001310 inner,
1311 );
1312 writeln!(
1313 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001314 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001315 inner,
1316 );
1317 writeln!(
1318 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001319 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001320 instance, inner,
1321 );
David Tolnay7e219b82020-03-01 13:14:51 -08001322 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001323 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001324 if can_construct_from_value {
1325 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001326 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001327 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001328 instance, inner, inner,
1329 );
David Tolnay63da4d32020-04-25 09:41:12 -07001330 writeln!(
1331 out,
1332 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1333 inner, inner,
1334 );
1335 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001336 }
David Tolnay7db73692019-10-20 14:51:12 -04001337 writeln!(
1338 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001339 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001340 instance, inner, inner,
1341 );
David Tolnay7e219b82020-03-01 13:14:51 -08001342 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001343 writeln!(out, "}}");
1344 writeln!(
1345 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001346 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001347 inner, instance, inner,
1348 );
1349 writeln!(out, " return ptr.get();");
1350 writeln!(out, "}}");
1351 writeln!(
1352 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001353 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001354 inner, instance, inner,
1355 );
1356 writeln!(out, " return ptr.release();");
1357 writeln!(out, "}}");
1358 writeln!(
1359 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001360 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001361 instance, inner,
1362 );
1363 writeln!(out, " ptr->~unique_ptr();");
1364 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001365}
Myron Ahneba35cf2020-02-05 19:41:51 +07001366
David Tolnaya7c2ea12020-10-30 21:32:53 -07001367fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001368 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001369 let inner = to_typename(&element, out.types);
1370 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001371
David Tolnay8f16ae72020-10-08 18:21:13 -07001372 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1373 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001374 writeln!(
1375 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001376 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001377 instance, inner,
1378 );
1379 writeln!(out, " return s.size();");
1380 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001381 writeln!(
1382 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001383 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001384 inner, instance, inner,
1385 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001386 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001387 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001388
David Tolnaya7c2ea12020-10-30 21:32:53 -07001389 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001390
David Tolnay8f16ae72020-10-08 18:21:13 -07001391 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001392}