blob: 3be5e353bf1f283f627d8c62ad389e43f7f897f4 [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
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();
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();
39 write_generic_instantiations(out, types);
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) {
83 write_struct(out, strct, types);
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();
Adrian Taylorc8713432020-10-21 18:20:55 -070097 write_struct_with_methods(out, ety, methods, types);
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 {
Adrian Taylor21f0ff02020-07-21 16:21:48 -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();
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700123 write(out, efn, types, &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();
131 write_rust_function_shim(out, efn, types);
132 }
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
144fn write_includes(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400145 for ty in types {
146 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 Tolnayf51447e2020-03-06 14:14:27 -0800163fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
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 Tolnay7db73692019-10-20 14:51:12 -0400173 for ty in 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 {
243 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
244 needs_manually_drop = true;
245 break;
246 }
247 }
248 if let Some(ret) = &efn.ret {
249 if types.needs_indirect_abi(ret) {
250 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 Tolnay8f16ae72020-10-08 18:21:13 -0700284 include::write(out, needs_rust_string, "CXXBRIDGE05_RUST_STRING");
285 include::write(out, needs_rust_str, "CXXBRIDGE05_RUST_STR");
286 include::write(out, needs_rust_slice, "CXXBRIDGE05_RUST_SLICE");
287 include::write(out, needs_rust_box, "CXXBRIDGE05_RUST_BOX");
288 include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE05_RUST_BITCOPY");
289 include::write(out, needs_rust_vec, "CXXBRIDGE05_RUST_VEC");
290 include::write(out, needs_rust_fn, "CXXBRIDGE05_RUST_FN");
291 include::write(out, needs_rust_error, "CXXBRIDGE05_RUST_ERROR");
292 include::write(out, needs_rust_isize, "CXXBRIDGE05_RUST_ISIZE");
David Tolnayf51447e2020-03-06 14:14:27 -0800293
294 if needs_manually_drop {
295 out.next_section();
David Tolnay4791f1c2020-03-17 21:53:16 -0700296 out.include.utility = true;
David Tolnayf51447e2020-03-06 14:14:27 -0800297 writeln!(out, "template <typename T>");
298 writeln!(out, "union ManuallyDrop {{");
299 writeln!(out, " T value;");
300 writeln!(
301 out,
302 " ManuallyDrop(T &&value) : value(::std::move(value)) {{}}",
303 );
304 writeln!(out, " ~ManuallyDrop() {{}}");
305 writeln!(out, "}};");
306 }
307
David Tolnay09011c32020-03-06 14:40:28 -0800308 if needs_maybe_uninit {
309 out.next_section();
310 writeln!(out, "template <typename T>");
311 writeln!(out, "union MaybeUninit {{");
312 writeln!(out, " T value;");
313 writeln!(out, " MaybeUninit() {{}}");
314 writeln!(out, " ~MaybeUninit() {{}}");
315 writeln!(out, "}};");
316 }
317
David Tolnay8f16ae72020-10-08 18:21:13 -0700318 out.end_block("namespace cxxbridge05");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700319
David Tolnay5d121442020-03-17 22:14:40 -0700320 if needs_trycatch {
David Tolnay3e3e0af2020-03-17 22:42:49 -0700321 out.begin_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700322 out.include.exception = true;
David Tolnay04722332020-03-18 11:31:54 -0700323 out.include.type_traits = true;
324 out.include.utility = true;
325 writeln!(out, "class missing {{}};");
326 writeln!(out, "missing trycatch(...);");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700327 writeln!(out);
David Tolnay04722332020-03-18 11:31:54 -0700328 writeln!(out, "template <typename Try, typename Fail>");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700329 writeln!(out, "static typename ::std::enable_if<");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700330 writeln!(
331 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700332 " ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
David Tolnay3e3e0af2020-03-17 22:42:49 -0700333 );
David Tolnay04722332020-03-18 11:31:54 -0700334 writeln!(out, " missing>::value>::type");
335 writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
David Tolnay5d121442020-03-17 22:14:40 -0700336 writeln!(out, " func();");
337 writeln!(out, "}} catch (const ::std::exception &e) {{");
338 writeln!(out, " fail(e.what());");
339 writeln!(out, "}}");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700340 out.end_block("namespace behavior");
David Tolnay5d121442020-03-17 22:14:40 -0700341 }
342
David Tolnay9ad1fbc2020-03-01 14:01:24 -0800343 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -0400344}
345
Adrian Taylorc8713432020-10-21 18:20:55 -0700346fn write_struct(out: &mut OutFile, strct: &Struct, types: &Types) {
347 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700348 writeln!(out, "#ifndef {}", guard);
349 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400350 for line in strct.doc.to_string().lines() {
351 writeln!(out, "//{}", line);
352 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700353 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400354 for field in &strct.fields {
355 write!(out, " ");
Adrian Taylorc8713432020-10-21 18:20:55 -0700356 write_type_space(out, &field.ty, types);
David Tolnay7db73692019-10-20 14:51:12 -0400357 writeln!(out, "{};", field.ident);
358 }
359 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700360 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400361}
362
363fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
364 writeln!(out, "struct {};", ident);
365}
366
Adrian Taylorc8713432020-10-21 18:20:55 -0700367fn write_struct_using(out: &mut OutFile, ident: &CppName) {
368 writeln!(
369 out,
370 "using {} = {};",
371 ident.ident,
372 ident.to_fully_qualified()
373 );
David Tolnay8861bee2020-01-20 18:39:24 -0800374}
375
Adrian Taylorc8713432020-10-21 18:20:55 -0700376fn write_struct_with_methods(
377 out: &mut OutFile,
378 ety: &ExternType,
379 methods: &[&ExternFn],
380 types: &Types,
381) {
382 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700383 writeln!(out, "#ifndef {}", guard);
384 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700385 for line in ety.doc.to_string().lines() {
386 writeln!(out, "//{}", line);
387 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700388 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
389 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
390 writeln!(
391 out,
392 " {}(const {} &) = delete;",
393 ety.ident.cxx.ident, ety.ident.cxx.ident
394 );
Joel Galenson968738f2020-04-15 14:19:33 -0700395 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700396 write!(out, " ");
397 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700398 let local_name = method.ident.cxx.ident.to_string();
399 write_rust_function_shim_decl(out, &local_name, sig, false, types);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700400 writeln!(out, ";");
401 }
402 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700403 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700404}
405
Joel Galensonc03402a2020-04-23 17:31:09 -0700406fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700407 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700408 writeln!(out, "#ifndef {}", guard);
409 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700410 for line in enm.doc.to_string().lines() {
411 writeln!(out, "//{}", line);
412 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700413 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700414 write_atom(out, enm.repr);
415 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700416 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700417 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700418 }
419 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700420 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700421}
422
Joel Galenson905eb2e2020-05-04 14:58:14 -0700423fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700424 write!(
425 out,
426 "static_assert(sizeof({}) == sizeof(",
427 enm.ident.cxx.ident
428 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700429 write_atom(out, enm.repr);
430 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700431 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700432 write!(out, "static_assert(static_cast<");
433 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700434 writeln!(
435 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700436 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700437 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700438 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700439 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700440}
441
Adrian Taylorc8713432020-10-21 18:20:55 -0700442fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700443 // NOTE: The following two static assertions are just nice-to-have and not
444 // necessary for soundness. That's because triviality is always declared by
445 // the user in the form of an unsafe impl of cxx::ExternType:
446 //
447 // unsafe impl ExternType for MyType {
448 // type Id = cxx::type_id!("...");
449 // type Kind = cxx::kind::Trivial;
450 // }
451 //
452 // Since the user went on the record with their unsafe impl to unsafely
453 // claim they KNOW that the type is trivial, it's fine for that to be on
454 // them if that were wrong.
455 //
456 // There may be a legitimate reason we'll want to remove these assertions
457 // for support of types that the programmer knows are Rust-movable despite
458 // not being recognized as such by the C++ type system due to a move
459 // constructor or destructor.
460
Adrian Taylorc8713432020-10-21 18:20:55 -0700461 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700462 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700463 writeln!(out, "static_assert(");
464 writeln!(
465 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700466 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700467 id,
468 );
469 writeln!(
470 out,
471 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
472 id,
473 );
474 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700475 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700476 writeln!(
477 out,
478 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
479 id,
480 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700481}
482
Adrian Taylorc8713432020-10-21 18:20:55 -0700483fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700484 let mut has_cxx_throws = false;
485 for api in apis {
486 if let Api::CxxFunction(efn) = api {
487 if efn.throws {
488 has_cxx_throws = true;
489 break;
490 }
491 }
492 }
493
494 if has_cxx_throws {
495 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700496 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700497 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700498 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700499 );
500 }
501}
502
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700503fn write_cxx_function_shim(
504 out: &mut OutFile,
505 efn: &ExternFn,
506 types: &Types,
507 impl_annotations: &Option<String>,
508) {
509 if !out.header {
510 if let Some(annotation) = impl_annotations {
511 write!(out, "{} ", annotation);
512 }
513 }
David Tolnayebef4a22020-03-17 15:33:47 -0700514 if efn.throws {
515 write!(out, "::rust::Str::Repr ");
516 } else {
David Tolnay99642622020-03-25 13:07:35 -0700517 write_extern_return_type_space(out, &efn.ret, types);
David Tolnayebef4a22020-03-17 15:33:47 -0700518 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700519 let mangled = mangle::extern_fn(efn, types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700520 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700521 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700522 if receiver.mutability.is_none() {
523 write!(out, "const ");
524 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700525 write!(
526 out,
527 "{} &self",
528 types.resolve(&receiver.ty).to_fully_qualified()
529 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700530 }
David Tolnay7db73692019-10-20 14:51:12 -0400531 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700532 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400533 write!(out, ", ");
534 }
David Tolnaya46a2372020-03-06 10:03:48 -0800535 if arg.ty == RustString {
536 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700537 } else if let Type::RustVec(_) = arg.ty {
538 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800539 }
David Tolnay7db73692019-10-20 14:51:12 -0400540 write_extern_arg(out, arg, types);
541 }
David Tolnay277e3cc2020-03-17 00:11:01 -0700542 let indirect_return = indirect_return(efn, types);
David Tolnay7db73692019-10-20 14:51:12 -0400543 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700544 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400545 write!(out, ", ");
546 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700547 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap(), types);
David Tolnay7db73692019-10-20 14:51:12 -0400548 write!(out, "*return$");
549 }
550 writeln!(out, ") noexcept {{");
551 write!(out, " ");
Adrian Taylorc8713432020-10-21 18:20:55 -0700552 write_return_type(out, &efn.ret, types);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700553 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700554 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700555 Some(receiver) => write!(
556 out,
557 "({}::*{}$)(",
558 types.resolve(&receiver.ty).to_fully_qualified(),
559 efn.ident.rust
560 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700561 }
David Tolnay7db73692019-10-20 14:51:12 -0400562 for (i, arg) in efn.args.iter().enumerate() {
563 if i > 0 {
564 write!(out, ", ");
565 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700566 write_type(out, &arg.ty, types);
David Tolnay7db73692019-10-20 14:51:12 -0400567 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700568 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700569 if let Some(receiver) = &efn.receiver {
570 if receiver.mutability.is_none() {
571 write!(out, " const");
572 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700573 }
574 write!(out, " = ");
575 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700576 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
577 Some(receiver) => write!(
578 out,
579 "&{}::{}",
580 types.resolve(&receiver.ty).to_fully_qualified(),
581 efn.ident.cxx.ident
582 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700583 }
584 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400585 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700586 if efn.throws {
587 writeln!(out, "::rust::Str::Repr throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700588 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700589 writeln!(out, " [&] {{");
590 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700591 }
David Tolnay7db73692019-10-20 14:51:12 -0400592 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700593 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400594 write!(out, "new (return$) ");
Adrian Taylorc8713432020-10-21 18:20:55 -0700595 write_indirect_return_type(out, efn.ret.as_ref().unwrap(), types);
David Tolnay7db73692019-10-20 14:51:12 -0400596 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700597 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400598 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700599 }
600 match &efn.ret {
601 Some(Type::Ref(_)) => write!(out, "&"),
602 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::Str::Repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700603 Some(Type::SliceRefU8(_)) if !indirect_return => {
604 write!(out, "::rust::Slice<uint8_t>::Repr(")
605 }
David Tolnay99642622020-03-25 13:07:35 -0700606 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400607 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700608 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700609 None => write!(out, "{}$(", efn.ident.rust),
610 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700611 }
David Tolnay7db73692019-10-20 14:51:12 -0400612 for (i, arg) in efn.args.iter().enumerate() {
613 if i > 0 {
614 write!(out, ", ");
615 }
616 if let Type::RustBox(_) = &arg.ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700617 write_type(out, &arg.ty, types);
David Tolnay7db73692019-10-20 14:51:12 -0400618 write!(out, "::from_raw({})", arg.ident);
619 } else if let Type::UniquePtr(_) = &arg.ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700620 write_type(out, &arg.ty, types);
David Tolnay7db73692019-10-20 14:51:12 -0400621 write!(out, "({})", arg.ident);
David Tolnaya46a2372020-03-06 10:03:48 -0800622 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800623 write!(
624 out,
625 "::rust::String(::rust::unsafe_bitcopy, *{})",
626 arg.ident,
627 );
David Tolnay313b10e2020-04-25 16:30:51 -0700628 } else if let Type::RustVec(_) = arg.ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700629 write_type(out, &arg.ty, types);
David Tolnay313b10e2020-04-25 16:30:51 -0700630 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400631 } else if types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700632 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800633 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400634 } else {
635 write!(out, "{}", arg.ident);
636 }
637 }
638 write!(out, ")");
639 match &efn.ret {
640 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
641 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700642 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400643 _ => {}
644 }
645 if indirect_return {
646 write!(out, ")");
647 }
648 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700649 if efn.throws {
650 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700651 writeln!(out, " throw$.ptr = nullptr;");
652 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700653 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700654 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700655 writeln!(
656 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700657 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700658 );
David Tolnay5d121442020-03-17 22:14:40 -0700659 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700660 writeln!(out, " return throw$;");
661 }
David Tolnay7db73692019-10-20 14:51:12 -0400662 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700663 for arg in &efn.args {
664 if let Type::Fn(f) = &arg.ty {
665 let var = &arg.ident;
666 write_function_pointer_trampoline(out, efn, var, f, types);
667 }
668 }
669}
670
671fn write_function_pointer_trampoline(
672 out: &mut OutFile,
673 efn: &ExternFn,
674 var: &Ident,
675 f: &Signature,
676 types: &Types,
677) {
678 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700679 let r_trampoline = mangle::r_trampoline(efn, var, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700680 let indirect_call = true;
681 write_rust_function_decl_impl(out, &r_trampoline, f, types, indirect_call);
682
683 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -0700684 let c_trampoline = mangle::c_trampoline(efn, var, types).to_string();
David Tolnay75dca2e2020-03-25 20:17:52 -0700685 write_rust_function_shim_impl(out, &c_trampoline, f, types, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400686}
687
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700688fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, types: &Types, _: &Option<String>) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700689 let link_name = mangle::extern_fn(efn, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700690 let indirect_call = false;
691 write_rust_function_decl_impl(out, &link_name, efn, types, indirect_call);
692}
693
694fn write_rust_function_decl_impl(
695 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700696 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700697 sig: &Signature,
698 types: &Types,
699 indirect_call: bool,
700) {
701 if sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700702 write!(out, "::rust::Str::Repr ");
703 } else {
David Tolnay75dca2e2020-03-25 20:17:52 -0700704 write_extern_return_type_space(out, &sig.ret, types);
David Tolnay1e548172020-03-16 13:37:09 -0700705 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700706 write!(out, "{}(", link_name);
707 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700708 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700709 if receiver.mutability.is_none() {
710 write!(out, "const ");
711 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700712 write!(
713 out,
714 "{} &self",
715 types.resolve(&receiver.ty).to_fully_qualified()
716 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700717 needs_comma = true;
718 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700719 for arg in &sig.args {
720 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400721 write!(out, ", ");
722 }
723 write_extern_arg(out, arg, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700724 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400725 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700726 if indirect_return(sig, types) {
727 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400728 write!(out, ", ");
729 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700730 write_return_type(out, &sig.ret, types);
David Tolnay7db73692019-10-20 14:51:12 -0400731 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700732 needs_comma = true;
733 }
734 if indirect_call {
735 if needs_comma {
736 write!(out, ", ");
737 }
738 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400739 }
740 writeln!(out, ") noexcept;");
741}
742
743fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400744 for line in efn.doc.to_string().lines() {
745 writeln!(out, "//{}", line);
746 }
David Tolnaya73853b2020-04-20 01:19:56 -0700747 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700748 None => efn.ident.cxx.ident.to_string(),
749 Some(receiver) => format!(
750 "{}::{}",
751 types.resolve(&receiver.ty).ident,
752 efn.ident.cxx.ident
753 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700754 };
Adrian Taylorc8713432020-10-21 18:20:55 -0700755 let invoke = mangle::extern_fn(efn, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700756 let indirect_call = false;
757 write_rust_function_shim_impl(out, &local_name, efn, types, &invoke, indirect_call);
758}
759
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700760fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700761 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700762 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700763 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700764 indirect_call: bool,
Adrian Taylorc8713432020-10-21 18:20:55 -0700765 types: &Types,
David Tolnay75dca2e2020-03-25 20:17:52 -0700766) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700767 write_return_type(out, &sig.ret, types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700768 write!(out, "{}(", local_name);
769 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400770 if i > 0 {
771 write!(out, ", ");
772 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700773 write_type_space(out, &arg.ty, types);
David Tolnay7db73692019-10-20 14:51:12 -0400774 write!(out, "{}", arg.ident);
775 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700776 if indirect_call {
777 if !sig.args.is_empty() {
778 write!(out, ", ");
779 }
780 write!(out, "void *extern$");
781 }
David Tolnay1e548172020-03-16 13:37:09 -0700782 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700783 if let Some(receiver) = &sig.receiver {
784 if receiver.mutability.is_none() {
785 write!(out, " const");
786 }
787 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700788 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700789 write!(out, " noexcept");
790 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700791}
792
793fn write_rust_function_shim_impl(
794 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700795 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700796 sig: &Signature,
797 types: &Types,
David Tolnay891061b2020-04-19 22:42:33 -0700798 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700799 indirect_call: bool,
800) {
801 if out.header && sig.receiver.is_some() {
802 // We've already defined this inside the struct.
803 return;
804 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700805 write_rust_function_shim_decl(out, local_name, sig, indirect_call, types);
David Tolnay7db73692019-10-20 14:51:12 -0400806 if out.header {
807 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700808 return;
David Tolnay7db73692019-10-20 14:51:12 -0400809 }
David Tolnay439cde22020-04-20 00:46:25 -0700810 writeln!(out, " {{");
811 for arg in &sig.args {
812 if arg.ty != RustString && types.needs_indirect_abi(&arg.ty) {
813 out.include.utility = true;
814 write!(out, " ::rust::ManuallyDrop<");
Adrian Taylorc8713432020-10-21 18:20:55 -0700815 write_type(out, &arg.ty, types);
David Tolnay439cde22020-04-20 00:46:25 -0700816 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
817 }
818 }
819 write!(out, " ");
820 let indirect_return = indirect_return(sig, types);
821 if indirect_return {
822 write!(out, "::rust::MaybeUninit<");
Adrian Taylorc8713432020-10-21 18:20:55 -0700823 write_type(out, sig.ret.as_ref().unwrap(), types);
David Tolnay439cde22020-04-20 00:46:25 -0700824 writeln!(out, "> return$;");
825 write!(out, " ");
826 } else if let Some(ret) = &sig.ret {
827 write!(out, "return ");
828 match ret {
829 Type::RustBox(_) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700830 write_type(out, ret, types);
David Tolnay439cde22020-04-20 00:46:25 -0700831 write!(out, "::from_raw(");
832 }
833 Type::UniquePtr(_) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700834 write_type(out, ret, types);
David Tolnay439cde22020-04-20 00:46:25 -0700835 write!(out, "(");
836 }
837 Type::Ref(_) => write!(out, "*"),
838 _ => {}
839 }
840 }
841 if sig.throws {
842 write!(out, "::rust::Str::Repr error$ = ");
843 }
844 write!(out, "{}(", invoke);
845 if sig.receiver.is_some() {
846 write!(out, "*this");
847 }
848 for (i, arg) in sig.args.iter().enumerate() {
849 if i > 0 || sig.receiver.is_some() {
850 write!(out, ", ");
851 }
852 match &arg.ty {
853 Type::Str(_) => write!(out, "::rust::Str::Repr("),
854 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
855 ty if types.needs_indirect_abi(ty) => write!(out, "&"),
856 _ => {}
857 }
858 write!(out, "{}", arg.ident);
859 match &arg.ty {
860 Type::RustBox(_) => write!(out, ".into_raw()"),
861 Type::UniquePtr(_) => write!(out, ".release()"),
862 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
863 ty if ty != RustString && types.needs_indirect_abi(ty) => write!(out, "$.value"),
864 _ => {}
865 }
866 }
867 if indirect_return {
868 if !sig.args.is_empty() {
869 write!(out, ", ");
870 }
871 write!(out, "&return$.value");
872 }
873 if indirect_call {
874 if !sig.args.is_empty() || indirect_return {
875 write!(out, ", ");
876 }
877 write!(out, "extern$");
878 }
879 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400880 if !indirect_return {
881 if let Some(ret) = &sig.ret {
882 if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
883 write!(out, ")");
884 }
David Tolnay439cde22020-04-20 00:46:25 -0700885 }
886 }
887 writeln!(out, ";");
888 if sig.throws {
889 writeln!(out, " if (error$.ptr) {{");
890 writeln!(out, " throw ::rust::Error(error$);");
891 writeln!(out, " }}");
892 }
893 if indirect_return {
894 out.include.utility = true;
895 writeln!(out, " return ::std::move(return$.value);");
896 }
897 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400898}
899
Adrian Taylorc8713432020-10-21 18:20:55 -0700900fn write_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400901 match ty {
902 None => write!(out, "void "),
Adrian Taylorc8713432020-10-21 18:20:55 -0700903 Some(ty) => write_type_space(out, ty, types),
David Tolnay7db73692019-10-20 14:51:12 -0400904 }
905}
906
David Tolnay75dca2e2020-03-25 20:17:52 -0700907fn indirect_return(sig: &Signature, types: &Types) -> bool {
908 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700909 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700910 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700911}
912
Adrian Taylorc8713432020-10-21 18:20:55 -0700913fn write_indirect_return_type(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay99642622020-03-25 13:07:35 -0700914 match ty {
915 Type::RustBox(ty) | Type::UniquePtr(ty) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700916 write_type_space(out, &ty.inner, types);
David Tolnay99642622020-03-25 13:07:35 -0700917 write!(out, "*");
918 }
919 Type::Ref(ty) => {
920 if ty.mutability.is_none() {
921 write!(out, "const ");
922 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700923 write_type(out, &ty.inner, types);
David Tolnay99642622020-03-25 13:07:35 -0700924 write!(out, " *");
925 }
926 Type::Str(_) => write!(out, "::rust::Str::Repr"),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700927 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
Adrian Taylorc8713432020-10-21 18:20:55 -0700928 _ => write_type(out, ty, types),
David Tolnay99642622020-03-25 13:07:35 -0700929 }
930}
931
Adrian Taylorc8713432020-10-21 18:20:55 -0700932fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type, types: &Types) {
933 write_indirect_return_type(out, ty, types);
David Tolnay99642622020-03-25 13:07:35 -0700934 match ty {
935 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700936 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700937 _ => write_space_after_type(out, ty),
938 }
939}
940
941fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400942 match ty {
943 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700944 write_type_space(out, &ty.inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400945 write!(out, "*");
946 }
David Tolnay4a441222020-01-25 16:24:27 -0800947 Some(Type::Ref(ty)) => {
948 if ty.mutability.is_none() {
949 write!(out, "const ");
950 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700951 write_type(out, &ty.inner, types);
David Tolnay4a441222020-01-25 16:24:27 -0800952 write!(out, " *");
953 }
David Tolnay750755e2020-03-01 13:04:08 -0800954 Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700955 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnay7db73692019-10-20 14:51:12 -0400956 Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
Adrian Taylorc8713432020-10-21 18:20:55 -0700957 _ => write_return_type(out, ty, types),
David Tolnay7db73692019-10-20 14:51:12 -0400958 }
959}
960
961fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
962 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700963 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700964 write_type_space(out, &ty.inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400965 write!(out, "*");
966 }
David Tolnay750755e2020-03-01 13:04:08 -0800967 Type::Str(_) => write!(out, "::rust::Str::Repr "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700968 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
Adrian Taylorc8713432020-10-21 18:20:55 -0700969 _ => write_type_space(out, &arg.ty, types),
David Tolnay7db73692019-10-20 14:51:12 -0400970 }
971 if types.needs_indirect_abi(&arg.ty) {
972 write!(out, "*");
973 }
974 write!(out, "{}", arg.ident);
975}
976
Adrian Taylorc8713432020-10-21 18:20:55 -0700977fn write_type(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -0400978 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700979 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700980 Some(atom) => write_atom(out, atom),
Adrian Taylorc8713432020-10-21 18:20:55 -0700981 None => write!(out, "{}", types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -0400982 },
983 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800984 write!(out, "::rust::Box<");
Adrian Taylorc8713432020-10-21 18:20:55 -0700985 write_type(out, &ty.inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400986 write!(out, ">");
987 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700988 Type::RustVec(ty) => {
989 write!(out, "::rust::Vec<");
Adrian Taylorc8713432020-10-21 18:20:55 -0700990 write_type(out, &ty.inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +0700991 write!(out, ">");
992 }
David Tolnay7db73692019-10-20 14:51:12 -0400993 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800994 write!(out, "::std::unique_ptr<");
Adrian Taylorc8713432020-10-21 18:20:55 -0700995 write_type(out, &ptr.inner, types);
David Tolnay7db73692019-10-20 14:51:12 -0400996 write!(out, ">");
997 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700998 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700999 write!(out, "::std::vector<");
Adrian Taylorc8713432020-10-21 18:20:55 -07001000 write_type(out, &ty.inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001001 write!(out, ">");
1002 }
David Tolnay7db73692019-10-20 14:51:12 -04001003 Type::Ref(r) => {
1004 if r.mutability.is_none() {
1005 write!(out, "const ");
1006 }
Adrian Taylorc8713432020-10-21 18:20:55 -07001007 write_type(out, &r.inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001008 write!(out, " &");
1009 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001010 Type::Slice(_) => {
1011 // For now, only U8 slices are supported, which are covered separately below
1012 unreachable!()
1013 }
David Tolnay7db73692019-10-20 14:51:12 -04001014 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -08001015 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -04001016 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001017 Type::SliceRefU8(_) => {
1018 write!(out, "::rust::Slice<uint8_t>");
1019 }
David Tolnay75dca2e2020-03-25 20:17:52 -07001020 Type::Fn(f) => {
1021 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
1022 match &f.ret {
Adrian Taylorc8713432020-10-21 18:20:55 -07001023 Some(ret) => write_type(out, ret, types),
David Tolnay75dca2e2020-03-25 20:17:52 -07001024 None => write!(out, "void"),
1025 }
1026 write!(out, "(");
1027 for (i, arg) in f.args.iter().enumerate() {
1028 if i > 0 {
1029 write!(out, ", ");
1030 }
Adrian Taylorc8713432020-10-21 18:20:55 -07001031 write_type(out, &arg.ty, types);
David Tolnay75dca2e2020-03-25 20:17:52 -07001032 }
1033 write!(out, ")>");
1034 }
David Tolnay2fb14e92020-03-15 23:11:38 -07001035 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001036 }
1037}
1038
David Tolnayf6a89f22020-05-10 23:39:27 -07001039fn write_atom(out: &mut OutFile, atom: Atom) {
1040 match atom {
1041 Bool => write!(out, "bool"),
1042 U8 => write!(out, "uint8_t"),
1043 U16 => write!(out, "uint16_t"),
1044 U32 => write!(out, "uint32_t"),
1045 U64 => write!(out, "uint64_t"),
1046 Usize => write!(out, "size_t"),
1047 I8 => write!(out, "int8_t"),
1048 I16 => write!(out, "int16_t"),
1049 I32 => write!(out, "int32_t"),
1050 I64 => write!(out, "int64_t"),
1051 Isize => write!(out, "::rust::isize"),
1052 F32 => write!(out, "float"),
1053 F64 => write!(out, "double"),
1054 CxxString => write!(out, "::std::string"),
1055 RustString => write!(out, "::rust::String"),
1056 }
1057}
1058
Adrian Taylorc8713432020-10-21 18:20:55 -07001059fn write_type_space(out: &mut OutFile, ty: &Type, types: &Types) {
1060 write_type(out, ty, types);
David Tolnay99642622020-03-25 13:07:35 -07001061 write_space_after_type(out, ty);
1062}
1063
1064fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -04001065 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -07001066 Type::Ident(_)
1067 | Type::RustBox(_)
1068 | Type::UniquePtr(_)
1069 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -07001070 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +07001071 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -07001072 | Type::SliceRefU8(_)
1073 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -04001074 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -07001075 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -04001076 }
1077}
1078
David Tolnaycd08c442020-04-25 10:16:33 -07001079// Only called for legal referent types of unique_ptr and element types of
1080// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001081fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -07001082 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001083 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
1084 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -07001085 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -07001086 }
1087}
1088
David Tolnayacdf20a2020-04-25 12:40:53 -07001089// Only called for legal referent types of unique_ptr and element types of
1090// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -07001091fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -07001092 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -07001093 Type::Ident(ident) => ident.to_symbol(types),
1094 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -07001095 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -07001096 }
1097}
1098
David Tolnay7db73692019-10-20 14:51:12 -04001099fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
David Tolnay7db73692019-10-20 14:51:12 -04001100 out.begin_block("extern \"C\"");
1101 for ty in types {
1102 if let Type::RustBox(ty) = ty {
1103 if let Type::Ident(inner) = &ty.inner {
1104 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -07001105 write_rust_box_extern(out, &types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001106 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001107 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001108 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001109 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -07001110 out.next_section();
Adrian Taylorc8713432020-10-21 18:20:55 -07001111 write_rust_vec_extern(out, inner, types);
David Tolnay6787be62020-04-25 11:01:02 -07001112 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001113 }
David Tolnay7db73692019-10-20 14:51:12 -04001114 } else if let Type::UniquePtr(ptr) = ty {
1115 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001116 if Atom::from(&inner.rust).is_none()
1117 && (!types.aliases.contains_key(&inner.rust)
1118 || types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001119 {
David Tolnay7db73692019-10-20 14:51:12 -04001120 out.next_section();
David Tolnay63da4d32020-04-25 09:41:12 -07001121 write_unique_ptr(out, inner, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001122 }
1123 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001124 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001125 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001126 if Atom::from(&inner.rust).is_none()
1127 && (!types.aliases.contains_key(&inner.rust)
1128 || types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001129 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001130 out.next_section();
David Tolnay92105da2020-04-25 11:15:31 -07001131 write_cxx_vector(out, ty, inner, types);
David Tolnay7db73692019-10-20 14:51:12 -04001132 }
1133 }
1134 }
1135 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001136 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001137
David Tolnay750755e2020-03-01 13:04:08 -08001138 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001139 out.begin_block("inline namespace cxxbridge05");
David Tolnay7db73692019-10-20 14:51:12 -04001140 for ty in types {
1141 if let Type::RustBox(ty) = ty {
1142 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001143 write_rust_box_impl(out, &types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001144 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001145 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001146 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001147 if Atom::from(&inner.rust).is_none() {
1148 write_rust_vec_impl(out, inner, types);
David Tolnay6787be62020-04-25 11:01:02 -07001149 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001150 }
David Tolnay7db73692019-10-20 14:51:12 -04001151 }
1152 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001153 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001154 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001155}
1156
Adrian Taylorc8713432020-10-21 18:20:55 -07001157fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1158 let inner = ident.to_fully_qualified();
1159 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001160
David Tolnay8f16ae72020-10-08 18:21:13 -07001161 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1162 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001163 writeln!(
1164 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001165 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001166 instance, inner,
1167 );
1168 writeln!(
1169 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001170 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001171 instance, inner,
1172 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001173 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001174}
1175
Adrian Taylorc8713432020-10-21 18:20:55 -07001176fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName, types: &Types) {
David Tolnay6787be62020-04-25 11:01:02 -07001177 let element = Type::Ident(element.clone());
Adrian Taylorc8713432020-10-21 18:20:55 -07001178 let inner = to_typename(&element, types);
1179 let instance = to_mangled(&element, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001180
David Tolnay8f16ae72020-10-08 18:21:13 -07001181 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1182 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001183 writeln!(
1184 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001185 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001186 instance, inner,
1187 );
1188 writeln!(
1189 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001190 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001191 instance, inner,
1192 );
1193 writeln!(
1194 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001195 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001196 instance, inner,
1197 );
David Tolnay219c0792020-04-24 20:31:37 -07001198 writeln!(
1199 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001200 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001201 inner, instance,
1202 );
David Tolnay503d0192020-04-24 22:18:56 -07001203 writeln!(
1204 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001205 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001206 instance,
1207 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001208 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001209}
1210
Adrian Taylorc8713432020-10-21 18:20:55 -07001211fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1212 let inner = ident.to_fully_qualified();
1213 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001214
1215 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001216 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001217 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001218 writeln!(out, "}}");
1219
1220 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001221 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001222 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001223 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001224}
1225
Adrian Taylorc8713432020-10-21 18:20:55 -07001226fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName, types: &Types) {
David Tolnay6787be62020-04-25 11:01:02 -07001227 let element = Type::Ident(element.clone());
Adrian Taylorc8713432020-10-21 18:20:55 -07001228 let inner = to_typename(&element, types);
1229 let instance = to_mangled(&element, types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001230
Myron Ahneba35cf2020-02-05 19:41:51 +07001231 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001232 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001233 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001234 writeln!(out, "}}");
1235
1236 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001237 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1238 writeln!(
1239 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001240 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001241 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001242 );
1243 writeln!(out, "}}");
1244
1245 writeln!(out, "template <>");
1246 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001247 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001248 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001249
1250 writeln!(out, "template <>");
1251 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1252 writeln!(
1253 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001254 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001255 instance,
1256 );
1257 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001258
1259 writeln!(out, "template <>");
1260 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001261 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001262 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001263}
1264
Adrian Taylorc8713432020-10-21 18:20:55 -07001265fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001266 let ty = Type::Ident(ident.clone());
Adrian Taylorc8713432020-10-21 18:20:55 -07001267 let instance = to_mangled(&ty, types);
David Tolnay63da4d32020-04-25 09:41:12 -07001268
David Tolnay8f16ae72020-10-08 18:21:13 -07001269 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1270 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001271
1272 write_unique_ptr_common(out, &ty, types);
1273
David Tolnay8f16ae72020-10-08 18:21:13 -07001274 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001275}
1276
1277// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
1278fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001279 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001280 out.include.utility = true;
Adrian Taylorc8713432020-10-21 18:20:55 -07001281 let inner = to_typename(ty, types);
1282 let instance = to_mangled(ty, types);
David Tolnay7db73692019-10-20 14:51:12 -04001283
David Tolnay63da4d32020-04-25 09:41:12 -07001284 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001285 // Some aliases are to opaque types; some are to trivial types. We can't
1286 // know at code generation time, so we generate both C++ and Rust side
1287 // bindings for a "new" method anyway. But the Rust code can't be called
1288 // for Opaque types because the 'new' method is not implemented.
1289 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -07001290 types.structs.contains_key(&ident.rust) || types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001291 }
David Tolnay63da4d32020-04-25 09:41:12 -07001292 _ => false,
1293 };
1294
David Tolnay7db73692019-10-20 14:51:12 -04001295 writeln!(
1296 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001297 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001298 inner,
1299 );
1300 writeln!(
1301 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001302 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001303 inner,
1304 );
1305 writeln!(
1306 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001307 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001308 instance, inner,
1309 );
David Tolnay7e219b82020-03-01 13:14:51 -08001310 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001311 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001312 if can_construct_from_value {
1313 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001314 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001315 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001316 instance, inner, inner,
1317 );
David Tolnay63da4d32020-04-25 09:41:12 -07001318 writeln!(
1319 out,
1320 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1321 inner, inner,
1322 );
1323 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001324 }
David Tolnay7db73692019-10-20 14:51:12 -04001325 writeln!(
1326 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001327 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001328 instance, inner, inner,
1329 );
David Tolnay7e219b82020-03-01 13:14:51 -08001330 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001331 writeln!(out, "}}");
1332 writeln!(
1333 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001334 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001335 inner, instance, inner,
1336 );
1337 writeln!(out, " return ptr.get();");
1338 writeln!(out, "}}");
1339 writeln!(
1340 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001341 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001342 inner, instance, inner,
1343 );
1344 writeln!(out, " return ptr.release();");
1345 writeln!(out, "}}");
1346 writeln!(
1347 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001348 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001349 instance, inner,
1350 );
1351 writeln!(out, " ptr->~unique_ptr();");
1352 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001353}
Myron Ahneba35cf2020-02-05 19:41:51 +07001354
Adrian Taylorc8713432020-10-21 18:20:55 -07001355fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName, types: &Types) {
David Tolnay63da4d32020-04-25 09:41:12 -07001356 let element = Type::Ident(element.clone());
Adrian Taylorc8713432020-10-21 18:20:55 -07001357 let inner = to_typename(&element, types);
1358 let instance = to_mangled(&element, types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001359
David Tolnay8f16ae72020-10-08 18:21:13 -07001360 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1361 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001362 writeln!(
1363 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001364 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001365 instance, inner,
1366 );
1367 writeln!(out, " return s.size();");
1368 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001369 writeln!(
1370 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001371 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001372 inner, instance, inner,
1373 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001374 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001375 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001376
1377 write_unique_ptr_common(out, vector_ty, types);
1378
David Tolnay8f16ae72020-10-08 18:21:13 -07001379 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001380}