blob: 5dcf91ca7b7490b722171b1f7cc517f239ad1f90 [file] [log] [blame]
Adrian Taylor565ddf02020-10-29 21:12:36 -07001use crate::gen::namespace_organizer::NamespaceEntries;
David Tolnay8810a542020-10-31 21:39:22 -07002use crate::gen::out::OutFile;
David Tolnay3374d8d2020-10-31 22:18:45 -07003use crate::gen::{builtin, 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::{
David Tolnay2f3e90b2020-10-31 22:16:51 -07007 mangle, Api, CppName, Enum, ExternFn, ExternType, ResolvableName, Signature, Struct, Type,
8 Types, Var,
Adrian Taylorc8713432020-10-21 18:20:55 -07009};
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 {
David Tolnay8810a542020-10-31 21:39:22 -070018 writeln!(out.include, "#pragma once");
David Tolnay54702b92020-07-31 11:50:09 -070019 }
20
David Tolnaye629c642020-10-31 22:02:09 -070021 pick_includes_and_builtins(out, apis);
David Tolnay4aae7c02020-10-28 12:35:42 -070022 out.include.extend(&opt.include);
David Tolnay7db73692019-10-20 14:51:12 -040023
Adrian Taylor565ddf02020-10-29 21:12:36 -070024 let apis_by_namespace = NamespaceEntries::new(apis);
Adrian Taylorc8713432020-10-21 18:20:55 -070025
David Tolnay04b81652020-10-31 22:43:25 -070026 gen_namespace_forward_declarations(out, &apis_by_namespace);
27 gen_namespace_contents(out, &apis_by_namespace, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -070028
29 if !header {
30 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -070031 write_generic_instantiations(out);
David Tolnay7db73692019-10-20 14:51:12 -040032 }
33
David Tolnay3374d8d2020-10-31 22:18:45 -070034 builtin::write(out);
David Tolnay2f3e90b2020-10-31 22:16:51 -070035 include::write(out);
Adrian Taylorc8713432020-10-21 18:20:55 -070036
37 out_file
38}
39
David Tolnay04b81652020-10-31 22:43:25 -070040fn gen_namespace_forward_declarations(out: &mut OutFile, ns_entries: &NamespaceEntries) {
Adrian Taylor565ddf02020-10-29 21:12:36 -070041 let apis = ns_entries.entries();
Adrian Taylorc8713432020-10-21 18:20:55 -070042
David Tolnay7db73692019-10-20 14:51:12 -040043 out.next_section();
David Tolnay630af882020-10-31 22:03:47 -070044 for api in apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 match api {
David Tolnay880d1f82020-10-31 22:05:24 -070046 Api::Include(include) => out.include.insert(include),
Adrian Taylorc8713432020-10-21 18:20:55 -070047 Api::Struct(strct) => write_struct_decl(out, &strct.ident.cxx.ident),
48 Api::CxxType(ety) => write_struct_using(out, &ety.ident.cxx),
49 Api::RustType(ety) => write_struct_decl(out, &ety.ident.cxx.ident),
David Tolnay7c295462020-04-25 12:45:07 -070050 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 }
53
Adrian Taylorf9213622020-10-31 22:25:42 -070054 out.next_section();
55
56 for (child_ns, child_ns_entries) in ns_entries.children() {
57 writeln!(out, "namespace {} {{", child_ns);
David Tolnayef0473a2020-10-31 22:44:52 -070058 gen_namespace_forward_declarations(out, child_ns_entries);
Adrian Taylorf9213622020-10-31 22:25:42 -070059 writeln!(out, "}} // namespace {}", child_ns);
60 }
61}
62
David Tolnay04b81652020-10-31 22:43:25 -070063fn gen_namespace_contents(out: &mut OutFile, ns_entries: &NamespaceEntries, opt: &Opt) {
Adrian Taylorf9213622020-10-31 22:25:42 -070064 let apis = ns_entries.entries();
65
David Tolnayf94bef12020-04-17 14:46:42 -070066 let mut methods_for_type = HashMap::new();
David Tolnay630af882020-10-31 22:03:47 -070067 for api in apis {
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();
David Tolnay4d148422020-10-31 22:41:37 -070082 if !out.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();
David Tolnay4d148422020-10-31 22:41:37 -070088 if out.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 {
David Tolnay4d148422020-10-31 22:41:37 -0700107 if out.types.required_trivial.contains_key(&ety.ident.rust) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700108 check_trivial_extern_type(out, &ety.ident.cxx)
David Tolnayfabca772020-10-03 21:25:41 -0700109 }
110 }
111 }
112
David Tolnayce5a91f2020-10-31 22:42:08 -0700113 if !out.header {
David Tolnay7db73692019-10-20 14:51:12 -0400114 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);
David Tolnayef0473a2020-10-31 22:44:52 -0700139 gen_namespace_contents(out, child_ns_entries, opt);
Adrian Taylorc8713432020-10-21 18:20:55 -0700140 writeln!(out, "}} // namespace {}", child_ns);
David Tolnay7db73692019-10-20 14:51:12 -0400141 }
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
David Tolnay528200f2020-10-31 21:02:22 -0700144fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700145 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,
David Tolnayb9da1462020-10-31 21:03:41 -0700151 Some(Isize) => {
152 out.include.basetsd = true;
153 out.builtin.rust_isize = true;
154 }
David Tolnay89e386d2020-10-03 19:02:19 -0700155 Some(CxxString) => out.include.string = true,
David Tolnayb9da1462020-10-31 21:03:41 -0700156 Some(RustString) => {
157 out.include.array = true;
158 out.include.cstdint = true;
159 out.include.string = true;
160 out.builtin.rust_string = true;
161 }
162 Some(Bool) | Some(F32) | Some(F64) | None => {}
David Tolnay89e386d2020-10-03 19:02:19 -0700163 },
David Tolnayb7a7cb62020-03-17 21:18:40 -0700164 Type::RustBox(_) => {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700165 out.include.new = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700166 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700167 out.builtin.rust_box = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700168 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 Type::RustVec(_) => {
David Tolnay9c6bf2d2020-04-24 15:27:07 -0700170 out.include.array = true;
David Tolnay0ecd05a2020-07-29 16:32:03 -0700171 out.include.new = true;
David Tolnayc87c2152020-04-24 17:07:41 -0700172 out.include.type_traits = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700173 out.builtin.panic = true;
174 out.builtin.rust_vec = true;
175 out.builtin.unsafe_bitcopy = true;
Myron Ahneba35cf2020-02-05 19:41:51 +0700176 }
David Tolnayb9da1462020-10-31 21:03:41 -0700177 Type::UniquePtr(_) => out.include.memory = true,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700178 Type::Str(_) => {
179 out.include.cstdint = true;
180 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700181 out.builtin.rust_str = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700182 }
David Tolnayb9da1462020-10-31 21:03:41 -0700183 Type::CxxVector(_) => out.include.vector = true,
David Tolnay75dca2e2020-03-25 20:17:52 -0700184 Type::Fn(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700185 out.builtin.rust_fn = true;
David Tolnay75dca2e2020-03-25 20:17:52 -0700186 }
David Tolnayb9da1462020-10-31 21:03:41 -0700187 Type::Slice(_) => {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700188 out.builtin.rust_slice = true;
David Tolnay4770b472020-04-14 16:32:59 -0700189 }
David Tolnayb9da1462020-10-31 21:03:41 -0700190 Type::SliceRefU8(_) => {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700191 out.include.cstdint = true;
David Tolnayb9da1462020-10-31 21:03:41 -0700192 out.builtin.rust_slice = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700193 }
194 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400195 }
196 }
197
David Tolnay09011c32020-03-06 14:40:28 -0800198 for api in apis {
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199 match api {
200 Api::CxxFunction(efn) if !out.header => {
David Tolnay5d121442020-03-17 22:14:40 -0700201 if efn.throws {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700202 out.builtin.trycatch = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700203 } else if let Some(Type::Str(_)) = efn.ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700204 out.builtin.rust_str_repr = true;
David Tolnay5d121442020-03-17 22:14:40 -0700205 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700206 for arg in &efn.args {
David Tolnaya4eb9432020-10-31 20:32:19 -0700207 match arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700208 Type::Str(_) => out.builtin.rust_str_new_unchecked = true,
209 Type::RustVec(_) => out.builtin.unsafe_bitcopy = true,
210 _ => out.builtin.unsafe_bitcopy |= arg.ty == RustString,
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211 }
David Tolnay09011c32020-03-06 14:40:28 -0800212 }
213 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700214 Api::RustFunction(efn) if !out.header => {
215 if efn.throws {
216 out.include.exception = true;
David Tolnayc8870b82020-09-09 08:44:33 -0700217 out.include.string = true;
David Tolnay3be0e1f2020-10-31 20:53:00 -0700218 out.builtin.rust_str = true;
219 out.builtin.rust_error = true;
220 out.builtin.maybe_uninit = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700221 }
222 for arg in &efn.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700223 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700224 out.builtin.manually_drop = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700225 }
226 if let Type::Str(_) = arg.ty {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700227 out.builtin.rust_str_repr = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700228 }
229 }
230 if let Some(ret) = &efn.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700231 if out.types.needs_indirect_abi(ret) {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700232 out.builtin.maybe_uninit = true;
David Tolnaya4eb9432020-10-31 20:32:19 -0700233 } else if let Type::Str(_) = ret {
David Tolnay3be0e1f2020-10-31 20:53:00 -0700234 out.builtin.rust_str_new_unchecked = true;
David Tolnayb7a7cb62020-03-17 21:18:40 -0700235 }
David Tolnayf51447e2020-03-06 14:14:27 -0800236 }
237 }
David Tolnayb7a7cb62020-03-17 21:18:40 -0700238 _ => {}
David Tolnayf51447e2020-03-06 14:14:27 -0800239 }
240 }
David Tolnayec66d112020-10-31 21:00:26 -0700241}
David Tolnayf51447e2020-03-06 14:14:27 -0800242
David Tolnaya7c2ea12020-10-30 21:32:53 -0700243fn write_struct(out: &mut OutFile, strct: &Struct) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700244 let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700245 writeln!(out, "#ifndef {}", guard);
246 writeln!(out, "#define {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400247 for line in strct.doc.to_string().lines() {
248 writeln!(out, "//{}", line);
249 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700250 writeln!(out, "struct {} final {{", strct.ident.cxx.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400251 for field in &strct.fields {
252 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700253 write_type_space(out, &field.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400254 writeln!(out, "{};", field.ident);
255 }
256 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700257 writeln!(out, "#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -0400258}
259
260fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
261 writeln!(out, "struct {};", ident);
262}
263
Adrian Taylorc8713432020-10-21 18:20:55 -0700264fn write_struct_using(out: &mut OutFile, ident: &CppName) {
265 writeln!(
266 out,
267 "using {} = {};",
268 ident.ident,
269 ident.to_fully_qualified()
270 );
David Tolnay8861bee2020-01-20 18:39:24 -0800271}
272
David Tolnaya7c2ea12020-10-30 21:32:53 -0700273fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700274 let guard = format!("CXXBRIDGE05_STRUCT_{}", ety.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700275 writeln!(out, "#ifndef {}", guard);
276 writeln!(out, "#define {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700277 for line in ety.doc.to_string().lines() {
278 writeln!(out, "//{}", line);
279 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700280 writeln!(out, "struct {} final {{", ety.ident.cxx.ident);
281 writeln!(out, " {}() = delete;", ety.ident.cxx.ident);
282 writeln!(
283 out,
284 " {}(const {} &) = delete;",
285 ety.ident.cxx.ident, ety.ident.cxx.ident
286 );
Joel Galenson968738f2020-04-15 14:19:33 -0700287 for method in methods {
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700288 write!(out, " ");
289 let sig = &method.sig;
Adrian Taylorc8713432020-10-21 18:20:55 -0700290 let local_name = method.ident.cxx.ident.to_string();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700291 write_rust_function_shim_decl(out, &local_name, sig, false);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700292 writeln!(out, ";");
293 }
294 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700295 writeln!(out, "#endif // {}", guard);
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700296}
297
Joel Galensonc03402a2020-04-23 17:31:09 -0700298fn write_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700299 let guard = format!("CXXBRIDGE05_ENUM_{}", enm.ident.cxx.to_symbol());
David Tolnaya25ea9c2020-08-27 22:59:38 -0700300 writeln!(out, "#ifndef {}", guard);
301 writeln!(out, "#define {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700302 for line in enm.doc.to_string().lines() {
303 writeln!(out, "//{}", line);
304 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700305 write!(out, "enum class {} : ", enm.ident.cxx.ident);
David Tolnayf6a89f22020-05-10 23:39:27 -0700306 write_atom(out, enm.repr);
307 writeln!(out, " {{");
Joel Galensonc03402a2020-04-23 17:31:09 -0700308 for variant in &enm.variants {
Joel Galenson88547732020-05-05 08:23:42 -0700309 writeln!(out, " {} = {},", variant.ident, variant.discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700310 }
311 writeln!(out, "}};");
David Tolnaya25ea9c2020-08-27 22:59:38 -0700312 writeln!(out, "#endif // {}", guard);
Joel Galensonc03402a2020-04-23 17:31:09 -0700313}
314
Joel Galenson905eb2e2020-05-04 14:58:14 -0700315fn check_enum(out: &mut OutFile, enm: &Enum) {
Adrian Taylorc8713432020-10-21 18:20:55 -0700316 write!(
317 out,
318 "static_assert(sizeof({}) == sizeof(",
319 enm.ident.cxx.ident
320 );
David Tolnayf6a89f22020-05-10 23:39:27 -0700321 write_atom(out, enm.repr);
322 writeln!(out, "), \"incorrect size\");");
Joel Galenson0f654ff2020-05-04 20:04:21 -0700323 for variant in &enm.variants {
David Tolnayf6a89f22020-05-10 23:39:27 -0700324 write!(out, "static_assert(static_cast<");
325 write_atom(out, enm.repr);
Joel Galenson0f654ff2020-05-04 20:04:21 -0700326 writeln!(
327 out,
David Tolnayf6a89f22020-05-10 23:39:27 -0700328 ">({}::{}) == {}, \"disagrees with the value in #[cxx::bridge]\");",
Adrian Taylorc8713432020-10-21 18:20:55 -0700329 enm.ident.cxx.ident, variant.ident, variant.discriminant,
Joel Galenson0f654ff2020-05-04 20:04:21 -0700330 );
Joel Galenson0f654ff2020-05-04 20:04:21 -0700331 }
Joel Galenson905eb2e2020-05-04 14:58:14 -0700332}
333
Adrian Taylorc8713432020-10-21 18:20:55 -0700334fn check_trivial_extern_type(out: &mut OutFile, id: &CppName) {
David Tolnayfd0034e2020-10-04 13:15:34 -0700335 // NOTE: The following two static assertions are just nice-to-have and not
336 // necessary for soundness. That's because triviality is always declared by
337 // the user in the form of an unsafe impl of cxx::ExternType:
338 //
339 // unsafe impl ExternType for MyType {
340 // type Id = cxx::type_id!("...");
341 // type Kind = cxx::kind::Trivial;
342 // }
343 //
344 // Since the user went on the record with their unsafe impl to unsafely
345 // claim they KNOW that the type is trivial, it's fine for that to be on
346 // them if that were wrong.
347 //
348 // There may be a legitimate reason we'll want to remove these assertions
349 // for support of types that the programmer knows are Rust-movable despite
350 // not being recognized as such by the C++ type system due to a move
351 // constructor or destructor.
352
Adrian Taylorc8713432020-10-21 18:20:55 -0700353 let id = &id.to_fully_qualified();
David Tolnayf57f7562020-10-04 19:56:26 -0700354 out.include.type_traits = true;
David Tolnay7426cc12020-10-03 19:04:04 -0700355 writeln!(out, "static_assert(");
356 writeln!(
357 out,
David Tolnaycc44c7a2020-10-04 13:20:03 -0700358 " ::std::is_trivially_move_constructible<{}>::value,",
David Tolnay7426cc12020-10-03 19:04:04 -0700359 id,
360 );
361 writeln!(
362 out,
363 " \"type {} marked as Trivial in Rust is not trivially move constructible in C++\");",
364 id,
365 );
366 writeln!(out, "static_assert(");
David Tolnaycc44c7a2020-10-04 13:20:03 -0700367 writeln!(out, " ::std::is_trivially_destructible<{}>::value,", id);
David Tolnay7426cc12020-10-03 19:04:04 -0700368 writeln!(
369 out,
370 " \"type {} marked as Trivial in Rust is not trivially destructible in C++\");",
371 id,
372 );
Adrian Taylor405e8742020-09-25 14:01:25 -0700373}
374
Adrian Taylorc8713432020-10-21 18:20:55 -0700375fn write_exception_glue(out: &mut OutFile, apis: &[&Api]) {
David Tolnayebef4a22020-03-17 15:33:47 -0700376 let mut has_cxx_throws = false;
377 for api in apis {
378 if let Api::CxxFunction(efn) = api {
379 if efn.throws {
380 has_cxx_throws = true;
381 break;
382 }
383 }
384 }
385
386 if has_cxx_throws {
387 out.next_section();
David Tolnaye68634c2020-03-18 12:03:40 -0700388 writeln!(
David Tolnayebef4a22020-03-17 15:33:47 -0700389 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700390 "const char *cxxbridge05$exception(const char *, size_t);",
David Tolnayebef4a22020-03-17 15:33:47 -0700391 );
392 }
393}
394
David Tolnaya7c2ea12020-10-30 21:32:53 -0700395fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, impl_annotations: &Option<String>) {
David Tolnaycc1ae762020-10-31 15:53:50 -0700396 if let Some(annotation) = impl_annotations {
397 write!(out, "{} ", annotation);
Adrian Taylor21f0ff02020-07-21 16:21:48 -0700398 }
David Tolnayebef4a22020-03-17 15:33:47 -0700399 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700400 write!(out, "::rust::repr::PtrLen ");
David Tolnayebef4a22020-03-17 15:33:47 -0700401 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700402 write_extern_return_type_space(out, &efn.ret);
David Tolnayebef4a22020-03-17 15:33:47 -0700403 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700404 let mangled = mangle::extern_fn(efn, out.types);
David Tolnay3caa50a2020-04-19 21:25:34 -0700405 write!(out, "{}(", mangled);
David Tolnaye439c772020-04-20 00:23:55 -0700406 if let Some(receiver) = &efn.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700407 if receiver.mutability.is_none() {
408 write!(out, "const ");
409 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700410 write!(
411 out,
412 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700413 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700414 );
Joel Galenson3d4f6122020-04-07 15:54:05 -0700415 }
David Tolnay7db73692019-10-20 14:51:12 -0400416 for (i, arg) in efn.args.iter().enumerate() {
Joel Galenson3d4f6122020-04-07 15:54:05 -0700417 if i > 0 || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400418 write!(out, ", ");
419 }
David Tolnaya46a2372020-03-06 10:03:48 -0800420 if arg.ty == RustString {
421 write!(out, "const ");
David Tolnay313b10e2020-04-25 16:30:51 -0700422 } else if let Type::RustVec(_) = arg.ty {
423 write!(out, "const ");
David Tolnaya46a2372020-03-06 10:03:48 -0800424 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700425 write_extern_arg(out, arg);
David Tolnay7db73692019-10-20 14:51:12 -0400426 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700427 let indirect_return = indirect_return(efn, out.types);
David Tolnay7db73692019-10-20 14:51:12 -0400428 if indirect_return {
myronahne3b78ea2020-05-23 01:08:13 +0700429 if !efn.args.is_empty() || efn.receiver.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400430 write!(out, ", ");
431 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700432 write_indirect_return_type_space(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400433 write!(out, "*return$");
434 }
435 writeln!(out, ") noexcept {{");
436 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700437 write_return_type(out, &efn.ret);
Joel Galenson3d4f6122020-04-07 15:54:05 -0700438 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700439 None => write!(out, "(*{}$)(", efn.ident.rust),
Adrian Taylorc8713432020-10-21 18:20:55 -0700440 Some(receiver) => write!(
441 out,
442 "({}::*{}$)(",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700443 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700444 efn.ident.rust
445 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700446 }
David Tolnay7db73692019-10-20 14:51:12 -0400447 for (i, arg) in efn.args.iter().enumerate() {
448 if i > 0 {
449 write!(out, ", ");
450 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700451 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400452 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700453 write!(out, ")");
David Tolnay4e7123f2020-04-19 21:11:37 -0700454 if let Some(receiver) = &efn.receiver {
455 if receiver.mutability.is_none() {
456 write!(out, " const");
457 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700458 }
459 write!(out, " = ");
460 match &efn.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700461 None => write!(out, "{}", efn.ident.cxx.to_fully_qualified()),
462 Some(receiver) => write!(
463 out,
464 "&{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700465 out.types.resolve(&receiver.ty).to_fully_qualified(),
Adrian Taylorc8713432020-10-21 18:20:55 -0700466 efn.ident.cxx.ident
467 ),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700468 }
469 writeln!(out, ";");
David Tolnay7db73692019-10-20 14:51:12 -0400470 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700471 if efn.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700472 writeln!(out, "::rust::repr::PtrLen throw$;");
David Tolnay3e3e0af2020-03-17 22:42:49 -0700473 writeln!(out, " ::rust::behavior::trycatch(");
David Tolnay5d121442020-03-17 22:14:40 -0700474 writeln!(out, " [&] {{");
475 write!(out, " ");
David Tolnayebef4a22020-03-17 15:33:47 -0700476 }
David Tolnay7db73692019-10-20 14:51:12 -0400477 if indirect_return {
David Tolnay0ecd05a2020-07-29 16:32:03 -0700478 out.include.new = true;
David Tolnay7db73692019-10-20 14:51:12 -0400479 write!(out, "new (return$) ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700480 write_indirect_return_type(out, efn.ret.as_ref().unwrap());
David Tolnay7db73692019-10-20 14:51:12 -0400481 write!(out, "(");
David Tolnay99642622020-03-25 13:07:35 -0700482 } else if efn.ret.is_some() {
David Tolnay7db73692019-10-20 14:51:12 -0400483 write!(out, "return ");
David Tolnay99642622020-03-25 13:07:35 -0700484 }
485 match &efn.ret {
486 Some(Type::Ref(_)) => write!(out, "&"),
David Tolnay0356d332020-10-31 19:46:41 -0700487 Some(Type::Str(_)) if !indirect_return => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnayeb952ba2020-04-14 15:02:24 -0700488 Some(Type::SliceRefU8(_)) if !indirect_return => {
489 write!(out, "::rust::Slice<uint8_t>::Repr(")
490 }
David Tolnay99642622020-03-25 13:07:35 -0700491 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -0400492 }
Joel Galenson3d4f6122020-04-07 15:54:05 -0700493 match &efn.receiver {
David Tolnaya4641c72020-09-08 14:05:53 -0700494 None => write!(out, "{}$(", efn.ident.rust),
495 Some(_) => write!(out, "(self.*{}$)(", efn.ident.rust),
Joel Galenson3d4f6122020-04-07 15:54:05 -0700496 }
David Tolnay7db73692019-10-20 14:51:12 -0400497 for (i, arg) in efn.args.iter().enumerate() {
498 if i > 0 {
499 write!(out, ", ");
500 }
501 if let Type::RustBox(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700502 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400503 write!(out, "::from_raw({})", arg.ident);
504 } else if let Type::UniquePtr(_) = &arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700505 write_type(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400506 write!(out, "({})", arg.ident);
David Tolnay0356d332020-10-31 19:46:41 -0700507 } else if let Type::Str(_) = arg.ty {
508 write!(
509 out,
510 "::rust::impl<::rust::Str>::new_unchecked({})",
511 arg.ident,
512 );
David Tolnaya46a2372020-03-06 10:03:48 -0800513 } else if arg.ty == RustString {
David Tolnaycc3767f2020-03-06 10:41:51 -0800514 write!(
515 out,
516 "::rust::String(::rust::unsafe_bitcopy, *{})",
517 arg.ident,
518 );
David Tolnay313b10e2020-04-25 16:30:51 -0700519 } else if let Type::RustVec(_) = arg.ty {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700520 write_type(out, &arg.ty);
David Tolnay313b10e2020-04-25 16:30:51 -0700521 write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
David Tolnaya7c2ea12020-10-30 21:32:53 -0700522 } else if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay4791f1c2020-03-17 21:53:16 -0700523 out.include.utility = true;
David Tolnay7e219b82020-03-01 13:14:51 -0800524 write!(out, "::std::move(*{})", arg.ident);
David Tolnay7db73692019-10-20 14:51:12 -0400525 } else {
526 write!(out, "{}", arg.ident);
527 }
528 }
529 write!(out, ")");
530 match &efn.ret {
531 Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
532 Some(Type::UniquePtr(_)) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700533 Some(Type::Str(_)) | Some(Type::SliceRefU8(_)) if !indirect_return => write!(out, ")"),
David Tolnay7db73692019-10-20 14:51:12 -0400534 _ => {}
535 }
536 if indirect_return {
537 write!(out, ")");
538 }
539 writeln!(out, ";");
David Tolnayebef4a22020-03-17 15:33:47 -0700540 if efn.throws {
541 out.include.cstring = true;
David Tolnay5d121442020-03-17 22:14:40 -0700542 writeln!(out, " throw$.ptr = nullptr;");
543 writeln!(out, " }},");
David Tolnay82c16172020-03-17 22:54:12 -0700544 writeln!(out, " [&](const char *catch$) noexcept {{");
David Tolnay5d121442020-03-17 22:14:40 -0700545 writeln!(out, " throw$.len = ::std::strlen(catch$);");
David Tolnayebef4a22020-03-17 15:33:47 -0700546 writeln!(
547 out,
David Tolnay8f16ae72020-10-08 18:21:13 -0700548 " throw$.ptr = cxxbridge05$exception(catch$, throw$.len);",
David Tolnayebef4a22020-03-17 15:33:47 -0700549 );
David Tolnay5d121442020-03-17 22:14:40 -0700550 writeln!(out, " }});");
David Tolnayebef4a22020-03-17 15:33:47 -0700551 writeln!(out, " return throw$;");
552 }
David Tolnay7db73692019-10-20 14:51:12 -0400553 writeln!(out, "}}");
David Tolnay75dca2e2020-03-25 20:17:52 -0700554 for arg in &efn.args {
555 if let Type::Fn(f) = &arg.ty {
556 let var = &arg.ident;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700557 write_function_pointer_trampoline(out, efn, var, f);
David Tolnay75dca2e2020-03-25 20:17:52 -0700558 }
559 }
560}
561
562fn write_function_pointer_trampoline(
563 out: &mut OutFile,
564 efn: &ExternFn,
565 var: &Ident,
566 f: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700567) {
568 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700569 let r_trampoline = mangle::r_trampoline(efn, var, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700570 let indirect_call = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700571 write_rust_function_decl_impl(out, &r_trampoline, f, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700572
573 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700574 let c_trampoline = mangle::c_trampoline(efn, var, out.types).to_string();
575 write_rust_function_shim_impl(out, &c_trampoline, f, &r_trampoline, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400576}
577
David Tolnaya7c2ea12020-10-30 21:32:53 -0700578fn write_rust_function_decl(out: &mut OutFile, efn: &ExternFn, _: &Option<String>) {
579 let link_name = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700580 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700581 write_rust_function_decl_impl(out, &link_name, efn, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700582}
583
584fn write_rust_function_decl_impl(
585 out: &mut OutFile,
David Tolnay891061b2020-04-19 22:42:33 -0700586 link_name: &Symbol,
David Tolnay75dca2e2020-03-25 20:17:52 -0700587 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700588 indirect_call: bool,
589) {
590 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700591 write!(out, "::rust::repr::PtrLen ");
David Tolnay1e548172020-03-16 13:37:09 -0700592 } else {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700593 write_extern_return_type_space(out, &sig.ret);
David Tolnay1e548172020-03-16 13:37:09 -0700594 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700595 write!(out, "{}(", link_name);
596 let mut needs_comma = false;
David Tolnaye439c772020-04-20 00:23:55 -0700597 if let Some(receiver) = &sig.receiver {
David Tolnay86710612020-04-20 00:30:32 -0700598 if receiver.mutability.is_none() {
599 write!(out, "const ");
600 }
Adrian Taylorc8713432020-10-21 18:20:55 -0700601 write!(
602 out,
603 "{} &self",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700604 out.types.resolve(&receiver.ty).to_fully_qualified()
Adrian Taylorc8713432020-10-21 18:20:55 -0700605 );
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700606 needs_comma = true;
607 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700608 for arg in &sig.args {
609 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400610 write!(out, ", ");
611 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700612 write_extern_arg(out, arg);
David Tolnay75dca2e2020-03-25 20:17:52 -0700613 needs_comma = true;
David Tolnay7db73692019-10-20 14:51:12 -0400614 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700615 if indirect_return(sig, out.types) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700616 if needs_comma {
David Tolnay7db73692019-10-20 14:51:12 -0400617 write!(out, ", ");
618 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700619 write_return_type(out, &sig.ret);
David Tolnay7db73692019-10-20 14:51:12 -0400620 write!(out, "*return$");
David Tolnay75dca2e2020-03-25 20:17:52 -0700621 needs_comma = true;
622 }
623 if indirect_call {
624 if needs_comma {
625 write!(out, ", ");
626 }
627 write!(out, "void *");
David Tolnay7db73692019-10-20 14:51:12 -0400628 }
629 writeln!(out, ") noexcept;");
630}
631
David Tolnaya7c2ea12020-10-30 21:32:53 -0700632fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400633 for line in efn.doc.to_string().lines() {
634 writeln!(out, "//{}", line);
635 }
David Tolnaya73853b2020-04-20 01:19:56 -0700636 let local_name = match &efn.sig.receiver {
Adrian Taylorc8713432020-10-21 18:20:55 -0700637 None => efn.ident.cxx.ident.to_string(),
638 Some(receiver) => format!(
639 "{}::{}",
David Tolnaya7c2ea12020-10-30 21:32:53 -0700640 out.types.resolve(&receiver.ty).ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700641 efn.ident.cxx.ident
642 ),
David Tolnaya73853b2020-04-20 01:19:56 -0700643 };
David Tolnaya7c2ea12020-10-30 21:32:53 -0700644 let invoke = mangle::extern_fn(efn, out.types);
David Tolnay75dca2e2020-03-25 20:17:52 -0700645 let indirect_call = false;
David Tolnaya7c2ea12020-10-30 21:32:53 -0700646 write_rust_function_shim_impl(out, &local_name, efn, &invoke, indirect_call);
David Tolnay75dca2e2020-03-25 20:17:52 -0700647}
648
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700649fn write_rust_function_shim_decl(
David Tolnay75dca2e2020-03-25 20:17:52 -0700650 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700651 local_name: &str,
David Tolnay75dca2e2020-03-25 20:17:52 -0700652 sig: &Signature,
David Tolnay75dca2e2020-03-25 20:17:52 -0700653 indirect_call: bool,
654) {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700655 write_return_type(out, &sig.ret);
David Tolnay75dca2e2020-03-25 20:17:52 -0700656 write!(out, "{}(", local_name);
657 for (i, arg) in sig.args.iter().enumerate() {
David Tolnay7db73692019-10-20 14:51:12 -0400658 if i > 0 {
659 write!(out, ", ");
660 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700661 write_type_space(out, &arg.ty);
David Tolnay7db73692019-10-20 14:51:12 -0400662 write!(out, "{}", arg.ident);
663 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700664 if indirect_call {
665 if !sig.args.is_empty() {
666 write!(out, ", ");
667 }
668 write!(out, "void *extern$");
669 }
David Tolnay1e548172020-03-16 13:37:09 -0700670 write!(out, ")");
David Tolnay86710612020-04-20 00:30:32 -0700671 if let Some(receiver) = &sig.receiver {
672 if receiver.mutability.is_none() {
673 write!(out, " const");
674 }
675 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700676 if !sig.throws {
David Tolnay1e548172020-03-16 13:37:09 -0700677 write!(out, " noexcept");
678 }
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700679}
680
681fn write_rust_function_shim_impl(
682 out: &mut OutFile,
David Tolnaya73853b2020-04-20 01:19:56 -0700683 local_name: &str,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700684 sig: &Signature,
David Tolnay891061b2020-04-19 22:42:33 -0700685 invoke: &Symbol,
Joel Galensonc1c4e7a2020-04-15 10:21:00 -0700686 indirect_call: bool,
687) {
688 if out.header && sig.receiver.is_some() {
689 // We've already defined this inside the struct.
690 return;
691 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700692 write_rust_function_shim_decl(out, local_name, sig, indirect_call);
David Tolnay7db73692019-10-20 14:51:12 -0400693 if out.header {
694 writeln!(out, ";");
David Tolnay439cde22020-04-20 00:46:25 -0700695 return;
David Tolnay7db73692019-10-20 14:51:12 -0400696 }
David Tolnay439cde22020-04-20 00:46:25 -0700697 writeln!(out, " {{");
698 for arg in &sig.args {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700699 if arg.ty != RustString && out.types.needs_indirect_abi(&arg.ty) {
David Tolnay439cde22020-04-20 00:46:25 -0700700 out.include.utility = true;
701 write!(out, " ::rust::ManuallyDrop<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700702 write_type(out, &arg.ty);
David Tolnay439cde22020-04-20 00:46:25 -0700703 writeln!(out, "> {}$(::std::move({0}));", arg.ident);
704 }
705 }
706 write!(out, " ");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700707 let indirect_return = indirect_return(sig, out.types);
David Tolnay439cde22020-04-20 00:46:25 -0700708 if indirect_return {
709 write!(out, "::rust::MaybeUninit<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700710 write_type(out, sig.ret.as_ref().unwrap());
David Tolnay439cde22020-04-20 00:46:25 -0700711 writeln!(out, "> return$;");
712 write!(out, " ");
713 } else if let Some(ret) = &sig.ret {
714 write!(out, "return ");
715 match ret {
716 Type::RustBox(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700717 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700718 write!(out, "::from_raw(");
719 }
720 Type::UniquePtr(_) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700721 write_type(out, ret);
David Tolnay439cde22020-04-20 00:46:25 -0700722 write!(out, "(");
723 }
724 Type::Ref(_) => write!(out, "*"),
David Tolnay0356d332020-10-31 19:46:41 -0700725 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::new_unchecked("),
David Tolnay439cde22020-04-20 00:46:25 -0700726 _ => {}
727 }
728 }
729 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700730 write!(out, "::rust::repr::PtrLen error$ = ");
David Tolnay439cde22020-04-20 00:46:25 -0700731 }
732 write!(out, "{}(", invoke);
733 if sig.receiver.is_some() {
734 write!(out, "*this");
735 }
736 for (i, arg) in sig.args.iter().enumerate() {
737 if i > 0 || sig.receiver.is_some() {
738 write!(out, ", ");
739 }
740 match &arg.ty {
David Tolnay0356d332020-10-31 19:46:41 -0700741 Type::Str(_) => write!(out, "::rust::impl<::rust::Str>::repr("),
David Tolnay439cde22020-04-20 00:46:25 -0700742 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr("),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700743 ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
David Tolnay439cde22020-04-20 00:46:25 -0700744 _ => {}
745 }
746 write!(out, "{}", arg.ident);
747 match &arg.ty {
748 Type::RustBox(_) => write!(out, ".into_raw()"),
749 Type::UniquePtr(_) => write!(out, ".release()"),
David Tolnay0356d332020-10-31 19:46:41 -0700750 Type::Str(_) | Type::SliceRefU8(_) => write!(out, ")"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700751 ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
David Tolnay439cde22020-04-20 00:46:25 -0700752 _ => {}
753 }
754 }
755 if indirect_return {
756 if !sig.args.is_empty() {
757 write!(out, ", ");
758 }
759 write!(out, "&return$.value");
760 }
761 if indirect_call {
762 if !sig.args.is_empty() || indirect_return {
763 write!(out, ", ");
764 }
765 write!(out, "extern$");
766 }
767 write!(out, ")");
David Tolnay22602b42020-09-21 18:04:05 -0400768 if !indirect_return {
769 if let Some(ret) = &sig.ret {
David Tolnay0356d332020-10-31 19:46:41 -0700770 if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) = ret {
David Tolnay22602b42020-09-21 18:04:05 -0400771 write!(out, ")");
772 }
David Tolnay439cde22020-04-20 00:46:25 -0700773 }
774 }
775 writeln!(out, ";");
776 if sig.throws {
David Tolnayd68dfa82020-10-31 16:01:24 -0700777 writeln!(out, " if (error$.ptr) {{");
David Tolnay84ddf9e2020-10-31 15:36:48 -0700778 writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
David Tolnay439cde22020-04-20 00:46:25 -0700779 writeln!(out, " }}");
780 }
781 if indirect_return {
782 out.include.utility = true;
783 writeln!(out, " return ::std::move(return$.value);");
784 }
785 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -0400786}
787
David Tolnaya7c2ea12020-10-30 21:32:53 -0700788fn write_return_type(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400789 match ty {
790 None => write!(out, "void "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700791 Some(ty) => write_type_space(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400792 }
793}
794
David Tolnay75dca2e2020-03-25 20:17:52 -0700795fn indirect_return(sig: &Signature, types: &Types) -> bool {
796 sig.ret
David Tolnay277e3cc2020-03-17 00:11:01 -0700797 .as_ref()
David Tolnay75dca2e2020-03-25 20:17:52 -0700798 .map_or(false, |ret| sig.throws || types.needs_indirect_abi(ret))
David Tolnay277e3cc2020-03-17 00:11:01 -0700799}
800
David Tolnaya7c2ea12020-10-30 21:32:53 -0700801fn write_indirect_return_type(out: &mut OutFile, ty: &Type) {
David Tolnay99642622020-03-25 13:07:35 -0700802 match ty {
803 Type::RustBox(ty) | Type::UniquePtr(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700804 write_type_space(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700805 write!(out, "*");
806 }
807 Type::Ref(ty) => {
808 if ty.mutability.is_none() {
809 write!(out, "const ");
810 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700811 write_type(out, &ty.inner);
David Tolnay99642622020-03-25 13:07:35 -0700812 write!(out, " *");
813 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700814 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr"),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700815 _ => write_type(out, ty),
David Tolnay99642622020-03-25 13:07:35 -0700816 }
817}
818
David Tolnaya7c2ea12020-10-30 21:32:53 -0700819fn write_indirect_return_type_space(out: &mut OutFile, ty: &Type) {
820 write_indirect_return_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700821 match ty {
822 Type::RustBox(_) | Type::UniquePtr(_) | Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700823 Type::Str(_) | Type::SliceRefU8(_) => write!(out, " "),
David Tolnay99642622020-03-25 13:07:35 -0700824 _ => write_space_after_type(out, ty),
825 }
826}
827
David Tolnaya7c2ea12020-10-30 21:32:53 -0700828fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
David Tolnay7db73692019-10-20 14:51:12 -0400829 match ty {
830 Some(Type::RustBox(ty)) | Some(Type::UniquePtr(ty)) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700831 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400832 write!(out, "*");
833 }
David Tolnay4a441222020-01-25 16:24:27 -0800834 Some(Type::Ref(ty)) => {
835 if ty.mutability.is_none() {
836 write!(out, "const ");
837 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700838 write_type(out, &ty.inner);
David Tolnay4a441222020-01-25 16:24:27 -0800839 write!(out, " *");
840 }
David Tolnay0356d332020-10-31 19:46:41 -0700841 Some(Type::Str(_)) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700842 Some(Type::SliceRefU8(_)) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700843 Some(ty) if out.types.needs_indirect_abi(ty) => write!(out, "void "),
844 _ => write_return_type(out, ty),
David Tolnay7db73692019-10-20 14:51:12 -0400845 }
846}
847
David Tolnaya7c2ea12020-10-30 21:32:53 -0700848fn write_extern_arg(out: &mut OutFile, arg: &Var) {
David Tolnay7db73692019-10-20 14:51:12 -0400849 match &arg.ty {
David Tolnay4377a9e2020-04-24 15:20:26 -0700850 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700851 write_type_space(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400852 write!(out, "*");
853 }
David Tolnay0356d332020-10-31 19:46:41 -0700854 Type::Str(_) => write!(out, "::rust::repr::PtrLen "),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700855 Type::SliceRefU8(_) => write!(out, "::rust::Slice<uint8_t>::Repr "),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700856 _ => write_type_space(out, &arg.ty),
David Tolnay7db73692019-10-20 14:51:12 -0400857 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700858 if out.types.needs_indirect_abi(&arg.ty) {
David Tolnay7db73692019-10-20 14:51:12 -0400859 write!(out, "*");
860 }
861 write!(out, "{}", arg.ident);
862}
863
David Tolnaya7c2ea12020-10-30 21:32:53 -0700864fn write_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400865 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700866 Type::Ident(ident) => match Atom::from(&ident.rust) {
David Tolnayf6a89f22020-05-10 23:39:27 -0700867 Some(atom) => write_atom(out, atom),
David Tolnaya7c2ea12020-10-30 21:32:53 -0700868 None => write!(out, "{}", out.types.resolve(ident).to_fully_qualified()),
David Tolnay7db73692019-10-20 14:51:12 -0400869 },
870 Type::RustBox(ty) => {
David Tolnay750755e2020-03-01 13:04:08 -0800871 write!(out, "::rust::Box<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700872 write_type(out, &ty.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400873 write!(out, ">");
874 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700875 Type::RustVec(ty) => {
876 write!(out, "::rust::Vec<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700877 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700878 write!(out, ">");
879 }
David Tolnay7db73692019-10-20 14:51:12 -0400880 Type::UniquePtr(ptr) => {
David Tolnay7e219b82020-03-01 13:14:51 -0800881 write!(out, "::std::unique_ptr<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700882 write_type(out, &ptr.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400883 write!(out, ">");
884 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700885 Type::CxxVector(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +0700886 write!(out, "::std::vector<");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700887 write_type(out, &ty.inner);
Myron Ahneba35cf2020-02-05 19:41:51 +0700888 write!(out, ">");
889 }
David Tolnay7db73692019-10-20 14:51:12 -0400890 Type::Ref(r) => {
891 if r.mutability.is_none() {
892 write!(out, "const ");
893 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700894 write_type(out, &r.inner);
David Tolnay7db73692019-10-20 14:51:12 -0400895 write!(out, " &");
896 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700897 Type::Slice(_) => {
898 // For now, only U8 slices are supported, which are covered separately below
899 unreachable!()
900 }
David Tolnay7db73692019-10-20 14:51:12 -0400901 Type::Str(_) => {
David Tolnay750755e2020-03-01 13:04:08 -0800902 write!(out, "::rust::Str");
David Tolnay7db73692019-10-20 14:51:12 -0400903 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700904 Type::SliceRefU8(_) => {
905 write!(out, "::rust::Slice<uint8_t>");
906 }
David Tolnay75dca2e2020-03-25 20:17:52 -0700907 Type::Fn(f) => {
908 write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
909 match &f.ret {
David Tolnaya7c2ea12020-10-30 21:32:53 -0700910 Some(ret) => write_type(out, ret),
David Tolnay75dca2e2020-03-25 20:17:52 -0700911 None => write!(out, "void"),
912 }
913 write!(out, "(");
914 for (i, arg) in f.args.iter().enumerate() {
915 if i > 0 {
916 write!(out, ", ");
917 }
David Tolnaya7c2ea12020-10-30 21:32:53 -0700918 write_type(out, &arg.ty);
David Tolnay75dca2e2020-03-25 20:17:52 -0700919 }
920 write!(out, ")>");
921 }
David Tolnay2fb14e92020-03-15 23:11:38 -0700922 Type::Void(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400923 }
924}
925
David Tolnayf6a89f22020-05-10 23:39:27 -0700926fn write_atom(out: &mut OutFile, atom: Atom) {
927 match atom {
928 Bool => write!(out, "bool"),
929 U8 => write!(out, "uint8_t"),
930 U16 => write!(out, "uint16_t"),
931 U32 => write!(out, "uint32_t"),
932 U64 => write!(out, "uint64_t"),
933 Usize => write!(out, "size_t"),
934 I8 => write!(out, "int8_t"),
935 I16 => write!(out, "int16_t"),
936 I32 => write!(out, "int32_t"),
937 I64 => write!(out, "int64_t"),
938 Isize => write!(out, "::rust::isize"),
939 F32 => write!(out, "float"),
940 F64 => write!(out, "double"),
941 CxxString => write!(out, "::std::string"),
942 RustString => write!(out, "::rust::String"),
943 }
944}
945
David Tolnaya7c2ea12020-10-30 21:32:53 -0700946fn write_type_space(out: &mut OutFile, ty: &Type) {
947 write_type(out, ty);
David Tolnay99642622020-03-25 13:07:35 -0700948 write_space_after_type(out, ty);
949}
950
951fn write_space_after_type(out: &mut OutFile, ty: &Type) {
David Tolnay7db73692019-10-20 14:51:12 -0400952 match ty {
David Tolnayeb952ba2020-04-14 15:02:24 -0700953 Type::Ident(_)
954 | Type::RustBox(_)
955 | Type::UniquePtr(_)
956 | Type::Str(_)
David Tolnay4377a9e2020-04-24 15:20:26 -0700957 | Type::CxxVector(_)
Myron Ahneba35cf2020-02-05 19:41:51 +0700958 | Type::RustVec(_)
David Tolnayeb952ba2020-04-14 15:02:24 -0700959 | Type::SliceRefU8(_)
960 | Type::Fn(_) => write!(out, " "),
David Tolnay7db73692019-10-20 14:51:12 -0400961 Type::Ref(_) => {}
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700962 Type::Void(_) | Type::Slice(_) => unreachable!(),
David Tolnay7db73692019-10-20 14:51:12 -0400963 }
964}
965
David Tolnaycd08c442020-04-25 10:16:33 -0700966// Only called for legal referent types of unique_ptr and element types of
967// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -0700968fn to_typename(ty: &Type, types: &Types) -> String {
David Tolnay2eca4a02020-04-24 19:50:51 -0700969 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700970 Type::Ident(ident) => types.resolve(&ident).to_fully_qualified(),
971 Type::CxxVector(ptr) => format!("::std::vector<{}>", to_typename(&ptr.inner, types)),
David Tolnaycd08c442020-04-25 10:16:33 -0700972 _ => unreachable!(),
David Tolnay2eca4a02020-04-24 19:50:51 -0700973 }
974}
975
David Tolnayacdf20a2020-04-25 12:40:53 -0700976// Only called for legal referent types of unique_ptr and element types of
977// std::vector and Vec.
Adrian Taylorc8713432020-10-21 18:20:55 -0700978fn to_mangled(ty: &Type, types: &Types) -> Symbol {
David Tolnaybae50ef2020-04-25 12:38:41 -0700979 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700980 Type::Ident(ident) => ident.to_symbol(types),
981 Type::CxxVector(ptr) => to_mangled(&ptr.inner, types).prefix_with("std$vector$"),
David Tolnayacdf20a2020-04-25 12:40:53 -0700982 _ => unreachable!(),
David Tolnaybae50ef2020-04-25 12:38:41 -0700983 }
984}
985
David Tolnaya7c2ea12020-10-30 21:32:53 -0700986fn write_generic_instantiations(out: &mut OutFile) {
David Tolnay7db73692019-10-20 14:51:12 -0400987 out.begin_block("extern \"C\"");
David Tolnaya7c2ea12020-10-30 21:32:53 -0700988 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -0400989 if let Type::RustBox(ty) = ty {
990 if let Type::Ident(inner) = &ty.inner {
991 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700992 write_rust_box_extern(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -0400993 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700994 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -0700995 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700996 if Atom::from(&inner.rust).is_none() {
David Tolnay6787be62020-04-25 11:01:02 -0700997 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -0700998 write_rust_vec_extern(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -0700999 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001000 }
David Tolnay7db73692019-10-20 14:51:12 -04001001 } else if let Type::UniquePtr(ptr) = ty {
1002 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001003 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001004 && (!out.types.aliases.contains_key(&inner.rust)
1005 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001006 {
David Tolnay7db73692019-10-20 14:51:12 -04001007 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001008 write_unique_ptr(out, inner);
Myron Ahneba35cf2020-02-05 19:41:51 +07001009 }
1010 }
David Tolnay4377a9e2020-04-24 15:20:26 -07001011 } else if let Type::CxxVector(ptr) = ty {
Myron Ahneba35cf2020-02-05 19:41:51 +07001012 if let Type::Ident(inner) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001013 if Atom::from(&inner.rust).is_none()
David Tolnaya7c2ea12020-10-30 21:32:53 -07001014 && (!out.types.aliases.contains_key(&inner.rust)
1015 || out.types.explicit_impls.contains(ty))
David Tolnay7e69f892020-10-03 22:20:22 -07001016 {
Myron Ahneba35cf2020-02-05 19:41:51 +07001017 out.next_section();
David Tolnaya7c2ea12020-10-30 21:32:53 -07001018 write_cxx_vector(out, ty, inner);
David Tolnay7db73692019-10-20 14:51:12 -04001019 }
1020 }
1021 }
1022 }
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001023 out.end_block("extern \"C\"");
David Tolnay7db73692019-10-20 14:51:12 -04001024
David Tolnay750755e2020-03-01 13:04:08 -08001025 out.begin_block("namespace rust");
David Tolnay8f16ae72020-10-08 18:21:13 -07001026 out.begin_block("inline namespace cxxbridge05");
David Tolnaya7c2ea12020-10-30 21:32:53 -07001027 for ty in out.types {
David Tolnay7db73692019-10-20 14:51:12 -04001028 if let Type::RustBox(ty) = ty {
1029 if let Type::Ident(inner) = &ty.inner {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001030 write_rust_box_impl(out, &out.types.resolve(&inner));
David Tolnay7db73692019-10-20 14:51:12 -04001031 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001032 } else if let Type::RustVec(ty) = ty {
David Tolnay6787be62020-04-25 11:01:02 -07001033 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -07001034 if Atom::from(&inner.rust).is_none() {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001035 write_rust_vec_impl(out, inner);
David Tolnay6787be62020-04-25 11:01:02 -07001036 }
Myron Ahneba35cf2020-02-05 19:41:51 +07001037 }
David Tolnay7db73692019-10-20 14:51:12 -04001038 }
1039 }
David Tolnay8f16ae72020-10-08 18:21:13 -07001040 out.end_block("namespace cxxbridge05");
David Tolnay9ad1fbc2020-03-01 14:01:24 -08001041 out.end_block("namespace rust");
David Tolnay7db73692019-10-20 14:51:12 -04001042}
1043
Adrian Taylorc8713432020-10-21 18:20:55 -07001044fn write_rust_box_extern(out: &mut OutFile, ident: &CppName) {
1045 let inner = ident.to_fully_qualified();
1046 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001047
David Tolnay8f16ae72020-10-08 18:21:13 -07001048 writeln!(out, "#ifndef CXXBRIDGE05_RUST_BOX_{}", instance);
1049 writeln!(out, "#define CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001050 writeln!(
1051 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001052 "void cxxbridge05$box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001053 instance, inner,
1054 );
1055 writeln!(
1056 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001057 "void cxxbridge05$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
David Tolnay7db73692019-10-20 14:51:12 -04001058 instance, inner,
1059 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001060 writeln!(out, "#endif // CXXBRIDGE05_RUST_BOX_{}", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001061}
1062
David Tolnaya7c2ea12020-10-30 21:32:53 -07001063fn write_rust_vec_extern(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001064 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001065 let inner = to_typename(&element, out.types);
1066 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001067
David Tolnay8f16ae72020-10-08 18:21:13 -07001068 writeln!(out, "#ifndef CXXBRIDGE05_RUST_VEC_{}", instance);
1069 writeln!(out, "#define CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001070 writeln!(
1071 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001072 "void cxxbridge05$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
David Tolnayf97c2d52020-04-25 16:37:48 -07001073 instance, inner,
1074 );
1075 writeln!(
1076 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001077 "void cxxbridge05$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001078 instance, inner,
1079 );
1080 writeln!(
1081 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001082 "size_t cxxbridge05$rust_vec${}$len(const ::rust::Vec<{}> *ptr) noexcept;",
Myron Ahneba35cf2020-02-05 19:41:51 +07001083 instance, inner,
1084 );
David Tolnay219c0792020-04-24 20:31:37 -07001085 writeln!(
1086 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001087 "const {} *cxxbridge05$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
David Tolnay219c0792020-04-24 20:31:37 -07001088 inner, instance,
1089 );
David Tolnay503d0192020-04-24 22:18:56 -07001090 writeln!(
1091 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001092 "size_t cxxbridge05$rust_vec${}$stride() noexcept;",
David Tolnay503d0192020-04-24 22:18:56 -07001093 instance,
1094 );
David Tolnay8f16ae72020-10-08 18:21:13 -07001095 writeln!(out, "#endif // CXXBRIDGE05_RUST_VEC_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001096}
1097
Adrian Taylorc8713432020-10-21 18:20:55 -07001098fn write_rust_box_impl(out: &mut OutFile, ident: &CppName) {
1099 let inner = ident.to_fully_qualified();
1100 let instance = ident.to_symbol();
David Tolnay7db73692019-10-20 14:51:12 -04001101
1102 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001103 writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001104 writeln!(out, " cxxbridge05$box${}$uninit(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001105 writeln!(out, "}}");
1106
1107 writeln!(out, "template <>");
David Tolnay324437a2020-03-01 13:02:24 -08001108 writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001109 writeln!(out, " cxxbridge05$box${}$drop(this);", instance);
David Tolnay7db73692019-10-20 14:51:12 -04001110 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001111}
1112
David Tolnaya7c2ea12020-10-30 21:32:53 -07001113fn write_rust_vec_impl(out: &mut OutFile, element: &ResolvableName) {
David Tolnay6787be62020-04-25 11:01:02 -07001114 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001115 let inner = to_typename(&element, out.types);
1116 let instance = to_mangled(&element, out.types);
David Tolnay4791f1c2020-03-17 21:53:16 -07001117
Myron Ahneba35cf2020-02-05 19:41:51 +07001118 writeln!(out, "template <>");
David Tolnayf97c2d52020-04-25 16:37:48 -07001119 writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001120 writeln!(out, " cxxbridge05$rust_vec${}$new(this);", instance);
David Tolnayf97c2d52020-04-25 16:37:48 -07001121 writeln!(out, "}}");
1122
1123 writeln!(out, "template <>");
Myron Ahneba35cf2020-02-05 19:41:51 +07001124 writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
1125 writeln!(
1126 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001127 " return cxxbridge05$rust_vec${}$drop(this);",
David Tolnay85db5a02020-04-25 13:17:27 -07001128 instance,
Myron Ahneba35cf2020-02-05 19:41:51 +07001129 );
1130 writeln!(out, "}}");
1131
1132 writeln!(out, "template <>");
1133 writeln!(out, "size_t Vec<{}>::size() const noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001134 writeln!(out, " return cxxbridge05$rust_vec${}$len(this);", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001135 writeln!(out, "}}");
David Tolnay219c0792020-04-24 20:31:37 -07001136
1137 writeln!(out, "template <>");
1138 writeln!(out, "const {} *Vec<{0}>::data() const noexcept {{", inner);
1139 writeln!(
1140 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001141 " return cxxbridge05$rust_vec${}$data(this);",
David Tolnay219c0792020-04-24 20:31:37 -07001142 instance,
1143 );
1144 writeln!(out, "}}");
David Tolnay503d0192020-04-24 22:18:56 -07001145
1146 writeln!(out, "template <>");
1147 writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
David Tolnay8f16ae72020-10-08 18:21:13 -07001148 writeln!(out, " return cxxbridge05$rust_vec${}$stride();", instance);
David Tolnay503d0192020-04-24 22:18:56 -07001149 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001150}
1151
David Tolnaya7c2ea12020-10-30 21:32:53 -07001152fn write_unique_ptr(out: &mut OutFile, ident: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001153 let ty = Type::Ident(ident.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001154 let instance = to_mangled(&ty, out.types);
David Tolnay63da4d32020-04-25 09:41:12 -07001155
David Tolnay8f16ae72020-10-08 18:21:13 -07001156 writeln!(out, "#ifndef CXXBRIDGE05_UNIQUE_PTR_{}", instance);
1157 writeln!(out, "#define CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001158
David Tolnaya7c2ea12020-10-30 21:32:53 -07001159 write_unique_ptr_common(out, &ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001160
David Tolnay8f16ae72020-10-08 18:21:13 -07001161 writeln!(out, "#endif // CXXBRIDGE05_UNIQUE_PTR_{}", instance);
David Tolnay63da4d32020-04-25 09:41:12 -07001162}
1163
1164// Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
David Tolnaya7c2ea12020-10-30 21:32:53 -07001165fn write_unique_ptr_common(out: &mut OutFile, ty: &Type) {
David Tolnay0ecd05a2020-07-29 16:32:03 -07001166 out.include.new = true;
Myron Ahneba35cf2020-02-05 19:41:51 +07001167 out.include.utility = true;
David Tolnaya7c2ea12020-10-30 21:32:53 -07001168 let inner = to_typename(ty, out.types);
1169 let instance = to_mangled(ty, out.types);
David Tolnay7db73692019-10-20 14:51:12 -04001170
David Tolnay63da4d32020-04-25 09:41:12 -07001171 let can_construct_from_value = match ty {
David Tolnayca0f9da2020-10-16 13:16:17 -07001172 // Some aliases are to opaque types; some are to trivial types. We can't
1173 // know at code generation time, so we generate both C++ and Rust side
1174 // bindings for a "new" method anyway. But the Rust code can't be called
1175 // for Opaque types because the 'new' method is not implemented.
1176 Type::Ident(ident) => {
David Tolnaya7c2ea12020-10-30 21:32:53 -07001177 out.types.structs.contains_key(&ident.rust)
1178 || out.types.aliases.contains_key(&ident.rust)
David Tolnayca0f9da2020-10-16 13:16:17 -07001179 }
David Tolnay63da4d32020-04-25 09:41:12 -07001180 _ => false,
1181 };
1182
David Tolnay7db73692019-10-20 14:51:12 -04001183 writeln!(
1184 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001185 "static_assert(sizeof(::std::unique_ptr<{}>) == sizeof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001186 inner,
1187 );
1188 writeln!(
1189 out,
David Tolnay7e219b82020-03-01 13:14:51 -08001190 "static_assert(alignof(::std::unique_ptr<{}>) == alignof(void *), \"\");",
David Tolnay7db73692019-10-20 14:51:12 -04001191 inner,
1192 );
1193 writeln!(
1194 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001195 "void cxxbridge05$unique_ptr${}$null(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001196 instance, inner,
1197 );
David Tolnay7e219b82020-03-01 13:14:51 -08001198 writeln!(out, " new (ptr) ::std::unique_ptr<{}>();", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001199 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001200 if can_construct_from_value {
1201 writeln!(
David Tolnay53838912020-04-09 20:56:44 -07001202 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001203 "void cxxbridge05$unique_ptr${}$new(::std::unique_ptr<{}> *ptr, {} *value) noexcept {{",
David Tolnay53838912020-04-09 20:56:44 -07001204 instance, inner, inner,
1205 );
David Tolnay63da4d32020-04-25 09:41:12 -07001206 writeln!(
1207 out,
1208 " new (ptr) ::std::unique_ptr<{}>(new {}(::std::move(*value)));",
1209 inner, inner,
1210 );
1211 writeln!(out, "}}");
David Tolnay53838912020-04-09 20:56:44 -07001212 }
David Tolnay7db73692019-10-20 14:51:12 -04001213 writeln!(
1214 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001215 "void cxxbridge05$unique_ptr${}$raw(::std::unique_ptr<{}> *ptr, {} *raw) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001216 instance, inner, inner,
1217 );
David Tolnay7e219b82020-03-01 13:14:51 -08001218 writeln!(out, " new (ptr) ::std::unique_ptr<{}>(raw);", inner);
David Tolnay7db73692019-10-20 14:51:12 -04001219 writeln!(out, "}}");
1220 writeln!(
1221 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001222 "const {} *cxxbridge05$unique_ptr${}$get(const ::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001223 inner, instance, inner,
1224 );
1225 writeln!(out, " return ptr.get();");
1226 writeln!(out, "}}");
1227 writeln!(
1228 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001229 "{} *cxxbridge05$unique_ptr${}$release(::std::unique_ptr<{}>& ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001230 inner, instance, inner,
1231 );
1232 writeln!(out, " return ptr.release();");
1233 writeln!(out, "}}");
1234 writeln!(
1235 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001236 "void cxxbridge05$unique_ptr${}$drop(::std::unique_ptr<{}> *ptr) noexcept {{",
David Tolnay7db73692019-10-20 14:51:12 -04001237 instance, inner,
1238 );
1239 writeln!(out, " ptr->~unique_ptr();");
1240 writeln!(out, "}}");
David Tolnay7db73692019-10-20 14:51:12 -04001241}
Myron Ahneba35cf2020-02-05 19:41:51 +07001242
David Tolnaya7c2ea12020-10-30 21:32:53 -07001243fn write_cxx_vector(out: &mut OutFile, vector_ty: &Type, element: &ResolvableName) {
David Tolnay63da4d32020-04-25 09:41:12 -07001244 let element = Type::Ident(element.clone());
David Tolnaya7c2ea12020-10-30 21:32:53 -07001245 let inner = to_typename(&element, out.types);
1246 let instance = to_mangled(&element, out.types);
Myron Ahneba35cf2020-02-05 19:41:51 +07001247
David Tolnay8f16ae72020-10-08 18:21:13 -07001248 writeln!(out, "#ifndef CXXBRIDGE05_VECTOR_{}", instance);
1249 writeln!(out, "#define CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001250 writeln!(
1251 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001252 "size_t cxxbridge05$std$vector${}$size(const ::std::vector<{}> &s) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001253 instance, inner,
1254 );
1255 writeln!(out, " return s.size();");
1256 writeln!(out, "}}");
Myron Ahneba35cf2020-02-05 19:41:51 +07001257 writeln!(
1258 out,
David Tolnay8f16ae72020-10-08 18:21:13 -07001259 "const {} *cxxbridge05$std$vector${}$get_unchecked(const ::std::vector<{}> &s, size_t pos) noexcept {{",
Myron Ahneba35cf2020-02-05 19:41:51 +07001260 inner, instance, inner,
1261 );
David Tolnayb3fcf7b2020-04-30 22:58:28 -07001262 writeln!(out, " return &s[pos];");
Myron Ahneba35cf2020-02-05 19:41:51 +07001263 writeln!(out, "}}");
David Tolnay63da4d32020-04-25 09:41:12 -07001264
David Tolnaya7c2ea12020-10-30 21:32:53 -07001265 write_unique_ptr_common(out, vector_ty);
David Tolnay63da4d32020-04-25 09:41:12 -07001266
David Tolnay8f16ae72020-10-08 18:21:13 -07001267 writeln!(out, "#endif // CXXBRIDGE05_VECTOR_{}", instance);
Myron Ahneba35cf2020-02-05 19:41:51 +07001268}